3

I use IUniswapV3PoolState.slot0 to return sqrtPriceX96 and tick for different pairs in Uniswap V3. The results are very reasonable for ETH/DAI, but it's quite different for ETH/USDT and ETH/USDC.

This is not because of the order of the tokens, but rather the final result of price (after taking care of square root and Q96) differ by 10^(-12). So I would get ETH/DAI around $3200, while ETH/USDT and ETH/USDC will give $3200*10^(-12). Is there anything I'm missing? Thank you!

TylerH
  • 20,799
  • 66
  • 75
  • 101
Tian L
  • 49
  • 3
  • 2
    I’m voting to close this question because it appears to be about math/3rd party service implementation, not about programming. – TylerH May 23 '22 at 13:51

1 Answers1

7

EVM compatible blockchains use fixed-point math. The floating point values that you see in the UI are abstractions, technically everything is an integer; a specific number of digits reserved to represent the decimals. Different ERC-20 tokens reserve a different number of decimals.

  • WETH and DAI have 18 decimals
  • USDT and USDC have 6 decimals.

If you have asset X that has 6 and an asset Y that has 18 decimals, then the price of Y in terms of X has to be corrected for this fact.

Lets use price = y/x, then the price adjusted for the amount of decimals is going to be price_adjusted = y/x * 10^(-12). To see why refer to Section 3.3.2 here:

Price adjusted for decimal equation

Jasperan
  • 2,154
  • 1
  • 16
  • 40
kfx
  • 8,136
  • 3
  • 28
  • 52