@meteora-ag/cp-amm-sdk 1.0.0-rc.4 → 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.mjs CHANGED
@@ -6616,6 +6616,24 @@ function getAllUserPositionNftAccount(connection, user) {
6616
6616
  return userPositionNftAccount;
6617
6617
  });
6618
6618
  }
6619
+ function getAllPositionNftAccountByOwner(connection, user) {
6620
+ return __async(this, null, function* () {
6621
+ const tokenAccounts = yield connection.getTokenAccountsByOwner(user, {
6622
+ programId: TOKEN_2022_PROGRAM_ID
6623
+ });
6624
+ const userPositionNftAccount = [];
6625
+ for (const { account, pubkey } of tokenAccounts.value) {
6626
+ const tokenAccountData = 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
+ }
6619
6637
 
6620
6638
  // src/helpers/fee.ts
6621
6639
  import { BN as BN5 } from "@coral-xyz/anchor";
@@ -6748,7 +6766,8 @@ function getNextSqrtPrice(amount, sqrtPrice, liquidity, aToB) {
6748
6766
  if (aToB) {
6749
6767
  const product = amount.mul(sqrtPrice);
6750
6768
  const denominator = liquidity.add(product);
6751
- result = mulDiv(liquidity, sqrtPrice, denominator, 0 /* Up */);
6769
+ const numerator = liquidity.mul(sqrtPrice);
6770
+ result = numerator.add(denominator.sub(new BN4(1))).div(denominator);
6752
6771
  } else {
6753
6772
  const quotient = amount.shln(SCALE_OFFSET * 2).div(liquidity);
6754
6773
  result = sqrtPrice.add(quotient);
@@ -7485,6 +7504,34 @@ var CpAmm = class {
7485
7504
  return instructions;
7486
7505
  });
7487
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: TOKEN_2022_PROGRAM_ID2,
7526
+ systemProgram: SystemProgram2.programId
7527
+ }).instruction();
7528
+ return {
7529
+ ix,
7530
+ position,
7531
+ positionNftAccount
7532
+ };
7533
+ });
7534
+ }
7488
7535
  /**
7489
7536
  * Fetches the Config state of the program.
7490
7537
  * @param config - Public key of the config account.
@@ -7588,7 +7635,7 @@ var CpAmm = class {
7588
7635
  */
7589
7636
  getPositionsByUser(user) {
7590
7637
  return __async(this, null, function* () {
7591
- const userPositionAccounts = yield getAllUserPositionNftAccount(
7638
+ const userPositionAccounts = yield getAllPositionNftAccountByOwner(
7592
7639
  this._program.provider.connection,
7593
7640
  user
7594
7641
  );
@@ -7676,6 +7723,32 @@ var CpAmm = class {
7676
7723
  return poolState !== null;
7677
7724
  });
7678
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 min2(liquidityDeltaFromAmountA, liquidityDeltaFromAmountB);
7751
+ }
7679
7752
  /**
7680
7753
  * Calculates swap quote based on input amount and pool state.
7681
7754
  * @param params - Swap parameters including input amount, pool state, slippage, etc.
@@ -8151,20 +8224,8 @@ var CpAmm = class {
8151
8224
  */
8152
8225
  createPosition(params) {
8153
8226
  return __async(this, null, function* () {
8154
- const { owner, payer, pool, positionNft } = params;
8155
- const position = derivePositionAddress(positionNft);
8156
- const positionNftAccount = derivePositionNftAccount(positionNft);
8157
- return yield this._program.methods.createPosition().accountsPartial({
8158
- owner,
8159
- positionNftMint: positionNft,
8160
- poolAuthority: this.poolAuthority,
8161
- positionNftAccount,
8162
- payer,
8163
- pool,
8164
- position,
8165
- tokenProgram: TOKEN_2022_PROGRAM_ID2,
8166
- systemProgram: SystemProgram2.programId
8167
- }).transaction();
8227
+ const { ix } = yield this.buildCreatePositionInstruction(params);
8228
+ return new Transaction().add(ix);
8168
8229
  });
8169
8230
  }
8170
8231
  /**
@@ -8251,6 +8312,104 @@ var CpAmm = class {
8251
8312
  return transaction;
8252
8313
  });
8253
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(NATIVE_MINT2)) {
8354
+ const wrapSOLIx = wrapSOLInstruction(
8355
+ owner,
8356
+ tokenAAccount,
8357
+ BigInt(maxAmountTokenA.toString())
8358
+ );
8359
+ preInstructions.push(...wrapSOLIx);
8360
+ }
8361
+ if (tokenBMint.equals(NATIVE_MINT2)) {
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
+ NATIVE_MINT2.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 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
+ }
8254
8413
  /**
8255
8414
  * Builds a transaction to remove liquidity from a position.
8256
8415
  * @param {RemoveLiquidityParams} params - Parameters for removing liquidity.
@@ -9060,6 +9219,7 @@ export {
9060
9219
  deriveRewardVaultAddress,
9061
9220
  deriveTokenBadgeAddress,
9062
9221
  deriveTokenVaultAddress,
9222
+ getAllPositionNftAccountByOwner,
9063
9223
  getAllUserPositionNftAccount,
9064
9224
  getAmountAFromLiquidityDelta,
9065
9225
  getAmountBFromLiquidityDelta,