DEV Community

Cover image for Understanding Gas on the Blockchain: A Practical Guide Using Real Transactions
Obinna Duru
Obinna Duru

Posted on

Understanding Gas on the Blockchain: A Practical Guide Using Real Transactions

When I first encountered the concept of gas in blockchain, I understood on the surface that it measured computational work. However, I did not fully understand what that meant in practice, how gas is actually calculated under the hood, why transactions need gas limits, or how all of those numbers come together to form a transaction fee.

Reading high-level definitions is one thing, but opening a block explorer and staring at a raw transaction receipt is another. Where does a specific number like 49,785 or 152,492 gas come from? Why does a transaction that transfers 0 ETH or 0 MON still cost money? And what actually happens to the gas you don't use?

For me, the best way to truly grasp a technical concept--to the point where you can confidently explain it to someone else--is to connect the abstract math to a physical, real-world analogy and then test it against real data.

In this article, we are going to break down how gas works from the ground up. We will build a simple physical mental model, look at how the Ethereum Virtual Machine (EVM) counts work, and then inspect two real blockchain transactions: one where I interacted directly with a smart contract on Monad Testnet, and a second transaction hash I pulled from the Ethereum Sepolia Testnet explorer to see how the two networks compare.

Separating the Five Core Concepts

When people talk about "gas," they often lump several different things into one blurry idea of "the fee I pay." To make transaction receipts easy to read, we first need to separate five distinct concepts:

  1. Gas: The unit used to measure the computational work required to process a transaction.
  2. Gas Limit: The maximum amount of gas a transaction is allowed to consume.
  3. Gas Used: The amount of gas the transaction actually consumed during execution.
  4. Gas Price: The monetary rate (in cryptocurrency) paid per single unit of gas.
  5. Transaction Fee (Gas Fee): The final amount of cryptocurrency paid for processing the transaction, calculated according to the network's rules.

These concepts are closely related, but they do not mean the same thing.

A Relatable Analogy: The Delivery Truck
Think of executing a blockchain transaction like hiring a delivery truck to drive across town and flip a switch at a warehouse:

  • Gas (The Work): Driving 15 kilometers up a steep hill physically requires 10 liters of fuel. Notice that "liters" measures physical effort, not money. Whether fuel is cheap or expensive today, the engine still burns 10 liters to climb that hill.
  • Gas Limit (Your Fuel Cap): Before the driver leaves, you hand them a fuel card authorized for a maximum of 12 liters. You are saying: "This trip should take 10 liters, so I am allowing up to 12 liters as a safety buffer. If the road is blocked and you burn all 12 liters before getting there, stop driving immediately."
  • Gas Used (Actual Consumption): The driver completes the trip and the engine actually burns 10 liters.
  • Gas Price (Price per Liter): At the pump today, fuel costs $2 per liter. Tomorrow, during heavy traffic demand, it might cost $4 per liter. The distance and effort didn't change, only the price per unit of fuel.
  • Transaction Fee (The Total Bill): The final payment calculated from the fuel and the price per liter.

With that picture in mind, let's briefly look at how the blockchain measures those "liters of fuel" under the hood.

Under the Hood: How Does the Blockchain Count Gas?

When you submit a transaction on an EVM-compatible blockchain, the network doesn't guess how hard your transaction worked. Instead, every tiny instruction the computer executes--called an opcode--has a fixed cost measured in gas:

  • The Base Cover Charge (21,000 Intrinsic Gas): Before the blockchain even opens a smart contract, every standard transaction costs a flat minimum of 21,000 gas. This covers the basic work of verifying your cryptographic signature, checking your account nonce, and updating your account balance.
  • Basic Math & Memory: Inside a smart contract, simple operations like adding two numbers together (ADD) cost just 3 gas.
  • Reading & Writing Permanent Storage: Updating the blockchain's permanent database is the heaviest work a transaction can do, because every node on the network has to store that state change. Reading a variable from permanent storage for the first time in a transaction (SLOAD) costs 2,100 gas. Writing a brand-new non-zero value into an empty storage slot (SSTORE from 0 to 1) costs 20,000 gas, while updating an already non-zero slot (SSTORE from 1 to 2) costs 2,900 gas.

