Sandwich Attack

A sandwich attack is a form of front-running in which a bot places one trade just before and another just after a victim's pending swap on a decentralized exchange, profiting from the price movement and leaving the victim with a worse price.

Sandwich attacks target swaps on automated market makers (AMMs), where every trade moves the pool's price in a predictable way. On public blockchains such as Ethereum, pending transactions wait in the mempool, where anyone can see them. An automated bot spots a sizeable swap and submits a buy of the same token, arranged to execute just before the victim's trade. It then submits a sell to execute just after. The victim's trade is 'sandwiched' between the two.

For example, a trader swaps ETH for a token with a 1% slippage tolerance. The bot buys the token first, pushing its price up. The trader's swap still goes through, but at a worse price, so they receive fewer tokens than expected. The bot then sells into the raised price and keeps the difference, minus gas and pool fees. The trader's loss is the bot's profit, which makes this a form of maximal extractable value (MEV).

A common misconception is that a low slippage tolerance prevents sandwich attacks. A low tolerance limits how far the price can be pushed and makes attacks less profitable, but a large trade can still be worth attacking. A high tolerance invites bigger losses. Other defences include sending transactions through private RPC endpoints that keep them out of the public mempool, using limit orders, and using protocols that settle trades through batch auctions. Some Layer 2 networks use a centralized sequencer with no public mempool, which makes classic sandwiching by outside bots harder.

Learn more

Related terms