A sandwich is the simplest extractive trade there is. Someone sees your swap waiting to be included in a block, buys the same asset immediately before it, and sells immediately after. Your trade pushes the price up, their sale takes the difference, and you receive fewer tokens than the interface quoted.
Nothing about it is a hack. It is ordinary transaction ordering, visible to anyone watching the public mempool. Which is why the defences are procedural rather than technical, and why most of them cost nothing.
1. Recognise which trades are worth sandwiching
The attack only pays when your trade moves the price enough to cover the attacker’s own gas and fees. That makes the target profile specific: large trades relative to pool depth, generous slippage settings, and thin pools where a modest order shifts the curve.
A small swap in a deep pool is generally not worth anyone’s effort. If your order is a noticeable share of the liquidity, assume you are visible and worth taking.
2. Set slippage to the smallest number that still executes
Slippage tolerance is a permission. It states the worst price you will accept, and an attacker can extract exactly up to that limit and no further. A default of one or two percent on a volatile token is an invitation, and raising it to fifty percent to force a stuck trade through is the most expensive click in decentralised trading.
Start low, let the transaction fail, and raise in small increments. A failed transaction costs gas. A filled one at maximum tolerance costs the whole tolerance.
3. Split the order
Two or three smaller swaps in sequence usually beat one large one in a thin pool. Each individual trade moves the price less, so each is less profitable to surround, and the pool has time to be arbitraged back toward fair value between them.
The cost is extra gas. Compare that against the price impact your full size shows in the quote, because on a shallow pool the impact number is frequently several times the gas.
4. Route through an aggregator
An aggregator splits one order across several pools and venues. A sandwich then has to be constructed against multiple pools at once, which raises the attacker’s cost and lowers their edge, sometimes past the point of being worth it.
This is a side benefit rather than a defence in itself, and it comes with its own routing considerations, covered in our guide to DEX aggregators.
5. Use a private transaction endpoint
This is the actual fix. Instead of broadcasting to the public mempool where anyone can read your pending trade, you submit it directly to block builders. The trade is not visible until it is already included, so there is nothing to front-run.
Most major wallets now offer this as a network setting or a toggle described as protected or private transactions. Turning it on takes under a minute and applies to every subsequent trade. On chains where it is available, it removes the attack rather than pricing it down.
Two limits are worth knowing. Private routing can mean slower inclusion during congestion, and it depends on the builders you are submitting to, which is a trust assumption of its own, just a much narrower one than broadcasting your intent to the entire network.
6. Avoid the moments when everyone is trading
Launches, unlock dates, listing announcements and sharp market moves are when extraction is most concentrated, because pools are volatile and slippage settings across the market are loose.
If the trade does not have to happen in the first minutes of an event, it will almost always execute better afterwards, on quieter blocks and a rebalanced pool.
7. Check what you actually received
After the swap, compare the tokens received against the quote you accepted. A gap close to your full slippage tolerance is the signature of a sandwich, and a small gap is ordinary price movement between quote and inclusion.
Open the transaction in a block explorer and look at its position within the block. A buy of the same pair immediately before yours and a sell immediately after, from the same address, is the attack written out in full.

What none of this prevents
Sandwiching is distinct from a token whose contract blocks selling, from a pool whose liquidity is withdrawn after you buy, and from ordinary price impact in a shallow market. Those are different problems with different checks, covered in how to buy altcoins on a DEX.
Private routing also does not exist on every chain, and on chains with a single sequencer the ordering risk moves to the sequencer rather than disappearing. Knowing which model the chain uses tells you which of these steps is doing the work.
For teams launching a token, this is a first-impression problem: the earliest buyers get the worst execution, and they say so publicly. Pool depth at launch and the messaging around it are covered on our crypto traffic acquisition page.