Sandwich Attack

Definition

A sandwich attack is a form of Maximal Extractable Value (MEV) exploitation in decentralized finance (DeFi) where an attacker strategically places two transactions around a victim’s pending swap transaction on a decentralized exchange (DEX) — one immediately before (front-run) and one immediately after (back-run) — to extract profit at the victim’s expense. 

The attack works by exploiting the transparent nature of blockchain mempools and the mechanics of automated market makers (AMMs). When a victim submits a large swap on a DEX like Uniswap (e.g., buying ETH with USDC), the transaction sits in the public mempool waiting to be included in a block. 

An attacker (often a MEV bot) detects this pending transaction, calculates the price impact it will cause, and executes the following sequence: 

(1) Front-run — the attacker buys the same token just before the victim, pushing the price up;
(2) Victim’s transaction executes at the now-inflated price, paying more than they would have without the attacker’s interference;
(3) Back-run — the attacker immediately sells the token they just bought at the higher post-victim price, pocketing the difference as profit. 

The victim receives fewer tokens than expected, effectively paying a hidden tax to the attacker. Sandwich attacks are extremely common on Ethereum and other EVM-compatible chains where DEX trading is prevalent. 

Research by Flashbots and academic groups has estimated that sandwich attacks extract hundreds of millions of dollars annually from DeFi users. The attacks are facilitated by the fact that block proposers (or MEV searchers working with them) can order transactions within a block to maximize extraction. 

Common mitigation strategies include using private mempools (Flashbots Protect, MEV Blocker), setting tight slippage tolerances, using DEX aggregators with MEV protection (CoW Swap, 1inch Fusion), and trading on chains with encrypted mempools or time-based ordering. 

Sandwich attacks represent one of the most visible and economically damaging forms of MEV, and they highlight a fundamental tension in transparent blockchain architectures: the same openness that enables trustless verification also enables adversarial transaction ordering.

Origin & History

DateEvent
2017Early “priority gas auctions” (PGA) identified on Ethereum — precursor to MEV extraction
2019 (Apr)Daian et al. publish “Flash Boys 2.0” — first academic paper formally documenting DEX front-running and MEV
2019Term “sandwich attack” coined to describe front-run + back-run pattern on Uniswap
2020DeFi Summer explodes DEX volume; sandwich attacks become widespread and profitable
2020First automated sandwich bots appear; gas price bidding wars ensue
2021Flashbots launches — introduces MEV-aware block building and private transaction pools
2021Sandwich attacks extract estimated $200M+ from Uniswap users in a single year
2022MEV-Boost launches; PBS (Proposer-Builder Separation) restructures MEV extraction
2023Flashbots Protect and MEV Blocker (CoW Protocol) offer user-facing sandwich protection
2023jaredfromsubway.eth bot identified as one of the most prolific sandwich attackers ($40M+ revenue (~$6-10M net profit))
2024CoW Swap, 1inch Fusion, UniswapX offer built-in MEV protection via intent-based trading
2025Encrypted mempools and threshold encryption proposed as protocol-level sandwich defenses

“Sandwich attacks are the invisible tax on every DeFi swap. Most users don’t even know they’re being sandwiched — they just wonder why they got fewer tokens than expected.” — Flashbots research team

How It Works

Attack ComponentWhat HappensWhy It Works
Front-runAttacker buys target token before victimPushes price up; mempool is public so attacker sees pending trades
Victim tradeVictim’s swap executes at inflated priceAMM formula gives fewer tokens due to front-run’s price impact
Back-runAttacker sells target token after victimSells at higher post-victim price; locks in profit
Gas manipulationAttacker pays higher gas to ensure orderingBlock builders include higher-gas txs first (or use MEV bundles)

In Simple Terms

  1. A Crypto Ambush: A sandwich attack is like someone cutting in front of you at a currency exchange, buying up the currency to raise the price, then selling it back after you buy at the inflated price. They profit from the price difference you unknowingly paid.
  2. Front-Run + Back-Run = Sandwich: The attacker places one transaction BEFORE yours (front-run) and one AFTER yours (back-run), “sandwiching” your trade. They buy before you to raise the price, and sell after you at the higher price.
  3. Mempool Exploitation: On Ethereum, pending transactions sit in a public waiting room (mempool) visible to everyone. MEV bots scan this mempool 24/7, looking for profitable trades to sandwich.
  4. Slippage Is the Attack Surface: The maximum price movement you’ll accept (slippage tolerance) determines how much an attacker can extract. Setting 5% slippage means an attacker can push your price up to 4.9% against you.
  5. Billions Lost Annually: Sandwich attacks collectively extract hundreds of millions to billions of dollars per year from DeFi users. Most victims never even realize they were attacked — they just receive slightly fewer tokens.

Real-World Examples

ScenarioImplementationOutcome
jaredfromsubway.eth (2023)Single MEV bot conducted thousands of sandwich attacks daily on UniswapExtracted over $40M in gross revenue; spent $30M+ on gas fees; net profit ~$6-10M
Uniswap V2/V3 sandwichingBots monitor Uniswap pools for large pending swapsEstimated 3-5% of all Uniswap V2 swaps were sandwiched at peak (2021-2022)
Flashbots Protect adoptionUsers route trades through private mempool to avoid sandwich botsMillions of transactions protected; significantly reduces sandwich exposure
CoW Swap batch auctionsTrades matched off-chain in batches, eliminating ordering exploitationUsers get MEV-protected execution; surplus returned to traders

Advantages

AdvantageDescription
Awareness drives innovationSandwich attacks catalyzed development of MEV protection tools and protocols
Market efficiency argumentSome argue MEV extraction (including sandwiching) speeds up price discovery across venues
Research catalystAcademic study of sandwich attacks advanced understanding of DEX mechanism design
Protocol improvementLed to better AMM designs (concentrated liquidity, batch auctions, intent-based trading)
Private mempool developmentDrove creation of Flashbots Protect, MEV Blocker, and encrypted mempool research

Disadvantages & Risks

RiskDescription
Direct financial lossUsers receive fewer tokens than they would without the attack
Hidden tax on DeFiMost users don’t realize they’re being sandwiched — it appears as normal slippage
Centralization of block buildingMEV extraction incentivizes sophisticated block builders, concentrating power
Gas price inflationSandwich bots’ gas bidding wars raise transaction costs for all network users
Trust erosionSandwich attacks undermine trust in DeFi’s promise of fair, decentralized trading
Unequal playing fieldSophisticated MEV bots have massive advantages over regular users

Risk Management Tips:

  • Use Flashbots Protect or MEV Blocker RPC to route transactions through private mempools
  • Set slippage tolerance as tight as possible — 0.5-1% for stablecoin swaps, 1-3% for volatile pairs
  • Use MEV-protected DEX aggregators: CoW Swap, 1inch Fusion, UniswapX
  • Break large swaps into smaller transactions to reduce price impact and sandwich profitability
  • Consider trading during low-activity periods when fewer MEV bots are active
  • For very large trades, use OTC desks or RFQ (Request for Quote) systems instead of AMMs

FAQ

Q: How do I know if I’ve been sandwiched?

A: Check your transaction on a block explorer like Etherscan. If you see a large buy of the same token immediately before your swap and a large sell immediately after (often from the same address), you were likely sandwiched. Tools like EigenPhi and MEV Blocker Dashboard visualize MEV extraction.

Q: Can sandwich attacks happen on any blockchain?

A: They can happen on any chain with public mempools and AMM-based DEXs. Ethereum is the primary target due to high DEX volume, but sandwich attacks also occur on BSC, Polygon, Arbitrum, and other EVM chains. Chains with encrypted mempools or different ordering mechanisms are more resistant.

Q: Does setting 0% slippage prevent sandwich attacks?

A: Setting 0% slippage would prevent sandwiching but would also cause most trades to fail (any price movement between submission and execution would revert the transaction). The practical approach is setting the tightest slippage that still allows execution.

Q: Are sandwich attacks illegal?

A: The legal status is ambiguous. In traditional finance, front-running is illegal. In DeFi, there’s no clear regulation — transactions are processed according to protocol rules, and MEV extraction is technically “playing by the rules.” However, regulatory scrutiny of MEV practices is increasing.

Q: What is Flashbots Protect and how does it help?

A: Flashbots Protect is a free RPC endpoint that routes your transactions through a private mempool instead of the public mempool. Since MEV bots can’t see your transaction before it’s included in a block, they can’t sandwich it. Simply add the Flashbots Protect RPC to your wallet (MetaMask, etc.) to enable it.

UPay Tip: Sandwich attacks are the single biggest hidden cost for DeFi traders. Protect yourself by adding Flashbots Protect (rpc.flashbots.net) or MEV Blocker (mevblocker.io) as a custom RPC in your wallet — it takes 30 seconds and saves you money on every swap. Also, keep your slippage tolerance as low as possible. If you’re setting 5% slippage on a Uniswap trade, you’re leaving the door wide open for MEV bots to extract value from you.

Disclaimer: This content is for educational purposes only and does not constitute financial advice. Always conduct your own research (DYOR) and consult qualified financial advisors before making investment decisions.

UPay — Making Crypto Encyclopedic

News & Events