Skip to content

Chainlink Oracle

Chainlink Moonbeam Banner

Introduction

Developers can now use Chainlink's decentralized Oracle network to fetch data in the Moonbase Alpha TestNet and Moonriver. Price Feeds contain real-time price data that is continuously updated by Oracle operators in a smart contract so that other smart contracts can fetch and consume it. This guide will cover the available price feeds and how to fetch the latest price data on Moonriver.

Price Feeds

Before we go into fetching the data itself, it is important to understand the basics of price feeds.

In a standard configuration, each price feed is updated by a decentralized Oracle network. Each Oracle node is rewarded for publishing the price data to the Aggregator contract. The Aggregator contract receives periodic data updates from the network of oracles and aggregates and stores the data on-chain so that consumers can easily fetch it. However, the information is only updated if a minimum number of responses from Oracle nodes are received (during an aggregation round).

The end-user can retrieve price feeds with read-only operations via an Aggregator interface, or via a Consumer interface through the Proxy.

Price Feed Diagram

Fetch Price Data

There are Data Feed contracts available for both Moonbase Alpha and Moonriver to help simplify the process of requesting price feeds. In our current configuration for Moonbase Alpha, we are running only one Oracle node that fetches the price from a single API source. Price data is checked every minute and updated in the smart contracts every hour unless there is a price deviation of 1 %. The Moonriver Data Feed contracts are updated by multiple Chainlink nodes on a regular basis.

The data lives in a series of smart contracts (one per price feed) and can be fetched with the Aggregator interface:

pragma solidity ^0.8.0;

interface AggregatorV3Interface {
    /**
     * Returns the decimals to offset on the getLatestPrice call
     */
    function decimals() external view returns (uint8);

    /**
     * Returns the description of the underlying price feed aggregator
     */
    function description() external view returns (string memory);

    /**
     * Returns the version number representing the type of aggregator the proxy points to
     */
    function version() external view returns (uint256);

    /**
     * Returns price data about a specific round
     */
    function getRoundData(uint80 _roundId) external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);

    /**
     * Returns price data from the latest round
     */
    function latestRoundData() external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}

As seen above in the interface, there are five functions for fetching data: decimal, description, version, getRoundData, and latestRoundData.

Currently, there are Data Feed contracts for the the following price pairs:

Base/Quote Data Feed Contract
BTC to USD 0x1B5C6cF9Df1CBF30387C24CC7DB1787CCf65C797
ETH to USD 0xc3cF399566220dc5Ed6C8CFbf8247214Af103C72
KSM to USD 0x6e0513145FCE707Cd743528DB7C1cAB537DE9d1B
LINK to USD 0xdD27789b504fEd690F406A82F16B45a0901172C0
BNB to USD 0xD6B013A65C22C372F995864CcdAE202D0194f9bf
FRAX to USD 0xD080d4760318710e795B0a59f181f6C1512ffB15
MIM to USD 0xdD6296BD7515271F7E4b10C3A87A2f9863fECa97
MOVR to USD 0x3f8BFbDc1e79777511c00Ad8591cef888C2113C1
USDT to USD 0xF80DAd54AF79257D41c30014160349896ca5370a
Base/Quote Data Feed Contract
BTC to USD 0xCf88A8d7fc1A687895fC8ffAad567f303926B094
ETH to USD 0x3669da30c33D27A6A579548fCfc345fE5dEdda6e
DOT to USD 0xA873F6b30aD79fCAF9b03A0A883d6D1f18D661d7
KSM to USD 0x0C515E77897b2A7181C875c88FaF9BC8E5661E3b
AAVE to USD 0x64B22D2B8c3CA311a0C2de34bf799f8101c89362
ALGO to USD 0x9fc3b0BF1156868085AFC1cFf4Bf6D85ea301371
BAND to USD 0xC5aeD933FEb49794A8Bf2FB0e73D9c958c8a07ba
LINK to USD 0x446b93236B4d34642732B8dcbeB3cb4f4FA03C70
SUSHI to USD 0x4a6Cf10C0f5c4D4e7cf7385bFfecDAec0778357C
UNI to USD 0x326997c21451DaB916F9f01684991B6169dAf3E5

For example, you can use the Aggregator interface to fetch the price feed of BTC to USD using Remix. If you need help loading a contract into Remix, check out the Using Remix page of the documentation site.

You will need to connect your MetaMask account to Remix, so make sure you have MetaMask installed and are connected to the Moonbase Alpha TestNet or Moonriver. To get help setting up MetaMask, check out the Interacting with Moonbeam Using MetaMask guide.

After creating the file and compiling the contract, you will need to follow these steps:

  1. Head to the Deploy and Run Transactions tab
  2. Set the Environment to Injected Web3
  3. If your MetaMask is already connected it will appear in the Account selector. Otherwise, you will be prompted by MetaMask to select and connect your account(s)
  4. Select the AggregatorV3Interface contract from the Contract dropdown
  5. Enter the Data Feed contract address corresponding to BTC to USD in the At Address field and click the At Address button:

    0x1B5C6cF9Df1CBF30387C24CC7DB1787CCf65C797
    
    0xCf88A8d7fc1A687895fC8ffAad567f303926B094
    

Load the Chainlink Price Feed Aggregator Interface on Moonriver

This will create an instance of the Aggregator interface that you can interact with and it will appear under the Deployed Contracts section in Remix. To get the latest price data you can follow these steps:

  1. Expand the AggregatorV3Interface contract to reveal the available functions
  2. Click latestRoundData() to query the data of the corresponding price feed, in this case BTC to USD

Interact with the Chainlink Price Feed Aggregator Interface on Moonriver

Note that to obtain the real price, you must account for the decimals of the price feed, available with the decimals() method.

If there is any specific pair you want us to include, feel free to reach out to us through our Discord server.