As your transaction runs, an internal meter starts at 21,000 and adds up the exact cost of every instruction executed along the way.

Now let's see how this looks on two real transaction receipts.

Case Study One: Calling increment() on Monad Testnet

For our first case study, I interacted directly with a smart contract deployed on Monad Testnet by calling its increment() function.

Monad Transaction hash receipt

Monad Transaction hash receipt

Here are the exact details from the transaction receipt:

Field Value
Network Monad Testnet
Function Called increment()
Transaction Status Success
Transaction Value 0 MON
Gas Limit 49,785
Gas Used (as reported) 49,785
Gas Usage 100%
Gas Price 102.82 Gwei
Transaction Fee 0.0051188937 MON

Why Did a 0 MON Transaction Still Incur a Fee?

Notice that the Transaction value is 0 MON. My transaction didn't send any MON tokens to another person or contract, yet it still charged a fee of 0.0051188937 MON. Why?

In our delivery truck analogy, Transaction value is the cash you put inside an envelope to send to someone across town. Even if you send the driver with $0 in the envelope just to flip a switch at the warehouse, the truck still has to burn fuel to drive there and flip the switch.

Look at the Solidity contract behind increment():

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

contract Counter {
    uint256 public count;

    function increment() external {
        count = count + 1;
    }
}
Enter fullscreen mode Exit fullscreen mode

This contract stores a number called count on the blockchain. Whenever someone calls increment(), the contract increases count by one (changing it from 0 to 1, or 1 to 2). Even though the code looks tiny, the blockchain still has to process the transaction, run the contract's instructions, read the current value of count, and write the updated number into permanent storage.

The transaction fee pays for that computational processing and state update, not for transferring currency.

How Was the Fee Calculated--And Why Was Gas Usage 100%?

Monad Transaction hash receipt

On this receipt, the Gas limit was 49,785, and the Gas used, as reported was also 49,785:

Gas Usage Percentage = (49,785 ÷ 49,785) × 100 = 100%
Enter fullscreen mode Exit fullscreen mode

And if we multiply the Gas limit (49,785) by the Gas price (102.82 Gwei):

$49,785 x 102.82 Gwei = 5,118,893.7 Gwei
Enter fullscreen mode Exit fullscreen mode

Using the conversion of $10⁹ Gwei per 1 MON:

Transaction Fee = 5,118,893.7 ÷ 10⁹ = 0.0051188937 MON
Enter fullscreen mode Exit fullscreen mode

This matches the displayed transaction fee down to the last digit.

But this raises an important question: Why does Monad calculate the fee using the Gas Limit, and why did the receipt report 100% gas usage?

Unlike Ethereum, which charges you based on the gas your transaction actually used after running, Monad charges based on the Gas Limit you declare upfront (and sets gasUsed equal to gasLimit on the top-level receipt).

To understand why, compare a supermarket checkout (Ethereum) with booking cargo on an airplane (Monad):

  • Ethereum (Supermarket Checkout): When an Ethereum validator builds a block, it runs each user's transaction on the spot before picking the next one. Because it runs the code immediately, it sees the exact Gas Used right away and knows exactly how much space is left in the block for the next person.
  • Monad (Booking Cargo on a Flight): To process thousands of transactions per second, Monad uses asynchronous execution: validators pack transactions into a block and lock in the order before running the smart contract code. Because the network hasn't run your code yet when packing the block, it doesn't know what your actual Gas Used will be--it only sees the Gas Limit you asked to reserve!

If Monad let users reserve massive amounts of block space using a huge Gas Limit and then only charged them for a tiny Gas Usedafterward, someone could reserve an entire block, crowd out everyone else on the network, and pay almost nothing. Just like reserving 10 seats on a bus or cargo flight where other passengers have to be turned away at the gate, on Monad you pay for the exact capacity you reserve upfront (Gas Limit).

Case Study Two: Inspecting a Transaction on Ethereum Sepolia Testnet

To see how standard Ethereum fee accounting differs, I went to the Ethereum Sepolia Testnet block explorer and pulled up a random transaction hash to inspect its receipt. In this transaction, the sender called the close() function on a smart contract.

Ethereum Transaction hash receipt

Here are the transaction details:

Field Value
Network Ethereum Sepolia Testnet
Function Called close()
Transaction Status Success
Transaction Value 0 ETH
Gas Limit 156,059
Gas Used 152,492
Gas Usage 97.71%
Effective Gas Price (as reported) 0.000153611 Gwei
Transaction Fee 0.000000023424448612 ETH

Gas Limit vs. Gas Used (And What Happens to Unused Gas)

Just like the Monad transaction, this transaction sent 0 ETH in value while paying a fee to execute smart contract logic. However, look at the difference between the limit and the actual consumption.

  • Gas limit: 156,059
  • Gas used: 152,49
156,059 - 152,492 = 3,567 unused gas
Enter fullscreen mode Exit fullscreen mode
Percentage Consumed = (152,492 ÷ 156,059) × 100 ≈ 97.71%
Enter fullscreen mode Exit fullscreen mode

Here, the transaction consumed 97.71% of its limit and left 3,567 gas (2.29%) untouched.

What happens to that 3,567 unused gas on Ethereum?

When a transaction begins on Ethereum, the protocol temporarily locks enough ETH from the sender's balance to cover the entire 156,059 Gas Limit (just like a fuel pump pre-authorizing your card before you pump). As the code executes, the meter ticks up to 152,492. The moment execution finishes, the leftover 3,567 gas is immediately refunded back to the sender's wallet inside the exact same transaction, and that unused capacity is freed up for other transactions in the block.

How the Sepolia Transaction Fee Was Calculated

Because Ethereum refunds unused gas, the final fee is calculated strictly on Gas used:

Transaction fee = Gas used x Effective gas price
Enter fullscreen mode Exit fullscreen mode

Multiplying the 152,492 gas actually consumed by the 0.000153611 Gwei effective gas price:

152,492 x 0.000153611 Gwei = 23.424448612 Gwei
Enter fullscreen mode Exit fullscreen mode

Since 1 ETH = 10⁹ Gwei:

Fee = 23.424448612 ÷ 10⁹ = 0.000000023424448612 ETH
Enter fullscreen mode Exit fullscreen mode

Notice that the unused 3,567 gas was never charged to the sender.

How Do You Know What Gas Limit to Set?

Looking at numbers like 49,785 on Monad and 156,059 on Sepolia, a natural question comes up: How does anyone know how much gas a contract will need before sending the transaction?

You don't have to calculate opcodes manually. In practice, gas limits are estimated automatically:

  1. Wallet & RPC Simulation (eth_estimateGas): Before you click "Confirm" in your wallet, the wallet silently calls a method on an RPC node called eth_estimateGas. The node runs a free practice simulation (dry run) of your transaction against the current state of the blockchain in its local memory, counts the exact gas used, and reports that number back. Your wallet then adds a small safety buffer (often 10% to 20%) in case the blockchain state changes slightly before your transaction lands in a block.
  2. Developer Tooling (Foundry): When writing smart contracts in Foundry, running forge test --gas-report or cast estimate simulates your function calls and prints out the exact gas numbers before you ever deploy to a live network.

Practical takeaway: Because Monad charges you for your declared Gas Limit, you should rely on eth_estimateGas with a modest buffer rather than manually typing in an artificially huge gas limit!

Understanding Gas Price and Gwei

Now that we've covered Gas Limit and Gas Used, let's look at Gas Price and Gwei

While gas measures the computational effort (liters of fuel), gas price is the monetary cost per single unit of gas (price per liter). Because a single unit of gas costs a tiny fraction of 1 ETH or 1 MON, we express gas prices in Gwei (short for Giga-wei) so we don't have to read strings of nine zeros:

1 ETH (or MON) = 10⁹ Gwei = 1,000,000,000 Gwei
Enter fullscreen mode Exit fullscreen mode
1 Gwei = 0.000000001 ETH
Enter fullscreen mode Exit fullscreen mode

When someone says "Gas price is 20 Gwei," they don't mean the whole transaction costs 20 Gwei. They mean each single unit of gas costs 20 billionths of an ETH.

