Liquidity Provider Fundamentals: Uniswap’s DEX Fee Formula Finally Derived (Part 9)

You are currently viewing Liquidity Provider Fundamentals: Uniswap’s DEX Fee Formula Finally Derived (Part 9)

The Missing Trillion-Dollar Math

Uniswap has processed nearly $3 trillion in trading volume since its launch. With a 0.3% fee, this translates to roughly $9 billion in cumulative fees for liquidity providers and the protocol.

Figure 1. Uniswap Cumulative Trading Volume Since Inception.

Yet, despite these staggering numbers, nobody has publicly derived the formula behind Uniswap’s fee calculations – until now.

The Mysterious Formula in Uniswap’s White Paper

Uniswap’s whitepaper references the following formula for total fees collected by liquidity providers:

\displaystyle f_{1,2} = 1 - \frac{\sqrt{k_1}}{\sqrt{k_2}}

But where does this come from? Surprisingly, there’s zero public derivation – no academic papers, no StackExchange explanations, and not even AI tools (ChatGPT, Grok, DeepSeek) could crack it.

Figure 2. StackExchange Forum Request.

Does Uniswap Actually Use This Formula?

Unlike many “Web2.5” projects that overpromise and underdeliver, Uniswap’s code matches its whitepaper. A deep dive into UniswapV2Pair.sol confirms the formula is implemented as described.

Figure 3. UniswapV2Pair.sol Smart Contract.

The First-Ever Public Derivation of Uniswap’s DEX Fees

Key Insight: Fees = IL(with fees) – IL(no fees)

We start with the assumption that cumulative DEX fees can be derived from the difference between:

  • Impermanent Loss (IL) with fees
  • IL without fees

Mathematically:

\displaystyle \text{DEX Fees}_{\text{Total}} = \text{IL}_{\text{(with fee)}} - \text{IL}_{\text{(no fee)}}

Recap: Impermanent Loss Formulas

From previous articles, we know:

IL with fees:

\displaystyle \text{IL}_{\text{(with fee)}} \% = \sqrt{\frac{k_2}{k_1}} \times \frac{2\sqrt{d}}{d + 1} - 1

IL without fees:

\displaystyle \text{IL}_{\text{(no fee)}} \% = \frac{2\sqrt{d}}{d + 1} - 1

Where:

\displaystyle d = \frac{P(X)_2}{P(X)_1} = \frac{y_2/x_2}{y_1/x_1} = \frac{y_2}{x_2} \times \frac{x_1}{y_1} = \frac{x_1 y_2}{x_2 y_1} (price change ratio)

\displaystyle k_1 = x_1 y_1 \quad \text{and} \quad k_2 = x_2 y_2 (pool constants before/after swap)

Step-by-Step Derivation

Express IL in absolute terms (not %):

\displaystyle IL_{\text{(no fee)}} = IL\%_{\text{(no fee)}} \times \text{Value(LP)}_{\text{HODL}} = \left( \frac{2\sqrt{d}}{d+1} - 1 \right) \times \text{Value(LP)}_{\text{HODL}}

\displaystyle IL_{\text{(with fee)}} = IL\%_{\text{(with fee)}} \times \text{Value(LP)}_{\text{HODL}} = \left( \sqrt{\frac{k_2}{k_1}} \times \frac{2\sqrt{d}}{d+1} - 1 \right) \times \text{Value(LP)}_{\text{HODL}}

Subtract to isolate DEX fees:

\displaystyle \text{DEX Fees}_{\text{Total}} = IL_{\text{(DEX fees)}} - IL_{\text{(No DEX fees)}} =

\displaystyle \left( \sqrt{\frac{k_2}{k_1}} \times \frac{2\sqrt{d}}{d+1} - 1 \right) \times \text{Value(LP)}_{\text{HODL}} - \left( \frac{2\sqrt{d}}{d+1} - 1 \right) \times \text{Value(LP)}_{\text{HODL}} =

= \left[ \left( \sqrt{\frac{k_2}{k_1}} \times \frac{2\sqrt{d}}{d+1} - 1 \right) - \left( \frac{2\sqrt{d}}{d+1} - 1 \right) \right] \times \text{Value(LP)}_{\text{HODL}} =

\displaystyle \left( \sqrt{\frac{k_2}{k_1}} \times \frac{2\sqrt{d}}{d+1} - 1 - \frac{2\sqrt{d}}{d+1} + 1 \right) \times \text{Value(LP)}_{\text{HODL}} =

