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

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:

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!

Pool size:

• I do not understand this part in your question : So I should still see 0.98 ETH in the pool, right? Why don't I see them? Jul 27, 2021 at 18:37
• Hello @Xavier59, As I said: the frontrunner put 47ETH in the pool with his purchase, then the other person put 0.98ETH and finally the frontrunner sell 48ETH. But if I look at the pool at that time, I should see the remaining 0.98ETH from the guy, right? Jul 27, 2021 at 19:25
• @Xavier59 I added a little image to describe the pool size during this frontrun Jul 27, 2021 at 19:31
• The frontrunner drained 0.93 eth from the pool (48.693 - 47.76) . So if the pool had very low liquidity, it is possible that it allowed the attacker drain almost all of the 0.98 eth from the user. If the pool size was `n`, the pool size should then be after the attack `n+0.98 - (48.693 - 47.76)` Jul 27, 2021 at 19:39
• This what is don't undestand: how it's possible to "drain" the pool? I mean, when you buy, you put ETH in the pool, and when you sell, you take ETH from the pool. How is it possible to drain? Jul 27, 2021 at 19:49

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.