Ethereum Gas Mechanism Explained: A Deep Dive into Gas and Gas Prices

Β·

Overview

After Ethereum's London upgrade, the network implemented EIP1559 for gas calculation. This introduced a more complex gas mechanism, which we'll explore in detail here.

Key topics covered:

Note: This article refers to "miners" as the merge was imminent during writing.


Core Concepts

Gas vs. Gas Price

Gas:

Contract MethodGas Used
deploy1,200,000
transfer21,000

Gas Price:

πŸ‘‰ Understand Ethereum gas like a pro


Gas Limit Estimation

Ethereum provides the eth_estimateGas RPC API to predict gas requirements.

Example 1: ETH Transfer

Request:

{
  "jsonrpc": "2.0",
  "method": "eth_estimateGas",
  "params": [{
    "from": "0x8D976...",
    "to": "0xd3CdA...",
    "value": "0x186a0"
  }],
  "id": 0
}

Response:

{
  "jsonrpc": "2.0",
  "result": "0x5208",  // 21,000 in decimal
  "id": 0
}

Example 2: Smart Contract Interaction

For WETH deposits (deposit() function):

{
  "jsonrpc": "2.0",
  "method": "eth_estimateGas",
  "params": [{
    "to": "0xC02aaA...",
    "data": "0xd0e30db0"  // deposit() function selector
  }],
  "id": 1
}

Key Takeaways

  1. Gas Limits

    • Transactions fail if Gas Limit < Actual Gas Used (e.g., this failed call).
    • Unused gas is refunded.
  2. EIP1559 Updates

    • Introduces a base fee + priority tip model.
    • Reduces fee volatility and improves UX.

πŸ‘‰ Master Ethereum transactions today


FAQ

Q: Why does ETH transfer cost exactly 21,000 gas?

A: It’s a fixed cost for simple transfers without contract logic.

Q: How is gas price determined post-EIP1559?

A: A base fee (burned) + priority tip (to miners).

Q: Can I estimate gas without running a transaction?

A: Yes, using eth_estimateGas or tools like Foundry.