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 question becomes - what is the optimal amount to add to pool x to get the best gain for 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

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

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:

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?