Bright blue glacial lake surrounded by white ice on a dark mountain

Curve’s Invariant

A mathematical breakdown

Philip Lu | Oct. 2022


Curve.fiCurve V2

Liquidity Equation

Before we talk about Curve’s liquidity equation, we’ll first discuss its predecessor called Stableswap.

Stableswap

As we’ve seen, exchanges like Uniswap use the , or constant-product, invariant to maintain liquidity even if prices shift significantly. Before concentrated liquidity, however, stablecoins were facing price stability and liquidity issues; in addition, only a few stablecoin LPs earned fees. That’s why, in 2019, Curve.fi founder Michael Egorov sought to build “Stableswap”, an efficient, stablecoin-oriented AMM with lower slippage than Uniswap.

The Constant-Price Invariant

To begin, assume that by selling any amount of one stablecoin, we can buy the exact same amount of another, and that prices remain constant after trades. We generalize this behaviour for any number of tokens , giving us a “linear” constant-price invariant. For example, we get  for two tokens. Let  denote the balance of token  deposited:

For any two stablecoins, the exchange rate between each token would be . However, we can’t maintain a price of exactly  because prices are constantly fluctuating by tiny amounts.

The Constant-Product Invariant

We generalize the constant-product equation  for any number of tokens  with  representing a weighting factor proportional to the ratio of a pool’s assets comprised by token , where all  add up to :

Finding a Middle Ground

Chart, histogram

Description automatically generated

Figure X: Stableswap Invariant in Between Constant-Price and Uniswap Invariants (Curve)

In the figure above, the desired Stableswap invariant is “flat” when the reserves of X and Y are balanced, meaning the price remains constant at close to . If the price goes outside of equilibrium, there is still liquidity offered with less slippage than what Uniswap users would experience.

Chart, line chart

Description automatically generated

Figure X: Price Slippage Under Uniswap and Stableswap (Curve)

As we swap more of one token for the other, both invariants experience considerable slippage due to providing liquidity while the pool is heavily unbalanced.

The Stableswap Equation

Inspired by both graphs above, we want a “straight-line" constant-price part to have more influence when reserves are balanced, but as the portfolio becomes more imbalanced, we want the curved constant-product part to take over.

Assume we start a pool with  stablecoins with equal balances in it. If and only if the pool is balanced, do the following two statements hold:

(Balanced Constant-Price Invariant)

(Balanced Constant-Product Invariant)

, where D is the total balance of the pool when all the coins’ prices are equal. If each coin has equal reserves, each reserve is equal to the average amount , so the constant-product invariant is the product of multiplied by itself  times.  

But what happens when the reserves become imbalanced? That’s where we introduce an “amplification coefficient” parameter . A higher  extends the range where Stableswap looks like the straight-line constant-price invariant above for longer. A lower  means Stableswap looks more like the curved Uniswap invariant above.

Diagram

Description automatically generated

Figure X: Stableswap (Red) Approximating a Straight Line as  Approaches  and a Curve as  Approaches  (Balancer)

We say that our constant-price invariant that experiences close to zero slippage corresponds to having infinite leverage because it's like there's an infinite reserve of coins that keep replenishing the token balances after swaps such that there is no price impact. On the other hand,  leverage gives us our regular constant-product invariant that experiences significant slippage but offers liquidity when there is imbalance.

Let  denote the leverage we apply to our Stableswap invariant. As such, we want the constant-price invariant as  and the constant-product invariant when . We add the Balanced Constant-Price and the Balanced Constant-Product Invariants to each other to reflect Stableswap’s two parts. We apply leverage to the constant-price part to help “flatten” the graph by multiplying by .

(Leveraged Invariant)

We multiply the constant-price term by  to also get two terms with  in them because we want to account for the fact that  could be much larger than  . As planned, if our leverage , , and we only have the constant-product term . In contrast, as , , and  is very small in comparison since the number of stablecoins  does not grow when  grows, which makes the constant-price invariant  more dominant.

However, this invariant relies on the fact that the reserves of each token are equal, and liquidity is only supported close to the ideal price of . We alter  to make it dynamic: when the portfolio is perfectly balanced,  is equal to the amplification coefficient ; however, when the portfolio becomes imbalanced,  starts to fall off to .

 and  cancel each other out when the token reserves are balanced, leaving only , but how do we know that this new  decreases towards  as the pool becomes imbalanced? When the pool is balanced, all token balances  are the same, so  .

If a pool starts off balanced and becomes imbalanced, we can write any reserve amount  as , where  is the difference between the starting amount  and current amount . Assume you can swap stablecoins 1:1. For an imbalanced pool, we then have

, where .. Say that a balanced pool is started with  of token 1 and  of token 2. If you deposit 5 of token 1 and take out 5 of token 2. We have that

In this case, , so . Clearly,  for any , so  here. We can mathematically describe imbalance using the ratio  for any two tokens and : the closer  for any token pair is to 1, the more balanced the pool is, and the further away  for any token pair is from 1 (towards either 0 or ), the less balanced the pool is.

Let’s plug some numbers into the two equations above. We have three scenarios where both X and Y have starting balances of 20, 10, and 6; we deposit 5 of token 1 and take out 5 of token 2.

For :

For :

For :

We see that as the imbalance represented by  increased,  got closer to 0 since grew relative to , so  is working as intended.

We sub in  into our leveraged invariant:

Divide both sides by , and then multiply both sides by , and we’re done:

(The Stableswap Invariant)

Both  and  were working against our amplification coefficient  that helps keep prices constant (in the first term on the left-hand side of the equation), so we got rid of them.

And there we have it. Our Stableswap invariant, with the help of the amplification coefficient , keeps prices constant when reserves are reasonably balanced, otherwise providing liquidity under the constant-product curve with some slippage.

Wow, that was a long ride. Don’t worry, we’re done with the math now.

Sike! There’s even more! We haven’t even touched upon Curve’s actual invariant that it uses in its V2, which includes non-stablecoin assets!

The CurveCrypto Invariant

From Curve v2’s whitepaper:

Text, letter

Description automatically generated

 denotes how imbalanced the pool is, as  becomes smaller relative to as the pool grows imbalanced.  is our regular amplification coefficient.  is the tiny distance between two constant-product variants that form a “range” that CurveCrypto can deviate within.

If the reserves are balanced:

giving us our Stableswap invariant.

If the reserves are imbalanced:

causing our invariant to shift towards constant-product.

As  becomes smaller relative to as the pool grows imbalanced,  decreases as imbalance increases. Thus,

decreases due to having a bigger denominator.

 has a linear relationship with , so  essentially has a square relationship with . Therefore,  decreases by a square factor as  decreases due to greater imbalance, assuming  remains constant. Furthermore,  drops off even faster.

We’ve now established that the “leverage” factor  is more sensitive to reserve imbalances, causing a quicker shift to the constant-product invariant in CurveCrypto than in Stableswap. This is likely compensation for the fact that Curve is working with assets that are more volatile than stablecoins.

This begs the question: what is the meaning of taking , the small distance between two constant-product invariant hypersurfaces?  We’ve mentioned that these invariants could form a miniscule range that includes our CurveCrypto invariant inside.

Due to constant trading volume affecting prices and the volatile nature of non-stablecoin assets, this small range provides a bit of leeway for the Curve invariant to shift in when reserves are imbalanced, which is when we shift into our constant-product invariant. In other words,  is meant to help compensate for the volatility of the assets Curve v2 works with.

Thus, the smaller  is— that is, the closer together our boundaries are—the smaller  is, and the faster  decreases, and the faster we shift to the constant-product invariant to provide liquidity. In this sense, a lower  expresses that CurveCrypto necessarily needs to allow “faster” slippage, or more slippage per token traded than what Stableswap allows, for more volatile assets to disincentivize trading large amounts of a highly volatile token to manipulate prices.

Page