DeCrypto Markets

Learn: DeFi

What is MEV, and how does a sandwich attack work?

MEV, maximal extractable value, is the profit that can be made by choosing which transactions go into a block and in what order. The best-known form is the sandwich attack: a bot buys just before your swap and sells just after it. Your slippage tolerance sets how much it can take, and a well-sized attack takes nearly all of it.

How it works

On many public blockchains, a swap you send waits in a public queue of pending transactions before it is included in a block. Anyone can read it there, including its size and its minimum received: the least you agreed to accept, set by your slippage tolerance. Bots read the queue all day, looking for transactions whose order can be exploited.

In a sandwich, the bot places a buy just before your swap, which pushes the pool price up. Your swap then fills at a worse price. Right after it, the bot sells back into the pool at the higher price your swap created. The bot keeps the difference, minus the pool fees on its two trades and its gas.

The bot's only limit is your minimum received. If its front-run pushes your fill below that, your swap reverts and the attack fails. So the bot sizes its front-run to bring your fill as close to your minimum as possible. A loose tolerance hands it room, and it takes nearly all of that room. A tolerance that is too tight, on the other hand, makes your swap revert on ordinary price drift. See slippage and price impact.

The exact rule the games grade by

The rules the games use: the pool is constant-product with the fee taken off the input. A trade from other users may land first, so the price drifts against you. Minimum received = floor(quote x (10000 - tolerance in bps) / 10000); below it the swap reverts. The bot can borrow any amount within the block, so only your minimum received limits it, and it attacks only when its profit after pool fees and gas is above zero. The safe tolerances run from tMin (the smallest that does not revert) to one basis point below tMax (the smallest that gets sandwiched).

Step by step

  1. Get a quote for your swap and note the price impact of your own trade.
  2. Estimate the drift: other trades may land before yours and move the price.
  3. Set the tolerance just above that drift, not "as loose as it takes".
  4. Check the minimum received the wallet shows: that number is exactly what a bot can push you down to.
  5. If the window between failing and being sandwiched is narrow, split the swap or use a deeper pool.

Other kinds of MEV

  • Arbitrage: bringing a pool back in line with other markets after a large trade. It is a normal part of how pools stay priced, and it does not harm the trader directly.
  • Liquidations: racing to liquidate under-collateralised loans on lending platforms for the reward.
  • Front-running and back-running: placing a transaction right before or after a known one to profit from its effect. A sandwich is both at once.

Worked example

Which slippage tolerances fail, fill or get sandwichedA swap of 9,687.84 USDT for SOL. Below 0.35% tolerance it reverts; from 0.35% to 0.85% it fills safely; from 0.86% a bot profits by sandwiching it.Swap 9,687.84 USDT for SOL: outcome by tolerancerevertssafe 0.35% to 0.85%sandwichedAt 2.00%0.0%0.5%1.0%1.5%2.0%2.5%At 2.00% the bot fronts 9,157.61 USDT and the swap gets 41.2700 SOL, not 42.1123.The swapper loses 2.00% of the quote; the bot keeps 74.06 USDT after gas.
The tolerance window of a generated swap: below it the swap reverts, inside it the swap is safe, above it a bot profits.

A swap of 9,687.84 USDT for SOL waits in the queue. The quote is 42.1123 SOL, and the swap's own price impact is 0.89%. The pool fee is 30 basis points, and the bot pays 32.76 USDT of gas for its two transactions.

Below a tolerance of 0.35%, the drift from the trade ahead is already enough to make the swap revert. From 0.35% to 0.85%, the swap fills and no sandwich can make a profit after fees and gas. From 0.86%, a bot can.

At a loose 2.00% tolerance, the bot fronts 9,157.61 USDT. The swap fills at 41.2700 SOL instead of the quoted 42.1123: 0.8422 SOL less, or 2.00% of the quote. The bot sells back and keeps 74.06 USDT after gas.

Notice that the swapper loses almost exactly the tolerance they allowed. That is not a coincidence: the bot sizes its front-run to use the whole room.

Takeaway: A tolerance of 2.00% costs about 2.00%; a tolerance inside the safe window, 0.35% to 0.85%, costs the bot money to attack. Your minimum received is the price you invite.

Common mistakes

  • Setting slippage as loose as possible. A swap that keeps failing is tempting to fix with a high tolerance. Every extra basis point is room a bot can take. Raise it a little at a time, and only as far as the drift you expect.
  • Swapping large amounts in small pools. A big trade in a shallow pool has a large price impact, which makes the drift larger and the sandwich more profitable. Smaller swaps and deeper pools leave the bot less to take.
  • Ignoring the minimum received. Wallets show it under the swap. It is the worst fill you have signed up for, and a bot will usually deliver something very close to it.

What this does not tell you, and what can go wrong

The model in the games is a single pool, one victim and a bot that knows everything. Real MEV involves competing bots, builders who order the block, and private transaction routes. Some tools claim to protect swaps from sandwiches by keeping them out of the public queue; how well they work varies and changes over time, and using one means trusting its operator.

Tight tolerances limit the damage; they do not remove every cost of trading on a public chain. They also cannot help if the token itself is worthless or the pool is a trap. MEV is one cost among several, alongside fees, price impact and impermanent loss for liquidity providers.

Questions people ask

What does MEV stand for?

Maximal extractable value. It was first called miner extractable value, when miners ordered transactions on proof-of-work chains. The name changed because validators, block builders and searchers now do that work on many chains. The idea is the same: profit from the order of transactions.

How do I protect a swap from a sandwich attack?

Set your slippage tolerance just above the drift you expect, not much higher. Split large swaps, use deep pools, and check the minimum received before you confirm. Some wallets offer private transaction routes; they can help, but you are trusting whoever runs them.

Is a sandwich attack illegal?

That depends on the jurisdiction and is not settled in most places. On public blockchains it is technically possible because anyone can read pending transactions and pay to order theirs first. Whatever its legal status, it is a cost you can reduce with tighter settings.

Why did my swap fail instead of being sandwiched?

Because your tolerance was tighter than the price drift between your quote and your fill. The swap reverts rather than filling below your minimum. You usually still pay the network fee for a failed transaction, which is why tolerance is a trade-off, not a dial to turn to zero.

Practise it

Reading about a rule is not the same as applying it. These games generate a fresh simulation each round, ask you to make the call first, and grade you against the same rule.

MEV Sandwich

A swap waits in the public queue. Play the sandwich bot: find the largest front-run its slippage tolerance allows.

What it teaches

AMM Swap

Predict the price impact of a swap in an x*y=k pool, then set the slippage tolerance.

What it teaches