Blockchain oracles

At this level, the previous contract enables basic operations to lock and unlock funds against commodities delivery. Nevertheless, once the contract expires, we want the contract to calculate the offset benefit (savings) or loss made by the futures deal  in other words, how much the hedger has economized or lost.

In this case, at settlement time, the smart contract needs a solid and reliable source of information to determine the underlying commodity (oil) price to calculate the offset. But a question arises here: is the contract able to know by itself what the oil market price is?

As we introduced in a previous chapter, the smart contract is deterministic code executed in an isolated VM without direct connection to an outside source of information!

However, there is a workaround for that situation: oracles.

Oracles are trusted agents or data suppliers that read and send requested information to the smart contract from reliable external data sources. We have a few providers of this type of service in the blockchain space, such as Chainlink, Link, Blocksense, and Oraclize. In our example, we will use Oraclize (http://www.oraclize.it/) as it is an industry leader in oracle servicing.

Let's see how we can connect our contract to an oracle contract. In our previous code, we have to bring in the following changes:

pragma solidity ^0.4.24;

import "./oraclizeAPI_0.5.sol";

contract CFutures is usingOraclize {
uint public fuelPriceUSD;

event NewOraclizeQuery(string description);
event NewDieselPrice(string price);

function CFutures(
uint assetID,
uint Quantity,
uint price,
address buyer,
address seller,
uint date) public {
… // keep it as defined previously
}
function __callback(bytes32 myid, string result) public {
require(msg.sender == oraclize_cbAddress());
NewDieselPrice(result);
fuelPriceUSD = parseInt(result, 2);
}

function updateprice() public payable {
NewOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query("URL","xml(https://www.fueleconomy.gov/ws/rest/fuelprices).fuelPrices.diesel");
}
}

In result we end up with a simple contract using the Oraclize service to request Fuelprice (let's consider fuel as equivalent to oil).

As you can see, we start by importing the Oraclize API: import "./oraclizeAPI_0.5.sol";.

Then, we inherit the usingOraclize contract to access the library methods. 

Next, to help the compiler resolve the import, we need to download the Oraclize API contract and libraries into Truffle's contracts/ folder by running wget https://raw.githubusercontent.com/oraclize/ethereum-api/master/oraclizeAPI_0.5.sol.

In the Oracle code part, we defined, along with fuelPriceUSD, two new functions: updateprice() and __callback.

The first one uses oraclize_query to send a query to the Oraclize's smart contract with two arguments: the type of request (URL, WolframAlpha, IPFS multihash) and the requested information (a specific property in the XML response). The updateprice function is declared as payable because the Oraclize service isn't free, and this function should accept ether to be able to pay for the Oraclize service. However, the first query made by a contract is free, so we include a call for the updateprice() function without sending ether in our constructor.

On the other hand, the second function, __callback is defined by the API and, as its name indicates, it's a callback function executed once Oracle sends back the response. Its first parameter is the id of the request, while the second parameter is the result of your request. In this example, we use the parseInt function, which is defined by the Oraclize API file, to set the number precision. In the preceding example, $3.20 will be returned as 320 cents.

The following diagram represents the interaction between the initial contract and the data source using Oraclize:

Lastly, let's implement the offset() method that will calculate the offset profit the hedger made by taking this futures contract based on the price variation:

function offset() public view returns (uint) {
return TradedAsset.Quantity * (fuelPriceUSD - TradedAsset.price);
}

Now that the contract is complete, compile the contract using truffle compile and check whether there are any errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset