Skip to main content
Helpful?

TickMath

Git Source - Generated with forge doc

Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports prices between 2-128 and 2128

State Variables

MIN_TICK

The minimum tick that may be passed to #getSqrtPriceAtTick computed from log base 1.0001 of 2**-128

If ever MIN_TICK and MAX_TICK are not centered around 0, the absTick logic in getSqrtPriceAtTick cannot be used

int24 internal constant MIN_TICK = -887272;

MAX_TICK

The maximum tick that may be passed to #getSqrtPriceAtTick computed from log base 1.0001 of 2**128

If ever MIN_TICK and MAX_TICK are not centered around 0, the absTick logic in getSqrtPriceAtTick cannot be used

int24 internal constant MAX_TICK = 887272;

MIN_TICK_SPACING

The minimum tick spacing value drawn from the range of type int16 that is greater than 0, i.e. min from the range [1, 32767]

int24 internal constant MIN_TICK_SPACING = 1;

MAX_TICK_SPACING

The maximum tick spacing value drawn from the range of type int16, i.e. max from the range [1, 32767]

int24 internal constant MAX_TICK_SPACING = type(int16).max;

MIN_SQRT_PRICE

The minimum value that can be returned from #getSqrtPriceAtTick. Equivalent to getSqrtPriceAtTick(MIN_TICK)

uint160 internal constant MIN_SQRT_PRICE = 4295128739;

MAX_SQRT_PRICE

The maximum value that can be returned from #getSqrtPriceAtTick. Equivalent to getSqrtPriceAtTick(MAX_TICK)

uint160 internal constant MAX_SQRT_PRICE = 1461446703485210103287273052203988822378723970342;

MAX_SQRT_PRICE_MINUS_MIN_SQRT_PRICE_MINUS_ONE

A threshold used for optimized bounds check, equals MAX_SQRT_PRICE - MIN_SQRT_PRICE - 1

uint160 internal constant MAX_SQRT_PRICE_MINUS_MIN_SQRT_PRICE_MINUS_ONE =
1461446703485210103287273052203988822378723970342 - 4295128739 - 1;

Functions

maxUsableTick

Given a tickSpacing, compute the maximum usable tick

function maxUsableTick(int24 tickSpacing) internal pure returns (int24);

minUsableTick

Given a tickSpacing, compute the minimum usable tick

function minUsableTick(int24 tickSpacing) internal pure returns (int24);

getSqrtPriceAtTick

Calculates sqrt(1.0001^tick) * 2^96

Throws if |tick| > max tick

function getSqrtPriceAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96);

Parameters

NameTypeDescription
tickint24The input tick for the above formula

Returns

NameTypeDescription
sqrtPriceX96uint160A Fixed point Q64.96 number representing the sqrt of the price of the two assets (currency1/currency0) at the given tick

getTickAtSqrtPrice

Calculates the greatest tick value such that getSqrtPriceAtTick(tick) <= sqrtPriceX96

Throws in case sqrtPriceX96 < MIN_SQRT_PRICE, as MIN_SQRT_PRICE is the lowest value getSqrtPriceAtTick may ever return.

function getTickAtSqrtPrice(uint160 sqrtPriceX96) internal pure returns (int24 tick);

Parameters

NameTypeDescription
sqrtPriceX96uint160The sqrt price for which to compute the tick as a Q64.96

Returns

NameTypeDescription
tickint24The greatest tick for which the getSqrtPriceAtTick(tick) is less than or equal to the input sqrtPriceX96

Errors

InvalidTick

Thrown when the tick passed to #getSqrtPriceAtTick is not between MIN_TICK and MAX_TICK

error InvalidTick(int24 tick);

InvalidSqrtPrice

Thrown when the price passed to #getTickAtSqrtPrice does not correspond to a price between MIN_TICK and MAX_TICK

error InvalidSqrtPrice(uint160 sqrtPriceX96);
Helpful?