MEV Protection: How to Avoid Getting Sandwiched on a DEX
What MEV and sandwich attacks are, how they skim value from your swaps, and the practical defenses — slippage limits, liquid pairs, and private order flow — that protect automated trades.
Table of Contents
If you have ever gotten a noticeably worse price than expected on a DEX swap, MEV may be why. This article explains what MEV is, walks through the sandwich attack that causes most retail losses, and lays out the practical defenses — the ones that matter for automated trading, where the same swap runs again and again.
What MEV Actually Is
Blockchains order transactions into blocks, and whoever builds a block has discretion over that order. MEV — Maximal Extractable Value — is the profit available from exercising that discretion: reordering, inserting, or dropping transactions. Some MEV is benign (arbitrage that keeps prices aligned); some is predatory, and the sandwich attack is the form most likely to touch a normal trader. The term is defined in our glossary.
The Sandwich Attack, Step by Step
- You submit a swap that will move the price a little (its “price impact”).
- A searcher sees it pending and buys just before you, pushing the price up.
- Your swap executes at that worse price.
- The searcher immediately sells, pocketing the difference your trade created.
The attacker needs two things to profit: a swap with enough price impact, and a slippage tolerance loose enough to let the price move. Remove either and the attack stops being worthwhile.
Practical Defenses
| Defense | How it helps |
|---|---|
| Tight slippage tolerance | Caps how far price can move — often makes the attack unprofitable |
| Trade liquid pairs | Low price impact leaves little for an attacker to skim |
| Reasonable trade size | Smaller swaps on deep pools are not worth attacking |
| Private order flow | Your pending tx is hidden from searchers until it confirms |
Slippage: Your First Line of Defense
Slippage tolerance is the maximum price movement you will accept between quote and execution. Set it too high (say 5%) and you invite sandwiching; set it too low and volatile moments cause failed trades. For liquid majors, a tolerance in the 0.1%–0.5% range is usually the sweet spot. The key insight: slippage is not just about volatility — it is the single most important MEV defense you control on every trade. Its mechanics are covered in our Uniswap V3 trading guide.
What This Means for Automated Trading
A bot runs the same swap repeatedly, so a bad slippage default compounds into repeated small losses. The fix is to let each strategy set a sensible slippage tolerance (in basis points), trade liquid pairs, and keep per-trade size proportional to pool depth. A well-configured bot on a liquid L2 pair is a poor target for sandwiching. One detail worth asking any bot about: who computes the minimum output of each swap? In a session-key design the bot’s server usually does, from a fresh quote and your tolerance — so the on-chain permission protects where your funds can go, while the slippage floor protects the price. Both matter.
You cannot abolish MEV, but you can make your trades not worth attacking — and for automated strategies, that discipline is built into the settings you choose once and reuse on every swap.
Frequently Asked Questions
What is MEV in crypto?
MEV (Maximal Extractable Value) is the profit that block producers or specialized "searchers" can capture by reordering, inserting, or censoring transactions in a block. On a DEX, the most common form is the sandwich attack, where a bot front-runs and back-runs your swap to skim value from the price impact your trade creates.
What is a sandwich attack?
A sandwich attack surrounds your swap with two of the attacker’s trades: they buy just before you (pushing the price up), let your buy execute at the worse price, then sell right after. The difference is their profit and your loss. It works on trades with meaningful price impact and loose slippage settings.
How do I protect my swaps from MEV?
Three practical defenses: set a tight, sensible slippage tolerance so an attacker cannot move the price far enough to profit; trade liquid pairs where your swap causes little price impact; and, where available, route through private order flow so your pending transaction is not visible to searchers before it confirms.
Does MEV affect small trades?
Small trades on liquid pairs are usually not worth attacking, because they create little price impact and the attacker’s costs would exceed the gain. MEV mostly targets larger swaps and illiquid pairs. Keeping trades reasonably sized on deep pairs is itself a form of protection.
Ready to automate your crypto trading?
Set up DCA, grid, or limit-order strategies on Uniswap V3 for Arbitrum and Base — from your own Safe, with a free demo to start.