It is common to integrate with decentralized exchanges (DEX) while building DeFi products. Curve is the most preferred choice when it comes to stable coins because of low slippage. However, to integrate with such protocols, one needs to clearly understand its automated market maker (AMM) design. Curve’s stableswap AMM algorithm is more complex and calculation-heavy than Uniswap’s constant product AMM.
In this post, I have explained the maths behind the stableswap invariant, the method used to solve it, and how it is used in the protocol while swapping tokens or finding other parameters.
StableSwap Invariant
An invariant is an equation that defines the relationship between balances of pool tokens and pool parameters. The AMM design makes sure that this relationship is strictly followed before and after each trade. Examples of invariants are
- Constant sum or linear invariant
- Constant product invariant
Stableswap invariant is a combination of the above two and offers very low price slippage than Uniswap for stable coin pools. It is given by
where

Figure 1: Comparison of constant product, constant sum, and stableswap invariant
As it can be seen in Figure 1, the stableswap invariant is a combination of the constant sum and constant product invariant. Check out this notebook to see the impact of different parameters on the stableswap invariant curve.
Let’s write
This is the equation that Curve’s AMM follows for stable coin pools. This basically means that if a trader wants to swap token
Newton’s Method
Newton’s Method, also known as Newton Raphson Method, is an iterative process that can approximate solutions to an equation with incredible accuracy. It’s a method to approximate numerical solutions (i.e., x-intercepts, zeros, or roots) to equations that are too hard for us to solve by hand. To find the roots of the equation
where
Swapping Tokens
Coming back to the StableSwap equation, let’s look at how it is used in the Curve protocol. I have taken 3pool (DAI/USDC/USDT ) as an example.
Let’s say the trader wants to know the amount DAI will they receive for depositing
where,
The root of the above equation can be calculated using Newton’s method by iterating the below equation until convergence.
Finally, the amount of token get_y(i,j,x,xp)
function of 3poolSwap contract. The variable naming is consistent with the code.
Parameter D
The value of parameter
where
This is implemented in the get_D(xp, amp)
function of the 3poolSwap contract and is called before performing any swap.
Next Up
Stableswap invariant works fantastically for stable coin pools and has lower price slippage and higher APR compared to existing AMMs like the constant product. However, it cannot be used as it is for non-stable coin pools. For that, a little tweak is required in the stableswap invariant. Curve has introduced CurveCrypto invariant which is encouraged from stableswap invariant and can be used for non-stable coin pools like the tricrypto2 pool. In the next post, I will be explaining the CurveCrypto invariant and how is used as AMM in the tricrypto2 pool.