Curve V1: The StableSwap Invariant

0xMirai77
7 min readOct 31, 2023

--

Contents:

1. Introduction
2. Constant Sum Invariant
3. Constant Product Invariant
4. StableSwap Invariant
5. Conclusion

1. Introduction

Curve Finance is a decentralized exchange (DEX) built on Ethereum. It follows the Automated Market Maker (AMM) model, a concept shared by other notable DEXes like Uniswap.

The first version of Curve (formerly named StableSwap) was proposed by Michael Egorov back in November 2019. Since its launch in January 2020, Curve has quickly grown into one of the most dominant protocols in DeFi. The initial idea of Curve was to build a low-slippage and low-fee exchange for users to trade between pegged assets such as stablecoins. Curve’s novel AMM model became far superior in terms of efficiency when facilitating stablecoin trades compared to its competitors.

Curve’s Classic UI (https://classic.curve.fi/)

What sets Curve apart from other DEXes and makes the protocol so efficient? The core to understanding Curve’s innovation lies in the StableSwap invariant, the concept that combines elements from two other invariants: the constant sum invariant and the constant product invariant. (Note: An invariant is a quantity in the formula that remains constant)

In this article, I’ll guide you through the intricacies of these individual invariants and explain the inner workings of the StableSwap AMM model. This article is not intended as a guide to use Curve; for that, you can refer to Curve’s official website for more info. Let’s dive in!

2. Constant Sum Invariant

Before getting to the StableSwap invariant, we’ll first go over the constant sum invariant, given as:

The variables X and Y represent the number of tokens in the pool, while C represents the invariant. In a constant sum invariant, the token exchange rate stays the same, regardless of the number of tokens in the pool. (Note: the exchange rate is the gradient on a point of the curve)

For instance, let’s say 1X token can be swapped out for 5Y tokens. The exchange rate can be calculated as the gradient of the curve, so 1X = 5Y. This exchange rate between the two tokens will always remain constant after every transaction.

Constant sum invariant graph

However, such invariants do not have any slippage, meaning there is no price impact before and after a trade. Thus, as more tokens get swapped out, the pool will eventually become imbalanced and entirely ran out of tokens.

3. Constant Product Invariant

The constant product invariant addresses the issue of tokens getting drained from the pool by introducing slippage into the system. It was first popularized by the early version of Uniswap AMM back in 2019. The constant product invariant is given as:

The variables X and Y represent the tokens in the pool, while K represents the invariant. In a constant product invariant, the token exchange rate changes in relative to the number of tokens available in the pool.

Constant product invariant graph

As per the curve above, let’s say there are initially 50 X and 50 Y tokens in a pool. Then, the invariant K would be 2500 (X * Y = K).

A user wants to buy 10 X tokens in exchange for Y tokens, decreasing the supply of X tokens and increasing the supply of Y tokens. The amount of Y tokens the user has to deposit is calculated as (assuming no trading fees):

X * Y = K
(50 - 10) * Y = 2500
Y = 62.5

Amount of Y deposits = Y_after - Y_before
= 62.5 - 50
= 12.5

The user would have to deposit 12.5 Y tokens in order to exchange for 10 X tokens, and the exchange rate can be calculated as 1 X = 1.25Y. Now, there are 40 X and 62.5Y tokens in the pool. If the user attempts to buy another 10 X tokens, more Y tokens (20.83 Y) will have to be deposited. The pool auto-adjusts the price of tokens! This means that as the pool gets imbalanced, the exchange rate exponentially increases, making it expensive and harder to drain the pool.

As the token price in the pool changes due to slippage, an arbitrage space is created, incentivizing arbitrageurs to step in and keep the pool balanced.

4. StableSwap Invariant

Curve V1 combines the constant sum invariant and constant product invariant to find the middle ground between low slippage and keeping the pool balanced. Thus, the StableSwap invariant was born.

Constructing the StableSwap invariant:

We will first rewrite the constant sum invariant and constant product invariant to represent any generalized n number of tokens:

The invariant D represents the total number of coins in the pool when prices are equal, and n represents the number of tokens. Now, our new invariant becomes:

And graphing the new invariant:

New invariant (Green line) between constant product (Red line) and constant sum (Blue line) invariants

Leverage ‘χ’:

As shown in the graph above, our new invariant still closely resembles the constant produce invariant (Uniswap AMM). In order for our new invariant to have lower slippage, we amplify the constant sum invariant by the leverage factor χ (chi):

The new invariant changes in relative to χ (chi):

New invariant (Green line) ‘flattens’ depending on X (chi)

The value of χ determines the “flatness” of the curve around the center, thus determining the slippage. Our new invariant (green line) lies between the constant product (blue line) and constant sum (red line) invariants, making it more efficient than the Uniswap invariant, but also ensures the pool remained balanced.

Dimensionless Leverage ‘χ’:

To make the leverage factor χ dimensionless, meaning χ will not depend on the total number of coins D, we multiply the constant sum invariant by D^(n-1):

(Note: We multiply by D^(n-1) instead of just D to simplify the equation)

Amplification Coefficient ‘A’:

Just hang in there a bit, we are almost done. We need to make sure our new invariant support liquidity at all prices, not just around the ideal price of 1. In order to do that, we make the leverage factor χ dynamic:

The amplification coefficient A is a constant. When the pool is balanced, the leverage factor χ = A (since X * Y= (D/n)^n), the curve “flattens” around the center and behaves like a constant sum invariant. When the pool goes out of balance, χ falls off to 0 (χ → 0) and the curve transitions to a constant sum invariant.

Substitute the new χ into our new invariant:

Solve and simplify the equation, we get our final StableSwap invariant:

StableSwap invariant (Green line) ‘flattens’ depending on A (amplification coefficient)

5. Conclusion

  • The first version of Curve implements the StableSwap invariant to effectively lower the slippage while keeping the pool balanced.
  • The StableSwap invariant combines both the constant sum and the constant product invariants.
  • When a Curve pool is balanced, the StableSwap AMM behaves like a constant sum invariant, but transitions to a constant product invariant as the pool goes out of balance.

And that’s all for Curve V1!

This article gives my own understanding of the inner workings of Curve. If you want to understand more of the details, check out the reference links below and go give the protocol itself a try.

If you noticed any mistakes, please feel free to reach out. Feedbacks are much welcomed.

Give a follow if you enjoyed the article, I hope you learned something new. I’m working on the second version of Curve Finance (Curve V2) for my next article, so stay tuned for that too.

--

--