Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what is the math formula behind getAmountIn ? #3

Open
nzhl opened this issue May 3, 2023 · 3 comments
Open

what is the math formula behind getAmountIn ? #3

nzhl opened this issue May 3, 2023 · 3 comments

Comments

@nzhl
Copy link

nzhl commented May 3, 2023

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 ?

function getAmountIn(PairReserves memory firstPairData, PairReserves memory secondPairData) internal pure returns (uint256) {

@bertmiller
Copy link
Contributor

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

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

@githuber1001
Copy link

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
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants