I'm looking at the uniswap docs which states this example:
An example of finding the price of WETH in a WETH / USDC pool, where
WETH is token0 and USDC is token1:You have an oracle reading that shows a return of tickCumulative as
[70_000, 1_070_000], with an elapsed time between the observations of
10 seconds.We can derive the average tick over this interval by taking the
difference in accumulator values (1_070_000 – 70_000 = 1_000_000), and
dividing by the time elapsed (1_000_000 / 10 = 100_000).With a tick reading of 100_000, we can find the value of token1 (USDC)
in terms of token0 (WETH) by using the current tick as i in the
formula p(i) = 1.0001**i (see 6.1 in the whitepaper).1.0001**100_000 ≅ 22015.5 USDC / WETH
The price of WETH is not $22015.50. I though maybe they just use an example with easy numbers. So I decided to try the example from the whitepaper on the USDC/WETH pool
Calling Slot0 on the Contract Returns:
Making the price
1.0001 ** 205930 = 876958666.4726943
Clearly the price for ETH is not 876958666, how do I get the usdc price of eth?
Best Answer
So the whitepaper is great, but doesn't get into Token Decimals, since tokens will have different decimals, this must be taken into account
for tick to price (which is imprecise compared to sqrtPrice)
Price from sqrtPriceX96
and here is a script in mjs form, but math is extrapolatable