Categories
DeFi javascript web3js

Reading Chainlink price oracles from JavaScript

A common problem you may face in Ethereum programming is getting a price feed of digital currencies. Chainlink is an oracle platform that is used by many DeFi projects and incentives actors to publish on-chain several information like prices of tokens or price of Ether. in this tutorial we’ll learn how you can retrieve the price of Ether from the Chainlink oracle using JavaScript and Web3.js.

If you’re not sure how to interact with the Ethereum blockchain using Javascript and web3.js, we encourage you to read this tutorial first to get started with web3.js.

The first thing you’ll need to get started is to pick the oracle you want to use. You can browse the list of available feeds from the Chainlink website here.

Once you click on a oracle you want to use you’ll see a new page containing the oracle address. In our case, we’ll use the ETH/USD price oracle. The address where the oracle is is displayed at the top, in our case it’s: 0xF79D6aFBb6dA890132F9D7c355e3015f15F3406F. Just copy it for later use.

Then we’ll need to get the ABI of a Chainlink price feed contract so we can interact with it using Web3.js:

Join the newsletter

Get a weekly summary of what is happening in the Ethereum developer space for free

const CHAINLINK_ORACLE_ABI = [
    {
        "constant": true,
        "inputs": [],
        "name": "latestAnswer",
        "outputs": [
            {
                "name": "",
                "type": "int256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "latestTimestamp",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
]

We will then instanciate our web3 instance and the oracle contract instance to be able to call the price feed:

const ORACLE_ADDRESS = "0xF79D6aFBb6dA890132F9D7c355e3015f15F3406F"

var Web3 = require('web3');
let w3 = new Web3(new Web3.providers.HttpProvider("[YOUR BLOCKCHAIN PROVIDER]"));

let oracle = new w3.eth.Contract(CHAINLINK_ORACLE_ABI, ORACLE_ADDRESS);

We can then interact with the onchain oracle contract to ask it two informations: The latest price feed it got and the time this latest price feed was validated:

oracle.methods.latestAnswer().call({}, function(error, res) {
    if (error != null) {
        console.log(error)
        return;
    }
    console.log("Latest price was:" ,res)
});

oracle.methods.latestTimestamp().call({}, function(error, res) {
    if (error != null) {
        console.log(error)
        return;
    }
    console.log("Latest timestamp for price was:" ,res)
});

At the time of writting the article the oracle answers is:

You can note that you may need to add some decimals to the result given for the price. You can easily manipulate uin256 returned by web3js with the BigNumber library in JavaScript.

If you’re doing any critical operation with this returned information, we encourage you to check nd compare if the latest time the oracle was updated was not too long ago. For this you can check the returned timestamp with the current timestamp on your machine or the latest blocknumber.

Leave a Reply