@meteora-ag/cp-amm-sdk 1.0.0-rc.3 → 1.0.0-rc.5

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
@@ -6401,7 +6401,6 @@ var SCALE_OFFSET = 64;
6401
6401
  var BASIS_POINT_MAX = 1e4;
6402
6402
  var MAX_FEE_NUMERATOR = 5e8;
6403
6403
  var FEE_DENOMINATOR = 1e9;
6404
- var PRECISION = 1e6;
6405
6404
  var MIN_SQRT_PRICE = new (0, _anchor.BN)("4295048016");
6406
6405
  var MAX_SQRT_PRICE = new (0, _anchor.BN)("79226673521066979257578248091");
6407
6406
  var MIN_CU_BUFFER = 5e4;
@@ -6493,12 +6492,6 @@ function derivePositionNftAccount(positionNftMint) {
6493
6492
  CP_AMM_PROGRAM_ID
6494
6493
  )[0];
6495
6494
  }
6496
- function deriveTokenBadge(tokenMint) {
6497
- return _web3js.PublicKey.findProgramAddressSync(
6498
- [Buffer.from("token_badge"), tokenMint.toBuffer()],
6499
- CP_AMM_PROGRAM_ID
6500
- )[0];
6501
- }
6502
6495
 
6503
6496
  // src/helpers/token.ts
6504
6497
  var _bytes = require('@coral-xyz/anchor/dist/cjs/utils/bytes');
@@ -6519,7 +6512,6 @@ var _bytes = require('@coral-xyz/anchor/dist/cjs/utils/bytes');
6519
6512
 
6520
6513
 
6521
6514
 
6522
-
6523
6515
  function getTokenProgram(flag) {
6524
6516
  return flag == 0 ? _spltoken.TOKEN_PROGRAM_ID : _spltoken.TOKEN_2022_PROGRAM_ID;
6525
6517
  }
@@ -6590,16 +6582,6 @@ var unwrapSOLInstruction = (owner, allowOwnerOffCurve = true) => __async(void 0,
6590
6582
  }
6591
6583
  return null;
6592
6584
  });
