1.2.3 • Published 9 months ago

@meteora-ag/dlmm v1.2.3

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

DLMM SDK

Getting started

NPM: https://www.npmjs.com/package/@meteora-ag/dlmm

SDK: https://github.com/MeteoraAg/dlmm-sdk

Discord: https://discord.com/channels/841152225564950528/864859354335412224

Install

  1. Install deps
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
  • Get Active Bin
const activeBin = await dlmmPool.getActiveBin();
const activeBinPriceLamport = activeBin.price;
const activeBinPricePerToken = dlmmPool.fromPricePerLamport(
  Number(activeBin.price)
);
  • Create 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
const { userPositions } = await dlmmPool.getPositionsByUserAndLbPair(
  user.publicKey
);
const binData = userPositions[0].positionData.positionBinData;
  • Add liquidity to existing 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
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
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

FunctionDescriptionReturn
createGiven the DLMM address, create an instance to access the state and functionsPromise<DLMM>
createMultipleGiven a list of DLMM addresses, create instances to access the state and functionsPromise<Array<DLMM>>
getAllLbPairPositionsByUserGiven a list of DLMM addresses, create instances to access the state and functionsPromise<Map<string, PositionInfo>>

DLMM instance functions

FunctionDescriptionReturn
refetchStatesUpdate onchain state of DLMM instance. It's recommend to call this before interact with the program (Deposit/ Withdraw/ Swap)Promise<void>
getBinArraysRetrieves List of Bin ArraysPromise<BinArrayAccount[]>
getBinArrayForSwapRetrieves List of Bin Arrays for swap purposePromise<BinArrayAccount[]>
getFeeInfoRetrieves LbPair's fee info including base fee, protocol fee & max feeFeeInfo
getDynamicFeeRetrieves LbPair's dynamic feeDecimal
getBinsAroundActiveBinretrieves a specified number of bins to the left and right of the active bin and returns them along with the active bin ID.Promise<{ activeBin: number; bins: BinLiquidity[] }>
getBinsBetweenMinAndMaxPriceRetrieves a list of bins within a specified pricePromise<{ activeBin: number; bins: BinLiquidity[] }>
getBinsBetweenLowerAndUpperBoundretrieves a list of bins between a lower and upper bin ID and returns the active bin ID and the list of bins.Promise<{ activeBin: number; bins: BinLiquidity[] }>
toPricePerLamportConverts a real price of bin to lamport pricestring
fromPricePerLamportconverts a price per lamport value to a real price of binstring
getActiveBinRetrieves the active bin ID and its corresponding pricePromise<{ binId: number; price: string }>
getPriceOfBinByBinIdGet the price of a bin based on its bin IDstring
getBinIdFromPriceget bin ID based on a given price and a boolean flag indicating whether to round down or up.number
getPositionsByUserAndLbPairRetrieves positions by user and LB pair, including active bin and user positions.Promise<{ activeBin: { binId: any; price: string; }; userPositions: Array<Position>;}>
initializePositionAndAddLiquidityByStrategyInitializes a position and adds liquidityPromise<Transaction\|Transaction[]>
addLiquidityByStrategyAdd liquidity to existing positionPromise<Transaction\|Transaction[]>
removeLiquidityfunction is used to remove liquidity from a position, with the option to claim rewards and close the position.Promise<Transaction\|Transaction[]>
closePositionCloses a positionPromise<Transaction\|Transaction[]>
swapQuoteQuote for a swapSwapQuote
swapSwap token within the LbPairPromise<Transaction>
claimLMRewardClaim rewards for a specific position owned by a specific ownerPromise<Transaction>
claimAllLMRewardsClaim all liquidity mining rewards for a given owner and their positions.Promise<Transaction[]>
claimSwapFeeClaim swap fees for a specific position owned by a specific ownerPromise<Transaction>
claimAllSwapFeeClaim swap fees for multiple positions owned by a specific ownerPromise<Transaction>
claimAllRewardsClaim swap fees and LM rewards for multiple positions owned by a specific ownerPromise<Transaction[]>
syncWithMarketPriceSync the pool current active bin to match nearest market price binPromise<Transaction>
getPairPubkeyIfExistsGet existing pool address given parameter, if not return nullPromise<PublicKey \| null>
getMaxPriceInBinArraysGet max price of the last bin that has liquidity given bin arraysPromise<string \| null>
1.2.4-rc2.0

9 months ago

1.2.3

9 months ago

1.0.44-rc.0

1 year ago

1.0.44-rc.2

1 year ago

1.0.44-rc.1

1 year ago

1.0.4-9.rc.1

