Slippage Protection

Set minimum output amounts to protect swaps from price movements between quote and execution. Essential for safe trading in volatile markets.

What is Slippage?

Slippage occurs when:

  • Price moves between quote and execution

  • Other trades consume liquidity

  • Network delays cause stale quotes

Protection ensures you receive at least a minimum amount.

Basic Implementation

// Calculate minimum output with slippage tolerance
function calculateMinimumOutput(
  expectedOutput: bigint,
  slippagePercent: number = 0.5 // 0.5% default
): bigint {
  const slippageFactor = 10000 - Math.floor(slippagePercent * 100);
  return (expectedOutput * BigInt(slippageFactor)) / 10000n;
}

// Usage
const expectedOut = await calculateSwapOutput(pair, amountIn, xtoy);
const minimumOut = calculateMinimumOutput(expectedOut, 1.0); // 1% slippage

Protected Swap Pattern

Dynamic Slippage

Slippage by Asset Type

User-Friendly Display

MEV Protection

Handling Failures

Best Practices

  1. Default Settings

    • 0.5% for normal conditions

    • 0.1% for stable pairs

    • 2-5% for volatile assets

  2. User Control

    • Allow manual adjustment

    • Show impact clearly

    • Warn on high settings

  3. Auto-adjust

    • Monitor recent volatility

    • Check liquidity depth

    • Consider trade size

Last updated