ETH Explorer: Understanding Ethereum Gas Fees with EIP-1559

·

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)

👉 Learn more about Ethereum gas fees

Breaking Down the Base Fee

The base fee adjusts automatically based on the previous block’s congestion:

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:

  1. Scan recent blocks for typical tip amounts.
  2. 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

Implementing Gas Fee Options

Using historical data, you can offer users three fee tiers:

TierPriority FeeUse Case
Slow25th percentileCost-efficient
Average50th percentileBalanced speed/cost
Fast75th percentileUrgent 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! 🚀