Module 3: How Blockchains and Transactions Work

MEV, the Invisible Tax

Chapter 11 of 13 · about 6 minutes

11.1 A worse price than expected

Mint swapped 10,000 USDC for ETH on Ethereum mainnet. The interface quoted about 3.32 ETH. Mint received about 3.21 ETH. The swap had succeeded, within Mint's slippage tolerance of 5%, but something felt off.

On a block explorer, Mint noticed two transactions right next to the swap in the same block: one just before, buying ETH in the same pool, and one just after, selling ETH. Both from the same address.

"Someone traded right before me and right after me," Mint said. "Coincidence?"

"No," said Hash. "You were sandwiched. Welcome to MEV."

11.2 What MEV is

MEV stands for maximal extractable value. It's the profit someone can make by choosing which transactions go into a block, and in what order.

Chapter 1 explained that pending transactions sit in public mempools, visible to everyone, before they're included in a block. Chapter 2 explained that block producers decide what goes in. Put those together, and anyone watching the mempool can spot profitable opportunities created by other people's pending transactions, and pay to have their own transactions placed just before or after them.

Think of a busy auction house where every bidder must write their bid on a public whiteboard a few seconds before it's submitted. A sharp trader standing by the whiteboard can see what's coming and act first. That's essentially what happens on public blockchains, except the traders are bots.

11.3 How the ecosystem works

MEV on Ethereum involves several specialised participants.

ParticipantRole
SearchersRun bots that scan the mempool and blockchain for profitable opportunities, and create bundles of transactions to capture them
BuildersAssemble full blocks from user transactions and searcher bundles, ordering them to maximise value
RelaysPass blocks from builders to validators, acting as trusted intermediaries
ValidatorsChoose the most valuable block offered and propose it to the network

Most Ethereum blocks today are built this way, through a system often called proposer-builder separation. Validators earn more by accepting blocks from builders, and builders compete to offer the most valuable blocks.

11.4 Types of MEV

Not all MEV harms users. Some of it performs useful functions.

Arbitrage. When the price of a token differs between two exchanges, bots buy on one and sell on the other, bringing prices back in line. This is largely beneficial, keeping prices consistent across the market (Module 1, Chapter 6).

Liquidations. When a borrower's collateral in a lending protocol falls below a safe level, bots compete to liquidate the position and earn a reward. This keeps lending protocols solvent, which the DeFi module covers.

Front-running. A bot sees your pending transaction and places its own similar transaction ahead of yours, capturing the opportunity first.

Back-running. A bot places its transaction right after yours, to take advantage of the price change your transaction caused, often through arbitrage.

Sandwich attacks. A bot places one transaction before yours and one after, profiting directly at your expense. This is what happened to Mint.

11.5 The sandwich, step by step

Let's walk through Mint's sandwich using a simplified pool from Module 1, Chapter 8, ignoring pool fees to keep the maths clear. The pool holds 1,000 ETH and 3,000,000 USDC, so ETH starts at $3,000.

Without the bot:

StepActionResult
1Mint swaps 10,000 USDC for ETHMint receives ~3.322 ETH, an average price of ~$3,010

The small difference from $3,000 is normal price impact.

With the bot:

StepActionResult
1Bot sees Mint's pending swap and 5% slippage toleranceCalculates it can safely push the price up
2Bot buys ETH with 50,000 USDC, placed just before MintBot receives ~16.393 ETH; pool price rises to ~$3,101
3Mint's swap executes at the worse priceMint receives ~3.214 ETH, an average of ~$3,111
4Bot sells its ~16.393 ETH, placed just after MintBot receives ~50,326 USDC
Without the botWith the bot
ETH Mint received~3.322~3.214
Mint's loss~0.108 ETH, about $324 or 3.2%
Bot's profit (before gas and fees)~$326

Chapter 11 · A sandwich attack, step by step

