You can use Chainlink Data Feeds to connect your smart contracts to asset pricing data like the ETH / USD feed. These data feeds use data aggregated from many independent Chainlink node operators. Each price feed has an onchain address and functions that enable contracts to read pricing data from that address.
This guide uses the Hardhat development environment to deploy and interact with the contracts. To learn more about Hardhat, read the Hardhat Documentation.
- Git: Make sure you have Git installed. You can check your current version by running in your terminal and download the latest version from the official Git website if necessary.
- Nodejs and npm: Install the latest release of Node.js 20. Optionally, you can use the nvm package to switch between Node.js versions with
nvm use 20. To ensure you are running the correct version in a terminal, typenode -v.$ node -v v20.11.0
- Testnet funds: This guide requires testnet ETH on Ethereum Sepolia. If necessary, go to faucets.chain.link and get testnet ETH on Ethereum Sepolia.
This example contract obtains the latest price answer from the BTC / USD feed on the Sepolia testnet, but you can modify it to read any of the different Types of Data Feeds.
The contract has the following components:
-
The
importline imports an interface namedAggregatorV3Interface. Interfaces define functions without their implementation, which leaves inheriting contracts to define the actual implementation themselves. In this case,AggregatorV3Interfacedefines that all v3 Aggregators have the functionlatestRoundData. You can see the complete code for theAggregatorV3Interfaceon GitHub. -
The
constructor() {}initializes an interface object nameddataFeedthat usesAggregatorV3Interfaceand connects specifically to a proxy aggregator contract that is already deployed at0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43. The interface allows your contract to run functions on that deployed aggregator contract. -
The
getChainlinkDataFeedLatestAnswer()function calls yourdataFeedobject and runs thelatestRoundData()function. When you deploy the contract, it initializes thedataFeedobject to point to the aggregator at0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43, which is the proxy address for the Sepolia BTC / USD data feed. Your contract connects to that address and executes the function. The aggregator connects with several oracle nodes and aggregates the pricing data from those nodes. The response from the aggregator includes several variables, butgetChainlinkDataFeedLatestAnswer()returns only theanswervariable.
-
Clone the repository that contains the Hardhat project setup for this guide. This repository contains the Solidity contract and the Hardhat configuration files you need to deploy and interact with the contract.
git clone https://github.com/smartcontractkit/smart-contract-examples.git cd data-feeds/getting-started/hardhat -
Install all the dependencies:
npm install
-
Set an encryption password for your environment variables. This password needs to be set each time you create or restart a terminal shell session.
npx env-enc set-pw
-
Set the required environment variables using the following command:
npx env-enc setPRIVATE_KEY: The private key for your testnet wallet that will deploy and interact with the contracts. If you use MetaMask, follow the instructions to Export a Private Key.ETHEREUM_SEPOLIA_RPC_URL: The Remote Procedure Call (RPC) URL for the Ethereum Sepolia network. You can obtain one by creating an account on Alchemy or Infura and setting up an Ethereum Sepolia project.
Execute the following command to deploy the DataConsumerV3 contract on the Ethereum Sepolia testnet:
npx hardhat deployDataConsumerV3 --network ethereumSepoliaAfter a few seconds, the transaction completes. Expect output similar to the following in your terminal:
ℹ Compiling contracts...
Compiled 2 Solidity files successfully (evm target: paris).
ℹ Starting deployment of DataConsumerV3 with account: 0x45C90FBb5acC1a5c156a401B56Fea55e69E7669d
✔ DataConsumerV3 deployed at: 0xcbEAC520915727e2cf242feA77EEEEEb319A43bB on ethereumSepoliaSave the deployed contract address. You will use this address later.
Execute the following command to get the latest answer from the aggregator contract:
npx hardhat getLatestAnswer --data-consumer-v3 <DataConsumerV3Address> --network ethereumSepoliaReplace <DataConsumerV3Address> with the address of the DataConsumerV3 contract you saved earlier.
Expect output similar to the following in your terminal:
✔ Latest BTC / USD Data Feed answer: 6292416053902In this example, the answer is the latest price. The returned answer is an integer, so it is missing its decimal point. You can find the correct number of decimal places for this answer on the Price Feed addresses page by clicking the Show more details checkbox. The answer on the BTC / USD feed uses 8 decimal places, so an answer of 6292416053902 indicates a BTC / USD price of 62924.16053902. Each feed uses a different number of decimal places for answers.