[Ethereum] How do frontrunners / sandwich bots work? How do they affect the price in the pool

decentralized-exchangeflashbotsfrontrunningmev

I'm trying to understand how frontrunners work in ERC20-Uniswap.

Currently, I don't understand how they can "drain" ETH from a transaction. My understanding is that the price of the pool after the transactions should not be the same as before the transactions, but I observed that they are indeed the same (ie. the bot "drained" the ETH sent to the pool by the innocent trader).

Let's take an example:

enter image description here

As you can see, the frontrunner bought for 47.760194ETH and sell for 48.693721. So he drained 0.93352668 (huh?) from this unlucky guy (0.98 ETH), but from what I understand, the token pool should have increased by 0.98 ETH… but no, this is not true and this is what I don't understand…

Here is the concept I understand about frontrunners:

Imagine I want to buy an apple with 100$ at 1$/apple, so technically youI will buy 100 apples.
But now, a frontrunner comes and buy apples with 100k$, the price per apple is now much higher (let's take 1 apple = 100$ for the example) he will has 100k apples
Then, after his transaction, i just will buy one apple and increase the price to 120$/apple (example)
Finally, the frontrunner sell 100k apples for 120$ and makes money

From the point of view of the token pool, we will have:
-Frontrunner add 47.76 to the pool
-Unlucky guy add 0.98 to the pool
-Frontrunner makes gains and remove 48.693.. from the pool

So I should still see 0.98 ETH in the pool, right?
Why don't I see them?

Thank you for your help, it's hard for me to explain what I saw!

enter image description here

Pool size:
enter image description here

Best Answer

TL;DR Sandwich bots work by having a buy transaction before the victim's buy transaction, then a sell transaction just after. They profit from the increase of price caused by the victim's buy transaction in the middle.

When you send a swap transaction to a DEX like Uniswap/Pancakeswap, you must also specify a slippage tolerance (in %). It is like saying "I want to buy 1 apple for 100$, but I am ok if, when the transaction is processed, the price is up to 102$ and I want the transaction to go through anyway." The bot exploit this slippage tolerance for profit.

Here is a very (very) simplified example:

  1. The bot will first buy the apple at 100$ for an amount that will raise the price to 101$. The bot now has 1 apple paid 100$.
  2. The victim buys 1 apple. The victim wanted to pay 100$, but is still ok with a price of 101$ (slippage tolerance), so the transaction goes through. This purchase raises the price to 102$ after the transaction is processed.
  3. The bot sells its apple at the current price of 102$ for a 2$ profit. The price goes back down to 101$ after the transaction.

Basically, sandwich bots are extracting the slippage tolerance of traders.

What about the price discrepancy?

Your question seems to imply that the price reverts back to the original 100$ following those transactions. I don't think that it is the case. The net effect should be that the price of apples will rise because of the victim's transaction. Otherwise, the constant product formula (x*y=k) would not be upheld. If you saw the price revert back to 100$, it was most likely because of an arbitrage transaction somewhere else in the block, not the sandwich bot (but that is a subject for another day).

Related Topic