# 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.

```typescript
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

```typescript
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

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

### 4. Sqrt price x64 to tick index

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

### 5. Price to sqrt price

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

### 6. Sqrt price to price

```typescript
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

```typescript
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()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cetus-1.gitbook.io/cetus-developer-docs/developer/via-sdk-v2/sdk-modules/cetusprotocol-sui-clmm-sdk/utility-functions-for-handling-price-sqrt-price-and-tick-index.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
