> For the complete documentation index, see [llms.txt](https://docs.gaming-dex.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gaming-dex.com/logic/stable-math.md).

# Stable Math

### **Overview**

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 -> ∞).

<figure><img src="/files/opX5X5ShHaVHeg8wiiOU" alt=""><figcaption></figcaption></figure>

### **Calculations**

#### **Invariant**

Since the equations for Stable Math are so complex, the invariant D is usually determined iteratively. This function demonstrates how this is done.

$$
A･n^n･\sum x\_i+D=A･D･n^n+ \cfrac {D^{n+1}}{n^n･\prod x\_i}
$$

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.

#### **outGivenIn**

$$
y^2+(\cfrac{D}{An^n}+\sum\_{j≠out}x'*j-D)y-\cfrac{D^{n+1}}{An^2\Pi*{j≠out}x'\_j}=0
$$

$$
a\_{out}=x\_{out}-x'*{out}=x*{out}-y
$$

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

#### **inGivenOut**

$$
y^2+(\cfrac{D}{An^n}+\sum\_{j≠in}x'*j-D)y-\cfrac{D^{n+1}}{An^2\Pi*{j≠in}x'\_j}=0
$$

$$
a\_{in}=x\_{in}-x'*{in}=x*{in}-y
$$

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gaming-dex.com/logic/stable-math.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