6593
- function getNftOwner(connection, nftMint) {
6594
- return __async(this, null, function* () {
6595
- const largesTokenAccount = yield connection.getTokenLargestAccounts(nftMint);
6596
- const accountInfo = yield connection.getParsedAccountInfo(
6597
- largesTokenAccount.value[0].address
6598
- );
6599
- const owner = new (0, _web3js.PublicKey)(accountInfo.value.data.parsed.info.owner);
6600
- return new (0, _web3js.PublicKey)(owner);
6601
- });
6602
- }
6603
6585
  function getAllUserPositionNftAccount(connection, user) {
6604
6586
  return __async(this, null, function* () {
6605
6587
  const filters = [
@@ -6634,6 +6616,24 @@ function getAllUserPositionNftAccount(connection, user) {
6634
6616
  return userPositionNftAccount;
6635
6617
  });
6636
6618
  }
6619
+ function getAllPositionNftAccountByOwner(connection, user) {
6620
+ return __async(this, null, function* () {
6621
+ const tokenAccounts = yield connection.getTokenAccountsByOwner(user, {
6622
+ programId: _spltoken.TOKEN_2022_PROGRAM_ID
6623
+ });
6624
+ const userPositionNftAccount = [];
6625
+ for (const { account, pubkey } of tokenAccounts.value) {
6626
+ const tokenAccountData = _spltoken.AccountLayout.decode(account.data);
6627
+ if (tokenAccountData.amount.toString() === "1") {
6628
+ userPositionNftAccount.push({
6629
+ positionNft: tokenAccountData.mint,
6630
+ positionNftAccount: pubkey
6631
+ });
6632
+ }
6633
+ }
6634
+ return userPositionNftAccount;
6635
+ });
6636
+ }
6637
6637
 
6638
6638
  // src/helpers/fee.ts
6639
6639
 
@@ -6752,12 +6752,6 @@ function mulDiv(x, y, denominator, rounding) {
6752
6752
  }
6753
6753
  return div;
6754
6754
  }
6755
- function divCeil(a, b) {
6756
- if (a.isZero()) {
6757
- return new (0, _anchor.BN)(0);
6758
- }
6759
- return a.add(b.sub(new (0, _anchor.BN)(1))).div(b);
6760
- }
6761
6755
  function q64ToDecimal(num, decimalPlaces) {
6762
6756
  return new (0, _decimaljs2.default)(num.toString()).div(_decimaljs2.default.pow(2, 64)).toDecimalPlaces(decimalPlaces);
6763
6757
  }
@@ -6772,7 +6766,8 @@ function getNextSqrtPrice(amount, sqrtPrice, liquidity, aToB) {
6772
6766
  if (aToB) {
6773
6767
  const product = amount.mul(sqrtPrice);
6774
6768
  const denominator = liquidity.add(product);
6775
- result = mulDiv(liquidity, sqrtPrice, denominator, 0 /* Up */);
6769
+ const numerator = liquidity.mul(sqrtPrice);
6770
+ result = numerator.add(denominator.sub(new (0, _anchor.BN)(1))).div(denominator);
6776
6771
  } else {
6777
6772
  const quotient = amount.shln(SCALE_OFFSET * 2).div(liquidity);
6778
6773
  result = sqrtPrice.add(quotient);
@@ -6849,7 +6844,7 @@ function getFeeNumerator(currentPoint, activationPoint, numberOfPeriod, periodFr
6849
6844
  new (0, _anchor.BN)(binStep),
6850
6845
  new (0, _anchor.BN)(variableFeeControl)
6851
6846
  );
6852
- feeNumerator.add(dynamicFeeNumberator);
6847
+ feeNumerator = feeNumerator.add(dynamicFeeNumberator);
6853
6848
  }
6854
6849
  return feeNumerator.gt(new (0, _anchor.BN)(MAX_FEE_NUMERATOR)) ? new (0, _anchor.BN)(MAX_FEE_NUMERATOR) : feeNumerator;
6855
6850
  }
@@ -7164,11 +7159,11 @@ function getAvailableVestingLiquidity(vestingData, currentPoint) {
7164
7159
  numberOfPeriod,
7165
7160
  totalReleasedLiquidity
7166
7161
  } = vestingData;
7167
- if (currentPoint < cliffPoint) {
7162
+ if (currentPoint.lt(cliffPoint)) {
7168
7163
  return new (0, _anchor.BN)(0);
7169
7164
  }
7170
7165
  if (periodFrequency.isZero()) {
7171
- return cliffUnlockLiquidity.sub(totalReleasedLiquidity);
7166
+ return cliffUnlockLiquidity;
7172
7167
  }
7173
7168
  let passedPeriod = new (0, _anchor.BN)(currentPoint).sub(cliffPoint).div(periodFrequency);
7174
7169
  passedPeriod = _bnjs.min.call(void 0, passedPeriod, new (0, _anchor.BN)(numberOfPeriod));
@@ -7246,12 +7241,12 @@ var CpAmm = class {
7246
7241
  getTokenBadgeAccounts(tokenAMint, tokenBMint) {
7247
7242
  return [
7248
7243
  {
7249
- pubkey: deriveTokenBadge(tokenAMint),
7244
+ pubkey: deriveTokenBadgeAddress(tokenAMint),
7250
7245
  isWritable: false,
7251
7246
  isSigner: false
7252
7247
  },
7253
7248
  {
7254
- pubkey: deriveTokenBadge(tokenBMint),
7249
+ pubkey: deriveTokenBadgeAddress(tokenBMint),
7255
7250
  isWritable: false,
7256
7251
  isSigner: false
7257
7252
  }
@@ -7509,6 +7504,34 @@ var CpAmm = class {
7509
7504
  return instructions;
7510
7505
  });
7511
7506
  }
7507
+ /**
7508
+ * Builds a instruction to create a position.
7509
+ * @param {CreatePositionParams} params - Parameters for position creation.
7510
+ * @returns Transaction instruction.
7511
+ */
7512
+ buildCreatePositionInstruction(params) {
7513
+ return __async(this, null, function* () {
7514
+ const { owner, payer, pool, positionNft } = params;
7515
+ const position = derivePositionAddress(positionNft);
7516
+ const positionNftAccount = derivePositionNftAccount(positionNft);
7517
+ const ix = yield this._program.methods.createPosition().accountsPartial({
7518
+ owner,
7519
+ positionNftMint: positionNft,
7520
+ poolAuthority: this.poolAuthority,
7521
+ positionNftAccount,
7522
+ payer,
7523
+ pool,
7524
+ position,
7525
+ tokenProgram: _spltoken.TOKEN_2022_PROGRAM_ID,
7526
+ systemProgram: _web3js.SystemProgram.programId
7527
+ }).instruction();
7528
+ return {
7529
+ ix,
7530
+ position,
7531
+ positionNftAccount
7532
+ };
7533
+ });
7534
+ }
7512
7535
  /**
7513
7536
  * Fetches the Config state of the program.
7514
7537
  * @param config - Public key of the config account.
@@ -7612,7 +7635,7 @@ var CpAmm = class {
7612
7635
  */
7613
7636
  getPositionsByUser(user) {
7614
7637
  return __async(this, null, function* () {
7615
- const userPositionAccounts = yield getAllUserPositionNftAccount(
7638
+ const userPositionAccounts = yield getAllPositionNftAccountByOwner(
7616
7639
  this._program.provider.connection,
7617
7640
  user
7618
7641
  );
@@ -7700,93 +7723,117 @@ var CpAmm = class {
7700
7723
  return poolState !== null;
7701
7724
  });
7702
7725
  }
7726
+ /**
7727
+ * Computes the liquidity delta based on the provided token amounts and sqrt price
7728
+ *
7729
+ * @param {LiquidityDeltaParams} params - The parameters for liquidity calculation
7730
+ * @returns {Promise<BN>} - The computed liquidity delta in Q64 value.
7731
+ */
7732
+ getLiquidityDelta(params) {
7733
+ const {
7734
+ maxAmountTokenA,
7735
+ maxAmountTokenB,
7736
+ sqrtMaxPrice,
7737
+ sqrtMinPrice,
7738
+ sqrtPrice
7739
+ } = params;
7740
+ const liquidityDeltaFromAmountA = getLiquidityDeltaFromAmountA(
7741
+ maxAmountTokenA,
7742
+ sqrtPrice,
7743
+ sqrtMaxPrice
7744
+ );
7745
+ const liquidityDeltaFromAmountB = getLiquidityDeltaFromAmountB(
7746
+ maxAmountTokenB,
7747
+ sqrtMinPrice,
7748
+ sqrtPrice
7749
+ );
7750
+ return _bnjs.min.call(void 0, liquidityDeltaFromAmountA, liquidityDeltaFromAmountB);
7751
+ }
7703
7752
  /**
7704
7753
  * Calculates swap quote based on input amount and pool state.
7705
7754
  * @param params - Swap parameters including input amount, pool state, slippage, etc.
7706
7755
  * @returns Swap quote including expected output amount, fee, and price impact.
7707
7756
  */
7708
7757
  getQuote(params) {
7709
- return __async(this, null, function* () {
7710
- const {
7758
+ const {
7759
+ inAmount,
7760
+ inputTokenMint,
7761
+ slippage,
7762
+ poolState,
7763
+ currentTime,
7764
+ currentSlot,
7765
+ inputTokenInfo,
7766
+ outputTokenInfo
7767
+ } = params;
7768
+ const {
7769
+ sqrtPrice: sqrtPriceQ64,
7770
+ liquidity: liquidityQ64,
7771
+ activationType,
7772
+ activationPoint,
7773
+ collectFeeMode,
7774
+ poolFees
7775
+ } = poolState;
7776
+ const {
7777
+ feeSchedulerMode,
7778
+ cliffFeeNumerator,
7779
+ numberOfPeriod,
7780
+ reductionFactor,
7781
+ periodFrequency
7782
+ } = poolFees.baseFee;
7783
+ const dynamicFee = poolFees.dynamicFee;
7784
+ let actualAmountIn = inAmount;
7785
+ if (inputTokenInfo) {
7786
+ actualAmountIn = calculateTransferFeeExcludedAmount(
7711
7787
  inAmount,
7712
- inputTokenMint,
7713
- slippage,
7714
- poolState,
7715
- currentTime,
7716
- currentSlot,
7717
- inputTokenInfo,
7718
- outputTokenInfo
7719
- } = params;
7720
- const {
7721
- sqrtPrice: sqrtPriceQ64,
7722
- liquidity: liquidityQ64,
7723
- activationType,
7724
- activationPoint,
7725
- collectFeeMode,
7726
- poolFees
7727
- } = poolState;
7728
- const {
7729
- feeSchedulerMode,
7730
- cliffFeeNumerator,
7731
- numberOfPeriod,
7732
- reductionFactor,
7733
- periodFrequency
7734
- } = poolFees.baseFee;
7735
- const dynamicFee = poolFees.dynamicFee;
7736
- let actualAmountIn = inAmount;
7737
- if (inputTokenInfo) {
7738
- actualAmountIn = calculateTransferFeeExcludedAmount(
7739
- inAmount,
7740
- inputTokenInfo.mint,
7741
- inputTokenInfo.currentEpoch
7742
- ).amount;
7743
- }
7744
- const aToB = poolState.tokenAMint.equals(inputTokenMint);
7745
- const currentPoint = activationType ? currentTime : currentSlot;
7746
- let dynamicFeeParams;
7747
- if (dynamicFee.initialized) {
7748
- const { volatilityAccumulator, binStep, variableFeeControl } = dynamicFee;
7749
- dynamicFeeParams = { volatilityAccumulator, binStep, variableFeeControl };
7750
- }
7751
- const tradeFeeNumerator = getFeeNumerator(
7752
- currentPoint,
7753
- activationPoint,
7754
- numberOfPeriod,
7755
- periodFrequency,
7756
- feeSchedulerMode,
7757
- cliffFeeNumerator,
7758
- reductionFactor,
7759
- dynamicFeeParams
7760
- );
7761
- const { amountOut, totalFee } = getSwapAmount(
7762
- actualAmountIn,
7763
- sqrtPriceQ64,
7764
- liquidityQ64,
7765
- tradeFeeNumerator,
7766
- aToB,
7767
- collectFeeMode
7768
- );
7769
- let actualAmountOut = amountOut;
7770
- if (outputTokenInfo) {
7771
- actualAmountOut = calculateTransferFeeExcludedAmount(
7772
- amountOut,
7773
- outputTokenInfo.mint,
7774
- outputTokenInfo.currentEpoch
7775
- ).amount;
7776
- }
7777
- const minSwapOutAmount = getMinAmountWithSlippage(
7778
- actualAmountOut,
7779
- slippage
7780
- );
7781
- return {
7782
- swapInAmount: inAmount,
7783
- consumedInAmount: actualAmountIn,
7784
- swapOutAmount: actualAmountOut,
7785
- minSwapOutAmount,
7786
- totalFee,
7787
- priceImpact: getPriceImpact(minSwapOutAmount, actualAmountOut)
7788
- };
7789
- });
7788
+ inputTokenInfo.mint,
7789
+ inputTokenInfo.currentEpoch
7790
+ ).amount;
7791
+ }
7792
+ const aToB = poolState.tokenAMint.equals(inputTokenMint);
7793
+ const currentPoint = activationType ? currentTime : currentSlot;
7794
+ let dynamicFeeParams;
7795
+ if (dynamicFee.initialized) {
7796
+ const { volatilityAccumulator, binStep, variableFeeControl } = dynamicFee;
7797
+ dynamicFeeParams = { volatilityAccumulator, binStep, variableFeeControl };
7798
+ }
7799
+ const tradeFeeNumerator = getFeeNumerator(
7800
+ currentPoint,
7801
+ activationPoint,
7802
+ numberOfPeriod,
7803
+ periodFrequency,
7804
+ feeSchedulerMode,
7805
+ cliffFeeNumerator,
7806
+ reductionFactor,
7807
+ dynamicFeeParams
7808
+ );
7809
+ const { amountOut, totalFee } = getSwapAmount(
7810
+ actualAmountIn,
7811
+ sqrtPriceQ64,
7812
+ liquidityQ64,
7813
+ tradeFeeNumerator,
7814
+ aToB,
7815
+ collectFeeMode
7816
+ );
7817
+ let actualAmountOut = amountOut;
7818
+ if (outputTokenInfo) {
7819
+ actualAmountOut = calculateTransferFeeExcludedAmount(
7820
+ amountOut,
7821
+ outputTokenInfo.mint,
7822
+ outputTokenInfo.currentEpoch
7823
+ ).amount;
7824
+ }
7825
+ const minSwapOutAmount = getMinAmountWithSlippage(
7826
+ actualAmountOut,
7827
+ slippage
7828
+ );
7829
+ return {
7830
+ swapInAmount: inAmount,
7831
+ consumedInAmount: actualAmountIn,
7832
+ swapOutAmount: actualAmountOut,
7833
+ minSwapOutAmount,
7834
+ totalFee,
7835
+ priceImpact: getPriceImpact(minSwapOutAmount, actualAmountOut)
7836
+ };
7790
7837
  }
7791
7838
  /**
7792
7839
  * Calculates the deposit quote for liquidity pool.
@@ -7799,61 +7846,59 @@ var CpAmm = class {
7799
7846
  * @returns {BN} returns.liquidityDelta - The amount of liquidity that will be added to the pool.
7800
7847
  */
7801
7848
  getDepositQuote(params) {
7802
- return __async(this, null, function* () {
7803
- const {
7849
+ const {
7850
+ inAmount,
7851
+ isTokenA,
7852
+ inputTokenInfo,
7853
+ outputTokenInfo,
7854
+ minSqrtPrice,
7855
+ maxSqrtPrice,
7856
+ sqrtPrice
7857
+ } = params;
7858
+ const actualAmountIn = inputTokenInfo ? inAmount.sub(
7859
+ calculateTransferFeeIncludedAmount(
7804
7860
  inAmount,
7805
- isTokenA,
7806
- inputTokenInfo,
7807
- outputTokenInfo,
7861
+ inputTokenInfo.mint,
7862
+ inputTokenInfo.currentEpoch
7863
+ ).transferFee
7864
+ ) : inAmount;
7865
+ const { liquidityDelta, rawAmount } = isTokenA ? {
7866
+ liquidityDelta: getLiquidityDeltaFromAmountA(
7867
+ actualAmountIn,
7868
+ sqrtPrice,
7869
+ maxSqrtPrice
7870
+ ),
7871
+ rawAmount: (delta) => getAmountBFromLiquidityDelta(
7872
+ delta,
7873
+ sqrtPrice,
7874
+ minSqrtPrice,
7875
+ 0 /* Up */
7876
+ )
7877
+ } : {
7878
+ liquidityDelta: getLiquidityDeltaFromAmountB(
7879
+ actualAmountIn,
7808
7880
  minSqrtPrice,
7809
- maxSqrtPrice,
7810
7881
  sqrtPrice
7811
- } = params;
7812
- const actualAmountIn = inputTokenInfo ? inAmount.sub(
7813
- calculateTransferFeeIncludedAmount(
7814
- inAmount,
7815
- inputTokenInfo.mint,
7816
- inputTokenInfo.currentEpoch
7817
- ).transferFee
7818
- ) : inAmount;
7819
- const { liquidityDelta, rawAmount } = isTokenA ? {
7820
- liquidityDelta: getLiquidityDeltaFromAmountA(
7821
- actualAmountIn,
7822
- sqrtPrice,
7823
- maxSqrtPrice
7824
- ),
7825
- rawAmount: (delta) => getAmountBFromLiquidityDelta(
7826
- delta,
7827
- sqrtPrice,
7828
- minSqrtPrice,
7829
- 0 /* Up */
7830
- )
7831
- } : {
7832
- liquidityDelta: getLiquidityDeltaFromAmountB(
7833
- actualAmountIn,
7834
- minSqrtPrice,
7835
- sqrtPrice
7836
- ),
7837
- rawAmount: (delta) => getAmountAFromLiquidityDelta(
7838
- delta,
7839
- sqrtPrice,
7840
- maxSqrtPrice,
7841
- 0 /* Up */
7842
- )
7843
- };
7844
- const rawOutputAmount = new (0, _anchor.BN)(rawAmount(liquidityDelta));
7845
- const outputAmount = outputTokenInfo ? calculateTransferFeeIncludedAmount(
7846
- rawOutputAmount,
7847
- outputTokenInfo.mint,
7848
- outputTokenInfo.currentEpoch
7849
- ).amount : rawOutputAmount;
7850
- return {
7851
- actualInputAmount: actualAmountIn,
7852
- consumedInputAmount: inAmount,
7853
- liquidityDelta,
7854
- outputAmount
7855
- };
7856
- });
7882
+ ),
7883
+ rawAmount: (delta) => getAmountAFromLiquidityDelta(
7884
+ delta,
7885
+ sqrtPrice,
7886
+ maxSqrtPrice,
7887
+ 0 /* Up */
7888
+ )
7889
+ };
7890
+ const rawOutputAmount = new (0, _anchor.BN)(rawAmount(liquidityDelta));
7891
+ const outputAmount = outputTokenInfo ? calculateTransferFeeIncludedAmount(
7892
+ rawOutputAmount,
7893
+ outputTokenInfo.mint,
7894
+ outputTokenInfo.currentEpoch
7895
+ ).amount : rawOutputAmount;
7896
+ return {
7897
+ actualInputAmount: actualAmountIn,
7898
+ consumedInputAmount: inAmount,
7899
+ liquidityDelta,
7900
+ outputAmount
7901
+ };
7857
7902
  }
7858
7903
  /**
7859
7904
  * Calculates the withdrawal quote for removing liquidity from a concentrated liquidity pool.
@@ -7869,41 +7914,39 @@ var CpAmm = class {
7869
7914
  * @returns {BN} returns.outAmountB - The calculated amount of token B to be received (after deducting transfer fees)
7870
7915
  */
7871
7916
  getWithdrawQuote(params) {
7872
- return __async(this, null, function* () {
7873
- const {
7874
- liquidityDelta,
7875
- sqrtPrice,
7876
- maxSqrtPrice,
7877
- minSqrtPrice,
7878
- tokenATokenInfo,
7879
- tokenBTokenInfo
7880
- } = params;
7881
- const amountA = getAmountAFromLiquidityDelta(
7882
- liquidityDelta,
7883
- sqrtPrice,
7884
- maxSqrtPrice,
7885
- 1 /* Down */
7886
- );
7887
- const amountB = getAmountBFromLiquidityDelta(
7888
- liquidityDelta,
7889
- sqrtPrice,
7890
- minSqrtPrice,
7891
- 1 /* Down */
7892
- );
7893
- return {
7894
- liquidityDelta,
7895
- outAmountA: tokenATokenInfo ? calculateTransferFeeExcludedAmount(
7896
- amountA,
7897
- tokenATokenInfo.mint,
7898
- tokenATokenInfo.currentEpoch
7899
- ).amount : amountA,
7900
- outAmountB: tokenBTokenInfo ? calculateTransferFeeExcludedAmount(
7901
- amountB,
7902
- tokenBTokenInfo.mint,
7903
- tokenBTokenInfo.currentEpoch
7904
- ).amount : amountB
7905
- };
7906
- });
7917
+ const {
7918
+ liquidityDelta,
7919
+ sqrtPrice,
7920
+ maxSqrtPrice,
7921
+ minSqrtPrice,
7922
+ tokenATokenInfo,
7923
+ tokenBTokenInfo
7924
+ } = params;
7925
+ const amountA = getAmountAFromLiquidityDelta(
7926
+ liquidityDelta,
7927
+ sqrtPrice,
7928
+ maxSqrtPrice,
7929
+ 1 /* Down */
7930
+ );
7931
+ const amountB = getAmountBFromLiquidityDelta(
7932
+ liquidityDelta,
7933
+ sqrtPrice,
7934
+ minSqrtPrice,
7935
+ 1 /* Down */
7936
+ );
7937
+ return {
7938
+ liquidityDelta,
7939
+ outAmountA: tokenATokenInfo ? calculateTransferFeeExcludedAmount(
7940
+ amountA,
7941
+ tokenATokenInfo.mint,
7942
+ tokenATokenInfo.currentEpoch
7943
+ ).amount : amountA,
7944
+ outAmountB: tokenBTokenInfo ? calculateTransferFeeExcludedAmount(
7945
+ amountB,
7946
+ tokenBTokenInfo.mint,
7947
+ tokenBTokenInfo.currentEpoch
7948
+ ).amount : amountB
7949
+ };
7907
7950
  }
7908
7951
  /**
7909
7952
  * Calculates liquidity and corresponding token amounts for token A single-sided pool creation
@@ -8181,20 +8224,8 @@ var CpAmm = class {
8181
8224
  */
8182
8225
  createPosition(params) {
8183
8226
  return __async(this, null, function* () {
8184
- const { owner, payer, pool, positionNft } = params;
8185
- const position = derivePositionAddress(positionNft);
8186
- const positionNftAccount = derivePositionNftAccount(positionNft);
8187
- return yield this._program.methods.createPosition().accountsPartial({
8188
- owner,
8189
- positionNftMint: positionNft,
8190
- poolAuthority: this.poolAuthority,
8191
- positionNftAccount,
8192
- payer,
8193
- pool,
8194
- position,
8195
- tokenProgram: _spltoken.TOKEN_2022_PROGRAM_ID,
8196
- systemProgram: _web3js.SystemProgram.programId
8197
- }).transaction();
8227
+ const { ix } = yield this.buildCreatePositionInstruction(params);
8228
+ return new (0, _web3js.Transaction)().add(ix);
8198
8229
  });
8199
8230
  }
8200
8231
  /**
@@ -8281,6 +8312,104 @@ var CpAmm = class {
8281
8312
  return transaction;
8282
8313
  });
8283
8314
  }
8315
+ /**
8316
+ * Creates a new position and add liquidity to position it in a single transaction.
8317
+ * Handles both native SOL and other tokens, automatically wrapping/unwrapping SOL as needed.
8318
+ *
8319
+ * @param {CreatePositionAndAddLiquidity} params - Parameters for creating position and adding liquidity
8320
+ *
8321
+ * @returns {Transaction} A transaction that creates a position and adds liquidity
8322
+ *
8323
+ **/
8324
+ createPositionAndAddLiquidity(params) {
8325
+ return __async(this, null, function* () {
8326
+ const {
8327
+ owner,
8328
+ pool,
8329
+ positionNft,
8330
+ liquidityDelta,
8331
+ maxAmountTokenA,
8332
+ maxAmountTokenB,
8333
+ tokenAAmountThreshold,
8334
+ tokenBAmountThreshold,
8335
+ tokenAMint,
8336
+ tokenBMint,
8337
+ tokenAVault,
8338
+ tokenBVault,
8339
+ tokenAProgram,
8340
+ tokenBProgram
8341
+ } = params;
8342
+ const {
8343
+ tokenAAta: tokenAAccount,
8344
+ tokenBAta: tokenBAccount,
8345
+ instructions: preInstructions
8346
+ } = yield this.prepareTokenAccounts(
8347
+ owner,
8348
+ tokenAMint,
8349
+ tokenBMint,
8350
+ tokenAProgram,
8351
+ tokenBProgram
8352
+ );
8353
+ if (tokenAMint.equals(_spltoken.NATIVE_MINT)) {
8354
+ const wrapSOLIx = wrapSOLInstruction(
8355
+ owner,
8356
+ tokenAAccount,
8357
+ BigInt(maxAmountTokenA.toString())
8358
+ );
8359
+ preInstructions.push(...wrapSOLIx);
8360
+ }
8361
+ if (tokenBMint.equals(_spltoken.NATIVE_MINT)) {
8362
+ const wrapSOLIx = wrapSOLInstruction(
8363
+ owner,
8364
+ tokenBAccount,
8365
+ BigInt(maxAmountTokenB.toString())
8366
+ );
8367
+ preInstructions.push(...wrapSOLIx);
8368
+ }
8369
+ const postInstructions = [];
8370
+ if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
8371
+ _spltoken.NATIVE_MINT.toBase58()
8372
+ )) {
8373
+ const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
8374
+ closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
8375
+ }
8376
+ const {
8377
+ ix: createPositionIx,
8378
+ position,
8379
+ positionNftAccount
8380
+ } = yield this.buildCreatePositionInstruction({
8381
+ owner,
8382
+ payer: owner,
8383
+ pool,
8384
+ positionNft
8385
+ });
8386
+ const addLiquidityInstruction = yield this.buildAddLiquidityInstruction({
8387
+ pool,
8388
+ position,
8389
+ positionNftAccount,
8390
+ owner,
8391
+ tokenAAccount,
8392
+ tokenBAccount,
8393
+ tokenAMint,
8394
+ tokenBMint,
8395
+ tokenAVault,
8396
+ tokenBVault,
8397
+ tokenAProgram,
8398
+ tokenBProgram,
8399
+ liquidityDelta,
8400
+ tokenAAmountThreshold,
8401
+ tokenBAmountThreshold
8402
+ });
8403
+ const transaction = new (0, _web3js.Transaction)();
8404
+ transaction.add(createPositionIx);
8405
+ transaction.add(
8406
+ ...preInstructions.length > 0 ? preInstructions : [],
8407
+ addLiquidityInstruction,
8408
+ ...postInstructions.length > 0 ? postInstructions : []
8409
+ );
8410
+ return transaction;
8411
+ });
8412
+ }
8284
8413
  /**
8285
8414
  * Builds a transaction to remove liquidity from a position.
8286
8415
  * @param {RemoveLiquidityParams} params - Parameters for removing liquidity.
@@ -9125,8 +9254,5 @@ var index_default = cp_amm_default;
9125
9254
 
9126
9255
 
9127
9256
 
9128
-
9129
-
9130
-
9131
- 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;
9257
+ 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.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.deriveTokenBadgeAddress = deriveTokenBadgeAddress; exports.deriveTokenVaultAddress = deriveTokenVaultAddress; exports.getAllPositionNftAccountByOwner = getAllPositionNftAccountByOwner; 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.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;
9132
9258
  //# sourceMappingURL=index.js.map