Code Monkey home page Code Monkey logo

gasestimatoreth's Introduction

Gas Fee Estimator (ETH)

We will be using getFeeHistory to get the data of the previous blocks and then we will use that data to get the estimated gas fee for a transaction to be included.

We will use this logic to get the estimated gas fees

Top 1 %ile of the transactions to get slow gas fee
Top 50 %ile of the transactions to get average gas fee
Top 99 %ile of the transactions to get fast gas fee

Demo Code :

web3.eth
  .getFeeHistory(4, "pending", [25, 50, 75])
  .then((result) => {
    console.log(result);
});

// gives 4 blocks and their transaction data

O/P :

{
  baseFeePerGas: [
    '0x1ca4585f75',
    '0x20088ba00f',
    '0x1f69bc3768',
    '0x1dcf8fe9b1',
    '0x1c0df207ce'
  ],
  gasUsedRatio: [
    0.9736275666666667,
    0.4225367666666667,
    0.29597783333333333,
    0.2643383
  ],
  oldestBlock: '0x84e95a',
  reward: [
    [ '0x362845373', '0x3eefd5de7', '0x4729167a3' ],
    [ '0x59682f00', '0x59682f00', '0x9502f900' ],
    [ '0x59682f00', '0x59682f00', '0xdf736f7f' ],
    [ '0x40a406d8', '0x59682f00', '0x59682f00' ]
  ]
}

I wrote a formatter to format the above data into more readable form so the above data is represented as :

[
  {
    block_number: 8710493,
    baseFeePerGas: 128036366769,
    gasUsedRatio: 0.3185674666666667,
    priorityFeePerGasPercentiles: [ 1084491480, 1500000000, 1500000000 ]
  },
  {
    block_number: 8710494,
    baseFeePerGas: 122228876174,
    gasUsedRatio: 0.46493013333333333,
    priorityFeePerGasPercentiles: [ 1331000000, 1500000000, 1500000000 ]
  },
  {
    block_number: 8710495,
    baseFeePerGas: 121157238577,
    gasUsedRatio: 0.397112,
    priorityFeePerGasPercentiles: [ 1500000000, 1500000000, 5000000000 ]
  },
  {
    block_number: 8710496,
    baseFeePerGas: 118040832087,
    gasUsedRatio: 0.9201721666666667,
    priorityFeePerGasPercentiles: [ 1500000000, 1500000000, 2500000000 ]
  }
]

Now after writing the logic our final output is :

web3.eth
  .getFeeHistory(historicalBlocks, "pending", [1, 50, 99])
  .then((result) => {
    const res = formatResult(result, historicalBlocks);

    const slow = calcAverage(
      res.map((block) => block.priorityFeePerGasPercentiles[0])
    );
    const average = calcAverage(
      res.map((block) => block.priorityFeePerGasPercentiles[1])
    );
    const fast = calcAverage(
      res.map((block) => block.priorityFeePerGasPercentiles[2])
    );

    // Getting the latest block
    web3.eth.getBlock("pending").then((block) => {
      const baseFeeOfLatestBlock = Number(block.baseFeePerGas);
      console.log(`Gas Estimation (Block Number : ${Number(block.number)}) :
slow ๐ŸŒ : ${slow + baseFeeOfLatestBlock}
average ๐Ÿšฉ : ${average + baseFeeOfLatestBlock}
fast โšก : ${fast + baseFeeOfLatestBlock}`);
    });
  });
Gas Estimation (Block Number : 8710534) :
slow ๐ŸŒ : 160758582325
average ๐Ÿšฉ : 161582842530
fast โšก : 455478648655

gasestimatoreth's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.