Mint's pending swap of 10,000 USDC for ETH, with 5% slippage tolerance, sits in the public mempool where a bot spots it. In one block, in order: first, the bot buys ETH with 50,000 USDC, receiving about 16.393 ETH and pushing the pool price from $3,000 to about $3,101. Second, Mint's swap fills at the worse price, receiving about 3.214 ETH at an average of about $3,111. Third, the bot sells its 16.393 ETH for about 50,326 USDC. Without the bot Mint would have received about 3.322 ETH at about $3,010. Mint's cost is about 0.108 ETH, around $324 or 3.2%, and the bot's profit is about $326 before gas and fees.

The bot pays to be placed directly before and after Mint's swap, so Mint buys at the price the bot just pushed up and the bot sells into it. Mint's 5% slippage tolerance is what told the bot how much it could take.

The bot's profit came almost entirely out of Mint's trade. Mint's swap still "succeeded," because the result stayed within the 5% slippage tolerance.

"So my slippage tolerance told the bot how much it could take," said Mint.

"Exactly," said Hash. "Your tolerance is the size of the sandwich."

If Mint had set slippage to 0.5%, the minimum acceptable output would have been about 3.306 ETH. The bot's sandwich would have pushed Mint below that, causing Mint's transaction to fail rather than execute at a bad price. The bot would have to use a much smaller front-run, with much less profit, or skip Mint entirely.

11.6 How to protect yourself

Set sensible slippage tolerance. For liquid pairs, a low tolerance like 0.5% is usually enough. Only raise it when necessary, such as for volatile or illiquid tokens, and understand that higher tolerance means more room to be exploited.

Use MEV protection. Some wallets and services send your transactions through private transaction channels directly to builders, rather than the public mempool. Bots can't sandwich what they can't see. Many wallets now offer this as a setting.

Use applications that protect against MEV. Some trading applications use designs like batch auctions or solvers, where your order is matched or filled in ways that reduce exposure to sandwiches.

Split very large trades, or use aggregators that route across many pools to reduce price impact.

Consider Layer 2s, where ordering works differently. Many rollup sequencers process transactions in the order received and don't run a public mempool in the same way, which reduces some forms of MEV, though other forms still exist.

ProtectionWhat it does
Low slippage toleranceLimits how much a sandwich can extract; may cause failed transactions in volatile markets
Private transaction channelsHides your transaction from public mempool bots
MEV-aware applicationsMatches or fills orders in ways that reduce sandwiching
Splitting or routing tradesReduces price impact, making sandwiches less profitable

11.7 Is MEV good or bad?

Node, as usual, wanted the honest answer. "Should MEV be banned?"

"It can't be banned, because it comes from how blockchains work," said Hash. "Whoever orders transactions can profit from ordering. The industry's goal is to reduce the harmful kinds, like sandwiches, while keeping the useful kinds, like arbitrage and liquidations, and to make the system fairer and more transparent."

There are also wider concerns. MEV rewards sophisticated, well-funded players, and block building has become concentrated among a small number of builders. That raises questions about centralisation and censorship, which Chapter 13 discusses.

11.8 Try it yourself

Find an MEV dashboard or explorer that tracks sandwich attacks on Ethereum. Look at a recent sandwiched transaction. Identify the bot's front-run, the victim's swap, and the bot's back-run. Calculate the victim's loss using the token transfers.

Then check your own wallet's settings for slippage defaults and MEV protection options.

Key takeaways from this chapter

  1. MEV is the profit available from choosing which transactions go into a block and in what order.
  2. Public mempools let bots see pending transactions and act before or after them.
  3. Searchers, builders, relays, and validators make up the MEV supply chain on Ethereum.
  4. Some MEV, like arbitrage and liquidations, is useful. Front-running and sandwich attacks harm users.
  5. In a sandwich attack, a bot buys before you and sells after you, profiting from your price impact.
  6. Your slippage tolerance limits how much a sandwich can take. Keep it as low as practical.
  7. Private transaction channels, MEV-aware applications, trade splitting, and Layer 2s reduce your exposure.