AMM Research

stableswap25.png

No image

stableswap25-ALL.png

No image

stableswap25-A1.png

No image

stableswap25-A2.png

No image

stableswap25-A3.png

No image

stableswap25-CPMM.png

No image

API Response

null

No image loaded

StableSwap Invariants

StableSwap Invariant

Ann∑i=1nxi+D  =  ADnn+D n+1nn∏i=1nxiA n^n \sum_{i=1}^n x_i + D \;=\; A D n^n + \frac{D^{\,n+1}}{n^n \prod_{i=1}^n x_i}

This equation defines the StableSwap invariant. Here:

  • xix_i: token balances in the pool
  • DD: invariant (constant once solved for given balances)
  • AA: amplification factor (fixed for a given AMM)
  • nn: number of tokens in the pool

In an AMM simulation, AA is fixed, while the balances xix_i change during trades. The invariant DD is preserved, ensuring the pool follows the StableSwap curve.

Analysis

Ref: StableSwap Whitepaper

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.

∑ixi=D\sum_{i} x_{i} = D
∏ixi=(Dn)n\prod_{i} x_{i} = \left(\tfrac{D}{n}\right)^n

Now we consider an amplification factor for the constant sum invariant.

χDn−1∑ixi+∏ixi=χDn+(Dn)n\chi D^{n-1} \sum_i x_i + \prod_i x_i = \chi D^n + \left(\tfrac{D}{n}\right)^n

Relation Between χ and Amplification A

In the StableSwap invariant, the effective leverage χ\chi is defined as

χ=A⋅∏i=1nxi(Dn)n,D=∑i=1nxi\chi = A \cdot \frac{\prod_{i=1}^n x_i}{\left(\tfrac{D}{n}\right)^n}, \qquad D = \sum_{i=1}^n x_i

Balanced Pool

If x1=x2=⋯=xn=D/nx_1 = x_2 = \dots = x_n = D/n, then

∏ixi=(Dn)n\prod_i x_i = \left(\tfrac{D}{n}\right)^n

giving χ=A\chi = A. Thus in perfect balance, χ\chi equals the amplification factor AA.

Imbalanced Pool

By the AM–GM inequality,

0  ≤  ∏ixi(D/n)n  ≤  10 \;\leq\; \frac{\prod_i x_i}{(D/n)^n} \;\leq\; 1

with strict inequality when the pool is imbalanced. Hence

0  ≤  χ  <  A0 \;\leq\; \chi \;<\; A

Extreme Imbalance

If one asset tends to zero or one asset dominates, then ∏ixi→0\prod_i x_i \to 0 while (D/n)n(D/n)^n stays positive or grows faster. Therefore,

χ  →  0\chi \;\to\; 0

Conclusion

The parameter χ\chi acts as an effective leverage measure:

  • χ=A\chi = A when balanced
  • 0≤χ<A0 \leq \chi < A when imbalanced
  • χ→0\chi \to 0 under extreme imbalance

This ensures StableSwap interpolates smoothly between constant-product and constant-sum behaviour, with AA setting the maximum effective amplification.

StableSwap Invariant Equation

The StableSwap invariant combines constant-product and constant-sum properties using the amplification parameter AA. It is defined as:

Ann∑ixi+D  =  ADnn+Dn+1nn∏ixiA n^n \sum_i x_i + D \;=\; A D n^n + \frac{D^{n+1}}{n^n \prod_i x_i}

Here:

  • xix_i are the token balances
  • D=∑ixiD = \sum_i x_i is the total balance
  • AA is the amplification factor controlling curvature
  • nn 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.

Ref: StableSwap Whitepaper

StableSwap Invariant: D as a Dynamic Constant

In a constant-product AMM (CPMM), the invariant is k=∏ixik = \prod_i x_i. This kk remains constant during trades.

In StableSwap, the invariant is instead DD, which depends on the amplification parameter AA and the number of assets nn. For given A,nA, n, the invariant DD is defined implicitly as a function of the balances x1,…,xnx_1, \dots, x_n.

The StableSwap invariant is given by:

Ann∑i=1nxi+D  =  ADnn+D n+1nn∏i=1nxi A n^n \sum_{i=1}^n x_i + D \;=\; A D n^n + \frac{D^{\,n+1}}{n^n \prod_{i=1}^n x_i}

Unlike CPMM where k=∏ixik = \prod_i x_i has a closed form, here DD must be solved numerically for given balances. During swaps, DD remains constant, but the balances xix_i adjust.

Special Case: n = 2

For two assets (n=2n=2), the invariant becomes:

4A(x1+x2)+D  =  4AD+D34x1x2 4A(x_1 + x_2) + D \;=\; 4AD + \frac{D^3}{4 x_1 x_2}

