Pact
Search
K
Comment on page

Calculation Methodologies

Exchange Rate Calculation

We use the formula below to calculate the exchange rate:
rcv = a * B / (A + a)
rcv_net = rcv * (1 - fee)
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.
In terms of how much a user shall receive b given a supplied:
b = a * B / (A + a) * (1 - fee)

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:
  • 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:
priceasset=priceSTABLEtotalSTABLE10decimalsassettotalSTABLE10decimalsSTABLEprice_{asset} = \frac{price_{STABLE} * total_{STABLE} * 10 ^{decimals_{asset}}}{total_{STABLE} *10 ^{decimals_{STABLE}} }
priceSTABLEprice_{STABLE}
is the price of STABLE (1$)
totalSTABLEtotal_{STABLE}
is a total number of STABLE in pool after SWAP
totalassettotal_{asset}
is a total number of asset in pool after SWAP
decimalsassetdecimals_{asset}
asset’s decimals
decimalsSTABLEdecimals_{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:
priceA=priceBtotalB10decimalsAtotalA10decimalsBprice_A=\frac{price_B*total_B * 10^{decimals_A}}{total_A * 10^{decimals_B}}
priceBprice_B
is the price of B calculated from SWAP app call in STABLE/B pool
totalAtotal_A
is a total number of A in pool after SWAP
totalBtotal_B
is a total number of B in pool after SWAP
decimalsAdecimals_A
is A’s decimals
decimalsBdecimals_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 "-".