Sandwich attacks are a form of front-running on decentralized exchanges (DEXs) where a bot places a transaction before and after your swap to profit from the price movement. This guide explains how to reduce your risk by using specific tools and strategies. You’ll need access to a Web3 wallet (like MetaMask), some ETH for gas, and possibly a small amount of tokens for fees.
What You’ll Need
- A Web3 wallet (e.g., MetaMask, WalletConnect-compatible wallet)
- Ether (ETH) for transaction fees
- Access to a DEX aggregator or a private RPC endpoint (optional but recommended)
Step-by-Step
- Use a DEX Aggregator with MEV Protection – Platforms like 1inch, Paraswap, or Matcha offer “MEV protection” or “sandwich protection” modes. When you place a swap, enable this option. It routes your trade through private transactions or uses a “virtual” balance to hide the size of your trade from public mempool.
- Set a Low Slippage Tolerance – In your wallet or DEX interface, set the slippage to 1% or lower. Sandwich bots often rely on high slippage to profit. Lower slippage makes it harder for them to push the price against you. For very liquid pairs, 0.5% is safe.
- Use a Private RPC Endpoint – Services like Flashbots Protect or BloXroute offer private RPC URLs. When you connect your wallet to these, your transaction is sent directly to miners/validators, bypassing the public mempool where bots monitor pending transactions. Configure your wallet to use the private RPC (e.g.,
https://rpc.flashbots.net). - Consider a Time-Weighted Average Price (TWAP) Strategy – For large swaps, split your order into smaller chunks and execute them over several blocks. This reduces the impact of a single sandwich attack. Some DEXs like Uniswap v3 have a built-in TWAP oracle, but you can manually execute smaller trades.
- Use a Limit Order Protocol – Platforms like 1inch Limit Order or CowSwap allow you to place orders that are executed at a specific price. CowSwap uses “batch auctions” where orders are matched off-chain, making sandwich attacks nearly impossible because your order is not visible in the mempool.
- Check the Token’s Liquidity – Before swapping, verify the liquidity pool has enough depth. A shallow pool is more susceptible to price manipulation. Use tools like DexScreener to see the pool size. If liquidity is low, consider using a different pair or a DEX with deeper reserves.
Common Mistakes
- Ignoring Slippage Settings – Setting slippage to 5% or higher invites sandwich bots. Always keep it as low as possible while still allowing the trade to fill.
- Using a Public RPC – Most wallets default to a public RPC (like Infura or Alchemy). This exposes your transaction to the public mempool. Switch to a private RPC for sensitive trades.
- Not Checking Token Approval – If you have previously approved a token with unlimited allowance, a malicious contract could drain your funds. Revoke unused approvals using tools like Revoke.cash.
- Overlooking Gas Price – If you set a low gas price, your transaction may sit in the mempool for a long time, giving bots more opportunity to sandwich you. Use a reasonable gas price or a priority fee.
Tips
- Use Flashbots Protect for Ethereum – It’s free and integrates directly with MetaMask. Your transactions are sent to Flashbots, which bundles them with other transactions and sends them directly to miners.
- Trade During Low Volatility – Sandwich bots are more active during high volatility when price swings are larger. If you can wait, trade during calmer periods.
- Monitor the Mempool – Tools like Etherscan’s “Pending Transactions” can show you if your transaction is exposed. If you see a suspicious transaction right before yours, you can cancel or speed up.
- Use a Hardware Wallet – While not directly related to sandwich attacks, a hardware wallet adds a layer of security against malicious approvals.
Troubleshooting
If your swap fails or is reverted: This often happens because your slippage is too low or the price moved. Try increasing slippage slightly (e.g., to 1.5%) or wait for the price to stabilize.
If you suspect you’ve been sandwiched: Check the transaction on Etherscan. If you see a buy transaction right before yours and a sell right after, you were likely sandwiched. For future trades, use a private RPC or a MEV-protected aggregator.
If your private RPC isn’t working: Ensure you’ve entered the correct URL in your wallet settings. Some private RPCs require a fee or registration. Try a different provider like BloXroute or Eden Network.
Frequently Asked Questions
What exactly is a sandwich attack?
It’s a type of front-running where a bot detects your pending swap, buys the token first (driving the price up), then sells after your trade executes (driving it down), profiting from the price difference.
Can I get a full refund if I’m sandwiched?
No, you lose the difference between the expected price and the actual executed price. You can try to contact the DEX support, but there’s no guarantee.
Is using a private RPC 100% safe?
No, but it significantly reduces the risk. Private RPCs still rely on the provider’s integrity. Use reputable ones like Flashbots or BloXroute.
Do sandwich attacks happen on other chains like BSC or Polygon?
Yes, they are common on any EVM-compatible chain with a public mempool. The same protections apply.
Should I avoid using DEXs altogether?
No, DEXs are useful. Just use the protection mechanisms available. For large trades, consider using a centralized exchange (CEX) if you don’t mind the custodial risk.
How can I check if my token approval is safe?
Use a token approval checker like Revoke.cash or Etherscan’s “Token Approvals” section to review and revoke any suspicious allowances.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Windows Notifications: Cut Pop-Ups, Keep Alerts
- Twitter X Data: Collect It via Authorized API Access
- Microsoft Teredo Adapter Not Working? Fix It
- Microsoft Laptop vs MacBook: Key Differences
- BioShock Co-Op: Supported Modes & PC Settings
- Fix Windows 10 Troubleshooter Not Working