# Stable Math

## Overview

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.

## Implementations

### TypeScript

Developers can use the TypeScript math implementations used by the Smart Order router

## Invariant

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

Where:

is the number of tokens is is balance of token is the amplification parameter

## Trade Equations

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.

### outGivenIn

Where:

is the **ending**amount of each tokenis the amount out is the **starting**amount of the output tokenis the **ending**amount of the output tokenis the pool invariant is the amplification parameter is the number of tokens

### inGivenOut

Where:

is the **ending**amount of each tokenis the amount in is the **starting**amount of the input tokenis the **ending**amount of the input tokenis the pool invariant is the amplification parameter is the number of tokens