@saros-finance/dlmm-sdkTypeScript

Quick Start Guide

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.

Prerequisites
  • • Node.js 16+ and npm/yarn
  • • Basic knowledge of TypeScript/JavaScript
  • • Solana wallet with some SOL for transaction fees
  • • Understanding of Solana's account model (helpful but not required)

1. Installation

Install the Saros Finance DLMM SDK using your preferred package manager:

Install SDK
npm install @saros-finance/dlmm-sdk
# or
yarn add @saros-finance/dlmm-sdk

2. Import and Initialize

Import the necessary classes and initialize the service:

Import SDK
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";

Initialize Service

Initialize DLMM Service
const liquidityBookServices = new LiquidityBookServices({
  mode: MODE.DEVNET, // or MODE.MAINNET
});

3. Get a Swap Quote

Before executing a swap, get a quote for the expected output:

Get Swap Quote
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
});

4. Execute Swap

Use the quote data to create and execute the swap transaction:

Execute Swap
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,
});
Understanding the Swap Process
1. Get Quote: First, we call liquidityBookServices.getQuote to calculate the expected output amount and other parameters needed for the swap.
2. Execute Swap: Then we use liquidityBookServices.swap to create the transaction for the token exchange.
3. Sign and Send: In a real implementation, you would sign the transaction with your wallet and send it to the network.
🎉 Congratulations!
You've successfully set up the Saros Finance DLMM SDK and performed your first swap.
Troubleshooting
Connection Issues: Make sure you have a stable internet connection and the Solana network is accessible.
Transaction Failures: Ensure your wallet has sufficient SOL for transaction fees and the tokens you're trying to swap.
Import Errors: Verify that you've installed all required dependencies and are using compatible versions.