For the complete documentation index, see llms.txt. This page is also available as Markdown.

Price, Bin ID and Sqrt Price

The SDK provides comprehensive utility functions for working with bins and prices through the BinUtils class:

import { BinUtils } from '@cetusprotocol/dlmm-sdk'

// Convert price to bin ID
const binId = BinUtils.getBinIdFromPrice(
  '1040.07',  // price
  2,          // bin step
  true,       // is base coin A
  6,          // decimals for coin A
  9           // decimals for coin B
)

// Convert bin ID to price
const price = BinUtils.getPriceFromBinId(
  -4787,      // bin ID
  2,          // bin step
  6,          // decimals for coin A
  9           // decimals for coin B
)

// Get Q price from bin ID
const q_price = BinUtils.getQPriceFromId(
  -4400,      // bin ID
  100         // bin step
)

// Get price per lamport from Q price
const price_per_lamport = BinUtils.getPricePerLamportFromQPrice(q_price)

// Get liquidity from amounts
const liquidity = BinUtils.getLiquidity('0', '266666', '18431994054197767090')

// Get amount A from liquidity
const amountA = BinUtils.getAmountAFromLiquidity('4101094304427826916657468', '18461505896777422276')

// Get amount B from liquidity
const amountB = BinUtils.getAmountBFromLiquidity('4919119455159831291232256')


// Split bin liquidity info
const split_bin_infos = BinUtils.splitBinLiquidityInfo(bin_infos, 0, 70)

// Get position count between bin ranges
const positionCount = BinUtils.getPositionCount(-750, 845)

// Find min/max bin ID for a given bin step
const { minBinId, maxBinId } = BinUtils.findMinMaxBinId(10)

These utility functions are particularly useful when:

  • Setting up price ranges for liquidity positions

  • Calculating optimal bin ranges for trading strategies

  • Converting between different price representations

  • Managing liquidity distributions across bins

  • Analyzing position density and distribution

Last updated