1.1.2 • Published 4 months ago

meteora-clmm-sdk v1.1.2

Weekly downloads
-
License
ISC
Repository
-
Last release
4 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.create(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 bins = [activeBin.binId]; // Make sure bins is less than 70, as currently only support up to 70 bins for 1 position
for (
  let i = activeBin.binId;
  i < activeBin.binId + TOTAL_RANGE_INTERVAL / 2;
  i++
) {
  const rightNextBinId = i + 1;
  const leftPrevBinId = activeBin.binId - (rightNextBinId - activeBin.binId);
  bins.push(rightNextBinId);
  bins.unshift(leftPrevBinId);
}

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

// Get spot distribution (You can calculate with other strategy `calculateSpotDistribution`, `calculateNormalDistribution`)
const spotXYAmountDistribution = calculateSpotDistribution(
  activeBin.binId,
  bins
);
const newPosition = new Keypair();
const createPositionTx =
  await dlmmPool.initializePositionAndAddLiquidityByWeight({
    positionPubKey: newPosition.publicKey,
    lbPairPubKey: dlmmPool.pubkey,
    user: user.publicKey,
    totalXAmount,
    totalYAmount,
    xYAmountDistribution: spotXYAmountDistribution,
  });

try {
  for (let tx of Array.isArray(createPositionTx)
    ? createPositionTx
    : [createPositionTx]) {
    const createPositionTxHash = await sendAndConfirmTransaction(
      connection,
      tx,
      [user, newPosition]
    );
  }
} 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 addLiquidityTx = await dlmmPool.addLiquidityByWeight({
  positionPubKey: userPositions[0].publicKey,
  lbPairPubKey: dlmmPool.pubkey,
  user: user.publicKey,
  totalXAmount,
  totalYAmount,
  xYAmountDistribution: spotXYAmountDistribution,
});

try {
  for (let tx of Array.isArray(addLiquidityTx)
    ? addLiquidityTx
    : [addLiquidityTx]) {
    const addLiquidityTxHash = await sendAndConfirmTransaction(connection, tx, [
      user,
      newPosition,
    ]);
  }
} catch (error) {}
  • Remove Liquidity
const binIdsToRemove = userPositions[0].positionData.positionBinData.map(
  (bin) => bin.binId
);
const removeLiquidityTx = await dlmmPool.removeLiquidity({
  position: userPositions[0].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 removeLiquidityTxHash = await sendAndConfirmTransaction(
      connection,
      tx,
      [user, newPosition]
    );
  }
} 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>;}>
initializePositionAndAddLiquidityByWeightInitializes a position and adds liquidityPromise<Transaction\|Transaction[]>
addLiquidityByWeightAdd 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[]>
1.1.1

4 months ago

1.1.0

4 months ago

1.1.2

4 months ago

1.0.9

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago

1.0.6

4 months ago

0.6.1-abc123.0

6 months ago

0.6.4-abc123.4

6 months ago

0.6.4-abc123.3

6 months ago

0.6.4-abc123.6

6 months ago

0.6.4-abc123.5

6 months ago

0.6.4-abc123.0

6 months ago

0.6.4-abc123.2

6 months ago

0.6.4-abc123.1

6 months ago

0.5.6

6 months ago

0.5.5

6 months ago

0.6.3

6 months ago

0.6.2

6 months ago

0.6.1

6 months ago

0.6.0

6 months ago

0.5.8

6 months ago

0.5.7

6 months ago

0.5.9

6 months ago

0.6.4-abc1234.0

5 months ago

0.4.31

7 months ago

0.4.25-abc123.11

7 months ago

0.4.32

7 months ago

0.4.25-abc123.12

7 months ago

0.4.25-abc123.13

7 months ago

0.4.30

7 months ago

0.4.25-abc123.10

7 months ago

0.4.35

7 months ago

0.4.36

7 months ago

0.4.33

7 months ago

0.4.34

7 months ago

0.4.19-abc123.3

7 months ago

0.4.20

7 months ago

0.4.21

7 months ago

0.4.19-abc123.4

7 months ago

0.4.19-abc123.1

7 months ago

0.4.19-abc123.2

7 months ago

0.4.19-abc123.5

7 months ago

0.4.31-abc123.3

7 months ago

0.4.28

7 months ago

0.4.31-abc123.4

7 months ago

0.4.29

7 months ago

0.4.31-abc123.5

7 months ago

0.4.26

7 months ago

0.4.31-abc123.6

7 months ago

0.4.27

7 months ago

0.4.24

7 months ago

0.4.31-abc123.0

7 months ago

0.4.25

7 months ago

0.4.31-abc123.1

7 months ago

0.4.31-abc123.2

7 months ago

0.4.23

7 months ago

0.4.31-abc123.7

7 months ago

0.4.19

7 months ago

0.4.17

8 months ago

0.4.18

7 months ago

0.4.12

8 months ago

0.4.22-abc123.2

7 months ago

0.4.22-abc123.1

7 months ago

0.4.22-abc123.0

7 months ago

0.4.19-abc123.0

7 months ago

0.4.23-abc123.0

7 months ago

0.4.23-abc123.1

7 months ago

0.4.23-abc123.2

7 months ago

0.4.12-abc123.0

8 months ago

0.5.4

6 months ago

0.5.3

6 months ago

0.5.2

6 months ago

0.5.1

6 months ago

0.4.25-abc123.3

7 months ago

0.4.25-abc123.2

7 months ago

0.4.25-abc123.1

7 months ago

0.4.25-abc123.0

7 months ago

0.4.25-abc123.7

7 months ago

0.4.12-a12.0

8 months ago

0.4.25-abc123.6

7 months ago

0.4.25-abc123.5

7 months ago

0.4.25-abc123.4

7 months ago

0.4.25-abc123.9

7 months ago

0.4.25-abc123.8

7 months ago

0.4.12-abc1234.2

8 months ago

0.4.20-abc123.0

7 months ago

0.4.12-abc1234.0

8 months ago

0.4.12-abc12345.0

8 months ago

0.5.4-abc123.0

6 months ago

0.5.4-abc123.1

6 months ago

0.4.30-abc123.11

7 months ago

0.4.30-abc123.12

7 months ago

0.4.30-abc123.10

7 months ago

0.4.30-abc123.15

7 months ago

0.4.12-a123.0

8 months ago

0.4.30-abc123.16

7 months ago

0.4.30-abc123.13

7 months ago

0.4.30-abc123.14

7 months ago

0.4.30-abc123.17

7 months ago

0.4.14-a12.0

8 months ago

0.4.30-abc123.5

7 months ago

0.4.30-abc123.2

7 months ago

0.4.13-a12.0

8 months ago

0.4.30-abc123.0

7 months ago

0.4.24-abc123.0

7 months ago

0.4.24-abc123.1

7 months ago

0.4.18-abc123.1

7 months ago

0.4.24-abc123.2

7 months ago

0.4.18-abc123.0

7 months ago

0.4.13-abcd12.0

8 months ago

0.4.33-abc123.0

7 months ago

0.4.30-abc123.20

7 months ago

0.4.30-abc123.21

7 months ago

0.4.33-abc123.2

7 months ago

0.4.33-abc123.1

7 months ago

0.4.30-abc123.8

7 months ago

0.4.30-abc123.9

7 months ago

0.4.30-abc123.6

7 months ago

0.4.14-a1.1

8 months ago

0.4.30-abc123.7

7 months ago

0.4.13-abcd12345.0

8 months ago

0.4.13-a1.0

8 months ago

0.4.13-abc123456.0

8 months ago

0.4.13-abcd1234.0

8 months ago

0.4.28-abc123.2

7 months ago

0.4.28-abc123.1

7 months ago

0.4.28-abc123.0

7 months ago

0.4.12-a1234.0

8 months ago

0.4.16-a12.0

8 months ago

0.4.26-abc123.5

7 months ago

0.4.26-abc123.6

7 months ago

0.5.5-abc123.0

6 months ago

0.4.13-a123.0

8 months ago

0.4.26-abc123.0

7 months ago

0.4.26-abc123.3

7 months ago

0.4.26-abc123.4

7 months ago

0.4.26-abc123.1

7 months ago

0.4.26-abc123.2

7 months ago

0.4.13-abcd123.0

8 months ago

0.4.16-abc123.4

8 months ago

0.4.16-abc123.5

8 months ago

0.4.16-abc123.2

8 months ago

0.4.16-abc123.3

8 months ago

0.4.16-abc123.0

8 months ago

0.4.16-abc123.1

8 months ago

0.4.9

8 months ago

0.4.8

8 months ago

0.4.6-123abcdefg.0

9 months ago

0.4.6-abc12345.0

9 months ago

0.4.6-abcd123.0

9 months ago

0.4.6-123abcd.0

9 months ago

0.4.4-abcd123.0

9 months ago

0.4.6-abc1234.0

9 months ago

0.4.4-abcd1234.0

9 months ago

0.4.8-abc.0

8 months ago

0.4.4-abc12345.0

9 months ago

0.4.6-1abcde.0

9 months ago

0.4.6-1abc.0

9 months ago

0.4.6-1abcd.0

9 months ago

0.4.4-1abc.0

9 months ago

0.4.4-abc123.0

9 months ago

0.4.8-abc123.0

8 months ago

0.4.4-abc123456.0

9 months ago

0.4.6-123abcdef.0

9 months ago

0.4.10

8 months ago

0.4.6-1abcdef.0

9 months ago

0.4.4-abcd123456.0

9 months ago

0.4.11

8 months ago

0.4.4-abc1234.0

9 months ago

0.4.6-123abc.0

9 months ago

0.4.6-123abcde.0

9 months ago

0.4.5

9 months ago

0.4.6

9 months ago

0.4.6-abc123.0

9 months ago

0.4.3

9 months ago

0.4.3-1abc.0

9 months ago

0.4.3-123abcde.0

9 months ago

0.4.3-123abcd.0

9 months ago

0.4.3-123abc.0

9 months ago

0.4.3-abc1234.0

9 months ago

0.4.3-abc123.0

9 months ago

0.4.2

9 months ago

0.4.1

9 months ago

0.4.0

9 months ago

0.3.9

9 months ago

0.3.8

9 months ago

0.3.7

9 months ago

0.3.6

9 months ago

0.3.5

9 months ago

0.3.4

9 months ago

0.3.3

10 months ago

0.3.2

10 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.9

10 months ago

0.2.8

10 months ago

0.2.7

10 months ago

0.2.6

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.9

10 months ago

0.1.8

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.3

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago