DeFi sandwich attacks explained
Maximal Extractable Value (MEV) is a serious problem in the Ethereum ecosystem that accounts for more than $1.3 billion in lost value for traders just over the last few years. While there are different types of MEV, most extracted value comes from the dreaded sandwich attack.
Sandwich attacks occur when a user’s transaction gets trapped, or “sandwiched,” between two hostile transactions — one before and one after. As a result, the original transaction executes at a much higher price than necessary, leading to an inflated price for the original trader and a profit for the malicious trader placing the two extra trades (known as a “searcher”).
What's the difference between a sandwich attack and other forms of MEV?
Sandwich attacks specifically target trades by manipulating prices before and after a transaction. Unlike arbitrage MEV which exploits price differences across platforms, sandwich attacks directly harm individual users. Sandwich attacks are particularly damaging because they directly steal value from regular traders by impacting the price before the trade executes.
Overview: How a sandwich attack works
When a searcher spots an opportunity to sandwich a transaction, they place a trade before (known as “frontrunning”) and a trade after (known as “backrunning”) the transaction in order to manipulate the price of the trade.
Due to the design of Automated Market Makers (AMM) such as Uniswap, these trades strategically manipulate the price of the assets, leaving room for profit. To really understand how sandwich attacks work, though, we first need to understand some core concepts.
If you don't know what #MEV (Miner/Maximal Extractable Value) is and how it can hurt your swaps, make sure to watch this video! 🐮#CoWGMI 🐄 https://t.co/2g1cFLYvAL
— CoW DAO | CoW Swap & MEV Blocker (@CoWSwap) March 1, 2022
What makes a sandwich attack possible?
Let’s look at sandwich attacks on a more granular level. The following concepts and\ tech make sandwich attacks possible.
Transaction reordering
Your crypto trade doesn't always happen immediately. It waits in line with other trades.
But here's the problem: the order can be changed. Attackers can pay extra fees to cut in line. They can place their trades before and after yours.
This reordering is what makes sandwich attacks possible. Attackers use it to manipulate prices around your trade.
Automated market makers (AMMs)
Think of AMMs as robot traders that never sleep. They let you buy and sell crypto instantly.
Traditional exchanges need another person to trade with you. You need a seller to buy from someone. AMMs work differently.
AMMs keep pools of two different cryptocurrencies. For example, ETH and another token called COW. The AMM automatically sets prices based on how much of each token is available.
When you buy COW, you're taking COW from the pool. You're also adding ETH to the pool. This changes the balance and moves the price.
If more people buy COW, its price goes up. If more people sell COW, its price goes down. The AMM adjusts prices automatically to keep things balanced.
Slippage tolerance
Crypto prices change constantly. Your trade might execute at a different price than expected.
Slippage tolerance is your safety buffer. It's the maximum extra amount you're willing to pay.
Here's an example: ETH costs $2,000 right now. You want to buy some ETH. You set 5% slippage tolerance. This means you're okay paying up to $2,100 for ETH. If the price goes higher than $2,100, your trade fails.
You need some slippage tolerance because prices move fast. But setting it too high creates problems. It gives attackers room to manipulate your trade.
Price impact
Every trade moves the market price slightly. This movement is called price impact.
Small trades barely affect prices. A $100 trade won't change ETH's price noticeably.
Big trades move prices more. A $1 million trade will definitely change the price. This happens because large trades use up more of the available tokens.
Think of it like a small pond. Dropping a pebble creates tiny ripples. Dropping a boulder creates big waves.
The sandwich attack: Step by step
Let’s examine a sandwich attack through a step-by-step example. In this example, we’re trading ETH and COW.
Step 1: Bessie wants to buy COW using her ETH. She goes to a decentralized exchange (DEX) like Uniswap, and places an order for 4,000 COW. This should cost her around 1 ETH, but due to significant market volatility, Bessie decides to set a 10% slippage tolerance. This means she’s willing to pay up to 1.1 ETH for 4,000 COW.
Step 2: Bessie’s trade enters the Ethereum mempool (the pending order queue), and a lurking searcher spots an opportunity. Springing into action, the searcher places a trade just before Bessie’s large enough to push the COW price up to her slippage tolerance. In this case, the searcher buys 4,000 COW for exactly 1 ETH. As a result of this first trade’s price impact, Bessie’s 4,000 COW purchase now costs 1.1 ETH — the maximum she’s willing to pay.
Step 3: Once the searcher’s transaction clears, Bessie’s transaction also goes through and she receives her 4,000 COW in exchange for 1.1 ETH. The searcher takes advantage of this price impact and sells his original COW at this new rate — 4,000 COW for 1.1 ETH.
In the end, the searcher buys 4,000 COW for 1 ETH and sells it for 1.1 ETH, earning a profit of 0.1 ETH (before gas and fees) for not much effort.
It’s easy to see how lucrative sandwich attacks are!
Bessie, on the other hand, ends up with a bad deal. She could have purchased 4,000 COW for just 1 ETH, but her slippage tolerance left room for a sandwich attack that forced her to pay an extra 10% for her trade.
The consequences of sandwich attacks
Sandwich attacks siphon millions of dollars from everyday Ethereum traders. Every dollar captured by searchers is a dollar stolen from the pockets of traders.
EigenPhi, a website tracking on-chain data, offers a dashboard displaying the latest sandwich attacks.
At the time of writing, sandwich attacks on AMMs and aggregators alike generate over $1 million in profit for searchers each week.
How can you protect against sandwich attacks?
There are several steps traders can take to protect themselves from sandwich attacks.
Reduce Slippage
A simple way to reduce your exposure to sandwich attacks is to decrease your slippage tolerance. Sandwich attacks rely on a high slippage tolerance that leaves arbitrage for searchers to exploit.
Lowering your slippage tolerance decreases the chance of falling victim to a sandwich attack, but, on the other hand, it means there’s a greater chance that your transaction will fail. Failed transactions can be frustrating and costly, since you still have to pay a gas fee even for failed transactions.
It can be difficult to know exactly what slippage to use. This is why using a trading mechanism solution that optimizes slippage tolerance on your behalf and doesn’t make you pay for failed transactions, such as CoW Protocol, can be useful.
Use a DEX Aggregator
Decentralized exchange aggregators scan multiple on-chain liquidity sources to offer you better prices for your trades. Spreading out a single trade across multiple liquidity pools means a reduced price impact, and thus less room for searchers to profit.
In addition, DEX aggregators often provide an “auto-slippage” option to algorithmically determine the appropriate slippage for your trade, reducing the chances of sandwich attacks.
Use a Custom RPC Endpoint
For advanced MEV protection, consider using a custom RPC (remote procedure call) endpoint for your wallet.
RPC endpoints act as intermediaries between your wallet and on-chain transactions. Most RPC endpoints (such as the default ones on your wallet) simply do the technical work of routing your trades to the chain. Others, however, will also provide protection from MEV.
A notable RPC endpoint that provides MEV protection is MEV Blocker. When using RPC’s like this, users are protected from MEV for a wide range of transactions including DeFi trading, NFT minting, liquidity providing, and more.
Use CoW Swap
For the most comprehensive MEV protection, it’s a good idea to use dApps that are specifically optimized to protect you from MEV.
CoW Swap is a meta-DEX Aggregator that combines all of the benefits of price-optimized trading with the safety features of an MEV-resistant protocol. Cow Swap matches your order against the best prices for on-chain liquidity, and protects your trades from MEV.
For a comprehensive dive into how CoW Swap protects traders from MEV, check out “How CoW Swap solves the MEV problem.”
CoW Swap’s approach to sandwich attacks
Trading on CoW Swap is similar to using a platform like 1inch or Uniswap. You simply navigate to the swap interface, select the tokens you want to trade, specify your trade amount, and click “Swap.”
Behind the scenes, however, CoW Swap is quite different.
First, your order submission is no order at all, it’s actually a signed “intent to trade” message. This message gets sent to a network of “solvers” who find the best route for your trade, optimizing price and slippage tolerance in the process.
This delegated trading model means that your transaction is protected from MEV. If there is a sandwich attack that takes place, it will be the solvers getting sandwiched, not you. And the CoW Swap solver are some of the toughest bulls in the pasture, so they’re not easy to sandwich!
But there’s more: CoW Swap also utilizes Uniform Clearing Prices (UCP) to protect you from MEV.
CoW Swap groups orders into batches and executes them all at the same clearing price per asset. This approach makes transaction reordering ineffective as an MEV strategy since all orders in a CoW Swap batch trade for the same price.
Don’t end up lunch meat
Sandwich attacks pose a constant threat to Ethereum AMM (DEX) users who may fall victim to searchers looking to make a quick buck. Trades with high slippage tolerances are most susceptible to sandwich attacks and other types of MEV. The bigger the trade, and the bigger the slippage tolerance, the higher the chance that you’ll end up as lunch meat.
Thankfully, innovations such as DEX aggregators and custom RPC endpoints have made it much easier to protect yourself from all types of MEV.
For the most comprehensive MEV protection, however, whales, DAOs, and traders of all sizes turn to CoW Swap. Built from the ground up with MEV protection in mind, CoW Swap puts users first and protects every transaction from searchers… And we’ve got the data to back it up.
So try CoW Swap for yourself and leave MEV in the past.
FAQs about sandwich attacks
What's the difference between a sandwich attack and other forms of MEV?
Sandwich attacks specifically target trades by manipulating prices before and after a transaction. Unlike arbitrage MEV which exploits price differences across platforms, sandwich attacks directly harm individual users. Sandwich attacks are particularly damaging because they directly steal value from regular traders by impacting the price before the trade executes.
Can sandwich attacks occur on all blockchains and DEXs?
Sandwich attacks can potentially occur on any blockchain with transparent mempools and AMM-based DEXs. Ethereum and EVM-compatible chains are particularly vulnerable due to their transaction visibility. Some newer blockchains implement mempool privacy features that reduce these risks significantly. Certain DEX designs like batch auctions or order book models are inherently more resistant to sandwich attacks.
How much slippage tolerance is safe to use when trading?
The ideal slippage tolerance balances protection from sandwich attacks with transaction success. For small trades on liquid pairs, 0.5-1% slippage may be sufficient. Larger trades or trades on illiquid pairs might require 2-3% slippage to execute successfully. Consider using platforms that intelligently calculate optimal slippage settings based on trade size and current market conditions.
How do custom RPC endpoints protect against sandwich attacks?
Custom RPC endpoints like MEV Blocker route transactions through protected channels rather than public mempools. They often use private transaction bundles submitted directly to validators, bypassing searcher visibility. Some RPCs employ sophisticated algorithms to detect and prevent harmful reordering of transactions. This protection extends beyond sandwich attacks to other MEV forms, safeguarding users across various DeFi activities.