# StableSwap Pools Notes and explicit expressions for StableSwap pools. ## Background The StableSwap invariant follows $$ An^n \sum_i x_i + D = A D n^n + \frac{D^{n+1}}{n^n \prod_i x_i} $$ where - $A$ is a per-pool parameter assessing the deviation away from constant product toward constant sum ("leverage") - $D$ is total amount of coins in the pool when all coins have the same price The StableSwap invariant can be thought of as a superposition of the constant product invariant and the constant sum linear invariant $$ \prod_i x_i = \bigg(\frac{D}{n}\bigg)^n \tag{CP} $$ $$ \sum_i x_i = D \tag{CS} $$ $$ \mathrm{StableSwap} = \chi D^{n-1} \cdot \mathrm{CS} + \mathrm{CP} $$ where $\chi = \frac{A\prod_i x_i}{(D/n)^n}$ maintains the nice AMM property of providing liquidity at all prices: i.e. $x_0 \to 0$ implies $x_1 \to \infty$ for the two coin case. A StableSwap metapool is simply a Curve pool with at least one of the coins in the pool as a Curve LP token from a base pool. e.g. MIM Metapool has token pairs $x_0 = \mathrm{3Crv}$ and $x_1 = \mathrm{MIM}$, where 3Crv is the LP token for the base 3pool with tokens DAI, USDC, USDT. The metapool allows the usual swaps between e.g. MIM and 3Crv or swaps between MIM and USDC/USDT/DAI via `exchange_underlying`. Balances along the invariant curve compared to constant product are most pronounced near e.g. $x_0 = x_1$ in the two token case, where price is 1. <iframe src="https://www.desmos.com/calculator/lolpqmctl4?embed" width="500" height="500" style="border: 1px solid #ccc" frameborder=0></iframe> Blue dotted is CP, green dotted is CS, and orangle is StableSwap invariant. You can see the transition toward/away from constant sum as you move toward/away from the equilibrium point of close to equal balances between $(x, y)$ tokens. ## Marginal Price Define the marginal price quoted by the pool for base token $x_i$ and quote token $x_j$ as the negative of the derivative of base w.r.t quote $$ P_{ij} = -\frac{d x_i}{d x_j} $$ Applying $d / d x_j$ to the StableSwap invariant equation gives an expression for the marginal price as a function of token balances in the pool $$ 0 = \sum_i \frac{P_{ij}}{x_i} \bigg[ An^n \cdot \bigg(x_i + \sum_k x_k \bigg) + D \cdot \bigg( 1 - An^n \bigg) \bigg] $$ where $P_{jj} = -1, \forall j$. For the MIM metapool example, it's relatively easy to solve for this price w.r.t the 3Crv quote token (assuming $\mathrm{DAI} = \mathrm{USDC} = \mathrm{USDT} \sim 1$ relative to MIM). Solving the quadratic in the two coin case, $$ P_{10}(x_0, x_1) = \frac{x_1}{x_0} \cdot \frac{D^3 + 16A \cdot x_0^2 x_1}{D^3 + 16A \cdot x_0 x_1^2} \\ x_1(x_0) = \frac{-B(x_0) + \sqrt{B(x_0)^2 + E(x_0) \cdot D^3}}{2 E(x_0)} $$ where $$ B(x_0) = D (1 - 4A) \cdot x_0 + 4Ax_0^2 \\ E(x_0) = 4A x_0 $$ for $A > 0$ (i.e. not pure constant product). <!-- TODO: find inverse of P expression (or approx) --> Plots of the marginal price (green) with balance invariant (orange dotted) in the background illustrate the flattening of the price curve as the pool becomes more balanced (i.e. $x \approx y$): <iframe src="https://www.desmos.com/calculator/xczouesm8o?embed" width="500" height="500" style="border: 1px solid #ccc" frameborder=0></iframe> ## Marginal Slippage Define the marginal slippage experienced by the trader as the negative of the derivative of marginal price w.r.t. quote $$ S_{ij} = - \frac{d P_{ij}}{d x_j} = \frac{d^2 x_i}{d x_j^2} $$ Applying $d/d x_j$ to the general expression for marginal price $$ 0 = \sum_i \bigg\{ \bigg[ -\frac{S_{ij}}{x_i} + \bigg(\frac{P_{ij}}{x_i}\bigg)^2 \bigg] \bigg[ A n^n \cdot \bigg(x_i + \sum_k x_k \bigg) + D \cdot \bigg( 1 - An^n \bigg) \bigg] - \\ \frac{P_{ij}}{x_i} A n^n \cdot \bigg[ P_{ij} + \sum_k P_{kj} \bigg] \bigg\} $$ where $S_{jj} = 0, \forall j$. For the two coin case, slippage is given by $$ S_{10}(x_0, x_1) = \frac{x_1}{D^3 + 16A \cdot x_0 x_1^2} \cdot \\ \bigg\{ D^3 \cdot \bigg[ \bigg(\frac{1}{x_0}\bigg)^2 + \bigg( \frac{P_{10}}{x_1} \bigg)^2 \bigg] - 16A \cdot x_0 x_1 \cdot \bigg( P_{10} - 1 \bigg) \bigg( \frac{P_{10}}{x_1} - \frac{1}{x_0} \bigg) \bigg\} $$ Plots of the marginal slippage (red) with balance invariant (orange dotted) and marginal price (green dotted) in the background illustrate the flattening of the slippage curve toward zero as the pool becomes more balanced (i.e. $x \approx y$): <iframe src="https://www.desmos.com/calculator/0sj6hdi5bo?embed" width="500" height="500" style="border: 1px solid #ccc" frameborder=0></iframe> ## References - [StableSwap -- efficient mechanism for Stablecoin liquidity (Egorov)](https://classic.curve.fi/files/stableswap-paper.pdf)