The constant product formula in Uniswap v3

Our goal is to derive the constant product formula based on real reserves for a segment, given by

L2=(xr+Lpu)(yr+Lpl)L^2 = (x_r+\frac{L}{\sqrt{p_u}})(y_r+L\sqrt{p_l})

and from there recover some of the formulas obtained in the previous chapter.

This chapter is optional. It introduces no fundamentally new concepts compared to the previous ones, but offers a different perspective on deriving real reserves from price and liquidity. It can be skipped without any detriment to the rest of this book.

Uniswap is a Constant Product Automated Market Maker. In Uniswap v2, if we disregard fees, swaps in the pool follow the rule that the constant kk in the formula xy=kxy=k remains unchanged, with xx and yy representing the pool’s reserves of tokens X and Y. In this sense, one can say that the formula xy=kxy=k defines the behavior of swaps in the pool.

Uniswap v3 is also a Constant Product AMM, with the main difference being that the pool does not have constant liquidity but is composed of several curve segments, each with a certain liquidity LL.

Comparing an infinite curve in uniswap v2 to finite curves in uniswap v3

In each segment, swaps follow the same behavior as in Uniswap v2 and, disregarding fees, must maintain the value LL constant in the formula L2=xyL^2 = xy, as long as the swap does not cross the boundaries of the segment.

Thus, the formula governing swaps in a segment remains L2=xyL^2 = xy. However, unlike in Uniswap v2, xx and yy are not the segment’s real reserves but its virtual reserves, i.e., the reserves the segment would have if it extended to an infinite curve (see the chapter on real and virtual reserves).

One might ask whether the constant product formula L2=xyL^2=xy for a segment could be expressed in terms of the segment’s real reserves rather than its virtual reserves. The answer is yes. In fact, this formula can be found on page 2 of the Uniswap v3 whitepaper, as shown below (here, xx and yy denote the real reserves, which we denote as xrx_r and yry_r in our notation). This is the same formula shown in the introduction.

Constant product formula in the Uniswap V3 Whitepaper

The formula above establishes a relationship between liquidity, segment boundaries, and real reserves. In our notation, this formula should be written as

L2=(xr+Lpu)(yr+Lpl)L^2 = (x_r+\frac{L}{\sqrt{p_u}})(y_r+L\sqrt{p_l})

where plp_l is the price at the lower tick, pup_u is the price at the upper tick, and LL is the liquidity.

For a given segment, these values are fixed, while the real reserves vary. This is similar to Uniswap v2, where liquidity kk is fixed in k=xyk=xy and the reserves xx and yy can change. In both cases, the formulas constrain the possible reserve values—if the reserves of one token increase, the reserves of the other must decrease so that the liquidity remains constant.

This idea can be better visualized in the interactive tool below. You can choose the upper tick, lower tick, the liquidity of the segment and the current price. As the price moves, the real reserves xrx_r and yry_r change, but in such a way that the liquidity LL stays constant. In other words, the proportion between the real reserves is not arbitrary but fully determined by the liquidity and the tick boundaries.

One major difference between v2 and v3 is that, in v3, one of the reserves can reach zero without the other approaching infinity. It’s normal to exhaust the real reserves of one token in a segment, at which point the price moves to the next segment.

It’s worth noting that the constant product formula appears nowhere in the codebase. In Uniswap v2, the protocol can simply enforce the constant product formula in swaps. In v3, however, this is more complex because the price may pass through multiple segments during a swap.

Still, the constant product formula can be used to derive expressions for the real reserves in terms of the segment boundaries, liquidity, and current price. We already derived these formulas in the previous chapter by other means, and for this reason, this chapter is optional.

Nevertheless, if the reader wishes to understand where the constant product formula comes from and how it can be used to calculate the real reserves of a segment, that is the purpose of this chapter, and we invite you to proceed.

Relating the real and virtual reserves in a segment

The constant product formula for a segment is the same as in Uniswap v2,

L2=xyL^2=xy

but now xx and yy are virtual reserves.

We can derive the constant product formula for real reserves if we find a way to relate virtual reserves to real reserves.

Consider the illustration below, where plp_l is the price at the lower tick and pup_u is the price at the upper tick.

Diagram showing virtual reserves, lower tick, and upper tick

In the image above, we have

  • (x,y)(x,y) are the virtual reserves of the current price,
  • yly_l are the virtual reserves in Y at plp_l - the amount of tokens Y the segment would have if it were an infinite curve and the price were at plp_l. However, we are more interested in yly_l geometrically than in its interpretation,
  • xux_u are the virtual reserves in X at pup_u,
  • (xr,yr)(x_r, y_r) are the real reserves of the segment.

The relation between virtual and real reserves is then given by

y=yl+yrx=xu+xr\begin{align*} y &= y_l+y_r \\ x &= x_u + x_r \end{align*}

