I'm calculating a UniswapV3 pool's token0 price in Node.js. Using UniswapV3 SDK gives correct result, but manual calculation isn't.
From the docs -
sqrtPriceX96 = sqrt(price) * 2 ** 96
# divide both sides by 2 ** 96
sqrtPriceX96 / (2 ** 96) = sqrt(price)
# square both sides
(sqrtPriceX96 / (2 ** 96)) ** 2 = price
# expand the squared fraction
(sqrtPriceX96 ** 2) / ((2 ** 96) ** 2) = price
# multiply the exponents in the denominator to get the final expression
sqrtRatioX96 ** 2 / 2 ** 192 = price
Now let's take pool WMATIC-USDC on Polygon, querying slot0
returns
sqrtPriceX96 uint160 : 76424446980909989196624
tick int24 : -277045
observationIndex uint16 : 4
observationCardinality uint16 : 64
observationCardinalityNext uint16 : 64
feeProtocol uint8 : 0
unlocked bool : true
Let's assign this value into the formula above -
token0Price = 76140828840970807549375^2 / 2^192 = 0.000000000000924
Which is wrong, it should be 0.9102578244
(calculated by UniswapV3 SDK).
Any idea what's wrong there?