1 year ago

1.0.43-rc.0

1 year ago

1.0.39-rc.9

1 year ago

1.0.39-rc.4

1 year ago

1.0.39-rc.3

1 year ago

1.0.39-rc.2

1 year ago

1.0.39-rc.1

1 year ago

1.0.39-rc.8

1 year ago

1.0.39-rc.7

1 year ago

1.0.39-rct.0

1 year ago

1.0.39-rc.6

1 year ago

1.0.39-rc.5

1 year ago

1.2.2-rc.1

10 months ago

1.2.2-rc.0

10 months ago

1.0.42-rc.0

1 year ago

1.0.39-rc.0

1 year ago

1.0.46-rc.0

1 year ago

1.1.3-rc.0

11 months ago

1.1.3-rc.1

11 months ago

1.2.2

10 months ago

1.2.1

11 months ago

1.0.50-rc.0

1 year ago

1.0.50-rc.1

1 year ago

1.0.50-rc.2

1 year ago

1.0.56-rc.0

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.0.40

1 year ago

1.0.44

1 year ago

1.0.43

1 year ago

1.0.42

1 year ago

1.0.41

1 year ago

1.0.48

1 year ago

1.0.47

1 year ago

1.0.46

1 year ago

1.0.45

1 year ago

1.0.41-rc.1

1 year ago

1.0.41-rc.0

1 year ago

1.0.51

12 months ago

1.0.50

12 months ago

1.0.55-rc.0

11 months ago

1.0.55

11 months ago

1.0.54

11 months ago

1.0.53

11 months ago

1.0.39-rc.32

1 year ago

1.0.39-rc.33

1 year ago

1.0.39-rc.34

1 year ago

1.0.39-rc.35

1 year ago

1.0.39-rc.36

1 year ago

1.0.39-rc.37

1 year ago

1.0.39-rc.30

1 year ago

1.0.52-rc.8

11 months ago

1.0.39-rc.29

1 year ago

1.0.52-rc.9

11 months ago

1.0.52-rc.10

11 months ago

1.0.39-rc.21

1 year ago

1.0.39-rc.22

1 year ago

1.0.39-rc.23

1 year ago

1.0.39-rc.24

1 year ago

1.0.39-rc.25

1 year ago

1.0.39-rc.26

1 year ago

1.0.39-rc.27

1 year ago

1.0.39-rc.28

1 year ago

1.0.39-rc.20

1 year ago

1.0.52-rc.6

11 months ago

1.0.52-rc.7

11 months ago

1.0.39-rc.18

1 year ago

1.0.39-rc.19

1 year ago

1.0.49-rc.0

1 year ago

1.0.49-rc.1

1 year ago

1.0.49-rc.2

1 year ago

1.0.39-rc.10

1 year ago

1.0.39-rc.11

1 year ago

1.0.39-rc.12

1 year ago

1.0.39-rc.13

1 year ago

1.0.39-rc.14

1 year ago

1.0.39-rc.15

1 year ago

1.0.39-rc.16

1 year ago

1.0.39-rc.17

1 year ago

1.0.47-rc.2

1 year ago

1.0.47-rc.0

1 year ago

1.0.47-rc.1

1 year ago

1.0.38

1 year ago

1.0.38-rc.0

1 year ago

1.0.37

1 year ago

1.0.37-rc.0

1 year ago

1.0.36-rc.1

1 year ago

1.0.35-rc.7

1 year ago

1.0.35-rc.8

1 year ago

1.0.35-rc.5

1 year ago

1.0.35-rc.6

1 year ago

1.0.35-rc.3

1 year ago

1.0.35-rc.4

1 year ago

1.0.35-rc.2

1 year ago

1.0.36

1 year ago

1.0.35-rc.0

1 year ago

1.0.35

1 year ago

1.0.34-rc.0

1 year ago

1.0.33

1 year ago

1.0.34

1 year ago

1.0.32-rc.0

1 year ago

1.0.32-rcc.2

1 year ago

1.0.32-rcc.1

1 year ago

1.0.32-rcc.5

1 year ago

1.0.32-rcc.4

1 year ago

1.0.32-rcc.3

1 year ago

1.0.32

1 year ago

1.0.32-rcc.0

1 year ago

1.0.31

1 year ago

1.0.28-rcc.1

1 year ago

1.0.28-rcc.0

1 year ago

1.0.30-rcc.0

1 year ago

1.0.30-rcc.9

1 year ago

1.0.30-rcc.8

1 year ago

1.0.30-rcc.7

1 year ago

1.0.30-rcc.6

1 year ago

