Jump Crypto: How to Avoid Sandwich Attacks and Achieve Optimal Swaps with Mempool Transactions

Jump Crypto: Optimize Swaps with Mempool Transactions and Avoid Sandwich Attacks

Authors: Lucas Baker, Nihar Shah, Alex Toberoff, and Suraj Srinivasan, Jump Crypto Researchers; Translation: Blocking0xxz

Decentralized exchanges (DEXs) are a core foundation of DeFi, facilitating billions of dollars in trades every day. One might think that such a critical and well-established component of the ecosystem would face significant optimization pressure, and many aspects of the “trading supply chain” have indeed been optimized, whether it be DEX aggregators that enable cross-pool trading or gasless atomic swaps.

Protocol-level innovation abounds as well, including RFQ platforms, private relays, order flow auctions (OFA), and even competitive tools for capturing the value created by trading activity. However, surprisingly little scrutiny has been devoted to analyzing the basic parameters that make trading vulnerable in the first place.

For transactions submitted publicly via the mempool, “sandwich” attacks remain a major issue, whereby value is extracted from swap trades or similar operations by placing trades for the same asset before and after the transaction. Eigenphi researchers estimate that on Ethereum alone, sandwich attacks have generated over $8 million in profit within the past 30 days, constituting the majority of all on-chain “miner-extractable value” (MEV).

While using the services mentioned above certainly enables private trading or otherwise protects the value of trades from leaking, the continued prevalence of sandwich attacks suggests that the majority of users have not adopted these services. In practice, a large number of DeFi swap trades are submitted directly through protocol front-ends using default execution parameters (e.g. maximum slippage of 0.5% on Uniswap). Whether due to unfamiliarity or reluctance to rely on third-party services, we believe this indicates a need for a simpler solution. One potential solution is smart transaction structures—optimized numbers and parameters per transaction—that can be implemented at the DEX front-end alone, without any additional trust assumptions or changes to infrastructure.

Recent research into game-theoretic approaches to preventing sandwich attacks explains how the range of slippage depending on trade size can reduce expected losses for many Uniswap traders by an order of magnitude or more. However, a wider design space remains to answer the following three questions:

  • Single swap slippage : How should a liquidity provider set the range of allowable slippage to minimize expected loss per trade?

  • Optimal swap partitioning : How should a liquidity provider split a single large swap into multiple parts executed across sequential blocks within a single pool?

  • MEV-aware DEX routing : How should a DEX aggregator platform account for MEV considerations across multiple pools to minimize expected end-to-end loss for cross-pool trades?

We recently proposed a theoretical framework to address the first two questions, simplifying them into a set of closed-form solutions. First, we show how to set parameters for a given trade size to balance expected loss incurred during execution (e.g., trade failures and gas costs) and loss extracted (e.g., sandwich attacks). Second, we show that for a given concave function where slippage increases with trade size (e.g., constant-product DEXes like Uniswap and Curve), the optimal solution is to split large trades into multiple equal-sized trades executed across sequential blocks , with the exact size determined by a value function.

While the current framework is still preliminary and theoretical in nature, we are excited to see its implementation and extension. Future work may include extending it to smaller swaps where gas fees represent a larger fraction of the wealth per trade and promoting multi-pool swaps across multiple blocks. More generally, we encourage DeFi builders to optimize the efficiency of swap markets from the perspective of both sides of the trade, help users express intelligent front-end defaults, and continue to improve market design and architecture. As trading volumes increase, applications mature, and adoption widens, providing users with efficient trading tools will become increasingly important, regardless of whether they are sophisticated or experienced with cryptocurrencies.