Stableswap Math
Curve-style Stableswap 2-asset pools use an amplified invariant that interpolates between constant-product () and constant-sum () behavior:
where is the amplification coefficient, for a 2-asset pool, and is the pool invariant (total liquidity at equal prices). High concentrates liquidity around the 1:1 peg; low behaves more like a standard constant-product pool.
Amplification coefficient
controls how tightly the pool prices around peg. At , the pool degrades to constant-product. As , the pool approaches constant-sum (zero slippage at peg, catastrophic slippage off-peg). Real-world pools typically use 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 : higher means a sharper cliff.
The ε ↔ δ relationship
For a depeg of magnitude (fractional price deviation from peg), the reserve imbalance is a function of and . 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 handlingSimulateStableswapPriceMove— project position value at a hypothetical price changeAssessDepegRisk— quantify exposure to a stablecoin depeg using the ε ↔ δ relationship
See agentic_primitives_by_category for executable examples.