Spot price in Uniswap V2
In Uniswap V2, the protocol tracks token reserves and derives the spot price, \( p_x = \frac{y}{x}, \)
, and total liquidity, \( L = xy, \), where \( x \) and \( y \) are the reserves of tokens X and Y.
Spot price in Uniswap V3
- Uniswap V3, instead, tracks the current price and liquidity, and derives the reserves.
- Uniswap V3 actually stores the square root of the price,\( \sqrt{p} \),instead of the price itself.
The relationship between \( \sqrt{p} \) and sqrtPriceX96
- To convert \( \sqrt{p} \) to sqrtPriceX96,
\[
\text{sqrtPriceX96} = \left\lfloor \sqrt{p} \times 2^{96} \right\rfloor
\]
- To convert back sqrtPriceX96 to \( \sqrt{p} \),
\[
\sqrt{p} = \frac{\text{sqrtPriceX96}}{2^{96}}
\]