could you help me figure out why the calculation of price using slot0 sqrtPriceX96 from UniswapV3 pool doesn't work for one of the tokens?
This code works correctly for several other tokens. Just doesn't work with this one. WETH and TOKEN have 18 decimal places.
Code:
const pairAddress = computePoolAddress({ factoryAddress: "0x1F98431c8aD98523631AE4a59f267346ea31F984", tokenA: TOKEN, tokenB: WETH, fee: FeeAmount.HIGH, // 1% }); const slot0 = await publicClient.readContract({ address: pairAddress, abi: ABI, functionName: "slot0", }); RESULT of slot = [6330088222351278055629588808447067n, 225781, 39, 100, 100, 0, true] // First return value from slot is sqrtPriceX96 const sqrtPriceX96 = slot0[0]; // Calculate price of Token in terms of WETH const priceInWETH = Number(sqrtPriceX96) ** 2 / 2 ** 192; // Convert to USD using WETH to USD price const tokenToUsdPrice = priceInWETH * 3537 USD (price of WETH at the time of writing); The tokenToUsdPrice should be = 0.0000005539$ The tokenToUsdPrice from calculation = 22506747214727.152