Pact
  • Introduction
    • About
    • Disclaimer
    • Terms and Conditions
  • Getting Started
    • Creating an Algorand Wallet
    • Getting Algorand's native tokens
    • Connecting your Wallet
  • How to
    • Swaps
    • Pools
      • Multiple Fee Tiers
      • Constant Product Market Maker (CPMM)
      • StableSwap Invariant Market Maker (SIMM)
      • Lending Pools
    • Smart Router
    • Analytics
  • FAQ
    • FAQs
  • Knowledge Base
    • Glossary
    • Partnerships
    • Calculation Methodologies
  • Help
    • Contact Us
    • Diagnosing Common Issues
  • Security
    • Security Overview
    • Bug Bounty Program
  • Miscellaneous
    • AMA Archive
Powered by GitBook
On this page
  • Exchange Rate Calculation
  • Asset Price Calculation
  1. Knowledge Base

Calculation Methodologies

PreviousPartnershipsNextContact Us

Last updated 2 years ago

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=priceSTABLE∗totalSTABLE∗10decimalsassettotalSTABLE∗10decimalsSTABLEprice_{asset} = \frac{price_{STABLE} * total_{STABLE} * 10 ^{decimals_{asset}}}{total_{STABLE} *10 ^{decimals_{STABLE}} }priceasset​=totalSTABLE​∗10decimalsSTABLE​priceSTABLE​∗totalSTABLE​∗10decimalsasset​​

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:

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 "-".

priceSTABLEprice_{STABLE}priceSTABLE​ is the price of STABLE (1$)

totalSTABLEtotal_{STABLE}totalSTABLE​ is a total number of STABLE in pool after SWAP

totalassettotal_{asset}totalasset​ is a total number of asset in pool after SWAP

decimalsassetdecimals_{asset}decimalsasset​ asset’s decimals

decimalsSTABLEdecimals_{STABLE}decimalsSTABLE​ STABLE’s decimals

priceA=priceB∗totalB∗10decimalsAtotalA∗10decimalsBprice_A=\frac{price_B*total_B * 10^{decimals_A}}{total_A * 10^{decimals_B}}priceA​=totalA​∗10decimalsB​priceB​∗totalB​∗10decimalsA​​

priceBprice_BpriceB​ is the price of B calculated from SWAP app call in STABLE/B pool

totalAtotal_AtotalA​ is a total number of A in pool after SWAP

totalBtotal_BtotalB​ is a total number of B in pool after SWAP

decimalsAdecimals_AdecimalsA​is A’s decimals

decimalsBdecimals_BdecimalsB​is B’s decimals