Linea Swap All articles
Security & Due Diligence

Caught in the Mempool: Understanding Sandwich Attacks and the Defenses That Actually Work

Linea Swap

The Trade You Submitted Is Already Being Watched

Every transaction submitted to a public blockchain passes through a waiting area before it is confirmed—a queue of pending operations known as the mempool. This staging environment is visible to anyone who chooses to observe it, including sophisticated actors running automated software designed to identify and exploit profitable opportunities within that queue.

For traders executing token swaps on decentralized exchanges, this visibility creates a specific and well-documented vulnerability. The attack that exploits it has acquired a vivid name in the DeFi community: the sandwich attack. Understanding how it works—and more importantly, how to reduce your exposure to it—is a practical necessity for anyone conducting swaps beyond nominal amounts.

Anatomy of a Sandwich Attack

The mechanics of a sandwich attack follow a consistent pattern. When an attacker's bot detects a pending swap transaction in the mempool, it evaluates whether that transaction is large enough, and has sufficient slippage tolerance set, to make front-running profitable. If the conditions are favorable, the bot executes two transactions in rapid succession.

The first transaction—placed before the victim's swap—buys the same token the victim intends to purchase. This purchase moves the token's price upward on the automated market maker curve, because AMM pricing is a direct function of the ratio of assets in the liquidity pool. When the victim's transaction executes moments later, it does so against this artificially elevated price, receiving fewer tokens than the original quote indicated. The attacker then immediately sells the tokens acquired in the first transaction—now at the inflated price the victim's trade helped create—capturing the price difference as profit.

The victim's transaction is, in effect, sandwiched between the attacker's buy and sell orders. The entire sequence can complete within a single block.

To ensure their transactions are processed in the correct order, attackers pay elevated gas fees—a practice that guarantees block producers prioritize their operations over the victim's pending swap. In some cases, attackers work directly with block builders through private channels, bypassing the public mempool entirely and making their own activity invisible while still being able to observe public transactions.

Why This Matters More Than Most Traders Acknowledge

The financial impact of sandwich attacks is frequently underestimated, in part because individual instances may appear small. A trader who intended to receive 1,000 USDC worth of a given token and instead receives 985 USDC worth may attribute the shortfall to ordinary slippage and move on. Aggregated across millions of transactions and thousands of traders, however, the scale of value extraction through sandwich attacks and related MEV strategies is substantial—estimated in the hundreds of millions of dollars annually across major EVM-compatible networks.

The problem is particularly acute for traders who set generous slippage tolerances. Slippage tolerance settings exist to allow a transaction to succeed even if market conditions shift slightly between submission and execution. A tolerance of 1% on a $10,000 swap creates up to $100 of extractable value that an attacker can capture without the victim's transaction failing. A tolerance of 3% on the same swap raises that ceiling to $300. Bots are calibrated to exploit precisely this window.

Traders in less liquid markets face compounded exposure. Thin pools are more easily moved by a single transaction, meaning the price distortion created by the attacker's front-run order is proportionally larger. A trader executing a meaningful swap in a low-TVL pool against a wide slippage tolerance is operating in near-ideal conditions for a sandwich attack.

The Actors Behind the Bots

Sandwich attacks are not conducted by individual hackers in the conventional sense. They are largely the domain of sophisticated trading operations—sometimes referred to as MEV searchers—that run continuous monitoring infrastructure against public mempools across multiple blockchain networks. These operations are often highly profitable and technically advanced, employing engineers with backgrounds in high-frequency trading, distributed systems, and smart contract development.

The term MEV, or Maximal Extractable Value, refers to the broader category of profit that can be captured by controlling or influencing transaction ordering within a block. Sandwich attacks are one form of MEV, alongside arbitrage, liquidation front-running, and more exotic strategies. The ecosystem around MEV extraction has matured considerably, with established tooling, competitive markets for block space, and ongoing research into both attack and defense methodologies.

Understanding that you are competing against automated, professionally operated systems—not individual bad actors—is important context for evaluating the defenses available to you.

Practical Defenses Available to Traders Today

The good news is that the DeFi infrastructure community has developed meaningful countermeasures, and several are accessible to traders without requiring technical expertise.

Reduce Slippage Tolerance Deliberately. The simplest and most immediately available defense is tightening your slippage tolerance setting. While this increases the probability that a transaction fails in volatile conditions, it also reduces the extractable window that makes your trade a profitable target. For liquid pairs on well-capitalized pools, a tolerance of 0.1% to 0.5% is often sufficient. Higher tolerances should be reserved for situations where they are genuinely necessary.

Use MEV-Resistant RPC Endpoints. Several services now offer private transaction submission, routing your swap directly to block builders without passing through the public mempool. Flashbots Protect and similar services make your pending transaction invisible to searchers while still ensuring it reaches the network for inclusion. This does not eliminate all MEV risk, but it removes the mempool visibility that sandwich attacks depend on.

Leverage Aggregators with MEV Protection Built In. A growing number of DEX aggregators have integrated order flow protection mechanisms that either submit transactions privately or use commit-reveal schemes to obscure trade details until execution. When evaluating aggregator platforms, MEV resistance features are worth examining alongside routing efficiency and fee structures.

Consider Order Flow Auctions. More sophisticated market participants may explore order flow auction systems, where transaction rights are sold to block builders under conditions that guarantee users receive better execution than the open market would provide. These systems are still maturing but represent a structurally sound approach to aligning builder incentives with trader outcomes.

Break Large Swaps Into Smaller Tranches. Executing a large swap as a single transaction concentrates risk. Splitting the same trade into multiple smaller transactions over time reduces the price impact of each individual order and lowers the profitability of a sandwich attack against any single tranche, though this approach involves additional gas costs and requires patience.

The Broader Principle

Sandwich attacks are a concrete illustration of a broader principle that applies throughout decentralized finance: public information is exploitable information. Every detail of your pending transaction—the tokens involved, the amounts, the slippage tolerance, the wallet address—is available to anyone running monitoring software against the public mempool.

This does not mean decentralized trading is untenable. It means that trading effectively in these markets requires the same awareness of structural vulnerabilities that professional traders bring to any competitive environment. The infrastructure to protect yourself exists and continues to improve. The traders who use it consistently will, over time, preserve meaningfully more of their intended execution value than those who do not.

All Articles

Related Articles

Invisible Tax: How MEV Bots Quietly Skim Profits From Every Swap You Make

Invisible Tax: How MEV Bots Quietly Skim Profits From Every Swap You Make

The Bridge Between Chains Is Also a Bridge to Risk: Understanding Wrapped Token and Cross-Chain Vulnerabilities

The Bridge Between Chains Is Also a Bridge to Risk: Understanding Wrapped Token and Cross-Chain Vulnerabilities

Divided Liquidity, Unified Strategy: Navigating Token Swaps Across a Fragmented DEX Landscape

Divided Liquidity, Unified Strategy: Navigating Token Swaps Across a Fragmented DEX Landscape