Genesis Arbitrage

Genesis Arbitrage · · 10 min read

MEV and sandwich attacks on DEXs: how bots profit from your swap and how to protect yourself

In short

  • A sandwich attack is when a bot spots your swap in the mempool, buys the token right before you and sells right after, taking the difference out of your price.
  • Your slippage tolerance is what makes you a target: the wider it is, the further a bot can push the price without making your trade fail.
  • Protection means the lowest slippage that works, a private RPC or MEV-protected wallet, trade sizes that fit the pool and checking the minimum received.

A sandwich attack is a way to profit from someone else's swap on a DEX. A bot spots your transaction before it lands in a block, buys the same token right before you and sells it right after. You receive fewer tokens than expected, and the difference goes to the bot. It is the best-known form of MEV — value extracted from the order of transactions in a block.

The attack does not require hacking your wallet or getting your signature. The bot relies on two things: transactions on a blockchain are visible in advance, and you yourself allowed the trade to execute at a worse price than the current one. That is why most losses can be prevented with settings that take a minute to change.

What MEV is in simple terms

MEV (Maximal Extractable Value) is the profit that can be made by choosing which transactions go into a block, in what order, and which of your own transactions to insert between them. The term appeared in 2019 in the Flash Boys 2.0 research paper, where it stood for Miner Extractable Value. After Ethereum moved to Proof of Stake, blocks are produced by validators, so "miner" in the name was replaced with "maximal".

In practice, MEV is captured not by validators themselves but by searchers — people who write bots that look for profitable combinations. Once a bot finds an opportunity, it pays a higher fee or tips the block producer directly to get its transaction into the right position.

Not all MEV hurts users:

Type of MEVWhat the bot doesWho loses
DEX arbitrageBrings a token's price back in line between pools after a large tradeUsually nobody: prices move closer to the market
LiquidationsCloses undercollateralized loans on lending protocols for a bonusThe borrower, but the liquidation would happen anyway
Front-runningCopies someone's profitable transaction with a higher feeThe author of the original transaction
Sandwich attackBuys before your swap and sells after itYou: you receive fewer tokens

The mempool and transaction ordering

When you click "Swap" on a DEX, the transaction does not execute right away. First it goes to the mempool — a shared queue of unconfirmed transactions that every node on the network can see. At that point anyone can already see which token you are swapping, for how much and the minimum you are willing to accept.

The order of transactions in a block is set by whoever builds it. Transactions with a higher priority fee usually go first, but that is not a protocol rule — it is simply what pays the block producer best. So a bot that pays more can end up both right before you and right after you.

How a sandwich attack works

On a DEX with an automated market maker (AMM), the price depends on the ratio of tokens in the pool. Every buy pushes the price up, every sell pushes it down. That is what the "sandwich" is built on: your trade becomes the filling between two of the bot's trades.

  1. The bot finds your transaction

    The bot watches the mempool for swaps worth attacking: large relative to the pool and with room in their slippage settings. It instantly calculates how far it can move the price while your trade still goes through.

  2. First trade: buying before you

    The bot buys the same token and places its transaction directly ahead of yours. The price in the pool goes up.

  3. Your swap executes at a worse price

    Your transaction goes through at the inflated price. You get fewer tokens, but not fewer than the minimum you set, so the swap is not cancelled.

  4. Second trade: selling after you

    Right after you, the bot sells what it bought. Your purchase pushed the price even higher, and the bot exits with a profit after pool and network fees.

All three transactions usually land in the same block and execute within seconds. From the user's side it simply looks like a bad rate: the swap went through, you just received fewer tokens than expected.

Why high slippage tolerance makes you a target

Slippage tolerance on a DEX is something you set yourself: it is how far the price may move away from the quote. Your wallet uses it to calculate the "Minimum received" line. If the swap would return less than that at execution, the transaction reverts and you only lose the network fee.

For a bot, your tolerance is its attack budget. It pushes the price almost down to your minimum and keeps the difference. The wider the tolerance, the more it can take and the more profitable the attack is after all fees.

A hypothetical example: you buy a token for $10,000, and the quote already accounts for your trade's impact on the pool price.

Slippage toleranceMinimum receivedHow much a bot can take beyond the quote
0.5%$9,950 worthup to $50
1%$9,900 worthup to $100
5%$9,500 worthup to $500
20%$8,000 worthup to $2,000

These numbers are illustrative: in reality the bot pays pool and network fees, and with a tight tolerance attacking a small swap often isn't worth it. But the pattern is clear: a 20% tolerance is an open invitation.

A case from March 12, 2025 shows how large the damage can get on big amounts. A trader swapped about $220K of USDC for USDT in a Uniswap v3 pool and received about $5.3K of USDT. The bot ran a textbook sandwich: it sold a huge amount of USDC into the pool before the trade and bought back after. That outcome was only possible because the transaction's minimum received allowed it.

Who gets targeted most often

How to protect yourself from sandwich attacks

How MEV affects DEX arbitrage

Arbitrage between pools is itself a form of MEV, and on-chain it is a race between fast bots. A price gap between two DEXs on the same network is often closed in the same block it appeared in. Catching such opportunities by hand is next to impossible.

For arbitrage between a DEX and a centralized exchange, something else matters. Your buy on the DEX is a swap like any other, and it is visible in the mempool. If the trade is large and the tolerance is wide, a bot will eat part of the spread before you do. A spread in a table is not yet profit: gas, fees, slippage and possible MEV losses all come out of it.

Practical rules for DEX arbitrage:

How to read the table is covered in the guide How to use the DEX – Spot table. If the topic is new to you, start with the lesson What is DEX arbitrage and continue with the lesson on DEX – Spot arbitrage. And for another way people lose money on DEXs, read Wallet drainers.

Questions and answers

How can I tell if my swap was sandwiched?

Open the transaction in a block explorer and look at the neighboring transactions in the same block. If the same address bought the same token right before you and sold it right after you, it was a sandwich. Another sign is that you received almost exactly your minimum received amount.

What slippage should I set to avoid sandwich attacks?

The lowest at which the swap goes through. For liquid pairs and stablecoins a fraction of a percent is usually enough; low-liquidity tokens need more, but every increase raises your potential loss. High tolerance is justified only when the token charges a transfer tax.

Can I get my money back after a sandwich attack?

No. The trade executed within the terms you signed, and it can't be reversed on the blockchain. That is why protection has to happen before the swap: with tight slippage and private submission.

Do sandwich attacks happen on centralized exchanges?

Not in this form: an exchange has no public mempool, and orders are matched by its own engine. Sandwich attacks are a blockchain problem, where transactions are visible before they are included in a block.