Skip to content

Instantly share code, notes, and snippets.

@ernestognw
Last active June 29, 2019 21:48
Show Gist options
  • Save ernestognw/90e5ec8363add0ef64c2774346d1dfa2 to your computer and use it in GitHub Desktop.
Save ernestognw/90e5ec8363add0ef64c2774346d1dfa2 to your computer and use it in GitHub Desktop.
/* ETH price fetcher
This smart contract keeps tracking
of ETH price in USD
*/
pragma solidity ^0.4.24;
import "github.com/oraclize/ethereum-api/oraclizeAPI_0.4.sol";
// For newer pragma solidity versions, the previous statement
// should be the following:
// pragma solidity ^0.5.0;
// import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract ETHUSD is usingOraclize {
uint public etherUSD;
constructor () public {
etherUSD = 0;
}
// __callback is an overridden function provided by oraclize api
// to handle API responses
function __callback(bytes32 myid, string memory result) public {
// oraclize_cbAddress() ensures that the data provided comes from
// oraclize provider, and not anyone could update this data
require(msg.sender == oraclize_cbAddress());
etherUSD = parseInt(result, 2); // Recover 2 decimals
}
function update() public payable {
// oraclize_query() is the way to query an API from the smart contract
// Receives 2 parameters:
// 1.- Data source
// 2.- Parser helper with data
oraclize_query("URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHUSD).result.XETHZUSD.c.0");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment