Liquidity Provider Fundamentals: Optimizing Token Allocation for LP Contributions (Part 3)

You are currently viewing Liquidity Provider Fundamentals: Optimizing Token Allocation for LP Contributions (Part 3)

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:

  1. Swap Token X for Token – This changes the pool’s reserves and token prices
  2. 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.

\displaystyle x_a = x_s + x_{ns}

Before Swap:

\displaystyle P(Y)_1 = \frac{x_1}{y_1}

After Swap:

\displaystyle P(Y)_2 = \frac{x_1 + x_s}{y_1 - y_s}

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

\displaystyle \frac{x_{ns}}{y_s} = \frac{x_1 + x_s}{y_1 - y_s}

Substitute xns:

\displaystyle x_{ns} = x_a - x_s

And now let’s solve for xs:

\displaystyle \frac{x_a - x_s}{y_s} = \frac{x_1 + x_s}{y_1 - y_s}

Cross-multiply and simplify:

\displaystyle (x_a - x_s) \times (y_1 - y_s) = y_s \times (x_1 + x_s)

\displaystyle x_a y_1 - x_a y_s - x_s y_1 + x_s y_s = x_1 y_s + x_s y_s

\displaystyle y_1 - x_a y_s - x_s y_1 = x_1 y_s

Group and factor ys:

\displaystyle x_a y_1 - x_s y_1 = x_1 y_s + x_a y_s

\displaystyle x_a y_1 - x_s y_1 = y_s (x_1 + x_a)

This is where it gets all crazy. Let’s introduce ys in terms of xs.

Remember the swap formula of ys:

\displaystyle y_s = \frac{x_s \times (1 - f) \times y_1}{x_1 + (1 - f) \times x_s}

Plugging xs into the above:

\displaystyle x_a y_1 - x_s y_1 = \frac{x_s \times (1 - f) \times y_1}{x_1 + (1 - f) \times x_s} \times (x_1 + x_a)

\displaystyle (x_a y_1 - x_s y_1) \times (x_1 + (1 - f) \times x_s) = (x_s \times (1 - f) \times y_1) \times (x_1 + x_a)

\displaystyle (x_a y_1 - x_s y_1) \times (x_1 + x_s - f x_s) = (x_s y_1 - x_s y_1 f) \times (x_1 + x_a)

\displaystyle x_1 x_a y_1 + x_a x_s y_1 - x_a x_s y_1 f - x_1 x_s y_1 - x_s^2 y_1 + x_s^2 y_1 f = x_1 x_s y_1 + x_a x_s y_1 - x_1 x_s y_1 f - x_a x_s y_1 f

Dividing the entire equation by y1 and simplifying:

\displaystyle x_1 x_a - x_1 x_s - x_s^2 + x_s^2 f = x_1 x_s - x_1 x_s f

\displaystyle x_s^2 - x_s^2 f + 2x_1 x_s - x_1 x_s f - x_1 x_a = 0

Solving the quadratic equation using the quadratic equation formula:

\displaystyle x_s^2 \times (1 - f) + x_s \times x_1 \times (2 - f) - x_1 x_a = 0

\displaystyle D = (x_1 \times (2 - f))^2 + 4x_a x_1

Negative solution is rejected, while the positive solution is:

\displaystyle x_s = \frac{-x_1 \times (2 - f) + \sqrt{(x_1 \times (2 - f))^2 + 4x_a x_1 \times (1 - f)}}{2 \times (1 - f)}

xns can then be calculated by plugging xs into the following equation:

\displaystyle x_{ns} = x_a - x_s

Approximation (For Simplicity):

Let’s try to also approximate xs.

Using Taylor Series Expansion where:

\displaystyle \sqrt{p + q} \approx \sqrt{p} + \frac{q}{2\sqrt{p}}

Approximating the square root term in the xs formula:

\displaystyle \sqrt{(x_1 \times (2-f))^2 + 4x_a x_1 \times (1-f)} = \sqrt{(x_1 \times (2-f))^2} + \frac{4x_a x_1 \times (1-f)}{2\sqrt{(x_1 \times (2-f))^2}} =

\displaystyle x_1(2 - f) + \frac{4x_a x_1(1 - f)}{2x_1(2 - f)} = x_1(2 - f) + \frac{2x_a(1 - f)}{2 - f}

Let’s plug it into the xs formula:

\displaystyle x_{s,approx} = \frac{-x_1 \times (2-f) + x_1 \times (2-f) + \frac{2x_a \times (1-f)}{(2-f)}}{2 \times (1-f)} = \frac{2x_a \times (1-f)}{(2-f)} \div \frac{2 \times (1-f)}{1} =

\displaystyle \frac{2x_a \times (1 - f)}{(2 - f)} \div \frac{1}{2 \times (1 - f)} = x_a \times \frac{1}{2 - f}

xns can then be calculated by plugging xs into the following equation:

\displaystyle x_{ns,approx} = x_a - x_s = \frac{x_a}{1} - \frac{x_a}{2 - f} = \frac{(2 - f) \times x_a - x_a}{2 - f} = \frac{2x_a - f x_a - x_a}{2 - f} =

\displaystyle \frac{x_a - f x_a}{2 - f} = x_a \times \frac{1 - f}{2 - f}

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:

\displaystyle P(\text{eGHST}) = \frac{QD(\text{DAI})}{QD(\text{eGHST})} = \frac{100,\!000}{10,\!000} = 10~\text{DAI}

Exact Calculation

How much DAI should Alice swap?

Using the precise formula:

\displaystyle x_s = \frac{-QD(\text{DAI}) \times (2 - DF) + \sqrt{(QD(\text{DAI}) \times (2 - DF))^2 + 4 \times Q(\text{DAI})_a \times QD(\text{DAI}) \times (1 - DF)}}{2 \times (1 - DF)} =

\displaystyle \frac{-100,\!000 \times (2 - 0.003) + \sqrt{(100,\!000 \times (2 - 0.003))^2 + 4 \times 5,\!000 \times 100,\!000 \times (1 - 0.003)}}{2 \times (1 - 0.003)}

\displaystyle x_s = 2,\!473.22~\text{DAI}

Resulting Swap & LP Allocation

  • Swapped DAI for eGHST:

\displaystyle y_s = \frac{x_s \times (1 - DF) \times QD(\text{eGHST})}{QD(\text{DAI}) + (1 - DF) \times x_s} = \frac{2,\!473.22 \times (1 - 0.003) \times 10,\!000}{100,\!000 + (1 - 0.003) \times 2,\!473.22}

\displaystyle y_s = 240.65~\text{eGHST}

  • Remaining DAI for LP:

\displaystyle x_{ns} = Q(\text{DAI}) - x_s = 5,\!000 - 2,\!473.22 = 2,\!526.78~\text{DAI}

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:

\displaystyle P(\text{eGHST})_{\text{new}} = \frac{QD(\text{DAI})_{\text{new}}}{QD(\text{eGHST})_{\text{new}}} = \frac{102,\!473.22}{9,\!759.35} = 10.50~\text{DAI}

eGHST price change after swap:

\displaystyle \frac{P(\text{eGHST})_{\text{new}}}{P(\text{eGHST})} - 1 = \frac{10.50}{10.00} - 1 = 0.05 or 5%

Approximation Method

For a quicker estimate:

\displaystyle x_{s,approx} = Q(\text{DAI}) \times \frac{1}{2 - DF} = 5,\!000 \times \frac{1}{2 - 0.003} = 2,\!503.76~\text{DAI}

Received eGHST from the swap:

\displaystyle y_{s,approx} = \frac{x_{s,approx} \times (1 - DF) \times QD(\text{eGHST})}{QD(\text{DAI}) + (1 - DF) \times x_{s,approx}} = \frac{2,\!503.76 \times (1 - 0.003) \times 10,\!000}{100,\!000 + (1 - 0.003) \times 2,\!503.76}

\displaystyle y_{s,approx} = 243.54~\text{eGHST}

Remaining DAI:

\displaystyle x_{ns,approx} = Q(\text{DAI}) - x_{s,approx} = 5,\!000 - 2,\!503.76 = 2,\!496.24~\text{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:

\displaystyle P(\text{eGHST})_{\text{new,approx}} = \frac{QD(\text{DAI})_{\text{new,approx}}}{QD(\text{eGHST})_{\text{new,approx}}} = \frac{102,\!503.76}{9,\!756.46} = 10.51~\text{DAI}

eGHST price change after swap:

\displaystyle \frac{P(\text{eGHST})_{\text{new,approx}}}{P(\text{eGHST})} - 1 = \frac{10.51}{10.00} - 1 = 0.0506 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.