When providing liquidity on V3, an LP doesn’t necessarily have to provide an equivalent amount of x and y tokens like how it was with V2. What an LP provides depends on the price range they specify and where the range is relative to the current price.
Each position that an LP creates is a mapping from an account address, lower bound, and upper bound to a liquidity value L. This liquidity will be combined later on, when the price crosses this particular LP’s range, with all the other LPs to create a global L value for swaps.
So, depending on the range specified by the LP relative to the current price P, there are three different ways to compute this L value, and therefore, how much of each x and y token to deposit.
1. P≤Pa, current price is below the LP’s range
2. Pb≤P, current price is above the LP’s range
3. Pa≤P≤Pb, current price is within the LP’s range
For each of these cases, we will see how much of token x and token y that an LP has to deposit when they specify some Pa and Pb
Calculate L when P≤Pa
Here, we have a graph where P is below Pa:
From Part 1 in this series, we know that yreal reserves become 0 when the price reaches or exceeds Pa. So, for any price at Pa or below, there are 0yreal tokens in the LP position for this range.
To solve for L, we take our concentrated liquidity function and specify that yreal is 0 when P≤Pa:
We can see that calculating liquidity to provide only requires the deposit of xreal tokens by the LP when price P is below Pa.
Calculate L when Pb≤P
Here, we have a graph where P is above Pb:
From Part 1 in this series, we know that xreal reserves become 0 when the price reaches or exceeds Pb. So, for any price at Pb or higher, there are 0xreal tokens in the LP position for this range.
To solve for L, we take our concentrated liquidity function and specify that xreal is 0 when Pb≤P:
We can see that calculating liquidity to provide only requires the deposit of yreal tokens by the LP when price P is above Pb.
Calculate L when Pa≤P≤Pb
Here, we have a graph when P is between Pa and Pb.
The goal here to determine the ratio of x and y tokens to deposit for the specified range, Pa and Pb.
We know that when P is below Pa, the range only has xreal tokens as liquidity:
And we also know that when P is above Pb the range only has y tokens in it:
If we combine these two examples, we can see that to the left of P only exists x tokens and to the right only y tokens.
The intuition here is that any range, where P is inside Pa and Pb, can be split up into two sub-ranges, with one only containing xreal tokens and one only containing yreal tokens:
Since we have already solved for L in both cases where a range has only xreal tokens or only yreal tokens, we can also solve for this case.
We start with our liquidity calculation for L in a range with only xreal tokens:
L=xreal⋅Pb−PaPb⋅Pa
Since we know the range Pb to P only has xreal tokens, we use this range instead:
L=xreal⋅Pb−PPb⋅P
Now that we have an L value, we can use it to solve for yreal. We’ll use the range P to Pa since we know it only has yreal tokens:
L=Pb−Payreal⟶L=P−Payreal
Solve for yreal:
yreal=L⋅(P−Pa)
So, given xreal as input, we can calculate the L value, then use L to solve for yreal. Using the L value from the xreal calculation works on the yreal side because on either side of P, there is expected to be L liquidity. This is the case because the entire range has L⋅L=L2 as its constant product.
This process is how V3 calculates how much of each token that an LP should provide. An LP specifies a range, enters an amount of token x, and based on the current price, a y amount of the token is specified to deposit as well.
Let’s see an example
An LP is adding a Δx of 2 ETH in a ETH/DAI pool. ETH’s current price is P=2000, and the LP has specified the range from Pa=1500 to Pb=2500. How much DAI does the LP need to add along with the 2 ETH?
With Pb to P as our range and xreal set as 2, we can calculate how much liquidity will be added to the pool: