1

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

1 Answer 1

3

Short answer

Plug these numbers in to calculate r, where

  • r = optimal amount in
  • x_a = reserve out of AMM A
  • y_a = reserve in of AMM A
  • x_b = reserve in of AMM B
  • y_b = reserve out of AMM B
  • f = fee (0.03%)
k = (1-f)*x_b + (1-f)**2*x_a
a = k**2
b = 2*k*y_a*x_b
c = (y_a*x_b)**2 - (1-f)**2*x_a*y_b*y_a*x_b

r = (-b + sqrt(b**2 - 4*a*c)) / (2*a)

Long answer

You need to find a value dy_0 for the function f,

f(dy_0) = dy_1 - dy_0

such that f(dy_0) is the maximum of the function

where dy_0 is the input amount of token and dy_1 is the amount of same token you receive after arbitrage.

If we know dy_0, we can calculate amount out (dx)

dx = dy_0 * (1 - f) * x_a / (y_a + dy_0 * (1 - f))

And, if we know dx, we can calculate dy_1 (amount out from second swap)

dy_1 = dx * (1 - f) * y_b / (x_b + dx * (1 - f))

Now working backwards, re-write f using the 2 equations above, we get a complicated function in terms of dy_0.

To find the maximum of f, take the derivative and then find where the derivative = 0. Finally use the quadratic equation, (-b + sqrt(b**2 - 4ac)) / (2a) to find where the derivate = 0.

Here is the graph of f in red, green is derivative of f and blue is the maximum of f, the optimal input amount assuming gas fee is 0.

enter image description here

https://www.desmos.com/calculator/gjujz3ayta

2
  • That's very useful, thanks you, but could you maybe modify the answer to take into account different fee pairs? I am not sure where to put f1 or f2 in the case fees are different.
    – flo
    Commented Aug 2 at 15:42
  • 1
    if f1 and f2 are close to each other set f = max(f1, f2) and it will give you an approximate answer Commented Sep 3 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.