This is the form implemented in Curve V1 two-coin pools. The equation is cubic in DD, so numerical methods (e.g. Newton iteration) are used in practice.

Conclusion

StableSwap generalizes CPMM by replacing the fixed product kk with a dynamic invariant DD. For given AA and nn, DD 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)=kxy(x) = \frac{k}{x}

and the price is simply the derivative:

Px→y=−dydx=yxP_{x \rightarrow y} = -\frac{dy}{dx} = \frac{y}{x}

In StableSwap (2 assets, n=2), the invariant is:

4A(x+y)+D=4AD+D34xy4A(x + y) + D = 4AD + \frac{D^3}{4xy}

This means y is implicitly defined by the function:

y=f(x;D,A)    such that the invariant holdsy = f(x; D, A) \;\; \text{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=−dydxP_{x \rightarrow y} = -\frac{dy}{dx}

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,…,xnx_1, x_2, \dots, x_n, we want to prove:

x1+x2+⋯+xnn  ≥  (x1x2⋯xn)1/n\frac{x_1 + x_2 + \cdots + x_n}{n} \;\geq\; (x_1 x_2 \cdots x_n)^{1/n}

We proceed by induction.

Base Case (n=2)

For n=2n=2, we must show:

x1+x22  ≥  x1x2\frac{x_1+x_2}{2} \;\geq\; \sqrt{x_1 x_2}

Squaring both sides gives:

(x1+x22)2≥x1x2\left(\frac{x_1+x_2}{2}\right)^2 \geq x_1 x_2

which simplifies to (x1−x2)2≥0(x_1 - x_2)^2 \geq 0. This is always true.

Inductive Step

Assume the inequality holds for some n=kn=k. Now consider n=k+1n=k+1.

Let y=xk+xk+12y = \frac{x_k + x_{k+1}}{2}. By the case n=2n=2:

y  ≥  xkxk+1y \;\geq\; \sqrt{x_k x_{k+1}}

Applying the inductive hypothesis to x1,…,xk−1,yx_1, \dots, x_{k-1}, y gives:

x1+⋯+xk−1+yk  ≥  (x1x2⋯xk−1y)1/k\frac{x_1+\cdots+x_{k-1}+y}{k} \;\geq\; (x_1 x_2 \cdots x_{k-1} y)^{1/k}

Substituting y≥xkxk+1y \geq \sqrt{x_k x_{k+1}} yields:

x1+⋯+xk+1k+1  ≥  (x1x2⋯xk+1)1/(k+1)\frac{x_1+\cdots+x_{k+1}}{k+1} \;\geq\; (x_1 x_2 \cdots x_{k+1})^{1/(k+1)}

Conclusion

Therefore, by induction, the inequality holds for all n≥2n \geq 2, with equality iff all xix_i are equal.

Proof: Ratio Behaviour Under Balance and Imbalance

Consider nn assets with balances x1,…,xnx_1, \dots, x_n. Define

R=∏i=1nxi(Dn)n,D=∑i=1nxiR = \frac{\prod_{i=1}^n x_i}{\left(\tfrac{D}{n}\right)^n}, \qquad D = \sum_{i=1}^n x_i

Balanced Case

If x1=x2=⋯=xn=D/nx_1 = x_2 = \dots = x_n = D/n, then

∏i=1nxi=(Dn)n\prod_{i=1}^n x_i = \left(\tfrac{D}{n}\right)^n

so R=1R = 1.

Imbalanced Case

By the AM–GM inequality,

Dn  ≥  (∏i=1nxi)1/n\frac{D}{n} \;\geq\; \left(\prod_{i=1}^n x_i\right)^{1/n}

with equality only if all xix_i are equal. Therefore,

0  ≤  R  ≤  10 \;\leq\; R \;\leq\; 1

and R<1R < 1 when the pool is imbalanced.

Extreme Imbalance

If one asset tends to zero, say x1→0x_1 \to 0, then ∏ixi→0\prod_i x_i \to 0 while (D/n)n(D/n)^n remains positive, so R→0R \to 0.

If one asset dominates, say x1→∞x_1 \to \infty with others fixed, then

(D/n)n∼(x1n)n,∏ixi∼x1⋅(x2⋯xn)(D/n)^n \sim \left(\tfrac{x_1}{n}\right)^n, \qquad \prod_i x_i \sim x_1 \cdot (x_2 \cdots x_n)
R∼nn⋅(x2⋯xn)x1 n−1    →0R \sim \frac{n^n \cdot (x_2 \cdots x_n)}{x_1^{\,n-1}} \;\;\to 0

Conclusion

The ratio RR satisfies R=1R=1 in perfect balance, 0≤R<10 \leq R < 1 under imbalance, and R→0R \to 0 for extreme imbalance. This property ensures that the StableSwap invariant smoothly interpolates between balanced and unbalanced states.