Price, tick index and sqrt price correlation calculation

What's difference between price, tick index and sqrt price?

Concept
Definition
Features
Application

Price

Token exchange ratio (e.g., token1/token0)

Continuous value, updates in real-time

Directly determines trading rates

Tick Index

Discretized price interval index

Spacing determined by fee tiers

Manages liquidity distribution ranges

Sqrt Price

Square root of price

Stored as fixed-point numbers (e.g., sqrtPriceX64)

Simplifies on-chain calculations, improves efficiency

Notes:

  1. Price: Represents the real-time ratio between two tokens (e.g., ETH/USDC).

  2. Tick Index:

    • Derived from P = 1.0001^i, where i is the Tick Index.

    • Liquidity can only be provided at discrete Ticks (e.g., multiples of 10).

  3. Sqrt Price:

    • Used for computational efficiency (avoids floating-point operations).

    • In Uniswap V3, stored as sqrtPriceX64 (Q64 fixed-point format).

1. Tick index to price

when you want to open position, you dont know how to set your tick_lower and tick_upper.

use /src/math/tick.ts TickMath.tickIndexToPrice()

import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const price = TickMath.tickIndexToPrice(tickIndex, decimalsA, decimalsB)

2.Price to tick index

use /src/math/tick.ts TickMath.priceToTickIndex()

import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const tickIndex = TickMath.priceToTickIndex(price, decimalsA, decimalsB)

3. Tick index to sqrt price x64

use /src/math/tick.ts TickMath.tickIndexToSqrtPriceX64()

import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'

const sqrtPriceX64 = TickMath.tickIndexToSqrtPriceX64(tickIndex)

4. Sqrt price x64 to tick index

use /src/math/tick.ts TickMath.sqrtPriceToTickIndex()

import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'

const tickIndex = TickMath.sqrtPriceX64ToTickIndex(sqrtPriceX64)

5. Price to sqrt price

use /src/math/tick.ts TickMath.priceToSqrtPriceX64()

import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const sqrtPriceX64 = TickMath.priceToSqrtPriceX64(price, decimalsA, decimalsB)

6. Sqrt price to price

use /src/math/tick.ts TickMath.sqrtPriceX64ToPrice()

import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const price = TickMath.sqrtPriceX64ToPrice(sqrtPriceX64, decimalsA, decimalsB)

7. Convert the tick index from i32 to u32

In the TS SDK, for easier display, we will convert u32 to i32. If you need to call the contract directly, you can also use the method below to convert i32 to u32.

import { asUintN } from '@cetusprotocol/cetus-sui-clmm-sdk'

const tickLowerI32 = -100
const tickLowerU32 = asUintN(BigInt(tickLowerI32)).toString()

const tickUpperI32 = 100
const tickUpperU32 = asUintN(BigInt(tickUpperI32)).toString()

Last updated