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

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
@@ -7439,6 +7439,76 @@ var CpAmm = class {
7439
7439
  ).instruction();
7440
7440
  });
7441
7441
  }
7442
+ /**
7443
+ * Helper function that builds instructions to claim fees, remove liquidity, and close a position
7444
+ * @param {BuildLiquidatePositionInstructionParams} params - Parameters for liquidating a position
7445
+ * @returns {Promise<TransactionInstruction[]>} Array of instructions
7446
+ * @private
7447
+ */
7448
+ buildLiquidatePositionInstruction(params) {
7449
+ return __async(this, null, function* () {
7450
+ const {
7451
+ owner,
7452
+ position,
7453
+ positionNftAccount,
7454
+ positionState,
7455
+ poolState,
7456
+ tokenAAccount,
7457
+ tokenBAccount,
7458
+ tokenAAmountThreshold,
7459
+ tokenBAmountThreshold
7460
+ } = params;
7461
+ const { nftMint: positionNftMint, pool } = positionState;
7462
+ const { tokenAMint, tokenBMint, tokenAVault, tokenBVault } = poolState;
7463
+ const tokenAProgram = getTokenProgram(poolState.tokenAFlag);
7464
+ const tokenBProgram = getTokenProgram(poolState.tokenBFlag);
7465
+ const instructions = [];
7466
+ const claimPositionFeeInstruction = yield this.buildClaimPositionFeeInstruction({
7467
+ owner,
7468
+ poolAuthority: this.poolAuthority,
7469
+ pool,
7470
+ position,
7471
+ positionNftAccount,
7472
+ tokenAAccount,
7473
+ tokenBAccount,
7474
+ tokenAVault,
7475
+ tokenBVault,
7476
+ tokenAMint,
7477
+ tokenBMint,
7478
+ tokenAProgram,
7479
+ tokenBProgram
7480
+ });
7481
+ instructions.push(claimPositionFeeInstruction);
7482
+ const removeAllLiquidityInstruction = yield this.buildRemoveAllLiquidityInstruction({
7483
+ poolAuthority: this.poolAuthority,
7484
+ owner,
7485
+ pool,
7486
+ position,
7487
+ positionNftAccount,
7488
+ tokenAAccount,
7489
+ tokenBAccount,
7490
+ tokenAAmountThreshold,
7491
+ tokenBAmountThreshold,
7492
+ tokenAMint,
7493
+ tokenBMint,
7494
+ tokenAVault,
7495
+ tokenBVault,
7496
+ tokenAProgram,
7497
+ tokenBProgram
7498
+ });
7499
+ instructions.push(removeAllLiquidityInstruction);
7500
+ const closePositionInstruction = yield this.buildClosePositionInstruction({
7501
+ owner,
7502
+ poolAuthority: this.poolAuthority,
7503
+ pool,
7504
+ position,
7505
+ positionNftMint,
7506
+ positionNftAccount
7507
+ });
7508
+ instructions.push(closePositionInstruction);
7509
+ return instructions;
7510
+ });
7511
+ }
7442
7512
  /**
7443
7513
  * Fetches the Config state of the program.
7444
7514
  * @param config - Public key of the config account.
@@ -8592,8 +8662,8 @@ var CpAmm = class {
8592
8662
  vestings,
8593
8663
  currentPoint
8594
8664
  } = params;
8595
- const { nftMint: positionNftMint, pool } = positionState;
8596
- const { tokenAMint, tokenBMint, tokenAVault, tokenBVault } = poolState;
8665
+ const { pool } = positionState;
8666
+ const { tokenAMint, tokenBMint } = poolState;
8597
8667
  const { canUnlock, reason } = this.canUnlockPosition(
8598
8668
  positionState,
8599
8669
  vestings,
@@ -8636,49 +8706,18 @@ var CpAmm = class {
8636
8706
  if (preInstructions.length > 0) {
8637
8707
  transaction.add(...preInstructions);
8638
8708
  }
8639
- const claimPositionFeeInstruction = yield this.buildClaimPositionFeeInstruction({
8640
- owner,
8641
- poolAuthority: this.poolAuthority,
8642
- pool,
8643
- position,
8644
- positionNftAccount,
8645
- tokenAAccount,
8646
- tokenBAccount,
8647
- tokenAVault,
8648
- tokenBVault,
8649
- tokenAMint,
8650
- tokenBMint,
8651
- tokenAProgram,
8652
- tokenBProgram
8653
- });
8654
- transaction.add(claimPositionFeeInstruction);
8655
- const removeAllLiquidityInstruction = yield this.buildRemoveAllLiquidityInstruction({
8656
- poolAuthority: this.poolAuthority,
8709
+ const liquidatePositionInstructions = yield this.buildLiquidatePositionInstruction({
8657
8710
  owner,
8658
- pool,
8659
8711
  position,
8660
8712
  positionNftAccount,
8713
+ positionState,
8714
+ poolState,
8661
8715
  tokenAAccount,
8662
8716
  tokenBAccount,
8663
8717
  tokenAAmountThreshold,
8664
- tokenBAmountThreshold,
8665
- tokenAMint,
8666
- tokenBMint,
8667
- tokenAVault,
8668
- tokenBVault,
8669
- tokenAProgram,
8670
- tokenBProgram
8671
- });
8672
- transaction.add(removeAllLiquidityInstruction);
8673
- const closePositionInstruction = yield this.buildClosePositionInstruction({
8674
- owner,
8675
- poolAuthority: this.poolAuthority,
8676
- pool,
8677
- position,
8678
- positionNftMint,
8679
- positionNftAccount
8718
+ tokenBAmountThreshold
8680
8719
  });
8681
- transaction.add(closePositionInstruction);
8720
+ transaction.add(...liquidatePositionInstructions);
8682
8721
  if (postInstructions.length > 0) {
8683
8722
  transaction.add(...postInstructions);
8684
8723
  }
@@ -8722,7 +8761,6 @@ var CpAmm = class {
8722
8761
  if (!canUnlock) {
8723
8762
  throw new Error(`Cannot remove liquidity: ${reason}`);
8724
8763
  }
8725
- const positionBLiquidityDelta = positionBState.unlockedLiquidity;
8726
8764
  const pool = positionBState.pool;
8727
8765
  const { tokenAMint, tokenBMint, tokenAVault, tokenBVault } = poolState;
8728
8766
  const tokenAProgram = getTokenProgram(poolState.tokenAFlag);
@@ -8745,7 +8783,21 @@ var CpAmm = class {
8745
8783
  const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
8746
8784
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
8747
8785
  }
8786
+ let positionBLiquidityDelta = positionBState.unlockedLiquidity;
8748
8787
  if (positionBVestings.length > 0) {
8788
+ const totalAvailableVestingLiquidity = positionBVestings.reduce(
8789
+ (total, position) => {
8790
+ const available = getAvailableVestingLiquidity(
8791
+ position.vestingState,
8792
+ currentPoint
8793
+ );
8794
+ return total.add(available);
8795
+ },
8796
+ new BN10(0)
8797
+ );
8798
+ positionBLiquidityDelta = positionBLiquidityDelta.add(
8799
+ totalAvailableVestingLiquidity
8800
+ );
8749
8801
  const refreshVestingInstruction = yield this.buildRefreshVestingInstruction({
8750
8802
  owner,
8751
8803
  position: positionB,
@@ -8759,40 +8811,18 @@ var CpAmm = class {
8759
8811
  if (preInstructions.length > 0) {
8760
8812
  transaction.add(...preInstructions);
8761
8813
  }
8762
- const claimPositionFeeInstruction = yield this.buildClaimPositionFeeInstruction({
8814
+ const liquidatePositionInstructions = yield this.buildLiquidatePositionInstruction({
8763
8815
  owner,
8764
- poolAuthority: this.poolAuthority,
8765
- pool,
8766
- position: positionB,
8767
- positionNftAccount: positionBNftAccount,
8768
- tokenAAccount,
8769
- tokenBAccount,
8770
- tokenAVault,
8771
- tokenBVault,
8772
- tokenAMint,
8773
- tokenBMint,
8774
- tokenAProgram,
8775
- tokenBProgram
8776
- });
8777
- transaction.add(claimPositionFeeInstruction);
8778
- const removeAllLiquidityInstruction = yield this.buildRemoveAllLiquidityInstruction({
8779
- poolAuthority: this.poolAuthority,
8780
- owner,
8781
- pool,
8782
8816
  position: positionB,
8783
8817
  positionNftAccount: positionBNftAccount,
8818
+ positionState: positionBState,
8819
+ poolState,
8784
8820
  tokenAAccount,
8785
8821
  tokenBAccount,
8786
8822
  tokenAAmountThreshold: tokenAAmountRemoveLiquidityThreshold,
8787
- tokenBAmountThreshold: tokenBAmountRemoveLiquidityThreshold,
8788
- tokenAMint,
8789
- tokenBMint,
8790
- tokenAVault,
8791
- tokenBVault,
8792
- tokenAProgram,
8793
- tokenBProgram
8823
+ tokenBAmountThreshold: tokenBAmountRemoveLiquidityThreshold
8794
8824
  });
8795
- transaction.add(removeAllLiquidityInstruction);
8825
+ transaction.add(...liquidatePositionInstructions);
8796
8826
  const addLiquidityInstruction = yield this.buildAddLiquidityInstruction({
8797
8827
  pool,
8798
8828
  position: positionA,
@@ -8811,15 +8841,6 @@ var CpAmm = class {
8811
8841
  tokenBAmountThreshold: tokenBAmountAddLiquidityThreshold
8812
8842
  });
8813
8843
  transaction.add(addLiquidityInstruction);
8814
- const closePositionInstruction = yield this.buildClosePositionInstruction({
8815
- owner,
8816
- poolAuthority: this.poolAuthority,
8817
- pool: positionBState.pool,
8818
- position: positionB,
8819
- positionNftMint: positionBState.nftMint,
8820
- positionNftAccount: positionBNftAccount
8821
- });
8822
- transaction.add(closePositionInstruction);
8823
8844
  if (postInstructions.length > 0) {
8824
8845
  transaction.add(...postInstructions);
8825
8846
  }