Jump to content
The Coin Wire

Crypto moves, protocols and policy

How to Limit Sandwich Loss on a DEX Swap

How does a sandwich attack work on a DEX swap? A bot trades before and after your order to profit from a worse fill. Tight slippage and private routing reduce exposure.

By The Coin Wire Editorial5 min read

How to Limit Sandwich Loss on a DEX Swap

After you submit a DEX swap, a bot can spot the pending transaction, buy the token first, let your swap execute at a worse price, then sell for a profit. This sandwich attack turns your slippage tolerance and the pool’s liquidity into room for extraction.

A visible pending swap gives bots a chance to trade around it

Your transaction may wait in Ethereum’s public mempool before a block includes it. Searchers scan those pending transactions for swaps where a trade ahead of yours could move the pool price and leave room to sell afterward. Block builders order transactions by fees or include bundles, so a searcher can pay for a favorable position around your swap.

The sequence is simple: the bot buys the token you want, your swap executes at a worse rate, and the bot sells into the price increase. The bot’s profit comes from part of the value lost in your worse fill, after accounting for its own trade costs. A sandwich is worthwhile only when the expected price movement and available liquidity leave enough value to cover those costs.

Slippage tolerance sets the worst output you are willing to accept relative to the quote. That makes it a budget a bot may try to use: the bot can push the execution price toward your minimum output, but the tolerance is not guaranteed profit. Pool liquidity, trade size, fees, and competing transactions all affect whether the attack can work and how much it can extract.

Large swaps in thin pools move the price more than small swaps in deep pools. That price impact can give a searcher more room to trade around you. A loose tolerance on a large order therefore creates more potential exposure than a tight tolerance on a small one.

A 2% limit can leave room for a worse ETH-to-SHIB fill

Say you are swapping ETH for SHIB and set 2% slippage. Your wallet quotes an expected amount of SHIB, then submits the trade with a minimum output roughly 2% below that quote. The bot sees the pending order and judges whether it can move the pool price while keeping your trade executable.

  1. The bot buys SHIB before your swap. That purchase raises SHIB’s price in the pool, leaving the pool with a less favorable ETH-to-SHIB rate.
  2. Your swap executes at the changed rate. You receive fewer SHIB than the original quote; if market conditions allow, the output can land near your 2% minimum.
  3. The bot sells the SHIB it bought into the higher pool price. The sequence leaves you with less SHIB than the initial quote and the bot with any remaining value after costs.

Your 2% setting is a maximum tolerated difference from the quote, not a prediction that the bot will take exactly 2%. A smaller tolerance narrows the range in which a worsened fill can still execute, but setting it too tight can cause an ordinary price move to make the swap fail. The useful setting is the tightest one that still lets your trade fill under normal conditions.

For a large order, splitting the swap into smaller trades can reduce the price movement caused by each individual fill. It may also mean more transactions and fees, and it does not guarantee that no bot will target the trades. Compare the added cost with the price impact and exposure of sending the full amount through a thin pool at once.

Tight limits and private routing reduce exposure

Set slippage to match the pool and the trade, rather than leaving a broad default in place. A volatile pool may need more room for ordinary movement, while a deep pool and a modest trade may work with less. Watch the quoted output and minimum output before confirming; a wide gap means you are accepting more price movement before the swap reverts.

You can also send a swap through a private RPC, such as Flashbots Protect, which sends transactions privately to builders instead of exposing them in the public mempool. This can reduce the opportunity for searchers who rely on seeing pending transactions. Private routing changes who can see the transaction before inclusion; execution still depends on the route and the builder.

On ShibaSwap, as on other automated market makers, pool depth and your trade size shape the price impact a searcher may exploit. shiba swap swaps are therefore worth checking for quoted output, slippage, and routing before you confirm.

Related questions

What slippage should I set on a DEX?

Use the lowest tolerance that still allows the trade to fill under normal price movement. The right setting varies with pool liquidity, volatility, and trade size. A lower limit reduces how far execution can move from the quote, while an overly tight limit can make the transaction revert.

Is MEV the same as front-running?

MEV is value extracted by choosing or changing transaction ordering. Front-running is one tactic: placing a transaction before another. A sandwich combines a front-run buy with a back-run sale around the target swap.

What else should a swapper check?

How can I tell if my swap was sandwiched?

Compare the transaction’s execution price with its quote and inspect the transactions immediately before and after it in a block explorer. A searcher buy before your swap and a matching sale after it are clues; also account for ordinary price movement and pool impact.

Does Shibarium have the same sandwich risk?

Any network with public pending swaps, automated market maker pools, and transaction ordering can have similar risks. The details depend on that network’s mempool visibility and block-building process, so check how transactions are submitted and ordered there.

Do aggregators protect against MEV?

An aggregator can compare routes and split a trade across pools, which may improve the quoted execution. Protection from sandwiching depends on its transaction submission and routing; an aggregator using public submission can still expose a pending swap. Check whether the service offers private routing and compare its final output with a direct pool swap.