A sandwich attack is a type of malicious activity on decentralized exchanges (DEXs) that exploits how the blockchain processes and orders transactions. Any user exchanging tokens on a DEX can encounter it, and the risk is especially high during periods of intense network activity when there is competition for block space.
Understanding the mechanics of these attacks is important for everyone working with DeFi, because asset security and the quality of trading decisions depend on it. A sandwich attack causes your trade to be executed at a worse price than you saw on screen, costing more than expected. If you understand how this works, you can anticipate such situations and at least partially mitigate their impact. This knowledge helps you navigate DeFi more consciously and safely.
KEY CONCEPTS
A sandwich attack is a sophisticated trading strategy that attackers use on decentralized exchanges. To fully understand it, keep a few terms handy:
- Decentralized Exchange (DEX): A platform where cryptocurrencies are exchanged directly between users, without a custodial intermediary.
- Transaction Order: The sequence in which transactions are confirmed and included in a block.
- Miner Extractable Value (MEV): The profit a block creator gains when they arbitrarily include, exclude, or reorder transactions within a block.
- Slippage: The difference between the expected price of a trade and the price at which it was actually executed.
- Front-running: A trade placed before another transaction, based on knowledge of that transaction.
- Back-running: A trade placed immediately after another transaction to profit from its market impact.
HOW IT WORKS
The essence of the attack is that the attacker surrounds the victim's trade with two of their own transactions. First, they place a buy order for the desired token right before the victim's order. This front-running transaction raises the token's price on the DEX.
Next, the victim's order is executed at an inflated price. Since the attacker's buy successfully pushed the price up, the victim experiences greater slippage than they would under normal circumstances. This is the first 'layer' of the sandwich. Finally, as soon as the victim's trade is confirmed, the attacker immediately sells the same token and pockets the difference created by the victim's purchase. This back-running transaction 'closes' the sandwich and turns someone else's trade into their own profit.
To make this less abstract, let's look at a simplified numerical example. Suppose you want to buy a token at a price of around 100 dollars and you see this price on your wallet screen.
| Step | What Happens | Price |
|---|---|---|
| Front-running | Bot buys token right before you | 100 → 103 |
| Your Trade | Your order executes at the shifted price | 103 |
| Back-running | Bot sells the token it just bought | 103 → 100 |
Ultimately, the bot enters at 100 and exits around 103, taking approximately three dollars per unit, while you pay 103 instead of the expected 100. These extra three percent are the cost of slippage that the attacker took.
How to distinguish a sandwich from normal slippage: With honest slippage, the price shifts due to real market flow and does not immediately return. In a sandwich attack, the price sharply moves right before your trade and almost instantly reverts in the same block because the bot closes its position. If you see two mirrored transactions in the pair's history (a large buy before yours and a large sell immediately after), this is a characteristic trace of an attack.
USE CASES
Most often, the consequences of a sandwich attack are noticeable with a large trade on a DEX. If the execution price turns out to be significantly worse than what was shown when initiating the swap, a sandwich attack could very well be the reason. This is especially true for low-liquidity pairs, where even a single trade can significantly move the price.
Such attacks are common in blockchains where transaction order can be manipulated, for example, in Ethereum before the transition to Proof-of-Stake. The profitability of an attack depends on the gas fees the attacker pays to miners or validators to position their transactions correctly around the victim's trade.
It's difficult for an individual user to be fully protected, but simply knowing about the existence of these attacks already changes one's approach to trading. You can trade during off-peak hours when competition for block space is lower, or use DEX aggregators that try to mitigate MEV.
FREQUENTLY ASKED QUESTIONS
The attacker's main goal is to profit from the price movement they themselves provoke. They achieve this by carefully placing their buy and sell orders around another's transaction and capturing the benefit from the slippage the victim experiences. Essentially, this is a form of MEV extraction.
You cannot directly stop a bot, but reducing your vulnerability is quite realistic. Trading smaller amounts, using DEX aggregators with MEV protection, and operating on networks with different transaction ordering mechanisms can help. Advanced users sometimes employ special bots that detect such attacks and even preemptively counteract them.
The profit from a sandwich attack is directly related to the size of the victim's trade and the liquidity of the pair. Large trades move the price more significantly, so the attacker extracts more from them. However, they must keep in mind the gas fees for both of their transactions, front-running and back-running, otherwise the attack will be unprofitable.