# Stable Math

Stable Math is designed to allow for swaps between any assets that have the same price, or are "pegged" to the same asset. The most common examples are stablecoins that track US Dollars (DAI, USDT, USDC), and assets that track the price of Bitcoin (WBTC, renBTC, sBTC). Prices are determined by the pool balances, the

*amplification parameter*, and amounts of the tokens that are being swapped.Developers can use the TypeScript math implementations used by the Smart Order router

There are also Python implementations in progress

- stableMath.py There are known bugs in this implementation. This warning will be removed when they are fixed.

Since the Stable Math equation is quite complex, determining the invariant,

$D$

, is typically done iteratively. For an example of how to do this, please refer to this function.$A \cdot n^n \cdot \sum{x_i} +D = A \cdot D \cdot n^n + { \frac{D^{n+1}}{{n}^{n}\cdot \prod{x_i} } }$

Where:

- $n$is the number of tokens
- $x_i$is is balance of token$i$
- $A$is the amplification parameter

Similar to determining the invariant, determining (out/in) amount given (in/out) amounts is also done iteratively. Both outGivenIn and inGivenOut use the same function, getTokenBalanceGivenInvariantAndAllOtherBalances.

$y^2 + (\frac{D}{An^n} + \sum_{j \neq out}{x'_j} - D)y -\frac{D^{n+1}}{An^{2n} \prod_{j \neq out}{x'_j}}= 0$

$a_{out} = x_{out} - x'_{out} = x_{out} - y$

Where:

- $x'_i$is the
**ending**amount of each token - $a_{out}$is the amount out
- $x_{out}$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

$y^2 + (\frac{D}{An^n} + \sum_{j \neq in}{x'_j} - D)y -\frac{D^{n+1}}{An^{2n} \prod_{j \neq in}{x'_j}}= 0$

$a_{in} = x'_{in} - x_{in} = y-x_{in}$

Where:

- $x'_i$is the
**ending**amount of each token - $a_{in}$is the amount in
- $x_{in}$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

Last modified 6mo ago