Band Protocol
How to Interact with BandChain from Harmony Smart Contracts
We will go through each of these steps:
Requesting data using BandChain.js library
Using BandChain data in EVM Smart Contract in Harmony
Requesting data using BandChain.js library
BandChain provides the BandChain.js JavaScript library so developers can submit data requests right from their dApp client.
Once BandChain has successfully executed the requested oracle script, it will return the data and EVM proof payload back to the client to then be passed to our smart contracts in harmony blockchain.
Here is an example simple Node file requesting data using the library that queries the current Bitcoin(BTC) price from BandChain.
Now let’s go through each section of the code.
Import the library and declare the necessary variables:
The code requires two libraries:
@bandprotocol/obi.js: a helper library to assist us when encoding/decoding data to/from Band Protocol’s OBI standard
@bandprotocol/bandchain.js: the library that we’ll be using to make the queries to BandChain
Now, set the request parameters:
Here we set the values that we will be using to make the request
endpoint
: the endpoint we will make the query tomnemonic
: the mnemonic we will use to make the query. The associated account must have a balance to make a requestbandchain
: contains the necessary functions we’ll need to make the requestoracleScript
: object containing details of the oracle script we will be queryingminCount
: the minimum number of BandChain’s validators that responds for us to consider the request successfulaskCount
: the maximum number of validators that we want to respond to the request
Now make the Oracle request and get the random hash:
Finally, we execute the submitRequestTx
member function of the previously declared bandchain object to make the oracle data request.
We can then do one of two things with regards to this request:
Call
getRequestResult
, as we did here, to get the actual result of the requestCall
getRequestProof
to get the proof bytes associated with the request
Both of these functions take in one argument, the requestID
of the request you want to retrieve the result/proof from.
If the query is successful, the code should print a value similar to:
{ price: 11406282500000n }
,
which is the retrieved price of Bitcoin multiplied by 1 billion.
Using BandChain data in EVM Smart Contract in Harmony
Now that we know how to make a request to BandChain and retrieve the corresponding proof, we will now examine how we can use that proof in our smart contract.
The code below demonstrates an example contract that retrieves the latest price of Bitcoin from Band’s bridge contract and saves it onto the contract’s state:
Okay, now let’s break down the code.
Aside from OpenZeppelin’s SafeMath.sol, the contract we will be writing requires three helper files specific to Band’s oracle and bridge architecture: Obi.sol
, Decoders.sol
, and IBridgeWithCache.sol
.
Obi.sol
This contains a set of functions to help serialize and deserialize binary data when interacting with the BandChain ecosystem. The full standard specification can be found on their wiki and the code on the BandChain repository.
Decoders.sol
This is what we will use to work with data related to requests made on BandChain. This will help us in extracting the various information, such as the price value, we may need from the request response from Band’s oracle. The file is available from an oracle script’s bridge code tab on the devnet explorer. For this particular example, the code is available here.
IBridgeCache.sol
The interface file for Band’s bridge contract. The code for this can also be found on the BandChain repository.
Now let’s look at the contract itself:
The contract itself can then be further broken down into two parts: the constructor
and the main getPrice
function.
Contract Constructor
The contract’s constructor takes one argument, the address of the bridge contract. It then sets the various fields of the req RequestPacket
variable. This req variable will be what we will use as the key to match and retrieve the price from the bridge contract. Specifically, in this case, we set req to have the following parameters.
bridge
: the address of Band’s bridge contract on the blockchain this example contract is deployed to. See this list for the chains that Band currently supports and corresponding contract addresses. You can deploy the bridge contract by yourself on the harmony chain. Bridge contract can be found here.clientId
(“from_scan”): the unique identifier of this oracle request, as specified by the client.oracleScriptId
(76): The unique identifier number assigned to the oracle script when it was first registered on Bandchain.params
(hex”00000003425443"): The data passed over to the oracle script for the script to use during its execution. In this case, it is hex representation of the OBI-encoded request structminCount
(4): The minimum number of validators necessary for the request to proceed to the execution phaseaskCount
(4): The number of validators that are requested to respond to this request
getPrice Function
This is then the main function that we will use to fetch the price from Band’s bridge contract and save it into our price database contract’s state. It calls the bridge contract’s getLatestResponse to retrieve the latest request response associated with a BTC/USD price request. It then uses Decoders.sol’s decodeResult method to parse that response into a struct. Finally, we save the price value from that response into the contract’s price variable.
References:
Examples
Examples can be found here https://github.com/harmony-one/band_oracle
Requesting data using BandChain.js library
https://github.com/harmony-one/band_oracle/blob/main/main.js
Using BandChain data in EVM Smart Contract in Harmony
https://github.com/harmony-one/band_oracle
contract's been deployed and you can call getPrice
or getMultiPrices
to get actual prices
Band’s StdReference contract for testnet 0xE740092E081CA7491A46C8Aa0175446e962e2A08
mainnet contract is coming soon
Last updated