Price Impact

Calculate how your swap affects price as it consumes liquidity across multiple bins. Essential for understanding large trade costs and market impact.

What is Price Impact?

Price impact measures the difference between:

  • Spot Price: Current price at active bin

  • Execution Price: Average price after swap

Small swap:  Uses 1 bin   → Low impact
Large swap:  Uses 5 bins  → High impact

Basic Impact Calculation

async function calculatePriceImpact(
  pair: LBPair,
  amountIn: bigint,
  xtoy: boolean
): Promise<number> {
  // Get spot price
  const spotPrice = getPriceFromBinId(
    pair.parameters.active_id,
    Number(pair.binStep)
  );
  
  // Calculate output
  const amountOut = await calculateSwapOutput(pair, amountIn, xtoy);
  
  // Calculate execution price
  const executionPrice = xtoy
    ? Number(amountOut) / Number(amountIn)
    : Number(amountIn) / Number(amountOut);
  
  // Price impact percentage
  return ((executionPrice - spotPrice) / spotPrice) * 100;
}

Bin-by-Bin Analysis

Impact Thresholds

Depth Chart Analysis

Optimal Trade Size

Real-Time Display

Key Insights

  • Linear in bins: Each bin adds similar impact

  • Exponential in size: Doubling size > doubles impact

  • Direction matters: Buy/sell impacts differ

  • Time sensitive: Impact changes with liquidity

Last updated