What is sandwich attack?
A form of value extraction in which a bot spots your pending swap, buys the same asset just before it, and sells just after, leaving you a worse price and the bot a profit.
Not yet verifiedHow we verify
3 min read
In this entry
A form of value extraction in which a bot spots your pending swap, buys the same asset just before it, and sells just after, leaving you a worse price and the bot a profit.
It works because pending transactions are publicly visible and block ordering can be bought. A wide slippage tolerance makes you a better target, since it defines exactly how much the bot is allowed to take.
The uncomfortable part is that nothing is hacked and no rule is broken. Your transaction sits in a public queue before it executes, anyone can read it, and whoever pays the most decides the order transactions land in. The attack is an entirely legitimate use of a system that was designed to be transparent.
How it works
- Your swap becomes public. Submitted to the mempool, it is visible to anyone watching, including the amount, the pair, and your slippage tolerance.
- A bot computes the opportunity. Your buy will push the pool's price up by a known amount, because an automated market maker's pricing formula is public. Your tolerance sets the ceiling on how far the price can move before your transaction reverts.
- The bot buys first. It pays for priority so its buy is ordered ahead of yours, pushing the price up.
- Your swap executes at the worse price, anywhere up to your tolerance.
- The bot sells immediately after, into the price your buy created, capturing the difference.
All three transactions land in the same block. Your tolerance is not a safety margin in this context; it is the budget you have authorized the bot to spend. See mev and front running.
Example
Illustrative. You swap $10,000 for a token at a quoted price, with slippage tolerance set to 3% because a previous attempt failed.
A bot sees the pending transaction and calculates that it can push the price up 2.9% and your swap will still execute. It buys ahead of you, your swap fills at 2.9% worse than quoted, and it sells immediately after. You received about $9,710 of value instead of $10,000, and the roughly $290 difference went to the bot, minus what it paid for priority.
Set tolerance to 0.5% and the same bot's opportunity shrinks to about $50, often below its costs, so it skips you. The trade-off is that a genuinely volatile market may move more than 0.5% and your transaction reverts, costing gas and no fill.
Why it matters when you buy
This is a decentralized exchange problem specifically. Buying on a centralized exchange does not expose an order to a public mempool, which is one concrete advantage of that route for a straightforward purchase. If you do swap on chain, tolerance is the setting that matters most. Centralized versus decentralized exchanges covers the comparison, spread and slippage covers the costs, and the liquidity pages show measured depth.
Related terms
mev — the broader extraction category; slippage tolerance — the budget you set; front running — ordering ahead of a known trade; mempool — where pending swaps are visible; price impact — why the bot's buy moves your price; automated market maker — the predictable pricing it exploits.
Questions
Can this happen on a centralized exchange?
Not in this form. Your order is not broadcast to a public queue that strangers can reorder. Centralized venues have their own concerns around order handling, but the public mempool is not one of them.
How do I protect myself?
Set tolerance as low as the market allows, split large swaps, use a venue with real depth in that pair, and where available submit through a private relay that keeps the transaction out of the public mempool until it is included.
Why did my swap fail with low slippage?
Because the price moved more than your tolerance between submission and execution, so the transaction reverted as instructed. That is the setting working. Raising it fixes the failure by accepting a worse price.