Converting a price to an initializable tick
Last modified:
A tick is "initializable" if it is evenly divisible by the pool's tick spacing.1 The following procedure converts a target price into the nearest valid tick.
1. Express the price as a ratio in smallest units
Use 1 unit of token1 in wei and the target price formatted in token0's smallest decimals.
TickLow_USDC = 1000 (target: 1,000 USDC per CTN)
TickLowFormatted = TickLow_USDC * 10^(decimals of token0)For USDC (token0, 6 decimals): TickLowFormatted = 1,000,000,000.
2. Compute the sqrtPrice ratio2
Token1ReserveRatio = 1000000000000000000 (1 CTN in wei)
Token0ReserveRatio = 1000000000 (1,000 USDC in 6-decimal units)
sqrtPriceRatio = Token1ReserveRatio / Token0ReserveRatio3. Derive the tick
tickForPrice = floor(log(sqrtPriceRatio) / log(1.0001))For this example the result is approximately 207,243.
4. Snap to the pool's tick spacing
Round down to the nearest multiple of the tick spacing:
closestLowTick = floor(tick / tickSpacing) * tickSpacingFor the upper bound, take the ceiling instead:
closestHighTick = ceil(tick / tickSpacing) * tickSpacingThis gives the tightest initializable range that contains the target price.
Footnotes
-
See the v3 whitepaper §6.1 for the formal definition of initializable ticks. ↩
-
sqrtPriceRatiorepresents the reserve ratio that would set the pool's spot price. Think of the two values as the initial deposit that establishes the price. See Tick Math for the full conversion formulas. ↩