We can use these relations in L2=xyL^2=xy, substituting xx and y,y, to get

L2=(xu+xr)(yl+yr)L^2 = (x_u+x_r)(y_l+y_r)

In a segment, xrx_r and yry_r can vary during swaps, but xux_u and yly_l depend only on the segment boundaries, so they are constants for the segment. Thus, if we can obtain xux_u and yly_l, we have found the desired formula. We derived formulas to obtain xux_u and yly_l in the previous chapter, and in the following section we will use these formulas to derive the constant product formula for real reserves.

The constant product formula for the real reserves

In the chapter on real and virtual reserves, we learned how to find the virtual reserves of a segment from price and liquidity: x=L/px = L/\sqrt{p} and y=Lpy=L \sqrt{p}. We can use these formulas to obtain xux_u and yly_l as follows:

xu=Lpuyl=Lpl\begin{align*} x_u &= \frac{L}{\sqrt{p_u}} \\ y_l &= L\sqrt{p_l} \end{align*}

Using these values, we can write the constant product formula

L2=(xr+xu)(yr+yl)L^2 = (x_r+x_u)(y_r+y_l)

as

L2=(xr+Lpu)(yr+Lpl)L^2 = (x_r+\frac{L}{\sqrt{p_u}})(y_r+L\sqrt{p_l})

This formula is valid for a segment with liquidity LL, where the lower tick is plp_l and the upper tick is pup_u.

We now have a constant product formula that relates liquidity, real reserves, and segment boundaries, without any reference to virtual reserves.

Note that calculating the liquidity of a segment from the real reserves is not straightforward because the formula above involves the parameter LL on both sides of the equation; it is a quadratic equation in LL.

In practice, we don’t need to worry about this because the protocol does not use the above formula to calculate liquidity. Instead, the protocol stores information about liquidity and, from the given liquidity, calculates the real reserves for tokens X and Y between two prices.

Thus, what we are interested in is finding a formula to calculate xrx_r and yry_r based on the segment’s liquidity, boundaries, and the current price.

In the previous chapter, we already derived these formulas without any reference to the constant product formula—as stated in the introduction, the constant product formula is not strictly necessary for this.

For educational purposes, we will re-derive the formulas from the previous chapter using the constant product formula. The key idea is that, even though the constant product formula is not used in the codebase, the formulas for calculating real reserves based on the current price, liquidity, and the segment boundaries are indeed used by the codebase, and those formulas can be derived from the constant product formula.

To demonstrate this, let’s perform the derivation for the three possible scenarios:

  1. When the current price is at or below the lower tick, so there are only real reserves in tokens X,
  2. When the current price is at or above the upper tick, so there are only real reserves in tokens Y,
  3. When the current price is within the segment, so there are real reserves in both tokens X and Y.

Let’s begin with the case where the current price is at or below the lower tick

1. Only tokens X in a segment

In the scenario where the segment only has X tokens, then yry_r equals zero and we can work out the constant product formula to derive xrx_r, as follows:

L2=(xr+Lpu)(0+Lpl)L2=(xr+Lpu)LplLpl=(xr+Lpu)divide both sides byLplxr=LplLpumove Lpu to the other side of the eq.\begin{align*} L^2 &= (x_r+\frac{L}{\sqrt{p_u}})(\boxed{0} + L\sqrt{p_l}) \\ L^2 &= (x_r+\frac{L}{\sqrt{p_u}})L\sqrt{p_l} \\ \frac{L}{\sqrt{p_l}} &= (x_r+\frac{L}{\sqrt{p_u}}) && \text{divide both sides by} L \sqrt{p_l} \\ x_r &= \frac{L}{\sqrt{p_l}} - \frac{L}{\sqrt{p_u}} && \text{move } \frac{L}{\sqrt{p_u}} \text{ to the other side of the eq.} \end{align*}

Note this is identical to the formula we derived in the last chapter.

Now let’s consider the case where the segment contains only Y tokens—when the current price is at or above the upper tick.

2. Only tokens Y in a segment

In the scenario where the segment only has Y tokens, then xrx_r equals zero and we can work out the constant product formula to derive yry_r, as follows:

L2=(0+Lpu)(yr+Lpl)L2=Lpu(yr+Lpl)Lpu=yr+Lplmultiply both sides by puLyr=LpuLplmove Lpl to the other side of the eq.\begin{align*} L^2 &= (0 +\frac{L}{\sqrt{p_u}})(y_r+L\sqrt{p_l}) \\ L^2 &= \frac{L}{\sqrt{p_u}}(y_r+L\sqrt{p_l}) \\ L\sqrt{p_u} &= y_r + L\sqrt{p_l} && \text{multiply both sides by } \frac{\sqrt{p_u}}{L} \\ y_r&=L\sqrt{p_u} - L\sqrt{p_l} && \text{move } L\sqrt{p_l} \text{ to the other side of the eq.} \end{align*}

