Uniswap v3 Liquidity Formula Explained

·

Uniswap v3 introduced a groundbreaking concept known as concentrated liquidity, allowing liquidity providers to allocate capital within specific price ranges. This innovation significantly improves capital efficiency but also introduces new mathematical complexities. Central to this system is a key formula from the official whitepaper used to compute the real token reserves in a position. This article breaks down the derivation of this formula and the conceptual models that underpin it.

What Is Concentrated Liquidity?

In traditional Automated Market Makers (AMMs) like Uniswap v2, liquidity is distributed uniformly along the entire price curve from zero to infinity. Uniswap v3 changes this by allowing liquidity to be concentrated within a custom price range chosen by the user.

This means your funds are only used for trades that occur within your specified price bracket. While this leads to higher fee-earning potential on your deployed capital, it requires a more sophisticated mathematical framework to track the real amounts of each token in a position at any given time.

Two Coordinate Systems for Understanding Liquidity

To fully grasp how concentrated liquidity works, it is helpful to view a liquidity position through two different lenses or coordinate systems.

1. The Liquidity/Price Coordinate System

In this model, the x-axis represents the price (often using its logarithm for technical scaling reasons), and the y-axis represents the amount of liquidity available at that price.

This view is particularly useful for understanding concepts like slippage and how trading fees are distributed proportionally among all positions in the pool.

2. The Token Amounts Coordinate System

Here, the x-axis and y-axis represent the reserves of token X and token Y in the pool, respectively.

This perspective is essential for seeing the exact quantities of each token that make up a specific liquidity position, which is necessary when you want to create or withdraw that position.

The key insight is that a single liquidity position can be cleanly represented in either system, and the formula we are deriving allows for translation between them.

Deriving the Real Reserves Formula

The core challenge is moving from the "virtual" reserves that define the shape of the liquidity curve to the "real" reserves that represent the actual tokens in your position. The process involves a strategic shift of the curve segment.

The Concept of Virtual and Real Reserves

In the token amounts coordinate system, a Uniswap v3 position is a segment of a hyperbola between two points, A and B.

The formula from the Uniswap v3 whitepaper (Eq. 2.2) calculates the real reserves ((x, y)) based on the current price (P), the liquidity (L) of the position, and the price range boundaries ([P_a, P_b]):

[
x = \begin{cases}
L \cdot \left( \frac{1}{\sqrt{P}} - \frac{1}{\sqrt{P_b}} \right) & \text{if } P < P_b \
0 & \text{if } P \geq P_b
\end{cases}
]
[
y = \begin{cases}
L \cdot \left( \sqrt{P} - \sqrt{P_a} \right) & \text{if } P > P_a \
0 & \text{if } P \leq P_a
\end{cases}
]

Step-by-Step Derivation

  1. Define the Virtual Curve: The fundamental equation relating virtual reserves (x_{virtual}) and (y_{virtual}) to liquidity (L) is:
    [
    (x_{virtual} + x_{offset})(y_{virtual} + y_{offset}) = L^2
    ]
    The offsets ((x_{offset}), (y_{offset})) are the key to converting virtual reserves to real reserves: (x_{real} = x_{virtual}), (y_{real} = y_{virtual}).
  2. Find the Offsets: The offsets are determined by the endpoints of your chosen price range. The price at any point is given by (P = y / x). Using the liquidity formula and the prices at the boundaries ((P_a), (P_b)), we can find the coordinates of points A and B in the virtual system.

    • The x-coordinate of point B (where (y_{real}=0)) is (x_{offset} = L / \sqrt{P_b}).
    • The y-coordinate of point A (where (x_{real}=0)) is (y_{offset} = L \cdot \sqrt{P_a}).
  3. Plug into the Main Equation: Substitute these offset values back into the main virtual curve equation:
    [
    (x_{real} + \frac{L}{\sqrt{P_b}})(y_{real} + L \cdot \sqrt{P_a}) = L^2
    ]
    Solving this equation for the real reserves (x_{real}) and (y_{real}) at a given price (P) (where (P = y_{real}/x_{real})) yields the final formula (Eq. 2.2) shown above.

This derivation shows that the seemingly complex formula is essentially the result of applying a precise geometric shift to a hyperbola to isolate a specific segment, defined by the provider's chosen price range. For those looking to dive even deeper into the mechanics and implications of this design, 👉 explore more advanced AMM strategies.

Frequently Asked Questions

What is the main advantage of concentrated liquidity?
Concentrated liquidity allows liquidity providers to allocate their capital with surgical precision within price ranges they believe will see high trading activity. This maximizes the utility of their funds, leading to higher potential fee earnings compared to providing liquidity across the entire price spectrum.

How do I choose the best price range for my Uniswap v3 position?
The optimal price range depends on your market outlook and risk tolerance. A narrow range around the current price offers the highest fee-earning potential but requires frequent active management to avoid capital becoming inactive if the price moves out of the range. A wider range requires less management but earns fees on a smaller portion of your capital.

What happens if the market price moves outside my set range?
If the market price exits your specified price range ([P_a, P_b]), your position effectively stops earning fees. Your liquidity is converted entirely into one of the two tokens (the one that is now "out-of-the-money"). You must either withdraw your position or adjust its range to重新开始earning fees.

Is providing concentrated liquidity on Uniswap v3 more risky than v2?
It carries a different risk profile. While impermanent loss still exists, the primary new risk is "liquidity divestment" or "LP rot," where your capital becomes inactive if the price moves away. This requires a more active management strategy compared to the passive "set-and-forget" approach of v2.

Can the real reserves of a position be negative?
No, the real reserves (x) and (y) calculated by the formula are always zero or positive. The cases where the value is zero indicate that the price is outside the position's active range, and the position holds only one type of token.

Does this formula apply to all AMMs that use concentrated liquidity?
While the core mathematical principles are similar, this specific formula and its derivation are for the Uniswap v3 constant product formula. Other AMMs implementing concentrated liquidity might use different bonding curves (e.g., stable swap curves), which would require a different set of equations.