Gas

如何獲取歷史 Gas (Gwei) 價格

  • April 3, 2022

我想以安全低、標準、快速和最快的方式獲得每小時或每分鐘的歷史 Gas (Gwei) 價格。我知道 etherscan.io 提供一天的平均汽油價格,以及提供實時數據的 ethstation。我可以從https://www.etherchain.org/tools/gasPriceOracle獲得實時資訊。有什麼方法可以獲取歷史價格嗎?

你可以關注這個網站,他們也提供了他們的 API,點擊這裡

您還可以獲得很多其他詳細資訊

如果您想從鏈上記錄中提取歷史數據,您可以從Chainlink Price feed 快速 gas 代理合約中獲取歷史數據。

您可以在契約中查看歷史記錄

/** 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;
   }
}

引用自:https://ethereum.stackexchange.com/questions/57215