StableSwap implements the Curve invariant for trading between assets that should maintain a 1:1 peg (stablecoins, wrapped assets, liquid staking derivatives). The invariant interpolates between constant-sum (x + y = k, zero slippage) and constant-product (x * y = k, full slippage) based on an amplification parameter A. At typical operating ranges, StableSwap achieves 100-1000x lower slippage than constant-product AMMs for pegged pairs.
The StableSwap invariant for n tokens:
A * n^n * sum(x_i) + D = A * D * n^n + D^(n+1) / (n^n * prod(x_i))
Where:
A: amplification coefficient (higher = closer to constant-sum)D: total deposit value at equilibriumx_i: reserve of token in: number of tokens in the poolThe amplification parameter A can be adjusted by governance over a ramping period:
Ramping prevents sudden invariant changes that could be exploited.
StableSwap LP tokens are fungible ERC-20. Deposits and withdrawals can be single-sided (any one token) or balanced (all tokens proportionally). Single-sided operations incur a small fee to compensate for the imbalance.
The LP token's virtual price is D / totalSupply. It monotonically increases as fees accrue. This makes LP tokens suitable as collateral in lending protocols (LP-044).
1. De-peg risk: if an asset in the pool loses its peg, the pool concentrates into the depegged asset. The amplification parameter A determines how quickly this happens.
2. Amplification attacks: malicious governance could ramp A to extreme values. The ramp rate limit and timelock mitigate this.
3. Read-only reentrancy: the get_virtual_price() function must be called with a reentrancy guard when used as an oracle by external protocols.
github.com/luxfi/standard/contracts/amm/stable/ |StableSwapFactory.sol |StableSwapMath.sol |Copyright (C) 2024-2026, Lux Partners Limited. All rights reserved.
Licensed under the MIT License.