1.4 KiB
1.4 KiB
Uniswap V3 Pricing Documentation
This document explains the Uniswap V3 pricing model and how it's implemented in the MEV bot.
Core Concepts
Ticks
In Uniswap V3, prices are represented as ticks. A tick is defined as:
tick = log√1.0001(√P)
Where P is the price of token1 in terms of token0.
Ticks are integers that range from -887272 to 887272.
sqrtPriceX96
Prices are stored internally as sqrtPriceX96, which is:
sqrtPriceX96 = √P * 2^96
This allows for precise fixed-point arithmetic without floating-point operations.
Liquidity
Liquidity in a pool is represented as a uint128 value that determines the depth of the pool at a given price.
Mathematical Relationships
Converting between representations
-
Tick to sqrtPriceX96:
sqrtPriceX96 = 1.0001^(tick/2) * 2^96 -
sqrtPriceX96 to Tick:
tick = log_1.0001(sqrtPriceX96 / 2^96)^2 -
Price to Tick:
tick = log_1.0001(price)
Implementation Details
Our Go implementation uses the math/big package for precision and handles the fixed-point arithmetic required for Uniswap V3 calculations.
Key functions:
SqrtPriceX96ToPrice: Converts sqrtPriceX96 to a floating-point pricePriceToSqrtPriceX96: Converts a floating-point price to sqrtPriceX96TickToSqrtPriceX96: Converts a tick to sqrtPriceX96SqrtPriceX96ToTick: Converts sqrtPriceX96 to a tick