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 (orUSD
) 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 (orUSD
) 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 placeslastUpdatedBase
uint256 - update timestamp for the_base
parameter, expressed in UNIX epochs, which is the number of seconds that have passed since01-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 since01-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.
Then, take the following steps:
- Head to the Deploy & Run Transactions tab
- Set the ENVIRONMENT to Injected Provider -- MetaMask
- Select the
IStdReference.sol
contract from the CONTRACT dropdown - 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
The contract should now be accessible. To interact with it, take the following steps:
- Expand the IStdReference contract to reveal the available functions
- Expand getReferenceData, and set the
_base
and_quote
input parameters toETH
andUSD
, respectively - Click Call
- The result will show three values: the price, update time for the
_base
parameter, and update time for the_quote
parameter
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
.
| Created: September 3, 2024