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.

TickLowUSDC=1000(target: 1,000 USDC per CTN)\text{TickLow}_{\text{USDC}} = 1000 \qquad (\text{target: } 1{,}000\ \text{USDC per CTN}) TickLowFormatted=TickLowUSDC10d0\text{TickLowFormatted} = \text{TickLow}_{\text{USDC}} \cdot 10^{d_0}

For USDC (token0, d0=6d_0 = 6):

TickLowFormatted=1000106=1,000,000,000.\text{TickLowFormatted} = 1000 \cdot 10^6 = 1{,}000{,}000{,}000.

2. Compute the sqrtPrice ratio2

Token1ReserveRatio=1018(1 CTN in wei)\text{Token1ReserveRatio} = 10^{18} \qquad (\text{1 CTN in wei}) Token0ReserveRatio=109(1,000 USDC in 6-decimal units)\text{Token0ReserveRatio} = 10^9 \qquad (\text{1,000 USDC in 6-decimal units}) sqrtPriceRatio=Token1ReserveRatioToken0ReserveRatio=1018109=109\text{sqrtPriceRatio} = \frac{\text{Token1ReserveRatio}}{\text{Token0ReserveRatio}} = \frac{10^{18}}{10^9} = 10^9

3. Derive the tick

tickForPrice=log(sqrtPriceRatio)log(1.0001)\text{tickForPrice} = \left\lfloor \frac{\log(\text{sqrtPriceRatio})}{\log(1.0001)} \right\rfloor

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=ticktickSpacingtickSpacing\text{closestLowTick} = \left\lfloor \frac{\text{tick}}{\text{tickSpacing}} \right\rfloor \cdot \text{tickSpacing}

For the upper bound, take the ceiling instead:

closestHighTick=ticktickSpacingtickSpacing\text{closestHighTick} = \left\lceil \frac{\text{tick}}{\text{tickSpacing}} \right\rceil \cdot \text{tickSpacing}

This gives the tightest initializable range that contains the target price.


Footnotes

  1. See the v3 whitepaper §6.1 for the formal definition of initializable ticks.

  2. sqrtPriceRatio\text{sqrtPriceRatio} represents 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.