Convert a Uniswap v3 square-root price into token display units
The pool exposes a fixed-point square root of the raw token ratio. A chart that squares it correctly but skips decimal conversion can be wrong by twelve orders of magnitude.
Start with pool order and integer precision
Read token0, token1 and slot0 at a single block. Compute sqrtPriceX96 squared divided by 2^192 using a wide integer or rational representation. The result is token1 raw units per token0 raw unit.
sqrtPriceX96 = 2^96
raw token1/token0 = 1
token0 decimals = 6; token1 decimals = 18
human token1 per token0 = 10^-12
Multiply by 10 raised to decimals0 minus decimals1 for display units. In the fixture one whole token0 corresponds to 10^-12 token1. Reverse the quote by inverting the resulting rational, and round only for presentation.
Separate a snapshot price from swap execution
slot0 describes the current marginal pool price. A finite swap crosses liquidity ranges and incurs fees, so a quote for a nontrivial amount requires actual swap math or a suitable simulation. Verify decimals separately for the specific token contracts. github.com UniswapV3Pool.sol