TickMath

Git Source

Converts between prices and ticks, where each tick represents an increase in price of 0.01%. Ticks are used instead of liquidation prices to limit the number of possible buckets where a position can land, and allows for batched liquidations.

The formula for calculating the price from a tick is: price = 1.0001^(tick). The formula for calculating the tick from a price is: tick = log_1.0001(price).

State Variables

MIN_TICK

The minimum price we want to resolve is 10_000 wei (1e-14 USD), which equates to 1.0001^-322378.

int24 public constant MIN_TICK = -322_378;

MAX_TICK

The maximum tick is determined by the limits of the libraries used for math and testing.

int24 public constant MAX_TICK = 980_000;

MIN_PRICE

The minimum representable values for the price.

uint256 public constant MIN_PRICE = 10_000;

MAX_PRICE

The maximum representable values for the price.

uint256 public constant MAX_PRICE = 3_620_189_675_065_328_806_679_850_654_316_367_931_456_599_175_372_999_068_724_197;

LN_BASE

Pre-computed value for ln(1.0001).

int256 public constant LN_BASE = 99_995_000_333_308;

Functions

maxUsableTick

Gets the largest usable tick, given a tick spacing.

function maxUsableTick(int24 tickSpacing) external pure returns (int24 tick_);

Parameters

NameTypeDescription
tickSpacingint24Only uses ticks that are a multiple of this value.

Returns

NameTypeDescription
tick_int24The largest tick that can be used.

minUsableTick

Gets the smallest usable tick, given a tick spacing.

function minUsableTick(int24 tickSpacing) external pure returns (int24 tick_);

Parameters

NameTypeDescription
tickSpacingint24Only uses ticks that are a multiple of this value.

Returns

NameTypeDescription
tick_int24The smallest tick that can be used.

getPriceAtTick

Gets the price at a given tick.

Calculates the price as 1.0001^tick = e^(tick * ln(1.0001)).

function getPriceAtTick(int24 tick) public pure returns (uint256 price_);

Parameters

NameTypeDescription
tickint24The tick.

Returns

NameTypeDescription
price_uint256The corresponding price.

getTickAtPrice

Gets the tick corresponding to a price, rounded down towards negative infinity.

log_1.0001(price) = ln(price)/ln(1.0001) gives the tick.

function getTickAtPrice(uint256 price) external pure returns (int24 tick_);

Parameters

NameTypeDescription
priceuint256The price.

Returns

NameTypeDescription
tick_int24The largest tick whose price is less than or equal to the given price.

Errors

TickMathInvalidTickSpacing

The provided tick spacing is invalid (zero).

error TickMathInvalidTickSpacing();

TickMathInvalidTick

The provided tick is out of bounds.

error TickMathInvalidTick();

TickMathInvalidPrice

The provided price is out of bounds.

error TickMathInvalidPrice();