Learn how to work with Dynamic Liquidity Market Maker pools using the Saros DLMM SDK.
This tutorial will guide you through the process of interacting with DLMM pools using the Saros DLMM SDK. DLMM provides concentrated liquidity and improved capital efficiency compared to traditional AMMs.
Before starting this tutorial, ensure you have the following:
// Install the required packages
npm install @saros-finance/dlmm-sdk @solana/web3.js
// Import the necessary modules
import { LiquidityBookServices, MODE } from "@saros-finance/dlmm-sdk";
import { PublicKey } from "@solana/web3.js";
First, we need to initialize the LiquidityBookServices with the appropriate network mode.
// Initialize the DLMM service
const liquidityBookServices = new LiquidityBookServices({
mode: MODE.DEVNET, // or MODE.MAINNET
});
// Define your wallet address (in a real app, this would come from a connected wallet)
const YOUR_WALLET = "YOUR_WALLET_PUBLIC_KEY";
Before executing a swap, get a quote to see the expected output amount and price impact.
// Get a quote for a swap operation
const quoteData = await liquidityBookServices.getQuote({
amount: BigInt(1000000), // 1 USDC (6 decimals)
isExactInput: true,
swapForY: true,
pair: new PublicKey("EwsqJeioGAXE5EdZHj1QvcuvqgVhJDp9729H5wjh28DD"), // Pool address
tokenBase: new PublicKey("C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9"), // Base token
tokenQuote: new PublicKey("EPjFWdd5AufqSSqeM2qN1zzybapC8G4wEGGkZwyTDt1v"), // Quote token
tokenBaseDecimal: 6,
tokenQuoteDecimal: 6,
slippage: 0.5 // 0.5% slippage tolerance
});
console.log('DLMM Quote:', {
amountIn: quoteData.amountIn.toString(),
amountOut: quoteData.amountOut.toString(),
priceImpact: quoteData.priceImpact,
});
Execute a swap using the quote data from the previous step.
// Execute a swap using the quote data
const transaction = await liquidityBookServices.swap({
amount: quoteData.amount,
tokenMintX: new PublicKey("C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9"), // Token X
tokenMintY: new PublicKey("EPjFWdd5AufqSSqeM2qN1zzybapC8G4wEGGkZwyTDt1v"), // Token Y
otherAmountOffset: quoteData.otherAmountOffset,
isExactInput: true,
swapForY: true,
pair: new PublicKey("EwsqJeioGAXE5EdZHj1QvcuvqgVhJDp9729H5wjh28DD"), // Pool address
payer: new PublicKey(YOUR_WALLET) // User's wallet
});
console.log('DLMM swap transaction created:', transaction);
liquidityBookServices.getQuote
to calculate the expected output amount and other parameters needed for the swap.liquidityBookServices.swap
to create the transaction for the token exchange.Manage liquidity positions in DLMM pools by adding or removing liquidity within specific price ranges.
// Example of position management in DLMM
// (This is a conceptual example as the actual API may vary)
// Add liquidity to a specific price range
const addLiquidityTx = await liquidityBookServices.addLiquidity({
pair: new PublicKey("EwsqJeioGAXE5EdZHj1QvcuvqgVhJDp9729H5wjh28DD"),
tokenXAmount: BigInt(1000000), // 1 token X
tokenYAmount: BigInt(2000000), // 2 token Y
binLower: -100, // Lower price bin
binUpper: 100, // Upper price bin
payer: new PublicKey(YOUR_WALLET)
});
// Remove liquidity from a position
const removeLiquidityTx = await liquidityBookServices.removeLiquidity({
position: positionPubkey, // Position NFT pubkey
liquidityBps: 5000, // 50% of liquidity (5000 bps)
payer: new PublicKey(YOUR_WALLET)
});
DLMM divides the price curve into discrete "bins" where liquidity is concentrated. Each bin represents a narrow price range.
Instead of spreading liquidity across the entire price curve, liquidity providers can concentrate their capital within specific price ranges.
Liquidity positions in DLMM are represented as NFTs, allowing for easy management and transfer of positions.
Concentrated liquidity leads to deeper markets within price ranges, resulting in reduced slippage for traders.