Skip to content

Instantly share code, notes, and snippets.

@djuanit0x
Created July 22, 2019 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djuanit0x/9581c838c0d78b162e7d0e7c4103b7ab to your computer and use it in GitHub Desktop.
Save djuanit0x/9581c838c0d78b162e7d0e7c4103b7ab to your computer and use it in GitHub Desktop.
provable-bitcoin-price
pragma solidity ^0.5.0;
import './provableAPI_0.5.sol';
contract BitcoinPrice is usingProvable {
string public GET_BITCOIN_PRICE_QUERY = "json(https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=1&page=1&sparkline=false).0.current_price";
event LogNewProvableQuery(string description);
event LogNewProvableResult(string result);
mapping (bytes32 => bool) public pendingQueries;
string public result;
constructor() public payable {
}
function __callback(bytes32 _myid, string memory _result) public {
require(msg.sender == provable_cbAddress());
require (pendingQueries[_myid] == true);
result = _result;
emit LogNewProvableResult(_result);
delete pendingQueries[_myid]; // This effectively marks the query id as processed.
}
function retrieveBitcoinPrice() public payable {
if (provable_getPrice("URL") > msg.value) {
revert("Provable query was NOT sent, please add some ETH to cover for the query fee!");
} else {
emit LogNewProvableQuery("Provable query was sent, standing by for the answer...");
bytes32 queryId = provable_query("URL", GET_BITCOIN_PRICE_QUERY);
pendingQueries[queryId] = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment