How to convert a price to a tick that can be initialized#

A tick that can be initialized must be evenly divisible by the pool's tick spacing. Converting a price into such a tick uses the following formula:

tick = floor( log(sqrtPriceRatio) / log(1.0001) )

To convert a price to a tick that can be initialized:

  1. Turn the price into a sqrtPrice. The sqrtPrice is the current price of the pool. To get it, work out the pool ratio using an example deposit: 1 unit of CTN (token1) and the amount of the stablecoin (token0) you want for the chosen range.

  2. Select a price.

    TickLow_stablecoin = 1000

    This represents 1,000 stablecoin for 1 CTN.

  3. Convert the price into its lowest decimal format.

    TickLowFormatted = TickLow_stablecoin * 10^(token0 decimals)

    In this example the stablecoin is token0 and has 6 decimals.

  4. Encode the sqrtPrice ratio. Take 1 CTN in its lowest denomination (10^18) and the formatted stablecoin price, and use them as the pool reserve ratio.

    Token1ReserveRatio = 1000000000000000000
    Token0ReserveRatio = 1000000000

    Divide the token1 amount by the token0 amount to get the sqrtPriceRatio.

    sqrtPriceRatio = Token1ReserveRatio / Token0ReserveRatio
  5. Get the tick at the sqrtPrice.

    tickForPrice = floor( log(sqrtPriceRatio) / log(1.0001) )
  6. After taking the rounded-down floor value and removing decimals, this gives the tick for the price (for example, 207243).

  7. Adjust the tick to the pool's tick spacing to get a valid tick that can be initialized. Divide the tick by the tick spacing, take the floor, then multiply back by the tick spacing.

    closestLowTick = floor( tick / tickSpacing ) * tickSpacing

    For the closest high tick, take the ceiling of tick / tickSpacing instead. This gives the closest tick range that includes the chosen price.