# Calculation Methodologies

## **Exchange Rate Calculation**

We use the formula below to calculate the exchange rate:

`rcv = a * B / (A + a)`&#x20;

`rcv_net = rcv * (1 - fee)`&#x20;

where `a` is the amount of token X sent by swapper and `A` is the total amount of tokens X in the pool before the swap

Then, the exchange rate is simply `lim a -> 0 (rcv / a)` which equates to `B/A`.&#x20;

In terms of how much a user shall receive `b` given `a` supplied:

`b = a * B / (A + a) * (1 - fee)`&#x20;

## Asset Price Calculation

Internally, a fixed pricing algorithm is used to calculate the spot $USD price of assets swapped via the Pact AMM pools. To achieve this the algorithm tracks every SWAP pool call, distinguishing between swaps with:&#x20;

* Stablecoin Swaps (USDT or USDC) and;
* General Swaps (all other assets)

#### Stablecoin Swaps

For swaps with either USDT or USDC (let’s call them STABLE), it assumes the stablecoin has price of $1 and uses the following formula:

$$
price\_{asset} = \frac{price\_{STABLE} \* total\_{STABLE} \* 10 ^{decimals\_{asset}}}{total\_{STABLE}  \*10 ^{decimals\_{STABLE}} }
$$

$$price\_{STABLE}$$ is the price of STABLE (1$)&#x20;

$$total\_{STABLE}$$ is a total number of STABLE in pool after SWAP

$$total\_{asset}$$ is a total number of asset in pool after SWAP

$$decimals\_{asset}$$ asset’s decimals&#x20;

$$decimals\_{STABLE}$$ STABLE’s decimals

#### General Swaps

For swaps between other assets, the price will only be calculated if there has been a prior SWAP app call to the `STABLE/A` or `STABLE/B` pool, because in this case `A/B` already have an implicit price calculated. From here, the formula is similar:

$$
price\_A=\frac{price\_B\*total\_B \* 10^{decimals\_A}}{total\_A \* 10^{decimals\_B}}
$$

$$price\_B$$ is the price of `B` calculated from SWAP app call in `STABLE/B` pool

$$total\_A$$ is a total number of `A` in pool after SWAP

$$total\_B$$ is a total number of `B` in pool after SWAP

$$decimals\_A$$is `A`’s decimals

$$decimals\_B$$is `B`’s decimals

Note: If there are no `STABLE/A` or `STABLE/B` pools, then the price won't be calculated and the value will be set to "-".


---

# Agent Instructions: 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.pact.fi/pact/knowledge-base/calculation-methodologies.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.
