Simple Oraclize example with Solidity

Bishesh A.
Coinmonks
Published in
2 min readJun 9, 2018

--

I have been working with Solidity smart contracts for a while now. Today I am going to show how we can use Oraclize to fetch data from outside world into Ethereum smart contract. This feature will enable our smart contract to be very dynamic and deal with lots of nice use cases. The focus of this story is to get you started with a simple working example.

source: blog.oraclize.it

First, you might be thinking why do we need outside world data. Well, there are various use cases where you need off chain data in your smart contract. i.e.

  • query weather data if your smart contract deals with weather condition
  • get stock price
  • generate secure random number (as on chain random number generation are not very safe)
  • do some complex computation via WolframAlpha
  • etc.

Example: ETH/BTC price checker

For our example, we are going to use Oraclize to get the latest pair ETH/BTC from Kraken Exchange and store it in our smart contract. Here is our SimpleOraclizeContract:

It is pretty simple to use Oraclize, we just need to import the Oraclize.sol contract and inherit our SimpleOraclizeContract from usingOraclize contract which can be found here :
https://gist.github.com/bishesh16/34cd6dff58f8d5b2865576a6157996b6
or
https://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI_0.4.sol

Just paste the content of the above contact on a new file and name the file “Oraclize.sol”.

That’s it !!!

Testing it on Remix IDE with Metamask

Use the remix IDE and copy the above two files and interact with the smart-contract on Ropsten, Kovan, Rinkeby testnets or even main-net :)

Deploying the contract on Ropsten network. Metamask is required !

Steps to test the contract:
1. deploy the contract
2. call the updatePrice() function with few ether i.e 0.05 ether (use test networks like Ropsten )
If the previous transaction is successful after a minute Oraclize will call back our contract and update the price pair ETH/XBT
3. check the price ETHXBT, it should be updated :)

Note: In order to call Oraclize you need to transfer some ether to your smart contract, otherwise it won’t be able to query Oraclize (check fees here).

References:
Github repo: https://github.com/bishesh16/simple-oraclize-solidity
Oraclize docs: http://docs.oraclize.it/#home

--

--