MEV and Sandwich Attacks: What the Mempool Reveals
A practical explanation of MEV, sandwich ordering, AMM assumptions, slippage, and private transaction trade-offs.
0xNN · · 8 min read
MEV and Sandwich Attacks: What the Mempool Reveals
The simple version says a bot pays more gas and cuts in line. The useful version asks which transactions are visible, who can order them, and whether the user's slippage limit allows worse execution.
MEV, or maximal extractable value, is value created by choosing, ordering, or including transactions. It is not automatically a hack. Arbitrage and liquidations can support a protocol; a sandwich usually transfers value from a trader through worse execution.
The sequence
For a public-mempool swap, an attacker may try to buy before the user's transaction and sell after it:
~text
1. attacker buys before the swap
2. user's swap moves the price
3. attacker sells after the swap
~
The user's transaction can still succeed. It simply receives less if its amountOutMin condition is loose. If the condition is strict, it reverts instead.
A small AMM model
For a constant-product pool, x times y equals k. A simulator must update both reserves in the correct direction, apply the pool fee, and calculate the attacker's profit from wallet flows. A toy result is not a forecast: gas, builder bids, failed bundles, token taxes, decimals, and competing transactions still matter.
Do not publish a profit number without stating those assumptions.
What users can do
Set a minimum output that matches the trade and liquidity instead of accepting an extremely loose slippage value. Compare routes and price impact. Consider a private transaction route or an aggregator with a documented protection model, but remember that private does not mean guaranteed; it adds another service and trust assumption.
Read the wallet and network documentation before changing RPCs, and test with a small amount.
Explanations that mislead
High gas does not guarantee priority. Ordering also depends on builders, validators, bundles, and the network's block-production design. A chain is not automatically MEV-proof because its execution model is different. Headlines about daily MEV need a source, timeframe, chain, and definition.
The practical lesson is modest: understand the public or private path your wallet uses, set sensible execution bounds, inspect the chain and spender before signing, and model the worst acceptable outcome.
References
• Ethereum.org: Maximal Extractable Value
• Flashbots documentation
• Uniswap v2 whitepaper
• Uniswap v2 Pair contract