mevcheb / optimal-sandwich

we do a little bit of mathematics to make a little bit of money
537 stars 136 forks source link

Does this formula really work? #1

Open pememoni opened 2 years ago

pememoni commented 2 years ago

I think you have assumed that K is constant; while in practice, Uniswap applies a 0.30% fee to trades, which is added to reserves. As a result, each trade actually increases k. So if we want to use this formula a) we should know the K exactly before the execution of our transaction which is not possible as we don't have control on the previous transaction even if we bribe the miner to execute it in a higher-order b) we should compute the new K after our front-running tx and affect it for the original transaction. Am I right?

yoongyy commented 2 years ago

What is K actually?

knkrth commented 1 year ago

Does fee represent network fee or Transaction fee from the mempool?

goheesheng commented 1 year ago

How much profit did you guys get?

jeanbmar commented 1 month ago

@pememoni Absolutely correct, this repo wrongly assumes the same k for the frontrun tx and the sandwiched tx. Indeed, k remains constant within a single swap only, because of the liquidity provider fees.

To verify this claim, take the following lp parameters:

rIn = 27925138973299724
rOut = 8992401273670500100800
feeFactor = 9975
feePrecision = 10000
targetAmountIn = 2000000000000000
targetMinAmountOut = 366371816369992062049

Calling worstReserves using these params will return an optimal frontrun amountIn of 8069026577219367. However, 8079399535895272 is a more optimal frontrun amountIn that satisfies the targetMinAmountOut requirement.