# Stableswaps

To synergize with the proliferation of stablecoins and stable pairs in the Cosmos ecosystem, the Nibi-Swap AMM supports stableswap pools based on Curve Finance’s Stableswap curve (opens new window).

The stableswap curve operates like a constant-price curve when a portfolio of assets is balanced and tends toward behaving like a constant-product curve if the tokens lose peg.

(constant-price)Ο•=βˆ‘i=1nxi(constant-product)∏xi=(Ο•n)n\begin{aligned} \text{(constant-price)} \quad& \phi = \sum_{i=1}^n x_i \quad\quad \\ \text{(constant-product)} \quad& \prod x_i = \left( \frac{\phi}{n} \right)^n \end{aligned}

Variable Description
Ο•\phi Liquidity depth. The liquidity depth Ο•\phi denotes the sum of all token quantities when they have an equal price.
xix_i Reserve amount amount of coin ii
nn The total number of tokens. The number of elements in the set {xi}\{ x_i \}.

# Amplification

How strongly the stableswap curve behaves like a constant-price curve is expressed by a non-negative quantity called the "Amplification", AA. When Aβ†’0A\to 0, the curve behaves more like a constant product, and as Aβ†’βˆžA \to \infty, the curve acts more like a constant-sum.

Shows how a Stable Swap curve changes with different values for the amplification A.

When changes occur to the reserves of a stableswap pool, we solve for the liquidity depth Ο•\phi using the following this constraint equation:

Ann(βˆ‘i=1nxi)+Ο•=AΟ•nn+Ο•n+1nn(∏i=1nxi)An^n \left( \sum_{i=1}^n x_i \right) + \phi = A\phi n^n + \frac{\phi^{n+1}}{n^n\left( \prod\limits_{i=1}^n x_i \right) }

This is done iteratively with Newton's method (opens new window), which is useful for approximating roots, zeros, or intercepts of real-valued functions. It's particularly useful here because the constraint equation doesn't have a clear analytical solution. The implementation for this is inside the SolveStableswapInvariant function of dex/types/pool.go (opens new window)