Magma Developer SDK
  • Getting started
  • Prerequisites
  • Features Available
  • Retrieve clmm pools
  • Retrieve positions
  • Retrieve reward
  • Retrieve ticks
  • Create clmm pool
  • Open position
  • Add liquidity
  • Remove liquidity
  • Close position
  • Collect fees
  • Collect rewards
  • Preswap
  • Swap
  • Partner swap
  • Smart router
  • Price impact
  • Minimum received & Maximum sold
  • Fee
  • Liquidity correlation calculation
  • APR correlation calculation
Powered by GitBook
On this page
  • 1. Liquidity to CoinAmount
  • Example
  • 2. Price to TickIndex
  • 3. Calculate price from sqrt price

Liquidity correlation calculation

1. Liquidity to CoinAmount

Method: ClmmPoolUtil.getCoinAmountFromLiquidity

Example

const pool = await sdk.Pool.getPool(poolAddress)

const position = await sdk.Position.getSimplePosition(positionAddress)

const lowerSqrtPrice = TickMath.tickIndexToSqrtPriceX64(position.tick_lower_index)
const upperSqrtPrice = TickMath.tickIndexToSqrtPriceX64(position.tick_upper_index)


const liquidity = new BN(position.liquidity)
const curSqrtPrice = new BN(pool.current_sqrt_price)

const lowerSqrtPrice = TickMath.tickIndexToSqrtPriceX64(position.tick_lower_index)
const upperSqrtPrice = TickMath.tickIndexToSqrtPriceX64(position.tick_upper_index)

const amounts = ClmmPoolUtil.getCoinAmountFromLiquidity(
  liquidity,
  curSqrtPrice,
  lowerSqrtPrice,
  upperSqrtPrice,
  false
)

const {coinA, coinB} = amounts

2. Price to TickIndex

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

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

// decimalsA and decimalsB means the decimal of coinA and coinB
const tick_lower = TickMath.priceToTickIndex(price, decimalsA, decimalsB)

3. Calculate price from sqrt price

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

const pool = await sdk.Pool.getPool(poolAddress)
const price = TickMath.sqrtPriceX64ToPrice(new BN(pool.current_sqrt_price))
PreviousFeeNextAPR correlation calculation

Last updated 4 months ago