Skip to content

Accesing Price Feeds with the Band Protocol

Introduction

Band Protocol is a decentralized oracle network that provides reliable, secure, real-time data to smart contracts on various blockchain networks.

The protocol is built on top of BandChain, an appchain designed to be compatible with most EVM-compatible chains, such as Tanssi EVM appchains, and blockchain development frameworks. The protocol aims to provide a solution that is:

  • Decentralized, leveraging the computational power of a network of validators
  • Flexible, supporting a wide range of data sources and formats, making integrations easy
  • Scalable, designed to handle high volumes of data requests
  • Affordable, allowing users to request data only when they need to and pay the associated fees

Band protocol is currently deployed on many blockchains (Moonbeam, for example) across different ecosystems. To deploy the oracle onto your appchain, reach out to the Band Protocol team directly.

This tutorial will walk through the steps to interact with price feeds using the Band protocol's oracle on the Tanssi demo EVM-compatible appchain.

Setup on the Tanssi Demo EVM Appchain

The Band Protocol oracle is already deployed on the Tanssi demo EVM appchain and configured to provide prices for the ETH and DOT tokens.

The price feeds are pushed regularly to a smart contract that is accessible at the following address:

0x8c064bCf7C0DA3B3b090BAbFE8f3323534D84d68

The smart can be interacted with using the interface:

IStdReference.sol
// SPDX-License-Identifier: Apache-2.0

pragma solidity 0.8.26;

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);
}

As seen above in the interface, there are two functions for fetching data:

getReferenceData(_base, _quote) — fetches the price for a given base/quote pair
  • _base string memory - the token you want to get the price for
  • _quote string memory - the token (or USD) in which the price is expressed
  • _base - ETH
  • _quote - USD
getReferenceDataBulk(_bases, _quotes) — fetches prices for the given base/quote pairs simultaneously
  • _bases string[] memory - the list of base tokens you want to get the prices for
  • _quotes string[] memory - the list of tokens (or USD) in which the prices are expressed
  • _bases - ["ETH", "DOT"]
  • _quotes - ["USD", "USD"]

The response for both functions consists of the following data, grouped in one tuple in the case of getReferenceData and one list of tuples (one tuple per pair) in the case of getReferenceDataBulk:

  • rate uint256 - price for the given base/quote pair. Note that the result must be adjusted to consider eighteen decimal places
  • lastUpdatedBase uint256 - update timestamp for the _base parameter, expressed in UNIX epochs, which is the number of seconds that have passed since 01-01-1970 00:00:00 UT
  • lastUpdatedQuote uint256 - update timestamp for the _quote parameter, expressed in UNIX epochs, which is the number of seconds that have passed since 01-01-1970 00:00:00 UT

Fetching Price Feeds Using Remix

In this section, we'll use remix to fetch the price of the pair ETH/USD.

First, make sure you have an EVM-compatible wallet connected to the demo EVM appchain. MetaMask is used as an example in this guide. Now, head to Remix, paste the IStdReference interface into a new file, and compile it.

Compile interface contract

Then, take the following steps:

  1. Head to the Deploy & Run Transactions tab
  2. Set the ENVIRONMENT to Injected Provider -- MetaMask
  3. Select the IStdReference.sol contract from the CONTRACT dropdown
  4. Enter the data feed contract address, which is 0x8c064bCf7C0DA3B3b090BAbFE8f3323534D84d68 on the demo EVM appchain in the At Address field and click the At Address button

Access Interface contract

The contract should now be accessible. To interact with it, take the following steps:

  1. Expand the IStdReference contract to reveal the available functions
  2. Expand getReferenceData, and set the _base and _quote input parameters to ETH and USD, respectively
  3. Click Call
  4. The result will show three values: the price, update time for the _base parameter, and update time for the _quote parameter

Check price data

Note that to obtain a readable price from the price feed, it's essential to adjust for the feed's decimal places, which are eighteen. For instance, the example above shows a value of 2361167929271984201806, corresponding to an ETH price of $2,361.167929271984201806 expressed in USD. Also, note that the update timestamp values are expressed in UNIX epoch time, expressed as the number of seconds that have passed since 01-01-1970 00:00:00 UT.

The information presented herein has been provided by third parties and is made available solely for general information purposes. Tanssi does not endorse any project listed and described on the Tanssi Doc Website (https://docs.tanssi.network/). Tanssi Foundation does not warrant the accuracy, completeness or usefulness of this information. Any reliance you place on such information is strictly at your own risk. Tanssi Foundation disclaims all liability and responsibility arising from any reliance placed on this information by you or by anyone who may be informed of any of its contents. All statements and/or opinions expressed in these materials are solely the responsibility of the person or entity providing those materials and do not necessarily represent the opinion of Tanssi Foundation. The information should not be construed as professional or financial advice of any kind. Advice from a suitably qualified professional should always be sought in relation to any particular matter or circumstance. The information herein may link to or integrate with other websites operated or content provided by third parties, and such other websites may link to this website. Tanssi Foundation has no control over any such other websites or their content and will have no liability arising out of or related to such websites or their content. The existence of any such link does not constitute an endorsement of such websites, the content of the websites, or the operators of the websites. These links are being provided to you only as a convenience and you release and hold Tanssi Foundation harmless from any and all liability arising from your use of this information or the information provided by any third-party website or service.
Last update: September 6, 2024
| Created: September 3, 2024