DLMM SDK

This Typescript SDK is meant to help developers easily integrate Meteora's DLMM pools into their protocols. This SDK uses the latest AddLiquidityByStrategy endpoint (instead of AddLiquidityByWeight).

Getting started

List of sections

CPI call example

Access this repo in the SDK, import, CPI:

Rust examples

Access these repos in the SDK, import, and use the necessary functions:

Example codes for key DLMM actions

Below we have provided example codes for the key DLMM actions:

  • Getting the active bin

  • Creating a position

  • Getting the list of positions

  • Adding liquidity to an existing position

  • Removing liquidity from an existing position

  • Swapping tokens within a specified pool

We have also provided a list of the necessary static functions and DLMM instance functions. To view other SDK files on github, click here.

Main Steps for SDK testing

1. cd ts-client
2. anchor localnet -- --features localnet
3. pnpm run test

Install

  1. Install dependencies

    You will need to first install these SDK dependencies in order for it to function properly

npm i @meteora-ag/dlmm @coral-xyz/anchor @solana/web3.js
  1. Initialize DLMM instance

import DLMM from '@meteora-ag/dlmm'

const USDC_USDT_POOL = new PublicKey('ARwi1S4DaiTG5DX7S4M4ZsrXqpMD1MrTmbu9ue2tpmEq') // You can get your desired pool address from the API https://dlmm-api.meteora.ag/pair/all
const dlmmPool = await DLMM.create(connection, USDC_USDT_POOL);

// If you need to create multiple, can consider using `createMultiple`
const dlmmPool = await DLMM.createMultiple(connection, [USDC_USDT_POOL, ...]);
  1. To interact with the AmmImpl

    Changing Ammlmpl parameters to interact with the lb_pair / or pool

  • Get Active Bin

    To get the active bin where the current pool price resides

const activeBin = await dlmmPool.getActiveBin();
const activeBinPriceLamport = activeBin.price;
const activeBinPricePerToken = dlmmPool.fromPricePerLamport(
  Number(activeBin.price)
);
  • Create Position

    To create a new liquidity position

const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
const minBinId = activeBin.bin_id - TOTAL_RANGE_INTERVAL;
const maxBinId = activeBin.bin_id + TOTAL_RANGE_INTERVAL;

const activeBinPricePerToken = dlmmPool.fromPricePerLamport(
  Number(activeBin.price)
);
const totalXAmount = new BN(100);
const totalYAmount = totalXAmount.mul(new BN(Number(activeBinPricePerToken)));

// Create Position (Spot Balance deposit, Please refer ``example.ts` for more example)
const createPositionTx =
  await dlmmPool.initializePositionAndAddLiquidityByStrategy({
    positionPubKey: newBalancePosition.publicKey,
    user: user.publicKey,
    totalXAmount,
    totalYAmount,
    strategy: {
      maxBinId,
      minBinId,
      strategyType: StrategyType.SpotBalanced,
    },
  });

try {
  const createBalancePositionTxHash = await sendAndConfirmTransaction(
    connection,
    createPositionTx,
    [user, newBalancePosition]
  );
} catch (error) {}
  • Get list of positions

    To get the list of liquidity positions for a specified pair and pool

const { userPositions } = await dlmmPool.getPositionsByUserAndLbPair(
  user.publicKey
);
const binData = userPositions[0].positionData.positionBinData;
  • Add liquidity to existing position

    To add liquidity to an existing liquidity position

const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
const minBinId = activeBin.bin_id - TOTAL_RANGE_INTERVAL;
const maxBinId = activeBin.bin_id + TOTAL_RANGE_INTERVAL;

const activeBinPricePerToken = dlmmPool.fromPricePerLamport(
  Number(activeBin.price)
);
const totalXAmount = new BN(100);
const totalYAmount = totalXAmount.mul(new BN(Number(activeBinPricePerToken)));

// Add Liquidity to existing position
const addLiquidityTx = await dlmmPool.addLiquidityByStrategy({
  positionPubKey: newBalancePosition.publicKey,
  user: user.publicKey,
  totalXAmount,
  totalYAmount,
  strategy: {
    maxBinId,
    minBinId,
    strategyType: StrategyType.SpotBalanced,
  },
});

try {
  const addLiquidityTxHash = await sendAndConfirmTransaction(
    connection,
    addLiquidityTx,
    [user]
  );
} catch (error) {}
  • Remove Liquidity

    To remove liquidity from an existing liquidity position

const userPosition = userPositions.find(({ publicKey }) =>
  publicKey.equals(newBalancePosition.publicKey)
);
// Remove Liquidity
const binIdsToRemove = userPosition.positionData.positionBinData.map(
  (bin) => bin.binId
);
const removeLiquidityTx = await dlmmPool.removeLiquidity({
  position: userPosition.publicKey,
  user: user.publicKey,
  binIds: binIdsToRemove,
  liquiditiesBpsToRemove: new Array(binIdsToRemove.length).fill(
    new BN(100 * 100)
  ), // 100% (range from 0 to 100)
  shouldClaimAndClose: true, // should claim swap fee and close position together
});

try {
  for (let tx of Array.isArray(removeLiquidityTx)
    ? removeLiquidityTx
    : [removeLiquidityTx]) {
    const removeBalanceLiquidityTxHash = await sendAndConfirmTransaction(
      connection,
      tx,
      [user],
      { skipPreflight: false, preflightCommitment: "singleGossip" }
    );
  }
} catch (error) {}
  • Swap

    To swap tokens within the specified liquidity pool

const swapAmount = new BN(100);
// Swap quote
const swapYtoX = true;
const binArrays = await dlmmPool.getBinArrayForSwap(swapYtoX);
const swapQuote = await dlmmPool.swapQuote(
  swapAmount,
  swapYtoX,
  new BN(10),
  binArrays
);

// Swap
const swapTx = await dlmmPool.swap({
  inToken: dlmmPool.tokenX.publicKey,
  binArraysPubkey: swapQuote.binArraysPubkey,
  inAmount: swapAmount,
  lbPair: dlmmPool.pubkey,
  user: user.publicKey,
  minOutAmount: swapQuote.minOutAmount,
  outToken: dlmmPool.tokenY.publicKey,
});

try {
  const swapTxHash = await sendAndConfirmTransaction(connection, swapTx, [
    user,
  ]);
} catch (error) {}

Static functions

DLMM instance functions

Last updated