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:
-
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.
-
Select a price.
TickLow_stablecoin = 1000This represents 1,000 stablecoin for 1 CTN.
-
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.
-
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 = 1000000000Divide the token1 amount by the token0 amount to get the sqrtPriceRatio.
sqrtPriceRatio = Token1ReserveRatio / Token0ReserveRatio -
Get the tick at the sqrtPrice.
tickForPrice = floor( log(sqrtPriceRatio) / log(1.0001) ) -
After taking the rounded-down floor value and removing decimals, this gives the tick for the price (for example,
207243). -
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 ) * tickSpacingFor the closest high tick, take the ceiling of
tick / tickSpacinginstead. This gives the closest tick range that includes the chosen price.