Fetching Off-Chain Data with Band Protocol on Oasis Sapphire Chain

Fetching Off-Chain Data with Band Protocol on Oasis Sapphire Chain

The Oasis Sapphire Chain is a layer-two EVM-compatible protocol that enables building confidential or privacy-supported smart contract backends for Dapp applications using Solidity.

When building backends for Web2 applications, it is easier to make an API request to fetch off-chain data such as stock prices, currency rates, etc.

However, the challenge with building our smart contract backend is that the blockchain is limited to its immediate environment and cannot interact with external entities, which are necessary for providing the various off-chain data needed for our Dapp application to work properly.

BLOCKCHAIN ORACLE

As a result of the challenge described above, there has to be a way of fetching off-chain data into our smart contract. From time to time, this data is needed to build highly efficient Dapp applications.

In a bid to solve this issue, a blockchain oracle was created. In simpler terms, blockchain oracles are decentralized protocols that assist with bringing off-chain data into our smart contract code. They achieve this by deploying smart contracts on various chains, which then get updated periodically with that data.

Having looked at what blockchain oracle are and how they work, it is important to learn how to work with one by building an actual smart contract.

To that effect, we will be building a rate fetching smart contract, which helps with fetching the rate for a specific token pair. We will be using it to get the rate for the BTC/USD pair.

Recently, Band Protocol (a blockchain oracle) just added support for the Oasis Sapphire and Oasis Emerald chain, i.e., Band Protocol can now be used in getting off-chain data into any smart contract deployed on either the Oasis Sapphire or Oasis Emerald chain.

We will be using Band Protocol in building our rate getter smart contract by using Band Protocol's standard Dataset which is a smart contract deployed on various chains supported by Band Protocol and updated periodically. The sole aim of the standard Dataset Smart contract is to help in fetching the rate for a specific token pair, e.g., BTC/USD. You can learn more about Band Protocol here.

Building A Rate Getter Smart Contract Using Solidity

Setting Up The Development Environment.

There is not much setup to be done because we will be using the Remix online IDE for our development. You can head over to Remix using this link

Writing Our Solidity Code

Create a filecalled RateGetter.sol, copy and paste the code snippet below into it.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
import "./Idataset.sol";



contract RateGetter{
       IStdReference public DatasetContract;

    constructor( address _datasetContractAddress ){
        DatasetContract = IStdReference( _datasetContractAddress); 

    }


      function getRate(string memory from, string memory to) external view returns (IStdReference.ReferenceData memory) {
        return DatasetContract.getReferenceData(from, to);
    }


}

Create another file named IDataset.sol, which contains the interface of the Band Protocol Dataset smart contract we are interacting with to obtain off-chain data.

Now, let's take a deeper look at the various components that make up the smart contract:

  • Constructor Function: The constructor function is only called once, and that is during smart contract deployment. In this function, we are creating a contract instance and assigning it to the DatasetContract variable for later use.

  • GetRate Function: This function enables the fetching of the rate for a particular pair. In this function, we are interacting with the getReferenceData function provided to us in the Band Protocol Dataset smart contract to fetch a specific token pair

Adding Sapphire chain to your metamask

To add Oasis Sapphire Testnet to your project, kindly visit this link.

Deploying Your Contract To Oasis Sapphire Chain

Note: It is recommended that you use Solidity version less than or equal to 0.8.0

Follow the steps below to deploy your contract:

  1. Head over to remix, save, and compile your contract.

  2. Open the menu bar by the left hand side. Click on the icon as shown in the image below.

  3. Navigate to the table called testnet, or mainnet, containing the names of blockchains and their corresponding addresses.

    We are interested in the address attached to the Oasis Sapphire Chain. Copy it, and head over to Remix.

  4. Paste the address in the box as shown in the image below, then click on the deploy button.

  5. Hurray!!! We have been able to deploy our contract to the Oasis Sapphire chain. The next step is to interact with our contract.

Testing Our Deployed Contract

At this point, you should have been able to successfully deploy your contract to the Oasis Sapphire chain.

Now, let's test our contract by calling our getRate function to get the conversion rate for the BTC/USD pair.

After a successful deployment of our smart contract, a new UI will be provided by Remix, enabling us to interact with our contract as shown in the image below.

Now, let's interact with our getRate function. From the image above, you can see that our getRate function accepts two parameters: the FROM (base value of the pair), and the TO (quote value).

So, we want to get the conversion rate for the BTC/USD. Automatically, the value of FROM will be BTC, while the value of TO will be USD.

Click on the call button , and a struct value will be returned, having this structure as shown below.

struct ReferenceData { 
uint256 rate; 
uint256 lastUpdatedBase; 
uint256 lastUpdatedQuote;
}

let's take a deeper look into the various properties of the struct

  • Rate: This is the rate value for a specific pair. And the value returned is multiplied by 1e18. so, to get the actual value you have to divide by 1e18

  • Last updated base: it indicates the last time the base value was updated (since the UNIX epoch)

  • Last updated quote: it indicates the last time the base value was updated (since the UNIX epoch)

After calling the getRate function this is the value returned

{
rate:19231000000000000000000,

lastUpdatedBase:1674041289,

lastUpdatedQuote:1712660515

}

No need to dive deeper into what the various properties of the struct are; we have looked into it previously.

Yeah, at this point, we have been able to build our RateGetter smart contract and also utilize the Band Protocol to fetch off-chain data into our smart contract. To learn more about the Band Protocol, you can visit their official documentation here. Also, visit the Oasis Sapphire documentation to start building your confidential smart contract.