@meteora-ag/dlmm 1.3.1 → 1.3.4

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
@@ -1,11 +1,27 @@
1
1
  // src/dlmm/index.ts
2
+ import { AnchorProvider as AnchorProvider2, BN as BN10, Program as Program3 } from "@coral-xyz/anchor";
3
+ import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
4
+ import {
5
+ AccountLayout,
6
+ MintLayout,
7
+ NATIVE_MINT as NATIVE_MINT2,
8
+ TOKEN_PROGRAM_ID as TOKEN_PROGRAM_ID2,
9
+ createAssociatedTokenAccountInstruction as createAssociatedTokenAccountInstruction2,
10
+ createTransferInstruction,
11
+ getAssociatedTokenAddressSync as getAssociatedTokenAddressSync2
12
+ } from "@solana/spl-token";
2
13
  import {
14
+ ComputeBudgetProgram as ComputeBudgetProgram2,
3
15
  PublicKey as PublicKey6,
4
- Transaction,
16
+ SYSVAR_CLOCK_PUBKEY,
5
17
  SYSVAR_RENT_PUBKEY,
6
18
  SystemProgram as SystemProgram2,
7
- SYSVAR_CLOCK_PUBKEY
19
+ Transaction
8
20
  } from "@solana/web3.js";
21
+ import Decimal5 from "decimal.js";
22
+
23
+ // src/dlmm/constants/index.ts
24
+ import { PublicKey } from "@solana/web3.js";
9
25
 
10
26
  // src/dlmm/idl.ts
