Track 7 · The Breach Lab · lesson 7
Front-running and MEV
18 min
Track 1 showed you the mempool: pending transactions, public, visible for seconds before they execute. This lesson is about who is watching it.
Everything you submit is public before it settles. Bots read the mempool continuously, and when they see a transaction that will make them money, they place their own around it — paying a higher tip to be ordered first.
This is MEV — maximal extractable value — and it is not a bug in any one contract. It is a property of a public mempool where block order is for sale.
Because it needs a live mempool and competing bids, MEV is one of the few attacks this course does not simulate for you — faking it in a single transaction would teach the wrong mechanism. But you can see exactly what makes a contract vulnerable, and what removes the opportunity.
The sandwich
You submit a large swap. A bot sees it pending:
- It buys the same asset just before you, nudging the price up.
- Your swap executes at that worse price, pushing the price up further.
- It sells immediately after, pocketing the difference.
You paid more; the bot took the gap. The contract behaved correctly the entire time.
What makes a swap vulnerable, and what fixes it
// The user accepts whatever they get.
function swap(uint256 amountIn) public {
uint256 out = pool.swap(amountIn);
// no floor on `out` — a sandwich can make it tiny
token.transfer(msg.sender, out);
}No minimum output means the user accepts any price. A sandwich can move the price arbitrarily and the swap still goes through.
That minOut is the "slippage tolerance" setting in every DEX interface. Now you
know what it is defending against.
Predict
A slippage limit makes sandwiching riskier for the bot. Why doesn't it stop MEV entirely?
Not all MEV is theftOptional
Some MEV keeps the system honest. Arbitrage bots that equalise a price across two exchanges are extracting value, but they are also doing the work that keeps prices consistent. Liquidation bots that close underwater loans protect lenders.
The line between "extraction" and "attack" is genuinely blurry. Sandwiching a retail swap is predatory; arbitraging a price gap is arguably a public good. The same mempool visibility enables both.
Check
What does a slippage limit (`minOut`) actually protect against?
Worth remembering
- The mempool is public, so bots can see and reorder profitable transactions — this is MEV.
- A sandwich buys before and sells after your trade, worsening your price.
- A slippage limit (`minOut`) caps how bad your fill can get, reverting otherwise.
- Slippage bounds one attack but does not remove ordering power; commit-reveal and private mempools go further.
- Not all MEV is predatory — arbitrage and liquidations use the same visibility usefully.