Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
xyk math to optimize join
Introduction
Let say I have 20 ATOM and 10 USDC and I want to join a ATOM-USDC xyk pool with these funds. Considering an even weighted pool, and the current price of $14.31 USDC per ATOM, I would need to first swap some ATOM to get more USDC in order to get the maximum amount of tokens.
Most of the protocols are currently defining the amount of token you can get using a binary search method, but math can give us an algaebric solution to this, and save some iterations for our precious nodes.
Math notation
We will write$x$ and $y$ the respective amount of ATOM and USDC tokens we want to use to join a price. We will also note $l_{x}$ and $l_{y}$ the liquidity of the pool in both denomination.
We will also consider$f$ the swapping fee of the protocol, and $k$ our invariant such as $k=l_{x}\times l_{y}$
Swap
In our case, we will first swap a quantity$x'$ of ATOM tokens to get $y'$ USDC and then join our pool.
Using our invariant$xy=k$ to our problem and applying fees, we get:
Which means that we know how much tokens we get our of our swap based on the tokens given in.
Using this, we then need to define the value of$y + y'$ in relation to our $x - x'$ of ATOM that we will have after the swap.
Join the pool
The logic to join a pool for a n token$x_1x_2...x_n=k$ pool with no weight is to find minimum amount of share of the pool we can get from $x$ token and $y$ token.
Meaning:
Which means that in order to have the highest possible share of the pool, we need to have all$s_i$ equals. Otherwise,
This means that we have $x'i$ amount of crypto that can be swapped for other denom to have a bigger $s{out}$.
Back to our problem, if we have our wallet with$y+y'$ USDC and $x-x'$ ATOM, we can maximise our share by satisfying the equality:
The liquidity of the pool was changed by the initial swap, which are represented by$l_y'$ and $l_x'$ .
We can compute their values as:
By combining$\mbox{(1), (2) and (3)}$ , we get:
Because of the term in$f$ , solving this means solving the root of a heavy quadratic formula:
For the sanity of the reader, we will compute the solution for a pool where the swap fee$f=0$ .
$(4)$ becomes:
$$\frac{\left(y+l_y\right)\left(l_x+x'\right)^2}{k}-\left(l_x+x'\right)=x-x'\\ \frac{\left(y+l_y\right)\left(l_x+x'\right)^2}{k}-l_x=x\\ \frac{\left(y+l_y\right)\left(l_x+x'\right)^2k}{k}=xk+l_xk;\quad \:k\ne \:0\\ \left(l_x+x'\right)^2=\frac{xk+kl_x}{y+l_y};\quad \:k\ne \:0,\:y+x'_y\ne \:0\\$$Our solutions are then:
Of course, since we want a positive$x'$ , our final solution will be the first term.
Practical example
Let's say Alice want to join the ATOM-NUSD pool in Nibiru with:
The individual shares without swapping would be$s_{atom}=2%;s_{nusd}=0.6%$ . Which means we would join the pool with 3,000 NUSD and 210 ATOM ($0.6% \times 35000$ ).
This means that we will need to swap some ATOM to get more NUSD.
We have:
Let's simulate this transaction to showcase the result: