Utility functions for handling price, sqrt price, and tick index

This document provides an overview of commonly used utility methods and helper functions in the Cetus SDK.

Core Concepts

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 don't know how to set your tick_lower and tick_upper.

import { TickMath } from '@cetusprotocol/common-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const price = TickMath.tickIndexToPrice(tick_index, decimals_a, decimals_b)

2. Price to tick index

import { TickMath } from '@cetusprotocol/common-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const price = TickMath.priceToTickIndex(tick_index, decimals_a, decimals_b)

3. Tick index to sqrt price x64

import { TickMath } from '@cetusprotocol/common-sdk'
const sqrt_price_x64 = TickMath.tickIndexToSqrtPriceX64(tick_index)

4. Sqrt price x64 to tick index

import { TickMath } from '@cetusprotocol/common-sdk'
const tick_index = TickMath.sqrtPriceX64ToTickIndex(sqrt_price_x64)

5. Price to sqrt price

import { TickMath } from '@cetusprotocol/common-sdk'
const sqrt_price_x64 = TickMath.priceToSqrtPriceX64(price, decimals_a, decimals_b)

6. Sqrt price to price

import { TickMath } from '@cetusprotocol/common-sdk'
const price = TickMath.sqrtPriceX64ToPrice(sqrt_price_x64, decimals_a, decimals_b)

7. Convert the tick index from i32 to u32

import { asUintN } from '@cetusprotocol/common-sdk'
const tick_lower_i32 = -100
const tick_lower_u32 = asUintN(BigInt(tick_lower_i32)).toString()

const tick_upper_i32 = 100
const tick_upper_u32 = asUintN(BigInt(tick_upper_i32)).toString()

Last updated