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

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.d.ts CHANGED
@@ -5226,7 +5226,6 @@ interface SwapQuote {
5226
5226
  minOutAmount: BN;
5227
5227
  priceImpact: Decimal;
5228
5228
  binArraysPubkey: any[];
5229
- endPrice: Decimal;
5230
5229
  }
5231
5230
  interface SwapQuoteExactOut {
5232
5231
  inAmount: BN;
@@ -5539,6 +5538,12 @@ declare class DLMM {
5539
5538
  * @returns an object with two properties: "binId" which is a number, and "price" which is a string.
5540
5539
  */
5541
5540
  getActiveBin(): Promise<BinLiquidity>;
5541
+ /**
5542
+ * The function get the price of a bin based on its bin ID.
5543
+ * @param {number} binId - The `binId` parameter is a number that represents the ID of a bin.
5544
+ * @returns {number} the calculated price of a bin based on the provided binId.
5545
+ */
5546
+ getPriceOfBinByBinId(binId: number): string;
5542
5547
  /**
5543
5548
  * The function get bin ID based on a given price and a boolean flag indicating whether to
5544
5549
  * round down or up.
@@ -5675,6 +5680,8 @@ declare class DLMM {
5675
5680
  * - `inAmount`: Amount of lamport to swap in
5676
5681
  * - `swapForY`: Swap token X to Y when it is true, else reversed.
5677
5682
  * - `allowedSlippage`: Allowed slippage for the swap. Expressed in BPS. To convert from slippage percentage to BPS unit: SLIPPAGE_PERCENTAGE * 100
5683
+ * - `binArrays`: binArrays for swapQuote.
5684
+ * - `isPartialFill`: Flag to check whether the the swapQuote is partial fill, default = false.
5678
5685
  * @returns {SwapQuote}
5679
5686
  * - `consumedInAmount`: Amount of lamport to swap in
5680
5687
  * - `outAmount`: Amount of lamport to swap out
@@ -5684,7 +5691,7 @@ declare class DLMM {
5684
5691
  * - `priceImpact`: Price impact of the swap
5685
5692
  * - `binArraysPubkey`: Array of bin arrays involved in the swap
5686
5693
  */
5687
- swapQuote(inAmount: BN, swapForY: boolean, allowedSlippage: BN, binArrays: BinArrayAccount[]): SwapQuote;
5694
+ swapQuote(inAmount: BN, swapForY: boolean, allowedSlippage: BN, binArrays: BinArrayAccount[], isPartialFill?: boolean): SwapQuote;
5688
5695
  swapExactOut({ inToken, outToken, outAmount, maxInAmount, lbPair, user, binArraysPubkey, }: SwapExactOutParams): Promise<Transaction>;
5689
5696
  /**
5690
5697
  * Returns a transaction to be signed and sent by user performing swap.
@@ -5839,6 +5846,7 @@ declare class DLMM {
5839
5846
  private static getClaimableSwapFee;
5840
5847
  private static processPosition;
5841
5848
  private static getBinsBetweenLowerAndUpperBound;
5849
+ private static getPriceOfBinByBinId;
5842
5850
  /** Private method */
5843
5851
  private processXYAmountDistribution;
5844
5852
  private getBins;
package/dist/index.js CHANGED
@@ -8485,6 +8485,17 @@ var DLMM = class {
8485
8485
  );
8486
8486
  return activeBinState;
8487
8487
  }
8488
+ /**
8489
+ * The function get the price of a bin based on its bin ID.
8490
+ * @param {number} binId - The `binId` parameter is a number that represents the ID of a bin.
8491
+ * @returns {number} the calculated price of a bin based on the provided binId.
8492
+ */
8493
+ getPriceOfBinByBinId(binId) {
8494
+ const binStepNum = new (0, _decimaljs2.default)(this.lbPair.binStep).div(
8495
+ new (0, _decimaljs2.default)(BASIS_POINT_MAX)
8496
+ );
8497
+ return new (0, _decimaljs2.default)(1).add(new (0, _decimaljs2.default)(binStepNum)).pow(new (0, _decimaljs2.default)(binId)).toString();
8498
+ }
8488
8499
  /**
8489
8500
  * The function get bin ID based on a given price and a boolean flag indicating whether to
8490
8501
  * round down or up.
@@ -9693,15 +9704,9 @@ var DLMM = class {
9693
9704
  }
9694
9705
  }
9695
9706
  }
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 (0, _decimaljs2.default)(100));
9707
+ const startPrice = this.getPriceOfBinByBinId(startBinId.toNumber());
9708
+ const endPrice = this.getPriceOfBinByBinId(activeId.toNumber());
9709
+ const priceImpact = new (0, _decimaljs2.default)(startPrice).sub(new (0, _decimaljs2.default)(endPrice)).abs().div(new (0, _decimaljs2.default)(startPrice)).mul(new (0, _decimaljs2.default)(100));
9705
9710
  const maxInAmount = actualInAmount.mul(new (0, _anchor.BN)(BASIS_POINT_MAX).add(allowedSlippage)).div(new (0, _anchor.BN)(BASIS_POINT_MAX));
9706
9711
  return {
9707
9712
  inAmount: actualInAmount,
@@ -9719,6 +9724,8 @@ var DLMM = class {
9719
9724
  * - `inAmount`: Amount of lamport to swap in
9720
9725
  * - `swapForY`: Swap token X to Y when it is true, else reversed.
9721
9726
  * - `allowedSlippage`: Allowed slippage for the swap. Expressed in BPS. To convert from slippage percentage to BPS unit: SLIPPAGE_PERCENTAGE * 100
9727
+ * - `binArrays`: binArrays for swapQuote.
9728
+ * - `isPartialFill`: Flag to check whether the the swapQuote is partial fill, default = false.
9722
9729
  * @returns {SwapQuote}
9723
9730
  * - `consumedInAmount`: Amount of lamport to swap in
9724
9731
  * - `outAmount`: Amount of lamport to swap out
@@ -9728,7 +9735,7 @@ var DLMM = class {
9728
9735
  * - `priceImpact`: Price impact of the swap
9729
9736
  * - `binArraysPubkey`: Array of bin arrays involved in the swap
9730
9737
  */
9731
- swapQuote(inAmount, swapForY, allowedSlippage, binArrays) {
9738
+ swapQuote(inAmount, swapForY, allowedSlippage, binArrays, isPartialFill) {
9732
9739
  const currentTimestamp = Date.now() / 1e3;
9733
9740
  let inAmountLeft = inAmount;
9734
9741
  let vParameterClone = Object.assign({}, this.lbPair.vParameters);
@@ -9755,7 +9762,11 @@ var DLMM = class {
9755
9762
  binArrays
9756
9763
  );
9757
9764
  if (binArrayAccountToSwap == null) {
9758
- throw new Error("Insufficient liquidity");
9765
+ if (isPartialFill) {
9766
+ break;
9767
+ } else {
9768
+ throw new Error("Insufficient liquidity");
9769
+ }
9759
9770
  }
9760
9771
  binArraysForSwap.set(binArrayAccountToSwap.publicKey, true);
9761
9772
  this.updateVolatilityAccumulator(
@@ -9796,6 +9807,7 @@ var DLMM = class {
9796
9807
  }
9797
9808
  if (!startBin)
9798
9809
  throw new Error("Invalid start bin");
9810
+ inAmount = inAmount.sub(inAmountLeft);
9799
9811
  const outAmountWithoutSlippage = getOutAmount(
9800
9812
  startBin,
9801
9813
  inAmount.sub(
@@ -9805,10 +9817,6 @@ var DLMM = class {
9805
9817
  );
9806
9818
  const priceImpact = new (0, _decimaljs2.default)(actualOutAmount.toString()).sub(new (0, _decimaljs2.default)(outAmountWithoutSlippage.toString())).div(new (0, _decimaljs2.default)(outAmountWithoutSlippage.toString())).mul(new (0, _decimaljs2.default)(100));
9807
9819
  const minOutAmount = actualOutAmount.mul(new (0, _anchor.BN)(BASIS_POINT_MAX).sub(allowedSlippage)).div(new (0, _anchor.BN)(BASIS_POINT_MAX));
9808
- const endPrice = getPriceOfBinByBinId(
9809
- activeId.toNumber(),
9810
- this.lbPair.binStep
9811
- );
9812
9820
  return {
9813
9821
  consumedInAmount: inAmount,
9814
9822
  outAmount: actualOutAmount,
@@ -9816,8 +9824,7 @@ var DLMM = class {
9816
9824
  protocolFee: protocolFeeAmount,
9817
9825
  minOutAmount,
9818
9826
  priceImpact,
9819
- binArraysPubkey: [...binArraysForSwap.keys()],
9820
- endPrice
9827
+ binArraysPubkey: [...binArraysForSwap.keys()]
9821
9828
  };
9822
9829
  }
9823
9830
  async swapExactOut({
@@ -10106,7 +10113,7 @@ var DLMM = class {
10106
10113
  positions
10107
10114
  }) {
10108
10115
  const claimAllTxs = (await Promise.all(
10109
- positions.map(async (position, idx) => {
10116
+ positions.filter(({ positionData: { rewardOne, rewardTwo } }) => !rewardOne.isZero() || !rewardTwo.isZero()).map(async (position, idx) => {
10110
10117
  return await this.createClaimBuildMethod({
10111
10118
  owner,
10112
10119
  position,
@@ -10190,12 +10197,12 @@ var DLMM = class {
10190
10197
  positions
10191
10198
  }) {
10192
10199
  const claimAllTxs = (await Promise.all(
10193
- positions.map(async (position, idx) => {
10200
+ positions.filter(({ positionData: { feeX, feeY } }) => !feeX.isZero() || !feeY.isZero()).map(async (position, idx, positions2) => {
10194
10201
  return await this.createClaimSwapFeeMethod({
10195
10202
  owner,
10196
10203
  position,
10197
10204
  shouldIncludePretIx: idx === 0,
10198
- shouldIncludePostIx: idx === positions.length - 1
10205
+ shouldIncludePostIx: idx === positions2.length - 1
10199
10206
  });
10200
10207
  })
10201
10208
  )).flat();
@@ -10584,7 +10591,7 @@ var DLMM = class {
10584
10591
  );
10585
10592
  createATAAccAndIx.forEach(({ ix }) => ix && preInstructions.push(ix));
10586
10593
  const claimAllSwapFeeTxs = (await Promise.all(
10587
- positions.map(async (position) => {
10594
+ positions.filter(({ positionData: { feeX, feeY } }) => !feeX.isZero() || !feeY.isZero()).map(async (position) => {
10588
10595
  return await this.createClaimSwapFeeMethod({
10589
10596
  owner,
10590
10597
  position,
@@ -10594,7 +10601,7 @@ var DLMM = class {
10594
10601
  })
10595
10602
  )).flat();
10596
10603
  const claimAllLMTxs = (await Promise.all(
10597
- positions.map(async (position) => {
10604
+ positions.filter(({ positionData: { rewardOne, rewardTwo } }) => !rewardOne.isZero() || !rewardTwo.isZero()).map(async (position) => {
10598
10605
  return await this.createClaimBuildMethod({
10599
10606
  owner,
10600
10607
  position,
@@ -10988,10 +10995,10 @@ var DLMM = class {
10988
10995
  binArray.bins.forEach((bin, idx) => {
10989
10996
  const binId = lowerBinIdForBinArray.toNumber() + idx;
10990
10997
  if (binId >= lowerBinId && binId <= upperBinId) {
10991
- const pricePerLamport = getPriceOfBinByBinId(
10992
- binId,
10993
- lbPair.binStep
10994
- ).toString();
10998
+ const pricePerLamport = this.getPriceOfBinByBinId(
10999
+ lbPair.binStep,
11000
+ binId
11001
+ );
10995
11002
  bins.push({
10996
11003
  binId,
10997
11004
  xAmount: bin.amountX,
@@ -11012,10 +11019,10 @@ var DLMM = class {
11012
11019
  binArray.bins.forEach((bin, idx) => {
11013
11020
  const binId = lowerBinIdForBinArray.toNumber() + idx;
11014
11021
  if (binId >= lowerBinId && binId <= upperBinId) {
11015
- const pricePerLamport = getPriceOfBinByBinId(
11016
- binId,
11017
- lbPair.binStep
11018
- ).toString();
11022
+ const pricePerLamport = this.getPriceOfBinByBinId(
11023
+ lbPair.binStep,
11024
+ binId
11025
+ );
11019
11026
  bins.push({
11020
11027
  binId,
11021
11028
  xAmount: bin.amountX,
@@ -11031,6 +11038,10 @@ var DLMM = class {
11031
11038
  }
11032
11039
  return bins;
11033
11040
  }
11041
+ static getPriceOfBinByBinId(binStep, binId) {
11042
+ const binStepNum = new (0, _decimaljs2.default)(binStep).div(new (0, _decimaljs2.default)(BASIS_POINT_MAX));
11043
+ return new (0, _decimaljs2.default)(1).add(new (0, _decimaljs2.default)(binStepNum)).pow(new (0, _decimaljs2.default)(binId)).toString();
11044
+ }
11034
11045
  /** Private method */
11035
11046
  processXYAmountDistribution(xYAmountDistribution) {
11036
11047
  let currentBinId = null;
@@ -11069,6 +11080,8 @@ var DLMM = class {
11069
11080
  const [lowerBinId2, upperBinId2] = getBinArrayLowerUpperBinId(lowerBinArrayIndex);
11070
11081
  const binArrayBins = [];
11071
11082
  for (let i = lowerBinId2.toNumber(); i <= upperBinId2.toNumber(); i++) {
11083
+ const binId = new (0, _anchor.BN)(i);
11084
+ const pricePerLamport = this.getPriceOfBinByBinId(binId.toNumber());
11072
11085
  binArrayBins.push({
11073
11086
  amountX: new (0, _anchor.BN)(0),
11074
11087
  amountY: new (0, _anchor.BN)(0),
@@ -11093,10 +11106,7 @@ var DLMM = class {
11093
11106
  binArray.bins.forEach((bin, idx) => {
11094
11107
  const binId = lowerBinIdForBinArray.toNumber() + idx;
11095
11108
  if (binId >= lowerBinId && binId <= upperBinId) {
11096
- const pricePerLamport = getPriceOfBinByBinId(
11097
- binId,
11098
- this.lbPair.binStep
11099
- ).toString();
11109
+ const pricePerLamport = this.getPriceOfBinByBinId(binId);
11100
11110
  bins.push({
11101
11111
  binId,
11102
11112
  xAmount: bin.amountX,
@@ -11137,10 +11147,7 @@ var DLMM = class {
11137
11147
  binArray.bins.forEach((bin, idx) => {
11138
11148
  const binId = lowerBinIdForBinArray.toNumber() + idx;
11139
11149
  if (binId >= lowerBinId && binId <= upperBinId) {
11140
- const pricePerLamport = getPriceOfBinByBinId(
11141
- binId,
11142
- this.lbPair.binStep
11143
- ).toString();
11150
+ const pricePerLamport = this.getPriceOfBinByBinId(binId);
11144
11151
  bins.push({
11145
11152
  binId,
11146
11153
  xAmount: bin.amountX,