Get up and running with the Saros Finance DLMM SDK in minutes. This guide covers installation, basic setup, and your first swap operation using the Dynamic Liquidity Market Maker.
Install the Saros Finance DLMM SDK using your preferred package manager:
npm install @saros-finance/dlmm-sdk
# or
yarn add @saros-finance/dlmm-sdk
Import the necessary classes and initialize the service:
import {
BIN_STEP_CONFIGS,
LiquidityBookServices,
MODE,
} from "@saros-finance/dlmm-sdk";
import { PublicKey, Transaction, Keypair } from "@solana/web3.js";
import {
LiquidityShape,
PositionInfo,
RemoveLiquidityType,
} from "@saros-finance/dlmm-sdk/types/services";
import {
createUniformDistribution,
findPosition,
getBinRange,
getMaxBinArray,
getMaxPosition,
} from "@saros-finance/dlmm-sdk/utils";
import bigDecimal from "js-big-decimal";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
const liquidityBookServices = new LiquidityBookServices({
mode: MODE.DEVNET, // or MODE.MAINNET
});
Before executing a swap, get a quote for the expected output:
const quoteData = await liquidityBookServices.getQuote({
amount: BigInt(1000000), // 1 USDC
isExactInput: true,
swapForY: true,
pair: new PublicKey("EwsqJeioGAXE5EdZHj1QvcuvqgVhJDp9729H5wjh28DD"),
tokenBase: new PublicKey("C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9"),
tokenQuote: new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
tokenBaseDecimal: 6,
tokenQuoteDecimal: 6,
slippage: 0.5
});
Use the quote data to create and execute the swap transaction:
const transaction = await liquidityBookServices.swap({
amount: quoteData.amount,
tokenMintX: new PublicKey("C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9"),
tokenMintY: new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
otherAmountOffset: quoteData.otherAmountOffset,
isExactInput: true,
swapForY: true,
pair: new PublicKey("EwsqJeioGAXE5EdZHj1QvcuvqgVhJDp9729H5wjh28DD"),
payer: new PublicKey("YOUR_WALLET_PUBLIC_KEY")
});
const signedTransaction = await signTransaction(transaction);
const signature = await liquidityBookServices.connection.sendRawTransaction(
signedTransaction.serialize(),
{
skipPreflight: true,
preflightCommitment: "confirmed",
}
);
const { blockhash, lastValidBlockHeight } = await liquidityBookServices.connection.getLatestBlockhash();
await liquidityBookServices.connection.confirmTransaction({
signature,
blockhash,
lastValidBlockHeight,
});
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.