Chainlink price feeds on Binance Smart Chain

Chainlink data feeds are the fastest way to connect a smart contract to real time market data. These feeds allow you to easily capture the latest market price in one function call.

There are many use cases when real time data is needed for contract evaluation. For example: lending and borrowing to determine collateral or real time pricing for synthetic contracts.

Below is an example of a smart contract that integrates ETH, BTC and BNB in three separate calls. I kept it very simple so you can follow the pattern. The contract addresses are for Binance Smart Chain main net and do not require any link tokens to execute.

This contract is very simple and uses the AggregatorV3Interface. This interface abstracts and defines the functions implemented by Chainlink price feeds.

This contract can also be expanded. For example create one function that accepts any symbol as input. This feature can be used internally or referenced in another contract.

pragma solidity ^0.6.7;

import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";

contract GetPricesFromBSC {

    //state variabile written to the block chain
    AggregatorV3Interface internal priceFeedETH;
    AggregatorV3Interface internal priceFeedBTC;
    AggregatorV3Interface internal priceFeedBNB;

    constructor() public {
        priceFeedETH = AggregatorV3Interface(0x9ef1B8c0E4F7dc8bF5719Ea496883DC6401d5b2e);
        priceFeedBTC = AggregatorV3Interface(0x264990fbd0A4796A3E3d8E37C4d5F87a3aCa5Ebf);
        priceFeedBNB = AggregatorV3Interface(0x0567F2323251f0Aab15c8dFb1967E4e8A7D42aeE);
    }

    //returns the latest price
    function getLatestETHPrice() public view returns (int) {
        (
            uint80 roundID, 
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeedETH.latestRoundData();
        return price;
    }
    
    //returns the latest price
    function getLatestBTCPrice() public view returns (int) {
        (
            uint80 roundID, 
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeedBTC.latestRoundData();
        return price;
    }
    
    //returns the latest price
    function getLatestBNBPrice() public view returns (int) {
        (
            uint80 roundID, 
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeedBNB.latestRoundData();
        return price;
    }

}

Try it in Remix

As a reminder use this link for instructions on how to deploy a contract to the Binance Smart Chain.

Use the link below to refer to the Chainlink documentation for Ethereum, Binance Smart Chain and Matic price feed addresses. https://docs.chain.link/docs/reference-contracts . There are test net addresses for Ethereum and Matic but unfortunately not for Binance Smart Chain. Use the link below to go directly to the Chainlink Binance Smart Chain price feed addresses https://docs.chain.link/docs/binance-smart-chain-addresses. Change the addresses that correspond to the block chain and environment (test, mainnet) that you require and deploy from Remix. It will work on all three block chains and is very simple.

Other Blockchains to build and test on

Next Review – Create a yield farm smart contract in Solidity

1 thought on “Chainlink price feeds on Binance Smart Chain

Leave a Reply