To query prices from Band's oracle through smart contracts, the contract looking to use the price values should reference Band's StdReference contract. This contract exposes getReferenceData and getReferenceDataBulk functions.
getReferenceData takes two strings as the inputs, the base and quote symbol, respectively. It then queries the StdReference contract for the latest rates for those two tokens, and returns a ReferenceData struct, shown below.
Copy
structReferenceData{uint256 rate;// base/quote exchange rate, multiplied by 1e18.uint256 lastUpdatedBase;// UNIX epoch of the last time when base price gets updated.uint256 lastUpdatedQuote;// UNIX epoch of the last time when quote price gets updated.}
getReferenceDataBulk instead takes two lists, one of the base tokens, and one of the quotes. It then proceeds to similarly queries the price for each base/quote pair at each index, and returns an array of ReferenceData structs.
For example, if we call getReferenceDataBulk with ['BTC','BTC','ETH'] and ['USD','ETH','BNB'], the returned ReferenceData array will contain information regarding the pairs:
The contract code below demonstrates a simple usage of the new StdReference contract and the getReferenceData function.
Copy
pragmasolidity0.6.11;pragma experimental ABIEncoderV2;interfaceIStdReference{/// A structure returned whenever someone requests for standard reference data.structReferenceData{uint256 rate;// base/quote exchange rate, multiplied by 1e18.uint256 lastUpdatedBase;// UNIX epoch of the last time when base price gets updated.uint256 lastUpdatedQuote;// UNIX epoch of the last time when quote price gets updated.}/// Returns the price data for the given base/quote pair. Revert if not available.functiongetReferenceData(stringmemory _base,stringmemory _quote)externalviewreturns(ReferenceData memory);/// Similar to getReferenceData, but with multiple base/quote pairs at once.functiongetReferenceDataBulk(string[]memory _bases,string[]memory _quotes)externalviewreturns(ReferenceData[]memory);}contractDemoOracle{
IStdReference ref;uint256public price;constructor(IStdReference _ref)public{
ref = _ref;}functiongetPrice()externalviewreturns(uint256){
IStdReference.ReferenceData memory data = ref.getReferenceData("BTC","USD");return data.rate;}functiongetMultiPrices()externalviewreturns(uint256[]memory){string[]memory baseSymbols =newstring[](2);
baseSymbols[0]="BTC";
baseSymbols[1]="BTC";string[]memory quoteSymbols =newstring[](2);
quoteSymbols[0]="USD";
quoteSymbols[1]="ETH";
IStdReference.ReferenceData[]memory data = ref.getReferenceDataBulk(baseSymbols,quoteSymbols);uint256[]memory prices =newuint256[](2);
prices[0]= data[0].rate;
prices[1]= data[1].rate;return prices;}functionsavePrice(stringmemory base,stringmemory quote)external{
IStdReference.ReferenceData memory data = ref.getReferenceData(base,quote);
price = data.rate;}}