I am trying to estimate the gas fee in USD for a function call. I came across the term "Chainlink Fast Gas oracle" but I couldn't find any documentation on how to use this in a smart contract? There is this website but nothing else: https://data.chain.link/ethereum/mainnet/gas/fast-gas-gwei
2 Answers
On your link you have the contract address
, which is 0x169e633a2d1e6c10dd91238ba11c4a708dfef37c
And you have this specific function that you can call to get the current gwei:
function latestRoundData()
public
view
virtual
override
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
It will returns something like this:
You can call this function from your smart contract if needed, if you want the USD price, you can do the same process by multiplying ETH/USD price by GWEI, ETH/USD
-
I guess the
answer
is the price of 1 GWEI (or wei? what is the unit?) in USD. I've tried this with the Rinkeby test network oracle address (0xCe3f7378aE409e1CE0dD6fFA70ab683326b73f04
) and it returned this:344162968520
. If this is the price of 1 GWEI in USD, it is too high (344.16296852 USD). I am confused.– MehmedBMar 29, 2022 at 12:20 -
One last thing: I guess there is no gas fee oracle deployed for the polygon network, right? I am looking here: docs.chain.link/docs/matic-addresses– MehmedBMar 29, 2022 at 12:22
-
1in the example above the price is expressed in
wei
, so 37 gwei. On rinkeby it shows40189528044
which is ~40 gwei You can check all the conversions here: eth-converter.com/extended-converter.html Mar 29, 2022 at 12:25 -
I had a similar question and needed more step by step approach.
I read this blog and it helped. https://dev.to/costamatheus97/web3-how-tos-interacting-with-external-smart-contracts-in-solidity-3of1
Then, I mocked it up on REMIX. I used two contracts to simulate the current LINK fast gas contract. One to represent the current contract and a second one to represent any future contract for gas (or if LINK changes address in future).
In both contracts, I used block.timestamp as a return value in but offset the time by A LOT of seconds so I could verify the contract update function and then see I got a different return value.
Since Chainlink uses int256 on their fast gas contract "latestAnswer" (the value I am looking for), I type casted the block.timestamp response to int256 to mimic the response I should get from Chainlink more closely.
That way, if you need to flip it back to uint256 for calculations or comparisons in your own contract you can see how to do that too. Maybe a better way, but I'm a hack so YMMV.
Chainlink Fast Gas contract 1
pragma solidity ^0.8.15;
contract CHLK_FastCash_1 {
function latestAnswer() external view returns(int256){
uint256 datetime = block.timestamp;
int256 converttype = int256(datetime);
return converttype;
}
}
Chainlink Fast Gas contract 2
pragma solidity ^0.8.15;
contract CHLK_FastCash_2 {
function latestAnswer() external view returns(int256){
uint256 datetime = block.timestamp - 10000;
int256 converttype = int256(datetime);
return converttype;
}
}
My Contract
pragma solidity ^0.8.15;
interface Interface_CHLK_FastGas {
function latestAnswer() external view returns(int256);
}
contract my_Contract {
address _CHLKFastGasAddress;
function get_External_Contract_data() external view returns(int256 latestGasFeeWei) {
Interface_CHLK_FastGas CHLK_getCurrentGas = Interface_CHLK_FastGas(_CHLKFastGasAddress);
(latestGasFeeWei) = CHLK_getCurrentGas.latestAnswer();
return latestGasFeeWei;
}
function setChainLinkFastGasContractAddress(address _address) external {
_CHLKFastGasAddress = _address;
}
}
In case you are new and wondering like I did... What do I do with the above?
Go to remix.ethereum.org (aka REMIX), create 3 contract files with solidity code above. Compile all 3 contracts. Deploy the first two contracts to mimic chainlink fast gas. This will give you their contract addresses.
Then deploy "my_Contract". Once you have all 3 deployed, you can copy the contract address of the first contract and paste/apply that in the "my_Contract" function 'setChainLinkF...' section. This will set the variable to the first contract address for mock Chainlink contract 1.
Once you do that, you can call the get_External function (press the button) from the first chainlink contract in the "my_Contract" deployed contract.
Make note of the ordinal time value you receive.
When you are ready to switch to the second chainlink contract, you can copy the second deployed contract address, paste/apply that into the "my_Contract" function function 'setChainLinkF...' input area (and press button to push this to contract). This will change "my_Contract" to use contract 2's address. Then, when you press the 'get_External...' button you will read the older timestamp value in contract 2.
Make note of the ordinal time value you receive. It will be ~1000 less than the first value.
HTH
S_Z