A guide to perform web queries in DAPP (Part 2)
After understanding the mechanism of Oraclize in Part 1, we can dive into details of implementing a DAPP.
We are going to implement a contract that:
- fetch the latest BTC/USD data using coindesk api
- update every 60s
Note that the fixer.io api used in the Oraclize official website is deprecated. And the coindesk api used in this example may also be deprecated in the future. So validate the api before following this tutorial.
Using the Oraclize api
The contract should be a child of the contract usingOraclize
However, you may find that the import statement doesn’t work. A quick solution is to download the oraclize.sol
file to your project directory from their repo. And import oraclize.sol
using the solidity import statement:
import "oraclize.sol";
Adding functions to make query
Then we are going to implement some more features:
- update every 60s
- using the “URL” data soruce type
- using json parsing helper to get the .rates.GBP attribute
By adding a theupdataPrice
function in the callback. The update process will initiate itself again. We specify the updataPrice
function to execute after 60s. So we will get a new piece of data after every 60s.
Handling data
Then we define a handler function that will log the data.
event LogPriceUpdated(string price); //a function that will log the input string
Putting it all together
We skip some of the features in order to keep it simple and clean. This is the minimal viable code snippet of using Oraclize to make web query.
The whole code snippet
The code is originally from http://docs.oraclize.it/#ethereum-quick-start.
Originally published at gist.github.com.