Stable Math
Last updated
Last updated
Stable Math is designed to allow Swaps between any assets that have the same price or are "pegged" to the same asset. The most common examples include stable coins that track the U.S. dollar (DAI, USDT, USDC) and assets that track the price of bitcoin (WBTC, renBTC, sBTC). The price is determined by the pool balance, amplification parameters, and the amount of tokens that are being swapped.
In an ideal scenario, it makes sense to allow one-to-one trading of these assets. This would result in a Constant Sum curve. Enforcing trading regulations for uncorrelated assets makes sense in the worst-case scenario, where one or more of these assets lose their pegs and their values diverge. This will be a Constant Product curve, like the one in the Weighted Math.
The Stable Math curve is intended to combine the Constant Sum and Constant Product curves to enable nearly one-for-one trades that generate significant price changes only when token balances differ significantly, since the majority of cases are uneventful. The amplification factor parameter defines the degree to which the Stable Math curve approximates the Constant Product curve (when A = 0) or the Constant Sum curve (A -> ∞).
Since the equations for Stable Math are so complex, the invariant D is usually determined iteratively. This function demonstrates how this is done.
n : is the number of tokens xi : is the balance of token i A : is the amplification parameter
Iteratively determining (out/in) quantities from (in/out) quantities is similar to determining invariants. outGivenIn and inGivenOut both use the same function, getTokenBalanceGivenInvariantAndAllOtherBalances.
x’i : is the ending amount of each token aout : is the amount out xout : is the starting amount of the output token y=x’out : is the ending amount of the output token D : is the pool invariant A : is the amplification parameter n : is the number of tokens
x’i : is the ending amount of each token ain : is the amount in xin : is the starting amount of the input token y=x’in : is the ending amount of the input token D : is the pool invariant A : is the amplification parameter n : is the number of tokens