Setting up the oraclize Service in Smart contracts

Source: Internet
Author: User

Have you ever the calling Third-party APIs in your smart contracts? It can be possible using the Oraclize service. The Oraclize service enables smart contracts to access data from a external environment.

This is article describes the use of oraclize with Testrpc,truffle,ethereum-bridge. It also explains the smart contract code which calls multiple APIs using Oraclize service.

You are are familiar with the Dapp development using TESTRPC and truffle. For using Oraclize service your have to install another too ' ethereum-bridge ' as a prerequisite for your service.

Download and install Ethereum-bridge from the following Github Link:https://github.com/oraclize/ethereum-bridge.

You also have the solidity contract for using Oraclize service from the link https://github.com/oraclize/ethereum-a Pi/blob/master/oraclizeapi_0.4.sol. You are have to download the contract to the Truffle contracts folder of your working directory, and rename the file to Usingo Raclize.sol and use the above contract in your contract by importing.

Import "./usingoraclize.sol";

Start your TESTRPC.

Then start Ethereum-bridge in active mode using the following command:-

Node Bridge-h localhost:8545-a 1

The tag-a defines the which is used to deploy oraclize. It'll return to you a OAR address.

Please add it to your constructor.
Oar=oraclizeaddrresolveri (0x5049063e4a7704ac155e4f1f42a4954bbef5bbde);

You are have to update your smart contract constructor with the new address resolver generated.

pragma solidity ^0.4.11;
Import "./usingoraclize.sol";  Importing Oraclize
Contract Testoraclizecall is usingoraclize {
     Constructor
     function Testoraclizecall () { 
         oar= oraclizeaddrresolveri ( 0x5049063e4a7704ac155e4f1f42a4954bbef5bbde);
         Rest of your constructor code
      }
   //Your code
}

In the Oraclize service, your have to send a query to the Oraclize smart contract. Oraclize receive your query and make the corresponding request. Once the service receive the data from API, they call a callback function in the smart contract where you'll be able to a Ccess and process the requested data.

pragma solidity ^0.4.16;  Import "./usingoraclize.sol"; Importing Oraclize 
contract Testoraclizecall is usingoraclize {UINT public price;
     Event Log (string text); constructor function Testoraclizecall () {OAR = Oraclizeaddrresolveri (0x5049063e4a7704ac155e4f1f42a4954bbe
     F5bbde);
          function __callback (bytes32 _myid, string _result) {require (Msg.sender = = Oraclize_cbaddress ());
          Log (_result);
       Price = parseint (_result, 2); function Update () payable {oraclize_query ("URL", "JSON" Https://min-api.cryptocompare.com/data/price?fsym=ET H&TSYMS=USD).
     USD "); }
}

The above code is used to retrieve the current USD 1 ETH. The oraclize_queryfunction is used to inform the oraclize about the third party API.

The Oraclize_query accept the ' the ' the ' the ' the ' the ' the ' the ' the '. Here in the example we are using it as ' URL '. The second parameter must is the query we are requesting. It is also possible to add a third parameter in order to make the query delayed.

You are have to define the payable modifier the function. The transaction originating from Oraclize to the __callback function pays a fee to the miner which include the transaction In a blocks, just like any other transaction. The miner fee are paid in ether and it are calculated by taking the amount of gas which covers the execution costs of the TR Ansaction multiplied by the selected gas/ether. Oraclize would set those parameters accordingly to the parameters in the smart specified, for contract contract-wide GS, and in the Oraclize_query function, for query-specific settings. The miner fee for the callback transaction are taken from the contract balance when the query transaction is executed.

Note:-the made by the Oraclize contract are free.

In The__callback function, the ' the ' the ' parameter ' is the ID of the request. The second parameter is the result of your request.

You are have to compile and run the contract using truffle. handling of multiple Requests:-

The parameter ' ID ' in the __callbackwill handle different requests the same function. The following is the example dealing with multiple requests.

pragma solidity ^0.4.16;

Import "./usingoraclize.sol"; Contract Testoraclizecall is usingoraclize {//Oraclize callback Types:enum oraclizestate {FORUSD, Fordist

      ance}//events Event Log_oraclizecallbackfordistance (string result,);

       Event Log_oraclizecallbackforweathercondition (string result,);
       The oraclize callback Structure:we use several oraclize calls.
       All oraclize calls would result in a common callback to __callback (...).
      To keep track of the different querys we have to introduce this struct. struct Oraclizecallback {//for which purpose did we call? {FORUSD |
      Fordistance} oraclizestate ostate;

    
      }//Lookup state from Queryids Mapping (bytes32 => oraclizecallback) public oraclizecallbacks; constructor function Testoraclizecall () {OAR = Oraclizeaddrresolveri (0x5049063e4a7704ac155e4F1f42a4954bbef5bbde);
            }//function for distance retrieval Function Distanceretrieve () payable returns (bool sufficient) { Bytes32 Queryid = oraclize_query ("URL", Strconcat ("JSON (http://www.distance24.org/route.json?stops=us). Distance
            "));
            Oraclizecallbacks[queryid] = Oraclizecallback (oraclizestate.fordistance);
      return true; function update () payable returns (bool sufficient) {Bytes32 Queryid = oraclize_query ("URL", "JSON" (HTTPS ://MIN-API.CRYPTOCOMPARE.COM/DATA/PRICE?FSYM=ETH&TSYMS=USD).
         USD ");
         Oraclizecallbacks[queryid] = Oraclizecallback (ORACLIZESTATE.FORUSD);
      return true;  }//function callback Function __callback (bytes32 myID, string result) {if (Msg.sender!=
                 Oraclize_cbaddress ()) throw;
                 Oraclizecallback memory o = Oraclizecallbacks[myid];
                 if (o.ostate = = oraclizestate.fordistance) {              Log_oraclizecallbackfordistance (result); else if (o.ostate = = ORACLIZESTATE.FORUSD) {LOG_ORACLIZECALLBACKFORUSD (res
               ULT); }

      }


}

To keep track of the different queries we have used struct to hold and defined a mapping between queryids and State of Query.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.