@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.mjs CHANGED
@@ -21,7 +21,7 @@ var __async = (__this, __arguments, generator) => {
21
21
  };
22
22
 
23
23
  // src/CpAmm.ts
24
- import { Program, BN as BN9 } from "@coral-xyz/anchor";
24
+ import { Program, BN as BN10 } from "@coral-xyz/anchor";
25
25
  import { NATIVE_MINT as NATIVE_MINT2, TOKEN_2022_PROGRAM_ID as TOKEN_2022_PROGRAM_ID2 } from "@solana/spl-token";
26
26
  import invariant from "invariant";
27
27
 
@@ -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
  import { BN as BN3 } from "@coral-xyz/anchor";
6747
6747
  import Decimal from "decimal.js";
6748
- function mulShr(x, y, offset, rounding) {
6749
- const denominator = new BN3(1).shln(offset);
6750
- return mulDiv(x, y, denominator, rounding);
6751
- }
6752
- function shlDiv(x, y, offset, rounding) {
6753
- const scale = new BN3(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
  import { BN as BN4 } from "@coral-xyz/anchor";
6778
- import Decimal2 from "decimal.js";
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 BN4(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 Decimal2(
6812
- maxAmountA.mul(upperSqrtPrice.mul(lowerSqrtPrice)).toString()
6813
- );
6814
- const delta = new Decimal2(upperSqrtPrice.sub(lowerSqrtPrice).toString());
6815
- return new BN4(prod.div(delta).floor().toFixed());
6816
- }
6817
- function getLiquidityDeltaFromAmountB(maxAmountB, lowerSqrtPrice, upperSqrtPrice) {
6818
- const denominator = new Decimal2(
6819
- upperSqrtPrice.sub(lowerSqrtPrice).toString()
6820
- );
6821
- const prod = new Decimal2(maxAmountB.toString()).mul(
6822
- Decimal2.pow(2, SCALE_OFFSET * 2)
6823
- );
6824
- return new BN4(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 Decimal2(liquidity.toString()).mul(
6828
- new Decimal2(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(Decimal2.pow(2, 64)).div(new Decimal2(denominator.toString()));
6832
6795
  if (rounding == 0 /* Up */) {
6833
- return result.div(Decimal2.pow(2, 64)).ceil().toFixed();
6796
+ return product.add(denominator.sub(new BN4(1))).div(denominator);
6834
6797
  }
6835
- return result.div(Decimal2.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 Decimal2(liquidity.toString()).mul(
6840
- new Decimal2(delta.toString())
6841
- );
6801
+ const one = new BN4(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(Decimal2.pow(2, 128)).ceil().toFixed();
6805
+ return result.add(one.sub(new BN4(1))).div(one);
6844
6806
  }
6845
- return prod.div(Decimal2.pow(2, 128)).floor().toFixed();
6807
+ return result.shrn(128);
6846
6808
  }
6847
6809
 
6848
6810
  // src/helpers/fee.ts
6849
- import Decimal3 from "decimal.js";
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 Decimal3(
6864
- volatilityAccumulator.toString()
6865
- ).div(Decimal3.pow(2, 64));
6866
- const squareVfaBin = volatilityAccumulatorDecimal.mul(new Decimal3(binStep.toString())).pow(2);
6867
- const vFee = squareVfaBin.mul(new Decimal3(variableFeeControl.toString()));
6868
- return new BN5(
6869
- vFee.add(99999999999).div(1e11).floor().toFixed()
6870
- );
6824
+ if (variableFeeControl.isZero()) {
6825
+ return new BN5(0);
6826
+ }
6827
+ const squareVfaBin = volatilityAccumulator.mul(new BN5(binStep)).pow(new BN5(2));
6828
+ const vFee = variableFeeControl.mul(squareVfaBin);
6829
+ return vFee.add(new BN5(99999999999)).div(new BN5(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,
@@ -7010,7 +6969,7 @@ var getEstimatedComputeUnitIxWithBuffer = (connection, instructions, feePayer, b
7010
6969
 
7011
6970
  // src/helpers/utils.ts
7012
6971
  import { BN as BN6 } from "@coral-xyz/anchor";
7013
- import Decimal4 from "decimal.js";
6972
+ import Decimal2 from "decimal.js";
7014
6973
  var getMaxAmountWithSlippage = (amount, rate) => {
7015
6974
  const slippage = (100 + rate) / 100 * BASIS_POINT_MAX;
7016
6975
  return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
@@ -7021,20 +6980,20 @@ var getMinAmountWithSlippage = (amount, rate) => {
7021
6980
  };
7022
6981
  var getPriceImpact = (actualAmount, idealAmount) => {
7023
6982
  const diff = idealAmount.sub(actualAmount);
7024
- return new Decimal4(diff.toString()).div(new Decimal4(idealAmount.toString())).mul(100).toNumber();
6983
+ return new Decimal2(diff.toString()).div(new Decimal2(idealAmount.toString())).mul(100).toNumber();
7025
6984
  };
7026
6985
  var getPriceFromSqrtPrice = (sqrtPrice, tokenADecimal, tokenBDecimal) => {
7027
- const decimalSqrtPrice = new Decimal4(sqrtPrice.toString());
7028
- const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new Decimal4(__pow(10, tokenADecimal - tokenBDecimal))).div(Decimal4.pow(2, 128)).toString();
6986
+ const decimalSqrtPrice = new Decimal2(sqrtPrice.toString());
6987
+ const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new Decimal2(__pow(10, tokenADecimal - tokenBDecimal))).div(Decimal2.pow(2, 128)).toString();
7029
6988
  return price;
7030
6989
  };
7031
6990
  var getSqrtPriceFromPrice = (price, tokenADecimal, tokenBDecimal) => {
7032
- const decimalPrice = new Decimal4(price);
6991
+ const decimalPrice = new Decimal2(price);
7033
6992
  const adjustedByDecimals = decimalPrice.div(
7034
- new Decimal4(__pow(10, tokenADecimal - tokenBDecimal))
6993
+ new Decimal2(__pow(10, tokenADecimal - tokenBDecimal))
7035
6994
  );
7036
- const sqrtValue = Decimal4.sqrt(adjustedByDecimals);
7037
- const sqrtValueQ64 = sqrtValue.mul(Decimal4.pow(2, 64));
6995
+ const sqrtValue = Decimal2.sqrt(adjustedByDecimals);
6996
+ const sqrtValueQ64 = sqrtValue.mul(Decimal2.pow(2, 64));
7038
6997
  return new BN6(sqrtValueQ64.floor().toFixed());
7039
6998
  };
7040
6999
  var getUnClaimReward = (poolState, positionState) => {
@@ -7074,28 +7033,28 @@ var vestingByPositionFilter = (position) => {
7074
7033
 
7075
7034
  // src/helpers/priceMath.ts
7076
7035
  import { BN as BN7 } from "@coral-xyz/anchor";
7077
- import Decimal5 from "decimal.js";
7036
+ import Decimal3 from "decimal.js";
7078
7037
  function calculateInitSqrtPrice(tokenAAmount, tokenBAmount, minSqrtPrice, maxSqrtPrice) {
7079
7038
  if (tokenAAmount.isZero() || tokenBAmount.isZero()) {
7080
7039
  throw new Error("Amount cannot be zero");
7081
7040
  }
7082
- const amountADecimal = new Decimal5(tokenAAmount.toString());
7083
- const amountBDecimal = new Decimal5(tokenBAmount.toString());
7084
- const minSqrtPriceDecimal = new Decimal5(minSqrtPrice.toString()).div(
7085
- Decimal5.pow(2, 64)
7041
+ const amountADecimal = new Decimal3(tokenAAmount.toString());
7042
+ const amountBDecimal = new Decimal3(tokenBAmount.toString());
7043
+ const minSqrtPriceDecimal = new Decimal3(minSqrtPrice.toString()).div(
7044
+ Decimal3.pow(2, 64)
7086
7045
  );
7087
- const maxSqrtPriceDecimal = new Decimal5(maxSqrtPrice.toString()).div(
7088
- Decimal5.pow(2, 64)
7046
+ const maxSqrtPriceDecimal = new Decimal3(maxSqrtPrice.toString()).div(
7047
+ Decimal3.pow(2, 64)
7089
7048
  );
7090
- const x = new Decimal5(1).div(maxSqrtPriceDecimal);
7049
+ const x = new Decimal3(1).div(maxSqrtPriceDecimal);
7091
7050
  const y = amountBDecimal.div(amountADecimal);
7092
7051
  const xy = x.mul(y);
7093
7052
  const paMinusXY = minSqrtPriceDecimal.sub(xy);
7094
7053
  const xyMinusPa = xy.sub(minSqrtPriceDecimal);
7095
- const fourY = new Decimal5(4).mul(y);
7054
+ const fourY = new Decimal3(4).mul(y);
7096
7055
  const discriminant = xyMinusPa.mul(xyMinusPa).add(fourY);
7097
7056
  const sqrtDiscriminant = discriminant.sqrt();
7098
- const result = paMinusXY.add(sqrtDiscriminant).div(new Decimal5(2)).mul(Decimal5.pow(2, 64));
7057
+ const result = paMinusXY.add(sqrtDiscriminant).div(new Decimal3(2)).mul(Decimal3.pow(2, 64));
7099
7058
  return new BN7(result.floor().toFixed());
7100
7059
  }
7101
7060
 
@@ -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
+ import { BN as BN9 } from "@coral-xyz/anchor";
7185
7145
  import { min } from "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 BN9(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 BN9(0);
7169
+ }
7170
+ if (periodFrequency.isZero()) {
7171
+ return cliffUnlockLiquidity.sub(totalReleasedLiquidity);
7172
+ }
7173
+ let passedPeriod = new BN9(currentPoint).sub(cliffPoint).div(periodFrequency);
7174
+ passedPeriod = min(passedPeriod, new BN9(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
+ import { min as min2 } from "bn.js";
7186
7186
  var CpAmm = class {
7187
7187
  constructor(connection) {
7188
7188
  this._program = new 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
  }
@@ -7703,7 +7771,7 @@ var CpAmm = class {
7703
7771
  0 /* Up */
7704
7772
  )
7705
7773
  };
7706
- const rawOutputAmount = new BN9(rawAmount(liquidityDelta));
7774
+ const rawOutputAmount = new BN10(rawAmount(liquidityDelta));
7707
7775
  const outputAmount = outputTokenInfo ? calculateTransferFeeIncludedAmount(
7708
7776
  rawOutputAmount,
7709
7777
  outputTokenInfo.mint,
@@ -7755,15 +7823,15 @@ var CpAmm = class {
7755
7823
  return {
7756
7824
  liquidityDelta,
7757
7825
  outAmountA: tokenATokenInfo ? calculateTransferFeeExcludedAmount(
7758
- new BN9(amountA),
7826
+ amountA,
7759
7827
  tokenATokenInfo.mint,
7760
7828
  tokenATokenInfo.currentEpoch
7761
- ).amount : new BN9(amountA),
7829
+ ).amount : amountA,
7762
7830
  outAmountB: tokenBTokenInfo ? calculateTransferFeeExcludedAmount(
7763
- new BN9(amountB),
7831
+ amountB,
7764
7832
  tokenBTokenInfo.mint,
7765
7833
  tokenBTokenInfo.currentEpoch
7766
- ).amount : new BN9(amountB)
7834
+ ).amount : amountB
7767
7835
  };
7768
7836
  });
7769
7837
  }
@@ -7813,7 +7881,7 @@ var CpAmm = class {
7813
7881
  tokenAInfo,
7814
7882
  tokenBInfo
7815
7883
  } = params;
7816
- if (tokenAAmount.eq(new BN9(0)) && tokenBAmount.eq(new BN9(0))) {
7884
+ if (tokenAAmount.eq(new BN10(0)) && tokenBAmount.eq(new BN10(0))) {
7817
7885
  throw new Error("Invalid input amount");
7818
7886
  }
7819
7887
  const actualAmountAIn = tokenAInfo ? tokenAAmount.sub(
@@ -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,
@@ -7846,7 +7914,7 @@ var CpAmm = class {
7846
7914
  minSqrtPrice,
7847
7915
  initSqrtPrice
7848
7916
  );
7849
- const liquidityDelta = min(
7917
+ const liquidityDelta = min2(
7850
7918
  liquidityDeltaFromAmountA,
7851
7919
  liquidityDeltaFromAmountB
7852
7920
  );
@@ -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 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 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 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
  });
@@ -8965,9 +9074,8 @@ export {
8965
9074
  getAllUserPositionNftAccount,
8966
9075
  getAmountAFromLiquidityDelta,
8967
9076
  getAmountBFromLiquidityDelta,
9077
+ getAvailableVestingLiquidity,
8968
9078
  getBaseFeeNumerator,
8969
- getDeltaAmountA,
8970
- getDeltaAmountB,
8971
9079
  getDynamicFeeNumerator,
8972
9080
  getEstimatedComputeUnitIxWithBuffer,
8973
9081
  getEstimatedComputeUnitUsageWithBuffer,
@@ -8988,13 +9096,13 @@ export {
8988
9096
  getSwapAmount,
8989
9097
  getTokenDecimals,
8990
9098
  getTokenProgram,
9099
+ getTotalLockedLiquidity,
8991
9100
  getUnClaimReward,
9101
+ isVestingComplete,
8992
9102
  mulDiv,
8993
- mulShr,
8994
9103
  positionByPoolFilter,
8995
9104
  pow,
8996
9105
  q64ToDecimal,
8997
- shlDiv,
8998
9106
  unwrapSOLInstruction,
8999
9107
  vestingByPositionFilter,
9000
9108
  wrapSOLInstruction