[DeFi Math] Uniswap V3 Concentrated Liquidity

Chaisomsri
10 min readFeb 5, 2024

This post introduces the DeFi Series on the core update of Uniswap V3: Concentrated Liquidity.

The previous version of Uniswap implemented a trading structure using the AMM (Automated Market Maker) model with a CPMM (Constant Product Market Maker) function, forming a marketplace where two cryptocurrencies could be traded automatically.

However, the CPMM function resulted in a difference between the intended price by the buyer and the actual transaction price, leading to losses for the buyer. This phenomenon is known as slippage, and it was especially severe in smaller liquidity pools.

This issue was referred to as Lazy Liquidity in Uniswap V2.

Uniswap V3 decided to address this chronic problem by introducing a change in the liquidity provision method, called Concentrated Liquidity, to solve the issue.

1. Uniswap V3 Concentrated Liquidity

Uniswap V2
Uniswap V3

The formula in Uniswap V3 has changed from the original formula in V2 as shown above. Notably, you can see the new variables Pa (minimum) and Pb (maximum). Concentrated liquidity has been updated to allow liquidity providers to set the maximum and minimum amounts of liquidity they wish to supply.

From now on, we will mathematically understand the Concentrated Liquidity CPMM as described in the Uniswap V3 Whitepaper, with examples.

Example

A liquidity provider has now formed an MTK/USDC liquidity pool.

  • X: Current supply of MTK in the MTK/USDC liquidity pool
  • Y: Current supply of USDC in the MTK/USDC liquidity pool
  • L²: Liquidity (constant) of the MTK/USDC liquidity pool
  • P: Price of the base token MTK
AMM CPMM

According to the principles and rules of AMM’s CPMM, the liquidity (L²) of the pool, which is the product of the supply of the two tokens provided to the liquidity pool, is constant. This variable remains fixed while trades are executed and is a determinant in deciding the amount that is settled in a trade.

Additionally, when basing on the MTK (My Token) token, the price (P) of MTK is determined by Y/X, and when liquidity is added or removed, it is a determinant in deciding the ratio of the two tokens.

The biggest drawback of CPMM, high slippage, was a formula that users had to bear the loss. The reason for the occurrence of such a problem is mathematically due to the lack of a price range (Range). In other words, the token price does not become 0 but can infinitely increase and infinitely decrease.

Uniswap V3 CPMM
  • Pa: Minimum price of MTK provided to the MTK/USDC liquidity pool (base token MTK)
  • Pb: Maximum price of MTK provided to the MTK/USDC liquidity pool (base token MTK)

To address these drawbacks, Uniswap V3’s concentrated liquidity was updated to allow liquidity providers to set their own price range within the principles and rules of CPMM without deviating from them. Now, liquidity providers can set the price range within [0, ∞].

The important aspect of transitioning from V2 to V3 is that updates must be made without deviating from the original principles and rules of the CPMM algorithm.

Otherwise, the value of liquidity that determines the trading volume and price of the two tokens would change, potentially affecting the cryptocurrency market prices irrationally.

  • Xr​: Actual total supply of MTK in the MTK/USDC liquidity pool
  • Yr​: Actual total supply of USDC in the MTK/USDC liquidity pool
  • Xv​: Virtual total supply of MTK in the MTK/USDC liquidity pool
  • Yv​: Virtual total supply of USDC in the MTK/USDC liquidity pool

Therefore, Uniswap V3 introduces virtual and actual supply amounts into CPMM, allowing liquidity providers to set the price range [0, ∞] without affecting the outcomes of the existing CPMM.

In conclusion, a mathematical algorithm that calculates the virtual supply amount using the formula above, and then calculates the actual supply amount, was introduced.

The mathematical proof of concentrated liquidity will be explained through the description of the Curve of Virtual Reserve and the Curve of Real Reserve for understanding, and we will look into it in the final part.

2. Curve of Virtual Reserve & Curve of Real Reserve

Example

  • X: Current supply of MTK in the MTK/USDC liquidity pool
  • Y: Current supply of USDC in the MTK/USDC liquidity pool
  • Pa​​: Minimum price of MTK offered in the MTK/USDC liquidity pool (base token MTK)
  • Pb​: Maximum price of MTK offered in the MTK/USDC liquidity pool (base token MTK)

As in the previous example, we intend to additionally provide liquidity within the price range [Pa​,Pb​] to the current MTK/USDC liquidity pool.

At this time, liquidity providers must add the actual supply amount calculated according to the predetermined price range to the liquidity.

To calculate this, Uniswap V3 computes virtual liquidity under the assumption that the current price of the token in the MTK/USDC liquidity pool has reached the price range [Pa​,Pb​].

Let’s assume the price of the MTK token in the current MTK/USDC liquidity pool has reached Pa​ and Pb​.

If MTK (X) tokens are added to the liquidity pool, and USDC (Y) tokens decrease, the price will drop from the original price P to Pa​.

If MTK (X) tokens decrease in the liquidity pool, and USDC (Y) tokens are added, the price will rise from the original price P to Pb​.

The focus here is on the decreasing token.

When a user trades in the liquidity pool, they add their tokens to the liquidity pool, and the tokens settled according to the CPMM formula are provided to the user.

The liquidity provider is the person supplying the tokens to the user. That is, the actual supply amount is equivalent to the amount of tokens provided to users trading within the price range [Pa​,Pb​].

Uniswap V3 does not provide liquidity for trades outside the price range set by the liquidity provider. This means that the liquidity provider does not receive the fees, which is their reward.

By applying the price range [Pa​,Pb​] to the Curve of Virtual Reserve, we were able to understand the Curve of Real Reserve through the actual supply amount. In essence, the Curve of Real Reserve becomes the AMM graph that liquidity providers using Uniswap V3 will have.

3. Mathematical Proof of Uniswap V3 Concentrated Liquidity

As explained above, Uniswap V3’s liquidity is calculated by substituting the price range [Pa​,Pb​] set by liquidity providers into the virtual supply graph, computing both virtual and actual supply amounts.

It is a reasonable CPMM formula for implementing AMM as claimed in the Uniswap V3 Whitepaper. Now, let’s prove mathematically that this formula is a rational liquidity calculation formula.

Before proving the concentrated liquidity formula, it is essential to understand the interrelationship between token supply, liquidity, and token price as claimed in the actual CPMM.

The CPMM formula is X×Y=K. Here, X and Y represent the total supply of the two assets tied up in the liquidity pool. L² plays the most crucial role in CPMM; it is a constant value determined by multiplying the supply of the two assets and is a value that never changes except in specific situations.

In the CPMM formula X×Y=K, the asset price of X is determined through X and Y. The price (P) of the two assets provided in the liquidity pool is calculated by dividing the supply of the two assets (X/Y).

Derive Value X

Derive Value Y

Final Derive

In conclusion, we can understand the final derivation of the CPMM formula through such proof. Now, using the formula mentioned above, we can define the virtual supply amounts (Xv​, Yv​).

As mentioned before, but to reiterate, Uniswap V3 claims that its concentrated liquidity maintains the principles of the original CPMM formula through the use of virtual supply. Ultimately, to calculate liquidity, one must know the actual supply + virtual supply. Let’s now define the virtual supply mathematically.

Derive virtual reserve of Xv

In Section 2 — Curve of Virtual Reserve & Curve of Real Reserve, we implemented the actual graph by substituting the price range [Pa​,Pb​] into the virtual graph. And the actual graph is the liquidity that the liquidity provider obtains. Assuming that the original price (P) becomes the maximum price in the range (Pb​), then at the price Pb​ in the actual graph, the actual supply of the X token (Xr​) becomes 0 because no more liquidity can be provided.

Using the premise that the actual supply of the X token (Xr​) becomes 0 when the price Pb​ is reached in the actual graph, we can define the virtual supply of the X token (Xv​) as above.

Next, using the Derive Value Y defined above and the actual + virtual supply, we can define the virtual supply of the X token (Xv​) as above.

Finally, by applying the premise that the current price (P) in the virtual liquidity graph becomes the maximum price set (Pb​), we can ultimately define the virtual supply of the X token as above.

Derive virtual reserve of Yv

For the virtual supply of the Y token (Yv​), as in Section 2 Curve of Virtual Reserve & Curve of Real Reserve, we implemented the actual graph by substituting the price range [Pa​,Pb​] into the virtual graph.

If we assume that the original price (P) becomes the minimum price in the range (Pa​), then at the price Pa​ in the actual graph, we reach a state where no more liquidity can be provided, so the actual supply of the Y token (Yr​) becomes 0.

Using the premise that the actual supply of the Y token (Yr​) becomes 0 when the price Pa​ is reached in the actual graph, we can define the virtual supply of the Y token (Yv​) as above.

Next, using the Derive Value X defined above and the actual + virtual supply, we can define the virtual supply of the Y token (Yv​) as above.

Finally, by applying the premise that the current price (P) in the virtual liquidity graph becomes the minimum price set (Pa​), we can ultimately define the virtual supply of the Y token as above.

Derive Final Real Liquidity

The important aspect of transitioning from V2 to V3 is that updates must be made without deviating from the original principles and rules of the CPMM algorithm.

Uniswap V3 has been updated with a new liquidity formula that adds the price range [Pa​,Pb​] variables. It was necessary to verify that this new formula does not deviate from the existing CPMM X×Y=L2 formula. Through a series of proofs like the one above, we can define the new formula and see that it has been updated rationally as an algorithm.

This concludes the DeFi Series on the mathematical proof of Uniswap V3 update’s key feature: Concentrated Liquidity.

--

--