Sandwich Attacks in DeFi: How They Work and How to Protect Yourself
In short: a sandwich attack is when a bot spots your pending swap on a decentralised exchange, buys just before you to push the price up, lets your trade fill at the worse price, then sells straight after for a profit. You still get your tokens, but fewer than you should have. It is a form of maximal extractable value (MEV). The key points:
- Large swaps with high price impact or loose slippage settings are the main targets.
- Tight slippage limits cap how much a bot can take.
- Private transaction services keep swaps out of the public mempool where bots watch.
- Aggregators and batch-auction exchanges reduce price impact and exposure.
Ethereum’s guide to maximal extractable value explains how MEV and sandwiching work, and Flashbots documents its private transaction service. Moneysmart’s overview of crypto assets covers the risks of holding crypto, and ASIC’s guidance on digital assets explains how crypto services are regulated in Australia.
A sandwich attack is a form of MEV (Maximal Extractable Value) extraction where a malicious bot “sandwiches” a victim’s pending DEX swap transaction between two of the bot’s own transactions, extracting value from the price impact of the victim’s trade. The name comes from the structure of the attack: the bot places a buy transaction before (front-running) the victim’s swap and a sell transaction after (back-running), with the victim’s transaction as the filling in the middle. The result is that the victim receives a worse price than they would have without the attack, and the bot captures the difference as profit. For Australian DeFi investors who trade on decentralised exchanges like Uniswap, Curve, or DEX aggregators, sandwich attacks are an invisible tax that can affect any swap visible in the public mempool, particularly those with large price impact or generous slippage tolerance settings.
How Sandwich Attacks Work Step by Step
Understanding the mechanics of a sandwich attack starts with understanding how AMM-based DEX pricing works. In a Uniswap-style constant product AMM, the price of a token in a pool is determined by the ratio of the two tokens in the pool (e.g., if a pool contains 1,000 ETH and 3,200,000 USDC, 1 ETH costs 3,200 USDC). When a user swaps 10 ETH for USDC, they add 10 ETH to the pool and receive about 31,683 USDC, roughly 1% less than the 32,000 the starting price implies, because the trade itself changes the pool ratio; the pool now has 1,010 ETH and about 3,168,317 USDC, and the price per ETH has fallen to about 3,137 USDC. This predictable price impact of any swap in an AMM is what sandwich bots exploit: knowing that a specific swap will move the price by a calculable amount, the bot can profitably trade around the victim’s swap.
The sandwich attack execution in detail: (1) The sandwich bot monitors the public mempool, where pending transactions broadcast their intended swap parameters before being included in a block. When the bot detects a victim’s pending swap (e.g., “swap 10 ETH for USDC with 1% max slippage”), it calculates the expected price impact and profit opportunity; (2) The bot submits a front-run transaction (buying ETH with USDC in this example, because the victim’s large USDC purchase will push the ETH price up) with a higher gas price than the victim’s transaction, ensuring the bot’s transaction is processed first in the same block; (3) The victim’s transaction executes at the now-elevated price (because the bot’s front-run has already moved the price in the direction the victim was trading); (4) The bot immediately submits a back-run transaction (selling the ETH it just bought at the now-higher price, back into the USDC that the victim’s transaction added to the pool) at a sufficient gas price to be included in the same block, capturing the profit. The victim pays a worse price than they would have received without the sandwich.
The profitability of a sandwich attack depends on the victim’s slippage tolerance and trade size. Slippage tolerance is the maximum price movement the user will accept between when they sign the transaction and when it executes: a 1% slippage tolerance means the user will accept up to 1% worse price than the quoted price at signing. This tolerance directly controls how much the sandwich bot can extract: if the slippage is 0.5%, the bot can move the price by at most 0.5% before the victim’s transaction reverts (failing due to exceeding the slippage limit). If the slippage is 3%, the bot has more room to front-run before the victim’s transaction reverts. For large trades (above AUD 10,000) with moderate slippage tolerance (0.5-1%), the absolute profit available to the sandwich bot (0.5-1% of AUD 10,000 = AUD 50-100) easily exceeds the gas cost of two additional transactions, making the attack profitable. For small trades (under AUD 1,000) with tight slippage (0.1-0.3%), the attack is rarely profitable due to gas costs exceeding the extractable profit.
How Sandwich Bots Operate
Sandwich bots operate with millisecond reaction times using dedicated infrastructure (co-located servers near major Ethereum node infrastructure, custom mempool monitoring software, pre-built transaction templates that can be filled and submitted in microseconds). The speed advantage of sandwich bots over any manual human response is essentially unlimited: by the time you notice your transaction is pending, a bot has already analysed it and submitted the sandwich if profitable. The gas price auction component of the sandwich attack (paying enough gas to be included in the same block as the victim, but no more than necessary) is handled algorithmically, with bots computing the optimal gas price dynamically based on current network conditions and the profit opportunity in each specific victim transaction. This sophistication means that attempting to “outmanoeuvre” sandwich bots through manual gas price manipulation is ineffective; the only effective defences are architectural (removing your transaction from the public mempool entirely or using DEX mechanisms that eliminate the sandwich opportunity).
Sandwich attacks are distinct from other DeFi trading risks in that they are entirely invisible to the victim at the time of execution: the victim simply observes that they received a worse price than expected (within the slippage tolerance), with no direct evidence of the sandwich attack in the transaction receipt. The sandwich bot’s front-run and back-run transactions appear in the block history near the victim’s transaction, but identifying them as a connected sandwich requires knowing which bot address executed the strategy and analysing the transaction sequence. Platforms like EigenPhi provide retrospective sandwich attack analysis that identifies whether specific transactions were sandwiched and how much value was extracted. For Australian DeFi investors who want to understand their historical MEV exposure, analysing past transactions through EigenPhi or similar platforms quantifies the actual DEX trading costs paid beyond the visible gas fees and protocol fees.
Which Transactions Are Most Vulnerable
Large trades on AMM DEXs with significant price impact are the primary sandwich attack targets. Price impact (which DEX interfaces display before trade execution) directly indicates the potential sandwich profit: a trade with 2% price impact in an AMM pool creates a 2% price range in which the front-run can operate before the victim’s transaction’s slippage limit is triggered. Trades with price impact below 0.3% are rarely targeted (below the gas cost threshold for profitability); trades with 0.5-2% price impact are commonly targeted; trades with 2%+ price impact are aggressively targeted by multiple competing sandwich bots. For Australian DeFi traders, the practical implication is that any trade large enough to show meaningful price impact in the DEX interface deserves either MEV protection routing or a transaction size reduction to bring the price impact below the profitable sandwich threshold.
Thin-liquidity token pairs are more vulnerable to sandwich attacks than deep-liquidity major pairs. A trade of AUD 5,000 in a small-cap token pool with total liquidity of AUD 100,000 creates a 5% price impact, making it highly profitable for sandwich bots even at relatively small absolute trade sizes. A trade of AUD 5,000 in the USDC/ETH Uniswap pool (with hundreds of millions in liquidity) creates a negligible price impact (less than 0.01%), making it unprofitable to sandwich. Australian DeFi investors trading small-cap or early-stage DeFi tokens with limited liquidity face the highest sandwich attack exposure, and should use MEV protection or DEX aggregator routing that splits trades across multiple smaller trades to reduce individual pool price impact.
Time-sensitive transactions (like liquidations or arbitrage trades that must execute in the same block as a specific event) are particularly vulnerable because the bot knows the transaction must execute now and the user cannot simply wait for better conditions. Liquidation transactions (which must be submitted promptly when a position becomes undercollateralised on DeFi lending protocols) are sometimes sandwiched by bots that front-run the liquidator, though the competitive dynamics of liquidation markets (with many bots competing simultaneously) mean liquidation sandwich attacks are more complex than standard DEX sandwich attacks. For Australian DeFi investors who are liquidators (running bots to capture DeFi liquidation rewards) rather than ordinary traders, understanding the MEV landscape for liquidations is relevant to assessing the actual profitability of liquidation bot strategies in a competitive environment.
Predictable Patterns and Repeat Targeting
Predictable transaction patterns increase sandwich vulnerability over time. An Australian DeFi investor who regularly executes large swaps at specific times (e.g., rebalancing their DeFi portfolio on the first of each month with a single large transaction) creates a predictable pattern that sophisticated adversarial actors could monitor and prepare for. Varying the timing, size, and routing of regular DeFi transactions reduces the predictability that enables targeted attacks. For institutional-sized DeFi participants (above AUD 100,000 per transaction), breaking large swaps into multiple smaller transactions through DEX aggregators that intelligently route across multiple pools (reducing the price impact of any individual pool interaction) is standard practice for reducing both sandwich vulnerability and market impact.
The DeFi security framework for thinking about sandwich attacks treats them as a trading cost that must be minimised through appropriate practices rather than eliminated entirely (since they cannot be fully prevented for all transaction types). The practical risk management: enable MEV protection for all Ethereum mainnet DEX trading (MEV Blocker or Flashbots Protect, both free), use DEX aggregators for large swaps (1inch and CoW Swap route intelligently to reduce price impact), set slippage tolerance as low as is consistent with acceptable transaction failure rates, and break large trades into smaller transactions where the price impact of any individual transaction falls below the profitable sandwich threshold. These practices collectively reduce sandwich attack exposure to near zero for typical Australian DeFi trading activity.
Protection Tools and Settings
MEV protection services are the most effective defences against sandwich attacks for Australian DeFi traders. MEV Blocker (mevblocker.io) and Flashbots Protect both work by routing your transactions through private channels that bypass the public mempool entirely: sandwich bots cannot monitor transactions they cannot see. Setting up MEV Blocker requires adding a custom RPC endpoint in MetaMask or Rabby wallet settings (Settings, Networks, Custom RPC), using the MEV Blocker RPC URL for Ethereum mainnet. This configuration routes all subsequent Ethereum mainnet transactions through the private channel automatically, providing continuous sandwich protection without any per-transaction action. The protection is essentially free (MEV Blocker monetises through other channels) and adds negligible latency for typical DeFi interactions. For Australian DeFi traders who regularly trade on Ethereum mainnet DEXs, enabling MEV Blocker is one of the highest-ROI wallet configuration changes available.
CoW Swap’s batch auction architecture provides structural sandwich protection by design, without requiring any custom RPC configuration. CoW Swap collects user orders and settles them in frequent batch auctions, with professional solvers competing to find the optimal routing and matching. Because orders are not broadcast to the public mempool in the standard transaction format that sandwich bots monitor, they cannot be front-run or sandwiched. For CoW Swap-native tokens (which the protocol supports with direct matching), the user also benefits from potential “coincidence of wants” matching (peer-to-peer matching of opposite orders without touching on-chain AMM liquidity) that eliminates price impact entirely. The trade-off is batch settlement latency (30-60 seconds rather than next-block confirmation) and potential failure if no solver can route the trade profitably. For large AMM swap transactions where sandwich risk is meaningful (above AUD 5,000 in moderate-liquidity pools), CoW Swap’s structural protection is worth the latency trade-off.
Slippage tolerance management is the within-wallet defence that does not require any third-party service. Setting slippage to the minimum that results in acceptable transaction success rates reduces the sandwich bot’s profit opportunity per transaction. For stablecoin-to-stablecoin swaps on Curve (where prices are mathematically close to 1:1), 0.01-0.1% slippage is often sufficient. For ETH/USDC or other major token pairs on Uniswap with deep liquidity, 0.3-0.5% slippage is typically adequate. For small-cap tokens with thin liquidity where large price movements between block initiations are expected, 1-3% may be necessary to achieve execution, but this range creates meaningful sandwich opportunity. Finding the minimum slippage that achieves your target transaction success rate (through observation of actual success/failure rates) is a practical calibration exercise. DeFi slippage management is directly linked to sandwich attack exposure.
DEX aggregator routing splits large trades across multiple liquidity sources to minimise the price impact on any individual pool, which reduces the sandwich opportunity even before MEV protection is applied. A AUD 50,000 swap that would cause 2% price impact in a single Uniswap pool might be split by a DEX aggregator into 5 AUD 10,000 swaps across 5 different pools (Uniswap V3 0.05% pool, Uniswap V3 0.3% pool, Curve, Balancer, and a secondary DEX), with each individual swap causing only 0.4% price impact. The reduced per-pool price impact both reduces the sandwich profit opportunity and improves the overall execution price for the user (by accessing more liquidity sources). 1inch is the most established aggregator with sophisticated routing; Velora (formerly ParaSwap) and OpenOcean are alternatives with slightly different routing algorithms and liquidity source coverage. Combining aggregator routing with MEV protection (routing through 1inch but using MEV Blocker as the underlying RPC) provides layered sandwich protection for large Australian DeFi swap transactions.
Monitoring for ongoing sandwich attack exposure is possible through blockchain analytics platforms that track MEV extraction. EigenPhi (eigenphi.io) allows searching by wallet address to see all identified sandwich attacks targeting specific addresses, including the amount of value extracted per attack. Reviewing this data for your main DeFi wallet addresses reveals whether your current practices are sufficient to prevent sandwich attacks (if the analysis shows no attacks since enabling MEV protection) or whether there are transaction patterns that remain exposed (if attacks still appear after enabling protection). The quantified evidence of actual MEV losses is often the most compelling motivation to take sandwich protection seriously, since the attacks are otherwise invisible. For Australian DeFi investors who are new to understanding MEV and sandwich attacks, running their historical wallet address through EigenPhi provides a personalised demonstration of how much this affects their specific trading activity.
Frequently Asked Questions
What is a sandwich attack in DeFi?
A sandwich attack is a form of MEV (Maximal Extractable Value) extraction where a malicious bot "sandwiches" a victim's pending DEX swap transaction between two of the bot's own transactions, extracting value from the price impact of the victim's trade. The name comes from the structure of the attack: the bot places a buy transaction before (front-running) the victim's swap and a sell transaction after (back-running), with the victim's transaction as the filling in the middle. The result is that the victim receives a worse price than they would have without the attack, and the bot captures the difference as profit.
How does a sandwich attack work step by step?
On an AMM exchange, every swap moves the pool price by a predictable amount. A bot watching the public mempool sees your pending swap, buys the same token just before you to push the price up, lets your trade fill at the worse price within your slippage limit, then sells straight after at the higher price. Your slippage tolerance sets the maximum the bot can take.
How do sandwich bots operate?
Sandwich bots run automated software that watches pending transactions, calculates which swaps are profitable to attack and submits their own trades with higher priority so they land around the victim's. They compete with each other for the same opportunities, so large swaps with loose slippage settings tend to be targeted quickly. The cost of the attack is mainly network fees, which is why small trades are rarely hit.
Which transactions are most vulnerable to sandwiching?
Large trades on AMM DEXs with significant price impact are the primary sandwich attack targets. Price impact (which DEX interfaces display before trade execution) directly indicates the potential sandwich profit: a trade with 2% price impact in an AMM pool creates a 2% price range in which the front-run can operate before the victim's transaction's slippage limit is triggered. Trades with price impact below 0.3% are rarely targeted (below the gas cost threshold for profitability); trades with 0.5-2% price impact are commonly targeted; trades with 2%+ price impact are aggressively targeted by multiple competing sandwich bots.
How do predictable trading patterns increase your exposure?
Predictable transaction patterns increase sandwich vulnerability over time. An Australian DeFi investor who regularly executes large swaps at specific times (e.g., rebalancing their DeFi portfolio on the first of each month with a single large transaction) creates a predictable pattern that sophisticated adversarial actors could monitor and prepare for. Varying the timing, size, and routing of regular DeFi transactions reduces the predictability that enables targeted attacks.
Which tools and settings protect against sandwich attacks?
MEV protection services are the most effective defences against sandwich attacks for Australian DeFi traders. MEV Blocker (mevblocker.io) and Flashbots Protect both work by routing your transactions through private channels that bypass the public mempool entirely: sandwich bots cannot monitor transactions they cannot see. Setting up MEV Blocker requires adding a custom RPC endpoint in MetaMask or Rabby wallet settings (Settings, Networks, Custom RPC), using the MEV Blocker RPC URL for Ethereum mainnet.
How does DEX aggregator routing reduce the risk?
Aggregators split a large trade across several pools and exchanges so that no single pool's price moves much, which shrinks the profit available to a sandwich bot. Combining an aggregator with a private transaction service, which keeps the swap out of the public mempool, adds a second layer of protection. Batch-auction exchanges such as CoW Swap take a different approach by settling orders together.
What are the risks associated with Sandwich Attacks in DeFi?
Sandwich attacks are invisible to the victim, appearing only as a worse execution price than expected, so most traders never realise they were targeted. Exposure scales with trade size and price impact, which means large swaps on shallow pools are the highest risk. Setting slippage tolerance too high is the most common self-inflicted cause, since it defines how much the attacker can extract. Protection routing helps but introduces a dependency on the service and can slow execution.