I would like to get the historical Gas (Gwei) price in hourly or minutely with safelow, Standard, fast and fastest. I am aware of the etherscan.io which gives avg gas price for a day and also ethstation which gives realtime data. I am able to get the real-time from https://www.etherchain.org/tools/gasPriceOracle. Is there a way where i can get historical prices?
4 Answers
You can follow this website they have provided their API as well click here
and you can get a lot of other detailed information as well
You can get the historical data from the Chainlink Price feeds fast gas proxy contract if you wanted to pull from an on-chain record.
You can see the history right in the contract
/** This example code is designed to quickly deploy an example contract using Remix.
*/
pragma solidity ^0.6.7;
import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract HistoricalPriceConsumerV3 {
AggregatorV3Interface internal priceFeed;
/**
* Network: Mainnet
* Aggregator: Fast Gas / Gwei Price
* Address: 0x169E633A2D1E6c10dD91238Ba11c4A708dfEF37C
*/
constructor() public {
priceFeed = AggregatorV3Interface(0x169E633A2D1E6c10dD91238Ba11c4A708dfEF37C);
}
/**
* Returns historical price for a round id.
* roundId is NOT incremental. Not all roundIds are valid.
* You must know a valid roundId before consuming historical data.
*
* ROUNDID VALUES:
* InValid: 18446744073709562300
* Valid: 18446744073709562301
*
* @dev A timestamp with zero value means the round is not complete and should not be used.
*/
function getHistoricalPrice(uint80 roundId) public view returns (int256) {
(
uint80 id,
int price,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = priceFeed.getRoundData(roundId);
require(timeStamp > 0, "Round not complete");
return price;
}
}
Since all the truly full length historic sources are paid in some-way, you should be able to materialize a view in bigquery using the public crypto datasets that is SQL accessible if that works for you.
Here's what I'm using:
SELECT
DATE(block_timestamp) as block_date,
Format("%.*f",3,(AVG(gas_price)/1000000000)) as avg_gas_price
FROM `bigquery-public-data.crypto_ethereum.transactions`
-- WHERE DATE(block_timestamp) > "2022-02-20"
group by 1
order by 1 desc
That's daily level granularity but you can break out the block_timestamp by minute or hour if you need it.
If you can provide an algorithm for calculating safelow, standard, fast, and fastest I will build that in as well and update this answer.
Edit: more information on the crypto_ethereum.transaction
data "source"
- Github project for the loader for this: https://github.com/blockchain-etl/ethereum-etl
EDIT:
I've been able to hone in on some estimates for different transaction types that can be used to calculate the costs for those transactions historically.
It goes something like this:
standard_trans_eth = 21000 * avg_gas_price / 1e9
erc20_trans_eth = 65000 * avg_gas_price / 1e9
stablecoin_swap_eth = 100000 * avg_gas_price / 1e9
token_swap_eth = 200000 * avg_gas_price / 1e9
pool_deposit_eth = 300000 * avg_gas_price / 1e9
nft_mint_721_eth = 350000 * avg_gas_price / 1e9
nft_mint_1155_eth = 150000 * avg_gas_price / 1e9
ico_contract_eth = 1000000 * avg_gas_price / 1e9
If you want the resulting price in usd, just multiply that _eth
column by the historic price of eth at the time.
Example: Jan 21, 2022 when avg gas price was 185.589 & ETH opened ~3000 USD, closed ~2500 USD.
standard_trans_eth = 21000 gas limit * 186 gwei / 1e9 * $2750 usd = $10.75
Going to be working on a complete visualization / dataset of this in bigquery but this is how I'm working it so far.
EDIT 2:
Highly recommend using the "Estimated Cost of Transactions" table for representative gas limits from: https://etherchain.org/tools/gasnow
The closest you can get is using this graph of historical gas prices.
Unless somebody has been storing the data you are asking for in a database, you will not be able to retrieve it. You can, however, run the same algorithm that ethgasstation.info uses on the graph above and interpolate the data, if desired.