Beginner
How to Fetch the Current Price of Ethereum in Solidity
Welcome to this tutorial on how to fetch the current price of Ethereum in Solidity.
Fetching price data into a Solidity smart contract is a common requirement for DeFi applications. It allows you to access current prices for Ethereum, Bitcoin, or other cryptocurrencies in your smart contract code. One way to fetch this data is by using Chainlink Price Feeds.
Chainlink Price Feeds use a decentralized network of Chainlink oracles to aggregate multiple sources of data and provide a trusted answer for the current price. These oracles feed data into reference contracts, where the results are then aggregated in an Aggregator Smart Contract as the latest, trusted answer. By using multiple sources of data aggregated by multiple nodes, we can ensure that our price data is of the highest quality and not subject to exploits or price oracle attacks.
Smart Contract
To fetch the current price of Ethereum in Solidity using Chainlink Price Feeds, the first step is to connect your Remix IDE with a ETH testnet. This is necessary because deploying a smart contract on the Ethereum network requires paying gas fees, and you will need to have some ETH in your wallet to cover these fees.
Once you are connected to a testnet and have enough ETH, you can write a contract that includes a function to fetch the gas price. To do this, you will need to import the AggregatorV3Interface contract interface and create an instance of it in a local variable. This interface allows your smart contract to reference the on-chain Price Feeds on the Kovan testnet.
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol" AggregatorV3Interface internal priceFeed;
Next, we can see a Price Feed reference contract was initialized in our constructor. The ETH/USD Price Feed reference contract on the Kovan Testnet is deployed at the address 0x9326BFA02ADD2366b30bacB125260Af641031331.
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
We can then see a function has been defined to obtain the latest price from the Price Feed Aggregator contract that was instantiated in the constructor above. To do this, a new function was defined that calls the latestRoundData function from the Aggregator proxy contract. This is the function that returns the current state of the proxy contract, and in this case we are taking the current price and returning it in our consuming function.
// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; contract Price{ AggregatorV3Interface internal priceFeed; constructor(){priceFeed = AggregatorV3Interface(0x779877A7B0D9E8603169DdbD7836e478b4624789);} function getLatestPrice() public view returns (int) { ( uint80 roundID, int price, uint startedAt, uint timeStamp, uint80 answeredInRound ) = priceFeed.latestRoundData(); return price; } }
Deploy and testing the smart contract
Once your contract is written, you can compile it in Remix and deploy it to the Kovan testnet. To do this, go to the deployment tab in Remix and change the environment to “Injected Web3”. Make sure the wallet address you are using is the one in your MetaMask wallet that contains some ETH obtained earlier. Press the deploy button and follow the prompts. The end result should be that your smart contract is deployed to the Kovan testnet. Make sure to take note of the deployed contract address via the transaction output in the Remix console.
To test your contract, simply execute the “getLatestPrice” function. The result should be that the function returns the latest price from the ETH/USD proxy contract, which you can then use on-chain in your smart contract. Note that you do not need to send any LINK or spend any ETH to make this request – it is a pure read of the data in the on-chain ETH-USD Aggregator contract.
With this, you have successfully completed this tutorial on how to fetch the current price of Ethereum in Solidity using Chainlink Price Feeds. To submit your code for review, record an explainer video and share the GitHub URL for the task.