In our previous article covering the constituents of Band’s oracle infrastructure, we explore how we can request data from BandChain’s oracle to use in our dApps and smart contracts. However, smart contracts are not the only place where decentralized data are needed.
No matter how decentralized the data we use in our smart contracts are, these are not what most application users will see nor will be interacting with. Instead, they will be relying on some sort of front-end interface to provide them with the necessary information for them to make their decisions. If these interfaces then still use data from centralized sources, this presents a clear point of failure which, if exploited, could have catastrophic implications. The team also has to maintain centralized architecture to handle such data feed.
This is where Band’s decentralized oracle network comes in. To help ensure that both the application’s smart contracts and user-facing client adhere to the same standard of decentralization, Band’s oracle infrastructure enables developers to not only make queries for data to use in their smart contracts, but to also retrieve the result of those requests to show in their application interface. And, in this article, we will cover how we can do just that.
Throughout this article, we will be writing a few code snippets to demonstrate how a web application can utilize oracle data as reported by BandChain.
The application consists of two components:
cron-requesterbackend service that periodically makes requests to BandChain’s oracle for the price of the various cryptocurrencies.
- The web page itself then retrieves the results of those requests and shows it accordingly. This is the part that we will be focusing on in this article.
Note that the web app does not make the requests itself, but instead queries the result of a previous request made that match a specified set of parameters.
BAND/USD in BandChain Guanyu devnet as an example, the code is as follows:
The above snippet utilizes Band’s
bandchain.js helper Node library to interact with requests made on BandChain. The code itself can then be broken down into the two main parts; specifying the required parameters and retrieving the matching request result from BandChain.
1) Specifying the Required Parameters
Before we can get the prices from BandChain, we need to specify some parameters. These parameters can be further broken down into two sections; the chain parameters and the request parameters.
The chain parameters simply consist of the endpoint to which we will be making the request. In this particular case, we will be using our devnet endpoint.
The request parameters, on the other hand, are how we will refine the request and the corresponding result that we want to retrieve. When requested, BandChain will return the results of the latest request whose input parameters match ours. The specific parameters are:
endpoint: The endpoint which we will query the result from. In this case, we will be using the BandChain
oracleScriptID: The ID of the oracle script executed to make the request
params: The input parameters to that oracle script. This includes the cryptocurrency symbol to query, the method to aggregate the results from BandChain’s validators, and the multiplier to multiply the aggregated result by
askCount: The number of validators that were requested to respond to the request
minCount: The minimum number of validators necessary for the request to proceed to the execution phase
For more information on each of these parameters, please see our whitepaper.
2) Retrieving the Matching Request Results from BandChain
Once we have specified the parameters, we can continue to make the query to BandChain.
The process starts by creating a new
bandchain object that we will use to interact with BandChain. We then use
getOracleScript function to return an object associated with the specified oracle script (oracle script ID 13 in this case).
The query itself is then made through executing the
getLatestMatchingResult function. If the execution is successful, the surrounding
getLatestRequestResult function will return the matching request’s
BAND/USD price value, along with the request’s
resolve_time, indicating when the price value was successfully aggregated and stored on BandChain.
Decentralized applications are created with interdependent elements, we have the back-end, which is responsible for logic and behavior, and the front-end which is for users to interact with the application. It’s critical that all layers in decentralized applications are indeed, decentralized.
With Band Protocol’s publicly available nodes, anyone can now request and consume external data using our custom oracle scripts in a trustless and decentralized manner including digital assets, commodities, foreign exchanges, market information, and real-world events.
There are many leading decentralized projects that we have been working with to implement such feature and we are very excited to showcase them over the coming months. Stay tuned for more updates!