Calculating LP Shares for Single Asset Joins
Single Asset Join LP Shares
Photo by Kanchanara on Unsplash
Manual Swap + Join
Let’s assume there is a 50/50 weighted X-Y pool. A user wishes to join the pool with x_in. Some portion of x_in will be swapped into Y for a balanced multi-asset join.
Then x_k and y_s are LPed into the pool. We must find the value of x_s such that
Since l_y, l_x, and x_in are known quantities, this gives us a linear relationship between y_s and x_k. Furthermore, we have the constant product invariant (including swap fees) from earlier. Given these two constraints on y_s, we can solve for x_s (the solution to x_s and y_s is the intersection of the linear equation and the hyperbola, where x_s, y_s > 0). Substituting y_s into the earlier equation, we have:
Which is a quadratic equation with the constants:
And we take the positive solution for x_s because the negative one doesn’t make physical sense.
Example
Let’s take the ATOM/OSMO pool from Osmosis Pool #1, which has 3,256,428 ATOM and 31,278,086 OSMO. We’ll take X as ATOM and Y as OSMO. Let’s say we have 1,000,000 ATOM we’d like to LP with. How much should we swap into OSMO before adding both assets to the pool?
Taking our equation from above, we have
We’ve algebraically calculated the amount of X the user needs to swap to perform a maximally balanced multi-asset join!
Final Pool Balances
After the swap, the pool balances are
And the user has x_k and y_s in their wallet. Once they join with those, the final pool balances become.
So even after doing the swap + join, we end up as though the user had just dumped all their x_in into the pool! Except there is a swap fee we need to account for, which Balancer’s single-asset join formula doesn’t account for.
Virtual Join
Photo by Luke Chesser on Unsplash
Instead of having the user perform two transactions (swap then join), what if we let the user just provide all their x_in in one transaction, and we calculate the exact amount of LP shares to mint, accounting for swap fees?
Returning to Balancer’s LP share formula, we know that the amount of LP shares minted is proportional to the increase in the value function. For a 50–50 weighted pool, this becomes
where k_postswap is invariant after the user swaps x_s into y_s, and k_final is the final invariant after LPing x_s and y_s. Note that k_postswap is not equal to the initial invariant since a portion of x_s is taken as a swap fee and added to the pool, increasing the invariant.
If f = 0 (i.e., no swap fees), then it becomes exactly the same as Balancer’s formula.
Going back to our ATOM-OSMO example,
Which is the same join ratio that we got in the manual swap + join case!