A sandwich attack is a form of MEV (Maximal Extractable Value) manipulation where a bot spots your pending swap in the mempool, places a buy order just before your trade and a sell order right after it, and profits from the price movement your trade causes. You pay more for the tokens you are buying, and the bot pockets the difference.
Every transaction on Ethereum and most EVM-compatible chains sits in a public waiting area called the mempool before a validator includes it in a block. MEV bots monitor this mempool in real time, scanning for large token swaps on decentralized exchanges like Uniswap.
When a bot detects your swap, it runs a quick calculation. If your trade is large enough to meaningfully move the price of the token, the bot has a profitable opportunity. It submits two transactions: one to buy the token just before your trade, and one to sell immediately after. The bot pays a higher gas fee to push its buy order in front of yours, and a lower fee to place its sell order just behind you.
Your trade arrives in the middle of those two bot transactions, like meat in a sandwich. The bot's buy order raises the price you receive. Your trade pushes it higher. The bot sells into that elevated price and exits with a gain. You get fewer tokens than you expected at the start.
The size of your trade and your slippage tolerance determine whether sandwich attacks are worth targeting. If you set a wide slippage tolerance, say 5% or 10%, you are telling the bot exactly how much it can extract before your transaction reverts. Bots use that tolerance ceiling as a profit guide.
Large swaps in thin liquidity pools are the most vulnerable. A $100,000 swap in a pool with $500,000 of liquidity can move price by several percentage points. That magnitude of movement is worth the gas cost for a bot to exploit.
You can take several practical steps to make your swaps less attractive to sandwich bots.
Sandwich attacks are one type of MEV extraction. The broader category includes arbitrage, liquidation running, and transaction ordering manipulation. Flashbots, an Ethereum research organization, estimates that hundreds of millions of dollars in MEV were extracted annually from Ethereum users prior to the Merge in 2022. Addressing MEV is an active area of protocol research, with proposals like encrypted mempools and MEV smoothing discussed as potential long-term solutions.
https://www.flashbots.net/mev-explore
https://ethereum.org/en/developers/docs/mev
https://cow.fi
https://docs.uniswap.org/concepts/protocol/swaps