Using Band Protocol
By the end of this tutorial, you will understand how to query the Band Protocol reference data smart contract from another Solidity smart contract on Celo.
This tutorial will go over:
- What is Band?
- Deploying an example Oracle contract that calls the Band reference data contract
- Calling the reference data contract for current rates of different assets
What is the Band Protocol?
Band Protocol is a cross-chain data oracle platform that aggregates and connects real-world data and APIs to smart contracts. You can read more about the specific details of the protocol here.
Deploy Oracle
- Folow this link to Remix. The link contains an encoded example
DemoOracle.sol
contract. - Compile the contract with compiler version
0.6.11
. - Switch to the Deploy tab of Remix.
- Select "Injected Web3" in the Environment dropdown in the top left to connect Metamask.
- Make sure that Metamask is connected to the Alfajores test network. You can read about adding Alfajores to Metamask here.
- Enter the Alfajores testnet Band reference data aggregator contract address
0x660cBc25F0cFD31F0Bdcaa43525f0bACC6DB2ABc
to theDemoOracle
constructor and deploy the contract. You can access the reference data aggregator contract on mainnet at0xDA7a001b254CD22e46d3eAB04d937489c93174C3
. Make sure you check the current address on their page, as it might happen that they updated their reference contract and then your orcale data will not be correct anymore.
An interface to interact with the contract will appear in the bottom left corner of Remix.
Get Rates
Clicking the getPrice
button will return the current price of CELO in USD. This function calls getReferenceData(string memory _base, string memory _quote)
on the Band reference data contract, passing "CELO" and "USD", indicating CELO as the base and USD as the quote. The rate returned is base/quote multiplied by 1e18.
Note that the DemoOracle
contract only returns the latest rate, but the reference contract also returns values of the last time the base and quote references were updated.
The price is offset by 1e18. The returned value at the time of testing is 3747326500000000000
. Multiplying by 1e-18 gives the current USD price given by the reference contract, 3.7473265 CELO/USD.
Clicking the getMultiPrices
button returns multiple quotes in the same call, BTC/USD and BTC/ETH in this case. This function calls getReferenceDataBulk(string[] memory _bases, string[] memory _quotes)
on the Band reference data contract, passing "CELO" as the base and "USD" and "ETH" for the quotes. This will return the current CELO prices in USD and ETH, as an array of integers. The call also returns just the exchange rates (multiplied by 1e18), but can be modified to return the last updated times for the bases and quotes.
The "savePrice" function will save any base/quote rate that is passed to it in the storage variable named price
. This storage data will only be updated when the “savePrice” function is called, so the saved price
value will go stale unless this function is called repeatedly.
Mainnet Reference Data Contract
You can access the reference data aggregator contract on mainnet at 0xDA7a001b254CD22e46d3eAB04d937489c93174C3
.
Available Reference Data
You can view the available reference data on the Band Data site here.
Bandchain.js
Band also has a javascript library that makes it easy to interact with BandChain directly from Javascript or Typescript applications. The library provides classes and methods for convenient to send transactions, query data, OBI encoding, and wallet management. You can read more about it here.