Gas fees
Understanding Gas Fees on the EVM and How to Optimize Transaction Costs
TL;DR
Some versions of the Ethers.js library hardcode the maxPriorityFee
to 1.5 gwei which may be unnecessarily high depending on the current base fee. To avoid overpaying for a transaction, calculate the maxPriorityFee
manually.
Gas fees are a critical component of the Ethereum Virtual Machine (EVM) ecosystem, ensuring that the network remains secure and efficient. They incentivize validators to process transactions and execute smart contracts, playing a pivotal role in maintaining decentralized operations.
The Role of Gas Fees in the EVM
Every transaction on the EVM requires computational resources, and gas fees are the mechanism by which users pay for those resources. Gas fees consist of two main components:
Base Fee: Introduced with Ethereum's London Hard Fork (EIP-1559), the base fee is a mandatory, network-determined amount that adjusts dynamically based on network demand. It is burned, reducing the total ETH supply, which helps stabilize gas prices over time.
Priority Fee (Tip): The priority fee is an optional amount that users can include to incentivize miners or validators to prioritize their transactions. This tip is paid directly to the miner or validator and is in addition to the base fee.
The Problem with Hardcoded maxPriorityFee in Ethers.js
The popular Ethers.js library, widely used by developers for building decentralized applications (dApps), hardcodes the maxPriorityFee
to 1.5 gwei in some versions. While this value might suffice in high-demand situations, it often leads to users overpaying for transactions, particularly when network or application demand is low.
Example Scenario
Base Fee: 0.000000008 gwei
Max Priority Fee: 1.5 gwei (hardcoded by Ethers.js)
In this scenario, the priority fee is disproportionately high compared to the base fee. Instead of paying a small additional amount to incentivize miners, the user ends up paying almost the entire transaction cost as a tip. This inefficiency can significantly increase transaction expenses, especially for frequent users or in low-cost networks like Botanix.
The Fix: Calculating the maxPriorityFee Dynamically
To prevent overpayment, developers should avoid relying on the hardcoded maxPriorityFee
value provided by Ethers.js. Instead, they can calculate an appropriate priority fee dynamically based on the current base fee and network conditions.
Steps to Calculate the maxPriorityFee Dynamically
Fetch the Current Base Fee Use the network’s RPC endpoint to retrieve the current base fee from the latest block.
Calculate an Appropriate Priority Fee Determine a reasonable multiplier or fixed amount relative to the base fee. For example:
Override the Default Ethers.js Behavior Explicitly set the
maxPriorityFee
when sending transactions:
By calculating the maxPriorityFee
dynamically, developers can significantly reduce transaction costs for their users while ensuring fair compensation for validators.
Last updated