DEV Community

Cover image for ⛽️ Understanding Gas in Ethereum — The Fuel of Every Transaction
Mhammed Talhaouy
Mhammed Talhaouy

Posted on

⛽️ Understanding Gas in Ethereum — The Fuel of Every Transaction

When you use Ethereum — whether sending ETH, minting NFTs, or interacting with DeFi — you’re paying gas.

But what exactly is gas?
Why does it fluctuate so much?
And how do different transaction types (Legacy, EIP-1559, EIP-2930, EIP-4844) affect how gas works?

Let’s break it all down — cleanly, deeply, and visually.


💡 What Is Gas?

Gas is the unit of computational cost in Ethereum.

Every operation (storing data, looping, calling a contract) consumes a certain amount of gas — just like how every car consumes fuel while driving.

💬 Gas measures how much work your transaction needs the Ethereum Virtual Machine (EVM) to do.


⛽️ Example: A Simple Transaction

Let’s say you send 1 ETH to a friend.
Your transaction might cost about 21,000 gas — that’s the cost of the computation to:

  • Verify your signature
  • Update account balances
  • Write to the blockchain

If you interact with a smart contract, the gas usage increases depending on:

  • Function complexity
  • Storage writes
  • External calls

💰 Gas vs Gas Price vs Gas Fee

Let’s break these terms clearly 👇

Term Meaning Example
Gas The amount of computational work (units) 21,000 gas
Gas Price How much ETH you pay per unit of gas 30 gwei
Gas Fee Total ETH you pay = Gas × Gas Price 21,000 × 30 gwei = 0.00063 ETH

⚙️ Units Refresher

  • Wei = smallest ETH unit (1 ETH = 1,000,000,000 Gwei = 10¹⁸ Wei)
  • Gwei = most common unit for gas prices

So when you see Gas Price: 25 Gwei, it means you’re paying 25 billion wei per gas unit.


🔥 How Gas Fees Are Calculated (Post EIP-1559)

In 2021, Ethereum introduced EIP-1559 — a major upgrade that changed how gas fees work.

Now, each block includes:

  • Base Fee → Minimum fee to include your transaction (burned 🔥)
  • Priority Fee (Tip) → Optional reward to miners/validators
  • Max Fee → The maximum you’re willing to pay

💵 Formula:

Total Fee = (Base Fee + Priority Fee) × Gas Used
Enter fullscreen mode Exit fullscreen mode

🧩 Example:

  • Base Fee = 20 Gwei
  • Priority Fee = 2 Gwei
  • Gas Used = 21,000

Fee = (20 + 2) × 21,000 = 462,000 Gwei = 0.000462 ETH

Of that:

  • 20 Gwei × 21,000 is burned 🔥
  • 2 Gwei × 21,000 goes to the validator 💸

🚦 Gas Limit and Refunds

🔹 Gas Limit

The maximum amount of gas you’re willing to spend.
If your transaction runs out of gas before completion → it fails ❌ but still consumes what was used.

🔹 Gas Refund

If you allocate too much gas but use less, you get the unused portion refunded (minus what was actually used).

Example:
You set 100,000 gas limit but only use 40,000 →
You pay for 40,000, not 100,000.


🔍 Gas in Smart Contracts

Every EVM operation has a gas cost:

Operation Description Gas Cost
ADD Add two numbers 3 gas
SSTORE Write to storage 20,000 gas
CALL Call another contract 700 gas + dynamic
LOG Emit event 375 + 8×data size
TRANSFER Send ETH 21,000 gas

💡 Auditor tip:
When reviewing contracts, pay attention to SSTORE and external calls — they are the most expensive operations and can cause DoS if not optimized.


🔄 All Transaction Types (Legacy → Latest)

Ethereum has evolved over time — each EIP improved how transactions handle gas.


🧾 1. Legacy Transactions (Pre-EIP-1559)

Used before August 2021.

  • Fields: nonce, gasPrice, gasLimit, to, value, data, v, r, s
  • Fee: gasPrice × gasUsed
  • Entire fee goes to miner.

⚠️ Problem: unpredictable fees — users overpaid during congestion.


🔥 2. EIP-1559 Transactions (Post-London Hard Fork)

Introduced base fee + tip model.

  • Fields: maxFeePerGas, maxPriorityFeePerGas
  • Fee split: Base fee (burned) + Tip (to miner)

✅ More predictable fees
✅ Partial ETH burn
✅ Dynamic fee adjustment

Used in most modern wallets (Metamask, etc.)


🧰 3. EIP-2930 Transactions (Access List Transactions)

Introduced access lists to reduce gas for state reads/writes.

  • Includes an accessList field (addresses + storage keys)
  • Helps contracts pre-declare what data they’ll touch.

✅ Gas optimization for complex contracts.
✅ Useful for Layer 2 and rollup environments.


💎 4. EIP-4844 (Proto-Danksharding Transactions)

Introduced in 2024 as part of the Dencun upgrade.

Adds a new transaction type for blob-carrying transactions (used by Layer 2 rollups).

  • Blobs = large chunks of off-chain data, verified by Ethereum
  • Cheaper data availability for L2s
  • Not directly stored on-chain

✅ Greatly reduces rollup costs
✅ Makes Ethereum scalable for the next decade


🧩 Transaction Type Summary

Transaction Type EIP Introduced Key Feature
Legacy Pre-2021 Fixed gas price
Type 1 (EIP-2930) 2930 2021 Access lists
Type 2 (EIP-1559) 1559 2021 Base + tip gas model
Type 3 (EIP-4844) 4844 2024 Blobs for L2 scalability

🧠 Tips for Developers & Auditors

  1. Always check gas optimization.
    Use tools like Foundry gas snapshot, Slither, or Hardhat-gas-reporter.

  2. Understand the transaction type.
    Some gas refund or storage behaviors differ between EIPs.

  3. Beware of reentrancy and DoS via gas exhaustion.
    Never rely on “gasLeft” logic for security.

  4. Simulate transactions before deploying (e.g., cast estimate in Foundry).

  5. Use events instead of on-chain storage when you can — it’s cheaper.


⚙️ TL;DR Summary

Concept Description
Gas Unit of computational cost
Gas Price ETH per gas unit (in Gwei)
Gas Fee Total ETH = Gas × Price
EIP-1559 Base fee + tip system (burns ETH)
EIP-2930 Access lists for optimization
EIP-4844 Blobs for cheaper L2 data
Auditor Focus Optimize heavy ops (SSTORE, CALL), verify transaction type

🚀 Final Thoughts

Gas is the heartbeat of the EVM — it keeps the network efficient, fair, and spam-resistant.

As Ethereum evolves through EIPs, gas handling becomes:

  • More predictable (EIP-1559),
  • More efficient (EIP-2930),
  • And more scalable (EIP-4844).

For developers and auditors alike, understanding gas deeply means writing cheaper, safer, and more predictable smart contracts.

Top comments (0)