@meteora-ag/dlmm 1.9.11 → 1.9.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -10165,6 +10165,9 @@ var POSITION_MAX_LENGTH = new BN(
10165
10165
  var MAX_RESIZE_LENGTH = new BN(
10166
10166
  CONSTANTS.find(([_, v]) => v.name == "MAX_RESIZE_LENGTH")?.[1].value ?? 0
10167
10167
  );
10168
+ var MAX_BIN_ID_PER_BIN_STEP = Number(
10169
+ CONSTANTS.find(([_, v]) => v.name == "MAX_BIN_ID_PER_BIN_STEP")?.[1].value ?? 0
10170
+ );
10168
10171
  var SIMULATION_USER = new PublicKey(
10169
10172
  "HrY9qR5TiB2xPzzvbBu5KrBorMfYGQXh9osXydz4jy9s"
10170
10173
  );
@@ -10202,7 +10205,6 @@ var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode2) => {
10202
10205
  CollectFeeMode2[CollectFeeMode2["OnlyY"] = 1] = "OnlyY";
10203
10206
  return CollectFeeMode2;
10204
10207
  })(CollectFeeMode || {});
10205
- var MAX_ALLOWED_REBALANCE_BIN_ARRAY_COUNT = 5;
10206
10208
 
10207
10209
  // src/dlmm/error.ts
10208
10210
  import { AnchorError } from "@coral-xyz/anchor";
@@ -13273,7 +13275,7 @@ function binRangeToBinIdArray(minBinId, maxBinId) {
13273
13275
  }
13274
13276
  return binIdArray;
13275
13277
  }
