Skip to content

Stableswap Math

Curve-style Stableswap 2-asset pools use an amplified invariant that interpolates between constant-product (‘xy=k‘`xy = k`) and constant-sum (‘x+y=k‘`x + y = k`) behavior:

A⋅nn⋅(x+y)+D=A⋅D⋅nn+Dn+1nn⋅x⋅yA \cdot n^n \cdot (x + y) + D = A \cdot D \cdot n^n + \frac{D^{n+1}}{n^n \cdot x \cdot y}

where ‘A‘`A` is the amplification coefficient, ‘n=2‘`n = 2` for a 2-asset pool, and ‘D‘`D` is the pool invariant (total liquidity at equal prices). High ‘A‘`A` concentrates liquidity around the 1:1 peg; low ‘A‘`A` behaves more like a standard constant-product pool.

Amplification coefficient

‘A‘`A` controls how tightly the pool prices around peg. At ‘A=0‘`A = 0`, the pool degrades to constant-product. As ‘A→∞‘`A \to \infty`, the pool approaches constant-sum (zero slippage at peg, catastrophic slippage off-peg). Real-world pools typically use ‘A‘`A` values between 10 and 2000.

Impermanent loss under depeg

Stableswap IL behaves differently from constant-product IL. Near peg, IL is suppressed by the amplification factor — the pool holds close to 50/50 reserves even as prices drift slightly. Off peg, IL accelerates sharply because the amplified invariant concentrates liquidity in a narrow price band. The transition between “near peg” and “off peg” depends on ‘A‘`A`: higher ‘A‘`A` means a sharper cliff.

The ε ↔ δ relationship

For a depeg of magnitude ‘ϵ‘`\epsilon` (fractional price deviation from peg), the reserve imbalance ‘δ‘`\delta` is a function of ‘A‘`A` and ‘ϵ‘`\epsilon`. This relationship is computed numerically via fixed-point iteration rather than closed-form — the invariant equation is transcendental in the general case. DeFiPy’s AssessDepegRisk primitive uses this numerical approach.

DeFiPy implementation

DeFiPy’s Stableswap math lives in StableswapPy. The amplified invariant, swap math, and D computation are implemented in stableswappy.cpt.exchg.StableswapExchange. The impermanent loss formula is implemented in stableswappy.analytics.risk.StableswapImpLoss.

The agentic primitives that consume Stableswap math:

  • AnalyzeStableswapPosition — full position decomposition with alpha/unreachable-alpha regime handling
  • SimulateStableswapPriceMove — project position value at a hypothetical price change
  • AssessDepegRisk — quantify exposure to a stablecoin depeg using the ε ↔ δ relationship

See agentic_primitives_by_category for executable examples.