# Uniswap V2 calculate quantity tradable at target execution price in Solidity?

Let's say the instantaneous price of `ETH/USDT` pair is 2000.

If I trade a lot of ETH for USDT, there could be a lot of slippage, and my trade might fulfill at an average execution price of 1950 USDT per ETH.

Let's say I'm ok with trading at 1950 USDT. How can I calculate the max amount of ETH I can trade at this average execution price?

How can I calculate this in either direction, either from ETH to USDT or vice versa, in Solidity, using the Uniswap V2 interface?

TL;DR - Final formula is at the end. It would be great if someone can verify that I didn't make any mistakes.

If we exchange token `x` for token `y`, as per the constant product formula:

``````x * y = k
``````

Let `a` be the amount of `x` we are exchanging to get `b` amount of `y`. Therefore:

``````(x + a) * (y - b) = k
``````

The execution price of the trade, by definition, is just `b / a`.

If our target execution price is `e`, then `b / a = e` => `b = ea`

Therefore:

``````(x + a) * (y - ea) = k
``````

But `x * y` is also equal to `k`, therefore:

``````x * y = (x + a) * (y - ea)
``````

Now we can just rewrite the equation to get `a` in terms of the other variables.

``````x * y = x * (y - ea) + a * (y - ea)
xy = xy - eax + ay - ea^2
- eax + ay - ea^2 = 0
-ea^2 + a(y - ex) = 0
``````

We know `a` is not zero, else the price would be undefined which is not possible. Because `a` is not zero, we can safely divide across by `a`:

``````-ea + y - ex = 0
``````

Now a few more slight adjustments:

``````ea = y - ex
a = (y - ex) / e
a = (y / e) - x
``````

So this is the final formula:

``````a = (y / e) - x
``````

where `a` is the maximum amount we can trade to get an execution price of `e` or better, and `x` and `y` are the number of input and output tokens in the pool before the trade respectively.

TEST CASE - where 1 ETH is worth 2,000 USD

``````x = 100 ETH
y = 200,000 USD
e = 1,950

a = (y / e) - x
= (200,000 / 1,950) - 100
= 2.564 ETH
``````
• to have a better result, we should also consider the swap fee Jun 8, 2022 at 9:51