@meteora-ag/cp-amm-sdk 1.0.1-rc.32 → 1.0.1-rc.34

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.js CHANGED
@@ -6371,10 +6371,10 @@ var FeeSchedulerMode = /* @__PURE__ */ ((FeeSchedulerMode2) => {
6371
6371
  FeeSchedulerMode2[FeeSchedulerMode2["Exponential"] = 1] = "Exponential";
6372
6372
  return FeeSchedulerMode2;
6373
6373
  })(FeeSchedulerMode || {});
6374
- var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode3) => {
6375
- CollectFeeMode3[CollectFeeMode3["BothToken"] = 0] = "BothToken";
6376
- CollectFeeMode3[CollectFeeMode3["OnlyB"] = 1] = "OnlyB";
6377
- return CollectFeeMode3;
6374
+ var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode2) => {
6375
+ CollectFeeMode2[CollectFeeMode2["BothToken"] = 0] = "BothToken";
6376
+ CollectFeeMode2[CollectFeeMode2["OnlyB"] = 1] = "OnlyB";
6377
+ return CollectFeeMode2;
6378
6378
  })(CollectFeeMode || {});
6379
6379
  var TradeDirection = /* @__PURE__ */ ((TradeDirection2) => {
6380
6380
  TradeDirection2[TradeDirection2["AtoB"] = 0] = "AtoB";
@@ -6745,14 +6745,6 @@ function pow(base, exp) {
6745
6745
  // src/math/mathUtils.ts
6746
6746
 
6747
6747
  var _decimaljs = require('decimal.js'); var _decimaljs2 = _interopRequireDefault(_decimaljs);
6748
- function mulShr(x, y, offset, rounding) {
6749
- const denominator = new (0, _anchor.BN)(1).shln(offset);
6750
- return mulDiv(x, y, denominator, rounding);
6751
- }
6752
- function shlDiv(x, y, offset, rounding) {
6753
- const scale = new (0, _anchor.BN)(1).shln(offset);
6754
- return mulDiv(x, scale, y, rounding);
6755
- }
6756
6748
  function mulDiv(x, y, denominator, rounding) {
6757
6749
  const { div, mod } = x.mul(y).divmod(denominator);
6758
6750
  if (rounding == 0 /* Up */ && !mod.isZero()) {
@@ -6775,7 +6767,6 @@ function decimalToQ64(num) {
6775
6767
 
6776
6768
  // src/helpers/curve.ts
6777
6769
 
6778
-
6779
6770
  function getNextSqrtPrice(amount, sqrtPrice, liquidity, aToB) {
6780
6771
  let result;
6781
6772
  if (aToB) {
@@ -6788,65 +6779,35 @@ function getNextSqrtPrice(amount, sqrtPrice, liquidity, aToB) {
6788
6779
  }
6789
6780
  return result;
6790
6781
  }
6791
- function getDeltaAmountA(lowerSqrtPrice, upperSqrtPrice, liquidity, rounding) {
6792
- const deltaSqrtPrice = upperSqrtPrice.sub(lowerSqrtPrice);
6793
- const prod = liquidity.mul(deltaSqrtPrice);
6794
- const denominator = lowerSqrtPrice.mul(upperSqrtPrice);
6795
- const result = shlDiv(prod, denominator, SCALE_OFFSET, rounding);
6796
- return result.shrn(SCALE_OFFSET);
6782
+ function getLiquidityDeltaFromAmountA(amountA, lowerSqrtPrice, upperSqrtPrice) {
6783
+ const product = amountA.mul(lowerSqrtPrice).mul(upperSqrtPrice);
6784
+ const denominator = upperSqrtPrice.sub(lowerSqrtPrice);
6785
+ return product.div(denominator);
6797
6786
  }
6798
- function getDeltaAmountB(lowerSqrtPrice, upperSqrtPrice, liquidity, rounding) {
6799
- const deltaSqrtPrice = upperSqrtPrice.sub(lowerSqrtPrice);
6800
- const prod = liquidity.mul(deltaSqrtPrice);
6801
- let result;
6802
- if (rounding == 0 /* Up */) {
6803
- const denominator = new (0, _anchor.BN)(1).shln(SCALE_OFFSET * 2);
6804
- result = divCeil(prod, denominator);
6805
- } else {
6806
- result = prod.shrn(SCALE_OFFSET * 2);
6807
- }
6808
- return result;
6809
- }
6810
- function getLiquidityDeltaFromAmountA(maxAmountA, lowerSqrtPrice, upperSqrtPrice) {
6811
- const prod = new (0, _decimaljs2.default)(
6812
- maxAmountA.mul(upperSqrtPrice.mul(lowerSqrtPrice)).toString()
6813
- );
6814
- const delta = new (0, _decimaljs2.default)(upperSqrtPrice.sub(lowerSqrtPrice).toString());
6815
- return new (0, _anchor.BN)(prod.div(delta).floor().toFixed());
6816
- }
6817
- function getLiquidityDeltaFromAmountB(maxAmountB, lowerSqrtPrice, upperSqrtPrice) {
6818
- const denominator = new (0, _decimaljs2.default)(
6819
- upperSqrtPrice.sub(lowerSqrtPrice).toString()
6820
- );
6821
- const prod = new (0, _decimaljs2.default)(maxAmountB.toString()).mul(
6822
- _decimaljs2.default.pow(2, SCALE_OFFSET * 2)
6823
- );
6824
- return new (0, _anchor.BN)(prod.div(denominator).floor().toFixed());
6787
+ function getLiquidityDeltaFromAmountB(amountB, lowerSqrtPrice, upperSqrtPrice) {
6788
+ const denominator = upperSqrtPrice.sub(lowerSqrtPrice);
6789
+ const product = amountB.shln(128);
6790
+ return product.div(denominator);
6825
6791
  }
6826
6792
  function getAmountAFromLiquidityDelta(liquidity, currentSqrtPrice, maxSqrtPrice, rounding) {
6827
- const prod = new (0, _decimaljs2.default)(liquidity.toString()).mul(
6828
- new (0, _decimaljs2.default)(maxSqrtPrice.sub(currentSqrtPrice).toString())
6829
- );
6793
+ const product = liquidity.mul(maxSqrtPrice.sub(currentSqrtPrice));
6830
6794
  const denominator = currentSqrtPrice.mul(maxSqrtPrice);
6831
- const result = prod.mul(_decimaljs2.default.pow(2, 64)).div(new (0, _decimaljs2.default)(denominator.toString()));
6832
6795
  if (rounding == 0 /* Up */) {
6833
- return result.div(_decimaljs2.default.pow(2, 64)).ceil().toFixed();
6796
+ return product.add(denominator.sub(new (0, _anchor.BN)(1))).div(denominator);
6834
6797
  }
6835
- return result.div(_decimaljs2.default.pow(2, 64)).floor().toFixed();
6798
+ return product.div(denominator);
6836
6799
  }
6837
6800
  function getAmountBFromLiquidityDelta(liquidity, currentSqrtPrice, minSqrtPrice, rounding) {
6838
- const delta = currentSqrtPrice.sub(minSqrtPrice);
6839
- const prod = new (0, _decimaljs2.default)(liquidity.toString()).mul(
6840
- new (0, _decimaljs2.default)(delta.toString())
6841
- );
6801
+ const one = new (0, _anchor.BN)(1).shln(128);
6802
+ const deltaPrice = currentSqrtPrice.sub(minSqrtPrice);
6803
+ const result = liquidity.mul(deltaPrice);
6842
6804
  if (rounding == 0 /* Up */) {
6843
- return prod.div(_decimaljs2.default.pow(2, 128)).ceil().toFixed();
6805
+ return result.add(one.sub(new (0, _anchor.BN)(1))).div(one);
6844
6806
  }
6845
- return prod.div(_decimaljs2.default.pow(2, 128)).floor().toFixed();
6807
+ return result.shrn(128);
6846
6808
  }
6847
6809
 
6848
6810
  // src/helpers/fee.ts
6849
-
6850
6811
  function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reductionFactor) {
6851
6812
  let feeNumerator;
6852
6813
  if (feeSchedulerMode == 0 /* Linear */) {
@@ -6860,14 +6821,12 @@ function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reduct
6860
6821
  return feeNumerator;
6861
6822
  }
6862
6823
  function getDynamicFeeNumerator(volatilityAccumulator, binStep, variableFeeControl) {
6863
- const volatilityAccumulatorDecimal = new (0, _decimaljs2.default)(
6864
- volatilityAccumulator.toString()
6865
- ).div(_decimaljs2.default.pow(2, 64));
6866
- const squareVfaBin = volatilityAccumulatorDecimal.mul(new (0, _decimaljs2.default)(binStep.toString())).pow(2);
6867
- const vFee = squareVfaBin.mul(new (0, _decimaljs2.default)(variableFeeControl.toString()));
6868
- return new (0, _anchor.BN)(
6869
- vFee.add(99999999999).div(1e11).floor().toFixed()
6870
- );
6824
+ if (variableFeeControl.isZero()) {
6825
+ return new (0, _anchor.BN)(0);
6826
+ }
6827
+ const squareVfaBin = volatilityAccumulator.mul(new (0, _anchor.BN)(binStep)).pow(new (0, _anchor.BN)(2));
6828
+ const vFee = variableFeeControl.mul(squareVfaBin);
6829
+ return vFee.add(new (0, _anchor.BN)(99999999999)).div(new (0, _anchor.BN)(1e11));
6871
6830
  }
6872
6831
  function getFeeNumerator(currentPoint, activationPoint, numberOfPeriod, periodFrequency, feeSchedulerMode, cliffFeeNumerator, reductionFactor, dynamicFeeParams) {
6873
6832
  if (Number(periodFrequency) == 0) {
@@ -6918,12 +6877,12 @@ function getSwapAmount(inAmount, sqrtPrice, liquidity, tradeFeeNumerator, aToB,
6918
6877
  totalFee = getTotalFeeOnAmount(inAmount, tradeFeeNumerator);
6919
6878
  actualInAmount = inAmount.sub(totalFee);
6920
6879
  }
6921
- const outAmount = aToB ? getDeltaAmountB(
6880
+ const outAmount = aToB ? getAmountBFromLiquidityDelta(
6922
6881
  getNextSqrtPrice(actualInAmount, sqrtPrice, liquidity, true),
6923
6882
  sqrtPrice,
6924
6883
  liquidity,
6925
6884
  1 /* Down */
6926
- ) : getDeltaAmountA(
6885
+ ) : getAmountBFromLiquidityDelta(
6927
6886
  sqrtPrice,
6928
6887
  getNextSqrtPrice(actualInAmount, sqrtPrice, liquidity, false),
6929
6888
  liquidity,
@@ -7181,8 +7140,49 @@ function calculateTransferFeeExcludedAmount(transferFeeIncludedAmount, mint, cur
7181
7140
  };
7182
7141
  }
7183
7142
 
7184
- // src/CpAmm.ts
7143
+ // src/helpers/vestings.ts
7144
+
7185
7145
  var _bnjs = require('bn.js');
7146
+ function isVestingComplete(vestingData, currentPoint) {
7147
+ const cliffPoint = vestingData.cliffPoint;
7148
+ const periodFrequency = vestingData.periodFrequency;
7149
+ const numberOfPeriods = vestingData.numberOfPeriod;
7150
+ const endPoint = cliffPoint.add(periodFrequency.muln(numberOfPeriods));
7151
+ return currentPoint.gte(endPoint);
7152
+ }
7153
+ function getTotalLockedLiquidity(vestingData) {
7154
+ return vestingData.cliffUnlockLiquidity.add(
7155
+ vestingData.liquidityPerPeriod.mul(new (0, _anchor.BN)(vestingData.numberOfPeriod))
7156
+ );
7157
+ }
7158
+ function getAvailableVestingLiquidity(vestingData, currentPoint) {
7159
+ const {
7160
+ cliffPoint,
7161
+ periodFrequency,
7162
+ cliffUnlockLiquidity,
7163
+ liquidityPerPeriod,
7164
+ numberOfPeriod,
7165
+ totalReleasedLiquidity
7166
+ } = vestingData;
7167
+ if (currentPoint < cliffPoint) {
7168
+ return new (0, _anchor.BN)(0);
7169
+ }
7170
+ if (periodFrequency.isZero()) {
7171
+ return cliffUnlockLiquidity.sub(totalReleasedLiquidity);
7172
+ }
7173
+ let passedPeriod = new (0, _anchor.BN)(currentPoint).sub(cliffPoint).div(periodFrequency);
7174
+ passedPeriod = _bnjs.min.call(void 0, passedPeriod, new (0, _anchor.BN)(numberOfPeriod));
7175
+ const unlockedLiquidity = cliffUnlockLiquidity.add(
7176
+ passedPeriod.mul(liquidityPerPeriod)
7177
+ );
7178
+ const availableReleasingLiquidity = unlockedLiquidity.sub(
7179
+ totalReleasedLiquidity
7180
+ );
7181
+ return availableReleasingLiquidity;
7182
+ }
7183
+
7184
+ // src/CpAmm.ts
7185
+
7186
7186
  var CpAmm = class {
7187
7187
  constructor(connection) {
7188
7188
  this._program = new (0, _anchor.Program)(cp_amm_default, {
@@ -7412,6 +7412,33 @@ var CpAmm = class {
7412
7412
  }).instruction();
7413
7413
  });
7414
7414
  }
7415
+ /**
7416
+ * Builds an instruction to refresh vesting for a position
7417
+ * @param params Parameters required for the refresh vesting instruction
7418
+ * @returns Transaction instruction or null if no vestings to refresh
7419
+ */
7420
+ buildRefreshVestingInstruction(params) {
7421
+ return __async(this, null, function* () {
7422
+ const { owner, position, positionNftAccount, pool, vestingAccounts } = params;
7423
+ if (vestingAccounts.length == 0) {
7424
+ return null;
7425
+ }
7426
+ return yield this._program.methods.refreshVesting().accountsPartial({
7427
+ position,
7428
+ positionNftAccount,
7429
+ pool,
7430
+ owner
7431
+ }).remainingAccounts(
7432
+ vestingAccounts.map((pubkey) => {
7433
+ return {
7434
+ isSigner: false,
7435
+ isWritable: true,
7436
+ pubkey
7437
+ };
7438
+ })
7439
+ ).instruction();
7440
+ });
7441
+ }
7415
7442
  /**
7416
7443
  * Fetches the Config state of the program.
7417
7444
  * @param config - Public key of the config account.
@@ -7559,6 +7586,44 @@ var CpAmm = class {
7559
7586
  );
7560
7587
  return totalLockedLiquidity.gtn(0);
7561
7588
  }
7589
+ isPermanentLockedPosition(positionState) {
7590
+ return positionState.permanentLockedLiquidity.gtn(0);
7591
+ }
7592
+ /**
7593
+ * Checks if a position can be unlocked based on its locking state and vesting schedules.
7594
+ *
7595
+ * This method evaluates whether a position is eligible for operations that require
7596
+ * unlocked liquidity, such as removing all liquidity or closing the position. It checks both
7597
+ * permanent locks and time-based vesting schedules.
7598
+ *
7599
+ * @private
7600
+ * @param {PositionState} positionState - The current state of the position
7601
+ * @param {Array<{account: PublicKey; vestingState: VestingState}>} vestings - Array of vesting accounts and their states
7602
+ * @param {BN} currentPoint - Current timestamp or slot number (depending on activation type of pool)
7603
+ *
7604
+ * @returns {Object} Result object containing unlock status and reason
7605
+ * @returns {boolean} result.canUnlock - Whether the position can be unlocked
7606
+ * @returns {string|undefined} result.reason - Reason why position cannot be unlocked (if applicable)
7607
+ */
7608
+ canUnlockPosition(positionState, vestings, currentPoint) {
7609
+ if (vestings.length > 0) {
7610
+ if (this.isPermanentLockedPosition(positionState)) {
7611
+ return {
7612
+ canUnlock: false,
7613
+ reason: "Position is permanently locked"
7614
+ };
7615
+ }
7616
+ for (const vesting of vestings) {
7617
+ if (!isVestingComplete(vesting.vestingState, currentPoint)) {
7618
+ return {
7619
+ canUnlock: false,
7620
+ reason: "Position has incomplete vesting schedule"
7621
+ };
7622
+ }
7623
+ }
7624
+ }
7625
+ return { canUnlock: true };
7626
+ }
7562
7627
  isPoolExist(pool) {
7563
7628
  return __async(this, null, function* () {
7564
7629
  const poolState = yield this._program.account.pool.fetchNullable(pool);
@@ -7631,22 +7696,25 @@ var CpAmm = class {
7631
7696
  aToB,
7632
7697
  collectFeeMode
7633
7698
  );
7634
- let actualAmoutOut = amountOut;
7699
+ let actualAmountOut = amountOut;
7635
7700
  if (outputTokenInfo) {
7636
- actualAmoutOut = calculateTransferFeeExcludedAmount(
7701
+ actualAmountOut = calculateTransferFeeExcludedAmount(
7637
7702
  amountOut,
7638
7703
  outputTokenInfo.mint,
7639
7704
  outputTokenInfo.currentEpoch
7640
7705
  ).amount;
7641
7706
  }
7642
- const minSwapOutAmount = getMinAmountWithSlippage(actualAmoutOut, slippage);
7707
+ const minSwapOutAmount = getMinAmountWithSlippage(
7708
+ actualAmountOut,
7709
+ slippage
7710
+ );
7643
7711
  return {
7644
7712
  swapInAmount: inAmount,
7645
7713
  consumedInAmount: actualAmountIn,
7646
- swapOutAmount: actualAmoutOut,
7714
+ swapOutAmount: actualAmountOut,
7647
7715
  minSwapOutAmount,
7648
7716
  totalFee,
7649
- priceImpact: getPriceImpact(minSwapOutAmount, actualAmoutOut)
7717
+ priceImpact: getPriceImpact(minSwapOutAmount, actualAmountOut)
7650
7718
  };
7651
7719
  });
7652
7720
  }
@@ -7755,15 +7823,15 @@ var CpAmm = class {
7755
7823
  return {
7756
7824
  liquidityDelta,
7757
7825
  outAmountA: tokenATokenInfo ? calculateTransferFeeExcludedAmount(
7758
- new (0, _anchor.BN)(amountA),
7826
+ amountA,
7759
7827
  tokenATokenInfo.mint,
7760
7828
  tokenATokenInfo.currentEpoch
7761
- ).amount : new (0, _anchor.BN)(amountA),
7829
+ ).amount : amountA,
7762
7830
  outAmountB: tokenBTokenInfo ? calculateTransferFeeExcludedAmount(
7763
- new (0, _anchor.BN)(amountB),
7831
+ amountB,
7764
7832
  tokenBTokenInfo.mint,
7765
7833
  tokenBTokenInfo.currentEpoch
7766
- ).amount : new (0, _anchor.BN)(amountB)
7834
+ ).amount : amountB
7767
7835
  };
7768
7836
  });
7769
7837
  }
@@ -7823,13 +7891,13 @@ var CpAmm = class {
7823
7891
  tokenAInfo.currentEpoch
7824
7892
  ).transferFee
7825
7893
  ) : tokenAAmount;
7826
- const actualAmountBIn = tokenBInfo ? tokenAAmount.sub(
7894
+ const actualAmountBIn = tokenBInfo ? tokenBAmount.sub(
7827
7895
  calculateTransferFeeIncludedAmount(
7828
7896
  tokenBAmount,
7829
7897
  tokenBInfo.mint,
7830
7898
  tokenBInfo.currentEpoch
7831
7899
  ).transferFee
7832
- ) : tokenAAmount;
7900
+ ) : tokenBAmount;
7833
7901
  const initSqrtPrice = calculateInitSqrtPrice(
7834
7902
  tokenAAmount,
7835
7903
  tokenBAmount,
@@ -8162,7 +8230,8 @@ var CpAmm = class {
8162
8230
  tokenAVault,
8163
8231
  tokenBVault,
8164
8232
  tokenAProgram,
8165
- tokenBProgram
8233
+ tokenBProgram,
8234
+ vestings
8166
8235
  } = params;
8167
8236
  const {
8168
8237
  tokenAAta: tokenAAccount,
@@ -8182,6 +8251,16 @@ var CpAmm = class {
8182
8251
  const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
8183
8252
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
8184
8253
  }
8254
+ if (vestings.length > 0) {
8255
+ const refreshVestingInstruction = yield this.buildRefreshVestingInstruction({
8256
+ owner,
8257
+ position,
8258
+ positionNftAccount,
8259
+ pool,
8260
+ vestingAccounts: vestings.map((item) => item.account)
8261
+ });
8262
+ refreshVestingInstruction && preInstructions.push(refreshVestingInstruction);
8263
+ }
8185
8264
  return yield this._program.methods.removeLiquidity({
8186
8265
  liquidityDelta,
8187
8266
  tokenAAmountThreshold,
@@ -8222,7 +8301,8 @@ var CpAmm = class {
8222
8301
  tokenAVault,
8223
8302
  tokenBVault,
8224
8303
  tokenAProgram,
8225
- tokenBProgram
8304
+ tokenBProgram,
8305
+ vestings
8226
8306
  } = params;
8227
8307
  const {
8228
8308
  tokenAAta: tokenAAccount,
@@ -8242,6 +8322,16 @@ var CpAmm = class {
8242
8322
  const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
8243
8323
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
8244
8324
  }
8325
+ if (vestings.length > 0) {
8326
+ const refreshVestingInstruction = yield this.buildRefreshVestingInstruction({
8327
+ owner,
8328
+ position,
8329
+ positionNftAccount,
8330
+ pool,
8331
+ vestingAccounts: vestings.map((item) => item.account)
8332
+ });
8333
+ refreshVestingInstruction && preInstructions.push(refreshVestingInstruction);
8334
+ }
8245
8335
  const removeAllLiquidityInstruction = yield this.buildRemoveAllLiquidityInstruction({
8246
8336
  poolAuthority: this.poolAuthority,
8247
8337
  owner,
@@ -8399,21 +8489,8 @@ var CpAmm = class {
8399
8489
  */
8400
8490
  refreshVesting(params) {
8401
8491
  return __async(this, null, function* () {
8402
- const { owner, position, positionNftAccount, pool, vestings } = params;
8403
- return yield this._program.methods.refreshVesting().accountsPartial({
8404
- position,
8405
- positionNftAccount,
8406
- pool,
8407
- owner
8408
- }).remainingAccounts(
8409
- vestings.map((pubkey) => {
8410
- return {
8411
- isSigner: false,
8412
- isWritable: true,
8413
- pubkey
8414
- };
8415
- })
8416
- ).transaction();
8492
+ const instruction = yield this.buildRefreshVestingInstruction(params);
8493
+ return new (0, _web3js.Transaction)().add(instruction);
8417
8494
  });
8418
8495
  }
8419
8496
  /**
@@ -8511,13 +8588,19 @@ var CpAmm = class {
8511
8588
  positionState,
8512
8589
  poolState,
8513
8590
  tokenAAmountThreshold,
8514
- tokenBAmountThreshold
8591
+ tokenBAmountThreshold,
8592
+ vestings,
8593
+ currentPoint
8515
8594
  } = params;
8516
8595
  const { nftMint: positionNftMint, pool } = positionState;
8517
8596
  const { tokenAMint, tokenBMint, tokenAVault, tokenBVault } = poolState;
8518
- const isLockedPosition = this.isLockedPosition(positionState);
8519
- if (isLockedPosition) {
8520
- throw Error("position must be unlocked");
8597
+ const { canUnlock, reason } = this.canUnlockPosition(
8598
+ positionState,
8599
+ vestings,
8600
+ currentPoint
8601
+ );
8602
+ if (!canUnlock) {
8603
+ throw new Error(`Cannot remove liquidity: ${reason}`);
8521
8604
  }
8522
8605
  const tokenAProgram = getTokenProgram(poolState.tokenAFlag);
8523
8606
  const tokenBProgram = getTokenProgram(poolState.tokenBFlag);
@@ -8539,6 +8622,16 @@ var CpAmm = class {
8539
8622
  const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
8540
8623
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
8541
8624
  }
8625
+ if (vestings.length > 0) {
8626
+ const refreshVestingInstruction = yield this.buildRefreshVestingInstruction({
8627
+ owner,
8628
+ position,
8629
+ positionNftAccount,
8630
+ pool,
8631
+ vestingAccounts: vestings.map((item) => item.account)
8632
+ });
8633
+ refreshVestingInstruction && preInstructions.push(refreshVestingInstruction);
8634
+ }
8542
8635
  const transaction = new (0, _web3js.Transaction)();
8543
8636
  if (preInstructions.length > 0) {
8544
8637
  transaction.add(...preInstructions);
@@ -8617,13 +8710,19 @@ var CpAmm = class {
8617
8710
  tokenAAmountAddLiquidityThreshold,
8618
8711
  tokenBAmountAddLiquidityThreshold,
8619
8712
  tokenAAmountRemoveLiquidityThreshold,
8620
- tokenBAmountRemoveLiquidityThreshold
8713
+ tokenBAmountRemoveLiquidityThreshold,
8714
+ positionBVestings,
8715
+ currentPoint
8621
8716
  } = params;
8622
- const isLockedPosition = this.isLockedPosition(positionBState);
8623
- if (isLockedPosition) {
8624
- throw Error("position must be unlocked");
8717
+ const { canUnlock, reason } = this.canUnlockPosition(
8718
+ positionBState,
8719
+ positionBVestings,
8720
+ currentPoint
8721
+ );
8722
+ if (!canUnlock) {
8723
+ throw new Error(`Cannot remove liquidity: ${reason}`);
8625
8724
  }
8626
- const postionBLiquidityDelta = positionBState.unlockedLiquidity;
8725
+ const positionBLiquidityDelta = positionBState.unlockedLiquidity;
8627
8726
  const pool = positionBState.pool;
8628
8727
  const { tokenAMint, tokenBMint, tokenAVault, tokenBVault } = poolState;
8629
8728
  const tokenAProgram = getTokenProgram(poolState.tokenAFlag);
@@ -8646,6 +8745,16 @@ var CpAmm = class {
8646
8745
  const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
8647
8746
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
8648
8747
  }
8748
+ if (positionBVestings.length > 0) {
8749
+ const refreshVestingInstruction = yield this.buildRefreshVestingInstruction({
8750
+ owner,
8751
+ position: positionB,
8752
+ positionNftAccount: positionBNftAccount,
8753
+ pool,
8754
+ vestingAccounts: positionBVestings.map((item) => item.account)
8755
+ });
8756
+ refreshVestingInstruction && preInstructions.push(refreshVestingInstruction);
8757
+ }
8649
8758
  const transaction = new (0, _web3js.Transaction)();
8650
8759
  if (preInstructions.length > 0) {
8651
8760
  transaction.add(...preInstructions);
@@ -8697,7 +8806,7 @@ var CpAmm = class {
8697
8806
  tokenBVault,
8698
8807
  tokenAProgram,
8699
8808
  tokenBProgram,
8700
- liquidityDelta: postionBLiquidityDelta,
8809
+ liquidityDelta: positionBLiquidityDelta,
8701
8810
  tokenAAmountThreshold: tokenAAmountAddLiquidityThreshold,
8702
8811
  tokenBAmountThreshold: tokenBAmountAddLiquidityThreshold
8703
8812
  });
@@ -8997,6 +9106,5 @@ var index_default = cp_amm_default;
8997
9106
 
8998
9107
 
8999
9108
 
9000
-
9001
- exports.ActivationPoint = ActivationPoint; exports.ActivationType = ActivationType; exports.BASIS_POINT_MAX = BASIS_POINT_MAX; exports.CP_AMM_PROGRAM_ID = CP_AMM_PROGRAM_ID; exports.CollectFeeMode = CollectFeeMode; exports.CpAmm = CpAmm; exports.FEE_DENOMINATOR = FEE_DENOMINATOR; exports.FeeSchedulerMode = FeeSchedulerMode; exports.LIQUIDITY_SCALE = LIQUIDITY_SCALE; exports.MAX_CU_BUFFER = MAX_CU_BUFFER; exports.MAX_FEE_NUMERATOR = MAX_FEE_NUMERATOR; exports.MAX_SQRT_PRICE = MAX_SQRT_PRICE; exports.MIN_CU_BUFFER = MIN_CU_BUFFER; exports.MIN_SQRT_PRICE = MIN_SQRT_PRICE; exports.ONE = ONE; exports.PRECISION = PRECISION; exports.Rounding = Rounding; exports.SCALE_OFFSET = SCALE_OFFSET; exports.TradeDirection = TradeDirection; exports.calculateInitSqrtPrice = calculateInitSqrtPrice; exports.calculateTransferFeeExcludedAmount = calculateTransferFeeExcludedAmount; exports.calculateTransferFeeIncludedAmount = calculateTransferFeeIncludedAmount; exports.decimalToQ64 = decimalToQ64; exports.default = index_default; exports.deriveClaimFeeOperatorAddress = deriveClaimFeeOperatorAddress; exports.deriveConfigAddress = deriveConfigAddress; exports.deriveCustomizablePoolAddress = deriveCustomizablePoolAddress; exports.derivePoolAddress = derivePoolAddress; exports.derivePoolAuthority = derivePoolAuthority; exports.derivePositionAddress = derivePositionAddress; exports.derivePositionNftAccount = derivePositionNftAccount; exports.deriveRewardVaultAddress = deriveRewardVaultAddress; exports.deriveTokenBadge = deriveTokenBadge; exports.deriveTokenBadgeAddress = deriveTokenBadgeAddress; exports.deriveTokenVaultAddress = deriveTokenVaultAddress; exports.divCeil = divCeil; exports.getAllUserPositionNftAccount = getAllUserPositionNftAccount; exports.getAmountAFromLiquidityDelta = getAmountAFromLiquidityDelta; exports.getAmountBFromLiquidityDelta = getAmountBFromLiquidityDelta; exports.getBaseFeeNumerator = getBaseFeeNumerator; exports.getDeltaAmountA = getDeltaAmountA; exports.getDeltaAmountB = getDeltaAmountB; exports.getDynamicFeeNumerator = getDynamicFeeNumerator; exports.getEstimatedComputeUnitIxWithBuffer = getEstimatedComputeUnitIxWithBuffer; exports.getEstimatedComputeUnitUsageWithBuffer = getEstimatedComputeUnitUsageWithBuffer; exports.getFeeNumerator = getFeeNumerator; exports.getFirstKey = getFirstKey; exports.getLiquidityDeltaFromAmountA = getLiquidityDeltaFromAmountA; exports.getLiquidityDeltaFromAmountB = getLiquidityDeltaFromAmountB; exports.getMaxAmountWithSlippage = getMaxAmountWithSlippage; exports.getMinAmountWithSlippage = getMinAmountWithSlippage; exports.getNextSqrtPrice = getNextSqrtPrice; exports.getNftOwner = getNftOwner; exports.getOrCreateATAInstruction = getOrCreateATAInstruction; exports.getPriceFromSqrtPrice = getPriceFromSqrtPrice; exports.getPriceImpact = getPriceImpact; exports.getSecondKey = getSecondKey; exports.getSimulationComputeUnits = getSimulationComputeUnits; exports.getSqrtPriceFromPrice = getSqrtPriceFromPrice; exports.getSwapAmount = getSwapAmount; exports.getTokenDecimals = getTokenDecimals; exports.getTokenProgram = getTokenProgram; exports.getUnClaimReward = getUnClaimReward; exports.mulDiv = mulDiv; exports.mulShr = mulShr; exports.positionByPoolFilter = positionByPoolFilter; exports.pow = pow; exports.q64ToDecimal = q64ToDecimal; exports.shlDiv = shlDiv; exports.unwrapSOLInstruction = unwrapSOLInstruction; exports.vestingByPositionFilter = vestingByPositionFilter; exports.wrapSOLInstruction = wrapSOLInstruction;
9109
+ exports.ActivationPoint = ActivationPoint; exports.ActivationType = ActivationType; exports.BASIS_POINT_MAX = BASIS_POINT_MAX; exports.CP_AMM_PROGRAM_ID = CP_AMM_PROGRAM_ID; exports.CollectFeeMode = CollectFeeMode; exports.CpAmm = CpAmm; exports.FEE_DENOMINATOR = FEE_DENOMINATOR; exports.FeeSchedulerMode = FeeSchedulerMode; exports.LIQUIDITY_SCALE = LIQUIDITY_SCALE; exports.MAX_CU_BUFFER = MAX_CU_BUFFER; exports.MAX_FEE_NUMERATOR = MAX_FEE_NUMERATOR; exports.MAX_SQRT_PRICE = MAX_SQRT_PRICE; exports.MIN_CU_BUFFER = MIN_CU_BUFFER; exports.MIN_SQRT_PRICE = MIN_SQRT_PRICE; exports.ONE = ONE; exports.PRECISION = PRECISION; exports.Rounding = Rounding; exports.SCALE_OFFSET = SCALE_OFFSET; exports.TradeDirection = TradeDirection; exports.calculateInitSqrtPrice = calculateInitSqrtPrice; exports.calculateTransferFeeExcludedAmount = calculateTransferFeeExcludedAmount; exports.calculateTransferFeeIncludedAmount = calculateTransferFeeIncludedAmount; exports.decimalToQ64 = decimalToQ64; exports.default = index_default; exports.deriveClaimFeeOperatorAddress = deriveClaimFeeOperatorAddress; exports.deriveConfigAddress = deriveConfigAddress; exports.deriveCustomizablePoolAddress = deriveCustomizablePoolAddress; exports.derivePoolAddress = derivePoolAddress; exports.derivePoolAuthority = derivePoolAuthority; exports.derivePositionAddress = derivePositionAddress; exports.derivePositionNftAccount = derivePositionNftAccount; exports.deriveRewardVaultAddress = deriveRewardVaultAddress; exports.deriveTokenBadge = deriveTokenBadge; exports.deriveTokenBadgeAddress = deriveTokenBadgeAddress; exports.deriveTokenVaultAddress = deriveTokenVaultAddress; exports.divCeil = divCeil; exports.getAllUserPositionNftAccount = getAllUserPositionNftAccount; exports.getAmountAFromLiquidityDelta = getAmountAFromLiquidityDelta; exports.getAmountBFromLiquidityDelta = getAmountBFromLiquidityDelta; exports.getAvailableVestingLiquidity = getAvailableVestingLiquidity; exports.getBaseFeeNumerator = getBaseFeeNumerator; exports.getDynamicFeeNumerator = getDynamicFeeNumerator; exports.getEstimatedComputeUnitIxWithBuffer = getEstimatedComputeUnitIxWithBuffer; exports.getEstimatedComputeUnitUsageWithBuffer = getEstimatedComputeUnitUsageWithBuffer; exports.getFeeNumerator = getFeeNumerator; exports.getFirstKey = getFirstKey; exports.getLiquidityDeltaFromAmountA = getLiquidityDeltaFromAmountA; exports.getLiquidityDeltaFromAmountB = getLiquidityDeltaFromAmountB; exports.getMaxAmountWithSlippage = getMaxAmountWithSlippage; exports.getMinAmountWithSlippage = getMinAmountWithSlippage; exports.getNextSqrtPrice = getNextSqrtPrice; exports.getNftOwner = getNftOwner; exports.getOrCreateATAInstruction = getOrCreateATAInstruction; exports.getPriceFromSqrtPrice = getPriceFromSqrtPrice; exports.getPriceImpact = getPriceImpact; exports.getSecondKey = getSecondKey; exports.getSimulationComputeUnits = getSimulationComputeUnits; exports.getSqrtPriceFromPrice = getSqrtPriceFromPrice; exports.getSwapAmount = getSwapAmount; exports.getTokenDecimals = getTokenDecimals; exports.getTokenProgram = getTokenProgram; exports.getTotalLockedLiquidity = getTotalLockedLiquidity; exports.getUnClaimReward = getUnClaimReward; exports.isVestingComplete = isVestingComplete; exports.mulDiv = mulDiv; exports.positionByPoolFilter = positionByPoolFilter; exports.pow = pow; exports.q64ToDecimal = q64ToDecimal; exports.unwrapSOLInstruction = unwrapSOLInstruction; exports.vestingByPositionFilter = vestingByPositionFilter; exports.wrapSOLInstruction = wrapSOLInstruction;
9002
9110
  //# sourceMappingURL=index.js.map