flashbots / simple-blind-arbitrage

MIT License
350 stars 92 forks source link

what is the math formula behind getAmountIn ? #3

Open nzhl opened 1 year ago

nzhl commented 1 year ago

I am struggling to figure out how can we deduce the formula used in getAmountIn. Basically here if we want to maximize our profit those two pools must have almost the same price at final stage. Hence we have:

(x1 + delta_x1) / (y1 - delta_y1) = (x2 - delta_x2) / (y2 + delta_y1)

Also we know how the pair pool is updating accordingly after swap:

(x1 + 0.997 * delta_x1) (y1 - delta_y1) = x1y1
(x2 - delta_x2)(y2 + 0.997 * delta_y1) = x2y2

Based on equations mentioned above, i just have no clue how can i simplify it to the formula in the code. Do i miss anything here ?

https://github.com/flashbots/simple-blind-arbitrage/blob/62582059a26f3852949e71a49821ee3c613f2f27/src/blindBackrun.sol#L83

bertmiller commented 1 year ago

See section 2.1 of this paper. Then it's a matter of calculus!

https://arxiv.org/pdf/1911.03380.pdf

githuber1001 commented 1 year ago

When listening to the Flashbots MEV-Share Matchmaker on the Goerli network this transaction was returned, 0xe678b8a63274d0a14c5b9438997157d5212cfaddb79151effd36afa291e8e9e6. This arbitrage appears to be losing WETH. Is the arbitrage formula reversed or am I just reading this wrong (more likely scenario)?

WXF-WangXuFeng commented 1 year ago

See section 2.1 of this paper. Then it's a matter of calculus!

https://arxiv.org/pdf/1911.03380.pdf

Hey handsome, I understand calculus, but I've looked through the link you gave me three times and still can't find the derivation process of the calculation formula in the BlindBackrun contract. This article is about the arbitrage process from the Uniswap market to a related market with infinite liquidity, while the calculation formula in your BlindBackrun contract refers to two identical Uniswap markets, such as the arbitrage between the currency pairs of Uniswap and Sushiswap. Would you be able to update an article showing how to derive the calculation formula inside the GatAmountIn method? This would be very helpful for us to research new arbitrage methods. Thank you very much!