What is a sandwich attack?

Let's consider a simple example:

A slippage/sandwich attack exploits the victim's high slippage tolerance, in our scenario, 10%.

Here's how such an attack typically unfolds:

1. Observation:

The attacker monitors the pool for large pending transactions, such as a substantial buy or sell order, which in this case would be the victim's order. The attacker looks for transactions with high slippage because they offer a wider price range for exploitation.

2. Initiation of the Sandwich Attack:

2.1 First Transaction (Front-Running):

The attacker places a large buy order right before the victim's transaction is executed. Since Uniswap V2 operates on an automated market maker (AMM) model, this buy order increases the price of the token because the AMM adjusts the price based on the ratio of the two tokens in the liquidity pool.

2.2 Victim's Transaction:

The victim's transaction is then executed, but due to the increased price caused by the attacker's buy order, the victim receives fewer tokens than initially anticipated. This is where the high slippage of 10% plays a critical role. It allows the transaction to go through despite a significantly worse rate.

Second Transaction (Back-Running):

After the victim's transaction, the attacker completes the sandwich by selling the tokens they initially bought. This sell order typically moves the price back down, allowing the attacker to profit from the price differential.

2.3 Outcome:

The victim ends up buying at a higher price and/or selling at a lower price than they would have without the attacker's interference. The attacker benefits from the price difference between the three transactions.