Position Value

Position Value

Calculate the current total value of your position including liquidity and accumulated fees.

Basic Value Calculation

// Get total position value
async function getPositionValue(
  pair: LBPair,
  positionId: string
) {
  // Get amounts from all bins (includes fees)
  const amounts = await sdk.Position.getPositionBinsAmount(pair, positionId);
  
  // Sum totals
  const totals = amounts.reduce((sum, bin) => ({
    x: sum.x + bin.amountX,
    y: sum.y + bin.amountY
  }), { x: 0n, y: 0n });
  
  return totals;
}

Calculate USD Value

// Convert to USD
async function getPositionValueUSD(
  pair: LBPair,
  positionId: string
) {
  const totals = await getPositionValue(pair, positionId);
  
  // Get current price
  const price = getPriceFromBinId(
    pair.parameters.active_id,
    Number(pair.binStep)
  );
  
  // Calculate USD value
  const valueUSD = 
    Number(totals.x) * price + // Token X value
    Number(totals.y);          // Token Y value
  
  return {
    tokenX: totals.x,
    tokenY: totals.y,
    totalUSD: valueUSD
  };
}

Quick Value Check

// One-liner value check
const amounts = await sdk.Position.getPositionBinsAmount(pair, positionId);
const total = amounts.reduce((sum, b) => ({
  x: sum.x + b.amountX,
  y: sum.y + b.amountY
}), { x: 0n, y: 0n });

What's Included

The value includes:

  • Original liquidity amounts

  • Accumulated trading fees

  • Current market prices

  • All bins in the position

Display Format

// Format for display
const value = await getPositionValueUSD(pair, positionId);

console.log("Position Value:", {
  tokenX: formatUnits(value.tokenX, 18),
  tokenY: formatUnits(value.tokenY, 6),
  totalUSD: `$${value.totalUSD.toFixed(2)}`
});

Track Changes

// Compare values over time
const before = await getPositionValueUSD(pair, positionId);

// ... later ...

const after = await getPositionValueUSD(pair, positionId);
const change = after.totalUSD - before.totalUSD;
const changePercent = (change / before.totalUSD) * 100;

console.log(`Change: $${change.toFixed(2)} (${changePercent.toFixed(2)}%)`);

Important Notes

  • Value changes with price movements

  • Includes all accumulated fees

  • No need for manual fee calculations

  • Updates in real-time with market

Last updated