Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Band Demo Oracle
pragma solidity 0.6.11;
pragma experimental ABIEncoderV2;
interface IStdReference {
/// A structure returned whenever someone requests for standard reference data.
struct ReferenceData {
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.
function getReferenceData(string memory _base, string memory _quote)
external
view
returns (ReferenceData memory);
/// Similar to getReferenceData, but with multiple base/quote pairs at once.
function getReferenceDataBulk(string[] memory _bases, string[] memory _quotes)
external
view
returns (ReferenceData[] memory);
}
contract DemoOracle {
IStdReference ref;
uint256 public price;
constructor(IStdReference _ref) public {
ref = _ref;
}
function getPrice() external view returns (uint256){
IStdReference.ReferenceData memory data = ref.getReferenceData("BNB","USD");
return data.rate;
}
function getMultiPrices() external view returns (uint256[] memory){
string[] memory baseSymbols = new string[](3);
baseSymbols[0] = "BNB";
baseSymbols[1] = "BTC";
baseSymbols[2] = "ETH";
string[] memory quoteSymbols = new string[](3);
quoteSymbols[0] = "USD";
quoteSymbols[1] = "USD";
quoteSymbols[2] = "USD";
IStdReference.ReferenceData[] memory data = ref.getReferenceDataBulk(baseSymbols,quoteSymbols);
uint256[] memory prices = new uint256[](3);
prices[0] = data[0].rate;
prices[1] = data[1].rate;
prices[2] = data[2].rate;
return prices;
}
function savePrice(string memory base, string memory quote) external {
IStdReference.ReferenceData memory data = ref.getReferenceData(base,quote);
price = data.rate;
}
}
@maxpru1991

This comment has been minimized.

Copy link

@maxpru1991 maxpru1991 commented Sep 15, 2021

pragma solidity 0.6.11;
pragma experimental ABIEncoderV2;

interface IStdReference {
/// A structure returned whenever someone requests for standard reference data.
struct ReferenceData {
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.
function getReferenceData(string memory _base, string memory _quote)
    external
    view
    returns (ReferenceData memory);

/// Similar to getReferenceData, but with multiple base/quote pairs at once.
function getReferenceDataBulk(string[] memory _bases, string[] memory _quotes)
    external
    view
    returns (ReferenceData[] memory);

}

contract DemoOracle {
IStdReference ref;

uint256 public price;

constructor(IStdReference _ref) public {
    ref = _ref;
}

function getPrice() external view returns (uint256){
    IStdReference.ReferenceData memory data = ref.getReferenceData("BNB","USD");
    return data.rate;
}

function getMultiPrices() external view returns (uint256[] memory){
    string[] memory baseSymbols = new string[](3);
    baseSymbols[0] = "BNB";
    baseSymbols[1] = "BTC";
    baseSymbols[2] = "ETH";


    string[] memory quoteSymbols = new string[](3);
    quoteSymbols[0] = "USD";
    quoteSymbols[1] = "USD";
    quoteSymbols[2] = "USD";

    IStdReference.ReferenceData[] memory data = ref.getReferenceDataBulk(baseSymbols,quoteSymbols);

    uint256[] memory prices = new uint256[](3);
    prices[0] = data[0].rate;
    prices[1] = data[1].rate;
    prices[2] = data[2].rate;

    return prices;
}

function savePrice(string memory base, string memory quote) external {
    IStdReference.ReferenceData memory data = ref.getReferenceData(base,quote);
    price = data.rate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment