kommunitas logo
BlogInvesting

How to Swap on a DEX Without Getting Sandwiched

How to Swap on a DEX Without Getting Sandwiched

How to Swap on a DEX Without Getting Sandwiched

Investing • November 27, 2026

By Priyo Harjiyono

TL;DR: A sandwich attack happens when a bot spots your swap in the public mempool, buys the same token just before you, lets your trade push the price up, and sells right after you. Between November 2024 and October 2025, sandwich attacks took about $60 million from Ethereum traders in more than 95,000 attacks, per Cointelegraph Research and EigenPhi. The fix is a set of small habits: keep slippage tolerance tight, check price impact before confirming, split large orders, and route swaps through MEV protection such as a private RPC or an intent-based aggregator.

Last updated: November 2026

Key Takeaways:

  • A sandwich attack is three transactions in one block: the bot buys before you, your swap executes at a worse price, and the bot sells after you. Your slippage tolerance sets the ceiling on what the bot can take.
  • Price impact and slippage are different costs. Price impact comes from your own trade size against the pool. Slippage is what happens between your quote and execution, including bot interference.
  • Uniswap defaults to 0.5 percent slippage tolerance, with an Auto setting from 0.5 to 5 percent. Deep pairs need 0.5 percent. Thin pools need more, and that is when you should add MEV protection.
  • Private RPCs such as Flashbots Protect and MEV Blocker keep your swap out of the public mempool, so bots never see it. They are free and take about a minute to set up.
  • Intent-based swaps such as 1inch Fusion and simple limit orders remove the public transaction entirely. Competing resolvers fill the trade at an agreed rate, not by a race in the mempool.
  • This article is educational only and is not financial advice. Nothing here is a recommendation to buy, sell, or hold any token.

Picture the scene. You swap 2 ETH for a new token, the quote looks fair, and you confirm. Seconds later the result is worse than the quote by a few percent. No error, no warning, no failed transaction. The tokens arrive, just fewer than expected. A bot took the difference.

This is the most common way ordinary DEX traders lose money without ever making a bad call on a token. It needs no hack, no fake website, no compromised wallet, only your transaction sitting visible in the public mempool long enough for a bot to trade around it.

What a sandwich attack actually is

When you submit a swap on Ethereum, the transaction does not go straight into a block. It waits in the mempool, a public waiting room where pending transactions sit until a block builder picks them up. Anyone can read it, including bots that scan it for swaps large enough to move a price.

A sandwich attack runs three transactions in the same block. First, the bot buys the token you are about to buy, paying higher gas so its transaction lands ahead of yours, which pushes the price up slightly. Second, your swap executes at the worse price, so you receive fewer tokens than quoted. Third, the bot sells at the higher price your own trade helped create. The bot ends the block holding roughly what it started with, plus your money.

The bot does not need to predict the market. It only needs your transaction to be visible and your slippage tolerance to be generous. Slippage tolerance is the maximum price movement you accept between quote and execution. If you allow 3 percent, the bot can take up to 3 percent and your swap still completes. As one MEV explainer puts it, the victim's slippage tolerance becomes the bot's profit margin.

The scale is large. Cointelegraph Research, using EigenPhi data, counted more than 95,000 sandwich attacks on Ethereum between November 2024 and October 2025, extracting about $60 million. Monthly extraction fell from nearly $10 million in late 2024 to about $2.5 million by October 2025, even as DEX volume grew, which suggests these defenses are working. But the activity has not stopped: one operator, jaredfromsubway.eth, is linked to roughly 70 percent of those attacks, and its bot has extracted about 117,000 ETH, roughly $295 million, since March 2023.

Why AMMs make sandwiches possible

Most DEX swaps run through an automated market maker, or AMM. A liquidity pool holds two tokens, and the price is set by their ratio. The classic design, Uniswap v2, uses the constant product formula: the two balances multiplied together must stay constant after every trade. Buying one token out of the pool means depositing enough of the other to keep the product the same, and that deposit is what moves the price.

Here is a worked example with round numbers, before fees. A pool holds 100 ETH and 200,000 USDC, so the price is $2,000 per ETH. You want to buy 10 ETH. Without interference, the pool goes from 100 ETH to 90 ETH, and the constant product rule means the USDC balance must rise to 222,222, so you pay 22,222 USDC, an average of $2,222 per ETH. That $222 gap is price impact, caused entirely by your own trade size.

Now add a bot. It sees your swap and buys 5 ETH first, paying about 10,526 USDC. The pool now holds 95 ETH. Your swap for 10 ETH then costs about 24,768 USDC instead of 22,222, because the pool is thinner when you arrive. The bot sells its 5 ETH right after you and receives about 13,072 USDC, a gross profit of about 2,546 USDC, exactly the extra amount you paid. Your loss and the bot's profit are the same number, minus the gas it spent to jump the queue.

Three lessons follow. First, the bigger your trade relative to the pool, the more room a bot has: a $1,000 trade in a $1 million pool barely moves anything, while a $100,000 trade in the same pool is a feast. Second, thin pools are the hunting ground, since even a modest swap moves a new token's price by several percent. Third, your slippage tolerance caps the damage: at 0.5 percent, your swap would have reverted instead of executing at the inflated price.

Slippage vs price impact: two different numbers

These two terms get confused constantly, and confusing them is expensive, because each one needs a different fix.

Price impact is how much your own trade moves the pool price. It depends only on your trade size relative to the pool, and the DEX calculates it before you execute and shows it in the interface. You cannot avoid it, but you can reduce it by trading smaller amounts, splitting an order, or choosing a route with deeper liquidity.

Slippage is what happens after the quote. Between the moment you submit and the moment the swap lands in a block, other people's trades move the price. You expected $2,000, but you got $2,010 because other traders bought while you waited, or because a bot traded ahead of you. That extra cost is slippage, not price impact. You caused the price impact. Other people caused the slippage.

In practice they compound: 3 percent price impact plus 1 percent slippage costs roughly 4 percent in total. That is why the interface shows both numbers. If price impact already shows more than 1 percent, your trade is moving the market against you. Above 5 percent, split the order or find deeper liquidity. Reducing slippage tolerance does not fix price impact. It only stops the swap from executing when the combined move passes your limit.

Set your slippage tolerance deliberately

Slippage tolerance is the maximum adverse move you permit, as a percentage below the quoted amount. The swap carries a minimum received amount with it, and if the pool cannot deliver at least that much when the transaction executes, the smart contract refuses the fill. For example, if the quote is 1,700 USDC and you set 2 percent slippage, the minimum received is 1,666 USDC. A fill worse than that reverts, and you lose only the gas fee.

Uniswap's v2 documentation sets 0.5 percent as the default tolerance. The current app offers an Auto setting between 0.5 and 5 percent depending on network cost and swap size, and you can override it with a custom value. The settings live behind the gear icon in the swap panel, next to the transaction deadline, which defaults to 30 minutes. On a congested network, a long deadline gives bots more time to see your swap.

There is no single correct tolerance, but there are sensible starting points by pair type:

Pair type Starting slippage tolerance Notes
Stablecoins and deep pairs (ETH/USDC, USDC/DAI) 0.5% Deep liquidity. The default is usually right.
Major altcoins 0.5% to 1% Top tokens, deep pools.
Mid-cap tokens 1% to 3% Check price impact first.
Thin or newly launched pools 3% to 5% Only with MEV protection on.

The trade-off is real. Too low, and the transaction reverts while still consuming gas. Too high, and the trade completes at whatever worse price the market offers, up to the whole margin you allowed. Start tight, widen only when a swap fails twice for the same pair, and never past what the pool justifies. If a token needs 10 percent slippage to trade at all, the pool may be too thin to trade in the first place.

Read the quote before you confirm

Every major DEX interface shows the same four numbers before you sign: the quoted output, the price impact, the minimum received, and the route. Reading them takes thirty seconds.

Start with price impact. Under 1 percent, your size is fine for that pool. Between 1 and 5 percent, split the swap into smaller trades spaced minutes apart, or route through an aggregator that splits the order across several pools. Above 5 percent, stop and rethink the trade.

Then read minimum received, not the headline quote. The quote is a promise about this second. Minimum received is the worst outcome you have already agreed to. If that number would leave you unhappy, tighten the tolerance, and accept that the trade may fail. A failed swap costs gas. A bad fill costs the difference, and nobody refunds it.

Check the route too. Aggregators often split a swap across pools, and the interface shows that route. A route through an obscure thin pool can be worse than a direct swap in a deep pool, even if the headline number looks better. If the route looks strange, switch to a direct route and compare. And watch the clock: a transaction pending for minutes is one bots have had minutes to study. If a swap is stuck, do not submit a second one on top of it. Speed it up or cancel it, then resubmit with a tighter tolerance.

Limit orders and aggregators do the work for you

The simplest way to beat a sandwich bot is to stop giving it a market order to attack. A limit order states the price you want and nothing else. It fills only if the market reaches that price, and it cannot be pushed around by a bot, because there is no pending swap in the mempool to copy. On 1inch, limit orders are stored off-chain and executed by resolvers when conditions match. No gas is paid until execution, and partial fills are supported. The trade-off: an order may never fill if the market never reaches your price, or if the token has too little volume.

Aggregators add a second layer. A DEX aggregator such as 1inch routes a swap across more than 200 liquidity sources to find the best rate, and it can split a large order across several pools. Its Fusion mode goes further: instead of submitting a transaction to the mempool, you submit an intent, and professional resolvers compete to fill it in a Dutch auction, where the offered rate starts high and falls until a resolver takes the order. The resolver pays the gas, so the swap can be gasless, and there is no public transaction for a bot to trade around.

A swap is a spot trade: you own the token the moment it settles. If what you actually want is leveraged exposure to a price move rather than the token itself, that is a different product, and the tutorial on how to trade perpetuals without getting liquidated covers it separately. Do not use a spot swap to express a leveraged view, and do not use leverage to express a simple swap.

Batch auctions are a third option. Instead of executing trades one by one in block order, a batch auction collects orders over a short window and settles them all at one uniform clearing price. There is no advantage to being first in the block, which removes the sandwich entirely. CoW Swap is the best-known DEX built on this model.

Hide the swap from the mempool

Private RPCs are the highest-impact change in this guide, and they take about a minute to set up. Normally your wallet sends a signed transaction to a public node, which broadcasts it to the mempool, where bots can read it. A private RPC sends the same transaction straight to block builders instead, so the swap never appears in the public mempool and a sandwich bot has nothing to copy.

Flashbots Protect is the best-known option. It is free: add its RPC endpoint to your wallet and select it for Ethereum mainnet. Transactions routed through Protect go directly to participating builders. Searchers can still backrun your transaction, and part of what they pay comes back to you as a refund. By default, 90 percent of MEV refunds go to you and 10 percent to the validator, and failed transactions do not cost gas because they are never landed. As of late 2024, more than 2.1 million accounts had used Protect, with $43 billion in cumulative DEX volume routed through it.

MEV Blocker works the same way, with different endpoints: a fast endpoint that sends your transaction to all major builders, and a no-reverts endpoint that avoids landing failed transactions. Both services are free, both refund part of any backrun value, and both take about thirty seconds to configure. If you only change one wallet setting, change the RPC.

Two honest caveats. First, a private RPC is a trust assumption, not magic. The builders who receive your transaction can see it, and the protection depends on them honoring the private submission. Second, hiding the transaction does not freeze the price. Other trades can still move the pool before yours executes, so your slippage limit remains the safeguard against receiving less than your stated minimum. Use both defenses together.

Chain choice matters too. A January 2026 study found no sustained sandwiching across major rollups with private mempools, one more reason large swaps are often cheaper on a layer 2 than on Ethereum mainnet, even before gas. That does not mean layer 2 swaps are automatically safe, only that the mempool there is not the open buffet it is on mainnet.

Timing: do not swap into a storm

Volatility is the sandwich bot's best friend, and two kinds of timing deserve extra care. Token unlocks release large amounts of previously locked supply on known dates, and trading around them is chaotic, with thin liquidity and sharp moves. A swap placed minutes before a major unlock is badly timed, so check the schedule first: the guide on how to track token unlocks shows how to read vesting schedules and cross-check tracker disagreements. Market-wide volatility is the second case. During a sharp sell-off or rally, pools get thin, gas spikes, and pending transactions wait longer, exactly the combination bots exploit. If the market is moving fast and the swap is not urgent, waiting an hour costs nothing.

Red flags your swap is being eaten

Sandwich attacks rarely announce themselves, but the symptoms are consistent. Learn them, and a bad fill becomes a warning instead of a mystery.

The first sign is a fill that is always worse than the quote, by a similar amount, on the same pair. One bad fill is noise. Five in a row is a pattern, and it points at your slippage setting or your routing, not at bad luck. The second sign is repeated failed transactions on a volatile token. That usually means your tolerance is too tight, but it can also mean bots are moving the price past your limit on every attempt.

The third sign is a token whose price jumps the moment you trade and then drifts back. That snap-back is the fingerprint of a sandwich: the bot bought before you and sold after you. The fourth sign is a swap that only works with very high slippage. A token that needs 10 percent tolerance is either in a pool too thin to trust, or it is designed to punish buyers. Before swapping into any unfamiliar token, spend two minutes checking whether a token is safe before you buy it: verify the contract address from an official source, check holder concentration and locked liquidity, and look for honeypot behavior.

Other red flags are about the token itself. A contract that cannot be verified on a block explorer, an anonymous team with no audit, a website that appeared last week, or a token traded in only one tiny pool are all reasons to walk away. No slippage setting protects you from a token built to take your money.

The pre-swap checklist

Run through this list before every swap that is large enough to matter. It takes under a minute once it becomes a habit.

  1. Check the token first. Contract address from an official source, holder distribution sane, liquidity locked, no honeypot flags.
  2. Check price impact. Under 1 percent is fine. Between 1 and 5 percent, split the order. Above 5 percent, stop.
  3. Set slippage deliberately. Start at 0.5 percent for deep pairs. Widen only when a swap fails twice, and never past what the pool justifies.
  4. Read minimum received. That is your real worst case, not the headline quote. If it would leave you unhappy, tighten the tolerance.
  5. Use MEV protection. A private RPC or an intent-based swap for anything above a trivial size.
  6. Consider a limit order. If you do not need the fill this minute, name your price and let the market come to you.
  7. Check the calendar. No major unlock or volatile event in the next hour.

None of these steps is complicated on its own. Together they remove the three things a sandwich bot needs: a visible transaction, a generous tolerance, and a thin pool. A bot that cannot see your swap cannot front-run it. A bot that sees it but cannot move the price past your tolerance cannot profit from it. And a bot that faces a deep pool with a tight tolerance will go and find an easier target, because there is always one.

Frequently asked questions

Can a sandwich attack happen on any DEX?

It can happen on any AMM where transactions sit visible in a public mempool, which covers most Ethereum mainnet DEXs. It is worst on thin pools and new tokens. Chains and rollups with private mempools see far less of it. The venue matters as much as the token.

Does a low slippage setting guarantee protection?

No. A tight tolerance caps the damage, because your swap reverts instead of executing past your limit. But a bot can still extract up to the tolerance you allow, and failed transactions still cost gas. Slippage is a ceiling on your loss, not a shield. Pair it with a private RPC or an intent-based swap.

Is using a private RPC enough on its own?

It removes the biggest risk, visibility in the public mempool, but it is not complete on its own. Builders who receive your transaction can still see it, so the protection depends on them honoring the private route. Keep a tight slippage tolerance as the second layer.

Why did my swap fail even though I set slippage?

Usually the price moved beyond your tolerance between quote and execution, which is the setting doing its job. It can also mean the token is very volatile, the pool is thin, or the deadline expired while the network was congested. Try again with a wider tolerance or a smaller size.

Are limit orders always safer than market swaps?

They are safer against sandwiches, because there is no pending market order for a bot to trade around. But they carry their own risks. An order may never fill if the market never reaches your price, the token may have too little volume for a taker, and the token itself can still fall while your order sits open. A limit order fixes your price. It does not fix the token.

This article is educational only and is not financial advice. Sandwich attacks are a real cost of trading on public mempools, but they are also one of the most avoidable costs in crypto. The tools that stop them are free, the settings take seconds to change, and reading a quote before confirming it pays for itself on the first large swap. Trade like someone is watching your transactions, because on a public mempool, someone is.

Sources

Share This Article

This link will open in a new window

    How to Swap on a DEX Without Getting Sandwiched | Kommunitas Blog