Suppose a transaction consumes 50,000 gas:

  • At 10 Gwei per gas: 50,000 x 10 Gwei = 500,000 Gwei (0.0005 ETH).
  • At 20 Gwei per gas: 50,000 x 20 Gwei = 1,000,000 Gwei (0.001 ETH).

Notice that the computational work (50,000 gas) stayed completely identical--the fee doubled purely because network demand pushed the price per unit of gas twice as high.

Decoding EIP-1559: Base Fee, Priority Fee, and Max Fee

Both the Monad and Sepolia receipts identify the transaction type as EIP-1559 (Type 2). Under EIP-1559, the gas price is broken down into specific components. Let's look at the fee breakdown from the Sepolia receipt:

Fee Field Value
Network Ethereum Sepolia Testnet
Base Fee 0.000000015 Gwei
Max Priority Fee 0.000153596 Gwei
Max Fee 0.000153614 Gwei
Effective Gas Price 0.000153611 Gwei

Here is how these four numbers fit together:

  1. Base fee (0.000000015 Gwei): The mandatory minimum rate per unit of gas for that block, determined automatically by the network based on how full previous blocks were. On Ethereum, the base fee is burned (destroyed) rather than paid to the block proposer.
  2. Max priority fee (0.000153596 Gwei): The maximum tip per unit of gas the sender offers directly to the block proposer (validator) to incentivize them to include the transaction.
  3. Max fee (0.000153614 Gwei): The absolute ceiling per unit of gas the sender is willing to pay in total (Base fee + Priority fee) in case the base fee jumps while the transaction is waiting.
  4. Effective gas price (0.000153611 Gwei): The actual rate per unit of gas charged when the block was produced:
Effective gas price = Base fee + Effective priority fee
Enter fullscreen mode Exit fullscreen mode
0.000000015 Gwei + 0.000153596 Gwei = 0.000153611 Gwei
Enter fullscreen mode Exit fullscreen mode

Because 0.000153611 Gwei was safely below the sender's Max fee cap of 0.000153614 Gwei, the transaction paid the exact sum of the base fee and priority tip.

Why Do Transactions Need a Gas Limit? (And Why Gas Costs Change)

Preventing Runaway Code

Why does every blockchain transaction require a Gas Limit in the first place? Consider a contract function with a massive loop:

function expensiveOperation() external {
    uint256 total;

    for (uint256 i = 0; i < 1_000_000; i++) {
        total += i;
    }
}
Enter fullscreen mode Exit fullscreen mode

If a transaction could execute without an upper bound--or accidentally hit an infinite loop--every node on the network would get stuck executing that loop forever, halting the blockchain.

The Gas Limit acts as a circuit breaker. If a transaction burns through all of its allotted gas before finishing, it stops with an Out of Gas error. When that happens, all state changes are reverted as if the transaction never happened, but the sender still pays for the gas consumed because the network already spent real computational work running the code up to that point.

Keep in mind: having plenty of gas does not guarantee a transaction will succeed. If a contract fails an internal require check or permission rule, the transaction will still revert even with gas left over.

Why Two Calls to the Same Function Can Use Different Gas

It is also easy to assume that calling the same function with the same inputs will always consume the exact same amount of gas. In practice, gas usage depends on the exact state of the contract at the moment of execution.

For example, in our Counter contract, the very first call to increment() updates count from 0 to 1. Allocating a brand-new storage slot from zero to a non-zero number costs 20,000 gas. The next call updates count from 1 to 2--and overwriting an already non-zero storage slot costs 2,900 gas. Even though both users called increment(), the first call performed heavier database work under the network's rules.

Comparing the Two Receipts: Are Fees on Monad Expensive?

Monad Transaction hash receipt

Ethereum Transaction hash receipt

Let's put our Monad Testnet interaction and the Sepolia Testnet receipt side by side

Concept Monad Testnet: increment() Sepolia Testnet: close()
Gas Limit 49,785 156,059
Gas Used 49,785 152,492
Gas Usage 100% 97.71%
Effective Gas Price (as reported) 102.82 Gwei 0.000153611 Gwei
Transaction Fee 0.0051188937 MON 0.000000023424448612 ETH
Fee Calculation Gas Used x Gas Price Gas Used x Effective Gas Price

