ganache icon indicating copy to clipboard operation
ganache copied to clipboard

feat: add `eth_feeHistory` JSON-RPC endpoint

Open tenthirtyone opened this issue 2 years ago • 1 comments

Fixes: https://github.com/trufflesuite/ganache/issues/1470 Adds eth_feeHistory to the json rpc api. This endpoint returns gas used, baseFeePerGas, and effective reward by array of percentages accounting for gas used for a range of blocks.

tenthirtyone avatar Jul 12 '22 07:07 tenthirtyone

Description

 eth_feeHistory(
    blockCount: QUANTITY,
    newestBlock: QUANTITY | Ethereum.Tag,
    rewardPercentiles: number[]
  )

For a range of blocks starting at newestBlock and going back blockCount blocks (inclusive of newestBlock) return

{
    oldestBlock: string; // oldest valid block
    baseFeePerGas: string[]; // baseFeePerGas for each block
    gasUsedRatio: number[]; // gasUsed / totalGas for each block
    reward?: string[]; // max effective reward per rewardPercentile accounting for gasUsed for each block
  };

Effective reward for a transaction is calculated depending on the transaction type. For type 1 legacy transactions this is tx.gasPrice - baseFeePerGas; For type 2 EIP-1559 transactions this is min(maxPriorityFeePerGas, (maxFeePerGas - baseFeePerGas)

For each block, transactions are sorted ascending by effective reward. RewardPercentile is calculated as targetGas = (blockGasUsed * rewardPercentile). Then, calculate a running sum of gas used for the transactions. When targetGas <= txGasUsed, return the effective reward of the current transaction.

image

To Test

The easiest way to test is to mainnet fork ganache and compare curl results from ganache to infura.

$ npm run start -- --port 8645 --fork mainnet --fork.blockNumber 15050015

Important block ranges/numbers to test: 15050010 is the date of London Hardfork (requesting 15050011 and going back before London, thus before EIP-1559 txs) 0 - Genesis Block (requesting blocks from before genesis should be ignored and only valid blocks returned) "latest"

ER:

$ curl -X POST -H "Content-Type: application/json" --data '{ "id": 1337, "jsonrpc": "2.0", "method": "eth_feeHistory", "params": ["0x5", "0xE5A51D", [10, 50, 80]] }' https://mainnet.infura.io/v3/[YOUR_INFURA_KEY]
                                                     
{"jsonrpc":"2.0","id":1337,"result":{"baseFeePerGas":["0x5620a134b","0x60e3d6a2c","0x6244f29e1","0x6830847ea","0x6b6c1600f","0x78d7abf3b"],"gasUsedRatio":[0.9998421666666667,0.5569443,0.7409783333333333,0.624108008136612,0.9997199997386664],"oldestBlock":"0xe5a519","reward":[["0x59682f00","0x59682f00","0xa6e49c00"],["0x0","0x59682f00","0xb8c63f00"],["0x3b9aca00","0x59682f00","0x1180db091"],["0x3d809a16","0x77359400","0xb2d05e00"],["0x59682f00","0x9502f900","0x2540be400"]]}}
$ curl -X POST -H "Content-Type: application/json" --data '{ "id": 1337, "jsonrpc": "2.0", "method": "eth_feeHistory", "params": ["0x5", "0xE5A51D", [10, 50, 80]] }' http://localhost:8645             
                                                                            7 ↵ 
{"id":1337,"jsonrpc":"2.0","result":{"baseFeePerGas":["0x5620a134b","0x60e3d6a2c","0x6244f29e1","0x6830847ea","0x6b6c1600f","0x78d7abf3b"],"gasUsedRatio":[0.9998421666666666,0.5569443,0.7409783333333333,0.624108008136612,0.9997199997386664],"oldestBlock":"0xe5a519","reward":[["0x59682f00","0x59682f00","0xa6e49c00"],["0x0","0x59682f00","0xb8c63f00"],["0x3b9aca00","0x59682f00","0x1180db091"],["0x3d809a16","0x77359400","0xb2d05e00"],["0x59682f00","0x9502f900","0x2540be400"]]}}

Call Outs

~Infura rounds the last digit of the gasUsedRatio, we truncate. I believe this matches geth but I borked my geth instance and wasn't going to wait to resync before opening the PR. It's not technically in spec but I think it would make more sense to ceil than round, which wouldn't match infura anyway.~ fixed

tenthirtyone avatar Jul 15 '22 23:07 tenthirtyone

Congrats, your important contribution to this open-source project has earned you a GitPOAP!

GitPOAP: 2022 Ganache Contributor:

GitPOAP: 2022 Ganache Contributor GitPOAP Badge

Head to gitpoap.io & connect your GitHub account to mint!

Learn more about GitPOAPs here.

gitpoap-bot[bot] avatar Oct 21 '22 19:10 gitpoap-bot[bot]