Learn how sandwich attacks work on decentralized exchanges (DEXs), how they exploit DeFi traders, and what strategies you can use to protect your trades.

A sandwich attack ** represents a prevalent and predatory tactic employed infront-running ** within Decentralized Finance (DeFi). This occurs when an attacker identifies a user's pending trade on a**Decentralized Exchange**(DEX) and positions their own trades around it to secure a profit. In this scenario, the attacker's transactions act as the "bread," while the victim's trade serves as the "filling."
This exploitative approach capitalizes on the transparent nature of blockchain mempools and the operational mechanics of Automated Market Makers (AMMs). The attacker profits directly from the victim, who ends up receiving a less favorable price for their trade than anticipated.
This article details the mechanics of sandwich attacks, explores the reasons behind their occurrence, and outlines protective measures users can adopt.
| Concept | Description |
|---|---|
| Core Concept | A sandwich attack involves three key steps: the attacker buys, the victim trades, then the attacker sells. |
| Potential Victim | Users executing large trades on a DEX with loose slippage settings are prime targets. |
| Attacker Profile | Bots designed to monitor the public mempool for profitable sandwiching opportunities. |
| Outcome for Victims | Victims receive fewer tokens than expected, suffering from value extraction by the attacker. |
| Effective Countermeasures | Implementing tight slippage tolerances and using MEV (Maximal Extractable Value) protection services. |
To illustrate a sandwich attack, consider the following three-step process. Bob intends to swap a significant amount of ETH for a token named "CAT" on a DEX such as Uniswap.
The block is now forming with the bot's transaction prioritized ahead of Bob's.
Outcome:- Bob receives fewer CAT tokens for his trade because the bot's initial transaction increased the price, forcing him to purchase at a less favorable rate. The difference between the expected and actual amount received constitutes Bob's slippage.
Sandwich attacks exploit specific characteristics of public blockchains:
DeFi users can take proactive measures to mitigate the risk of sandwich attacks:
Set Low Slippage Tolerance: This is the most effective method of protection. Slippage represents the allowable price change for a trade to execute. Many DEX interfaces default to higher percentages, inviting sandwich bots. Adjust your slippage to a lower value. If a bot attempts a sandwich and shifts the price beyond your slippage, your transaction will fail, preserving your investment.
Use MEV Protection Services:
Trade on Less Popular DEXs: Bots often target high-volume DEXs like Uniswap. Trading on less frequented platforms may lower the risk of sandwich attacks but can involve different risks and potentially higher fees.
Divide Large Trades: Instead of executing a single large trade, consider splitting it into several smaller transactions. Smaller trades exert less price influence and are less attractive targets for sandwich bots.
A: Typically, no. Sandwich bots seek trades large enough to move prices significantly, covering transaction costs and still yielding profit. Small retail trades generally do not present profitable opportunities.
A: Using an MEV protection service like Flashbots comes closest to guaranteeing this, as it completely conceals your transaction from the public mempool where bots operate.
A: No. You do not lose your principal investment. Instead, you lose value due to slippage. For example, you may expect a certain number of CAT tokens but receive fewer, with the value of the missing tokens representing the bot's profit.
A: Some protocols are beginning to integrate protective measures. For instance, CowSwap employs batch auctions to mitigate risks. However, standard AMMs like Uniswap face challenges in preventing MEV without significant changes to their architecture.
Not all decentralized exchanges possess the same vulnerability to sandwich attacks. Some protocols have been specifically designed or modified to reduce MEV exposure for traders.
| DEX Name | Protection Mechanism |
|---|---|
| CoW Protocol (CoW Swap) | The largest MEV-resistant DEX by volume, using batch auctions that settle trades at a uniform price, preventing individual order exploitation. |
| 1inch Fusion | Employs a Dutch auction mechanism with a network of resolvers who fill orders off-chain, keeping trades off the public mempool. |
| Flashbots Protect RPC | A free RPC endpoint that can be added to any wallet, routing transactions privately to block builders and eliminating front-running exposure. |
| Paraswap Delta | Offers private transaction routing through its order-flow system, providing equivalent protection against mempool visibility. |
Uniswap v4 hooks | Introduces a programmable layer to AMM logic, allowing the implementation of MEV protection mechanisms directly at the protocol level. |
For effective MEV protection, maintain slippage tolerance at a low percentage, use a private RPC endpoint, and choose DEX aggregators that route trades through protected channels when trading larger amounts.
In the evolving DeFi ecosystem, understanding and mitigating sandwich attacks is essential. By adopting these strategies, traders can safeguard their transactions and enhance their trading outcomes.
Explore more guides and career playbooks