Skip to the article
Crypto Today

What changed in crypto today

How Does a Sandwich Attack Work on an AMM Swap?

How a sandwich attack works step by step: a bot front-runs your AMM swap, worsens its price within your slippage limit, then sells for the spread.

The Crypto Today Editors3 min read

How Does a Sandwich Attack Work on an AMM Swap?

A sandwich attack lets a bot profit from your pending token swap by buying before it, pushing up the price you pay, then selling after you; your protection is limited by your slippage setting.

In practice, a larger swap or looser slippage limit can give the bot more room to take value from your trade.

How does a sandwich attack work?

A sandwich attack orders three transactions in one block: the bot’s buy, your swap, then the bot’s sell. It works when your pending trade is visible in the public mempool, the queue of transactions waiting to be processed.

Your transaction reveals the token pair, trade size and minimum output. That minimum comes from your slippage tolerance: the most the execution price can move against you before the swap reverts. A bot can use that limit to estimate how far it can move the pool price and still let your trade complete.

To reduce exposure, set slippage only as high as your trade needs. If you can accept a set price instead of immediate execution, cowswap limit orders let you specify that price; consider cowswap limit orders as part of that approach.

Think of the bot as stepping into a checkout line just before you and charging you more, then selling the item after you buy. The analogy stops at the line: on an AMM, the pool’s pricing formula changes the token price as trades move its reserves.

What does a sandwich attack look like step by step?

Say you submit a 10 ETH buy on Uniswap with 1% slippage tolerance. Your swap sets a minimum amount of tokens you will accept. The bot sees it in the mempool and calculates whether it can profit while keeping your output above that floor.

  1. Front-run: The bot buys the same token first, using higher gas priority to get ahead of your transaction. Its buy shifts the AMM price upward.
  2. Your swap: Your transaction executes at the now-worse price. If the price stays within your slippage limit, you receive fewer tokens, potentially close to your minimum output. If it crosses the limit, the swap reverts.
  3. Back-run: The bot sells its tokens after your buy, aiming to capture the spread created by the two trades.

The bot’s profit is approximately the price move it causes minus two pool fees and gas costs. On Uniswap v2, each swap pays a 0.3% pool fee, so a sandwich must move enough value to cover both fees and gas before it pays. That is why bots tend to target larger trades relative to pool liquidity and swaps with loose slippage: small trades or fee-heavy pools can leave too little spread.

Related

You can lower your exposure by limiting slippage and considering whether a large trade should be split or placed at a price limit. CoW Swap uses signed trade intents and solver-run batch auctions, so settlement differs from sending an individual pending AMM swap into the public mempool. That changes how the classic three-transaction sequence applies, though the execution method and protections still depend on the order.

How do I tell if my swap was sandwiched?

Compare your transaction’s execution price with the quote and minimum output you approved, then inspect the same block for a bot buy before your swap and a sell after it. A worse fill alone is not proof; the paired trades and their ordering are the key clues.

  • Check your slippage tolerance before submitting.
  • Compare the quote with your actual output.
  • Review the block for a buy before and sell after your swap.