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% slippageProtected Swap Pattern
Dynamic Slippage
Slippage by Asset Type
User-Friendly Display
MEV Protection
Handling Failures
Best Practices
Default Settings
0.5% for normal conditions
0.1% for stable pairs
2-5% for volatile assets
User Control
Allow manual adjustment
Show impact clearly
Warn on high settings
Auto-adjust
Monitor recent volatility
Check liquidity depth
Consider trade size
Related Topics
Calculate Swap Output - Get expected amounts
Price Impact - Understand trade effects
Execute Swap - Perform protected swaps
Last updated