Skip to content

Instantly share code, notes, and snippets.

@smiled0g
Last active November 21, 2019 12:45
Show Gist options
  • Save smiled0g/408a52f61d54254ff062ef244a32945f to your computer and use it in GitHub Desktop.
Save smiled0g/408a52f61d54254ff062ef244a32945f to your computer and use it in GitHub Desktop.
Solidity example code for getting volume data from CoinGecko via Band Protocol's OpenAPI
pragma solidity ^0.5.0;
interface Oracle {
enum QueryStatus { INVALID, OK, NOT_AVAILABLE, DISAGREEMENT }
function query(bytes calldata input)
external payable returns (bytes32 output, uint256 updatedAt, QueryStatus status);
function queryPrice() external view returns (uint256);
}
contract CoinGeckoVolumeContract {
bytes public queryKey = hex"12202ac5043a71c56f6633403905f748302c8d9ca12f695e2b6de8c20e2f05ab10fe";
uint256 public volume;
function update(bytes memory coinId) public payable {
Oracle oracle = Oracle(0x7f525974d824a6C4Efd54b9E7CB268eBEFc94aD8);
uint256 queryPrice = oracle.queryPrice();
require (address(this).balance >= queryPrice, "INSUFFICIENT_BALANCE_FOR_QUERY_FEE");
(bytes32 raw, , Oracle.QueryStatus status) = oracle.query.value(queryPrice)(abi.encodePacked(queryKey, coinId));
require(status == Oracle.QueryStatus.OK, "INVALID_QUERY_STATUS");
volume = uint256(raw);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment