Skip to content

Razor Network Oracle

Razor Network Moonbeam Diagram

Introduction

Developers can now fetch prices from Razor Network’s oracle using a Bridge contract deployed on the Moonbase Alpha TestNet. This Bridge acts as middleware, and events emitted by it are fetched by the Razor Network's oracle infrastructure, sending prices to the Bridge contract.

To access these price feeds, we need to interact with the Bridge contract address, which can be found in the following table:

Network Contract Address
Moonbase Alpha 0x53f7660Ea48289B5DA42f1d79Eb9d4F5eB83D3BE

Jobs

Each data-feed has a Job ID attached to it. For example:

Job ID Underlying Price [USD]
1 ETH
2 BTC
3 Microsoft Stocks

You can check Job IDs for each data-feed at the following link. Price feeds are updated every 5 minutes. More information can be found in [Razor's documentation website][https://docs.razor.network/].

Get Data From Bridge Contract

Contracts can query on-chain data such as token prices, from Razor Network's oracle by implementing the interface of the Bridge contract, which exposes the getResult and getJob functions.

pragma solidity 0.6.11;

interface Razor {

    function getResult(uint256 id) external view returns (uint256);

    function getJob(uint256 id) external view returns(string memory url, string memory selector, string memory name, bool repeat, uint256 result);
}

The first function, getResult, takes the Job ID associated with the data-feed and fetches the price. For example, if we pass in 1, we will receive the price of the data-feed related to the Job ID.

The second function, getJob, takes the Job ID associated with the data-feed and fetches the general information regarding the data-feed, such as the name of the data-feed, the price, and the URL being used to fetch the prices.

Example Contract

We've deployed the bridge contract in the Moonbase Alpha TestNet (at address 0xC6F33c0F15FE5e3A51A019524ac43574cFF29EFB) so you can quickly check the information fed from Razor Network's oracle.

The only requirement is the Bridge interface, which defines getResult structure and makes the functions available to the contract for queries.

We can use the following Demo script. It provides various functions:

  • fetchPrice: a view function that queries a single Job ID. For example, to fetch the price of ETH in USD, we will need to send the Job ID 1
  • fetchMultiPrices: a view function that queries multiple Job IDs. For example, to fetch the price of ETH and BTC in USD, we will need to send the Job IDs [1,2]
  • savePrice: a public function that queries a single Job ID. This sends a transaction and modifies the price variable stored in the contract.
  • saveMultiPrices: a public function that queries multiple Job IDs. For example, to fetch the price of ETH and BTC in USD, we will need to send the Job IDs [1,2]. This sends a transaction and modifies the pricesArr array stored in the contract, which will hold the price of each pair in the same order as specified in the input
pragma solidity 0.6.11;

interface Razor {
    function getResult(uint256 id) external view returns (uint256);
    function getJob(uint256 id) external view returns(string memory url, string memory selector, string memory name, bool repeat, uint256 result);
}

contract Demo {
    // Interface
    Razor internal razor;

    // Variables
    uint256 public price;
    uint256[] public pricesArr;

    constructor(address _bridgeAddress) public {
        razor = Razor(_bridgeAddress); // Bridge Contract Address
                                       // Moonbase Alpha 0xC6F33c0F15FE5e3A51A019524ac43574cFF29EFB
    }

    function fetchPrice(uint256 _jobID) public view returns (uint256){
        return razor.getResult(_jobID);
    }

    function fetchMultiPrices(uint256[] memory jobs) external view returns(uint256[] memory){
        uint256[] memory prices = new uint256[](jobs.length);
        for(uint256 i=0;i<jobs.length;i++){
            prices[i] = razor.getResult(jobs[i]);
        }
        return prices;
    }

    function savePrice(uint _jobID) public {
        price = razor.getResult(_jobID);
    }

    function saveMultiPrices(uint[] calldata _jobIDs) public {
        delete pricesArr;

        for (uint256 i = 0; i < _jobIDs.length; i++) {
            pricesArr.push(razor.getResult(_jobIDs[i]));
        }

    }
}

Try it on Moonbase Alpha

The easiest way to try their Oracle implementation is by pointing the interface to the Bridge contract deployed at address 0xC6F33c0F15FE5e3A51A019524ac43574cFF29EFB:

pragma solidity 0.6.11;

interface Razor {
    function getResult(uint256 id) external view returns (uint256);
    function getJob(uint256 id) external view returns(string memory url, string memory selector, string memory name, bool repeat, uint256 result);
}

With it, you will have two view functions available, very similar to our previous examples:

  • getPrice: provides the price feed for a single job ID given as input to the function. For example, to fetch the price of ETH in USD, we will need to send the Job ID 1
  • getMultiPrices: provides the price feed for multiple Job IDs given as an array input to the function. For example, to fetch the price of ETH and BTC in USD, we will need to send the job IDs [1,2]

Let's use Remix to fetch the BTC price in USD.

After creating the file and compiling the contract, head to the "Deploy and Run Transactions" tab, enter the contract address (0xC6F33c0F15FE5e3A51A019524ac43574cFF29EFB), and click on "At Address." Make sure you have set the "Environment" to "Injected Web3" so that you are connected to Moonbase Alpha (through the Web3 provider of the wallet).

Razor Remix deploy

This will create an instance of the demo contract that you can interact with. Use the functions getPrice() and getMultiPrices() to query the data of the corresponding pair.

Razor check price

The information presented herein has been provided by third parties and is made available solely for general information purposes. Moonbeam Foundation does not warrant the accuracy, completeness or usefulness of this information. Any reliance you place on such information is strictly at your own risk. Moonbeam Foundation disclaims all liability and responsibility arising from any reliance placed on this information by you or by anyone who may be informed of any of its contents. All statements and/or opinions expressed in these materials are solely the responsibility of the person or entity providing those materials and do not necessarily represent the opinion of Moonbeam Foundation. The information should not be construed as professional or financial advice of any kind. Advice from a suitably qualified professional should always be sought in relation to any particular matter or circumstance. The information herein may link to or integrate with other websites operated or content provided by third parties, and such other websites may link to this website. Moonbeam Foundation has no control over any such other websites or their content and will have no liability arising out of or related to such websites or their content. The existence of any such link does not constitute an endorsement of such websites, the content of the websites, or the operators of the websites. These links are being provided to you only as a convenience and you release and hold Moonbeam Foundation harmless from any and all liability arising from your use of this information or the information provided by any third-party website or service.