This equation defines the StableSwap invariant. Here:
xi: token balances in the pool
D: invariant (constant once solved for given balances)
A: amplification factor (fixed for a given AMM)
n: number of tokens in the pool
In an AMM simulation, A is fixed, while the balances xi change during trades. The invariant D is preserved, ensuring the pool follows the StableSwap curve.
We start by considering a pool of equally balanced assets. Then the total deposits D can be considered as the sum of all assets. But also the product can be expressed as below.
i∑xi=D
i∏xi=(nD)n
Now we consider an amplification factor for the constant sum invariant.
χDn−1i∑xi+i∏xi=χDn+(nD)n
Relation Between χ and Amplification A
In the StableSwap invariant, the effective leverage χ is defined as
χ=A⋅(nD)n∏i=1nxi,D=i=1∑nxi
Balanced Pool
If x1=x2=⋯=xn=D/n, then
i∏xi=(nD)n
giving χ=A. Thus in perfect balance, χ equals the amplification factor A.
Imbalanced Pool
By the AM–GM inequality,
0≤(D/n)n∏ixi≤1
with strict inequality when the pool is imbalanced. Hence
0≤χ<A
Extreme Imbalance
If one asset tends to zero or one asset dominates, then ∏ixi→0 while (D/n)n stays positive or grows faster. Therefore,
χ→0
Conclusion
The parameter χ acts as an effective leverage measure:
χ=A when balanced
0≤χ<A when imbalanced
χ→0 under extreme imbalance
This ensures StableSwap interpolates smoothly between constant-product and constant-sum behaviour, with A setting the maximum effective amplification.
StableSwap Invariant Equation
The StableSwap invariant combines constant-product and constant-sum properties using the amplification parameter A. It is defined as:
Anni∑xi+D=ADnn+nn∏ixiDn+1
Here:
xi are the token balances
D=∑ixi is the total balance
A is the amplification factor controlling curvature
n is the number of assets in the pool
This equation ensures that the invariant interpolates smoothly between constant-product (low leverage) and constant-sum (high leverage), giving StableSwap pools both stability near balance and safety under imbalance.
In a constant-product AMM (CPMM), the invariant is k=∏ixi. This k remains constant during trades.
In StableSwap, the invariant is instead D, which depends on the amplification parameter A and the number of assets n. For given A,n, the invariant D is defined implicitly as a function of the balances x1,…,xn.
The StableSwap invariant is given by:
Anni=1∑nxi+D=ADnn+nn∏i=1nxiDn+1
Unlike CPMM where k=∏ixi has a closed form, here D must be solved numerically for given balances. During swaps, D remains constant, but the balances xi adjust.
Special Case: n = 2
For two assets (n=2), the invariant becomes:
4A(x1+x2)+D=4AD+4x1x2D3
This is the form implemented in Curve V1 two-coin pools. The equation is cubic in D, so numerical methods (e.g. Newton iteration) are used in practice.
Conclusion
StableSwap generalizes CPMM by replacing the fixed product k with a dynamic invariant D. For given A and n, D is uniquely determined by pool balances and preserved under swaps, ensuring smooth interpolation between constant-sum and constant-product behaviour.
Price Calculation in StableSwap vs CPMM
In CPMM the relation is explicit:
y(x)=xk
and the price is simply the derivative:
Px→y=−dxdy=xy
In StableSwap (2 assets, n=2), the invariant is:
4A(x+y)+D=4AD+4xyD3
This means y is implicitly defined by the function:
y=f(x;D,A)such that the invariant holds
In words: given x, D, A, we must solve numerically for y that satisfies the invariant. Once we know y(x; D, A), the price is defined as:
Px→y=−dxdy
This is why StableSwap price calculation is different from CPMM: y(x) is not a simple closed form, but the solution to the implicit equation above. In code, this means solving for y numerically and then approximating dy/dx.
# Python-style pseudocode
def solve_y(x, D, A):
solve 4*A*(x+y) + D = 4*A*D + D**3/(4*x*y) for y
def price(x, D, A):
return -(y(x+h) - y(x-h)) / (2*h)
(Reserved for figures or diagrams)
Proof of the AM–GM Inequality (Induction)
For nonnegative reals x1,x2,…,xn, we want to prove:
nx1+x2+⋯+xn≥(x1x2⋯xn)1/n
We proceed by induction.
Base Case (n=2)
For n=2, we must show:
2x1+x2≥x1x2
Squaring both sides gives:
(2x1+x2)2≥x1x2
which simplifies to (x1−x2)2≥0. This is always true.
Inductive Step
Assume the inequality holds for some n=k. Now consider n=k+1.
Let y=2xk+xk+1. By the case n=2:
y≥xkxk+1
Applying the inductive hypothesis to x1,…,xk−1,y gives:
kx1+⋯+xk−1+y≥(x1x2⋯xk−1y)1/k
Substituting y≥xkxk+1 yields:
k+1x1+⋯+xk+1≥(x1x2⋯xk+1)1/(k+1)
Conclusion
Therefore, by induction, the inequality holds for all n≥2, with equality iff all xi are equal.
Proof: Ratio Behaviour Under Balance and Imbalance
Consider n assets with balances x1,…,xn. Define
R=(nD)n∏i=1nxi,D=i=1∑nxi
Balanced Case
If x1=x2=⋯=xn=D/n, then
i=1∏nxi=(nD)n
so R=1.
Imbalanced Case
By the AM–GM inequality,
nD≥(i=1∏nxi)1/n
with equality only if all xi are equal. Therefore,
0≤R≤1
and R<1 when the pool is imbalanced.
Extreme Imbalance
If one asset tends to zero, say x1→0, then ∏ixi→0 while (D/n)n remains positive, so R→0.
If one asset dominates, say x1→∞ with others fixed, then
(D/n)n∼(nx1)n,i∏xi∼x1⋅(x2⋯xn)
R∼x1n−1nn⋅(x2⋯xn)→0
Conclusion
The ratio R satisfies R=1 in perfect balance, 0≤R<1 under imbalance, and R→0 for extreme imbalance. This property ensures that the StableSwap invariant smoothly interpolates between balanced and unbalanced states.