EIP‑1559 fee calculation ambiguity in web3.eth.getGasPrice for low‑traffic workloads
0 reputation · 11 Jul 2023, 18:05 UTC
0 reputation · 11 Jul 2023, 18:05 UTC
When deploying a lightweight service on an EIP‑1559‑enabled chain, the goal is to keep transaction costs minimal. The library’s web3.eth.getGasPrice method, however, returns the legacy gasPrice field even on networks that support dynamic base fees. This legacy value can exceed the sum of the current baseFeePerGas and a modest priority fee, leading to overpayment, especially when traffic is low and base fees fluctuate.
The ambiguity lies in whether to rely on getGasPrice universally or to implement a conditional strategy that computes maxFeePerGas and maxPriorityFeePerGas based on the latest block’s baseFeePerGas. The library offers no dedicated method for this calculation, and the community has not reached consensus on the optimal approach.
Key questions for decision‑making:
web3.eth.getGasPrice with a custom calculation that uses baseFeePerGas plus a chosen priority fee for all EIP‑1559 chains?maxFeePerGas and maxPriorityFeePerGas?getGasPrice behavior have on predictable cost budgeting for low‑traffic applications?32275 reputation · 12 Jul 2023, 01:04 UTC
Replace web3.eth.getGasPrice with a custom calculation that uses the latest block’s baseFeePerGas plus a chosen priority fee (with a small buffer). This yields the correct maxFeePerGas and maxPriorityFeePerGas parameters for EIP‑1559 chains and avoids the over‑payment observed with the legacy gasPrice field.
maxFeePerGas and maxPriorityFeePerGas.maxFeePerGas and maxPriorityFeePerGas; maxFeePerGas should be set to baseFeePerGas + priorityFee (optionally plus a 10‑20 % buffer to absorb short‑term base‑fee swings).web3.eth.getGasPrice returns the legacy gasPrice field, which on EIP‑1559 chains reflects the miner tip and can exceed baseFeePerGas + modest priority fee when traffic is low.The library’s getGasPrice method was designed for pre‑EIP‑1559 chains and simply returns the gasPrice value from the latest block. On EIP‑1559 networks that value is not the total fee a user will pay; it only represents the priority (tip) component. Consequently, when the base fee is low (e.g., during off‑peak periods) the returned legacy price can be substantially higher than the sum of base fee + tip, leading to overpayment and unpredictable cost budgeting for low‑traffic services.
const block = await web3.eth.getBlock('latest');baseFeePerGas from the block (in wei).maxPriorityFeePerGas = priorityFeemaxFeePerGas = baseFeePerGas + priorityFee + buffermaxFeePerGas and maxPriorityFeePerGas fields (instead of gasPrice).If you need to fine‑tune the buffer size, please specify the acceptable over‑payment threshold (e.g., “no more than 10 % above baseFee + tip”) so the buffer can be calibrated accordingly.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.