CHARTTHREAD

Sandwich attack

Also called: sandwich, sandwiching, front-running, MEV attack, sandwich bot

A sandwich attack is when a bot sees your DEX swap waiting to be confirmed, trades just before you to push the price up, lets your swap fill at the worse price, and trades back right after you for a profit. Your slippage tolerance sets how much it can take.

A sandwich attack is a way of profiting from someone else’s swap on a decentralized exchange. It is possible because on a public blockchain a pending transaction is visible to anyone for a few seconds before it is included in a block, and that transaction shows the token, the size and the worst price you accept. A bot places its own trades on both sides of yours, like two slices of bread around a filling. It is one form of maximal extractable value (MEV).

How a sandwich attack works, step by step

  1. You send a swap. You buy a token with a slippage tolerance, the most you accept below the quote.
  2. The bot buys first. It sees your swap waiting and buys the same token just ahead of you, pushing the pool price up exactly as far as your tolerance allows.
  3. Your swap fills. It fills at the worse price, still inside your tolerance, so it does not cancel.
  4. The bot sells. Right after your swap, it sells the tokens it bought at the higher price your own swap created.

The attack works because of the liquidity pool formula: each purchase raises the price for the next buyer, and the bot makes sure it is the buyer before you and the seller after you.

What it costs: a worked example

The course computed this on a real LINK/WETH pool on Uniswap v2 with reserves from 11 October 2026 (0.3% fee included, gas ignored).

Example. You swap 10,000 dollars of WETH for LINK, quoted at 752.2 LINK, with a 1% tolerance, so you accept as little as 744.7 LINK. The bot buys about 1.49 WETH of LINK ahead of you. Your swap fills at 744.7 LINK, exactly 1% less than quoted. The bot sells back for about 78 dollars before gas. You received about 100 dollars less than expected; pool fees took the rest.

Tolerance Your loss Bot’s profit (before gas)
0.5% about 50 dollars about 39 dollars
1% about 100 dollars about 78 dollars
3% about 300 dollars about 235 dollars
5% about 500 dollars about 391 dollars

Your tolerance is the bot’s budget.

When a sandwich is worth it for the bot

The bot pays gas twice and pool fees on its own trades. A sandwich is profitable only when your swap is large enough relative to the pool and your tolerance is wide enough to leave room after those costs. Small swaps in deep pools with tight tolerances are rarely worth attacking.

How to avoid a sandwich attack

  • Keep the tolerance tight. Use the smallest setting your swap fills with; around 0.5% on deep pools.
  • Trade deep pools, or smaller sizes. Both price impact and the bot’s profit grow with your size against the pool.
  • Keep your swap private. Some wallets and DEX aggregators offer private or MEV-protected routes that send your transaction straight to block builders instead of the public waiting area.
  • Read the price impact line before confirming. Several percent means the pool is too small.

In the course

The lesson what slippage is walks through this sandwich step by step on the real pool, and shows why the slippage you accept on a DEX is public information.

Questions about sandwich attack

How does a sandwich attack work?

A bot watches swaps waiting in public to be included in a block. It buys the same token just ahead of your swap, which raises the price; your swap then fills at that higher price; and the bot sells right after you at the price your swap pushed up. You get fewer tokens, and the bot keeps the difference minus fees.

How much can a sandwich attack cost?

Up to your slippage tolerance. A bot pushes the price only as far as your swap still fills. With a 1% tolerance on a 10,000 dollar swap, you can lose about 100 dollars; at 5%, about 500 dollars.

What is MEV?

MEV, maximal extractable value, is the profit that can be made by choosing which transactions go into a block and in what order. Sandwich attacks are one kind of MEV; others include arbitrage between pools and liquidations.

How do I protect myself from a sandwich attack?

Keep the slippage tolerance as tight as your swap fills with, trade deep pools or smaller sizes, and use a wallet or route that sends your swap privately to block builders so bots cannot see it in advance.

Lessons that use this term

Related terms

All terms in the glossary