Sandwich trading
Wallets & security
Full explanation
Sandwich trading, also known as sandwich attacks (or sandwiching), is a trading strategy or manipulation technique in cryptocurrency markets.
This strategy exploits price fluctuations of tokens triggered by trades on decentralized exchanges (DEXs), profiting at the expense of unsuspecting traders.
How sandwich trading works
Before understanding sandwich trading, let’s briefly review how DEXs operate. In the constant product market maker (CPMM) model, a liquidity pool composed of token X and token Y follows the formula: XY = K, where K remains constant.
Transactions submitted to a DEX are not executed immediately; instead, they are sent to the mempool, where all pending transactions are visible to participants. This visibility creates an opportunity for sandwich attackers to profit at the expense of unsuspecting traders.
Suppose trader A submits a transaction to swap 10 units of token X for token Y, with a slippage tolerance of 1% (i.e., trader A accepts up to a 1% deviation in the final received value), in a pool holding 100 units of token X and 100 units of token Y, with a gas fee of 0.3%.
Under these parameters, trader A expects to receive 9.066 units of token Y.
However, a sandwich attacker strategically places two orders: one before trader A’s transaction (a “front-run order”) and one after (a “back-run order”), to profit from the resulting price movement.
First, the attacker uses 0.524 units of token Y to buy 0.529 units of token X, paying a higher gas fee. Due to the mechanics of the CPMM model, this front-run order increases the price of token Y.
As a result, trader A’s transaction executes at a higher price, yielding only 8.975 units of token Y — exactly 1% less than expected, matching trader A’s maximum slippage tolerance.
Then, the sandwich attacker sells the 0.524 units of token Y (the “back-run order”) at an even higher price, after trader A’s trade has further pushed up the price of token Y, receiving 0.635 units of token X.
The attacker’s profit from this sandwich attack is 0.106 units of token X (0.635 − 0.529 = 0.106).
Generally, the profitability of a sandwich attack increases with the size of the victim’s trade and their slippage tolerance.
Impact of sandwich trading
When used for manipulative purposes, sandwich trading can negatively affect decentralized exchanges (DEXs) and the broader cryptocurrency trading ecosystem.
1. Market manipulation
Sandwich trading constitutes a form of market manipulation that exploits price discrepancies for profit at other traders’ expense. This undermines public trust in markets and may deter legitimate participants.
2. Financial loss
Victims of sandwich trading may suffer financial losses due to price manipulation, fueling dissatisfaction and distrust within the cryptocurrency community.
3. Reduced liquidity
Frequent sandwich attacks may discourage liquidity providers from participating in DEXs, thereby reducing overall market liquidity.
How to protect yourself from sandwich attacks
You may consider the following methods to safeguard your personal trades against sandwich attacks.
1. Use limit orders
Prefer limit orders over market orders whenever possible. Although many DEXs do not support this order type, some do. Consider using DEXs that offer limit orders, allowing you to specify the exact price at which you are willing to buy or sell assets.
2. Set low slippage tolerance
Maintain a relatively low slippage tolerance, which reduces the potential reward for sandwich attackers. However, many DEXs now implement automatic slippage settings. Setting slippage too low may also delay transaction execution.
3. Split large trades
Large single trades are prime targets for sandwich attackers. Breaking them into smaller orders may help reduce potential slippage.
How DEXs can mitigate sandwich attacks
The following are strategies DEXs may adopt to reduce the negative impact of sandwich trading:
1. Anti-front-running measures
DEXs can implement mechanisms to detect and prevent front-running and sandwich trading — for example, by delaying order execution, randomizing execution order, or improving matching algorithms.
2. Transaction analysis tools
DEXs can develop or integrate tools that analyze transactions for suspicious patterns. Such tools can identify and flag potential sandwich attacks for further review.
3. Algorithmic trading policies
DEXs can establish clear policies and rules governing algorithmic and high-frequency trading, helping ensure trading bots and algorithms operate within acceptable boundaries.
Source: Binance Academy
Last updated:
