The Anatomy of a Transaction: Nonces, Gas, and RLP

It's not just 'sending money.' A deep dive into RLP encoding, the EIP-1559 fee market, and why Nonce gaps cause stuck transactions.

Intermediate 40 min read Expert Version →

🎯 What You'll Learn

  • Decode a raw transaction hex string (RLP)
  • Calculate the exact cost of an EIP-1559 transaction
  • Debug 'Stuck' transactions using Nonce replacement
  • Trace the Mempool lifecycle (Pending -> Mined -> Finalized)
  • Analyze the 'Dark Forest' of the public mempool

📚 Prerequisites

Before this lesson, you should understand:

Introduction

To a user, a transaction is a button click. To a protocol engineer, a transaction is a cryptographically signed state transition instruction.

When you send 1 ETH, you are not “moving” digital coins. You are broadcasting a signed blob of data that authorizes the global state machine (EVM) to decrement your balance variable and increment someone else’s.

In this lesson, we will dissect that blob byte-by-byte.


The Raw Data: RLP Encoding

Ethereum uses Recursive Length Prefix (RLP) serialization. A transaction is a list of fields serialized into a byte array.

The Fields (EIP-1559)

  1. ChainId: Which network? (1 = Mainnet, 11155111 = Sepolia)
  2. Nonce: Sequence number (Anti-replay).
  3. MaxPriorityFee: Tip to the miner.
  4. MaxFee: Absolute max you are willing to pay.
  5. GasLimit: Max computation steps allowed.
  6. To: Recipient address.
  7. Value: Amount of wei to transfer.
  8. Data: Payload (for contract calls).
  9. V, R, S: The ECDSA signature.

Let’s Decode One

If you broadcast this hex, you send money.

// A raw signed transaction looks like this:
// 0x02f8730101843b9aca008502540be40082520894...

import { ethers } from "ethers";

const rawTx = "0x02f8730180843b9aca008504a817c80082520894dac17f958d2ee523a2206206994597c13d831ec78080c080a0...";
const tx = ethers.Transaction.from(rawTx);

console.log(tx.nonce); // 42
console.log(tx.value); // 0 ETH
console.log(tx.to);    // 0xdAC17F958D2ee523a2206206994597C13D831ec7 (USDT)

Physics: The network doesn’t care who you are. It only cares that recover(hash(fields), v,r,s) equals a valid address with a balance.


The Nonce: Time is Strict

The EVM is a state machine. Order matters. The Nonce is a counter attached to every account.

  • Tx #0 must happen before Tx #1.
  • Tx #1 must happen before Tx #2.

The “Stuck” Transaction Problem

If you broadcast Tx #4 (Nonce 4) with a low gas fee, it sits in the mempool. You then get impatient and try to send Tx #5. Tx #5 will never be mined until Tx #4 is mined.

The Fix: You must send a new Tx #4 with the same nonce but higher gas. This “Replace-by-Fee” tells miners: “Ignore the cheap one, take this expensive one instead.”


Gas Economics: EIP-1559

Before EIP-1559, fees were a blind auction. Now, they are a predictable market.

Total Fee=Base Fee+Priority Fee\text{Total Fee} = \text{Base Fee} + \text{Priority Fee}

1. Base Fee (The Burn)

  • Determined by the protocol based on block congestion.
  • Target: 15 million gas per block. Max: 30 million.
  • If block > 15M gas, Base Fee increases by 12.5% for the next block.
  • This ETH is BURNED. It vanishes from supply.

2. Priority Fee (The Tip)

  • Goes directly to the Validator/Miner.
  • Incentive to include your tx.
  • Usually small (1-2 gwei) unless you are competing for an arb (MEV).

Why it matters: You can set a MaxFee of 500 gwei. If the Base Fee is only 20 gwei, you only pay 20 + Tip. The protocol refunds the rest.


The Mempool (The Dark Forest)

When you hit “Send,” your tx enters the Mempool (Memory Pool). It is a waiting room.

But it is a public waiting room. MEV Bots (Maximal Extractable Value) scan the mempool milliseconds after you broadcast.

  • Sandwich Attack: If you buy a token with high slippage, a bot sees your pending tx, buys before you (Frontrun), and sells after you (Backrun).
  • Arbitrage: If you reveal a price inefficiency, they copy your Data field and execute it with a higher gas fee.
User Broadcasts Public Mempool
Visible to all
MEV Bot Scans
Bot Frontruns Tx

Practice Exercises

Exercise 1: The Replacement (Beginner)

Scenario: You sent a transaction with Nonce 10 and 20 gwei gas. It’s stuck. Base fee is now 40 gwei. Task: Construct the fields for the replacement transaction. (Hint: What MUST match? What MUST be higher?)

Exercise 2: Cost Calculation (Intermediate)

Scenario:

  • Gas Limit: 21,000 (Standard Transfer)
  • Base Fee: 50 gwei
  • Priority Fee: 2 gwei
  • ETH Price: $3,000 Task: Calculate the total cost in USD.

Exercise 3: Decode the Blob (Advanced)

Scenario: 0xf86b808502540be40082520894... Task: Use ethers.utils.parseTransaction (or an online tool) to find the To address.


Knowledge Check

  1. What happens if you send Nonce 5 before Nonce 4?
  2. Does increasing the Gas Limit make a transaction faster?
  3. Where does the Base Fee go?
  4. Why do I need to sign the transaction?
  5. Per EIP-1559, what happens if blocks are consistently full?
Answers
  1. It waits. The node will store Nonce 5 in a separate “future” queue until Nonce 4 arrives.
  2. No. Gas Limit is the fuel tank capacity. Priority Fee is how much you pay the driver to speed up.
  3. It is burned. Removed from circulation forever.
  4. Authentication. The signature proves you own the private key for the address, authorizing the debit.
  5. Exponential Cost. The Base Fee increases by 12.5% every block, quickly becoming prohibitively expensive to reduce congestion.

Summary

  • Signatures: Authorize state changes.
  • Nonces: Enforce order.
  • EIP-1559: Makes fees predictable and burns ETH.
  • Mempool: A public, adversarial environment where ordering is auctioned.

Questions about this lesson? Working on related infrastructure?

Let's discuss