Converting a Price to a Tick Podcast

Converting a Price to a Tick that can be Initialized¹ is done using the Following Formula:

Converting a price to a tick that can be initialized

Converting a Price to an Initializable Tick on Goblins

Converting a price to an initializable tick involves several steps. Here’s a detailed guide, including additional information and considerations:

Steps to Convert a Price to a Tick

  1. Turn the Price into a SqrtPrice²:
    • Calculate Ratio: Obtain the ratio value using 1 token of BCH (token1) and the desired price of USDT (token0). This example uses the pool ratio:
      • Example Price: TickLow_USDT = 1000 USDT for 1 BCH
  2. Select a Price:
    • Price Selection: Choose the desired price for the conversion.
  3. Convert Price to Lowest Decimal Format:
    • Decimal Conversion: Multiply the price by 10 to the power of the token0 decimals (USDT has 6 decimals).
      • Formatted Price: TickLowFormatted = TickLow_USDT * 10 to the 6th power
  4. Encode the SqrtPrice Ratio:
    • Token Reserve Ratios: Use the lowest decimal values of the tokens as the pool ratio reserves³.
      • Example Values:
        • Token1ReserveRatio (BCH) = 1 BCH in wei (10 to the 18th power)
        • Token0ReserveRatio (USDT) = Formatted USDT price (10 to the 9th power)
    • Calculate SqrtPriceRatio: Divide the token1 amount by the token0 amount.
      • Formula: sqrtPriceRatio = Token1ReserveRatio / Token0ReserveRatio
  5. Get the Tick at the SqrtPrice:
    • Calculate Tick: Use the logarithm of the sqrtPriceRatio with base 1.0001.
      • Formula: tickForPrice = floor(log(sqrtPriceRatio) / log(1.0001))
  6. Floor Value and Remove Decimals:
    • Tick Value: This step gives you the tick for the price after taking the rounded-down floor value and removing decimals.
      • Example Result: tickForPrice = 207243
  7. Adjust to Tick Spacing:
    • Valid Initializable Tick: Adjust the tick to the tick spacing of the pool to get a valid initializable tick.
      • Calculate Closest Low Tick: Divide the tick by the tick spacing, floor the value, then multiply by the tick spacing.
        • Formula: closestLowTick = floor(tickForPrice / tickSpacing) * tickSpacing
      • Calculate Closest High Tick: Take the ceiling of the tick divided by the tick spacing for the upper bound.
        • Formula: closestHighTick = ceil(tickForPrice / tickSpacing) * tickSpacing

Example Calculation

  1. Set Desired Price:
    • TickLow_USDT = 1000 USDT for 1 BCH
  2. Convert to Decimal Format:
    • TickLowFormatted = 1000 * 10 to the 6th power = 1,000,000,000
  3. Encode the SqrtPrice Ratio:
    • Token1ReserveRatio = 1,000,000,000,000,000,000 (1 BCH in wei)
    • Token0ReserveRatio = 1,000,000,000 (formatted USDT)
    • sqrtPriceRatio = 1,000,000,000,000,000,000 / 1,000,000,000 = 1,000,000
  4. Calculate Tick at SqrtPrice:
    • tickForPrice = floor(log(1,000,000) / log(1.0001)) = 207243
  5. Adjust to Tick Spacing:
    • Assume tickSpacing = 60
    • closestLowTick = floor(207243 / 60) * 60 = 207240
    • closestHighTick = ceil(207243 / 60) * 60 = 207300

Tips and Considerations

  1. Precision:
    • Decimals and Rounding: Ensure you handle decimal places and rounding correctly to avoid calculation errors.
  2. Pool Parameters:
    • Check Tick Spacing: Verify the tick spacing for the specific pool, as it varies by fee tier.
  3. Monitor Market Conditions:
    • Price Changes: Be aware of market conditions and how price changes may impact your tick calculations and liquidity positions.
  4. Use Tools:
    • Calculators and Scripts: Utilize available tools, calculators, or scripts to automate and verify your calculations for accuracy.

By following these steps and considering the additional tips, you can accurately convert a price to an initializable tick on the Goblins Protocol, ensuring efficient and effective liquidity management.


Footnotes:

  1. A tick that can be initialized must be evenly divisible by the tick spacing of the pool. Initialized ticks are used for liquidity positions.
  2. SqrtPrice is the current price of the pool.
  3. SqrtPriceRatio / pool ratio reserves. This is the ratio of tokens that would be in the pool. Imagine these two numbers as the initial liquidity deposit that sets the price of the pool.