MEV sandwich attack

A MEV sandwich attack is when a bot sees your pending DEX swap, buys the same token just before it and sells just after it in the same block. Your swap fills at a worse price, up to your slippage tolerance, and the difference goes to the bot.

Sandwich cost calculator

Room an attacker can use
–
Worst-case extra cost to you
–

A sandwich can push your fill up to your slippage limit. Whatever is left between your swap's own impact and your tolerance is the room an attacker can take. Lower tolerance or a private RPC shrinks it.

How it works

MEV stands for maximal extractable value (originally miner extractable value): the profit available to whoever decides the order of transactions in a block. On Ethereum and most other public chains, a swap you sign is broadcast to a public mempool before it is included in a block, and anyone can read it there: the pool, the size, the direction and the minimum amount you are willing to receive.

A sandwich attack uses that information in three transactions placed around yours in the same block:

  1. Front-run. A bot buys the token you are buying, ordered just before your swap. This pushes the pool price up.
  2. Your swap. It fills at the higher price. As long as the total move stays within your slippage tolerance, the transaction does not revert. It just gives you fewer tokens.
  3. Back-run. The bot sells the tokens it bought, now into a price your swap pushed even higher.

The bot never holds a position for more than a block, and because it can see your limit, it sizes the front-run to push your fill as close to that limit as possible without making your swap fail.

That is why slippage tolerance matters so much here. Part of the tolerance is needed to cover your own slippage, the price impact of your swap in a pool of that size. The rest is room for price to move before confirmation, and a bot can claim that room on purpose. The calculator above computes it: tolerance minus your own price impact.

A worked example

You swap $20,000 into a token in a pool with $10,000,000 on the dollar side ($20,000,000 in total). Your own price impact is $20,000 ÷ $10,000,000 = 0.2%. You leave the slippage tolerance at 1%. These are the calculator's default inputs.

  • Room an attacker can use: 1% − 0.2% = 0.8%
  • Worst-case extra cost: $20,000 × 0.8% = $160

The same swap at other tolerance settings:

Slippage tolerance Room for an attacker Worst-case extra cost
0.5% 0.3% $60
1% 0.8% $160
3% 2.8% $560
5% 4.8% $960

Going from 0.5% to 5% multiplies the exposure by 16 on a trade whose real price impact is unchanged. This is a ceiling: the attacker also pays the pool fee on both legs plus gas, and needs capital for the front-run, so actual extraction is usually below it. A trade is attacked when the expected profit exceeds those costs, which depends on the room, not on how large the trade looks.

What it means in practice

A sandwiched swap does not look like an error. It confirms, and the only trace is a fill a little worse than the quote, which is easy to read as ordinary market movement. You can check by opening the block in an explorer: a buy of the same token from one address directly before your swap and a sell from the same address directly after is the signature.

The exposure is controlled by three things you choose:

  • Tolerance. Set it just above your expected price impact plus a margin for normal movement, not at a round 3% or 5% by habit. Too tight and the swap reverts when the market moves; too loose and the gap is free for the taking.
  • Size against pool depth. A large trade in a shallow pool has high impact, needs a high tolerance and is the most profitable target. Splitting it or routing it through deeper pools shrinks both.
  • Where the transaction goes. A private RPC such as Flashbots Protect sends the transaction to block builders without putting it in the public mempool, so there is nothing to front-run. Aggregators that use batch auctions or private order flow serve the same purpose.

Centralized exchanges match orders internally and do not publish pending trades, so sandwiching in this form does not exist there. It is a property of public blockchains with a public mempool.

When it matters most

Sandwiching concentrates where tolerance is loose and pools are thin: new tokens with high price impact, volatile periods when users raise tolerance to make sure swaps go through, and tokens with transfer taxes, where users routinely set 5–10% slippage. A small swap is not safe by itself. A $500 trade with 5% tolerance can leave up to $25 of room, which can be worth the gas on a cheap chain.

It also matters during market stress such as a liquidation cascade, when many users submit urgent swaps with wide limits at the same time.

Common misconceptions

"The market moved against me." If the fill was worse than the quote and the block shows a matching buy and sell around your swap, the move was made for your transaction, inside the limit you set.

"Higher slippage tolerance makes my swap safer." It makes the swap more likely to confirm. It also makes it more profitable to attack. Those are different things.

"Only whales get sandwiched." Bots attack any trade where the room times the size covers fees and gas. On low-fee chains that threshold can be small.

"Sandwich attacks are a bug that will be fixed." They follow from public transaction ordering. Private submission and batch auctions reduce exposure, but they work around the mechanism rather than remove it.