Best Practices

Performance optimization tips for efficient DLMM interactions. Minimize gas usage, batch operations effectively, and implement robust patterns for production applications.

Gas Optimization

Batch Operations

// ❌ Bad: Multiple transactions
await sdk.Pair.removeLiquidity(pair, { positionId, binIds: [1] });
await sdk.Pair.removeLiquidity(pair, { positionId, binIds: [2] });
await sdk.Pair.removeLiquidity(pair, { positionId, binIds: [3] });

// ✅ Good: Single transaction
await sdk.Pair.removeLiquidity(pair, { 
  positionId, 
  binIds: [1, 2, 3] 
});

Minimize Bin Count

// Gas scales with number of bins
const gasEfficient = {
  // ❌ Excessive: 100 bins
  wide: Array.from({length: 101}, (_, i) => i - 50),
  
  // ✅ Efficient: Focused liquidity
  focused: Array.from({length: 21}, (_, i) => i - 10)
};

// Consider gas cost vs capital efficiency
function optimizeBinCount(expectedVolatility: number): number {
  if (expectedVolatility < 0.01) return 10;  // Stable
  if (expectedVolatility < 0.05) return 20;  // Normal
  return 30; // Volatile
}

Efficient Data Fetching

Cache Pair Data

Batch RPC Calls

Selective Data Loading

Smart Contract Interactions

Reuse Transactions

Optimize Call Data

Error Handling Patterns

Retry with Backoff

Circuit Breaker

Memory Management

Clean Up Large Objects

Stream Large Results

Production Checklist

Pre-deployment

Performance Monitoring

Key Principles

  1. Batch when possible - Reduce transaction count

  2. Cache frequently used data - Minimize RPC calls

  3. Validate early - Fail fast with clear errors

  4. Handle errors gracefully - Implement retries

  5. Monitor performance - Track and optimize

  6. Use appropriate data structures - Maps for lookups

  7. Clean up resources - Prevent memory leaks

Last updated