Integrating Band Protocol to get off-chain data for smart contracts

Tram Ho

Wait to talk about what Band Protocol is, I will go straight to its use, maybe this is the piece that you are always looking for for your Dapp. Band protocol supports contracts that communicate with data from the outside world such as weather, sports, stock value data, exchange rate and of course the indispensable rate of crypto currencies . Band Protocol belongs to Oracle system similar to Chainlink.

Introduce

In this article, I will guide mainly on getting the values ​​of cryptocurrencies and foreign exchange rates and how to integrate them into your applications.

I will guide you to integrate oracle data of Band into Smart Contract through StdReference contract and Frontend through bandchain.js library

Integrated on Smart Contract

Theory

To get price data from Oracle of the band via smart contract, we can use StdReference contract. This contract has 2 functions to support data retrieval, getReferenceData and getReferenceDataBulk.

The function getReferenceData will take 2 input values, base and quote . It will query on the StdReference contract with 2 input parameters and return the value between these two pairs as struct ReferenceData data. This data looks like this:

Similar to getReferenceData , the getReferenceDataBulk function will take a bulk of data instead of a pair, it will take the input of an array of bases and a corresponding quote array. For example, input 2 arrays [‘ETH’, ‘BTC’, ‘BNB’] and [‘USD’, ‘KNC’, ‘DAI’], the output will be an array of ReferenceData similar to the pairs:

  • ETH / USD
  • BTC / KNC
  • BNB / DAI

Practice

To practice using the StdReference smart contract, Band Protocol has supported deploying a contract on Kovan testnet , you can test directly on Remix: https://remix.ethereum.org/

StdReference’s address on Kovan : 0xDA7a001b254CD22e46d3eAB04d937489c93174C3

Next, you deploy the DemoOracle contract with the constructor’s input argument which is the address of stdReference on Kovan: 0xDA7a001b254CD22e46d3eAB04d937489c93174C3

From there it is possible to test the getPrice function with 2 input arguments as crypto pairs.

Integration on Webapp

Theory

Similar to the integration for smart contracts, for integration on the Webapp we will use the bandchain.js package. However, this package currently supports only bulk pricing, which means the input will receive 2 arrays of pairs you want to get the corresponding rate.

Basically, the bandchain.js package is no different from using the binance or coinbase pricing APIs, but for Dapp applications that have integrated band in smart contracts, you should also recommend using bandchain.js for interface part

Practice

First create a node js file:

index.js :

The above endpoint will point to a node to query crypto pairs.

Test the code above

The result will be the values ​​of the corresponding pairs:

So this time I want to share how you can quickly integrate the band protocol into blockchain applications related to the value of crypto currencies. In the next article I will explain more about the operating principles of the band protocol.

Share the news now

Source : Viblo