b00ae5
What Is a Sandwich Attack on a DEX Swap? The Price Limit
A sandwich attack on a DEX swap uses a visible pending order to move the pool price against you: a bot buys first, your swap fills worse, then it sells.
The Hashbeam Desk··5 min read

A sandwich attack on a DEX swap is a form of MEV in which a bot buys just before your trade and sells just after, leaving you with a worse price while the bot keeps the difference. It works when the attacker can see your pending swap and use its slippage tolerance to estimate how far the price can move before your transaction reverts.
The three transactions are ordered around yours: the attacker’s buy comes first, your swap follows, and the attacker sells last. The first trade moves the pool price against you; your swap then executes at that worse price, and your trade’s price impact can leave the attacker able to sell at a profit. The term MEV was popularised by the 2019 paper Flash Boys 2.0, which examined transaction ordering and front-running in decentralised exchanges.
On SyncSwap, a decentralised exchange protocol for trading tokens and accessing liquidity pools on supported networks, a wallet connection is the step that lets a user submit a swap; connect wallet syncswap names that connection task. Whether a pending swap can be targeted depends on how transactions are exposed and ordered on the network.
How does a sandwich attack on a DEX swap work?
A sandwich attack uses the pool’s changing price and your minimum-received limit to place a buy before your swap and a sell after it. A DEX swap does not promise a fixed price while it waits to be included: the pool state can change before execution, and slippage tolerance sets the worst output you will accept.
Say you plan to swap a large amount of one token for another in a pool with little liquidity. Before your transaction executes, a searcher sees the order in the public mempool and estimates its effect on the pool. The searcher buys the token you want, moving its price up. Your swap then executes against the changed pool, so you receive fewer tokens. The searcher sells after your trade, using the price movement your order helped create.
Your slippage tolerance is the attacker’s budget for worsening your fill. With a 3% tolerance, your transaction can still succeed if the output stays just above the minimum you set, roughly 3% below the expected output. That does not mean the attacker pockets the full 3%: pool fees, the cost of moving the price, competition, and the attacker’s own trade size affect the profit. But the tolerance tells the attacker how much worse your fill might get before the swap reverts.
Compare two otherwise identical swaps: same trade size, same 3% tolerance, and the same transaction visibility. In the deeper pool, the attacker’s buy moves the price less, so there may be little room to profit after fees. In the shallower pool, the same buy moves the price further; your large trade then pushes it further still, giving the attacker a larger potential resale gain. Larger trades in shallower pools create more room for sandwich profit because they move the pool price more.
A tighter tolerance reduces the maximum price worsening your transaction will accept. It also makes a revert more likely: ordinary price movement while the swap is pending can push output below your minimum, even without an attack. A wider tolerance reduces that failure risk but leaves more room for a worse fill. Choose it with the trade size, pool depth, and expected price movement in mind.
Related questions
What is front-running in crypto?
Front-running in crypto is placing a transaction ahead of a pending transaction to benefit from its expected effect. In a sandwich attack, the attacker’s buy is the front-run: it moves the pool price before the victim swap executes. The attacker’s sell afterward is the back-run. Ethereum.org describes this pattern as a searcher watching the mempool for a large DEX trade, buying before it, then selling after it. Ethereum.org’s MEV explanation uses a Uniswap trade to illustrate the mechanism.
What is MEV?
MEV, or maximal extractable value, is value gained by influencing the order, inclusion, or exclusion of transactions in a block. Sandwiching is one form: the searcher seeks profit by placing its own transactions around a user’s swap. The ordering matters because the pool calculates each swap against its current state; the attacker’s first transaction changes the state your swap encounters.
Does a private RPC protect my swaps?
A private RPC can reduce exposure on Ethereum mainnet by sending a transaction to builders without placing it in the public mempool where sandwich bots can watch. Flashbots Protect says its transactions are hidden from the public mempool to provide front-running protection, though the transaction is still shared with the service and relevant builders under its submission flow. Flashbots Protect’s documentation explains how private submission works.
On SyncSwap’s zkSync Era deployment, the protection picture is different: transactions go to an operator-run sequencer for ordering, rather than sitting in a public mempool for bots to watch. That reduces the public-mempool route described above; it does not make slippage tolerance irrelevant or establish that every possible ordering risk is absent. Check which network your swap uses, set a tolerance that reflects the pool and trade, and consider private submission where it is supported.
- A sandwich orders an attacker’s buy before your swap and sell after it.
- Your slippage tolerance sets the room for a worse fill before the swap reverts.
- Large trades in shallow pools offer more potential profit; tighter tolerance limits loss but raises revert risk.
- Private submission can hide mainnet swaps from the public mempool; zkSync Era uses sequencer ordering.