11
27
  var IDL = {
@@ -5259,7 +5275,6 @@ var IDL = {
5259
5275
  };
5260
5276
 
5261
5277
  // src/dlmm/constants/index.ts
5262
- import { PublicKey } from "@solana/web3.js";
5263
5278
  import { BN } from "@coral-xyz/anchor";
5264
5279
  var LBCLMM_PROGRAM_IDS = {
5265
5280
  devnet: "LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo",
@@ -5309,66 +5324,48 @@ var ILM_BASE = new PublicKey(
5309
5324
  "MFGQxwAmB91SwuYX36okv2Qmdc9aMuHTwWGUrp4AtB1"
5310
5325
  );
5311
5326
 
5312
- // src/dlmm/types/index.ts
5313
- import { u64, i64, struct } from "@coral-xyz/borsh";
5314
- var PositionVersion = /* @__PURE__ */ ((PositionVersion2) => {
5315
- PositionVersion2[PositionVersion2["V1"] = 0] = "V1";
5316
- PositionVersion2[PositionVersion2["V2"] = 1] = "V2";
5317
- return PositionVersion2;
5318
- })(PositionVersion || {});
5319
- var PairType = /* @__PURE__ */ ((PairType2) => {
5320
- PairType2[PairType2["Permissionless"] = 0] = "Permissionless";
5321
- PairType2[PairType2["Permissioned"] = 1] = "Permissioned";
5322
- return PairType2;
5323
- })(PairType || {});
5324
- var Strategy = {
5325
- SpotOneSide: { spotOneSide: {} },
5326
- CurveOneSide: { curveOneSide: {} },
5327
- BidAskOneSide: { bidAskOneSide: {} },
5328
- SpotBalanced: { spotBalanced: {} },
5329
- CurveBalanced: { curveBalanced: {} },
5330
- BidAskBalanced: { bidAskBalanced: {} },
5331
- SpotImBalanced: { spotImBalanced: {} },
5332
- CurveImBalanced: { curveImBalanced: {} },
5333
- BidAskImBalanced: { bidAskImBalanced: {} }
5327
+ // src/dlmm/error.ts
5328
+ import { AnchorError } from "@coral-xyz/anchor";
5329
+ var DLMMError = class extends Error {
5330
+ errorCode;
5331
+ errorName;
5332
+ errorMessage;
5333
+ constructor(error) {
5334
+ let _errorCode = 0;
5335
+ let _errorName = "Something went wrong";
5336
+ let _errorMessage = "Something went wrong";
5337
+ if (error instanceof Error) {
5338
+ const anchorError = AnchorError.parse(
5339
+ JSON.parse(JSON.stringify(error)).logs
5340
+ );
5341
+ if (anchorError?.program.toBase58() === LBCLMM_PROGRAM_IDS["mainnet-beta"]) {
5342
+ _errorCode = anchorError.error.errorCode.number;
5343
+ _errorName = anchorError.error.errorCode.code;
5344
+ _errorMessage = anchorError.error.errorMessage;
5345
+ }
5346
+ } else {
5347
+ const idlError = IDL.errors.find((err) => err.code === error);
5348
+ if (idlError) {
5349
+ _errorCode = idlError.code;
5350
+ _errorName = idlError.name;
5351
+ _errorMessage = idlError.msg;
5352
+ }
5353
+ }
5354
+ super(_errorMessage);
5355
+ this.errorCode = _errorCode;
5356
+ this.errorName = _errorName;
5357
+ this.errorMessage = _errorMessage;
5358
+ }
5359
+ };
5360
+ var DlmmSdkError = class extends Error {
5361
+ name;
5362
+ message;
5363
+ constructor(name, message) {
5364
+ super();
5365
+ this.name = name;
5366
+ this.message = message;
5367
+ }
5334
5368
  };
5335
- var StrategyType = /* @__PURE__ */ ((StrategyType2) => {
5336
- StrategyType2[StrategyType2["SpotOneSide"] = 0] = "SpotOneSide";
5337
- StrategyType2[StrategyType2["CurveOneSide"] = 1] = "CurveOneSide";
5338
- StrategyType2[StrategyType2["BidAskOneSide"] = 2] = "BidAskOneSide";
5339
- StrategyType2[StrategyType2["SpotImBalanced"] = 3] = "SpotImBalanced";
5340
- StrategyType2[StrategyType2["CurveImBalanced"] = 4] = "CurveImBalanced";
5341
- StrategyType2[StrategyType2["BidAskImBalanced"] = 5] = "BidAskImBalanced";
5342
- StrategyType2[StrategyType2["SpotBalanced"] = 6] = "SpotBalanced";
5343
- StrategyType2[StrategyType2["CurveBalanced"] = 7] = "CurveBalanced";
5344
- StrategyType2[StrategyType2["BidAskBalanced"] = 8] = "BidAskBalanced";
5345
- return StrategyType2;
5346
- })(StrategyType || {});
5347
- var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
5348
- ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
5349
- ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
5350
- return ActivationType2;
5351
- })(ActivationType || {});
5352
- var BitmapType = /* @__PURE__ */ ((BitmapType2) => {
5353
- BitmapType2[BitmapType2["U1024"] = 0] = "U1024";
5354
- BitmapType2[BitmapType2["U512"] = 1] = "U512";
5355
- return BitmapType2;
5356
- })(BitmapType || {});
5357
- var ClockLayout = struct([
5358
- u64("slot"),
5359
- i64("epochStartTimestamp"),
5360
- u64("epoch"),
5361
- u64("leaderScheduleEpoch"),
5362
- i64("unixTimestamp")
5363
- ]);
5364
- var PairStatus = /* @__PURE__ */ ((PairStatus2) => {
5365
- PairStatus2[PairStatus2["Enabled"] = 0] = "Enabled";
5366
- PairStatus2[PairStatus2["Disabled"] = 1] = "Disabled";
5367
- return PairStatus2;
5368
- })(PairStatus || {});
5369
-
5370
- // src/dlmm/index.ts
5371
- import { AnchorProvider as AnchorProvider2, BN as BN9, Program as Program2 } from "@coral-xyz/anchor";
5372
5369
 
5373
5370
  // src/dlmm/helpers/index.ts
5374
5371
  import {
@@ -6344,6 +6341,14 @@ function generateBinAmount(amount, binStep, binId, tokenXDecimal, tokenYDecimal,
6344
6341
  return new BN5(c1.sub(c0).floor().toString());
6345
6342
  }
6346
6343
 
6344
+ // src/dlmm/helpers/index.ts
6345
+ import { getSimulationComputeUnits } from "@solana-developers/helpers";
6346
+
6347
+ // src/dlmm/helpers/computeUnit.ts
6348
+ var DEFAULT_ADD_LIQUIDITY_CU = 8e5;
6349
+ var MIN_CU_BUFFER = 5e4;
6350
+ var MAX_CU_BUFFER = 2e5;
6351
+
6347
6352
  // src/dlmm/helpers/derive.ts
6348
6353
  import { PublicKey as PublicKey2 } from "@solana/web3.js";
6349
6354
  function sortTokenMints(tokenX, tokenY) {
@@ -6459,11 +6464,110 @@ function deriveReserve(token, lbPair, programId) {
6459
6464
  }
6460
6465
 
6461
6466
  // src/dlmm/helpers/binArray.ts
6462
- import { BN as BN6 } from "@coral-xyz/anchor";
6467
+ import { BN as BN7 } from "@coral-xyz/anchor";
6463
6468
  import { PublicKey as PublicKey3 } from "@solana/web3.js";
6469
+
6470
+ // src/dlmm/types/index.ts
6471
+ import {
6472
+ BN as BN6
6473
+ } from "@coral-xyz/anchor";
6474
+ import Decimal4 from "decimal.js";
6475
+ import { u64, i64, struct } from "@coral-xyz/borsh";
6476
+ var PositionVersion = /* @__PURE__ */ ((PositionVersion2) => {
6477
+ PositionVersion2[PositionVersion2["V1"] = 0] = "V1";
6478
+ PositionVersion2[PositionVersion2["V2"] = 1] = "V2";
6479
+ return PositionVersion2;
6480
+ })(PositionVersion || {});
6481
+ var PairType = /* @__PURE__ */ ((PairType2) => {
6482
+ PairType2[PairType2["Permissionless"] = 0] = "Permissionless";
6483
+ PairType2[PairType2["Permissioned"] = 1] = "Permissioned";
6484
+ return PairType2;
6485
+ })(PairType || {});
6486
+ var Strategy = {
6487
+ SpotOneSide: { spotOneSide: {} },
6488
+ CurveOneSide: { curveOneSide: {} },
6489
+ BidAskOneSide: { bidAskOneSide: {} },
6490
+ SpotBalanced: { spotBalanced: {} },
6491
+ CurveBalanced: { curveBalanced: {} },
6492
+ BidAskBalanced: { bidAskBalanced: {} },
6493
+ SpotImBalanced: { spotImBalanced: {} },
6494
+ CurveImBalanced: { curveImBalanced: {} },
6495
+ BidAskImBalanced: { bidAskImBalanced: {} }
6496
+ };
6497
+ var StrategyType = /* @__PURE__ */ ((StrategyType2) => {
6498
+ StrategyType2[StrategyType2["SpotOneSide"] = 0] = "SpotOneSide";
6499
+ StrategyType2[StrategyType2["CurveOneSide"] = 1] = "CurveOneSide";
6500
+ StrategyType2[StrategyType2["BidAskOneSide"] = 2] = "BidAskOneSide";
6501
+ StrategyType2[StrategyType2["SpotImBalanced"] = 3] = "SpotImBalanced";
6502
+ StrategyType2[StrategyType2["CurveImBalanced"] = 4] = "CurveImBalanced";
6503
+ StrategyType2[StrategyType2["BidAskImBalanced"] = 5] = "BidAskImBalanced";
6504
+ StrategyType2[StrategyType2["SpotBalanced"] = 6] = "SpotBalanced";
6505
+ StrategyType2[StrategyType2["CurveBalanced"] = 7] = "CurveBalanced";
6506
+ StrategyType2[StrategyType2["BidAskBalanced"] = 8] = "BidAskBalanced";
6507
+ return StrategyType2;
6508
+ })(StrategyType || {});
6509
+ var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
6510
+ ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
6511
+ ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
6512
+ return ActivationType2;
6513
+ })(ActivationType || {});
6514
+ var BinLiquidity;
6515
+ ((BinLiquidity3) => {
6516
+ function fromBin(bin, binId, binStep, baseTokenDecimal, quoteTokenDecimal, version) {
6517
+ const pricePerLamport = getPriceOfBinByBinId(
6518
+ binId,
6519
+ binStep
6520
+ ).toString();
6521
+ return {
6522
+ binId,
6523
+ xAmount: bin.amountX,
6524
+ yAmount: bin.amountY,
6525
+ supply: bin.liquiditySupply,
6526
+ price: pricePerLamport,
6527
+ version,
6528
+ pricePerToken: new Decimal4(pricePerLamport).mul(new Decimal4(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString()
6529
+ };
6530
+ }
6531
+ BinLiquidity3.fromBin = fromBin;
6532
+ function empty(binId, binStep, baseTokenDecimal, quoteTokenDecimal, version) {
6533
+ const pricePerLamport = getPriceOfBinByBinId(
6534
+ binId,
6535
+ binStep
6536
+ ).toString();
6537
+ return {
6538
+ binId,
6539
+ xAmount: new BN6(0),
6540
+ yAmount: new BN6(0),
6541
+ supply: new BN6(0),
6542
+ price: pricePerLamport,
6543
+ version,
6544
+ pricePerToken: new Decimal4(pricePerLamport).mul(new Decimal4(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString()
6545
+ };
6546
+ }
6547
+ BinLiquidity3.empty = empty;
6548
+ })(BinLiquidity || (BinLiquidity = {}));
6549
+ var BitmapType = /* @__PURE__ */ ((BitmapType2) => {
6550
+ BitmapType2[BitmapType2["U1024"] = 0] = "U1024";
6551
+ BitmapType2[BitmapType2["U512"] = 1] = "U512";
6552
+ return BitmapType2;
6553
+ })(BitmapType || {});
6554
+ var ClockLayout = struct([
6555
+ u64("slot"),
6556
+ i64("epochStartTimestamp"),
6557
+ u64("epoch"),
6558
+ u64("leaderScheduleEpoch"),
6559
+ i64("unixTimestamp")
6560
+ ]);
6561
+ var PairStatus = /* @__PURE__ */ ((PairStatus2) => {
6562
+ PairStatus2[PairStatus2["Enabled"] = 0] = "Enabled";
6563
+ PairStatus2[PairStatus2["Disabled"] = 1] = "Disabled";
6564
+ return PairStatus2;
6565
+ })(PairStatus || {});
6566
+
6567
+ // src/dlmm/helpers/binArray.ts
6464
6568
  function internalBitmapRange() {
6465
6569
  const lowerBinArrayIndex = BIN_ARRAY_BITMAP_SIZE.neg();
6466
- const upperBinArrayIndex = BIN_ARRAY_BITMAP_SIZE.sub(new BN6(1));
6570
+ const upperBinArrayIndex = BIN_ARRAY_BITMAP_SIZE.sub(new BN7(1));
6467
6571
  return [lowerBinArrayIndex, upperBinArrayIndex];
6468
6572
  }
6469
6573
  function buildBitmapFromU64Arrays(u64Arrays, type) {
@@ -6472,7 +6576,7 @@ function buildBitmapFromU64Arrays(u64Arrays, type) {
6472
6576
  return b.toArrayLike(Buffer, "le", 8);
6473
6577
  })
6474
6578
  );
6475
- return new BN6(buffer, "le");
6579
+ return new BN7(buffer, "le");
6476
6580
  }
6477
6581
  function bitmapTypeDetail(type) {
6478
6582
  if (type == 0 /* U1024 */) {
@@ -6513,24 +6617,24 @@ function leastSignificantBit(number, bitLength) {
6513
6617
  function extensionBitmapRange() {
6514
6618
  return [
6515
6619
  BIN_ARRAY_BITMAP_SIZE.neg().mul(
6516
- EXTENSION_BINARRAY_BITMAP_SIZE.add(new BN6(1))
6620
+ EXTENSION_BINARRAY_BITMAP_SIZE.add(new BN7(1))
6517
6621
  ),
6518
6622
  BIN_ARRAY_BITMAP_SIZE.mul(
6519
- EXTENSION_BINARRAY_BITMAP_SIZE.add(new BN6(1))
6520
- ).sub(new BN6(1))
6623
+ EXTENSION_BINARRAY_BITMAP_SIZE.add(new BN7(1))
6624
+ ).sub(new BN7(1))
6521
6625
  ];
6522
6626
  }
6523
6627
  function findSetBit(startIndex, endIndex, binArrayBitmapExtension) {
6524
6628
  const getBinArrayOffset = (binArrayIndex) => {
6525
- return binArrayIndex.gt(new BN6(0)) ? binArrayIndex.mod(BIN_ARRAY_BITMAP_SIZE) : binArrayIndex.add(new BN6(1)).neg().mod(BIN_ARRAY_BITMAP_SIZE);
6629
+ return binArrayIndex.gt(new BN7(0)) ? binArrayIndex.mod(BIN_ARRAY_BITMAP_SIZE) : binArrayIndex.add(new BN7(1)).neg().mod(BIN_ARRAY_BITMAP_SIZE);
6526
6630
  };
6527
6631
  const getBitmapOffset = (binArrayIndex) => {
6528
- return binArrayIndex.gt(new BN6(0)) ? binArrayIndex.div(BIN_ARRAY_BITMAP_SIZE).sub(new BN6(1)) : binArrayIndex.add(new BN6(1)).neg().div(BIN_ARRAY_BITMAP_SIZE).sub(new BN6(1));
6632
+ return binArrayIndex.gt(new BN7(0)) ? binArrayIndex.div(BIN_ARRAY_BITMAP_SIZE).sub(new BN7(1)) : binArrayIndex.add(new BN7(1)).neg().div(BIN_ARRAY_BITMAP_SIZE).sub(new BN7(1));
6529
6633
  };
6530
6634
  if (startIndex <= endIndex) {
6531
6635
  for (let i = startIndex; i <= endIndex; i++) {
6532
- const binArrayOffset = getBinArrayOffset(new BN6(i)).toNumber();
6533
- const bitmapOffset = getBitmapOffset(new BN6(i)).toNumber();
6636
+ const binArrayOffset = getBinArrayOffset(new BN7(i)).toNumber();
6637
+ const bitmapOffset = getBitmapOffset(new BN7(i)).toNumber();
6534
6638
  const bitmapChunks = i > 0 ? binArrayBitmapExtension.positiveBinArrayBitmap[bitmapOffset] : binArrayBitmapExtension.negativeBinArrayBitmap[bitmapOffset];
6535
6639
  const bitmap = buildBitmapFromU64Arrays(bitmapChunks, 1 /* U512 */);
6536
6640
  if (bitmap.testn(binArrayOffset)) {
@@ -6539,8 +6643,8 @@ function findSetBit(startIndex, endIndex, binArrayBitmapExtension) {
6539
6643
  }
6540
6644
  } else {
6541
6645
  for (let i = startIndex; i >= endIndex; i--) {
6542
- const binArrayOffset = getBinArrayOffset(new BN6(i)).toNumber();
6543
- const bitmapOffset = getBitmapOffset(new BN6(i)).toNumber();
6646
+ const binArrayOffset = getBinArrayOffset(new BN7(i)).toNumber();
6647
+ const bitmapOffset = getBitmapOffset(new BN7(i)).toNumber();
6544
6648
  const bitmapChunks = i > 0 ? binArrayBitmapExtension.positiveBinArrayBitmap[bitmapOffset] : binArrayBitmapExtension.negativeBinArrayBitmap[bitmapOffset];
6545
6649
  const bitmap = buildBitmapFromU64Arrays(bitmapChunks, 1 /* U512 */);
6546
6650
  if (bitmap.testn(binArrayOffset)) {
@@ -6562,11 +6666,11 @@ function deriveBinArrayBitmapExtension(lbPair, programId) {
6562
6666
  }
6563
6667
  function binIdToBinArrayIndex(binId) {
6564
6668
  const { div: idx, mod } = binId.divmod(MAX_BIN_ARRAY_SIZE);
6565
- return binId.isNeg() && !mod.isZero() ? idx.sub(new BN6(1)) : idx;
6669
+ return binId.isNeg() && !mod.isZero() ? idx.sub(new BN7(1)) : idx;
6566
6670
  }
6567
6671
  function getBinArrayLowerUpperBinId(binArrayIndex) {
6568
6672
  const lowerBinId = binArrayIndex.mul(MAX_BIN_ARRAY_SIZE);
6569
- const upperBinId = lowerBinId.add(MAX_BIN_ARRAY_SIZE).sub(new BN6(1));
6673
+ const upperBinId = lowerBinId.add(MAX_BIN_ARRAY_SIZE).sub(new BN7(1));
6570
6674
  return [lowerBinId, upperBinId];
6571
6675
  }
6572
6676
  function isBinIdWithinBinArray(activeId, binArrayIndex) {
@@ -6601,18 +6705,18 @@ function findNextBinArrayIndexWithLiquidity(swapForY, activeId, lbPairState, bin
6601
6705
  binArrayBitmapExtension
6602
6706
  );
6603
6707
  if (binArrayIndex !== null) {
6604
- return new BN6(binArrayIndex);
6708
+ return new BN7(binArrayIndex);
6605
6709
  } else {
6606
6710
  return null;
6607
6711
  }
6608
6712
  } else {
6609
6713
  const binArrayIndex = findSetBit(
6610
6714
  startBinArrayIndex.toNumber(),
6611
- BIN_ARRAY_BITMAP_SIZE.neg().sub(new BN6(1)).toNumber(),
6715
+ BIN_ARRAY_BITMAP_SIZE.neg().sub(new BN7(1)).toNumber(),
6612
6716
  binArrayBitmapExtension
6613
6717
  );
6614
6718
  if (binArrayIndex !== null) {
6615
- return new BN6(binArrayIndex);
6719
+ return new BN7(binArrayIndex);
6616
6720
  } else {
6617
6721
  startBinArrayIndex = BIN_ARRAY_BITMAP_SIZE.neg();
6618
6722
  }
@@ -6625,9 +6729,9 @@ function findNextBinArrayIndexWithLiquidity(swapForY, activeId, lbPairState, bin
6625
6729
  binArrayBitmapExtension
6626
6730
  );
6627
6731
  if (binArrayIndex !== null) {
6628
- return new BN6(binArrayIndex);
6732
+ return new BN7(binArrayIndex);
6629
6733
  } else {
6630
- startBinArrayIndex = BIN_ARRAY_BITMAP_SIZE.sub(new BN6(1));
6734
+ startBinArrayIndex = BIN_ARRAY_BITMAP_SIZE.sub(new BN7(1));
6631
6735
  }
6632
6736
  } else {
6633
6737
  const binArrayIndex = findSetBit(
@@ -6636,7 +6740,7 @@ function findNextBinArrayIndexWithLiquidity(swapForY, activeId, lbPairState, bin
6636
6740
  binArrayBitmapExtension
6637
6741
  );
6638
6742
  if (binArrayIndex !== null) {
6639
- return new BN6(binArrayIndex);
6743
+ return new BN7(binArrayIndex);
6640
6744
  } else {
6641
6745
  return null;
6642
6746
  }
@@ -6651,22 +6755,22 @@ function findNextBinArrayIndexWithLiquidity(swapForY, activeId, lbPairState, bin
6651
6755
  bitmapType
6652
6756
  );
6653
6757
  if (swapForY) {
6654
- const upperBitRange = new BN6(bitmapDetail.bits - 1).sub(offset);
6758
+ const upperBitRange = new BN7(bitmapDetail.bits - 1).sub(offset);
6655
6759
  const croppedBitmap = bitmap.shln(upperBitRange.toNumber());
6656
6760
  const msb = mostSignificantBit(croppedBitmap, bitmapDetail.bits);
6657
6761
  if (msb !== null) {
6658
- return startBinArrayIndex.sub(new BN6(msb));
6762
+ return startBinArrayIndex.sub(new BN7(msb));
6659
6763
  } else {
6660
- startBinArrayIndex = lowerBinArrayIndex.sub(new BN6(1));
6764
+ startBinArrayIndex = lowerBinArrayIndex.sub(new BN7(1));
6661
6765
  }
6662
6766
  } else {
6663
6767
  const lowerBitRange = offset;
6664
6768
  const croppedBitmap = bitmap.shrn(lowerBitRange.toNumber());
6665
6769
  const lsb = leastSignificantBit(croppedBitmap, bitmapDetail.bits);
6666
6770
  if (lsb !== null) {
6667
- return startBinArrayIndex.add(new BN6(lsb));
6771
+ return startBinArrayIndex.add(new BN7(lsb));
6668
6772
  } else {
6669
- startBinArrayIndex = upperBinArrayIndex.add(new BN6(1));
6773
+ startBinArrayIndex = upperBinArrayIndex.add(new BN7(1));
6670
6774
  }
6671
6775
  }
6672
6776
  }
@@ -6695,9 +6799,9 @@ function getBinArraysRequiredByPositionRange(pair, fromBinId, toBinId, programId
6695
6799
  const positionCount = getPositionCount(minBinId, maxBinId);
6696
6800
  const binArrays = /* @__PURE__ */ new Map();
6697
6801
  for (let i = 0; i < positionCount.toNumber(); i++) {
6698
- const lowerBinId = minBinId.add(MAX_BIN_PER_POSITION.mul(new BN6(i)));
6802
+ const lowerBinId = minBinId.add(MAX_BIN_PER_POSITION.mul(new BN7(i)));
6699
6803
  const lowerBinArrayIndex = binIdToBinArrayIndex(lowerBinId);
6700
- const upperBinArrayIndex = lowerBinArrayIndex.add(new BN6(1));
6804
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN7(1));
6701
6805
  const [lowerBinArray] = deriveBinArray(pair, lowerBinArrayIndex, programId);
6702
6806
  const [upperBinArray] = deriveBinArray(pair, upperBinArrayIndex, programId);
6703
6807
  binArrays.set(lowerBinArray.toBase58(), lowerBinArrayIndex);
@@ -6708,19 +6812,29 @@ function getBinArraysRequiredByPositionRange(pair, fromBinId, toBinId, programId
6708
6812
  index
6709
6813
  }));
6710
6814
  }
6815
+ function* enumerateBins(binsById, lowerBinId, upperBinId, binStep, baseTokenDecimal, quoteTokenDecimal, version) {
6816
+ for (let currentBinId = lowerBinId; currentBinId <= upperBinId; currentBinId++) {
6817
+ const bin = binsById.get(currentBinId);
6818
+ if (bin != null) {
6819
+ yield BinLiquidity.fromBin(bin, currentBinId, binStep, baseTokenDecimal, quoteTokenDecimal, version);
6820
+ } else {
6821
+ yield BinLiquidity.empty(currentBinId, binStep, baseTokenDecimal, quoteTokenDecimal, version);
6822
+ }
6823
+ }
6824
+ }
6711
6825
 
6712
6826
  // src/dlmm/helpers/fee.ts
6713
- import { BN as BN7 } from "@coral-xyz/anchor";
6827
+ import { BN as BN8 } from "@coral-xyz/anchor";
6714
6828
  function getBaseFee(binStep, sParameter) {
6715
- return new BN7(sParameter.baseFactor).mul(new BN7(binStep)).mul(new BN7(10));
6829
+ return new BN8(sParameter.baseFactor).mul(new BN8(binStep)).mul(new BN8(10));
6716
6830
  }
6717
6831
  function getVariableFee(binStep, sParameter, vParameter) {
6718
6832
  if (sParameter.variableFeeControl > 0) {
6719
- const square_vfa_bin = new BN7(vParameter.volatilityAccumulator).mul(new BN7(binStep)).pow(new BN7(2));
6720
- const v_fee = new BN7(sParameter.variableFeeControl).mul(square_vfa_bin);
6721
- return v_fee.add(new BN7(99999999999)).div(new BN7(1e11));
6833
+ const square_vfa_bin = new BN8(vParameter.volatilityAccumulator).mul(new BN8(binStep)).pow(new BN8(2));
6834
+ const v_fee = new BN8(sParameter.variableFeeControl).mul(square_vfa_bin);
6835
+ return v_fee.add(new BN8(99999999999)).div(new BN8(1e11));
6722
6836
  }
6723
- return new BN7(0);
6837
+ return new BN8(0);
6724
6838
  }
6725
6839
  function getTotalFee(binStep, sParameter, vParameter) {
6726
6840
  const totalFee = getBaseFee(binStep, sParameter).add(
@@ -6731,30 +6845,30 @@ function getTotalFee(binStep, sParameter, vParameter) {
6731
6845
  function computeFee(binStep, sParameter, vParameter, inAmount) {
6732
6846
  const totalFee = getTotalFee(binStep, sParameter, vParameter);
6733
6847
  const denominator = FEE_PRECISION.sub(totalFee);
6734
- return inAmount.mul(totalFee).add(denominator).sub(new BN7(1)).div(denominator);
6848
+ return inAmount.mul(totalFee).add(denominator).sub(new BN8(1)).div(denominator);
6735
6849
  }
6736
6850
  function computeFeeFromAmount(binStep, sParameter, vParameter, inAmountWithFees) {
6737
6851
  const totalFee = getTotalFee(binStep, sParameter, vParameter);
6738
- return inAmountWithFees.mul(totalFee).add(FEE_PRECISION.sub(new BN7(1))).div(FEE_PRECISION);
6852
+ return inAmountWithFees.mul(totalFee).add(FEE_PRECISION.sub(new BN8(1))).div(FEE_PRECISION);
6739
6853
  }
6740
6854
  function computeProtocolFee(feeAmount, sParameter) {
6741
- return feeAmount.mul(new BN7(sParameter.protocolShare)).div(new BN7(BASIS_POINT_MAX));
6855
+ return feeAmount.mul(new BN8(sParameter.protocolShare)).div(new BN8(BASIS_POINT_MAX));
6742
6856
  }
6743
6857
  function swapExactOutQuoteAtBin(bin, binStep, sParameter, vParameter, outAmount, swapForY) {
6744
6858
  if (swapForY && bin.amountY.isZero()) {
6745
6859
  return {
6746
- amountIn: new BN7(0),
6747
- amountOut: new BN7(0),
6748
- fee: new BN7(0),
6749
- protocolFee: new BN7(0)
6860
+ amountIn: new BN8(0),
6861
+ amountOut: new BN8(0),
6862
+ fee: new BN8(0),
6863
+ protocolFee: new BN8(0)
6750
6864
  };
6751
6865
  }
6752
6866
  if (!swapForY && bin.amountX.isZero()) {
6753
6867
  return {
6754
- amountIn: new BN7(0),
6755
- amountOut: new BN7(0),
6756
- fee: new BN7(0),
6757
- protocolFee: new BN7(0)
6868
+ amountIn: new BN8(0),
6869
+ amountOut: new BN8(0),
6870
+ fee: new BN8(0),
6871
+ protocolFee: new BN8(0)
6758
6872
  };
6759
6873
  }
6760
6874
  let maxAmountOut;
@@ -6790,18 +6904,18 @@ function swapExactOutQuoteAtBin(bin, binStep, sParameter, vParameter, outAmount,
6790
6904
  function swapExactInQuoteAtBin(bin, binStep, sParameter, vParameter, inAmount, swapForY) {
6791
6905
  if (swapForY && bin.amountY.isZero()) {
6792
6906
  return {
6793
- amountIn: new BN7(0),
6794
- amountOut: new BN7(0),
6795
- fee: new BN7(0),
6796
- protocolFee: new BN7(0)
6907
+ amountIn: new BN8(0),
6908
+ amountOut: new BN8(0),
6909
+ fee: new BN8(0),
6910
+ protocolFee: new BN8(0)
6797
6911
  };
6798
6912
  }
6799
6913
  if (!swapForY && bin.amountX.isZero()) {
6800
6914
  return {
6801
- amountIn: new BN7(0),
6802
- amountOut: new BN7(0),
6803
- fee: new BN7(0),
6804
- protocolFee: new BN7(0)
6915
+ amountIn: new BN8(0),
6916
+ amountOut: new BN8(0),
6917
+ fee: new BN8(0),
6918
+ protocolFee: new BN8(0)
6805
6919
  };
6806
6920
  }
6807
6921
  let maxAmountOut;
@@ -6848,7 +6962,7 @@ function getAmountIn(amountOut, price, swapForY) {
6848
6962
  }
6849
6963
 
6850
6964
  // src/dlmm/helpers/strategy.ts
6851
- import { BN as BN8 } from "@coral-xyz/anchor";
6965
+ import { BN as BN9 } from "@coral-xyz/anchor";
6852
6966
  var DEFAULT_MAX_WEIGHT = 2e3;
6853
6967
  var DEFAULT_MIN_WEIGHT = 200;
6854
6968
  function toWeightSpotBalanced(minBinId, maxBinId) {
@@ -7008,7 +7122,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7008
7122
  for (let bin of amounts) {
7009
7123
  amountsInBin.push({
7010
7124
  binId: bin.binId,
7011
- amountX: new BN8(0),
7125
+ amountX: new BN9(0),
7012
7126
  amountY: bin.amount
7013
7127
  });
7014
7128
  }
@@ -7020,7 +7134,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7020
7134
  amountsInBin.push({
7021
7135
  binId: bin.binId,
7022
7136
  amountX: bin.amount,
7023
- amountY: new BN8(0)
7137
+ amountY: new BN9(0)
7024
7138
  });
7025
7139
  }
7026
7140
  }
@@ -7035,7 +7149,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7035
7149
  for (let bin of amountsIntoBidSide) {
7036
7150
  amountsInBin.push({
7037
7151
  binId: bin.binId,
7038
- amountX: new BN8(0),
7152
+ amountX: new BN9(0),
7039
7153
  amountY: bin.amount
7040
7154
  });
7041
7155
  }
@@ -7052,7 +7166,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7052
7166
  amountsInBin.push({
7053
7167
  binId: bin.binId,
7054
7168
  amountX: bin.amount,
7055
- amountY: new BN8(0)
7169
+ amountY: new BN9(0)
7056
7170
  });
7057
7171
  }
7058
7172
  }
@@ -7092,7 +7206,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7092
7206
  for (let bin of amounts) {
7093
7207
  amountsInBin.push({
7094
7208
  binId: bin.binId,
7095
- amountX: new BN8(0),
7209
+ amountX: new BN9(0),
7096
7210
  amountY: bin.amount
7097
7211
  });
7098
7212
  }
@@ -7104,7 +7218,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7104
7218
  amountsInBin.push({
7105
7219
  binId: bin.binId,
7106
7220
  amountX: bin.amount,
7107
- amountY: new BN8(0)
7221
+ amountY: new BN9(0)
7108
7222
  });
7109
7223
  }
7110
7224
  }
@@ -7119,7 +7233,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7119
7233
  for (let bin of amountsIntoBidSide) {
7120
7234
  amountsInBin.push({
7121
7235
  binId: bin.binId,
7122
- amountX: new BN8(0),
7236
+ amountX: new BN9(0),
7123
7237
  amountY: bin.amount
7124
7238
  });
7125
7239
  }
@@ -7136,7 +7250,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7136
7250
  amountsInBin.push({
7137
7251
  binId: bin.binId,
7138
7252
  amountX: bin.amount,
7139
- amountY: new BN8(0)
7253
+ amountY: new BN9(0)
7140
7254
  });
7141
7255
  }
7142
7256
  }
@@ -7176,7 +7290,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7176
7290
  for (let bin of amounts) {
7177
7291
  amountsInBin.push({
7178
7292
  binId: bin.binId,
7179
- amountX: new BN8(0),
7293
+ amountX: new BN9(0),
7180
7294
  amountY: bin.amount
7181
7295
  });
7182
7296
  }
@@ -7188,7 +7302,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7188
7302
  amountsInBin.push({
7189
7303
  binId: bin.binId,
7190
7304
  amountX: bin.amount,
7191
- amountY: new BN8(0)
7305
+ amountY: new BN9(0)
7192
7306
  });
7193
7307
  }
7194
7308
  }
@@ -7203,7 +7317,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7203
7317
  for (let bin of amountsIntoBidSide) {
7204
7318
  amountsInBin.push({
7205
7319
  binId: bin.binId,
7206
- amountX: new BN8(0),
7320
+ amountX: new BN9(0),
7207
7321
  amountY: bin.amount
7208
7322
  });
7209
7323
  }
@@ -7220,7 +7334,7 @@ function toAmountsBothSideByStrategy(activeId, binStep, minBinId, maxBinId, amou
7220
7334
  amountsInBin.push({
7221
7335
  binId: bin.binId,
7222
7336
  amountX: bin.amount,
7223
- amountY: new BN8(0)
7337
+ amountY: new BN9(0)
7224
7338
  });
7225
7339
  }
7226
7340
  }
@@ -7439,7 +7553,7 @@ function toStrategyParameters({
7439
7553
  }
7440
7554
 
7441
7555
  // src/dlmm/helpers/lbPair.ts
7442
- import { AnchorProvider, Program } from "@coral-xyz/anchor";
7556
+ import { AnchorProvider, Program as Program2 } from "@coral-xyz/anchor";
7443
7557
  import { PublicKey as PublicKey4 } from "@solana/web3.js";
7444
7558
  async function getTokensMintFromPoolAddress(connection, poolAddress, opt) {
7445
7559
  const provider = new AnchorProvider(
@@ -7447,7 +7561,7 @@ async function getTokensMintFromPoolAddress(connection, poolAddress, opt) {
7447
7561
  {},
7448
7562
  AnchorProvider.defaultOptions()
7449
7563
  );
7450
- const program = new Program(
7564
+ const program = new Program2(
7451
7565
  IDL,
7452
7566
  LBCLMM_PROGRAM_IDS[opt?.cluster ?? "mainnet-beta"],
7453
7567
  provider
@@ -7469,6 +7583,10 @@ function chunks(array, size) {
7469
7583
  (_, index) => array.slice(index * size, (index + 1) * size)
7470
7584
  );
7471
7585
  }
7586
+ function range(min, max, mapfn) {
7587
+ const length = max - min + 1;
7588
+ return Array.from({ length }, (_, i) => mapfn(min + i));
7589
+ }
7472
7590
  async function chunkedFetchMultiplePoolAccount(program, pks, chunkSize = 100) {
7473
7591
  const accounts = (await Promise.all(
7474
7592
  chunks(pks, chunkSize).map(
@@ -7574,64 +7692,37 @@ async function chunkedGetMultipleAccountInfos(connection, pks, chunkSize = 100)
7574
7692
  )).flat();
7575
7693
  return accountInfos;
7576
7694
  }
7577
- var computeBudgetIx = () => {
7578
- return ComputeBudgetProgram.setComputeUnitLimit({
7579
- units: 14e5
7580
- });
7581
- };
7582
-
7583
- // src/dlmm/index.ts
7584
- import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
7585
- import Decimal4 from "decimal.js";
7586
- import {
7587
- AccountLayout,
7588
- MintLayout,
7589
- NATIVE_MINT as NATIVE_MINT2,
7590
- TOKEN_PROGRAM_ID as TOKEN_PROGRAM_ID2,
7591
- getAssociatedTokenAddressSync as getAssociatedTokenAddressSync2
7592
- } from "@solana/spl-token";
7593
-
7594
- // src/dlmm/error.ts
7595
- import { AnchorError } from "@coral-xyz/anchor";
7596
- var DLMMError = class extends Error {
7597
- errorCode;
7598
- errorName;
7599
- errorMessage;
7600
- constructor(error) {
7601
- let _errorCode = 0;
7602
- let _errorName = "Something went wrong";
7603
- let _errorMessage = "Something went wrong";
7604
- if (error instanceof Error) {
7605
- const anchorError = AnchorError.parse(
7606
- JSON.parse(JSON.stringify(error)).logs
7607
- );
7608
- if (anchorError?.program.toBase58() === LBCLMM_PROGRAM_IDS["mainnet-beta"]) {
7609
- _errorCode = anchorError.error.errorCode.number;
7610
- _errorName = anchorError.error.errorCode.code;
7611
- _errorMessage = anchorError.error.errorMessage;
7612
- }
7613
- } else {
7614
- const idlError = IDL.errors.find((err) => err.code === error);
7615
- if (idlError) {
7616
- _errorCode = idlError.code;
7617
- _errorName = idlError.name;
7618
- _errorMessage = idlError.msg;
7619
- }
7620
- }
7621
- super(_errorMessage);
7622
- this.errorCode = _errorCode;
7623
- this.errorName = _errorName;
7624
- this.errorMessage = _errorMessage;
7695
+ var getEstimatedComputeUnitUsageWithBuffer = async (connection, instructions, feePayer, buffer) => {
7696
+ if (!buffer) {
7697
+ buffer = 0.1;
7625
7698
  }
7626
- };
7627
- var DlmmSdkError = class extends Error {
7628
- name;
7629
- message;
7630
- constructor(name, message) {
7631
- super();
7632
- this.name = name;
7633
- this.message = message;
7699
+ buffer = Math.max(0, buffer);
7700
+ buffer = Math.min(1, buffer);
7701
+ const estimatedComputeUnitUsage = await getSimulationComputeUnits(
7702
+ connection,
7703
+ instructions,
7704
+ feePayer,
7705
+ []
7706
+ );
7707
+ let extraComputeUnitBuffer = estimatedComputeUnitUsage * buffer;
7708
+ if (extraComputeUnitBuffer > MAX_CU_BUFFER) {
7709
+ extraComputeUnitBuffer = MAX_CU_BUFFER;
7710
+ } else if (extraComputeUnitBuffer < MIN_CU_BUFFER) {
7711
+ extraComputeUnitBuffer = MIN_CU_BUFFER;
7634
7712
  }
7713
+ return estimatedComputeUnitUsage + extraComputeUnitBuffer;
7714
+ };
7715
+ var getEstimatedComputeUnitIxWithBuffer = async (connection, instructions, feePayer, buffer) => {
7716
+ const units = await getEstimatedComputeUnitUsageWithBuffer(
7717
+ connection,
7718
+ instructions,
7719
+ feePayer,
7720
+ buffer
7721
+ ).catch((error) => {
7722
+ console.error("Error::getEstimatedComputeUnitUsageWithBuffer", error);
7723
+ return 14e5;
7724
+ });
7725
+ return ComputeBudgetProgram.setComputeUnitLimit({ units });
7635
7726
  };
7636
7727
 
7637
7728
  // src/dlmm/index.ts
@@ -7663,7 +7754,7 @@ var DLMM = class {
7663
7754
  {},
7664
7755
  AnchorProvider2.defaultOptions()
7665
7756
  );
7666
- const program = new Program2(
7757
+ const program = new Program3(
7667
7758
  IDL,
7668
7759
  opt?.programId ?? LBCLMM_PROGRAM_IDS[opt?.cluster ?? "mainnet-beta"],
7669
7760
  provider
@@ -7677,7 +7768,7 @@ var DLMM = class {
7677
7768
  {},
7678
7769
  AnchorProvider2.defaultOptions()
7679
7770
  );
7680
- const program = new Program2(
7771
+ const program = new Program3(
7681
7772
  IDL,
7682
7773
  opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
7683
7774
  provider
@@ -7724,7 +7815,7 @@ var DLMM = class {
7724
7815
  {},
7725
7816
  AnchorProvider2.defaultOptions()
7726
7817
  );
7727
- const program = new Program2(
7818
+ const program = new Program3(
7728
7819
  IDL,
7729
7820
  opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
7730
7821
  provider
@@ -7824,7 +7915,7 @@ var DLMM = class {
7824
7915
  {},
7825
7916
  AnchorProvider2.defaultOptions()
7826
7917
  );
7827
- const program = new Program2(
7918
+ const program = new Program3(
7828
7919
  IDL,
7829
7920
  opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
7830
7921
  provider
@@ -7944,7 +8035,7 @@ var DLMM = class {
7944
8035
  {},
7945
8036
  AnchorProvider2.defaultOptions()
7946
8037
  );
7947
- const program = new Program2(
8038
+ const program = new Program3(
7948
8039
  IDL,
7949
8040
  opt?.programId ?? LBCLMM_PROGRAM_IDS[opt?.cluster ?? "mainnet-beta"],
7950
8041
  provider
@@ -7970,7 +8061,7 @@ var DLMM = class {
7970
8061
  {},
7971
8062
  AnchorProvider2.defaultOptions()
7972
8063
  );
7973
- const program = new Program2(
8064
+ const program = new Program3(
7974
8065
  IDL,
7975
8066
  opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
7976
8067
  provider
@@ -7996,8 +8087,8 @@ var DLMM = class {
7996
8087
  const binArrayPubkeySet = /* @__PURE__ */ new Set();
7997
8088
  const lbPairSet = /* @__PURE__ */ new Set();
7998
8089
  positions.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
7999
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8000
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8090
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
8091
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
8001
8092
  const [lowerBinArrayPubKey] = deriveBinArray(
8002
8093
  lbPair,
8003
8094
  lowerBinArrayIndex,
@@ -8021,8 +8112,8 @@ var DLMM = class {
8021
8112
  const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
8022
8113
  const lbPairSetV2 = /* @__PURE__ */ new Set();
8023
8114
  positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
8024
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8025
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8115
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
8116
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
8026
8117
  const [lowerBinArrayPubKey] = deriveBinArray(
8027
8118
  lbPair,
8028
8119
  lowerBinArrayIndex,
@@ -8176,15 +8267,15 @@ var DLMM = class {
8176
8267
  mintYDecimal: mintY.decimals
8177
8268
  });
8178
8269
  });
8179
- const onChainTimestamp = new BN9(
8270
+ const onChainTimestamp = new BN10(
8180
8271
  clockAccInfo.data.readBigInt64LE(32).toString()
8181
8272
  ).toNumber();
8182
8273
  const positionsMap = /* @__PURE__ */ new Map();
8183
8274
  for (let position of positions) {
8184
8275
  const { account, publicKey: positionPubKey } = position;
8185
8276
  const { upperBinId, lowerBinId, lbPair } = account;
8186
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8187
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8277
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
8278
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
8188
8279
  const [lowerBinArrayPubKey] = deriveBinArray(
8189
8280
  lbPair,
8190
8281
  lowerBinArrayIndex,
@@ -8251,8 +8342,8 @@ var DLMM = class {
8251
8342
  for (let position of positionsV2) {
8252
8343
  const { account, publicKey: positionPubKey } = position;
8253
8344
  const { upperBinId, lowerBinId, lbPair, feeOwner } = account;
8254
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8255
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8345
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
8346
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
8256
8347
  const [lowerBinArrayPubKey] = deriveBinArray(
8257
8348
  lbPair,
8258
8349
  lowerBinArrayIndex,
@@ -8326,7 +8417,7 @@ var DLMM = class {
8326
8417
  {},
8327
8418
  AnchorProvider2.defaultOptions()
8328
8419
  );
8329
- const program = new Program2(
8420
+ const program = new Program3(
8330
8421
  IDL,
8331
8422
  opt?.programId ?? LBCLMM_PROGRAM_IDS[cluster],
8332
8423
  provider
@@ -8338,8 +8429,8 @@ var DLMM = class {
8338
8429
  return Promise.all(
8339
8430
  positionsState.map(async ({ lbPair, lowerBinId }, idx) => {
8340
8431
  const position = positions[idx];
8341
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8342
- const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
8432
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
8433
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN10(1));
8343
8434
  const [lowerBinArrayPubKey] = deriveBinArray(
8344
8435
  lbPair,
8345
8436
  lowerBinArrayIndex,
@@ -8370,11 +8461,11 @@ var DLMM = class {
8370
8461
  );
8371
8462
  }
8372
8463
  static getPricePerLamport(tokenXDecimal, tokenYDecimal, price) {
8373
- return new Decimal4(price).mul(new Decimal4(10 ** (tokenYDecimal - tokenXDecimal))).toString();
8464
+ return new Decimal5(price).mul(new Decimal5(10 ** (tokenYDecimal - tokenXDecimal))).toString();
8374
8465
  }
8375
8466
  static getBinIdFromPrice(price, binStep, min) {
8376
- const binStepNum = new Decimal4(binStep).div(new Decimal4(BASIS_POINT_MAX));
8377
- const binId = new Decimal4(price).log().dividedBy(new Decimal4(1).add(binStepNum).log());
8467
+ const binStepNum = new Decimal5(binStep).div(new Decimal5(BASIS_POINT_MAX));
8468
+ const binId = new Decimal5(price).log().dividedBy(new Decimal5(1).add(binStepNum).log());
8378
8469
  return (min ? binId.floor() : binId.ceil()).toNumber();
8379
8470
  }
8380
8471
  /** Public methods */
@@ -8384,7 +8475,7 @@ var DLMM = class {
8384
8475
  {},
8385
8476
  AnchorProvider2.defaultOptions()
8386
8477
  );
8387
- const program = new Program2(
8478
+ const program = new Program3(
8388
8479
  IDL,
8389
8480
  opt?.programId ?? LBCLMM_PROGRAM_IDS[opt.cluster],
8390
8481
  provider
@@ -8433,7 +8524,7 @@ var DLMM = class {
8433
8524
  {},
8434
8525
  AnchorProvider2.defaultOptions()
8435
8526
  );
8436
- const program = new Program2(
8527
+ const program = new Program3(
8437
8528
  IDL,
8438
8529
  opt?.programId ?? LBCLMM_PROGRAM_IDS[opt.cluster],
8439
8530
  provider
@@ -8481,7 +8572,7 @@ var DLMM = class {
8481
8572
  {},
8482
8573
  AnchorProvider2.defaultOptions()
8483
8574
  );
8484
- const program = new Program2(
8575
+ const program = new Program3(
8485
8576
  IDL,
8486
8577
  opt?.programId ?? LBCLMM_PROGRAM_IDS[opt.cluster],
8487
8578
  provider
@@ -8617,7 +8708,7 @@ var DLMM = class {
8617
8708
  while (!shouldStop) {
8618
8709
  const binArrayIndex = findNextBinArrayIndexWithLiquidity(
8619
8710
  swapForY,
8620
- new BN9(activeIdToLoop),
8711
+ new BN10(activeIdToLoop),
8621
8712
  this.lbPair,
8622
8713
  this.binArrayBitmapExtension?.account ?? null
8623
8714
  );
@@ -8659,9 +8750,9 @@ var DLMM = class {
8659
8750
  return binArrays;
8660
8751
  }
8661
8752
  static calculateFeeInfo(baseFactor, binStep) {
8662
- const baseFeeRate = new BN9(baseFactor).mul(new BN9(binStep)).mul(new BN9(10));
8663
- const baseFeeRatePercentage = new Decimal4(baseFeeRate.toString()).mul(new Decimal4(100)).div(new Decimal4(FEE_PRECISION.toString()));
8664
- const maxFeeRatePercentage = new Decimal4(MAX_FEE_RATE.toString()).mul(new Decimal4(100)).div(new Decimal4(FEE_PRECISION.toString()));
8753
+ const baseFeeRate = new BN10(baseFactor).mul(new BN10(binStep)).mul(new BN10(10));
8754
+ const baseFeeRatePercentage = new Decimal5(baseFeeRate.toString()).mul(new Decimal5(100)).div(new Decimal5(FEE_PRECISION.toString()));
8755
+ const maxFeeRatePercentage = new Decimal5(MAX_FEE_RATE.toString()).mul(new Decimal5(100)).div(new Decimal5(FEE_PRECISION.toString()));
8665
8756
  return {
8666
8757
  baseFeeRatePercentage,
8667
8758
  maxFeeRatePercentage
@@ -8675,7 +8766,7 @@ var DLMM = class {
8675
8766
  getFeeInfo() {
8676
8767
  const { baseFactor, protocolShare } = this.lbPair.parameters;
8677
8768
  const { baseFeeRatePercentage, maxFeeRatePercentage } = DLMM.calculateFeeInfo(baseFactor, this.lbPair.binStep);
8678
- const protocolFeePercentage = new Decimal4(protocolShare.toString()).mul(new Decimal4(100)).div(new Decimal4(BASIS_POINT_MAX));
8769
+ const protocolFeePercentage = new Decimal5(protocolShare.toString()).mul(new Decimal5(100)).div(new Decimal5(BASIS_POINT_MAX));
8679
8770
  return {
8680
8771
  baseFeeRatePercentage,
8681
8772
  maxFeeRatePercentage,
@@ -8688,7 +8779,7 @@ var DLMM = class {
8688
8779
  */
8689
8780
  getDynamicFee() {
8690
8781
  let vParameterClone = Object.assign({}, this.lbPair.vParameters);
8691
- let activeId = new BN9(this.lbPair.activeId);
8782
+ let activeId = new BN10(this.lbPair.activeId);
8692
8783
  const sParameters2 = this.lbPair.parameters;
8693
8784
  const currentTimestamp = Date.now() / 1e3;
8694
8785
  this.updateReference(
@@ -8707,7 +8798,7 @@ var DLMM = class {
8707
8798
  sParameters2,
8708
8799
  vParameterClone
8709
8800
  );
8710
- return new Decimal4(totalFee.toString()).div(new Decimal4(FEE_PRECISION.toString())).mul(100);
8801
+ return new Decimal5(totalFee.toString()).div(new Decimal5(FEE_PRECISION.toString())).mul(100);
8711
8802
  }
8712
8803
  /**
8713
8804
  * The function `getEmissionRate` returns the emission rates for two rewards.
@@ -8720,8 +8811,8 @@ var DLMM = class {
8720
8811
  ({ rewardRate, rewardDurationEnd }) => now > rewardDurationEnd.toNumber() ? void 0 : rewardRate
8721
8812
  );
8722
8813
  return {
8723
- rewardOne: rewardOneEmissionRate ? new Decimal4(rewardOneEmissionRate.toString()).div(PRECISION) : void 0,
8724
- rewardTwo: rewardTwoEmissionRate ? new Decimal4(rewardTwoEmissionRate.toString()).div(PRECISION) : void 0
8814
+ rewardOne: rewardOneEmissionRate ? new Decimal5(rewardOneEmissionRate.toString()).div(PRECISION) : void 0,
8815
+ rewardTwo: rewardTwoEmissionRate ? new Decimal5(rewardTwoEmissionRate.toString()).div(PRECISION) : void 0
8725
8816
  };
8726
8817
  }
8727
8818
  /**
@@ -8783,15 +8874,15 @@ var DLMM = class {
8783
8874
  * @returns an object with two properties: "activeBin" and "bins". The value of "activeBin" is the
8784
8875
  * active bin ID of the lbPair, and the value of "bins" is an array of BinLiquidity objects.
8785
8876
  */
8786
- async getBinsBetweenLowerAndUpperBound(lowerBinId, upperBinId, lowerBinArrays, upperBinArrays) {
8877
+ async getBinsBetweenLowerAndUpperBound(lowerBinId, upperBinId, lowerBinArray, upperBinArray) {
8787
8878
  const bins = await this.getBins(
8788
8879
  this.pubkey,
8789
8880
  lowerBinId,
8790
8881
  upperBinId,
8791
8882
  this.tokenX.decimal,
8792
8883
  this.tokenY.decimal,
8793
- lowerBinArrays,
8794
- upperBinArrays
8884
+ lowerBinArray,
8885
+ upperBinArray
8795
8886
  );
8796
8887
  return { activeBin: this.lbPair.activeId, bins };
8797
8888
  }
@@ -8814,7 +8905,7 @@ var DLMM = class {
8814
8905
  * @returns {string} real price of bin
8815
8906
  */
8816
8907
  fromPricePerLamport(pricePerLamport) {
8817
- return new Decimal4(pricePerLamport).div(new Decimal4(10 ** (this.tokenY.decimal - this.tokenX.decimal))).toString();
8908
+ return new Decimal5(pricePerLamport).div(new Decimal5(10 ** (this.tokenY.decimal - this.tokenX.decimal))).toString();
8818
8909
  }
8819
8910
  /**
8820
8911
  * The function retrieves the active bin ID and its corresponding price.
@@ -8904,8 +8995,8 @@ var DLMM = class {
8904
8995
  }
8905
8996
  const binArrayPubkeySet = /* @__PURE__ */ new Set();
8906
8997
  positions.forEach(({ account: { upperBinId, lowerBinId } }) => {
8907
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8908
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
8998
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
8999
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
8909
9000
  const [lowerBinArrayPubKey] = deriveBinArray(
8910
9001
  this.pubkey,
8911
9002
  lowerBinArrayIndex,
@@ -8924,8 +9015,8 @@ var DLMM = class {
8924
9015
  );
8925
9016
  const binArrayPubkeySetV2 = /* @__PURE__ */ new Set();
8926
9017
  positionsV2.forEach(({ account: { upperBinId, lowerBinId, lbPair } }) => {
8927
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8928
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
9018
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
9019
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
8929
9020
  const [lowerBinArrayPubKey] = deriveBinArray(
8930
9021
  this.pubkey,
8931
9022
  lowerBinArrayIndex,
@@ -8982,14 +9073,14 @@ var DLMM = class {
8982
9073
  }
8983
9074
  if (!lbPairAccInfo)
8984
9075
  throw new Error(`LB Pair account ${this.pubkey.toBase58()} not found`);
8985
- const onChainTimestamp = new BN9(
9076
+ const onChainTimestamp = new BN10(
8986
9077
  clockAccInfo.data.readBigInt64LE(32).toString()
8987
9078
  ).toNumber();
8988
9079
  const userPositions = await Promise.all(
8989
9080
  positions.map(async ({ publicKey, account }) => {
8990
9081
  const { lowerBinId, upperBinId } = account;
8991
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
8992
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
9082
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
9083
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
8993
9084
  const [lowerBinArrayPubKey] = deriveBinArray(
8994
9085
  this.pubkey,
8995
9086
  lowerBinArrayIndex,
@@ -9027,8 +9118,8 @@ var DLMM = class {
9027
9118
  const userPositionsV2 = await Promise.all(
9028
9119
  positionsV2.map(async ({ publicKey, account }) => {
9029
9120
  const { lowerBinId, upperBinId, feeOwner } = account;
9030
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
9031
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
9121
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
9122
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
9032
9123
  const [lowerBinArrayPubKey] = deriveBinArray(
9033
9124
  this.pubkey,
9034
9125
  lowerBinArrayIndex,
@@ -9070,10 +9161,10 @@ var DLMM = class {
9070
9161
  }
9071
9162
  async quoteCreatePosition({ strategy }) {
9072
9163
  const { minBinId, maxBinId } = strategy;
9073
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9074
- const upperBinArrayIndex = BN9.max(
9075
- binIdToBinArrayIndex(new BN9(maxBinId)),
9076
- lowerBinArrayIndex.add(new BN9(1))
9164
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(minBinId));
9165
+ const upperBinArrayIndex = BN10.max(
9166
+ binIdToBinArrayIndex(new BN10(maxBinId)),
9167
+ lowerBinArrayIndex.add(new BN10(1))
9077
9168
  );
9078
9169
  const binArraysCount = (await this.binArraysToBeCreate(lowerBinArrayIndex, upperBinArrayIndex)).length;
9079
9170
  const positionCount = Math.ceil((maxBinId - minBinId + 1) / MAX_BIN_PER_TX);
@@ -9097,29 +9188,34 @@ var DLMM = class {
9097
9188
  maxBinId,
9098
9189
  user
9099
9190
  }) {
9100
- const setComputeUnitLimitIx = computeBudgetIx();
9101
9191
  const createPositionIx = await this.program.methods.initializePosition(minBinId, maxBinId - minBinId + 1).accounts({
9102
9192
  payer: user,
9103
9193
  position: positionPubKey,
9104
9194
  lbPair: this.pubkey,
9105
9195
  owner: user
9106
9196
  }).instruction();
9107
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9108
- const upperBinArrayIndex = BN9.max(
9109
- lowerBinArrayIndex.add(new BN9(1)),
9110
- binIdToBinArrayIndex(new BN9(maxBinId))
9197
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(minBinId));
9198
+ const upperBinArrayIndex = BN10.max(
9199
+ lowerBinArrayIndex.add(new BN10(1)),
9200
+ binIdToBinArrayIndex(new BN10(maxBinId))
9111
9201
  );
9112
9202
  const createBinArrayIxs = await this.createBinArraysIfNeeded(
9113
9203
  upperBinArrayIndex,
9114
9204
  lowerBinArrayIndex,
9115
9205
  user
9116
9206
  );
9207
+ const instructions = [createPositionIx, ...createBinArrayIxs];
9208
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
9209
+ this.program.provider.connection,
9210
+ instructions,
9211
+ user
9212
+ );
9117
9213
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9118
9214
  return new Transaction({
9119
9215
  blockhash,
9120
9216
  lastValidBlockHeight,
9121
9217
  feePayer: user
9122
- }).add(setComputeUnitLimitIx, createPositionIx, ...createBinArrayIxs);
9218
+ }).add(setCUIx, ...instructions);
9123
9219
  }
9124
9220
  /**
9125
9221
  * The function `initializePositionAndAddLiquidityByStrategy` function is used to initializes a position and adds liquidity
@@ -9143,8 +9239,7 @@ var DLMM = class {
9143
9239
  }) {
9144
9240
  const { maxBinId, minBinId } = strategy;
9145
9241
  const maxActiveBinSlippage = slippage ? Math.ceil(slippage / (this.lbPair.binStep / 100)) : MAX_ACTIVE_BIN_SLIPPAGE;
9146
- const setComputeUnitLimitIx = computeBudgetIx();
9147
- const preInstructions = [setComputeUnitLimitIx];
9242
+ const preInstructions = [];
9148
9243
  const initializePositionIx = await this.program.methods.initializePosition(minBinId, maxBinId - minBinId + 1).accounts({
9149
9244
  payer: user,
9150
9245
  position: positionPubKey,
@@ -9152,15 +9247,15 @@ var DLMM = class {
9152
9247
  owner: user
9153
9248
  }).instruction();
9154
9249
  preInstructions.push(initializePositionIx);
9155
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9250
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(minBinId));
9156
9251
  const [binArrayLower] = deriveBinArray(
9157
9252
  this.pubkey,
9158
9253
  lowerBinArrayIndex,
9159
9254
  this.program.programId
9160
9255
  );
9161
- const upperBinArrayIndex = BN9.max(
9162
- lowerBinArrayIndex.add(new BN9(1)),
9163
- binIdToBinArrayIndex(new BN9(maxBinId))
9256
+ const upperBinArrayIndex = BN10.max(
9257
+ lowerBinArrayIndex.add(new BN10(1)),
9258
+ binIdToBinArrayIndex(new BN10(maxBinId))
9164
9259
  );
9165
9260
  const [binArrayUpper] = deriveBinArray(
9166
9261
  this.pubkey,
@@ -9214,8 +9309,8 @@ var DLMM = class {
9214
9309
  const closeWrappedSOLIx = await unwrapSOLInstruction(user);
9215
9310
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
9216
9311
  }
9217
- const minBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9218
- const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(maxBinId));
9312
+ const minBinArrayIndex = binIdToBinArrayIndex(new BN10(minBinId));
9313
+ const maxBinArrayIndex = binIdToBinArrayIndex(new BN10(maxBinId));
9219
9314
  const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
9220
9315
  const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
9221
9316
  const activeId = this.lbPair.activeId;
@@ -9244,13 +9339,24 @@ var DLMM = class {
9244
9339
  tokenYProgram: TOKEN_PROGRAM_ID2
9245
9340
  };
9246
9341
  const programMethod = this.program.methods.addLiquidityByStrategy(liquidityParams);
9247
- const createPositionTx = await programMethod.accounts(addLiquidityAccounts).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
9342
+ const addLiquidityIx = await programMethod.accounts(addLiquidityAccounts).instruction();
9343
+ const instructions = [
9344
+ ...preInstructions,
9345
+ addLiquidityIx,
9346
+ ...postInstructions
9347
+ ];
9348
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
9349
+ this.program.provider.connection,
9350
+ instructions,
9351
+ user
9352
+ );
9353
+ instructions.unshift(setCUIx);
9248
9354
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9249
9355
  return new Transaction({
9250
9356
  blockhash,
9251
9357
  lastValidBlockHeight,
9252
9358
  feePayer: user
9253
- }).add(createPositionTx);
9359
+ }).add(...instructions);
9254
9360
  }
9255
9361
  /**
9256
9362
  * The function `initializePositionAndAddLiquidityByWeight` function is used to initializes a position and adds liquidity
@@ -9287,15 +9393,15 @@ var DLMM = class {
9287
9393
  owner: user
9288
9394
  }).instruction();
9289
9395
  preInstructions.push(initializePositionIx);
9290
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
9396
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
9291
9397
  const [binArrayLower] = deriveBinArray(
9292
9398
  this.pubkey,
9293
9399
  lowerBinArrayIndex,
9294
9400
  this.program.programId
9295
9401
  );
9296
- const upperBinArrayIndex = BN9.max(
9297
- lowerBinArrayIndex.add(new BN9(1)),
9298
- binIdToBinArrayIndex(new BN9(upperBinId))
9402
+ const upperBinArrayIndex = BN10.max(
9403
+ lowerBinArrayIndex.add(new BN10(1)),
9404
+ binIdToBinArrayIndex(new BN10(upperBinId))
9299
9405
  );
9300
9406
  const [binArrayUpper] = deriveBinArray(
9301
9407
  this.pubkey,
@@ -9349,11 +9455,10 @@ var DLMM = class {
9349
9455
  const closeWrappedSOLIx = await unwrapSOLInstruction(user);
9350
9456
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
9351
9457
  }
9352
- const setComputeUnitLimitIx = computeBudgetIx();
9353
9458
  const minBinId = Math.min(...binIds);
9354
9459
  const maxBinId = Math.max(...binIds);
9355
- const minBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9356
- const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(maxBinId));
9460
+ const minBinArrayIndex = binIdToBinArrayIndex(new BN10(minBinId));
9461
+ const maxBinArrayIndex = binIdToBinArrayIndex(new BN10(maxBinId));
9357
9462
  const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
9358
9463
  const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
9359
9464
  const activeId = this.lbPair.activeId;
@@ -9414,19 +9519,34 @@ var DLMM = class {
9414
9519
  const isOneSideDeposit = totalXAmount.isZero() || totalYAmount.isZero();
9415
9520
  const programMethod = isOneSideDeposit ? this.program.methods.addLiquidityOneSide(oneSideLiquidityParams) : this.program.methods.addLiquidityByWeight(liquidityParams);
9416
9521
  if (xYAmountDistribution.length < MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX) {
9417
- const addLiqTx2 = await programMethod.accounts(
9522
+ const addLiqIx2 = await programMethod.accounts(
9418
9523
  isOneSideDeposit ? oneSideAddLiquidityAccounts : addLiquidityAccounts
9419
- ).preInstructions([setComputeUnitLimitIx, ...preInstructions]).postInstructions(postInstructions).transaction();
9524
+ ).instruction();
9525
+ const instructions = [...preInstructions, addLiqIx2, ...postInstructions];
9526
+ const setCUIx2 = await getEstimatedComputeUnitIxWithBuffer(
9527
+ this.program.provider.connection,
9528
+ instructions,
9529
+ user
9530
+ );
9531
+ instructions.unshift(setCUIx2);
9420
9532
  const { blockhash: blockhash2, lastValidBlockHeight: lastValidBlockHeight2 } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9421
9533
  return new Transaction({
9422
9534
  blockhash: blockhash2,
9423
9535
  lastValidBlockHeight: lastValidBlockHeight2,
9424
9536
  feePayer: user
9425
- }).add(addLiqTx2);
9537
+ }).add(...instructions);
9426
9538
  }
9427
- const addLiqTx = await programMethod.accounts(
9539
+ const addLiqIx = await programMethod.accounts(
9428
9540
  isOneSideDeposit ? oneSideAddLiquidityAccounts : addLiquidityAccounts
9429
- ).preInstructions([setComputeUnitLimitIx]).transaction();
9541
+ ).instruction();
9542
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
9543
+ this.program.provider.connection,
9544
+ [addLiqIx],
9545
+ user,
9546
+ DEFAULT_ADD_LIQUIDITY_CU
9547
+ // The function return multiple transactions that dependent on each other, simulation will fail
9548
+ );
9549
+ const mainInstructions = [setCUIx, addLiqIx];
9430
9550
  const transactions = [];
9431
9551
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9432
9552
  if (preInstructions.length) {
@@ -9441,7 +9561,7 @@ var DLMM = class {
9441
9561
  blockhash,
9442
9562
  lastValidBlockHeight,
9443
9563
  feePayer: user
9444
- }).add(addLiqTx);
9564
+ }).add(...mainInstructions);
9445
9565
  transactions.push(mainTx);
9446
9566
  if (postInstructions.length) {
9447
9567
  const postInstructionsTx = new Transaction({
@@ -9476,10 +9596,8 @@ var DLMM = class {
9476
9596
  const { maxBinId, minBinId } = strategy;
9477
9597
  const maxActiveBinSlippage = slippage ? Math.ceil(slippage / (this.lbPair.binStep / 100)) : MAX_ACTIVE_BIN_SLIPPAGE;
9478
9598
  const preInstructions = [];
9479
- const setComputeUnitLimitIx = computeBudgetIx();
9480
- preInstructions.push(setComputeUnitLimitIx);
9481
- const minBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9482
- const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(maxBinId));
9599
+ const minBinArrayIndex = binIdToBinArrayIndex(new BN10(minBinId));
9600
+ const maxBinArrayIndex = binIdToBinArrayIndex(new BN10(maxBinId));
9483
9601
  const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
9484
9602
  const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
9485
9603
  const strategyParameters = toStrategyParameters(strategy);
@@ -9487,9 +9605,9 @@ var DLMM = class {
9487
9605
  positionPubKey
9488
9606
  );
9489
9607
  const lowerBinArrayIndex = binIdToBinArrayIndex(
9490
- new BN9(positionAccount.lowerBinId)
9608
+ new BN10(positionAccount.lowerBinId)
9491
9609
  );
9492
- const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
9610
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN10(1));
9493
9611
  const [binArrayLower] = deriveBinArray(
9494
9612
  this.pubkey,
9495
9613
  lowerBinArrayIndex,
@@ -9571,13 +9689,24 @@ var DLMM = class {
9571
9689
  tokenYProgram: TOKEN_PROGRAM_ID2
9572
9690
  };
9573
9691
  const programMethod = this.program.methods.addLiquidityByStrategy(liquidityParams);
9574
- const createPositionTx = await programMethod.accounts(addLiquidityAccounts).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
9692
+ const addLiquidityIx = await programMethod.accounts(addLiquidityAccounts).instruction();
9693
+ const instructions = [
9694
+ ...preInstructions,
9695
+ addLiquidityIx,
9696
+ ...postInstructions
9697
+ ];
9698
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
9699
+ this.program.provider.connection,
9700
+ instructions,
9701
+ user
9702
+ );
9703
+ instructions.unshift(setCUIx);
9575
9704
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9576
9705
  return new Transaction({
9577
9706
  blockhash,
9578
9707
  lastValidBlockHeight,
9579
9708
  feePayer: user
9580
- }).add(createPositionTx);
9709
+ }).add(...instructions);
9581
9710
  }
9582
9711
  /**
9583
9712
  * The `addLiquidityByWeight` function is used to add liquidity to existing position
@@ -9614,8 +9743,8 @@ var DLMM = class {
9614
9743
  );
9615
9744
  const minBinId = Math.min(...binIds);
9616
9745
  const maxBinId = Math.max(...binIds);
9617
- const minBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9618
- const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(maxBinId));
9746
+ const minBinArrayIndex = binIdToBinArrayIndex(new BN10(minBinId));
9747
+ const maxBinArrayIndex = binIdToBinArrayIndex(new BN10(maxBinId));
9619
9748
  const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
9620
9749
  const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
9621
9750
  const activeId = this.lbPair.activeId;
@@ -9633,16 +9762,16 @@ var DLMM = class {
9633
9762
  throw new Error("No liquidity to add");
9634
9763
  }
9635
9764
  const lowerBinArrayIndex = binIdToBinArrayIndex(
9636
- new BN9(positionAccount.lowerBinId)
9765
+ new BN10(positionAccount.lowerBinId)
9637
9766
  );
9638
9767
  const [binArrayLower] = deriveBinArray(
9639
9768
  this.pubkey,
9640
9769
  lowerBinArrayIndex,
9641
9770
  this.program.programId
9642
9771
  );
9643
- const upperBinArrayIndex = BN9.max(
9644
- lowerBinArrayIndex.add(new BN9(1)),
9645
- binIdToBinArrayIndex(new BN9(positionAccount.upperBinId))
9772
+ const upperBinArrayIndex = BN10.max(
9773
+ lowerBinArrayIndex.add(new BN10(1)),
9774
+ binIdToBinArrayIndex(new BN10(positionAccount.upperBinId))
9646
9775
  );
9647
9776
  const [binArrayUpper] = deriveBinArray(
9648
9777
  this.pubkey,
@@ -9697,7 +9826,6 @@ var DLMM = class {
9697
9826
  const closeWrappedSOLIx = await unwrapSOLInstruction(user);
9698
9827
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
9699
9828
  }
9700
- const setComputeUnitLimitIx = computeBudgetIx();
9701
9829
  const liquidityParams = {
9702
9830
  amountX: totalXAmount,
9703
9831
  amountY: totalYAmount,
@@ -9742,19 +9870,32 @@ var DLMM = class {
9742
9870
  const isOneSideDeposit = totalXAmount.isZero() || totalYAmount.isZero();
9743
9871
  const programMethod = isOneSideDeposit ? this.program.methods.addLiquidityOneSide(oneSideLiquidityParams) : this.program.methods.addLiquidityByWeight(liquidityParams);
9744
9872
  if (xYAmountDistribution.length < MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX) {
9745
- const addLiqTx2 = await programMethod.accounts(
9873
+ const addLiqIx2 = await programMethod.accounts(
9746
9874
  isOneSideDeposit ? oneSideAddLiquidityAccounts : addLiquidityAccounts
9747
- ).preInstructions([setComputeUnitLimitIx, ...preInstructions]).postInstructions(postInstructions).transaction();
9875
+ ).instruction();
9876
+ const instructions = [...preInstructions, addLiqIx2, ...postInstructions];
9877
+ const setCUIx2 = await getEstimatedComputeUnitIxWithBuffer(
9878
+ this.program.provider.connection,
9879
+ instructions,
9880
+ user
9881
+ );
9882
+ instructions.unshift(setCUIx2);
9748
9883
  const { blockhash: blockhash2, lastValidBlockHeight: lastValidBlockHeight2 } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9749
9884
  return new Transaction({
9750
9885
  blockhash: blockhash2,
9751
9886
  lastValidBlockHeight: lastValidBlockHeight2,
9752
9887
  feePayer: user
9753
- }).add(addLiqTx2);
9888
+ }).add(...instructions);
9754
9889
  }
9755
- const addLiqTx = await programMethod.accounts(
9890
+ const addLiqIx = await programMethod.accounts(
9756
9891
  isOneSideDeposit ? oneSideAddLiquidityAccounts : addLiquidityAccounts
9757
- ).preInstructions([setComputeUnitLimitIx]).transaction();
9892
+ ).instruction();
9893
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
9894
+ this.program.provider.connection,
9895
+ [addLiqIx],
9896
+ user
9897
+ );
9898
+ const mainInstructions = [setCUIx, addLiqIx];
9758
9899
  const transactions = [];
9759
9900
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9760
9901
  if (preInstructions.length) {
@@ -9769,7 +9910,7 @@ var DLMM = class {
9769
9910
  blockhash,
9770
9911
  lastValidBlockHeight,
9771
9912
  feePayer: user
9772
- }).add(addLiqTx);
9913
+ }).add(...mainInstructions);
9773
9914
  transactions.push(mainTx);
9774
9915
  if (postInstructions.length) {
9775
9916
  const postInstructionsTx = new Transaction({
@@ -9801,8 +9942,8 @@ var DLMM = class {
9801
9942
  }) {
9802
9943
  const { lbPair, lowerBinId, owner, feeOwner } = await this.program.account.positionV2.fetch(position);
9803
9944
  const { reserveX, reserveY, tokenXMint, tokenYMint } = await this.program.account.lbPair.fetch(lbPair);
9804
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
9805
- const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
9945
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
9946
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN10(1));
9806
9947
  const [binArrayLower] = deriveBinArray(
9807
9948
  lbPair,
9808
9949
  lowerBinArrayIndex,
@@ -9814,8 +9955,6 @@ var DLMM = class {
9814
9955
  this.program.programId
9815
9956
  );
9816
9957
  const preInstructions = [];
9817
- const setComputeUnitLimitIx = computeBudgetIx();
9818
- preInstructions.push(setComputeUnitLimitIx);
9819
9958
  const walletToReceiveFee = feeOwner.equals(PublicKey6.default) ? user : feeOwner;
9820
9959
  const [
9821
9960
  { ataPubKey: userTokenX, ix: createPayerTokenXIx },
@@ -9882,7 +10021,7 @@ var DLMM = class {
9882
10021
  user
9883
10022
  );
9884
10023
  rewardAtaIx && preInstructions.push(rewardAtaIx);
9885
- const claimRewardIx = await this.program.methods.claimReward(new BN9(i)).accounts({
10024
+ const claimRewardIx = await this.program.methods.claimReward(new BN10(i)).accounts({
9886
10025
  lbPair: this.pubkey,
9887
10026
  sender: user,
9888
10027
  position,
@@ -9919,11 +10058,11 @@ var DLMM = class {
9919
10058
  }
9920
10059
  const minBinId = Math.min(...binIds);
9921
10060
  const maxBinId = Math.max(...binIds);
9922
- const minBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
9923
- const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(maxBinId));
10061
+ const minBinArrayIndex = binIdToBinArrayIndex(new BN10(minBinId));
10062
+ const maxBinArrayIndex = binIdToBinArrayIndex(new BN10(maxBinId));
9924
10063
  const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
9925
10064
  const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
9926
- const removeLiquidityTx = await this.program.methods.removeLiquidityByRange(minBinId, maxBinId, bps.toNumber()).accounts({
10065
+ const removeLiquidityIx = await this.program.methods.removeLiquidityByRange(minBinId, maxBinId, bps.toNumber()).accounts({
9927
10066
  position,
9928
10067
  lbPair,
9929
10068
  userTokenX,
@@ -9938,26 +10077,43 @@ var DLMM = class {
9938
10077
  tokenXProgram: TOKEN_PROGRAM_ID2,
9939
10078
  tokenYProgram: TOKEN_PROGRAM_ID2,
9940
10079
  sender: user
9941
- }).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
9942
- const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
10080
+ }).instruction();
10081
+ const instructions = [
10082
+ ...preInstructions,
10083
+ removeLiquidityIx,
10084
+ ...postInstructions
10085
+ ];
10086
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
10087
+ this.program.provider.connection,
10088
+ instructions,
10089
+ user
10090
+ );
10091
+ instructions.unshift(setCUIx);
9943
10092
  if (secondTransactionsIx.length) {
10093
+ const setCUIx2 = await getEstimatedComputeUnitIxWithBuffer(
10094
+ this.program.provider.connection,
10095
+ secondTransactionsIx,
10096
+ user
10097
+ );
10098
+ const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9944
10099
  const claimRewardsTx = new Transaction({
9945
10100
  blockhash,
9946
10101
  lastValidBlockHeight,
9947
10102
  feePayer: user
9948
- }).add(...secondTransactionsIx);
10103
+ }).add(setCUIx2, ...secondTransactionsIx);
9949
10104
  const mainTx = new Transaction({
9950
10105
  blockhash,
9951
10106
  lastValidBlockHeight,
9952
10107
  feePayer: user
9953
- }).add(removeLiquidityTx);
10108
+ }).add(...instructions);
9954
10109
  return [mainTx, claimRewardsTx];
9955
10110
  } else {
10111
+ const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
9956
10112
  return new Transaction({
9957
10113
  blockhash,
9958
10114
  lastValidBlockHeight,
9959
10115
  feePayer: user
9960
- }).add(removeLiquidityTx);
10116
+ }).add(...instructions);
9961
10117
  }
9962
10118
  }
9963
10119
  /**
@@ -9974,13 +10130,13 @@ var DLMM = class {
9974
10130
  const { lowerBinId } = await this.program.account.positionV2.fetch(
9975
10131
  position.publicKey
9976
10132
  );
9977
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
10133
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
9978
10134
  const [binArrayLower] = deriveBinArray(
9979
10135
  this.pubkey,
9980
10136
  lowerBinArrayIndex,
9981
10137
  this.program.programId
9982
10138
  );
9983
- const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
10139
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN10(1));
9984
10140
  const [binArrayUpper] = deriveBinArray(
9985
10141
  this.pubkey,
9986
10142
  upperBinArrayIndex,
@@ -10021,7 +10177,7 @@ var DLMM = class {
10021
10177
  const currentTimestamp = Date.now() / 1e3;
10022
10178
  let outAmountLeft = outAmount;
10023
10179
  let vParameterClone = Object.assign({}, this.lbPair.vParameters);
10024
- let activeId = new BN9(this.lbPair.activeId);
10180
+ let activeId = new BN10(this.lbPair.activeId);
10025
10181
  const binStep = this.lbPair.binStep;
10026
10182
  const sParameters2 = this.lbPair.parameters;
10027
10183
  this.updateReference(
@@ -10032,9 +10188,9 @@ var DLMM = class {
10032
10188
  );
10033
10189
  let startBinId = activeId;
10034
10190
  let binArraysForSwap = /* @__PURE__ */ new Map();
10035
- let actualInAmount = new BN9(0);
10036
- let feeAmount = new BN9(0);
10037
- let protocolFeeAmount = new BN9(0);
10191
+ let actualInAmount = new BN10(0);
10192
+ let feeAmount = new BN10(0);
10193
+ let protocolFeeAmount = new BN10(0);
10038
10194
  while (!outAmountLeft.isZero()) {
10039
10195
  let binArrayAccountToSwap = findNextBinArrayWithLiquidity(
10040
10196
  swapForY,
@@ -10077,9 +10233,9 @@ var DLMM = class {
10077
10233
  }
10078
10234
  if (!outAmountLeft.isZero()) {
10079
10235
  if (swapForY) {
10080
- activeId = activeId.sub(new BN9(1));
10236
+ activeId = activeId.sub(new BN10(1));
10081
10237
  } else {
10082
- activeId = activeId.add(new BN9(1));
10238
+ activeId = activeId.add(new BN10(1));
10083
10239
  }
10084
10240
  }
10085
10241
  }
@@ -10091,8 +10247,8 @@ var DLMM = class {
10091
10247
  activeId.toNumber(),
10092
10248
  this.lbPair.binStep
10093
10249
  );
10094
- const priceImpact = startPrice.sub(endPrice).abs().div(startPrice).mul(new Decimal4(100));
10095
- const maxInAmount = actualInAmount.mul(new BN9(BASIS_POINT_MAX).add(allowedSlippage)).div(new BN9(BASIS_POINT_MAX));
10250
+ const priceImpact = startPrice.sub(endPrice).abs().div(startPrice).mul(new Decimal5(100));
10251
+ const maxInAmount = actualInAmount.mul(new BN10(BASIS_POINT_MAX).add(allowedSlippage)).div(new BN10(BASIS_POINT_MAX));
10096
10252
  return {
10097
10253
  inAmount: actualInAmount,
10098
10254
  maxInAmount,
@@ -10125,7 +10281,7 @@ var DLMM = class {
10125
10281
  const currentTimestamp = Date.now() / 1e3;
10126
10282
  let inAmountLeft = inAmount;
10127
10283
  let vParameterClone = Object.assign({}, this.lbPair.vParameters);
10128
- let activeId = new BN9(this.lbPair.activeId);
10284
+ let activeId = new BN10(this.lbPair.activeId);
10129
10285
  const binStep = this.lbPair.binStep;
10130
10286
  const sParameters2 = this.lbPair.parameters;
10131
10287
  this.updateReference(
@@ -10136,9 +10292,9 @@ var DLMM = class {
10136
10292
  );
10137
10293
  let startBin = null;
10138
10294
  let binArraysForSwap = /* @__PURE__ */ new Map();
10139
- let actualOutAmount = new BN9(0);
10140
- let feeAmount = new BN9(0);
10141
- let protocolFeeAmount = new BN9(0);
10295
+ let actualOutAmount = new BN10(0);
10296
+ let feeAmount = new BN10(0);
10297
+ let protocolFeeAmount = new BN10(0);
10142
10298
  while (!inAmountLeft.isZero()) {
10143
10299
  let binArrayAccountToSwap = findNextBinArrayWithLiquidity(
10144
10300
  swapForY,
@@ -10188,9 +10344,9 @@ var DLMM = class {
10188
10344
  }
10189
10345
  if (!inAmountLeft.isZero()) {
10190
10346
  if (swapForY) {
10191
- activeId = activeId.sub(new BN9(1));
10347
+ activeId = activeId.sub(new BN10(1));
10192
10348
  } else {
10193
- activeId = activeId.add(new BN9(1));
10349
+ activeId = activeId.add(new BN10(1));
10194
10350
  }
10195
10351
  }
10196
10352
  }
@@ -10208,8 +10364,8 @@ var DLMM = class {
10208
10364
  ),
10209
10365
  swapForY
10210
10366
  );
10211
- const priceImpact = new Decimal4(actualOutAmount.toString()).sub(new Decimal4(outAmountWithoutSlippage.toString())).div(new Decimal4(outAmountWithoutSlippage.toString())).mul(new Decimal4(100));
10212
- const minOutAmount = actualOutAmount.mul(new BN9(BASIS_POINT_MAX).sub(allowedSlippage)).div(new BN9(BASIS_POINT_MAX));
10367
+ const priceImpact = new Decimal5(actualOutAmount.toString()).sub(new Decimal5(outAmountWithoutSlippage.toString())).div(new Decimal5(outAmountWithoutSlippage.toString())).mul(new Decimal5(100));
10368
+ const minOutAmount = actualOutAmount.mul(new BN10(BASIS_POINT_MAX).sub(allowedSlippage)).div(new BN10(BASIS_POINT_MAX));
10213
10369
  const endPrice = getPriceOfBinByBinId(
10214
10370
  activeId.toNumber(),
10215
10371
  this.lbPair.binStep
@@ -10235,7 +10391,8 @@ var DLMM = class {
10235
10391
  binArraysPubkey
10236
10392
  }) {
10237
10393
  const { tokenXMint, tokenYMint, reserveX, reserveY, activeId, oracle } = await this.program.account.lbPair.fetch(lbPair);
10238
- const preInstructions = [computeBudgetIx()];
10394
+ const preInstructions = [];
10395
+ const postInstructions = [];
10239
10396
  const [
10240
10397
  { ataPubKey: userTokenIn, ix: createInTokenAccountIx },
10241
10398
  { ataPubKey: userTokenOut, ix: createOutTokenAccountIx }
@@ -10260,8 +10417,9 @@ var DLMM = class {
10260
10417
  BigInt(maxInAmount.toString())
10261
10418
  );
10262
10419
  preInstructions.push(...wrapSOLIx);
10420
+ const closeWrappedSOLIx = await unwrapSOLInstruction(user);
10421
+ closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
10263
10422
  }
10264
- const postInstructions = [];
10265
10423
  if (outToken.equals(NATIVE_MINT2)) {
10266
10424
  const closeWrappedSOLIx = await unwrapSOLInstruction(user);
10267
10425
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
@@ -10276,7 +10434,7 @@ var DLMM = class {
10276
10434
  pubkey
10277
10435
  };
10278
10436
  });
10279
- const swapTx = await this.program.methods.swapExactOut(maxInAmount, outAmount).accounts({
10437
+ const swapIx = await this.program.methods.swapExactOut(maxInAmount, outAmount).accounts({
10280
10438
  lbPair,
10281
10439
  reserveX,
10282
10440
  reserveY,
@@ -10290,13 +10448,20 @@ var DLMM = class {
10290
10448
  binArrayBitmapExtension: this.binArrayBitmapExtension ? this.binArrayBitmapExtension.publicKey : null,
10291
10449
  oracle,
10292
10450
  hostFeeIn: null
10293
- }).remainingAccounts(binArrays).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
10451
+ }).remainingAccounts(binArrays).instruction();
10452
+ const instructions = [...preInstructions, swapIx, ...postInstructions];
10453
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
10454
+ this.program.provider.connection,
10455
+ instructions,
10456
+ user
10457
+ );
10458
+ instructions.unshift(setCUIx);
10294
10459
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
10295
10460
  return new Transaction({
10296
10461
  blockhash,
10297
10462
  lastValidBlockHeight,
10298
10463
  feePayer: user
10299
- }).add(swapTx);
10464
+ }).add(...instructions);
10300
10465
  }
10301
10466
  /**
10302
10467
  * Returns a transaction to be signed and sent by user performing swap.
@@ -10320,7 +10485,8 @@ var DLMM = class {
10320
10485
  binArraysPubkey
10321
10486
  }) {
10322
10487
  const { tokenXMint, tokenYMint, reserveX, reserveY, activeId, oracle } = await this.program.account.lbPair.fetch(lbPair);
10323
- const preInstructions = [computeBudgetIx()];
10488
+ const preInstructions = [];
10489
+ const postInstructions = [];
10324
10490
  const [
10325
10491
  { ataPubKey: userTokenIn, ix: createInTokenAccountIx },
10326
10492
  { ataPubKey: userTokenOut, ix: createOutTokenAccountIx }
@@ -10345,8 +10511,9 @@ var DLMM = class {
10345
10511
  BigInt(inAmount.toString())
10346
10512
  );
10347
10513
  preInstructions.push(...wrapSOLIx);
10514
+ const closeWrappedSOLIx = await unwrapSOLInstruction(user);
10515
+ closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
10348
10516
  }
10349
- const postInstructions = [];
10350
10517
  if (outToken.equals(NATIVE_MINT2)) {
10351
10518
  const closeWrappedSOLIx = await unwrapSOLInstruction(user);
10352
10519
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
@@ -10361,7 +10528,7 @@ var DLMM = class {
10361
10528
  pubkey
10362
10529
  };
10363
10530
  });
10364
- const swapTx = await this.program.methods.swapWithPriceImpact(
10531
+ const swapIx = await this.program.methods.swapWithPriceImpact(
10365
10532
  inAmount,
10366
10533
  this.lbPair.activeId,
10367
10534
  priceImpact.toNumber()
@@ -10379,13 +10546,20 @@ var DLMM = class {
10379
10546
  binArrayBitmapExtension: this.binArrayBitmapExtension ? this.binArrayBitmapExtension.publicKey : null,
10380
10547
  oracle,
10381
10548
  hostFeeIn: null
10382
- }).remainingAccounts(binArrays).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
10549
+ }).remainingAccounts(binArrays).instruction();
10550
+ const instructions = [...preInstructions, swapIx, ...postInstructions];
10551
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
10552
+ this.program.provider.connection,
10553
+ instructions,
10554
+ user
10555
+ );
10556
+ instructions.unshift(setCUIx);
10383
10557
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
10384
10558
  return new Transaction({
10385
10559
  blockhash,
10386
10560
  lastValidBlockHeight,
10387
10561
  feePayer: user
10388
- }).add(swapTx);
10562
+ }).add(...instructions);
10389
10563
  }
10390
10564
  /**
10391
10565
  * Returns a transaction to be signed and sent by user performing swap.
@@ -10409,7 +10583,8 @@ var DLMM = class {
10409
10583
  binArraysPubkey
10410
10584
  }) {
10411
10585
  const { tokenXMint, tokenYMint, reserveX, reserveY, activeId, oracle } = await this.program.account.lbPair.fetch(lbPair);
10412
- const preInstructions = [computeBudgetIx()];
10586
+ const preInstructions = [];
10587
+ const postInstructions = [];
10413
10588
  const [
10414
10589
  { ataPubKey: userTokenIn, ix: createInTokenAccountIx },
10415
10590
  { ataPubKey: userTokenOut, ix: createOutTokenAccountIx }
@@ -10434,8 +10609,9 @@ var DLMM = class {
10434
10609
  BigInt(inAmount.toString())
10435
10610
  );
10436
10611
  preInstructions.push(...wrapSOLIx);
10612
+ const closeWrappedSOLIx = await unwrapSOLInstruction(user);
10613
+ closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
10437
10614
  }
10438
- const postInstructions = [];
10439
10615
  if (outToken.equals(NATIVE_MINT2)) {
10440
10616
  const closeWrappedSOLIx = await unwrapSOLInstruction(user);
10441
10617
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
@@ -10450,7 +10626,7 @@ var DLMM = class {
10450
10626
  pubkey
10451
10627
  };
10452
10628
  });
10453
- const swapTx = await this.program.methods.swap(inAmount, minOutAmount).accounts({
10629
+ const swapIx = await this.program.methods.swap(inAmount, minOutAmount).accounts({
10454
10630
  lbPair,
10455
10631
  reserveX,
10456
10632
  reserveY,
@@ -10466,13 +10642,20 @@ var DLMM = class {
10466
10642
  binArrayBitmapExtension: this.binArrayBitmapExtension ? this.binArrayBitmapExtension.publicKey : null,
10467
10643
  oracle,
10468
10644
  hostFeeIn: null
10469
- }).remainingAccounts(binArrays).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
10645
+ }).remainingAccounts(binArrays).instruction();
10646
+ const instructions = [...preInstructions, swapIx, ...postInstructions];
10647
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
10648
+ this.program.provider.connection,
10649
+ instructions,
10650
+ user
10651
+ );
10652
+ instructions.unshift(setCUIx);
10470
10653
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
10471
10654
  return new Transaction({
10472
10655
  blockhash,
10473
10656
  lastValidBlockHeight,
10474
10657
  feePayer: user
10475
- }).add(swapTx);
10658
+ }).add(...instructions);
10476
10659
  }
10477
10660
  /**
10478
10661
  * The claimLMReward function is used to claim rewards for a specific position owned by a specific owner.
@@ -10491,12 +10674,18 @@ var DLMM = class {
10491
10674
  });
10492
10675
  if (!claimTransactions.length)
10493
10676
  return;
10677
+ const instructions = claimTransactions.map((t) => t.instructions).flat();
10678
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
10679
+ this.program.provider.connection,
10680
+ instructions,
10681
+ owner
10682
+ );
10494
10683
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
10495
10684
  return new Transaction({
10496
10685
  blockhash,
10497
10686
  lastValidBlockHeight,
10498
10687
  feePayer: owner
10499
- }).add(...claimTransactions);
10688
+ }).add(setCUIx, ...claimTransactions);
10500
10689
  }
10501
10690
  /**
10502
10691
  * The `claimAllLMRewards` function is used to claim all liquidity mining rewards for a given owner
@@ -10522,6 +10711,14 @@ var DLMM = class {
10522
10711
  })
10523
10712
  )).flat();
10524
10713
  const chunkedClaimAllTx = chunks(claimAllTxs, MAX_CLAIM_ALL_ALLOWED);
10714
+ if (chunkedClaimAllTx.length === 0)
10715
+ return [];
10716
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
10717
+ this.program.provider.connection,
10718
+ // First tx simulation will success because it will create all the ATA. Then, we use the simulated CU as references for the rest
10719
+ chunkedClaimAllTx[0].map((t) => t.instructions).flat(),
10720
+ owner
10721
+ );
10525
10722
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
10526
10723
  return Promise.all(
10527
10724
  chunkedClaimAllTx.map(async (claimAllTx) => {
@@ -10529,7 +10726,7 @@ var DLMM = class {
10529
10726
  feePayer: owner,
10530
10727
  blockhash,
10531
10728
  lastValidBlockHeight
10532
- }).add(computeBudgetIx()).add(...claimAllTx);
10729
+ }).add(setCUIx).add(...claimAllTx);
10533
10730
  })
10534
10731
  );
10535
10732
  }
@@ -10588,14 +10785,22 @@ var DLMM = class {
10588
10785
  })
10589
10786
  )).flat();
10590
10787
  const chunkedClaimAllTx = chunks(claimAllTxs, MAX_CLAIM_ALL_ALLOWED);
10788
+ if (chunkedClaimAllTx.length === 0)
10789
+ return [];
10790
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
10791
+ this.program.provider.connection,
10792
+ // First tx simulation will success because it will create all the ATA. Then, we use the simulated CU as references for the rest
10793
+ chunkedClaimAllTx[0].map((t) => t.instructions).flat(),
10794
+ owner
10795
+ );
10796
+ const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
10591
10797
  return Promise.all(
10592
10798
  chunkedClaimAllTx.map(async (claimAllTx) => {
10593
- const { recentBlockhash, lastValidBlockHeight } = claimAllTx[0];
10594
10799
  return new Transaction({
10595
10800
  feePayer: owner,
10596
- blockhash: recentBlockhash,
10801
+ blockhash,
10597
10802
  lastValidBlockHeight
10598
- }).add(computeBudgetIx()).add(...claimAllTx);
10803
+ }).add(setCUIx).add(...claimAllTx);
10599
10804
  })
10600
10805
  );
10601
10806
  }
@@ -10662,11 +10867,22 @@ var DLMM = class {
10662
10867
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
10663
10868
  return Promise.all(
10664
10869
  claimAllTxs.map(async (claimAllTx) => {
10870
+ const mainInstructions = claimAllTx.map((t) => t.instructions).flat();
10871
+ const instructions = [
10872
+ ...preInstructions,
10873
+ ...mainInstructions,
10874
+ ...postInstructions
10875
+ ];
10876
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
10877
+ this.program.provider.connection,
10878
+ instructions,
10879
+ owner
10880
+ );
10665
10881
  const tx = new Transaction({
10666
10882
  feePayer: owner,
10667
10883
  blockhash,
10668
10884
  lastValidBlockHeight
10669
- }).add(computeBudgetIx());
10885
+ }).add(setCUIx);
10670
10886
  if (preInstructions.length)
10671
10887
  tx.add(...preInstructions);
10672
10888
  tx.add(...claimAllTx);
@@ -10687,15 +10903,15 @@ var DLMM = class {
10687
10903
  * @returns {Promise<SeedLiquidityResponse>}
10688
10904
  */
10689
10905
  async seedLiquidity(owner, seedAmount, curvature, minPrice, maxPrice, base) {
10690
- const toLamportMultiplier = new Decimal4(
10906
+ const toLamportMultiplier = new Decimal5(
10691
10907
  10 ** (this.tokenY.decimal - this.tokenX.decimal)
10692
10908
  );
10693
- const minPricePerLamport = new Decimal4(minPrice).mul(toLamportMultiplier);
10694
- const maxPricePerLamport = new Decimal4(maxPrice).mul(toLamportMultiplier);
10695
- const minBinId = new BN9(
10909
+ const minPricePerLamport = new Decimal5(minPrice).mul(toLamportMultiplier);
10910
+ const maxPricePerLamport = new Decimal5(maxPrice).mul(toLamportMultiplier);
10911
+ const minBinId = new BN10(
10696
10912
  DLMM.getBinIdFromPrice(minPricePerLamport, this.lbPair.binStep, false)
10697
10913
  );
10698
- const maxBinId = new BN9(
10914
+ const maxBinId = new BN10(
10699
10915
  DLMM.getBinIdFromPrice(maxPricePerLamport, this.lbPair.binStep, true)
10700
10916
  );
10701
10917
  if (minBinId.toNumber() < this.lbPair.activeId) {
@@ -10714,7 +10930,7 @@ var DLMM = class {
10714
10930
  maxBinId,
10715
10931
  k
10716
10932
  );
10717
- const decompressMultiplier = new BN9(10 ** this.tokenX.decimal);
10933
+ const decompressMultiplier = new BN10(10 ** this.tokenX.decimal);
10718
10934
  let { compressedBinAmount, compressionLoss } = compressBinAmount(
10719
10935
  binDepositAmount,
10720
10936
  decompressMultiplier
@@ -10726,10 +10942,10 @@ var DLMM = class {
10726
10942
  compressedBinAmount,
10727
10943
  compressionLoss,
10728
10944
  decompressMultiplier,
10729
- new BN9(2 ** 32 - 1)
10945
+ new BN10(2 ** 32 - 1)
10730
10946
  // u32
10731
10947
  );
10732
- const positionCount = getPositionCount(minBinId, maxBinId.sub(new BN9(1)));
10948
+ const positionCount = getPositionCount(minBinId, maxBinId.sub(new BN10(1)));
10733
10949
  const seederTokenX = getAssociatedTokenAddressSync2(
10734
10950
  this.lbPair.tokenXMint,
10735
10951
  owner,
@@ -10739,8 +10955,8 @@ var DLMM = class {
10739
10955
  const addLiquidityIxs = [];
10740
10956
  const appendedInitBinArrayIx = /* @__PURE__ */ new Set();
10741
10957
  for (let i = 0; i < positionCount.toNumber(); i++) {
10742
- const lowerBinId = minBinId.add(MAX_BIN_PER_POSITION.mul(new BN9(i)));
10743
- const upperBinId = lowerBinId.add(MAX_BIN_PER_POSITION).sub(new BN9(1));
10958
+ const lowerBinId = minBinId.add(MAX_BIN_PER_POSITION.mul(new BN10(i)));
10959
+ const upperBinId = lowerBinId.add(MAX_BIN_PER_POSITION).sub(new BN10(1));
10744
10960
  const lowerBinArrayIndex = binIdToBinArrayIndex(lowerBinId);
10745
10961
  const upperBinArrayIndex = binIdToBinArrayIndex(upperBinId);
10746
10962
  const [positionPda, _bump] = derivePosition(
@@ -10765,7 +10981,7 @@ var DLMM = class {
10765
10981
  upperBinArray,
10766
10982
  positionPda
10767
10983
  ]);
10768
- let instructions = [computeBudgetIx()];
10984
+ let instructions = [];
10769
10985
  const lowerBinArrayAccount = accounts[0];
10770
10986
  if (!lowerBinArrayAccount && !appendedInitBinArrayIx.has(lowerBinArray.toBase58())) {
10771
10987
  instructions.push(
@@ -10804,10 +11020,17 @@ var DLMM = class {
10804
11020
  );
10805
11021
  }
10806
11022
  if (instructions.length > 1) {
11023
+ instructions.push(
11024
+ await getEstimatedComputeUnitIxWithBuffer(
11025
+ this.program.provider.connection,
11026
+ instructions,
11027
+ owner
11028
+ )
11029
+ );
10807
11030
  initializeBinArraysAndPositionIxs.push(instructions);
10808
- instructions = [computeBudgetIx()];
11031
+ instructions = [];
10809
11032
  }
10810
- const positionDeposited = positionAccount && this.program.coder.accounts.decode("positionV2", positionAccount.data).liquidityShares.reduce((total, cur) => total.add(cur), new BN9(0)).gt(new BN9(0));
11033
+ const positionDeposited = positionAccount && this.program.coder.accounts.decode("positionV2", positionAccount.data).liquidityShares.reduce((total, cur) => total.add(cur), new BN10(0)).gt(new BN10(0));
10811
11034
  if (!positionDeposited) {
10812
11035
  const cappedUpperBinId = Math.min(
10813
11036
  upperBinId.toNumber(),
@@ -10861,7 +11084,12 @@ var DLMM = class {
10861
11084
  }).instruction()
10862
11085
  );
10863
11086
  }
10864
- addLiquidityIxs.push(instructions);
11087
+ addLiquidityIxs.push([
11088
+ ComputeBudgetProgram2.setComputeUnitLimit({
11089
+ units: DEFAULT_ADD_LIQUIDITY_CU
11090
+ }),
11091
+ ...instructions
11092
+ ]);
10865
11093
  }
10866
11094
  }
10867
11095
  return {
@@ -10872,37 +11100,37 @@ var DLMM = class {
10872
11100
  /**
10873
11101
  * The `seedLiquidity` function create multiple grouped instructions. The grouped instructions will be either [initialize bin array + initialize position instructions] or [deposit instruction] combination.
10874
11102
  * @param
10875
- * - `owner`: The public key of the positions owner.
11103
+ * - `payer`: The public key of the tx payer.
10876
11104
  * - `base`: Base key
10877
11105
  * - `seedAmount`: Token X lamport amount to be seeded to the pool.
10878
11106
  * - `price`: TokenX/TokenY Price in UI format
10879
11107
  * - `roundingUp`: Whether to round up the price
11108
+ * - `positionOwner`: The owner of the position
10880
11109
  * - `feeOwner`: Position fee owner
10881
11110
  * - `operator`: Operator of the position. Operator able to manage the position on behalf of the position owner. However, liquidity withdrawal issue by the operator can only send to the position owner.
10882
11111
  * - `lockReleasePoint`: The lock release point of the position.
11112
+ * - `shouldSeedPositionOwner` (optional): Whether to send 1 lamport amount of token X to the position owner to prove ownership.
10883
11113
  *
10884
11114
  * The returned instructions need to be executed sequentially if it was separated into multiple transactions.
10885
11115
  * @returns {Promise<TransactionInstruction[]>}
10886
11116
  */
10887
- async seedLiquiditySingleBin(owner, base, seedAmount, price, roundingUp, feeOwner, operator, lockReleasePoint) {
10888
- const pricePerLamport = DLMM.getPricePerLamport(this.tokenX.decimal, this.tokenY.decimal, price);
10889
- const binIdNumber = DLMM.getBinIdFromPrice(pricePerLamport, this.lbPair.binStep, !roundingUp);
10890
- const binId = new BN9(binIdNumber);
11117
+ async seedLiquiditySingleBin(payer, base, seedAmount, price, roundingUp, positionOwner, feeOwner, operator, lockReleasePoint, shouldSeedPositionOwner = false) {
11118
+ const pricePerLamport = DLMM.getPricePerLamport(
11119
+ this.tokenX.decimal,
11120
+ this.tokenY.decimal,
11121
+ price
11122
+ );
11123
+ const binIdNumber = DLMM.getBinIdFromPrice(
11124
+ pricePerLamport,
11125
+ this.lbPair.binStep,
11126
+ !roundingUp
11127
+ );
11128
+ const binId = new BN10(binIdNumber);
10891
11129
  const lowerBinArrayIndex = binIdToBinArrayIndex(binId);
10892
- const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
11130
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN10(1));
10893
11131
  const [lowerBinArray] = deriveBinArray(this.pubkey, lowerBinArrayIndex, this.program.programId);
10894
11132
  const [upperBinArray] = deriveBinArray(this.pubkey, upperBinArrayIndex, this.program.programId);
10895
- const [positionPda] = derivePosition(this.pubkey, base, binId, new BN9(1), this.program.programId);
10896
- const operatorTokenX = getAssociatedTokenAddressSync2(
10897
- this.lbPair.tokenXMint,
10898
- operator,
10899
- true
10900
- );
10901
- const ownerTokenX = getAssociatedTokenAddressSync2(
10902
- this.lbPair.tokenXMint,
10903
- owner,
10904
- true
10905
- );
11133
+ const [positionPda] = derivePosition(this.pubkey, base, binId, new BN10(1), this.program.programId);
10906
11134
  const preInstructions = [];
10907
11135
  const [
10908
11136
  { ataPubKey: userTokenX, ix: createPayerTokenXIx },
@@ -10911,17 +11139,22 @@ var DLMM = class {
10911
11139
  getOrCreateATAInstruction(
10912
11140
  this.program.provider.connection,
10913
11141
  this.tokenX.publicKey,
10914
- owner
11142
+ operator,
11143
+ payer
10915
11144
  ),
10916
11145
  getOrCreateATAInstruction(
10917
11146
  this.program.provider.connection,
10918
11147
  this.tokenY.publicKey,
10919
- owner
11148
+ operator,
11149
+ payer
10920
11150
  )
10921
11151
  ]);
10922
11152
  createPayerTokenXIx && preInstructions.push(createPayerTokenXIx);
10923
11153
  createPayerTokenYIx && preInstructions.push(createPayerTokenYIx);
10924
- let [binArrayBitmapExtension] = deriveBinArrayBitmapExtension(this.pubkey, this.program.programId);
11154
+ let [binArrayBitmapExtension] = deriveBinArrayBitmapExtension(
11155
+ this.pubkey,
11156
+ this.program.programId
11157
+ );
10925
11158
  const accounts = await this.program.provider.connection.getMultipleAccountsInfo([
10926
11159
  lowerBinArray,
10927
11160
  upperBinArray,
@@ -10933,13 +11166,38 @@ var DLMM = class {
10933
11166
  if (!bitmapExtensionAccount) {
10934
11167
  preInstructions.push(await this.program.methods.initializeBinArrayBitmapExtension().accounts({
10935
11168
  binArrayBitmapExtension,
10936
- funder: owner,
11169
+ funder: payer,
10937
11170
  lbPair: this.pubkey
10938
11171
  }).instruction());
10939
11172
  }
10940
11173
  } else {
10941
11174
  binArrayBitmapExtension = this.program.programId;
10942
11175
  }
11176
+ const operatorTokenX = getAssociatedTokenAddressSync2(
11177
+ this.lbPair.tokenXMint,
11178
+ operator,
11179
+ true
11180
+ );
11181
+ const positionOwnerTokenX = getAssociatedTokenAddressSync2(
11182
+ this.lbPair.tokenXMint,
11183
+ positionOwner,
11184
+ true
11185
+ );
11186
+ if (shouldSeedPositionOwner) {
11187
+ const positionOwnerTokenXAccount = await this.program.provider.connection.getAccountInfo(positionOwnerTokenX);
11188
+ if (positionOwnerTokenXAccount) {
11189
+ const account = AccountLayout.decode(positionOwnerTokenXAccount.data);
11190
+ if (account.amount == BigInt(0)) {
11191
+ const transferIx = createTransferInstruction(operatorTokenX, positionOwnerTokenX, payer, 1);
11192
+ preInstructions.push(transferIx);
11193
+ }
11194
+ } else {
11195
+ const createPositionOwnerTokenXIx = createAssociatedTokenAccountInstruction2(payer, positionOwnerTokenX, positionOwner, this.lbPair.tokenXMint);
11196
+ preInstructions.push(createPositionOwnerTokenXIx);
11197
+ const transferIx = createTransferInstruction(operatorTokenX, positionOwnerTokenX, payer, 1);
11198
+ preInstructions.push(transferIx);
11199
+ }
11200
+ }
10943
11201
  const lowerBinArrayAccount = accounts[0];
10944
11202
  const upperBinArrayAccount = accounts[1];
10945
11203
  const positionAccount = accounts[2];
@@ -10947,7 +11205,7 @@ var DLMM = class {
10947
11205
  preInstructions.push(
10948
11206
  await this.program.methods.initializeBinArray(lowerBinArrayIndex).accounts({
10949
11207
  binArray: lowerBinArray,
10950
- funder: owner,
11208
+ funder: payer,
10951
11209
  lbPair: this.pubkey
10952
11210
  }).instruction()
10953
11211
  );
@@ -10956,22 +11214,27 @@ var DLMM = class {
10956
11214
  preInstructions.push(
10957
11215
  await this.program.methods.initializeBinArray(upperBinArrayIndex).accounts({
10958
11216
  binArray: upperBinArray,
10959
- funder: owner,
11217
+ funder: payer,
10960
11218
  lbPair: this.pubkey
10961
11219
  }).instruction()
10962
11220
  );
10963
11221
  }
10964
11222
  if (!positionAccount) {
10965
11223
  preInstructions.push(
10966
- await this.program.methods.initializePositionByOperator(binId.toNumber(), 1, feeOwner, lockReleasePoint).accounts({
10967
- payer: owner,
11224
+ await this.program.methods.initializePositionByOperator(
11225
+ binId.toNumber(),
11226
+ 1,
11227
+ feeOwner,
11228
+ lockReleasePoint
11229
+ ).accounts({
11230
+ payer,
10968
11231
  base,
10969
11232
  position: positionPda,
10970
11233
  lbPair: this.pubkey,
10971
- owner,
11234
+ owner: positionOwner,
10972
11235
  operator,
10973
11236
  operatorTokenX,
10974
- ownerTokenX
11237
+ ownerTokenX: positionOwnerTokenX
10975
11238
  }).instruction()
10976
11239
  );
10977
11240
  }
@@ -10982,7 +11245,7 @@ var DLMM = class {
10982
11245
  };
10983
11246
  const addLiquidityParams = {
10984
11247
  amountX: seedAmount,
10985
- amountY: new BN9(0),
11248
+ amountY: new BN10(0),
10986
11249
  binLiquidityDist: [binLiquidityDist]
10987
11250
  };
10988
11251
  const depositLiquidityIx = await this.program.methods.addLiquidity(addLiquidityParams).accounts({
@@ -10997,7 +11260,7 @@ var DLMM = class {
10997
11260
  tokenYMint: this.lbPair.tokenYMint,
10998
11261
  binArrayLower: lowerBinArray,
10999
11262
  binArrayUpper: upperBinArray,
11000
- sender: owner,
11263
+ sender: operator,
11001
11264
  tokenXProgram: TOKEN_PROGRAM_ID2,
11002
11265
  tokenYProgram: TOKEN_PROGRAM_ID2
11003
11266
  }).instruction();
@@ -11071,7 +11334,7 @@ var DLMM = class {
11071
11334
  owner,
11072
11335
  true
11073
11336
  );
11074
- let initializePositionByOperatorTx = await this.program.methods.initializePositionByOperator(
11337
+ const initializePositionByOperatorTx = await this.program.methods.initializePositionByOperator(
11075
11338
  lowerBinId.toNumber(),
11076
11339
  MAX_BIN_PER_POSITION.toNumber(),
11077
11340
  feeOwner,
@@ -11179,11 +11442,22 @@ var DLMM = class {
11179
11442
  const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
11180
11443
  return Promise.all(
11181
11444
  chunkedClaimAllTx.map(async (claimAllTx) => {
11445
+ const mainIxs = claimAllTx.map((t) => t.instructions).flat();
11446
+ const instructions = [
11447
+ ...preInstructions,
11448
+ ...mainIxs,
11449
+ ...postInstructions
11450
+ ];
11451
+ const setCUIx = await getEstimatedComputeUnitIxWithBuffer(
11452
+ this.program.provider.connection,
11453
+ instructions,
11454
+ owner
11455
+ );
11182
11456
  const tx = new Transaction({
11183
11457
  feePayer: owner,
11184
11458
  blockhash,
11185
11459
  lastValidBlockHeight
11186
- }).add(computeBudgetIx());
11460
+ }).add(setCUIx);
11187
11461
  if (preInstructions.length)
11188
11462
  tx.add(...preInstructions);
11189
11463
  tx.add(...claimAllTx);
@@ -11205,12 +11479,12 @@ var DLMM = class {
11205
11479
  false
11206
11480
  );
11207
11481
  const marketPriceBinArrayIndex = binIdToBinArrayIndex(
11208
- new BN9(marketPriceBinId)
11482
+ new BN10(marketPriceBinId)
11209
11483
  );
11210
11484
  const swapForY = marketPriceBinId < activeBinId;
11211
11485
  const toBinArrayIndex = findNextBinArrayIndexWithLiquidity(
11212
11486
  swapForY,
11213
- new BN9(activeBinId),
11487
+ new BN10(activeBinId),
11214
11488
  this.lbPair,
11215
11489
  this.binArrayBitmapExtension?.account ?? null
11216
11490
  );
@@ -11243,11 +11517,11 @@ var DLMM = class {
11243
11517
  "Unable to sync with market price due to bin with liquidity between current and market price bin"
11244
11518
  );
11245
11519
  }
11246
- const fromBinArrayIndex = binIdToBinArrayIndex(new BN9(activeBinId));
11520
+ const fromBinArrayIndex = binIdToBinArrayIndex(new BN10(activeBinId));
11247
11521
  const swapForY = marketPriceBinId < activeBinId;
11248
11522
  const toBinArrayIndex = findNextBinArrayIndexWithLiquidity(
11249
11523
  swapForY,
11250
- new BN9(activeBinId),
11524
+ new BN10(activeBinId),
11251
11525
  this.lbPair,
11252
11526
  this.binArrayBitmapExtension?.account ?? null
11253
11527
  );
@@ -11344,7 +11618,7 @@ var DLMM = class {
11344
11618
  const amount1 = isWithdrawForY ? amountY : amountX;
11345
11619
  const remainAmountX = bin.amountX.sub(amountX);
11346
11620
  const remainAmountY = bin.amountY.sub(amountY);
11347
- if (amount0.eq(new BN9(0))) {
11621
+ if (amount0.eq(new BN10(0))) {
11348
11622
  return {
11349
11623
  withdrawAmount: amount1
11350
11624
  };
@@ -11376,19 +11650,19 @@ var DLMM = class {
11376
11650
  };
11377
11651
  }
11378
11652
  async getWithdrawSingleSideAmount(positionPubkey, isWithdrawForY) {
11379
- let totalWithdrawAmount = new BN9(0);
11653
+ let totalWithdrawAmount = new BN10(0);
11380
11654
  let lowerBinArray;
11381
11655
  let upperBinArray;
11382
11656
  const position = await this.program.account.positionV2.fetch(
11383
11657
  positionPubkey
11384
11658
  );
11385
- const lowerBinArrayIdx = binIdToBinArrayIndex(new BN9(position.lowerBinId));
11659
+ const lowerBinArrayIdx = binIdToBinArrayIndex(new BN10(position.lowerBinId));
11386
11660
  const [lowerBinArrayPubKey] = deriveBinArray(
11387
11661
  position.lbPair,
11388
11662
  lowerBinArrayIdx,
11389
11663
  this.program.programId
11390
11664
  );
11391
- const upperBinArrayIdx = lowerBinArrayIdx.add(new BN9(1));
11665
+ const upperBinArrayIdx = lowerBinArrayIdx.add(new BN10(1));
11392
11666
  const [upperBinArrayPubKey] = deriveBinArray(
11393
11667
  position.lbPair,
11394
11668
  upperBinArrayIdx,
@@ -11400,10 +11674,10 @@ var DLMM = class {
11400
11674
  ]);
11401
11675
  for (let idx = 0; idx < position.liquidityShares.length; idx++) {
11402
11676
  const shareToRemove = position.liquidityShares[idx];
11403
- if (shareToRemove.eq(new BN9(0))) {
11677
+ if (shareToRemove.eq(new BN10(0))) {
11404
11678
  continue;
11405
11679
  }
11406
- const binId = new BN9(position.lowerBinId).add(new BN9(idx));
11680
+ const binId = new BN10(position.lowerBinId).add(new BN10(idx));
11407
11681
  const binArrayIndex = binIdToBinArrayIndex(binId);
11408
11682
  const binArray = binArrayIndex.eq(lowerBinArrayIdx) ? lowerBinArray : upperBinArray;
11409
11683
  if (!binArray) {
@@ -11411,15 +11685,15 @@ var DLMM = class {
11411
11685
  }
11412
11686
  const bin = getBinFromBinArray(binId.toNumber(), binArray);
11413
11687
  if (isWithdrawForY) {
11414
- if (binId.gt(new BN9(this.lbPair.activeId))) {
11688
+ if (binId.gt(new BN10(this.lbPair.activeId))) {
11415
11689
  break;
11416
11690
  }
11417
11691
  } else {
11418
- if (binId.lt(new BN9(this.lbPair.activeId))) {
11692
+ if (binId.lt(new BN10(this.lbPair.activeId))) {
11419
11693
  continue;
11420
11694
  }
11421
11695
  }
11422
- const price = getQPriceFromId(binId, new BN9(this.lbPair.binStep));
11696
+ const price = getQPriceFromId(binId, new BN10(this.lbPair.binStep));
11423
11697
  const { withdrawAmount } = this.getAmountOutWithdrawSingleSide(
11424
11698
  shareToRemove,
11425
11699
  price,
@@ -11463,20 +11737,20 @@ var DLMM = class {
11463
11737
  ]);
11464
11738
  }
11465
11739
  static async getClaimableLMReward(program, positionVersion, lbPair, onChainTimestamp, position, lowerBinArray, upperBinArray) {
11466
- const lowerBinArrayIdx = binIdToBinArrayIndex(new BN9(position.lowerBinId));
11467
- let rewards = [new BN9(0), new BN9(0)];
11740
+ const lowerBinArrayIdx = binIdToBinArrayIndex(new BN10(position.lowerBinId));
11741
+ let rewards = [new BN10(0), new BN10(0)];
11468
11742
  let _lowerBinArray = lowerBinArray;
11469
11743
  let _upperBinArray = upperBinArray;
11470
11744
  if (!lowerBinArray || !upperBinArray) {
11471
11745
  const lowerBinArrayIdx2 = binIdToBinArrayIndex(
11472
- new BN9(position.lowerBinId)
11746
+ new BN10(position.lowerBinId)
11473
11747
  );
11474
11748
  const [lowerBinArray2] = deriveBinArray(
11475
11749
  position.lbPair,
11476
11750
  lowerBinArrayIdx2,
11477
11751
  program.programId
11478
11752
  );
11479
- const upperBinArrayIdx = lowerBinArrayIdx2.add(new BN9(1));
11753
+ const upperBinArrayIdx = lowerBinArrayIdx2.add(new BN10(1));
11480
11754
  const [upperBinArray2] = deriveBinArray(
11481
11755
  position.lbPair,
11482
11756
  upperBinArrayIdx,
@@ -11490,7 +11764,7 @@ var DLMM = class {
11490
11764
  if (!_lowerBinArray || !_upperBinArray)
11491
11765
  throw new Error("BinArray not found");
11492
11766
  for (let i = position.lowerBinId; i <= position.upperBinId; i++) {
11493
- const binArrayIdx = binIdToBinArrayIndex(new BN9(i));
11767
+ const binArrayIdx = binIdToBinArrayIndex(new BN10(i));
11494
11768
  const binArray = binArrayIdx.eq(lowerBinArrayIdx) ? _lowerBinArray : _upperBinArray;
11495
11769
  const binState = getBinFromBinArray(i, binArray);
11496
11770
  const binIdxInPosition = i - position.lowerBinId;
@@ -11501,7 +11775,7 @@ var DLMM = class {
11501
11775
  if (!pairRewardInfo.mint.equals(PublicKey6.default)) {
11502
11776
  let rewardPerTokenStored = binState.rewardPerTokenStored[j];
11503
11777
  if (i == lbPair.activeId && !binState.liquiditySupply.isZero()) {
11504
- const currentTime = new BN9(
11778
+ const currentTime = new BN10(
11505
11779
  Math.min(
11506
11780
  onChainTimestamp,
11507
11781
  pairRewardInfo.rewardDurationEnd.toNumber()
@@ -11509,7 +11783,7 @@ var DLMM = class {
11509
11783
  );
11510
11784
  const delta2 = currentTime.sub(pairRewardInfo.lastUpdateTime);
11511
11785
  const liquiditySupply = binArray.version == 0 ? binState.liquiditySupply : binState.liquiditySupply.shrn(64);
11512
- const rewardPerTokenStoredDelta = pairRewardInfo.rewardRate.mul(delta2).div(new BN9(15)).div(liquiditySupply);
11786
+ const rewardPerTokenStoredDelta = pairRewardInfo.rewardRate.mul(delta2).div(new BN10(15)).div(liquiditySupply);
11513
11787
  rewardPerTokenStored = rewardPerTokenStored.add(
11514
11788
  rewardPerTokenStoredDelta
11515
11789
  );
@@ -11533,21 +11807,21 @@ var DLMM = class {
11533
11807
  };
11534
11808
  }
11535
11809
  static async getClaimableSwapFee(program, positionVersion, position, lowerBinArray, upperBinArray) {
11536
- const lowerBinArrayIdx = binIdToBinArrayIndex(new BN9(position.lowerBinId));
11537
- let feeX = new BN9(0);
11538
- let feeY = new BN9(0);
11810
+ const lowerBinArrayIdx = binIdToBinArrayIndex(new BN10(position.lowerBinId));
11811
+ let feeX = new BN10(0);
11812
+ let feeY = new BN10(0);
11539
11813
  let _lowerBinArray = lowerBinArray;
11540
11814
  let _upperBinArray = upperBinArray;
11541
11815
  if (!lowerBinArray || !upperBinArray) {
11542
11816
  const lowerBinArrayIdx2 = binIdToBinArrayIndex(
11543
- new BN9(position.lowerBinId)
11817
+ new BN10(position.lowerBinId)
11544
11818
  );
11545
11819
  const [lowerBinArray2] = deriveBinArray(
11546
11820
  position.lbPair,
11547
11821
  lowerBinArrayIdx2,
11548
11822
  program.programId
11549
11823
  );
11550
- const upperBinArrayIdx = lowerBinArrayIdx2.add(new BN9(1));
11824
+ const upperBinArrayIdx = lowerBinArrayIdx2.add(new BN10(1));
11551
11825
  const [upperBinArray2] = deriveBinArray(
11552
11826
  position.lbPair,
11553
11827
  upperBinArrayIdx,
@@ -11561,7 +11835,7 @@ var DLMM = class {
11561
11835
  if (!_lowerBinArray || !_upperBinArray)
11562
11836
  throw new Error("BinArray not found");
11563
11837
  for (let i = position.lowerBinId; i <= position.upperBinId; i++) {
11564
- const binArrayIdx = binIdToBinArrayIndex(new BN9(i));
11838
+ const binArrayIdx = binIdToBinArrayIndex(new BN10(i));
11565
11839
  const binArray = binArrayIdx.eq(lowerBinArrayIdx) ? _lowerBinArray : _upperBinArray;
11566
11840
  const binState = getBinFromBinArray(i, binArray);
11567
11841
  const binIdxInPosition = i - position.lowerBinId;
@@ -11607,18 +11881,18 @@ var DLMM = class {
11607
11881
  if (bins[0].binId !== lowerBinId || bins[bins.length - 1].binId !== upperBinId)
11608
11882
  throw new Error("Bin ID mismatch");
11609
11883
  const positionData = [];
11610
- let totalXAmount = new Decimal4(0);
11611
- let totalYAmount = new Decimal4(0);
11884
+ let totalXAmount = new Decimal5(0);
11885
+ let totalYAmount = new Decimal5(0);
11612
11886
  bins.forEach((bin, idx) => {
11613
- const binSupply = new Decimal4(bin.supply.toString());
11887
+ const binSupply = new Decimal5(bin.supply.toString());
11614
11888
  let posShare;
11615
11889
  if (bin.version === 1 && version === 0 /* V1 */) {
11616
- posShare = new Decimal4(posShares[idx].shln(64).toString());
11890
+ posShare = new Decimal5(posShares[idx].shln(64).toString());
11617
11891
  } else {
11618
- posShare = new Decimal4(posShares[idx].toString());
11892
+ posShare = new Decimal5(posShares[idx].toString());
11619
11893
  }
11620
- const positionXAmount = binSupply.eq(new Decimal4("0")) ? new Decimal4("0") : posShare.mul(bin.xAmount.toString()).div(binSupply);
11621
- const positionYAmount = binSupply.eq(new Decimal4("0")) ? new Decimal4("0") : posShare.mul(bin.yAmount.toString()).div(binSupply);
11894
+ const positionXAmount = binSupply.eq(new Decimal5("0")) ? new Decimal5("0") : posShare.mul(bin.xAmount.toString()).div(binSupply);
11895
+ const positionYAmount = binSupply.eq(new Decimal5("0")) ? new Decimal5("0") : posShare.mul(bin.yAmount.toString()).div(binSupply);
11622
11896
  totalXAmount = totalXAmount.add(positionXAmount);
11623
11897
  totalYAmount = totalYAmount.add(positionYAmount);
11624
11898
  positionData.push({
@@ -11666,8 +11940,8 @@ var DLMM = class {
11666
11940
  };
11667
11941
  }
11668
11942
  static getBinsBetweenLowerAndUpperBound(lbPair, lowerBinId, upperBinId, baseTokenDecimal, quoteTokenDecimal, lowerBinArrays, upperBinArrays) {
11669
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
11670
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
11943
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
11944
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
11671
11945
  let bins = [];
11672
11946
  if (lowerBinArrayIndex.eq(upperBinArrayIndex)) {
11673
11947
  const binArray = lowerBinArrays;
@@ -11688,7 +11962,7 @@ var DLMM = class {
11688
11962
  supply: bin.liquiditySupply,
11689
11963
  price: pricePerLamport,
11690
11964
  version: binArray.version,
11691
- pricePerToken: new Decimal4(pricePerLamport).mul(new Decimal4(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString()
11965
+ pricePerToken: new Decimal5(pricePerLamport).mul(new Decimal5(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString()
11692
11966
  });
11693
11967
  }
11694
11968
  });
@@ -11712,7 +11986,7 @@ var DLMM = class {
11712
11986
  supply: bin.liquiditySupply,
11713
11987
  price: pricePerLamport,
11714
11988
  version: binArray.version,
11715
- pricePerToken: new Decimal4(pricePerLamport).mul(new Decimal4(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString()
11989
+ pricePerToken: new Decimal5(pricePerLamport).mul(new Decimal5(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString()
11716
11990
  });
11717
11991
  }
11718
11992
  });
@@ -11744,112 +12018,45 @@ var DLMM = class {
11744
12018
  binIds
11745
12019
  };
11746
12020
  }
11747
- async getBins(lbPairPubKey, lowerBinId, upperBinId, baseTokenDecimal, quoteTokenDecimal, lowerBinArrays, upperBinArrays) {
11748
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
11749
- const upperBinArrayIndex = binIdToBinArrayIndex(new BN9(upperBinId));
11750
- let bins = [];
11751
- if (lowerBinArrayIndex.eq(upperBinArrayIndex)) {
11752
- const [binArrayPubKey] = deriveBinArray(
11753
- lbPairPubKey,
11754
- lowerBinArrayIndex,
11755
- this.program.programId
11756
- );
11757
- const binArray = lowerBinArrays ?? await this.program.account.binArray.fetch(binArrayPubKey).catch(() => {
11758
- const [lowerBinId2, upperBinId2] = getBinArrayLowerUpperBinId(lowerBinArrayIndex);
11759
- const binArrayBins = [];
11760
- for (let i = lowerBinId2.toNumber(); i <= upperBinId2.toNumber(); i++) {
11761
- binArrayBins.push({
11762
- amountX: new BN9(0),
11763
- amountY: new BN9(0),
11764
- liquiditySupply: new BN9(0),
11765
- rewardPerTokenStored: [new BN9(0), new BN9(0)],
11766
- amountXIn: new BN9(0),
11767
- amountYIn: new BN9(0),
11768
- feeAmountXPerTokenStored: new BN9(0),
11769
- feeAmountYPerTokenStored: new BN9(0),
11770
- price: new BN9(0)
11771
- });
11772
- }
11773
- return {
11774
- bins: binArrayBins,
11775
- index: lowerBinArrayIndex,
11776
- version: 1
11777
- };
11778
- });
11779
- const [lowerBinIdForBinArray] = getBinArrayLowerUpperBinId(
11780
- binArray.index
11781
- );
11782
- binArray.bins.forEach((bin, idx) => {
11783
- const binId = lowerBinIdForBinArray.toNumber() + idx;
11784
- if (binId >= lowerBinId && binId <= upperBinId) {
11785
- const pricePerLamport = getPriceOfBinByBinId(
11786
- binId,
11787
- this.lbPair.binStep
11788
- ).toString();
11789
- bins.push({
11790
- binId,
11791
- xAmount: bin.amountX,
11792
- yAmount: bin.amountY,
11793
- supply: bin.liquiditySupply,
11794
- price: pricePerLamport,
11795
- version: binArray.version,
11796
- pricePerToken: new Decimal4(pricePerLamport).mul(new Decimal4(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString()
11797
- });
11798
- }
11799
- });
11800
- } else {
11801
- const [lowerBinArrayPubKey] = deriveBinArray(
11802
- lbPairPubKey,
11803
- lowerBinArrayIndex,
11804
- this.program.programId
11805
- );
11806
- const [upperBinArrayPubKey] = deriveBinArray(
11807
- lbPairPubKey,
11808
- upperBinArrayIndex,
11809
- this.program.programId
11810
- );
11811
- const binArrays = await (async () => {
11812
- if (!lowerBinArrays || !upperBinArrays) {
11813
- return (await this.program.account.binArray.fetchMultiple([
11814
- lowerBinArrayPubKey,
11815
- upperBinArrayPubKey
11816
- ])).filter((binArray) => binArray !== null);
11817
- }
11818
- return [lowerBinArrays, upperBinArrays];
11819
- })();
11820
- binArrays.forEach((binArray) => {
11821
- if (!binArray)
11822
- return;
11823
- const [lowerBinIdForBinArray] = getBinArrayLowerUpperBinId(
11824
- binArray.index
11825
- );
11826
- binArray.bins.forEach((bin, idx) => {
11827
- const binId = lowerBinIdForBinArray.toNumber() + idx;
11828
- if (binId >= lowerBinId && binId <= upperBinId) {
11829
- const pricePerLamport = getPriceOfBinByBinId(
11830
- binId,
11831
- this.lbPair.binStep
11832
- ).toString();
11833
- bins.push({
11834
- binId,
11835
- xAmount: bin.amountX,
11836
- yAmount: bin.amountY,
11837
- supply: bin.liquiditySupply,
11838
- price: pricePerLamport,
11839
- version: binArray.version,
11840
- pricePerToken: new Decimal4(pricePerLamport).mul(new Decimal4(10 ** (baseTokenDecimal - quoteTokenDecimal))).toString()
11841
- });
11842
- }
11843
- });
11844
- });
11845
- }
11846
- return bins;
12021
+ async getBins(lbPairPubKey, lowerBinId, upperBinId, baseTokenDecimal, quoteTokenDecimal, lowerBinArray, upperBinArray) {
12022
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
12023
+ const upperBinArrayIndex = binIdToBinArrayIndex(new BN10(upperBinId));
12024
+ const hasCachedLowerBinArray = lowerBinArray != null;
12025
+ const hasCachedUpperBinArray = upperBinArray != null;
12026
+ const isSingleBinArray = lowerBinArrayIndex.eq(upperBinArrayIndex);
12027
+ const lowerBinArrayIndexOffset = hasCachedLowerBinArray ? 1 : 0;
12028
+ const upperBinArrayIndexOffset = hasCachedUpperBinArray ? -1 : 0;
12029
+ const binArrayPubkeys = range(
12030
+ lowerBinArrayIndex.toNumber() + lowerBinArrayIndexOffset,
12031
+ upperBinArrayIndex.toNumber() + upperBinArrayIndexOffset,
12032
+ (i) => deriveBinArray(lbPairPubKey, new BN10(i), this.program.programId)[0]
12033
+ );
12034
+ const fetchedBinArrays = binArrayPubkeys.length !== 0 ? await this.program.account.binArray.fetchMultiple(binArrayPubkeys) : [];
12035
+ const binArrays = [
12036
+ ...hasCachedLowerBinArray ? [lowerBinArray] : [],
12037
+ ...fetchedBinArrays,
12038
+ ...hasCachedUpperBinArray && !isSingleBinArray ? [upperBinArray] : []
12039
+ ];
12040
+ const binsById = new Map(binArrays.filter((x) => x != null).flatMap(({ bins, index }) => {
12041
+ const [lowerBinId2] = getBinArrayLowerUpperBinId(index);
12042
+ return bins.map((b, i) => [lowerBinId2.toNumber() + i, b]);
12043
+ }));
12044
+ const version = binArrays.length !== 0 ? binArrays[0].version : 1;
12045
+ return Array.from(enumerateBins(
12046
+ binsById,
12047
+ lowerBinId,
12048
+ upperBinId,
12049
+ this.lbPair.binStep,
12050
+ baseTokenDecimal,
12051
+ quoteTokenDecimal,
12052
+ version
12053
+ ));
11847
12054
  }
11848
12055
  async binArraysToBeCreate(lowerBinArrayIndex, upperBinArrayIndex) {
11849
12056
  const binArrayIndexes = Array.from(
11850
12057
  { length: upperBinArrayIndex.sub(lowerBinArrayIndex).toNumber() + 1 },
11851
12058
  (_, index) => index + lowerBinArrayIndex.toNumber()
11852
- ).map((idx) => new BN9(idx));
12059
+ ).map((idx) => new BN10(idx));
11853
12060
  const binArrays = [];
11854
12061
  for (const idx of binArrayIndexes) {
11855
12062
  const [binArrayPubKey] = deriveBinArray(
@@ -11867,7 +12074,7 @@ var DLMM = class {
11867
12074
  const binArrayIndexes = Array.from(
11868
12075
  { length: upperBinArrayIndex.sub(lowerBinArrayIndex).toNumber() + 1 },
11869
12076
  (_, index) => index + lowerBinArrayIndex.toNumber()
11870
- ).map((idx) => new BN9(idx));
12077
+ ).map((idx) => new BN10(idx));
11871
12078
  for (const idx of binArrayIndexes) {
11872
12079
  const [binArray] = deriveBinArray(
11873
12080
  this.pubkey,
@@ -11915,14 +12122,14 @@ var DLMM = class {
11915
12122
  shouldIncludePreIx = true
11916
12123
  }) {
11917
12124
  const lowerBinArrayIndex = binIdToBinArrayIndex(
11918
- new BN9(position.positionData.lowerBinId)
12125
+ new BN10(position.positionData.lowerBinId)
11919
12126
  );
11920
12127
  const [binArrayLower] = deriveBinArray(
11921
12128
  this.pubkey,
11922
12129
  lowerBinArrayIndex,
11923
12130
  this.program.programId
11924
12131
  );
11925
- const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
12132
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN10(1));
11926
12133
  const [binArrayUpper] = deriveBinArray(
11927
12134
  this.pubkey,
11928
12135
  upperBinArrayIndex,
@@ -11940,7 +12147,7 @@ var DLMM = class {
11940
12147
  owner
11941
12148
  );
11942
12149
  ix && preInstructions.push(ix);
11943
- const claimTransaction = await this.program.methods.claimReward(new BN9(i)).accounts({
12150
+ const claimTransaction = await this.program.methods.claimReward(new BN10(i)).accounts({
11944
12151
  lbPair: this.pubkey,
11945
12152
  sender: owner,
11946
12153
  position: position.publicKey,
@@ -11962,13 +12169,13 @@ var DLMM = class {
11962
12169
  shouldIncludePostIx = true
11963
12170
  }) {
11964
12171
  const { lowerBinId, feeOwner } = position.positionData;
11965
- const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
12172
+ const lowerBinArrayIndex = binIdToBinArrayIndex(new BN10(lowerBinId));
11966
12173
  const [binArrayLower] = deriveBinArray(
11967
12174
  this.pubkey,
11968
12175
  lowerBinArrayIndex,
11969
12176
  this.program.programId
11970
12177
  );
11971
- const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
12178
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN10(1));
11972
12179
  const [binArrayUpper] = deriveBinArray(
11973
12180
  this.pubkey,
11974
12181
  upperBinArrayIndex,
@@ -12039,6 +12246,7 @@ export {
12039
12246
  BASIS_POINT_MAX,
12040
12247
  BIN_ARRAY_BITMAP_SIZE,
12041
12248
  BIN_ARRAY_FEE,
12249
+ BinLiquidity,
12042
12250
  BitmapType,
12043
12251
  ClockLayout,
12044
12252
  DLMMError,
@@ -12078,7 +12286,6 @@ export {
12078
12286
  chunkedFetchMultiplePoolAccount,
12079
12287
  chunkedGetMultipleAccountInfos,
12080
12288
  chunks,
12081
- computeBudgetIx,
12082
12289
  computeFee,
12083
12290
  computeFeeFromAmount,
12084
12291
  computeProtocolFee,
@@ -12094,6 +12301,7 @@ export {
12094
12301
  derivePresetParameter,
12095
12302
  derivePresetParameter2,
12096
12303
  deriveReserve,
12304
+ enumerateBins,
12097
12305
  findNextBinArrayIndexWithLiquidity,
12098
12306
  findNextBinArrayWithLiquidity,
12099
12307
  fromWeightDistributionToAmount,
@@ -12102,6 +12310,8 @@ export {
12102
12310
  getBinArrayLowerUpperBinId,
12103
12311
  getBinArraysRequiredByPositionRange,
12104
12312
  getBinFromBinArray,
12313
+ getEstimatedComputeUnitIxWithBuffer,
12314
+ getEstimatedComputeUnitUsageWithBuffer,
12105
12315
  getOrCreateATAInstruction,
12106
12316
  getOutAmount,
12107
12317
  getPriceOfBinByBinId,
@@ -12113,6 +12323,7 @@ export {
12113
12323
  isBinIdWithinBinArray,
12114
12324
  isOverflowDefaultBinArrayBitmap,
12115
12325
  parseLogs,
12326
+ range,
12116
12327
  swapExactInQuoteAtBin,
12117
12328
  swapExactOutQuoteAtBin,
12118
12329
  toAmountAskSide,