\displaystyle \left( \sqrt{\frac{k_2}{k_1}} \times \frac{2\sqrt{d}}{d+1} - \frac{2\sqrt{d}}{d+1} \right) \times \text{Value(LP)}_{\text{HODL}} =

\displaystyle \frac{2\sqrt{d}}{d+1} \times \left( \sqrt{\frac{k_2}{k_1}} - 1 \right) \times \text{Value(LP)}_{\text{HODL}}

Simplify Value(LP)HODL:

\displaystyle \text{Value(LP)}_{\text{HODL}} = x_1 \times \frac{y_2}{x_2} + y_1 = x_1 \times \frac{y_2}{x_2} + y_1 \times \frac{x_2}{x_2} = \frac{x_1 y_2 + x_2 y_1}{x_2}

Integrating with d:

\displaystyle d = \frac{x_1 y_2}{x_2 y_1} \implies x_1 y_2 = d \times x_2 y_1

And plugging in x1y2 into the Value(LP)HODL:

\displaystyle \text{Value(LP)}_{\text{HODL}} = \frac{x_1 y_2 + x_2 y_1}{x_2} = \frac{d \times x_2 y_1 + x_2 y_1}{x_2} =

\displaystyle \frac{x_2 y_1 \times (d + 1)}{x_2} = y_1 \times (d + 1)

Substitute back:

\displaystyle \text{DEX Fees}_{\text{Total}} = \frac{2\sqrt{d}}{d + 1} \times \left(\sqrt{\frac{k_2}{k_1}} - 1\right) \times \text{Value(LP)}_{\text{HODL}} =

\displaystyle \frac{2\sqrt{d}}{d + 1} \times \left(\sqrt{\frac{k_2}{k_1}} - 1\right) \times y_1 \times (d + 1) = 2y_1 \sqrt{d} \times \left(\sqrt{\frac{k_2}{k_1}} - 1\right)

Revisiting d:

\displaystyle x_1 y_2 = d \times x_2 y_1

Multiply both sides by y1y2:

\displaystyle x_1 y_2 \times y_1 y_2 = d \times x_2 y_1 \times y_1 y_2

\displaystyle x_1 y_1 y_2^2 = d \times x_2 y_2 y_1^2

Where:

\displaystyle k_1 = x_1 y_1

\displaystyle k_2 = x_2 y_2

Plugging both k’s:

\displaystyle k_1 \times y_2^2 = d \times k_2 \times y_1^2

Solving for d:

\displaystyle d = \frac{k_1 y_2^2}{k_2 y_1^2}

Squaring both sides:

\displaystyle \sqrt{d} = \sqrt{\frac{k_1 y_2^2}{k_2 y_1^2}} = \sqrt{\frac{k_1}{k_2}} \times \frac{y_2}{y_1}

Plugging in  back into Total DEX Fees:

\displaystyle \text{DEX Fees}_{\text{Total}} = 2y_1 \sqrt{d} \times \left(\sqrt{\frac{k_2}{k_1}} - 1\right) = 2y_1 \times \sqrt{\frac{k_1}{k_2}} \times \frac{y_2}{y_1} \times \left(\sqrt{\frac{k_2}{k_1}} - 1\right) =

\displaystyle 2y_2 \times \sqrt{\frac{k_1}{k_2}} \times \left(\sqrt{\frac{k_2}{k_1}} - 1\right) = 2y_2 \times \left(1 - \sqrt{\frac{k_1}{k_2}}\right)

2y2 is the value of the LP after swap in terms of Token Y:

\displaystyle \text{Value(LP)}_2 = x_2 \times P(X)_2 + y_2 \times P(Y)_2 = x_2 \times \frac{y_2}{x_2} + y_2 \times 1 = y_2 + y_2 = 2y_2

Express as a % of LP value after swap (2y2):

\displaystyle \text{DEX Fees \%}_{\text{Total}} = \frac{\text{DEX Fees}_{\text{Total}}}{\text{Value(LP)}_2} = \frac{2y_2 \times \left(1 - \sqrt{\frac{k_1}{k_2}}\right)}{2y_2} = 1 - \sqrt{\frac{k_1}{k_2}}

This matches Uniswap’s formula exactly!

Why This Matters

  • First public proof of Uniswap’s fee model.
  • Confirms that fees offset Impermanent Loss.
  • Enables better LP strategies by quantifying fee impact.

Stay tuned for Part 9, where we apply this in real-world scenarios!

Final Note: This is the first and only complete derivation of Uniswap’s DEX fee formula. Bookmark it – you won’t find this anywhere else. 🚀