TLDR:
In today’s digital age, accessing real-time and reliable cryptocurrency data is crucial for many applications. While many resort to third-party APIs, these can sometimes introduce risks, such as downtime, potential manipulation, or sudden discontinuation of services. Chainlink, a decentralized oracle network, solves this dilemma as a dependable bridge between on-chain and off-chain data. Paired with the robust infrastructure of an Ethereum node hosted by Chainstack, we can further enhance the reliability of our data source.
One advantage of this approach is using decentralized exchange (DeX) oracles, which largely eliminates the risk of relying on single-point-of-failure third-party APIs. This ensures uninterrupted access to critical crypto pricing data and fosters a decentralized ethos in our applications.
In this guide, we’ll demonstrate how to harness the power of Chainlink oracles by using a Chainstack Ethereum node to fetch real-time crypto prices from the network, giving you a resilient and dependable data source.
For our purpose, we’ll use node.js alongside the web3.js library, allowing us to communicate with the Ethereum blockchain. We aim to fetch prices for five cryptocurrencies: BTC, ETH, LINK, BNB, and LTC against USD.
Check out Web3 node.js: From zero to a full-fledged project to learn how to set up a node.js project.
Follow these steps to deploy an Ethereum node:
Create a new project in a directory and open a terminal, then run:
In the same directory, create a new file named index.js
and paste the following code:
Add your Chainstack Ethereum endpoint to web3
, save and run node index
in the terminal; the console will display an object with the most up-to-date prices.
The script starts by importing the web3
library and creating an instance to an Ethereum node, then the bulk of the logic is as follows:
Specify the Chainlink price feeds. Ethereum smart contracts represent Chainlink’s decentralized price feeds. Each cryptocurrency price feed has a unique contract address. Our script stores these addresses in the pairs
dictionary with the cryptocurrency pair name as the key. You can find additional price feed contract addresses on Chainlink’s Price Feed Contract Addresses.
Specify the Chainlink price feed contract addresses.
Chainlink’s decentralized price feeds are represented through smart contracts. Each price feed for a specific cryptocurrency pair is associated with a unique contract address. In our script, these addresses are stored in a dictionary named pairs
, where the key is the name of the cryptocurrency pair. Refer to Chainlink’s Price Feed Contract Addresses page for additional price feed contract addresses.
The aggregatorV3Interface ABI.
We add the aggregatorV3Interface
ABI required to interact with the smart contract. The ABI outlines the functions in the smart contract so the library knows how to use them.
Interact with Chainlink price feeds.
Chainlink price feeds are implemented using the AggregatorV3Interface, which provides several functions for data interaction. Among these functions, latestRoundData
is the one we use to fetch the latest price information. For a complete reference to the functions and capabilities of the AggregatorV3Interface, you can consult Chainlink’s API Reference page.
Fetching the Prices.
In our fetchPrices
function, we iterate through the specified pairs, and for each pair:
latestRoundData
function to retrieve the most recent price data.1e8
and round to two decimal places) and store it in our conversionRate
dictionary.Continual data refresh.
To ensure we continually have up-to-date price information, we call our fetchPrices
function every minute using JavaScript’s setInterval
function.
Following the above steps, you’ve successfully built a robust and lightweight script to fetch real-time cryptocurrency prices directly from the Ethereum blockchain, eliminating the need for third-party APIs or services. With this approach, you ensure data accuracy and gain unparalleled control over how you fetch and manage this data. Happy coding and trading!
TLDR:
In today’s digital age, accessing real-time and reliable cryptocurrency data is crucial for many applications. While many resort to third-party APIs, these can sometimes introduce risks, such as downtime, potential manipulation, or sudden discontinuation of services. Chainlink, a decentralized oracle network, solves this dilemma as a dependable bridge between on-chain and off-chain data. Paired with the robust infrastructure of an Ethereum node hosted by Chainstack, we can further enhance the reliability of our data source.
One advantage of this approach is using decentralized exchange (DeX) oracles, which largely eliminates the risk of relying on single-point-of-failure third-party APIs. This ensures uninterrupted access to critical crypto pricing data and fosters a decentralized ethos in our applications.
In this guide, we’ll demonstrate how to harness the power of Chainlink oracles by using a Chainstack Ethereum node to fetch real-time crypto prices from the network, giving you a resilient and dependable data source.
For our purpose, we’ll use node.js alongside the web3.js library, allowing us to communicate with the Ethereum blockchain. We aim to fetch prices for five cryptocurrencies: BTC, ETH, LINK, BNB, and LTC against USD.
Check out Web3 node.js: From zero to a full-fledged project to learn how to set up a node.js project.
Follow these steps to deploy an Ethereum node:
Create a new project in a directory and open a terminal, then run:
In the same directory, create a new file named index.js
and paste the following code:
Add your Chainstack Ethereum endpoint to web3
, save and run node index
in the terminal; the console will display an object with the most up-to-date prices.
The script starts by importing the web3
library and creating an instance to an Ethereum node, then the bulk of the logic is as follows:
Specify the Chainlink price feeds. Ethereum smart contracts represent Chainlink’s decentralized price feeds. Each cryptocurrency price feed has a unique contract address. Our script stores these addresses in the pairs
dictionary with the cryptocurrency pair name as the key. You can find additional price feed contract addresses on Chainlink’s Price Feed Contract Addresses.
Specify the Chainlink price feed contract addresses.
Chainlink’s decentralized price feeds are represented through smart contracts. Each price feed for a specific cryptocurrency pair is associated with a unique contract address. In our script, these addresses are stored in a dictionary named pairs
, where the key is the name of the cryptocurrency pair. Refer to Chainlink’s Price Feed Contract Addresses page for additional price feed contract addresses.
The aggregatorV3Interface ABI.
We add the aggregatorV3Interface
ABI required to interact with the smart contract. The ABI outlines the functions in the smart contract so the library knows how to use them.
Interact with Chainlink price feeds.
Chainlink price feeds are implemented using the AggregatorV3Interface, which provides several functions for data interaction. Among these functions, latestRoundData
is the one we use to fetch the latest price information. For a complete reference to the functions and capabilities of the AggregatorV3Interface, you can consult Chainlink’s API Reference page.
Fetching the Prices.
In our fetchPrices
function, we iterate through the specified pairs, and for each pair:
latestRoundData
function to retrieve the most recent price data.1e8
and round to two decimal places) and store it in our conversionRate
dictionary.Continual data refresh.
To ensure we continually have up-to-date price information, we call our fetchPrices
function every minute using JavaScript’s setInterval
function.
Following the above steps, you’ve successfully built a robust and lightweight script to fetch real-time cryptocurrency prices directly from the Ethereum blockchain, eliminating the need for third-party APIs or services. With this approach, you ensure data accuracy and gain unparalleled control over how you fetch and manage this data. Happy coding and trading!