13276
- function getRebalanceBinArrayIndexesAndBitmapCoverage(adds, removes, activeId, pairAddress, programId, maxActiveBinSlippage = 0, includeSlippageForBinArray = false) {
13278
+ function getRebalanceBinArrayIndexesAndBitmapCoverage(adds, removes, activeId, pairAddress, programId) {
13277
13279
  let indexMap = /* @__PURE__ */ new Map();
13278
13280
  removes.forEach((value) => {
13279
13281
  let minBinId = value.minBinId;
@@ -13299,13 +13301,10 @@ function getRebalanceBinArrayIndexesAndBitmapCoverage(adds, removes, activeId, p
13299
13301
  adds.forEach((value) => {
13300
13302
  const minBinId = activeId + value.minDeltaId;
13301
13303
  const maxBinId = activeId + value.maxDeltaId;
13302
- let binArrayIndex = binIdToBinArrayIndex(
13303
- includeSlippageForBinArray ? new BN12(minBinId - maxActiveBinSlippage) : new BN12(minBinId)
13304
- );
13305
- let binArrayIndexes2 = [];
13306
- const upperBinId = includeSlippageForBinArray ? new BN12(maxBinId + maxActiveBinSlippage) : new BN12(maxBinId);
13304
+ let binArrayIndex = binIdToBinArrayIndex(new BN12(minBinId));
13305
+ const upperBinId = new BN12(maxBinId);
13307
13306
  while (true) {
13308
- binArrayIndexes2.push(binArrayIndex.toNumber());
13307
+ indexMap.set(binArrayIndex.toNumber(), true);
13309
13308
  const [binArrayLowerBinId, binArrayUpperBinId] = getBinArrayLowerUpperBinId(binArrayIndex);
13310
13309
  if (upperBinId.gte(binArrayLowerBinId) && upperBinId.lte(binArrayUpperBinId)) {
13311
13310
  break;
@@ -13313,13 +13312,6 @@ function getRebalanceBinArrayIndexesAndBitmapCoverage(adds, removes, activeId, p
13313
13312
  binArrayIndex = binArrayIndex.add(new BN12(1));
13314
13313
  }
13315
13314
  }
13316
- if (includeSlippageForBinArray && binArrayIndexes2.length > MAX_ALLOWED_REBALANCE_BIN_ARRAY_COUNT) {
13317
- const start = Math.floor((binArrayIndexes2.length - MAX_ALLOWED_REBALANCE_BIN_ARRAY_COUNT) / 2);
13318
- binArrayIndexes2 = binArrayIndexes2.slice(start, start + MAX_ALLOWED_REBALANCE_BIN_ARRAY_COUNT);
13319
- }
13320
- binArrayIndexes2.forEach((index) => {
13321
- indexMap.set(index, true);
13322
- });
13323
13315
  });
13324
13316
  const binArrayIndexes = Array.from(indexMap.keys()).map((idx) => new BN12(idx));
13325
13317
  const requireBitmapExtension = binArrayIndexes.some(
@@ -13385,19 +13377,22 @@ function findBaseDeltaX(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
13385
13377
  if (minDeltaId.gt(maxDeltaId) || amountX.lte(new BN13(0))) {
13386
13378
  return new BN13(0);
13387
13379
  }
13380
+ const m1 = minDeltaId;
13381
+ const m2 = maxDeltaId;
13388
13382
  let b = new BN13(0);
13389
13383
  let c = new BN13(0);
13390
- let m1 = minDeltaId;
13391
- let m2 = maxDeltaId.addn(1);
13392
- for (let m = m1.toNumber(); m <= m2.toNumber(); m++) {
13393
- const binId = activeId.addn(m);
13394
- const pm = getQPriceFromId(binId.neg(), binStep);
13395
- const bDelta = m1.mul(pm);
13396
- b = b.add(bDelta);
13397
- const cDelta = new BN13(m).mul(pm);
13398
- c = c.add(cDelta);
13384
+ let s = new BN13(0);
13385
+ const base = getQPriceBaseFactor(binStep);
13386
+ const maxBinId = activeId.add(maxDeltaId);
13387
+ let inverseBasePrice = pow(base, maxBinId.neg());
13388
+ for (let m = m2.toNumber(); m >= m1.toNumber(); m--) {
13389
+ const pm = inverseBasePrice;
13390
+ b = b.add(m1.mul(pm));
13391
+ c = c.add(new BN13(m).mul(pm));
13392
+ s = s.add(pm);
13393
+ inverseBasePrice = inverseBasePrice.mul(base).shrn(SCALE_OFFSET);
13399
13394
  }
13400
- return amountX.shln(SCALE_OFFSET).div(c.sub(b));
13395
+ return amountX.shln(SCALE_OFFSET).div(c.sub(b).add(s));
13401
13396
  }
13402
13397
  function findX0AndDeltaX(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
13403
13398
  if (minDeltaId.gt(maxDeltaId) || amountX.lte(new BN13(0)) || amountX.isZero()) {
@@ -13413,8 +13408,14 @@ function findX0AndDeltaX(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
13413
13408
  binStep,
13414
13409
  activeId
13415
13410
  );
13416
- const x0 = minDeltaId.neg().mul(baseDeltaX).add(baseDeltaX);
13411
+ let maxProbe = 100;
13417
13412
  while (true) {
13413
+ if (maxProbe < 0) {
13414
+ throw new Error(
13415
+ "Something wrong: Unable to find suitable x0 and delta_x within probe limit"
13416
+ );
13417
+ }
13418
+ const x0 = minDeltaId.neg().addn(1).mul(baseDeltaX);
13418
13419
  const amountInBins = getAmountInBinsAskSide(
13419
13420
  activeId,
13420
13421
  binStep,
@@ -13427,7 +13428,8 @@ function findX0AndDeltaX(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
13427
13428
  return acc.add(amountX2);
13428
13429
  }, new BN13(0));
13429
13430
  if (totalAmountX.gt(amountX)) {
13430
- baseDeltaX = baseDeltaX.sub(new BN13(1));
13431
+ baseDeltaX = baseDeltaX.subn(1);
13432
+ maxProbe--;
13431
13433
  } else {
13432
13434
  return {
13433
13435
  base: x0,
@@ -15313,7 +15315,7 @@ function resetUninvolvedLiquidityParams(minDeltaId, maxDeltaId, favorXInActiveId
15313
15315
  deltaY
15314
15316
  };
15315
15317
  }
15316
- async function chunkDepositWithRebalanceEndpoint(dlmm, strategy, slippagePercentage, maxActiveBinSlippage, position, positionMinBinId, positionMaxBinId, liquidityStrategyParameters, owner, payer, isParallel, skipSolWrappingOperation = false, includeSlippageForBinArray = false) {
15318
+ async function chunkDepositWithRebalanceEndpoint(dlmm, strategy, slippagePercentage, maxActiveBinSlippage, position, positionMinBinId, positionMaxBinId, liquidityStrategyParameters, owner, payer, isParallel, skipSolWrappingOperation = false) {
15317
15319
  const { slices, accounts: transferHookAccounts } = dlmm.getPotentialToken2022IxDataAndAccounts(0 /* Liquidity */);
15318
15320
  const userTokenX = getAssociatedTokenAddressSync(
15319
15321
  dlmm.lbPair.tokenXMint,
@@ -15354,14 +15356,10 @@ async function chunkDepositWithRebalanceEndpoint(dlmm, strategy, slippagePercent
15354
15356
  const chunkMaxBinId = chunkedBinRange[i].upperBinId;
15355
15357
  const initBinArrayIxs = [];
15356
15358
  const initBitmapIxs = [];
15357
- let binArrayIndexes = getBinArrayIndexesCoverage(
15358
- includeSlippageForBinArray ? new BN20(chunkMinBinId - maxActiveBinSlippage) : new BN20(chunkMinBinId),
15359
- includeSlippageForBinArray ? new BN20(chunkMaxBinId + maxActiveBinSlippage) : new BN20(chunkMaxBinId)
15359
+ const binArrayIndexes = getBinArrayIndexesCoverage(
15360
+ new BN20(chunkMinBinId),
15361
+ new BN20(chunkMaxBinId)
15360
15362
  );
15361
- if (includeSlippageForBinArray && binArrayIndexes.length > MAX_ALLOWED_REBALANCE_BIN_ARRAY_COUNT) {
15362
- const start = Math.floor((binArrayIndexes.length - MAX_ALLOWED_REBALANCE_BIN_ARRAY_COUNT) / 2);
15363
- binArrayIndexes = binArrayIndexes.slice(start, start + MAX_ALLOWED_REBALANCE_BIN_ARRAY_COUNT);
15364
- }
15365
15363
  const overflowDefaultBinArrayBitmap = binArrayIndexes.reduce(
15366
15364
  (acc, binArrayIndex) => acc || isOverflowDefaultBinArrayBitmap(binArrayIndex),
15367
15365
  false
@@ -18157,7 +18155,7 @@ var DLMM = class {
18157
18155
  * @param slippagePercentage The slippage percentage for adding liquidity.
18158
18156
  * @returns An object with two properties: `initPositionIxs` and `addLiquidityIxs`.
18159
18157
  */
18160
- async initializeMultiplePositionAndAddLiquidityByStrategy2(positionKeypairGenerator, totalXAmount, totalYAmount, strategy, owner, payer, slippagePercentage, altAddress, includeSlippageForBinArray = false) {
18158
+ async initializeMultiplePositionAndAddLiquidityByStrategy2(positionKeypairGenerator, totalXAmount, totalYAmount, strategy, owner, payer, slippagePercentage, altAddress) {
18161
18159
  const maxActiveBinSlippage = getAndCapMaxActiveBinSlippage(
18162
18160
  slippagePercentage,
18163
18161
  this.lbPair.binStep,
@@ -18228,8 +18226,7 @@ var DLMM = class {
18228
18226
  owner,
18229
18227
  payer,
18230
18228
  true,
18231
- this.opt?.skipSolWrappingOperation,
18232
- includeSlippageForBinArray
18229
+ this.opt?.skipSolWrappingOperation
18233
18230
  );
18234
18231
  for (const instructions of addLiquidityIxs) {
18235
18232
  const txIxs = [];
@@ -18265,7 +18262,7 @@ var DLMM = class {
18265
18262
  * @param slippagePercentage The slippage percentage for adding liquidity.
18266
18263
  * @returns An object with two properties: `initPositionIxs` and `addLiquidityIxs`.
18267
18264
  */
18268
- async initializeMultiplePositionAndAddLiquidityByStrategy(positionKeypairGenerator, totalXAmount, totalYAmount, strategy, owner, payer, slippagePercentage, includeSlippageForBinArray = false) {
18265
+ async initializeMultiplePositionAndAddLiquidityByStrategy(positionKeypairGenerator, totalXAmount, totalYAmount, strategy, owner, payer, slippagePercentage) {
18269
18266
  const maxActiveBinSlippage = getAndCapMaxActiveBinSlippage(
18270
18267
  slippagePercentage,
18271
18268
  this.lbPair.binStep,
@@ -18351,8 +18348,7 @@ var DLMM = class {
18351
18348
  owner,
18352
18349
  payer,
18353
18350
  false,
18354
- this.opt?.skipSolWrappingOperation,
18355
- includeSlippageForBinArray
18351
+ this.opt?.skipSolWrappingOperation
18356
18352
  );
18357
18353
  instructionsByPositions.push({
18358
18354
  positionKeypair: position,
@@ -18386,8 +18382,7 @@ var DLMM = class {
18386
18382
  totalYAmount,
18387
18383
  strategy,
18388
18384
  user,
18389
- slippage,
18390
- includeSlippageForBinArray = false
18385
+ slippage
18391
18386
  }) {
18392
18387
  const maxActiveBinSlippage = getAndCapMaxActiveBinSlippage(
18393
18388
  slippage,
@@ -18417,8 +18412,7 @@ var DLMM = class {
18417
18412
  user,
18418
18413
  user,
18419
18414
  true,
18420
- this.opt?.skipSolWrappingOperation,
18421
- includeSlippageForBinArray
18415
+ this.opt?.skipSolWrappingOperation
18422
18416
  );
18423
18417
  const latestBlockhashInfo = await this.program.provider.connection.getLatestBlockhash();
18424
18418
  return chunkedAddLiquidityIx.map((ixs) => {
@@ -21672,7 +21666,7 @@ var DLMM = class {
21672
21666
  *
21673
21667
  * @returns An object containing the instructions to initialize new bin arrays and the instruction to rebalance the position.
21674
21668
  */
21675
- async rebalancePosition(rebalancePositionResponse, maxActiveBinSlippage, rentPayer, slippage = 100, includeSlippageForBinArray = false) {
21669
+ async rebalancePosition(rebalancePositionResponse, maxActiveBinSlippage, rentPayer, slippage = 100) {
21676
21670
  const { rebalancePosition, simulationResult } = rebalancePositionResponse;
21677
21671
  const { lbPair, shouldClaimFee, shouldClaimReward, owner, address } = rebalancePosition;
21678
21672
  const { depositParams, withdrawParams } = simulationResult;
@@ -21745,9 +21739,7 @@ var DLMM = class {
21745
21739
  withdrawParams,
21746
21740
  activeId.toNumber(),
21747
21741
  this.pubkey,
21748
- this.program.programId,
21749
- maxActiveBinSlippage.toNumber(),
21750
- includeSlippageForBinArray
21742
+ this.program.programId
21751
21743
  );
21752
21744
  const binArrayPublicKeys = binArrayIndexes.map((index) => {
21753
21745
  const [binArrayPubkey] = deriveBinArray(
@@ -22911,9 +22903,9 @@ export {
22911
22903
  LIMIT_ORDER_MIN_SIZE,
22912
22904
  LimitOrderStatus,
22913
22905
  MAX_ACTIVE_BIN_SLIPPAGE,
22914
- MAX_ALLOWED_REBALANCE_BIN_ARRAY_COUNT,
22915
22906
  MAX_BINS_PER_POSITION,
22916
22907
  MAX_BIN_ARRAY_SIZE,
22908
+ MAX_BIN_ID_PER_BIN_STEP,
22917
22909
  MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX,
22918
22910
  MAX_BIN_PER_LIMIT_ORDER,
22919
22911
  MAX_CLAIM_ALL_ALLOWED,