Why This Matters
Most guides overlook a critical question: How should you split your tokens when joining a liquidity pool?
If you only hold Token X, becoming a liquidity provider (LP) requires two steps:
- Swap Token X for Token – This changes the pool’s reserves and token prices
- Deposit both tokens proportionally – Based on the newly updated reserves
This process introduces complexity – let’s break it down.
Before we proceed, take a deep breath. The math will get intense in the middle – like navigating a complex maze of variables. But stay with me! By the end of this derivation, everything will simplify beautifully, like reaching the clearing after a challenging hike. The reward for your patience will be crystal-clear understanding.
Step-by-Step Derivation
Initial Setup:
- x1: current reserves of Token X
- y1: current reserves of Token Y
- f: the DEX fee
Alice has xa of Token X and she wants to become a liquidity provider.
What quantities shall she allocate into Token X and Token Y?
Let’s assume that she needs to allocate:
- xs: quantity of Token X that needs to be swapped for ys of Token Y
- xns: quantity of Token X kept for LP contribution
Naturally, the initial balance of xa tokens is split between xs and xns.
Before Swap:
After Swap:
The new reserves after the swap, of course, are:
- x1 + xs: new reserves of Token X
- y1 – ys: new reserves of Token Y
LP Contribution Rule
To add liquidity, Alice’s remaining tokens must match the new reserve ratio
Substitute xns:
And now let’s solve for xs:
Cross-multiply and simplify:
Group and factor ys:
This is where it gets all crazy. Let’s introduce ys in terms of xs.
Remember the swap formula of ys:
Plugging xs into the above:
Dividing the entire equation by y1 and simplifying:
Solving the quadratic equation using the quadratic equation formula:
Negative solution is rejected, while the positive solution is:
xns can then be calculated by plugging xs into the following equation:
Approximation (For Simplicity):
Let’s try to also approximate xs.
Using Taylor Series Expansion where:
Approximating the square root term in the xs formula:
Let’s plug it into the xs formula:
xns can then be calculated by plugging xs into the following equation:
Practical Example
Consider a pool:
- QD(DAI) = 100,000 DAI
- QD(eGHST) = 10,000 eGHST
- DF = 0.3%
Alice wants to allocate 5,000 DAI into the pool. To do this optimally:
Current price of eGHST:
Exact Calculation
How much DAI should Alice swap?
Using the precise formula:
Resulting Swap & LP Allocation
- Swapped DAI for eGHST:
- Remaining DAI for LP:
New reserves after swap:
- QD(DAI)new = 100,000 + 2,473.22 = 102,473.22 DAI
- QD(eGHST)new = 10,000 – 240.65 = 9,759.35 eGHST
New eGHST price:
eGHST price change after swap:
or 5%
Approximation Method
For a quicker estimate:
Received eGHST from the swap:
Remaining DAI:
New reserves after swap:
- QD(DAI)new,approx = 100,000 + 2,503.76 = 102,503.76 DAI
- QD(eGHST)new,approx = 10,000 – 243.54 = 9,756.46 eGHST
New eGHST price:
eGHST price change after swap:
or 5.06%
Key Insight
The approximation is close (5.06% vs. 5% price impact) but not exact. For precise capital efficiency, use the full formula – especially in large swaps or low-liquidity pools.
Trade-off:
- Exact method – Maximizes LP value, but computationally heavy.
- Approximation – Good for quick estimates, but slight slippage.
This example highlights why proper modelling matters in DeFi.