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
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
constdlmmPool=awaitDLMM.create(connection,USDC_USDT_POOL);// If you need to create multiple, can consider using `createMultiple`constdlmmPool=awaitDLMM.create(connection, [USDC_USDT_POOL,...]);
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
constTOTAL_RANGE_INTERVAL=10; // 10 bins on each side of the active binconst 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++) {constrightNextBinId= i +1;constleftPrevBinId=activeBin.binId - (rightNextBinId -activeBin.binId);bins.push(rightNextBinId);bins.unshift(leftPrevBinId);}constactiveBinPricePerToken=dlmmPool.fromPricePerLamport(Number(activeBin.price));consttotalXAmount=newBN(100);consttotalYAmount=totalXAmount.mul(newBN(Number(activeBinPricePerToken)));// Get spot distribution (You can calculate with other strategy `calculateSpotDistribution`, `calculateNormalDistribution`)
constspotXYAmountDistribution=calculateSpotDistribution(activeBin.binId, bins);constnewPosition=newKeypair();constcreatePositionTx=awaitdlmmPool.initializePositionAndAddLiquidityByWeight({ positionPubKey:newPosition.publicKey, lbPairPubKey:dlmmPool.pubkey, user:user.publicKey, totalXAmount, totalYAmount, xYAmountDistribution: spotXYAmountDistribution, });try {for (let tx ofArray.isArray(createPositionTx)? createPositionTx: [createPositionTx]) {constcreatePositionTxHash=awaitsendAndConfirmTransaction( connection, tx, [user, newPosition] ); }} catch (error) {}
Get list of positions
To get the list of liquidity positions for a specified pair and pool