Skip to main content

pricing.ts

path: /src/utils/pricing.ts

WETH_ADDRESS

- type: string
- value: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
Address of wrapped-ETH (WETH) contract on ethereum mainnet.

Referenced at:

  1. WHITELIST_TOKENS
  2. findEthPerToken()

USDC_WETH_03_POOL

- type: string
- value: '0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8'

Address of Uniswap V3 pool contract between USDC and WETH ERC-20 tokens on the specific chain.

Referenced at:

  1. getEthPriceInUSD

DAI_WETH_03_POOL

- type: string
- value: '0x03af20bdaaffb4cc0a521796a223f7d85e2aac31'

Address of Uniswap V3 pool contract between DAI and WETH ERC-20 tokens on optimsim mainnet.

Referenced at:

  1. getEthPriceInUSD

WHITELIST_TOKENS

A list of tokens which have considerable usage and are likely to have pool pairing with other tokens. These can be used for calculating liquidity in USD by using the tokens price in USD.

The following token addresses are present in the list:

* -> The value is imported from a variable and listed directly in the list declaration.

STABLE_COINS

A list of ERC20 token contract addresses which have stable coin prices, i.e., 1 token expected to be valued at 1 USD.

MINIMUM_ETH_LOCKED

 - type: BigDecimal
- value: 60

While calculating token price in USD, the value of other token locked in the pool in terms of eth has to be greated than MINIMUM_ETH_LOCKED.

Referenced at:

  1. findEthPertoken()

sqrtPriceX96ToTokenPrices()

Params:
- sqrtPriceX96 (BigInt): The square root of the price of token1 in terms of token0 in Q64.96 format. Formula: sqrt(token0.price/token1.price)*(2^96)
- token0 (Token): The first token in the pool pair to calculate the relative price for
- token1 (Token): The second token in the pool pair to calculate the relative price for

ReturnType: BigDecimal[]

Find the price of token0 and token1 in the pool relative to each other and returns the two prices.

Formula:

    num = (sqrtPriceX96^2) # Squaring the root to get the price
denom = 2^192 # To divide price by 96^2 to convert the Q64.96 number to BigDecimal
price1 = ((num/denom) * (10^token0.decimals))/ (10^token1.decimals) # Calculating price1
price0 = 1/price1

Dependencies:

  1. exponentToBigDecimal()
  2. safeDiv()

Invoked at:

  1. handleSwap()

getEthPriceInUSD()

Params: none

ReturnType: BigDecimal

Returns the Price of ETH in terms of USD, based on the stable coin pools. Currently, the token0Price for the pool represented by USDC_WETH_03_POOL. When pool entity is not found, returns ZERO_BD.

Entites:

  1. Pool - Read Entity

Dependencies:

  1. USDC_WETH_03_POOL
  2. ZERO_BD

Invoked at:

  1. handleInitialize()
  2. handleSwap()

findEthPerToken()

Params:
- token (Token): Token entity to find the price in terms of ETH

ReturnType: BigDecimal

If token is weth, returns 1. If token in STABLE_COINS, returns 1/bundle.ethPriceUSD.

Else, iterates over all the whitelisted pools for the token using token.whitelistPools. Finds the pool with largest liquidity value in terms of ETH, as long as the value is atleast MINIMUM_ETH_LOCKED. Uses the eth value of the paired token and relative token price between the token pair to find the token's' value in terms of eth. If there's no whitelisted pool with MINIMUM_ETH_LOCKED, returns ZERO_BD.

Entites:

  1. Bundle - Read Entity
  2. Pool - Read Entity
  3. Token - Read Entity

Dependencies:

  1. WETH_ADDRESS
  2. ONE_BD
  3. ZERO_BD
  4. STABLE_COINS
  5. MINIMUM_ETH_LOCKED

Invoked at:

  1. handleInitialize()
  2. handleSwap()

getTrackedAmountUSD()

Params:
- tokenAmount0 (BigDecimal):
- token0 (Token0):
- tokenAmount1 (BigDecimal):
- token1 (Token):

ReturnType: BigDecimal

Returns the USD value equivalent to tokenAmoun0 and tokenAmount1 together. Calculates the USD price using token.derviedEth*bundle.ethPriceUSD as the multiplier if the token is present in WHITELIST_TOKENS. If both the tokens are present, it adds their individual USD prices. If only one is present, it uses 2X the value of that token. If neither are in the WHITELIST_TOKENS list, returns ZERO_BD.

Entites:

  1. Bundle - Read Entity

Dependencies:

  1. WHITELIST_TOKENS
  2. ZERO_BD

Invoked at:

  1. handleSwap()