MEV Sandwich Attack Explained: Slippage, Front-Running, Defence
Lesson 3 of 10 · Intermediate
What is slippage? How it differs from price impact
Slippage is the difference between the price you see when you place a trade and the price at which it actually executes. On a decentralised exchange, there is a gap of several seconds or more between the moment you press Swap and the moment your transaction lands in a block. In that gap, other trades can change the pool’s balances and therefore its price.
Keep two concepts apart:
- Price impact: the change that your own order causes in the pool’s price. It can be calculated in advance and depends on order size and pool depth.
- Slippage: the change that others cause between sending and executing your transaction, plus any difference from the initial estimate.
Your wallet or interface asks for a slippage tolerance, for example 0.5 percent. It means “if the execution price is more than 0.5 percent worse than the estimate, cancel the trade”. In effect it is the minimum amount you are willing to receive, and it is written into the transaction itself.
Slippage is not new. A trader entering the gold futures market with a market order experiences it too: if the order is larger than the size available at the best price, it “eats” several levels of the order book and the average price gets worse. On a DEX the same logic holds, except the order book levels are replaced by the pool’s curve, and the time gap before execution is longer and more visible. That visibility becomes a problem in the next section.
What is MEV?
Before they enter a block, transactions are usually visible in a public waiting area (the mempool). Whoever decides the order of transactions in a block (a validator or block builder), and bots that pay for better positioning, can profit from that ordering. This profit is called MEV (Maximal Extractable Value): the maximum value that can be extracted by arranging, inserting or excluding transactions in a block.
Not all MEV is harmful. Arbitrage that aligns pool prices with the market, or timely liquidation of risky loans, is also a form of MEV and helps the system function. But one type is taken directly from ordinary users’ pockets: the sandwich attack.
The sandwich attack, step by step
Suppose (a hypothetical example) you want to swap 10,000 dollars of a stablecoin for a token in a thin pool, and you have set slippage tolerance at 5 percent. A bot sees your transaction in the mempool:
- The top bun (front-run): just before you, the bot buys the same token and pushes the price up.
- The filling: your transaction executes at the higher price. Because it is still within 5 percent, it is not cancelled. You receive fewer tokens.
- The bottom bun (back-run): right after you, the bot sells the token at the higher price your purchase created.
The bot’s profit is roughly what you lost (minus fees). The key point: your slippage tolerance is the ceiling of the loss an attacker can take from you. The wider you set it, the more room you give for a sandwich. Set it too tight and the trade is cancelled in a volatile market, and you still lose the network fee. Finding the balance is part of the skill.
Why big trades in thin pools cost more
Three costs stack on top of each other:
- Price impact, which depends directly on the ratio of order size to pool depth.
- The pool fee, a percentage of the whole amount.
- MEV cost: a large trade with big price impact is a more attractive target for a sandwich, since the bot can move the price further.
This is why, for large orders, splitting the order into parts, using an aggregator that spreads across pools, or trading when the pool is deeper reduces the cost. These are order-execution techniques and have nothing to do with predicting price direction. In illiquid tokens, even small orders can have striking price impact.
Ways to reduce MEV risk
- Private or protected RPC: some wallets and services let you send a transaction straight to block builders instead of the public mempool, so bots cannot see it before execution. This reduces the risk but does not remove it, and you are trusting that service provider.
- Appropriate tolerance: for liquid, stable pairs a low tolerance is enough. Set a high figure only when you know why, for example tokens that take a tax on transfer.
- Splitting orders and checking price impact before confirming.
- Checking the result in an explorer: after the trade, look at the transactions just before and after yours in the same block to see whether you were sandwiched.
Remember that an interface suggesting a high “auto tolerance” has not necessarily set it in your favour. Look at the number yourself.
Common mistakes
- Setting a high slippage tolerance (say 10 percent or more) just to avoid a cancelled trade; this effectively invites sandwich bots.
- Treating slippage and price impact as the same thing; others cause the first, your own order causes the second.
- Executing one large order in a thin pool without looking at the price impact percentage the interface shows.
- Assuming a private RPC means complete protection; the risk drops, but you are trusting the service provider.
- Not checking the transaction in an explorer and never learning why you received less than expected.
What do volume and data say here?
On-chain data lets you see exactly what happened to your order:
- Open the transaction’s block: in an explorer, look at the trades made in the same pool just before and after yours. If one address bought before you and sold right after, that is the sandwich pattern.
- Compare the amount received with the estimate: calculate the gap as a percentage to learn your real execution cost.
- Depth against volume: as with the order book in gold futures, pool depth near the current price determines how far your order moves the price. High trading volume in a thin pool means more volatility and more opportunity for MEV.
These checks are for controlling execution cost, not for predicting the market.
📊 See real gold futures volume behind these ideas: free order flow course · today's gold volume map
Frequently asked questions
What is slippage?
It is the difference between the price you see when you place a trade and the price at which it actually executes. On a DEX it happens because other trades change the pool’s price between sending and executing your transaction.
What slippage tolerance should I set?
There is no single figure. For liquid pairs a low value is usually enough. The higher you set it, the greater the potential loss from a sandwich attack; the lower, the greater the chance of a cancelled trade in volatile moments.
What is a sandwich attack?
A bot sees your transaction before it executes, buys just before you to push the price up, your trade executes at a worse price, and the bot sells right after you. Your loss is roughly the bot’s profit.
Education only, not a recommendation to buy or sell; we never give signals. Price examples are illustrative. Test any method on a demo account first, with strict risk management.