​​Using Provable Things to retrieve an exchange price from the Smart Contract

dennis j
HARA Engineering
Published in
5 min readSep 20, 2019

G etting external data from any Web API is a non-trivial element in smart contract development. External data provides various benefits for blockchain such as generating a random number and getting real-time information from the internet. One way of getting real-time information from the internet is to use Provable. Provable, previously know as Oraclize, allows smart contracts to obtain data from any web API onto the blockchain, enriching the blockchain ecosystem with data from the internet.

This article will guide you to retrieve an exchange price from Coingecko API, an exchange price which in this article I will use Bitcoin as an example. The first part of this article gives brief instructions on how to set up the Truffle. Part two of this article provides and briefly explain about smart contracts you need to interact with Provable. The third part guides you on how to setup Docker. The last one is the most exciting part, it is when you can finally test whether your smart contracts have successfully retrieved the Bitcoin price using Provable.

Before moving on, I truly recommend that you’re already familiar with the technology or software listed below to get the most out of this article.

Prerequisites:

Let’s begin!

TL;DR

  1. Git clone this repo and cd to the newly created folder. Run git clone https://github.com/djuanit0x/provable-bitcoin-price && cd provable-bitcoin-price
  2. Start Docker on your local machine directly from your terminal or Docker Desktop
  3. Run a script chmod +x tldr.sh && ./tldr.sh Note: make sure you are the same directory as the script.
  4. Wait a couple of minutes and you will see the Bitcoin Price.

Part 1: Truffle Setup

In a nutshell, Truffle is a smart contract development framework for Ethereum. This framework offers various features, such as custom deployments, automated contract testing, and more. I personally use this framework to make the smart contract deployment easier and quicker.

To install Truffle locally runs npm i -g truffle@5.0.0-beta.2 from your . command line.

Note: I use version 5.0.0-beta.2, because it is the same version as the truffle version used for the docker image later on this article.

Then, run truffle -vto check whether truffle has successfully been installed in your computer.

Note: you don’t really need to install Truffle globally to finish this article, because you will use docker to create the image of truffle in your computer.

Following that, create an empty directory on your computer, and execute this command truffle init.

Once the operation has completed, your folder will now have the following items:

Next, you only need to delete everything in truffle-config.js and replace it with the content in the file below.

The code above is the truffle configuration that you need to retrieve the Bitcoin price.

Part 2: Implementation of Smart Contracts

You need to create 3 different contracts in order to obtain the price of Bitcoin.

All of the three contracts will be placed in /contracts directory.

  • BitcoinPrice.sol: A contract that uses the Provable API to retrieve the Bitcoin price.
  • Migrations.sol: A required contract that you need to use the Migration feature in the Truffle Framework.
  • provableAPI_0.5.sol: A contract that allows other smart contracts to get external data.

Note: You don’t need to create Migrations.sol contract as the command truffle init that you have used before, has generated that Migrations.sol contract.

Important Lines

[2–3] The statementis usingProvable will inherit every feature in which defined in the provableAPI file, allowing the contract to interact with the Provable services.

[15–23] A function that will be called by the provable, carrying the result from the Coin Gecko’s API. In this case, it’s the bitcoin price at the time the function is called.

[25–34] A function that will make a request to Provable. The provable then fetched the bitcoin price from Coin Gecko’s API.

Note: One big gotcha when using Provable is your smart contract will not be changed by Provable for the first request of data using the default gas parameters. To understand more about pricing in Provable, you can take a look at the provable documentation

Part 3: Docker Setup

In this article, the main reason that Docker is used is to retrieve the Bitcoin price in your laptop. One of the usability of Docker to allow any application or software to run anywhere and always stay the same regardless of the infrastructure.

Initially, you have to start Docker on your local machine from your terminal or Docker Desktop.

Prepare two Docker files that will create custom images that you need to retrieve the Bitcoin price. Note: To learn more about what an image is in Docker, you can visit this link.

Truffle Image

Ethereum Bridge Image

Notice that on line 17 to 18, I add the wait-for-it script to ensure that the Ganache service will run before the bridge service.

Be sure to place the 2 image files and wait-for-it script in your root project directory.

The last thing that you have to do is to create your Docker Compose file in the same folder as your Docker files. The docker-compose file is a config file for the docker-compose. The docker-compose allows you to run, deploy, and combine multiple containers at once.

From the docker-compose file, you can see that you define 3 services. I will try to succinctly explain to you the uses of the 3 services defined in the docker-compose file above.

You can visit this link to learn more about what a service is.

  • ganache: Create a local blockchain network on your computer based on thetrufflesuite/ganache-cliimage defined in the docker-compose file above.
  • bridge: Enable an instance of a local blockchain network, ganache, in this case, to interact with Provable Things
  • Truffle: Enable the Truffle framework capability, easy and quickly contract deployments and testing.

Part 4: Retrieving the Price of Bitcoin

Finally, it’s time to verify whether you correctly write code that obtains the Bitcoin price using Provable things.

Below is a file for testing that you need in order for you to retrieve the Bitcoin price. Place a test file inside the test folder.

To make it easier for you to obtain the Bitcoin price, I have written a script that you can use so you do not have to manually execute every single command to get the Bitcoin price from an Internet API. This is because every command that you need to fetch the Bitcoin price to your smart contract, will automatically be executed by the script from start to finish.

The Script

You just need to place the script above in your root project directory, and then, from your terminal, run chmod +x tldr.sh && ./tldr.sh. Note: make sure you are the same directory as the script.

Wait a little bit. You probably need to wait at least 1 minute. Note: it may take longer especially if you have never built Truffle and Ganache images with Docker.

Bitcoin Price after the test has been successfully executed

Congratulations, your smart contract have finally managed to fetch Bitcoin Price from Coingecko API with Provable.

There are many things that you can do with the Provable API. If you want to learn more, you can read examples here. I hope you find this article useful to start harnessing the Provable API in your smart contracts.

Join our community on Twitter!

--

--