Front-Running and Sandwich Attacks Explained
A sandwich attack is when a bot trades just before and after your DEX swap to profit from your slippage. Learn how front-running works and how to avoid it.
A sandwich attack is when a bot spots your pending swap on a decentralized exchange, places a buy just before it to push the price up, lets your trade execute at that worse price, then sells immediately after you for a profit. It is the most common form of front-running on public blockchains, and the cost to you shows up as fewer tokens received than expected.
Many DEX users never realize they were sandwiched — the swap simply returns a little less than the quote. Over many trades, especially large ones or trades in thinly traded tokens, that hidden cost adds up. Understanding how it works makes it easy to reduce.
What Front-Running Means
In traditional finance, front-running describes a broker trading ahead of a client's order to profit from the expected price move. On public blockchains, there is no broker. Instead, anyone can see pending transactions in the mempool and try to get their own transaction placed first, usually by paying a higher fee or bidding to block builders.
This is part of a broader phenomenon called maximal extractable value (MEV) — the profit available from choosing which transactions go into a block and in what order. Our guide to MEV explains the bigger picture.
How a Sandwich Attack Works Step by Step
- 1You submit a swap, for example selling ETH for a token on a DEX, with a 3% slippage tolerance.
- 2A bot sees your pending transaction and calculates that buying the token first will raise the price.
- 3The bot's buy executes first (the front-run), pushing the token price up.
- 4Your swap executes next at the higher price, still within your 3% tolerance, so you get fewer tokens.
- 5The bot sells right after you (the back-run) at the price your trade pushed even higher.
- 6The bot keeps the difference, minus fees.
All three transactions usually land in the same block. On a block explorer, you may see the bot's buy directly before your swap and its sell directly after.
A Simple Numerical Example
Suppose you want to swap 10 ETH for a token in a modest-sized pool. The quote says you will receive 10,000 tokens, and your slippage tolerance is 3%, so you will accept as few as 9,700.
A bot buys first, pushing the price up just enough that your trade now returns about 9,720 tokens — still within your tolerance, so it executes. The bot then sells its tokens into the higher price your trade created. You lose roughly 280 tokens' worth of value compared with the quote; the bot keeps most of that, minus its fees.
Had your tolerance been 0.5%, the bot could only have pushed your outcome down to 9,950 tokens, which may not have covered its costs — so it likely would have skipped your trade. The numbers are illustrative, but the logic is why tolerance matters so much.
How to Check Whether You Were Sandwiched
- 1Open your swap transaction on a block explorer.
- 2Look at the transactions immediately before and after yours in the same block.
- 3If the same address bought the token just before you and sold it just after, you were likely sandwiched.
- 4Compare the amount you received with the original quote.
Who Is Most at Risk
| Situation | Why it attracts sandwich bots |
|---|---|
| High slippage tolerance | More room for the bot to move the price |
| Large trade relative to pool size | Bigger price impact to exploit |
| Thinly traded or new tokens | Shallow liquidity moves easily |
| Swaps sent through the public mempool | Bots can see them before inclusion |
| Volatile moments, such as launches | Users raise slippage to get trades through |
Trades in deep, liquid pools with tight slippage are generally less attractive to sandwich bots.
How to Protect Your Trades
- 1Set a sensible slippage tolerance. Use the lowest setting that lets your trade succeed. Avoid raising it to high levels just to "make it go through." See how to avoid slippage on DEX trades.
- 2Use MEV-protected routing. Many wallets and services can send transactions privately to block builders instead of the public mempool.
- 3Break up large trades into smaller swaps, or use limit-style orders where available.
- 4Prefer deep liquidity. Trading in larger pools reduces price impact.
- 5Consider aggregators or intent-based systems that match orders off-chain or settle in batches. Read intent-based trading in DeFi.
- 6Be cautious during token launches, when bots are most active and slippage is high.
Other Forms of Front-Running
- Simple front-running: a bot copies a profitable transaction, such as an arbitrage, and gets it included first.
- Back-running: a bot places a transaction right after a large trade to profit from the price change, often through arbitrage. This does not usually hurt the original trader directly.
- Liquidation races: bots compete to be first to liquidate undercollateralized loans.
- Mint and launch sniping: bots buy new tokens or NFTs in the same block they launch.
Beware of "MEV Bot" Scams
Because MEV sounds profitable, scammers sell or give away "front-running bots" through videos and social media. Typically, you are asked to deploy a contract and fund it — and the code sends your funds to the scammer. Our article on MEV bot scams on YouTube explains how these work.
Bottom line
A sandwich attack quietly takes value from your DEX trades by wrapping your swap between a bot's buy and sell. You cannot stop bots from existing, but you can make your trades unattractive targets: keep slippage tight, use private or MEV-protected routing, trade in deep pools and split large orders. More protective habits are in our security category.
Frequently asked questions
What is a sandwich attack in crypto?
A sandwich attack is when a bot sees your pending swap on a decentralized exchange, buys the same token just before you to push the price up, lets your trade execute at the worse price, and then sells right after you. The bot profits from the price difference, and you receive fewer tokens than you would have otherwise.
Is front-running illegal in crypto?
In traditional markets, brokers front-running client orders is typically prohibited. On public blockchains, bots reorder publicly visible transactions without any client relationship, and how laws apply is unclear and varies by country. Regardless of legality, it is a real cost to traders and something you can take steps to reduce.
Does a low slippage tolerance stop sandwich attacks?
It limits them. A sandwich only works if the attacker can push the price within your tolerance and still have your trade execute. A tight tolerance leaves less room for profit, so bots often skip such trades. If the tolerance is too tight, though, your trade may fail during normal price movement.
What is a private RPC or MEV-protected transaction?
It is a way of sending your transaction directly to block builders instead of the public mempool, so bots cannot see it before it is included in a block. Several wallets and services offer this option. It reduces exposure to sandwich attacks but relies on the provider behaving as promised.
