{"id":26469846,"url":"https://github.com/ocdbytes/gasestimatoreth","last_synced_at":"2025-03-19T17:09:35.584Z","repository":{"id":192168634,"uuid":"618508054","full_name":"ocdbytes/GasEstimatorEth","owner":"ocdbytes","description":"Calculating the estimated gas fee of a transaction in current block using the data from the previous blocks.","archived":false,"fork":false,"pushed_at":"2023-03-24T16:09:15.000Z","size":53,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-14T10:46:08.199Z","etag":null,"topics":["eip-1559","ethereum","ethereum-blockchain","ethereum-gas-prices","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ocdbytes.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-03-24T16:08:29.000Z","updated_at":"2023-03-24T16:10:32.000Z","dependencies_parsed_at":"2023-09-03T04:46:04.895Z","dependency_job_id":null,"html_url":"https://github.com/ocdbytes/GasEstimatorEth","commit_stats":null,"previous_names":["ocdbytes/gasestimatoreth"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FGasEstimatorEth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FGasEstimatorEth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FGasEstimatorEth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FGasEstimatorEth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ocdbytes","download_url":"https://codeload.github.com/ocdbytes/GasEstimatorEth/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244470251,"owners_count":20457908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eip-1559","ethereum","ethereum-blockchain","ethereum-gas-prices","typescript"],"created_at":"2025-03-19T17:09:35.057Z","updated_at":"2025-03-19T17:09:35.575Z","avatar_url":"https://github.com/ocdbytes.png","language":"TypeScript","readme":"# Gas Fee Estimator (ETH)\n\nWe 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.\n\nWe will use this logic to get the estimated gas fees\n\n```text\nTop 1 %ile of the transactions to get slow gas fee\nTop 50 %ile of the transactions to get average gas fee\nTop 99 %ile of the transactions to get fast gas fee\n```\n\nDemo Code :\n\n```ts\nweb3.eth\n  .getFeeHistory(4, \"pending\", [25, 50, 75])\n  .then((result) =\u003e {\n    console.log(result);\n});\n\n// gives 4 blocks and their transaction data\n```\n\nO/P :\n\n```sh\n{\n  baseFeePerGas: [\n    '0x1ca4585f75',\n    '0x20088ba00f',\n    '0x1f69bc3768',\n    '0x1dcf8fe9b1',\n    '0x1c0df207ce'\n  ],\n  gasUsedRatio: [\n    0.9736275666666667,\n    0.4225367666666667,\n    0.29597783333333333,\n    0.2643383\n  ],\n  oldestBlock: '0x84e95a',\n  reward: [\n    [ '0x362845373', '0x3eefd5de7', '0x4729167a3' ],\n    [ '0x59682f00', '0x59682f00', '0x9502f900' ],\n    [ '0x59682f00', '0x59682f00', '0xdf736f7f' ],\n    [ '0x40a406d8', '0x59682f00', '0x59682f00' ]\n  ]\n}\n```\n\nI wrote a formatter to format the above data into more readable form so the above data is represented as :\n\n```sh\n[\n  {\n    block_number: 8710493,\n    baseFeePerGas: 128036366769,\n    gasUsedRatio: 0.3185674666666667,\n    priorityFeePerGasPercentiles: [ 1084491480, 1500000000, 1500000000 ]\n  },\n  {\n    block_number: 8710494,\n    baseFeePerGas: 122228876174,\n    gasUsedRatio: 0.46493013333333333,\n    priorityFeePerGasPercentiles: [ 1331000000, 1500000000, 1500000000 ]\n  },\n  {\n    block_number: 8710495,\n    baseFeePerGas: 121157238577,\n    gasUsedRatio: 0.397112,\n    priorityFeePerGasPercentiles: [ 1500000000, 1500000000, 5000000000 ]\n  },\n  {\n    block_number: 8710496,\n    baseFeePerGas: 118040832087,\n    gasUsedRatio: 0.9201721666666667,\n    priorityFeePerGasPercentiles: [ 1500000000, 1500000000, 2500000000 ]\n  }\n]\n```\n\nNow after writing the logic our final output is :\n\n```ts\nweb3.eth\n  .getFeeHistory(historicalBlocks, \"pending\", [1, 50, 99])\n  .then((result) =\u003e {\n    const res = formatResult(result, historicalBlocks);\n\n    const slow = calcAverage(\n      res.map((block) =\u003e block.priorityFeePerGasPercentiles[0])\n    );\n    const average = calcAverage(\n      res.map((block) =\u003e block.priorityFeePerGasPercentiles[1])\n    );\n    const fast = calcAverage(\n      res.map((block) =\u003e block.priorityFeePerGasPercentiles[2])\n    );\n\n    // Getting the latest block\n    web3.eth.getBlock(\"pending\").then((block) =\u003e {\n      const baseFeeOfLatestBlock = Number(block.baseFeePerGas);\n      console.log(`Gas Estimation (Block Number : ${Number(block.number)}) :\nslow 🐌 : ${slow + baseFeeOfLatestBlock}\naverage 🚩 : ${average + baseFeeOfLatestBlock}\nfast ⚡ : ${fast + baseFeeOfLatestBlock}`);\n    });\n  });\n```\n\n```sh\nGas Estimation (Block Number : 8710534) :\nslow 🐌 : 160758582325\naverage 🚩 : 161582842530\nfast ⚡ : 455478648655\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focdbytes%2Fgasestimatoreth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Focdbytes%2Fgasestimatoreth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focdbytes%2Fgasestimatoreth/lists"}