Skip to main content

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 cover:

  • Introduction to Band Protocol
  • Deploying an example Oracle contract that interacts with the Band reference data contract
  • Querying the reference data contract for current asset rates

Introduction to Band Protocol

Band Protocol is a cross-chain data oracle platform that aggregates and connects real-world data and APIs to smart contracts. For more detailed information about the protocol, please refer to the official documentation.

Deploying the Oracle

  1. Follow this link to Remix. The link contains an encoded example DemoOracle.sol contract.
  2. Compile the contract using compiler version 0.6.11.
  3. Switch to the Deploy tab in Remix.
    1. Select "Injected Web3" in the Environment dropdown in the top left to connect Metamask.
    2. Ensure Metamask is connected to the Alfajores test network. Instructions for adding Alfajores to Metamask can be found here.
environment
  1. Enter the Alfajores testnet Band reference data aggregator contract address 0x660cBc25F0cFD31F0Bdcaa43525f0bACC6DB2ABc into the DemoOracle constructor and deploy the contract. The mainnet reference data aggregator contract address is 0xDA7a001b254CD22e46d3eAB04d937489c93174C3. Always verify the current address on their page to ensure accuracy, as updates may occur.
environment

An interface to interact with the contract will appear in the bottom left corner of Remix.

Retrieving 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" as parameters. The returned rate is base/quote multiplied by 1e18.

get price

Note that the DemoOracle contract only returns the latest rate, but the reference contract also provides the last update times for the base and quote references.

The price is scaled by 1e18. For example, a returned value of 3747326500000000000 corresponds to a current USD price of 3.7473265 CELO/USD.

Clicking the getMultiPrices button returns multiple quotes in a single call, such as BTC/USD and BTC/ETH. 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" as the quotes. This returns the current CELO prices in USD and ETH as an array of integers. The call returns only the exchange rates (multiplied by 1e18) but can be modified to include the last update times for the bases and quotes.

get prices

The savePrice function stores any base/quote rate passed to it in the storage variable price. This data will only be updated when the savePrice function is called, so the stored price value may become outdated unless updated regularly.

get prices

Mainnet Reference Data Contract

The mainnet reference data aggregator contract address is 0xDA7a001b254CD22e46d3eAB04d937489c93174C3.

Available Reference Data

You can view the available reference data on the Band Data site.

Bandchain.js

Band also offers a JavaScript library, Bandchain.js, which facilitates interaction with BandChain directly from JavaScript or TypeScript applications. The library provides classes and methods for sending transactions, querying data, OBI encoding, and wallet management. More information can be found in the Bandchain.js documentation.