Before the implementation of EIP-1559, Ethereum's gas fee mechanism relied on a simple auction model. Transactions from the highest bidders were prioritized, leading to unpredictable and volatile fee fluctuations due to manual bidding. EIP-1559 introduced a more stable and predictable gas fee structure. This article explores how EIP-1559 works and how to estimate gas fees using its API.
How EIP-1559 Transformed Ethereum Gas Fees
Many applications allow users to set custom gas bids with options like "slow," "average," and "fast." With EIP-1559, these estimations are more reliable. Here’s how the gas fee is calculated:
Gas Fee Calculation Formula
Units of gas used × (Base fee + Priority fee)
- Units of gas used: The amount of gas consumed (e.g., 21,000 for a standard ETH transfer).
- Base fee: A dynamically adjusted fee (denominated in gwei).
- Priority fee (tip): An optional incentive for miners (also in gwei).
👉 Learn more about Ethereum gas fees
Breaking Down the Base Fee
The base fee adjusts automatically based on the previous block’s congestion:
- If the previous block was overutilized, the base fee increases.
- If it was underutilized, the base fee decreases.
This adjustment is calculated using Ethereum’s consensus rules:
from web3 import Web3
def calc_base_fee(parent_block):
target = parent_block.gasLimit // 2
if parent_block.gasUsed > target:
# Increase base fee if over target
delta = parent_block.gasUsed - target
fee_increase = (parent_block.baseFeePerGas * delta) // target // 8
return parent_block.baseFeePerGas + max(fee_increase, 1)
else:
# Decrease base fee if under target
delta = target - parent_block.gasUsed
fee_decrease = (parent_block.baseFeePerGas * delta) // target // 8
return max(parent_block.baseFeePerGas - fee_decrease, 0)
Estimating Priority Fees
Unlike the base fee, priority fees depend on user behavior. To predict them:
- Scan recent blocks for typical tip amounts.
- Use the
eth_feeHistory
API to gather percentile data (e.g., 25th, 50th, 75th percentiles).
fee_history = ethClient.eth.fee_history(4, "latest", [25, 50, 75])
blocks = [
{
"number": block["number"],
"baseFee": block["baseFeePerGas"],
"priority_fees": block["reward"],
}
for block in fee_history["blocks"]
]
Calculating Average Tips
- Low (25th percentile): Cheapest successful transactions.
- Medium (50th percentile): Typical tips.
- High (75th percentile): Faster confirmations.
Implementing Gas Fee Options
Using historical data, you can offer users three fee tiers:
Tier | Priority Fee | Use Case |
---|---|---|
Slow | 25th percentile | Cost-efficient |
Average | 50th percentile | Balanced speed/cost |
Fast | 75th percentile | Urgent transactions |
👉 Explore Ethereum’s latest gas trends
Frequently Asked Questions
Q: Why did EIP-1559 change gas fees?
A: To stabilize fees and reduce volatility caused by manual bidding.
Q: How is the base fee determined?
A: Automatically adjusted based on the previous block’s gas usage.
Q: Can I still set a custom tip?
A: Yes, but EIP-1559 ensures a more predictable base fee.
Q: How many blocks should I analyze for fee estimates?
A: 4–20 blocks, depending on desired accuracy.
Conclusion
EIP-1559 made Ethereum’s gas fees more predictable. By combining base fee calculations with historical priority fee data, developers can offer users reliable "slow," "average," and "fast" gas options. For further optimization, consider analyzing more blocks or refining tip estimation methods.
Happy building! 🚀