Try   HackMD

StableSwap Pools

Notes and explicit expressions for StableSwap pools.

Background

The StableSwap invariant follows

Annixi+D=ADnn+Dn+1nnixi

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

(CP)ixi=(Dn)n
(CS)ixi=D

StableSwap=χDn1CS+CP

where

χ=Aixi(D/n)n maintains the nice AMM property of providing liquidity at all prices: i.e.
x00
implies
x1
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

x0=3Crv and
x1=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.

x0=x1 in the two token case, where price is 1.

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

xi and quote token
xj
as the negative of the derivative of base w.r.t quote

Pij=dxidxj

Applying

d/dxj to the StableSwap invariant equation gives an expression for the marginal price as a function of token balances in the pool

0=iPijxi[Ann(xi+kxk)+D(1Ann)]

where

Pjj=1,j.

For the MIM metapool example, it's relatively easy to solve for this price w.r.t the 3Crv quote token (assuming

DAI=USDC=USDT1 relative to MIM). Solving the quadratic in the two coin case,

P10(x0,x1)=x1x0D3+16Ax02x1D3+16Ax0x12x1(x0)=B(x0)+B(x0)2+E(x0)D32E(x0)

where

B(x0)=D(14A)x0+4Ax02E(x0)=4Ax0

for

A>0 (i.e. not pure constant product).

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.

xy):

Marginal Slippage

Define the marginal slippage experienced by the trader as the negative of the derivative of marginal price w.r.t. quote

Sij=dPijdxj=d2xidxj2

Applying

d/dxj to the general expression for marginal price

0=i{[Sijxi+(Pijxi)2][Ann(xi+kxk)+D(1Ann)]PijxiAnn[Pij+kPkj]}

where

Sjj=0,j.

For the two coin case, slippage is given by

S10(x0,x1)=x1D3+16Ax0x12{D3[(1x0)2+(P10x1)2]16Ax0x1(P101)(P10x11x0)}

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.

xy):

References