Note this is identical to the formula we derived in the last chapter.

We now turn to the final scenario, where the price lies between the lower tick and the upper tick.

3. Both tokens in the segment

When the price is between the lower and upper ticks, the segment will have both tokens X and Y.

The constant product formula

L2=(xr+Lpu)(yr+Lpl)L^2 = (x_r+\frac{L}{\sqrt{p_u}})(y_r+L\sqrt{p_l})

does not involve the current price, nor can we calculate xrx_r and yry_r from it alone. Note that we have two unknowns (xrx_r and yry_r) but only one equation. To solve this problem, we need another equation.

We know that the formula for price is p=y/xp = y/x in terms of virtual reserves. Thus, in terms of real reserves, we have:

p=yx=yr+Lplxr+Lpup=\frac{y}{x} = \frac{y_r + L \sqrt{p_l}}{x_r + \frac{L}{\sqrt{p_u}}}

Now we have two equations (the constant product formula and the price) and two unknowns (xrx_r and yry_r). We can rearrange the price formula as:

p=yr+Lplxr+Lpuyr+Lpl=p(xr+Lpu)\begin{align*} p &= \frac{y_r + L \sqrt{p_l}}{x_r + \frac{L}{\sqrt{p_u}}} \\ y_r + L \sqrt{p_l} &= p\left( x_r + \frac{L}{\sqrt{p_u}} \right) \end{align*}

The term on the left-hand side appears in the constant product formula, so we can substitute it there.

L2=(xr+Lpu)(yr+Lpl)L2=(xr+Lpu)p(xr+Lpu)replaced (yr+Lpl)L2=(xr+Lpu)2pL=(xr+Lpu)ptake the square rootLp=xr+Lpudivide both sides by pxr=LpLpu\begin{align*} L^2 &= \left(x_r+\frac{L}{\sqrt{p_u}}\right)\boxed{(y_r+L\sqrt{p_l})} \\ L^2 &= \left(x_r+\frac{L}{\sqrt{p_u}}\right)p\left( x_r + \frac{L}{\sqrt{p_u}} \right) && \text{replaced } (y_r+L\sqrt{p_l}) \\ L^2 &= \left(x_r+\frac{L}{\sqrt{p_u}}\right)^2p \\ L &= \left(x_r+\frac{L}{\sqrt{p_u}}\right) \sqrt{p} && \text{take the square root} \\ \frac{L}{\sqrt{p}} &= x_r+\frac{L}{\sqrt{p_u}} && \text{divide both sides by } \sqrt{p} \\ x_r &= \frac{L}{\sqrt{p}} - \frac{L}{\sqrt{p_u}} \end{align*}

Now that we have xrx_r, we can use this expression in the constant product formula to find yry_r:

L2=(xr+Lpu)(yr+Lpl)L2=(LpLpu+Lpu)(yr+Lpl)used xrL2=(LpLpu+Lpu)(yr+Lpl)L2=(Lp)(yr+Lpl)Lp=yr+Lplmultiplied both sides by pLyr=LpLpl\begin{align*} L^2 &= \left(\boxed{x_r} +\frac{L}{\sqrt{p_u}}\right)(y_r+L\sqrt{p_l}) \\ L^2 &= \left(\frac{L}{\sqrt{p}}-\frac{L}{\sqrt{p_u}}+\frac{L}{\sqrt{p_u}}\right)(y_r+L\sqrt{p_l}) && \text{used } x_r \\ L^2 &= \left(\frac{L}{\sqrt{p}}-\cancel{\frac{L}{\sqrt{p_u}}}+\cancel{\frac{L}{\sqrt{p_u}}}\right)(y_r+L\sqrt{p_l}) \\ L^2 &= \left(\frac{L}{\sqrt{p}}\right)(y_r+L\sqrt{p_l}) \\ L \sqrt{p} &= y_r+L\sqrt{p_l} && \text{multiplied both sides by } \frac{\sqrt{p}}{L} \\ y_r &= L \sqrt{p} - L \sqrt{p_l} \end{align*}

The formulas for xrx_r and yry_r are identical to those we obtained in the previous chapter.

Summary

The constant product formula for a segment with liquidity LL between a lower tick plp_l and an upper tick pup_u is given by

L2=(xr+Lpu)(yr+Lpl)L^2 = (x_r+\frac{L}{\sqrt{p_u}})(y_r+L\sqrt{p_l})

where xrx_r and yry_r are the real reserves in tokens X and Y.

Ready to Get Started?Join Thousands of Users Today

Start your free trial now and experience the difference. No credit card required.

© 2025 Better-Start. All rights reserved.