In solidity, converting is not just taking the value of one Eth and pegging it against the USD. We must be able to tell solidity how to do it, but since the rate is constantly changing, it is sometimes difficult to know the latest value.
This is where Chainlink oracle comes in.
Chainlink is like a decentralized aggregator that takes a consensus of different nodes, making it tamper-proof and reliable for the network to work with.
Networks were not communicating with the outside world as there were deterministic approaches, making it difficult to get accurate world data into the chain.
Let us dive in and see an example of how to do this. We will be getting the real conversion value of ETH to USD.
First, we create a smart contract.
Full code can be found here:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract PriceOFETHTOUSD{
uint minimalValue = 50;
function PriceFeed() public payable{
msg.value > minimalValue;
}
function GetLastestPrice() public view returns (uint){
AggregatorV3Interface Price = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 price,,,) = Price.latestRoundData();
return uint (price * 1e18);
}
function GetValueInDOllar(uint _ethAmount) public view returns(uint){
uint ValuePrice = GetLastestPrice();
uint AmountinDollars =(ValuePrice * _ethAmount) /1e18;
return AmountinDollars;
}
}
Here we made a function where we called the payable function which can be called when we carry out a transaction.
Next we said the msg.value > minimalValue
which we created a variable for above uint minimalValue = 50;
This makes sense but we only determine the value of USD by making it 50, how do we allow our solidity to talk to the internet and get real value USD to ETH.
Next we will create a new function that will speak to the chainlink and get data from an Oracle and gives us an updated value.
For our function to speak to the oracle we must have two things.
- Address
- ABI — Application Binary Interface.
Here we made a function called the payable function called when we carry out a transaction.
To get the chainlink data feed address of ETH — USD conversion we must copy the address, remember for this article we will be using rinkerby testnet data feed which is good enough for our testing.
We also need to get our ABI which we can get from here as it calls the answer from this address we got from Chainlink.
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
Copying whole code makes our work messy, we would need to import it directly from Github, using the import.
function GetLastestPrice() public view returns (uint){
AggregatorV3Interface Price = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 price,,,) = Price.latestRoundData();
return uint (price * 1e18);
}
Here we created the second function, which will get the priceFeed, and then we want to return the price of ETH to USD inside our contract, and since we have imported the contract, we can call functions from it.
But we only need the price and not everything; solidity is warning us that the other values are unused.
Let us make our syntax much more readable and straightforward.
We called the AggregatorV3Interface Price = AggregatorV3Interface( address )
solidity knows there are other variables which we skipped by using the comma.
We ask solidity to return the price * 1e18.
Do not worry about 1e18 as we convert using the smallest value, which wei.
Remember, at this point; we can only get the price in ETH, which is not what we have set out to do.
Let’s create a new function that allows us to convert the ETH to USD.
function GetValueInDOllar(uint _ethAmount) public view returns(uint){
uint ValuePrice = GetLastestPrice();
uint AmountinDollars =(ValuePrice * _ethAmount) /1e18;
return AmountinDollars;
}
Here we created a function that called an argument of _ethAmount
.
We want it to return the converted value, which we called here by saying whatever we get in GetLastestPrice()
should be called ValuePrice
we said the AmountinDollars
is equal to (PriceEth * _ethAmount)
. _ethAmount is the value the user inputs inside the contract.
Return the value.
Conclusion 🔗
Here we went through the steps to follow when converting. Although we did not touch on many best practices, we saw how to convert from ETH to USD. You can go ahead and try other conversion values like ETH to BTC.