A sandwich attack is when a blockchain attacker sandwiches a swap between two transactions to make a profit.
How does a sandwich attack work?
- A user submits a swap, and it is pending confirmation.
- A blockchain attacker seeings the pending transaction, and knows the price for the token swapped will increase. So, they submit a swap. This is called front-running.
- The blockchain attackers swap is completed at a low price.
- The user’s transaction is completed at a high price, which means they receive less tokens than expected.
- The blockchain attacker swaps the tokens again at a higher price. This is called back-running.
The blockchain attacker profits from the increase in price from the previous transactions. This results in a gain for the attacker, and a loss for the user.
These attacks are possible because of price slippage, and the blockchains' transaction transparency.
Here are some ways to reduce the risk of sandwich attacks:
- Use large liquidity pools. Attackers target small liquidity pools because their transactions have a larger effect on a tokens price.
- Increase your network fee. Paying a lower network fee will slow your transaction down. Attackers look for slow transactions. It's more likely they'll be able to front-run them.
- Use Swap protection. If your wallet offers swap protection using a MEV Blocker, it can protect your swap from sandwich attacks and front-running.