Tried out whole week to find an answer somewhere for this question, but, nothing to see, unfortunately,
Let suppose there are 2 USDT/WETH pairs, one on Uniswap and another one on any Uniswap v2 fork ( like Sushi )
For example, keeping it little simpler, let's remove decimals:
UniswapV2 pool has: 900 ETH and 2238300 USDT, which means price per ETH = 2487 K = 900 * 2238300 = 2014470000
Sushiswap pool has: 487 ETH and 1210195 USDT, which means price per ETH = 2485 K = 487 * 1210195 = 589364965
To calculate output amount of a predefined swap amount: Suppose I want to swap 1 eth for USDT
In Uniswap pool:
input = 1 * 997 = 997 numerator = input * 2238300 = 2231585100 denomitor = 900 * 1000 + 997 = 900997 output = numerator / denomitor = 2,476.79 USDT received for 1 ETH // Or use router getAmountsOut
In Sushiswap pool:
input = 1 * 997 = 997 numerator = input * 1210195 = 1206564415 denomitor = 487 * 1000 + 997 = 487997 output = numerator / denomitor = 2472.48 ( actually, getAmountsOut gives me 2465.06 ) - Anyway
How can i calculate the optimal input amount if there is a price discrepancy between pools in order to arb the price of these two pools? Without considering gasFees, just need simple math