1.0.30-rcc.5

1 year ago

1.0.30-rcc.4

1 year ago

1.0.30-rcc.3

1 year ago

1.0.30-rcc.2

1 year ago

1.0.30-rcc.1

1 year ago

1.0.30

1 year ago

1.0.29-rc.18

1 year ago

1.0.29-rc.17

1 year ago

1.0.29-rc.19

1 year ago

1.0.29-rc.16

1 year ago

1.0.29-rc.15

1 year ago

1.0.29

1 year ago

1.0.29-rc.21

1 year ago

1.0.29-rc.20

1 year ago

1.0.30-rc.0

1 year ago

1.0.29-rc.10

1 year ago

1.0.29-rc.12

1 year ago

1.0.29-rc.11

1 year ago

1.0.29-rc.14

1 year ago

1.0.29-rc.13

1 year ago

1.0.29-rc.3

1 year ago

1.0.29-rc.2

1 year ago

1.0.29-rc.5

1 year ago

1.0.29-rc.4

1 year ago

1.0.29-rc.7

1 year ago

1.0.29-rc.6

1 year ago

1.0.29-rc.9

1 year ago

1.0.29-rc.8

1 year ago

1.0.28-rc.0

1 year ago

1.0.28-rc.1

1 year ago

1.0.28

1 year ago

1.0.29-rc.1

1 year ago

1.0.29-rc.0

1 year ago

1.0.27-rc.0

1 year ago

1.0.27

1 year ago

1.0.28-abc123.0

1 year ago

1.0.26-rc1

1 year ago

1.0.26

1 year ago

1.0.24-rc2

1 year ago

1.0.24-rc.3

1 year ago

1.0.25-rc2.2

1 year ago

1.0.25-rc2.3

1 year ago

1.0.25-rc2.1

1 year ago

1.0.25-rc1

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.24-rc.2

1 year ago

1.0.24-rc.1

1 year ago

1.0.24-abc123.3

1 year ago

1.0.24-abc123.2

1 year ago

1.0.24-abc123.1

1 year ago

1.0.24-abc123.0

1 year ago

1.0.23-abc123.1

1 year ago

1.0.23-abc123.0

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.21-abc123.0

1 year ago

1.0.21-abc123.2

1 year ago

1.0.21-abc123.1

1 year ago

1.0.21-rc.1

1 year ago

1.0.20-abc123.5

1 year ago

1.0.20-abc123.6

1 year ago

1.0.20

1 year ago

1.0.19-abc123.3

1 year ago

1.0.20-abc123.1

1 year ago

1.0.20-abc123.0

1 year ago

1.0.20-abc123.4

1 year ago

1.0.20-abc123.3

1 year ago

1.0.20-abc123.2

1 year ago

1.0.19

1 year ago

1.0.19-abc123.2

1 year ago

1.0.19-abc123.1

1 year ago

1.0.19-abc123.0

1 year ago

1.0.17

1 year ago

1.0.15

1 year ago

1.0.16-abc123.0

1 year ago

1.0.16-abc123.1

1 year ago

1.0.15-abc123.0

1 year ago

1.0.13-abc123.9

1 year ago

1.0.13-abc123.8

1 year ago

1.0.13-abc123.7

1 year ago

1.0.13-abc123.6

1 year ago

1.0.13

1 year ago

1.0.13-abc123.1

1 year ago

1.0.13-abc123.0

1 year ago

1.0.13-abc123.5

1 year ago

1.0.13-abc123.4

1 year ago

1.0.13-abc123.3

1 year ago

1.0.13-abc123.2

1 year ago

1.0.12-abc123.2

1 year ago

1.0.12-abc123.1

1 year ago

1.0.12-abc123.4

1 year ago

1.0.12-abc123.3

1 year ago

1.0.12-abc123.0

1 year ago

1.0.12-abc123.9

1 year ago

1.0.12-abc123.6

1 year ago

1.0.12-abc123.5

1 year ago

1.0.12-abc123.8

1 year ago

1.0.12-abc123.7

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.10-abc123.1

1 year ago

1.0.9-abc1234.0

1 year ago

1.0.9-abc1234.5

1 year ago

1.0.9-abc1234.1

1 year ago

1.0.10-abc123.0

1 year ago

1.0.9-abc1234.2

1 year ago

1.0.9-abc1234.3

1 year ago

1.0.9-abc1234.4

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.8-abc1234.0

1 year ago

1.0.7-abc1234.1

1 year ago

1.0.7-abc1234.0

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.2

1 year ago

1.0.2-abc1234.0

1 year ago

1.0.3

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago