@meteora-ag/dlmm 1.0.50-rc.2 → 1.0.51

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
@@ -6475,6 +6475,24 @@ function findNextBinArrayWithLiquidity(swapForY, activeBinId, lbPairState, binAr
6475
6475
  }
6476
6476
  return binArrayAccount;
6477
6477
  }
6478
+ function getBinArraysRequiredByPositionRange(pair, fromBinId, toBinId, programId) {
6479
+ const [minBinId, maxBinId] = fromBinId.lt(toBinId) ? [fromBinId, toBinId] : [toBinId, fromBinId];
6480
+ const positionCount = getPositionCount(minBinId, maxBinId);
6481
+ const binArrays = /* @__PURE__ */ new Map();
6482
+ for (let i = 0; i < positionCount.toNumber(); i++) {
6483
+ const lowerBinId = minBinId.add(MAX_BIN_PER_POSITION.mul(new BN6(i)));
6484
+ const lowerBinArrayIndex = binIdToBinArrayIndex(lowerBinId);
6485
+ const upperBinArrayIndex = lowerBinArrayIndex.add(new BN6(1));
6486
+ const [lowerBinArray] = deriveBinArray(pair, lowerBinArrayIndex, programId);
6487
+ const [upperBinArray] = deriveBinArray(pair, upperBinArrayIndex, programId);
6488
+ binArrays.set(lowerBinArray.toBase58(), lowerBinArrayIndex);
6489
+ binArrays.set(upperBinArray.toBase58(), upperBinArrayIndex);
6490
+ }
6491
+ return Array.from(binArrays, ([key, index]) => ({
6492
+ key: new PublicKey3(key),
6493
+ index
6494
+ }));
6495
+ }
6478
6496
 
6479
6497
  // src/dlmm/helpers/fee.ts
6480
6498
  import { BN as BN7 } from "@coral-xyz/anchor";
@@ -8485,6 +8503,17 @@ var DLMM = class {
8485
8503
  );
8486
8504
  return activeBinState;
8487
8505
  }
8506
+ /**
8507
+ * The function get the price of a bin based on its bin ID.
8508
+ * @param {number} binId - The `binId` parameter is a number that represents the ID of a bin.
8509
+ * @returns {number} the calculated price of a bin based on the provided binId.
8510
+ */
8511
+ getPriceOfBinByBinId(binId) {
8512
+ const binStepNum = new Decimal4(this.lbPair.binStep).div(
8513
+ new Decimal4(BASIS_POINT_MAX)
8514
+ );
8515
+ return new Decimal4(1).add(new Decimal4(binStepNum)).pow(new Decimal4(binId)).toString();
8516
+ }
8488
8517
  /**
8489
8518
  * The function get bin ID based on a given price and a boolean flag indicating whether to
8490
8519
  * round down or up.
@@ -9693,15 +9722,9 @@ var DLMM = class {
9693
9722
  }
9694
9723
  }
9695
9724
  }
9696
- const startPrice = getPriceOfBinByBinId(
9697
- startBinId.toNumber(),
9698
- this.lbPair.binStep
9699
- );
9700
- const endPrice = getPriceOfBinByBinId(
9701
- activeId.toNumber(),
9702
- this.lbPair.binStep
9703
- );
9704
- const priceImpact = startPrice.sub(endPrice).abs().div(startPrice).mul(new Decimal4(100));
9725
+ const startPrice = this.getPriceOfBinByBinId(startBinId.toNumber());
9726
+ const endPrice = this.getPriceOfBinByBinId(activeId.toNumber());
9727
+ const priceImpact = new Decimal4(startPrice).sub(new Decimal4(endPrice)).abs().div(new Decimal4(startPrice)).mul(new Decimal4(100));
9705
9728
  const maxInAmount = actualInAmount.mul(new BN9(BASIS_POINT_MAX).add(allowedSlippage)).div(new BN9(BASIS_POINT_MAX));
9706
9729
  return {
9707
9730
  inAmount: actualInAmount,
@@ -9719,6 +9742,8 @@ var DLMM = class {
9719
9742
  * - `inAmount`: Amount of lamport to swap in
9720
9743
  * - `swapForY`: Swap token X to Y when it is true, else reversed.
9721
9744
  * - `allowedSlippage`: Allowed slippage for the swap. Expressed in BPS. To convert from slippage percentage to BPS unit: SLIPPAGE_PERCENTAGE * 100
9745
+ * - `binArrays`: binArrays for swapQuote.
9746
+ * - `isPartialFill`: Flag to check whether the the swapQuote is partial fill, default = false.
9722
9747
  * @returns {SwapQuote}
9723
9748
  * - `consumedInAmount`: Amount of lamport to swap in
9724
9749
  * - `outAmount`: Amount of lamport to swap out
@@ -9728,7 +9753,7 @@ var DLMM = class {
9728
9753
  * - `priceImpact`: Price impact of the swap
9729
9754
  * - `binArraysPubkey`: Array of bin arrays involved in the swap
9730
9755
  */
9731
- swapQuote(inAmount, swapForY, allowedSlippage, binArrays) {
9756
+ swapQuote(inAmount, swapForY, allowedSlippage, binArrays, isPartialFill) {
9732
9757
  const currentTimestamp = Date.now() / 1e3;
9733
9758
  let inAmountLeft = inAmount;
9734
9759
  let vParameterClone = Object.assign({}, this.lbPair.vParameters);
@@ -9755,7 +9780,11 @@ var DLMM = class {
9755
9780
  binArrays
9756
9781
  );
9757
9782
  if (binArrayAccountToSwap == null) {
9758
- throw new Error("Insufficient liquidity");
9783
+ if (isPartialFill) {
9784
+ break;
9785
+ } else {
9786
+ throw new Error("Insufficient liquidity");
9787
+ }
9759
9788
  }
9760
9789
  binArraysForSwap.set(binArrayAccountToSwap.publicKey, true);
9761
9790
  this.updateVolatilityAccumulator(
@@ -9796,6 +9825,7 @@ var DLMM = class {
9796
9825
  }
9797
9826
  if (!startBin)
9798
9827
  throw new Error("Invalid start bin");
9828
+ inAmount = inAmount.sub(inAmountLeft);
9799
9829
  const outAmountWithoutSlippage = getOutAmount(
9800
9830
  startBin,
9801
9831
  inAmount.sub(
@@ -9805,10 +9835,6 @@ var DLMM = class {
9805
9835
  );
9806
9836
  const priceImpact = new Decimal4(actualOutAmount.toString()).sub(new Decimal4(outAmountWithoutSlippage.toString())).div(new Decimal4(outAmountWithoutSlippage.toString())).mul(new Decimal4(100));
9807
9837
  const minOutAmount = actualOutAmount.mul(new BN9(BASIS_POINT_MAX).sub(allowedSlippage)).div(new BN9(BASIS_POINT_MAX));
9808
- const endPrice = getPriceOfBinByBinId(
9809
- activeId.toNumber(),
9810
- this.lbPair.binStep
9811
- );
9812
9838
  return {
9813
9839
  consumedInAmount: inAmount,
9814
9840
  outAmount: actualOutAmount,
@@ -9816,8 +9842,7 @@ var DLMM = class {
9816
9842
  protocolFee: protocolFeeAmount,
9817
9843
  minOutAmount,
9818
9844
  priceImpact,
9819
- binArraysPubkey: [...binArraysForSwap.keys()],
9820
- endPrice
9845
+ binArraysPubkey: [...binArraysForSwap.keys()]
9821
9846
  };
9822
9847
  }
9823
9848
  async swapExactOut({
@@ -10106,7 +10131,9 @@ var DLMM = class {
10106
10131
  positions
10107
10132
  }) {
10108
10133
  const claimAllTxs = (await Promise.all(
10109
- positions.map(async (position, idx) => {
10134
+ positions.filter(
10135
+ ({ positionData: { rewardOne, rewardTwo } }) => !rewardOne.isZero() || !rewardTwo.isZero()
10136
+ ).map(async (position, idx) => {
10110
10137
  return await this.createClaimBuildMethod({
10111
10138
  owner,
10112
10139
  position,
@@ -10190,12 +10217,14 @@ var DLMM = class {
10190
10217
  positions
10191
10218
  }) {
10192
10219
  const claimAllTxs = (await Promise.all(
10193
- positions.map(async (position, idx) => {
10220
+ positions.filter(
10221
+ ({ positionData: { feeX, feeY } }) => !feeX.isZero() || !feeY.isZero()
10222
+ ).map(async (position, idx, positions2) => {
10194
10223
  return await this.createClaimSwapFeeMethod({
10195
10224
  owner,
10196
10225
  position,
10197
10226
  shouldIncludePretIx: idx === 0,
10198
- shouldIncludePostIx: idx === positions.length - 1
10227
+ shouldIncludePostIx: idx === positions2.length - 1
10199
10228
  });
10200
10229
  })
10201
10230
  )).flat();
@@ -10485,6 +10514,34 @@ var DLMM = class {
10485
10514
  addLiquidityIxs
10486
10515
  };
10487
10516
  }
10517
+ /**
10518
+ * Initializes bin arrays for the given bin array indexes if it wasn't initialized.
10519
+ *
10520
+ * @param {BN[]} binArrayIndexes - An array of bin array indexes to initialize.
10521
+ * @param {PublicKey} funder - The public key of the funder.
10522
+ * @return {Promise<TransactionInstruction[]>} An array of transaction instructions to initialize the bin arrays.
10523
+ */
10524
+ async initializeBinArrays(binArrayIndexes, funder) {
10525
+ const ixs = [];
10526
+ for (const idx of binArrayIndexes) {
10527
+ const [binArray] = deriveBinArray(
10528
+ this.pubkey,
10529
+ idx,
10530
+ this.program.programId
10531
+ );
10532
+ const binArrayAccount = await this.program.provider.connection.getAccountInfo(binArray);
10533
+ if (binArrayAccount === null) {
10534
+ ixs.push(
10535
+ await this.program.methods.initializeBinArray(idx).accounts({
10536
+ binArray,
10537
+ funder,
10538
+ lbPair: this.pubkey
10539
+ }).instruction()
10540
+ );
10541
+ }
10542
+ }
10543
+ return ixs;
10544
+ }
10488
10545
  /**
10489
10546
  *
10490
10547
  * @param
@@ -10584,7 +10641,9 @@ var DLMM = class {
10584
10641
  );
10585
10642
  createATAAccAndIx.forEach(({ ix }) => ix && preInstructions.push(ix));
10586
10643
  const claimAllSwapFeeTxs = (await Promise.all(
10587
- positions.map(async (position) => {
10644
+ positions.filter(
10645
+ ({ positionData: { feeX, feeY } }) => !feeX.isZero() || !feeY.isZero()
10646
+ ).map(async (position) => {
10588
10647
  return await this.createClaimSwapFeeMethod({
10589
10648
  owner,
10590
10649
  position,
@@ -10594,7 +10653,9 @@ var DLMM = class {
10594
10653
  })
10595
10654
  )).flat();
10596
10655
  const claimAllLMTxs = (await Promise.all(
10597
- positions.map(async (position) => {
10656
+ positions.filter(
10657
+ ({ positionData: { rewardOne, rewardTwo } }) => !rewardOne.isZero() || !rewardTwo.isZero()
10658
+ ).map(async (position) => {
10598
10659
  return await this.createClaimBuildMethod({
10599
10660
  owner,
10600
10661
  position,
@@ -10988,10 +11049,10 @@ var DLMM = class {
10988
11049
  binArray.bins.forEach((bin, idx) => {
10989
11050
  const binId = lowerBinIdForBinArray.toNumber() + idx;
10990
11051
  if (binId >= lowerBinId && binId <= upperBinId) {
10991
- const pricePerLamport = getPriceOfBinByBinId(
10992
- binId,
10993
- lbPair.binStep
10994
- ).toString();
11052
+ const pricePerLamport = this.getPriceOfBinByBinId(
11053
+ lbPair.binStep,
11054
+ binId
11055
+ );
10995
11056
  bins.push({
10996
11057
  binId,
10997
11058
  xAmount: bin.amountX,
@@ -11012,10 +11073,10 @@ var DLMM = class {
11012
11073
  binArray.bins.forEach((bin, idx) => {
11013
11074
  const binId = lowerBinIdForBinArray.toNumber() + idx;
11014
11075
  if (binId >= lowerBinId && binId <= upperBinId) {
11015
- const pricePerLamport = getPriceOfBinByBinId(
11016
- binId,
11017
- lbPair.binStep
11018
- ).toString();
11076
+ const pricePerLamport = this.getPriceOfBinByBinId(
11077
+ lbPair.binStep,
11078
+ binId
11079
+ );
11019
11080
  bins.push({
11020
11081
  binId,
11021
11082
  xAmount: bin.amountX,
@@ -11031,6 +11092,10 @@ var DLMM = class {
11031
11092
  }
11032
11093
  return bins;
11033
11094
  }
11095
+ static getPriceOfBinByBinId(binStep, binId) {
11096
+ const binStepNum = new Decimal4(binStep).div(new Decimal4(BASIS_POINT_MAX));
11097
+ return new Decimal4(1).add(new Decimal4(binStepNum)).pow(new Decimal4(binId)).toString();
11098
+ }
11034
11099
  /** Private method */
11035
11100
  processXYAmountDistribution(xYAmountDistribution) {
11036
11101
  let currentBinId = null;
@@ -11069,6 +11134,8 @@ var DLMM = class {
11069
11134
  const [lowerBinId2, upperBinId2] = getBinArrayLowerUpperBinId(lowerBinArrayIndex);
11070
11135
  const binArrayBins = [];
11071
11136
  for (let i = lowerBinId2.toNumber(); i <= upperBinId2.toNumber(); i++) {
11137
+ const binId = new BN9(i);
11138
+ const pricePerLamport = this.getPriceOfBinByBinId(binId.toNumber());
11072
11139
  binArrayBins.push({
11073
11140
  amountX: new BN9(0),
11074
11141
  amountY: new BN9(0),
@@ -11093,10 +11160,7 @@ var DLMM = class {
11093
11160
  binArray.bins.forEach((bin, idx) => {
11094
11161
  const binId = lowerBinIdForBinArray.toNumber() + idx;
11095
11162
  if (binId >= lowerBinId && binId <= upperBinId) {
11096
- const pricePerLamport = getPriceOfBinByBinId(
11097
- binId,
11098
- this.lbPair.binStep
11099
- ).toString();
11163
+ const pricePerLamport = this.getPriceOfBinByBinId(binId);
11100
11164
  bins.push({
11101
11165
  binId,
11102
11166
  xAmount: bin.amountX,
@@ -11137,10 +11201,7 @@ var DLMM = class {
11137
11201
  binArray.bins.forEach((bin, idx) => {
11138
11202
  const binId = lowerBinIdForBinArray.toNumber() + idx;
11139
11203
  if (binId >= lowerBinId && binId <= upperBinId) {
11140
- const pricePerLamport = getPriceOfBinByBinId(
11141
- binId,
11142
- this.lbPair.binStep
11143
- ).toString();
11204
+ const pricePerLamport = this.getPriceOfBinByBinId(binId);
11144
11205
  bins.push({
11145
11206
  binId,
11146
11207
  xAmount: bin.amountX,
@@ -11441,6 +11502,7 @@ export {
11441
11502
  fromWeightDistributionToAmountOneSide,
11442
11503
  getBaseFee,
11443
11504
  getBinArrayLowerUpperBinId,
11505
+ getBinArraysRequiredByPositionRange,
11444
11506
  getBinFromBinArray,
11445
11507
  getOrCreateATAInstruction,
11446
11508
  getOutAmount,