Sandwich Attacks in DeFi
- Lara Hanyaloglu
- Feb 19
- 2 min read
DeFi has revolutionized trading by offering trustless, open, and decentralized markets. However, it also introduces new risks, including sandwich attacks, a type of front-running manipulation that exploits users trading on decentralized exchanges (DEXs) like Uniswap and PancakeSwap.
What Is a Sandwich Attack?
A sandwich attack is a market manipulation technique where an attacker exploits blockchain transaction transparency to profit by placing two trades around a victim’s pending transaction—one before and one after—essentially “sandwiching” the victim.
The attack takes advantage of automated market makers (AMMs), which adjust token prices based on supply and demand. The attacker artificially inflates the price before the victim's trade and then profits when the price normalizes.
How Does a Sandwich Attack Work?
1️⃣ Monitoring Pending Transactions:
Attackers use bots to scan the public mempool (the waiting area for blockchain transactions) for large or high-slippage trades that are yet to be confirmed.
2️⃣ Front-Running (The Buy Order):
The attacker places a buy order right before the victim’s transaction.
This artificially increases the asset price, making the victim’s trade execute at a higher price than expected.
3️⃣ Victim’s Transaction Executes at the Inflated Price:
The victim’s trade goes through at the now-inflated price, meaning they receive fewer tokens than anticipated.
4️⃣ Back-Running (The Sell Order):
Right after the victim’s trade, the attacker sells their tokens at the new high price, securing a profit while the victim absorbs the loss.
Example of a Sandwich Attack
Let’s say Alice wants to buy 1 ETH using USDT on a DEX.
Alice’s transaction is in the mempool to buy ETH at $3,000.
A sandwich attacker detects her trade and places a buy order for ETH first, increasing ETH’s price to $3,050.
Alice’s transaction now executes at $3,050 per ETH instead of $3,000, meaning she gets less ETH.
After Alice’s trade is confirmed, the attacker sells their ETH at $3,050, profiting from the price difference.
How Do Sandwich Attacks Affect Traders?
Higher Trading Costs: Traders pay more for their assets than expected.
Reduced Token Amount: The victim receives fewer tokens than they should.
Undermines Market Fairness: Retail traders lose money while attackers exploit inefficiencies.
Lower Trust in DeFi: Frequent sandwich attacks make users hesitant to trade on DEXs.
How to Protect Yourself from Sandwich Attacks
🔹 1. Adjust Slippage Tolerance:
Lowering slippage settings reduces the chance of being front-run but may increase failed transactions.
Recommended: Use 0.1%-0.5% slippage for low-risk trades.
🔹 2. Use Private Transactions:
Services like Flashbots, MEV-Blocker, or Tornado Cash help traders hide their transactions from bots.
🔹 3. Break Large Trades into Smaller Parts:
Large trades attract front-runners—dividing them into smaller portions can make them less noticeable.
🔹 4. Trade on Protected DEXs:
Some DEXs, like CoW Swap and Balancer, include anti-MEV (Maximal Extractable Value) protections to prevent sandwich attacks.
🔹 5. Use Custom Gas Settings:
Setting a higher gas fee may speed up your transaction, making it harder for bots to react.
In a Nutshell:
Sandwich attacks are a serious issue in DeFi, but traders can take steps to minimize risk. By using private transactions, adjusting slippage, and trading on secure platforms, users can protect their trades from front-running bots.