Problem explained

Suppose we have two pools in which we are able to add to one pool and withdraw from another one.

Pool x y
Size 50 50

e.g., let us add 10 to pool x, and withdraw a known amount from pool y.
The optimal amount is the amount that allows us to equalize two different pools such that x and y are in an equal ratio.

x y
Pool 1 150 125
Pool 2 50 50

in this instance, we can add token x into pool 2, take out some token y, and add the token y into pool 1, then withdraw token x again, making the ratios more equal

Math involved

Linear invariant equation (x + y = C)

To begin with, we have a linear invariant equation `x + y = C`

• x and y refer to the quantities of the items in the pool
• C refers to the invariant which does not change

When we graph this, we will get a straight line which trends down: view desmos
The idea here is that when we add x and take out y from the pool, the sum must still be equal to C.
The problem with this equation is that there is no consequence from deviating from the original x/y ratio. The cost is constant.

Constant product equation (x * y = k)

In order to make the x/y ratio more stable, we can make it gradually more costly to add to `x` and take away from `y`. We do this with the equation `x * y = k`

• `x` and `y` refer to the quantities of the items in the pool
• `k` refers to invariant (which again, does not change)

when we graph this as `y = k/x`, we get a hyperbola: view desmos
As we add to `x` and take away from `y`, it becomes much more costly to move the pool away from the ideal `x/y` ratio.

Putting the equations together

So we have two problems:

• `x + y = C` does not stop us from adding lots to `x`, and taking lots from `y` so that we run out of `x`
• `x * y = k` suffers from being very expensive to bring the pool out of balance

The solution is to combine both formulas. We compromise by using the `x + y = C` equation while the pool is “pretty balanced”, and once it goes out of balance we switch back to the `x * y = k` equation.
We therefore make the equation `(x + y) + (x * y) = C + k`.
Let `D` be the quantities `x + y`, we therefore have the general equation `(x + y) + (x * y) = D + (D/2)^2`: view desmos

Which can be rewritten in terms of y:
\large \begin{align*} x+y + x\cdot y &= D + (\frac{D}{2})^2\\ x + y + x \cdot y &= D + \frac{D^2}{2^2}\\ y+x\cdot y &= D+D^2\cdot2^{-2}-x\\ y(1+x)&=...\\ y&=\frac{D+D^2\cdot2^{-2}-x}{1+x}\\\\ y&=(1+x)^{-1}(D+D^2\cdot2^{-2}-x) \end{align*}

We then want to take the derivative of this graph to get the maximum return:
\large \begin{align*} y&=(1+x)^{-1}(D+D^2\cdot2^{-2}-x)\\\\ y'&=(1+x)^{-1}(0+0-1)+(D+D^{2}\cdot2^{-2}-x)(-1(1+x)^{-2})\\ &=-1(1+x)^{-1}+-(D+D^{2}\cdot2^{-2}-x)(1+x)^{-2}\\ &=-(1+x)^{-1}[1+(D+D^{2}\cdot2^{-2}-x)(1+x)^{-1}]\\ \end{align*}

Let’s take a step back and look at the general equation we just made. It looks very similar to the `x * y = k` graph, because the sum invariant part (the `x + y = C` part) is not amplified. We can then amplify this by a given factor in order to make it more significant, let us use the new general equation:
\large \begin{align*} \chi(x+y)+(x\cdot y)=\chi D + (\frac{D}{2})^2 \end{align*}
where $\large\chi$ is the amplitude. As we increase the amplitude the curve becomes more linear - we do this when the pool is relatively equal.

Solving for y once again yields:
\large \begin{align*} \chi(x+y)+(x\cdot y)&=\chi D + (\frac{D}{2})^2\\ \chi x + \chi y + x\cdot y &= \chi D + (\frac{D}{2})^2\\ \chi y + x\cdot y &= \chi D + (\frac{D}{2})^2-\chi x\\ y(\chi + x) &= \chi D + (\frac{D}{2})^2 - \chi x\\\\ y &= (\chi D + (\frac{D}{2})^2 - \chi x)(\chi + x)^{-1}\\ \end{align*}

And now we can finally derive this monster of an equation:
\large \begin{align*} y &= (\chi D + (\frac{D}{2})^2 - \chi x)(\chi + x)^{-1}\\\\ y' &= (\chi D + D^2\cdot{2}^{-2}-\chi x)(-1)(\chi + x)^{-2} + (\chi + x)^{-1}(0 + 0 - \chi)\\ &=-(\chi + x)^{-2}(\chi D + D^2 \cdot 2^{-2} - \chi x) - \chi(\chi + x)^{-1}\\ &=-(\chi + x)^{-1}[\chi + (\chi D + D^2\cdot 2^{-2} - \chi x)(\chi + x)^{-1}] \end{align*}

Finally, we multiply the `x + y = C` pool by $\large\chi$, leaving us with
$\large D\chi(x+y)=D^2$
Our equation is now:
$\large D\chi(x+y)+(x\cdot y)=\chi D^2 + \frac{D^2}{4}$

And to make things even better, we can substitute $\large\chi$ with
$\large \frac{Ax \cdot y}{(\frac{D}{2})^2}$
Our FINAL formula (I promise) that must remain equal between adding to the pool and taking from the pool becomes:
$\large 4A(x+y)+D=4AD+\frac{D^3}{4xy}$

The final piece of information to include is that there is a 0.05% fee (fee = amt * `0.0005`) taken from the final amount of y withdrawn.

The complete problem becomes, what is the optimal amount of x to add to the pool so that we can take out y such that it is the lowest price (y/x) possible?

• using inputs x amount added, size of x, size of y, A and D