When you look at this table, you might notice that 102.82 Gwei (0.0051 MON) looks numerically much larger than 0.000153611 Gwei (0.0000000234 ETH). Does that mean gas fees on Monad are expensive?

Not at all. In real-world dollar terms, Monad is designed so transactions cost a fraction of a cent. Those raw numbers cannot be compared directly for two reasons:

  1. Different Native Tokens (MON-Gwei vs. ETH-Gwei): 1 Gwei on Ethereum is one-billionth of 1 ETH (an asset priced in thousands of dollars), whereas 1 Gwei on Monad is one-billionth of 1 MON. Comparing 0.005 MON to 0.00000002 ETH is like comparing 50 Naira (or 5 Japanese Yen) to 0.03 US Dollars--a bigger number in one currency doesn't mean a higher cost!
  2. Monad's 100 Gwei Minimum Base Fee Floor: To prevent spam when blocks aren't full, Monad enforces a minimum base fee of 100 MON-gwei (which is why our transaction was 102.82 Gwei: 100 Gwei base fee + 2.82 Gwei priority tip). Sepolia Testnet has no 100 Gwei floor, so its testnet base fee had drifted down to nearly zero (0.000000015 Gwei).

Common Misunderstandings About Gas (Quick Checklist)

  • Gas and cryptocurrency are the same thing. They are not. Gas is a measurement of computational work; ETH and MON are the native assets used to pay for that work.
  • The gas limit is the exact amount of gas a transaction will use. Not necessarily. The gas limit is the maximum allowance; actual execution often consumes less.
  • A transaction with 0 value is free. Incorrect. Both of our transactions had a value of 0, and both required fees to pay for smart contract execution.
  • A higher gas limit always means a higher fee. It depends on the network's architecture! On Ethereum, you are only billed for Gas Used, and unused gas is refunded. On Monad, because blocks are packed asynchronously before execution, you are billed for the entire Gas Limit you reserve.
  • Enough gas guarantees a transaction will succeed. Incorrect. Even with plenty of gas, a transaction will revert if it hits a failed contract condition or permission check.

How to Explain Gas to Someone Else

If someone asks you, "What is gas on the blockchain, and how do fees work?", here is how you can break it down cleanly

Gas is the unit that measures how much computational work a transaction requires--just like liters of fuel needed for a delivery truck to complete a trip.

Every transaction sets a Gas Limit (calculated automatically by your wallet simulating the transaction), which acts as a maximum fuel cap so a bad loop can't run forever. As the transaction runs, it tallies up the Gas Used based on the exact instructions executed.

To find the Transaction Fee, you multiply the gas quantity by the Gas Price (measured in Gwei). On Ethereum, you only pay for the fuel your code actually burned (Gas Used), and any unused gas is refunded immediately. On high-speed asynchronous networks like Monad, where block space is reserved before running the code, you pay for the entire fuel limit you reserved upfront (Gas Limit).

Once you separate the unit of work, the maximum allowance, the actual consumption, and the price per unit, any transaction receipt on any EVM block explorer becomes intuitive to read.

Let's Figure Out the Rest Together

Writing this guide started with a simple realization: it is completely normal to use blockchains, deploy contracts, and still feel a bit fuzzy on what every single line of a transaction receipt is actually telling you. Moving from memorizing definitions to tracing the exact numbers is how the mental model finally clicks.

Now I'd love to hear from you: What part of Gas still feels unclear, confusing, or tricky to explain?

  • Is there a field on a block explorer receipt that you've always wondered about?
  • Have you ever run into an unexpected Out of Gas error or a strange gas estimation spike while testing a contract?
  • Or do you have a favorite real-world analogy of your own for explaining gas, Gwei, or EIP-1559 to someone new?

Drop your questions, edge cases, or thoughts in the comments below--no question is too basic or too far down the rabbit hole. Let's unpack them together.

BinnaDevLab
Reasoning Precedes Coding.

Top comments (0)