### 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:

We then want to take the derivative of this graph to get the maximum return:

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:

where 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:

And now we can finally derive this monster of an equation:

Finally, we multiply the `x + y = C` pool by , leaving us with

Our equation is now:

And to make things even better, we can substitute with

Our FINAL formula (I promise) that must remain equal between adding to the pool and taking from the pool becomes:

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