Skip to content

Instantly share code, notes, and snippets.

@bertani
Created June 28, 2018 15:29
Show Gist options
  • Save bertani/c4cbd320e393d0fa830bbbfc32c05949 to your computer and use it in GitHub Desktop.
Save bertani/c4cbd320e393d0fa830bbbfc32c05949 to your computer and use it in GitHub Desktop.
Oraclize examples

pragma solidity ^0.4.0;

/*

Here you can find some Solidity examples showing how to use the Oraclize API in such context.

This is a list of the features you will find there, along with a reference to the relevant examples.

Please note that the most basic features are first.

  • sending simple URL queries #1
  • using the WolframAlpha datasource #1
  • requesting TLSNotary-based authenticity proofs #1
  • scheduling a query for a future date #1
  • sending calls recursively #1
  • leveraging JSONPATH parsing helpers #1
  • leveraging XPATH parsing helpers #1 #2
  • leveraging BINARY_SLICE parsing helpers
  • full query encryption
  • partial query encryption
  • nested queries
  • using the computation datasource

*/

/*
Diesel Price Peg
This contract keeps in storage a reference
to the Diesel Price in USD
*/
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract DieselPrice is usingOraclize {
uint public dieselPriceUSD;
event NewOraclizeQuery(string description);
event NewDieselPrice(string price);
function DieselPrice() public {
update(); // first check at contract creation
}
function __callback(bytes32 myid, string result) public {
if (msg.sender != oraclize_cbAddress()) revert();
NewDieselPrice(result);
dieselPriceUSD = parseInt(result, 2); // let's save it as $ cents
// do something with the USD Diesel price
}
function update() 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");
}
}
/*
Kraken-based ETH/XBT price ticker
This contract keeps in storage an updated ETH/XBT price,
which is updated every ~60 seconds.
*/
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract KrakenPriceTicker is usingOraclize {
string public priceETHXBT;
event NewOraclizeQuery(string description);
event NewKrakenPriceTicker(string price);
function KrakenPriceTicker() public {
oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
update();
}
function __callback(bytes32 myid, string result, bytes proof) public {
if (msg.sender != oraclize_cbAddress()) revert();
priceETHXBT = result;
NewKrakenPriceTicker(priceETHXBT);
update();
}
function update() public payable {
if (oraclize_getPrice("URL") > this.balance) {
NewOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
} else {
NewOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query(60, "URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHXBT).result.XETHXXBT.c.0");
}
}
}
/*
WolframAlpha example
This contract sends a temperature measure request to WolframAlpha
*/
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract WolframAlpha is usingOraclize {
string public temperature;
event NewOraclizeQuery(string description);
event NewTemperatureMeasure(string temperature);
function WolframAlpha() public {
update();
}
function __callback(bytes32 myid, string result) public {
if (msg.sender != oraclize_cbAddress()) revert();
temperature = result;
NewTemperatureMeasure(temperature);
// do something with the temperature measure..
}
function update() public payable {
NewOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query("WolframAlpha", "temperature in London");
}
}
/*
Youtube video views
This contract keeps in storage a views counter
for a given Youtube video.
*/
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract YoutubeViews is usingOraclize {
string public viewsCount;
event NewOraclizeQuery(string description);
event NewYoutubeViewsCount(string views);
function YoutubeViews() public {
update();
}
function __callback(bytes32 myid, string result) public {
if (msg.sender != oraclize_cbAddress()) revert();
viewsCount = result;
NewYoutubeViewsCount(viewsCount);
// do something with viewsCount. like tipping the author if viewsCount > X?
}
function update() public payable {
NewOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query("URL", 'html(https://www.youtube.com/watch?v=9bZkp7q19f0).xpath(//*[contains(@class, "watch-view-count")]/text())');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment