Sandwich Attack Bad Swap: What to Do Next
In this article
If a sandwich attack has just ruined a Solana swap, the immediate steps are to stop trading, assess the loss, and consider using a private RPC or a swap aggregator with MEV protection for future transactions. You cannot reverse a completed blockchain transaction, but you can often reclaim a portion of the loss through a refund from a MEV-protective service, and you can take concrete steps to prevent it from happening again.
What Is a Sandwich Attack?
A sandwich attack is a type of MEV (Miner/Maximal Extractable Value) exploit common in decentralized finance (DeFi). On Solana, a bot monitors the mempool (pending transactions) and detects a large swap you are about to make. The bot then places a buy order just before yours (the “front” slice), driving the price up, and a sell order right after yours (the “back” slice), profiting from the price difference. Your swap executes at a worse price than the fair market rate, and the bot pockets the difference.
On Solana, this is possible because transactions are visible in the network’s gossip protocol before they are finalized. While Solana’s high speed reduces the window, it is still feasible, especially for large swaps on popular pools.
Immediate Steps After a Bad Swap
Once a sandwich attack has occurred, the transaction is final on the blockchain. You cannot reverse it. However, you can take the following steps:
- Stop trading immediately. Do not make another swap from the same wallet until you have added protection, as the bot may target you again.
- Check the exact loss. Review the transaction on a Solana explorer (e.g., Solscan) to see the execution price and the price you expected. The difference is your loss.
- Contact the swap aggregator or DEX you used. Some platforms, like Jupiter, have a MEV protection feature that may offer a refund if you were attacked while using it. Check their documentation or support channels.
- Consider a private RPC. Switch your wallet to a private RPC endpoint (e.g., Helius, Triton) that hides your transactions from public mempools. This is one of the most effective ways to avoid sandwich attacks.
- Use a MEV-protected swap aggregator. Services like Jupiter’s “Strict” mode or a dedicated MEV-protected DEX can route your trade in a way that minimizes front-running.
How to Prevent Future Sandwich Attacks
Prevention is better than cure. Here are the most reliable methods:
- Use a private RPC. This hides your transaction from public mempools, so bots cannot see it. Most wallets (e.g., Phantom, Solflare) allow you to set a custom RPC URL.
- Use a MEV-protected aggregator. Jupiter’s “MEV Protect” mode and similar services route your swap through a private RPC or use a “tip” to incentivize validators to include your transaction without front-running.
- Split large swaps. If you need to swap a large amount, break it into smaller chunks. This reduces the profit a bot can make from attacking each trade, making it less attractive.
- Set a slippage limit. Most DEXs let you set a maximum slippage. A lower limit (e.g., 1%) reduces the room a bot has to manipulate the price, though it may cause your transaction to fail if the price moves too much.
- Use limit orders. Some platforms (e.g., Jupiter Limit Order) allow you to set a specific price. The trade only executes if that price is met, reducing the risk of front-running.
Can You Get a Refund?
In some cases, yes. If you used a MEV-protected service and the attack still happened, the service may refund the loss. For example, Jupiter has a “MEV Protect” feature that, if a sandwich attack occurs, automatically refunds the difference. However, this is not guaranteed, and you need to check the specific terms of the service you used. For unprotected swaps, there is no built-in refund mechanism; the loss is permanent.
To claim a refund, you typically need to:
- Find the transaction hash on a Solana explorer.
- Contact the support team of the aggregator/DEX with the hash and details of the attack.
- Wait for their review. They may ask for additional information.
Not all services offer refunds, so it is best to use one that explicitly does.
Common Mistakes After an Attack
Many users make the situation worse by reacting hastily. Avoid these common pitfalls:
- Immediately swapping again. This often triggers another attack from the same bot, increasing your loss.
- Increasing the priority fee. A higher fee makes your transaction more attractive to validators, but it also makes it more visible to bots, increasing the risk of front-running.
- Ignoring the loss. If you do not analyze what happened, you are likely to repeat the same mistake.
- Using an unknown DEX. Smaller, less audited platforms may have weaker MEV protection, making them easier targets.
Comparing MEV Protection Options
There are several ways to protect yourself, each with trade-offs:
- Private RPCs – Effective but may have a small fee or require technical setup. They also do not protect against all types of MEV, only front-running.
- MEV-protected aggregators – Convenient, often free, but may route through a limited set of liquidity sources, potentially giving a slightly worse price.
- Limit orders – Completely avoid front-running, but you may miss the market price if it moves away from your limit.
- Splitting swaps – Simple but not foolproof; a bot could still attack each chunk, though the profit is smaller.
For most users, using a MEV-protected aggregator like Jupiter’s “MEV Protect” is the easiest and most effective option, as it requires no technical setup and is integrated into the swap interface.
Frequently Asked Questions
Can I reverse a sandwich attack transaction?
No. Once a transaction is confirmed on the Solana blockchain, it is immutable. You cannot reverse it. The only recourse is to seek a refund from the service you used, if they offer one.
How do I know if I was sandwich attacked?
Check the transaction details on a Solana explorer. If the execution price is significantly worse than the expected price, and you see a buy transaction immediately before yours and a sell immediately after, it is likely a sandwich attack. The price impact shown on your swap interface can also indicate it.
Does using a hardware wallet protect against sandwich attacks?
No. A hardware wallet only stores your private keys securely; it does not affect transaction visibility. Sandwich attacks happen at the network level, so a hardware wallet does not prevent them.
What is the best RPC to avoid sandwich attacks?
There is no single “best” RPC, but popular private RPC providers like Helius and Triton offer MEV protection features. You can find their setup guides on their websites. The key is to use a private RPC that does not broadcast your transaction to the public mempool.
Are all Solana DEXs vulnerable to sandwich attacks?
Most DEXs that use an Automated Market Maker (AMM) model are vulnerable, as the price is determined by the pool’s liquidity. However, some DEXs have built-in MEV protection or use a different model (e.g., order books) that reduces the risk. It is best to use well-known aggregators that route through protected paths.