Skip to the article
Top Crypto

Crypto markets, protocols, policy

How does a sandwich attack work on a token swap?

A sandwich attack in a token swap brackets your pending Ethereum trade: a bot buys first, worsens your fill, then sells, bounded by slippage and fees.

The Top Crypto Editors 2 min read

How does a sandwich attack work on a token swap?

A sandwich attack on a token swap brackets your pending trade: a bot buys before you, lets your order move the price, then sells after it fills. On Ethereum, the bot can spot your transaction in the public mempool, and your slippage tolerance sets how much worse a price you will accept.

Say you swap 10 ETH for a thinly traded token with 3% slippage tolerance. A bot sees the pending swap and buys the token first, pushing its price up. Your trade then fills at a worse rate, potentially close to the point where your minimum output still allows it. The bot sells its tokens after your swap, aiming to keep the difference.

The front-run buy, your swap and the back-run sell can be ordered in one block. Your transaction’s minimum output tells the bot the limit you set: it can push the price only as far as that limit permits. If the move would make your output fall below the minimum, your swap reverts instead of completing at that price.

KyberSwap is a decentralized exchange aggregator that finds token-swap routes across supported liquidity sources and networks. A cross-chain swap connects that routing question to trades that span networks. KyberSwap’s MEV protection option can reduce the room for this kind of attack.

The bot’s apparent price advantage is not free profit. It pays a pool fee on its buy and another on its sell, plus gas for its transactions. Its resale proceeds must cover the original purchase, both pool fees and gas before any difference becomes profit. A smaller swap may move the pool price too little to leave enough after those costs; a tight slippage limit also caps how far the bot can push the price.

So slippage tolerance and transaction visibility both matter: the public mempool gives a bot a chance to act first, while your limit bounds the damage it can cause. Tightening the tolerance reduces that room, though a swap may then fail if the price moves beyond your limit.