A sandwich attack in DeFi works when a searcher buys a token just before a user's AMM buy, pushes up the price, lets the user buy at that higher price, then sells immediately after.
- The pending swap reveals its size and minimum acceptable output.
- The searcher places a buy, the user's swap, and a sell in that order.
- The searcher profits only if the round trip covers trading fees, gas, and the cost of securing that order.
Picture a user submitting a 10 WETH token buy through Uniswap on Ethereum. When the transaction enters the public mempool, its token pair, input amount, and amountOutMin are visible. A searcher can calculate how much it may buy first while leaving the user's output above that minimum.
A builder assembles transactions into a block, so the searcher also needs the three swaps executed in order. Flashbots is an Ethereum MEV research and infrastructure organization whose Auction bundles let searchers propose an ordered group of transactions to a builder, including a pending public swap. The bundle format keeps those transactions together and in sequence.
A Three-Trade Sandwich in One Pool
A three-trade sandwich changes the pool's reserves before the user's buy, then trades against the reserves that the user's buy leaves behind. Consider an illustrative Uniswap V2-style pool holding 100 WETH and 100,000 tokens. Its starting ratio is 1,000 tokens per WETH. With the pool's 0.30% swap fee, a 10 WETH buy made first would return about 9,066 tokens.
- Searcher buys first. The searcher puts in 5 WETH and receives about 4,748 tokens. The pool now holds 105 WETH and 95,252 tokens. Buying tokens has made the next token buy more expensive.
- User buys second. The same 10 WETH now returns about 8,260 tokens. The user's input and chosen pool have stayed the same; the reserves available when the swap executes have changed. The user receives about 806 fewer tokens than the first-in quote.
- Searcher sells third. The searcher sells the 4,748 tokens back into the pool and receives about 5.935 WETH. The round trip gains about 0.935 WETH after pool swap fees, but before gas and any builder payment. The searcher's sale lowers the price from the level reached after the user's buy.
The calculation comes from the constant-product rule, **x*y=k**: each swap changes both reserves, so the next swap gets a different price. Fees are taken from swap inputs and added to pool reserves, which makes the actual product grow. The example's amounts use those fees; they are not observed market prices. Uniswap's fee documentation specifies the V2 rate.
The user's amountOutMin is the decisive boundary. Suppose the pending swap requires at least 8,250 tokens. The illustrated 5 WETH front-run leaves the user about 8,260, so the swap succeeds only just above that floor. A larger front-run that drives output below 8,250 would make this exact-input swap revert. The searcher can simulate sizes against the pending transaction and aim near the floor because every extra token of permitted price movement creates room for extraction.
That is why a large slippage setting can become the searcher's budget. Slippage tolerance is translated into a minimum output when the transaction is built; for this swap, that minimum is the on-chain limit on adverse execution price. The searcher cannot necessarily collect the entire difference from the original quote: its own trades move the pool, incur fees, and require payment for inclusion. But a lower minimum gives it more possible front-run sizes that still allow the victim's swap to execute.
The setup must also survive inclusion. The public transaction must still be pending, the pool reserves must be close to those simulated, and the builder must place the sequence together. A searcher can give its final sell a minimum proceeds condition: if changed reserves erase the expected return, that sell reverts and a bundle that disallows reverts is omitted. Atomic ordering protects the sequence; the searcher's simulation and conditions protect its economics.
Profit rises when the victim's trade is large relative to the pool and its minimum output leaves room for a substantial first buy. Deeper liquidity reduces the price move from each trade. Pool fees on the searcher's buy and sell, Ethereum gas, and competing bids for builder inclusion reduce the remaining edge. Small swaps in deep pools often cannot cover those costs.
Related Questions
Sandwich attack vs front-running: what is the difference?
A sandwich attack is front-running followed by a back-run around one targeted transaction. Front-running means placing a transaction before another to benefit from its expected effect; it does not require a later sell. In a sandwich, the first trade worsens the user's AMM price and the last trade closes the searcher's position.
How to check if my swap was sandwiched on Etherscan
Find your swap's transaction hash on Etherscan, open its block, and inspect the transactions immediately before and after yours. Look for a buy in the same pool before your swap and a sale of that token after it. Compare token transfers and swap logs to see whether the trader bought before you and sold after you; the two legs may use different addresses. Your transaction's actual token output and decoded minimum output show how close execution came to your limit.
How to protect a swap from sandwich attacks
Protect a swap by setting a minimum output that reflects the price you are willing to accept, especially for a large trade in a thin pool. Splitting a trade or using a deeper route can reduce its immediate price impact, though execution costs and prices can change between swaps. Private routing such as Flashbots Protect keeps the pending transaction out of the public mempool, removing the public preview that this attack uses; private routing still depends on the parties that receive the transaction.
Questions Readers Ask Next
What is a backrun and is it harmful to the user?
A backrun is a transaction placed immediately after another to trade on the state that transaction created. The sell leg of a sandwich is a backrun, but the user's worse fill was caused by the buy placed before their swap. A separate arbitrage backrun can bring a pool price closer to another market without first pushing the user's execution price against them.
What is just-in-time liquidity in Uniswap V3?
Just-in-time liquidity is liquidity added in a concentrated price range immediately before an expected Uniswap V3 swap and removed soon after to earn its fees. It changes how much liquidity the swap encounters. A sandwich instead uses a buy and sell to move the swap price and capture part of the user's allowed slippage.