@meteora-ag/cp-amm-sdk 1.2.2 → 1.2.4

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
@@ -7952,7 +7952,7 @@ var getSqrtPriceFromPrice = (price, tokenADecimal, tokenBDecimal) => {
7952
7952
  const sqrtValueQ64 = sqrtValue.mul(Decimal.pow(2, 64));
7953
7953
  return new BN2(sqrtValueQ64.floor().toFixed());
7954
7954
  };
7955
- var getUnClaimReward = (poolState, positionState) => {
7955
+ var getUnClaimLpFee = (poolState, positionState) => {
7956
7956
  const totalPositionLiquidity = positionState.unlockedLiquidity.add(positionState.vestedLiquidity).add(positionState.permanentLockedLiquidity);
7957
7957
  const feeAPerTokenStored = new BN2(
7958
7958
  Buffer.from(poolState.feeAPerLiquidity).reverse()
@@ -7968,8 +7968,103 @@ var getUnClaimReward = (poolState, positionState) => {
7968
7968
  rewards: positionState.rewardInfos.length > 0 ? positionState.rewardInfos.map((item) => item.rewardPendings) : []
7969
7969
  };
7970
7970
  };
7971
+ function getRewardPerTokenStore(poolReward, poolLiquidity, currentTime) {
7972
+ if (poolLiquidity.eq(new BN2(0))) {
7973
+ return new BN2(0);
7974
+ }
7975
+ const lastTimeRewardApplicable = BN2.min(
7976
+ currentTime,
7977
+ poolReward.rewardDurationEnd
7978
+ );
7979
+ const timePeriod = lastTimeRewardApplicable.sub(poolReward.lastUpdateTime);
7980
+ const currentTotalReward = timePeriod.mul(poolReward.rewardRate);
7981
+ const rewardPerTokenStore = currentTotalReward.shln(128).div(poolLiquidity);
7982
+ const totalRewardPerTokenStore = new BN2(
7983
+ Buffer.from(poolReward.rewardPerTokenStored).reverse()
7984
+ ).add(rewardPerTokenStore);
7985
+ return totalRewardPerTokenStore;
7986
+ }
7987
+ function getRewardPerPeriod(poolReward, currentTime, periodTime) {
7988
+ const timeRewardAppicable = currentTime.add(periodTime);
7989
+ const period = timeRewardAppicable <= poolReward.rewardDurationEnd ? periodTime : poolReward.rewardDurationEnd.sub(currentTime);
7990
+ const rewardPerPeriod = poolReward.rewardRate.mul(period);
7991
+ return rewardPerPeriod;
7992
+ }
7993
+ function getRewardInfo(poolState, rewardIndex, periodTime, currentTime) {
7994
+ const poolReward = poolState.rewardInfos[rewardIndex];
7995
+ const rewardPerTokenStore = getRewardPerTokenStore(
7996
+ poolReward,
7997
+ poolState.liquidity,
7998
+ currentTime
7999
+ );
8000
+ const totalRewardDistributed = rewardPerTokenStore.mul(poolState.liquidity).shrn(192);
8001
+ if (poolReward.rewardDurationEnd <= currentTime) {
8002
+ return {
8003
+ rewardPerPeriod: new BN2(0),
8004
+ rewardBalance: new BN2(0),
8005
+ totalRewardDistributed
8006
+ };
8007
+ }
8008
+ const rewardPerPeriod = getRewardPerPeriod(
8009
+ poolReward,
8010
+ currentTime,
8011
+ periodTime
8012
+ );
8013
+ const remainTime = poolReward.rewardDurationEnd.sub(currentTime);
8014
+ const rewardBalance = poolReward.rewardRate.mul(remainTime).shrn(64);
8015
+ if (poolState.liquidity.eq(new BN2(0))) {
8016
+ return {
8017
+ rewardPerPeriod,
8018
+ rewardBalance,
8019
+ totalRewardDistributed: new BN2(0)
8020
+ };
8021
+ }
8022
+ return {
8023
+ rewardPerPeriod: rewardPerPeriod.shrn(64),
8024
+ rewardBalance,
8025
+ totalRewardDistributed
8026
+ };
8027
+ }
8028
+ function getUserRewardPending(poolState, positionState, rewardIndex, currentTime, periodTime) {
8029
+ if (poolState.liquidity.eq(new BN2(0))) {
8030
+ return {
8031
+ userRewardPerPeriod: new BN2(0),
8032
+ userPendingReward: new BN2(0)
8033
+ };
8034
+ }
8035
+ const poolReward = poolState.rewardInfos[rewardIndex];
8036
+ const userRewardInfo = positionState.rewardInfos[rewardIndex];
8037
+ const rewardPerTokenStore = getRewardPerTokenStore(
8038
+ poolReward,
8039
+ poolState.liquidity,
8040
+ currentTime
8041
+ );
8042
+ const totalPositionLiquidity = positionState.unlockedLiquidity.add(positionState.vestedLiquidity).add(positionState.permanentLockedLiquidity);
8043
+ const userRewardPerTokenCheckPoint = new BN2(
8044
+ Buffer.from(userRewardInfo.rewardPerTokenCheckpoint).reverse()
8045
+ );
8046
+ const newReward = totalPositionLiquidity.mul(rewardPerTokenStore.sub(userRewardPerTokenCheckPoint)).shrn(192);
8047
+ if (poolReward.rewardDurationEnd <= currentTime) {
8048
+ return {
8049
+ userPendingReward: userRewardInfo.rewardPendings.add(newReward),
8050
+ userRewardPerPeriod: new BN2(0)
8051
+ };
8052
+ }
8053
+ const rewardPerPeriod = getRewardPerPeriod(
8054
+ poolReward,
8055
+ currentTime,
8056
+ periodTime
8057
+ );
8058
+ const rewardPerTokenStorePerPeriod = rewardPerPeriod.shln(128).div(poolState.liquidity);
8059
+ const userRewardPerPeriod = totalPositionLiquidity.mul(rewardPerTokenStorePerPeriod).shrn(192);
8060
+ return {
8061
+ userPendingReward: userRewardInfo.rewardPendings.add(newReward),
8062
+ userRewardPerPeriod
8063
+ };
8064
+ }
7971
8065
 
7972
8066
  // src/helpers/accountFilters.ts
8067
+ import { PublicKey as PublicKey5 } from "@solana/web3.js";
7973
8068
  var positionByPoolFilter = (pool) => {
7974
8069
  return {
7975
8070
  memcmp: {
@@ -7986,6 +8081,18 @@ var vestingByPositionFilter = (position) => {
7986
8081
  }
7987
8082
  };
7988
8083
  };
8084
+ function offsetBasedFilter(value, offset) {
8085
+ const valueKey = typeof value === "string" ? new PublicKey5(value) : value;
8086
+ return [
8087
+ {
8088
+ memcmp: {
8089
+ offset,
8090
+ bytes: valueKey.toBase58(),
8091
+ encoding: "base58"
8092
+ }
8093
+ }
8094
+ ];
8095
+ }
7989
8096
 
7990
8097
  // src/helpers/token2022.ts
7991
8098
  import { BN as BN3 } from "@coral-xyz/anchor";
@@ -9714,11 +9821,11 @@ function validateFeeFraction(numerator, denominator) {
9714
9821
  }
9715
9822
 
9716
9823
  // src/helpers/common.ts
9717
- import { PublicKey as PublicKey5 } from "@solana/web3.js";
9824
+ import { PublicKey as PublicKey6 } from "@solana/web3.js";
9718
9825
  import BN15 from "bn.js";
9719
9826
  import Decimal4 from "decimal.js";
9720
9827
  function hasPartner(poolState) {
9721
- return !poolState.partner.equals(PublicKey5.default);
9828
+ return !poolState.partner.equals(PublicKey6.default);
9722
9829
  }
9723
9830
  function getCurrentPoint(connection, activationType) {
9724
9831
  return __async(this, null, function* () {
@@ -10457,6 +10564,18 @@ var CpAmm = class {
10457
10564
  return poolState;
10458
10565
  });
10459
10566
  }
10567
+ /**
10568
+ * Fetches all Pool states by tokenAMint.
10569
+ * @param tokenAMint - Public key of the tokenA mint.
10570
+ * @returns Array of matched pool accounts and their state.
10571
+ */
10572
+ fetchPoolStatesByTokenAMint(tokenAMint) {
10573
+ return __async(this, null, function* () {
10574
+ const filters = offsetBasedFilter(tokenAMint, 168);
10575
+ const pools = yield this._program.account.pool.all(filters);
10576
+ return pools;
10577
+ });
10578
+ }
10460
10579
  /**
10461
10580
  * Fetches the Position state.
10462
10581
  * @param position - Public key of the position.
@@ -10564,6 +10683,11 @@ var CpAmm = class {
10564
10683
  return positionResult;
10565
10684
  });
10566
10685
  }
10686
+ /**
10687
+ * Retrieves all vesting accounts associated with a position.
10688
+ * @param position - Public key of the position.
10689
+ * @returns Array of vesting account public keys and their states.
10690
+ */
10567
10691
  getAllVestingsByPosition(position) {
10568
10692
  return __async(this, null, function* () {
10569
10693
  const vestings = yield this._program.account.vesting.all([
@@ -10572,12 +10696,22 @@ var CpAmm = class {
10572
10696
  return vestings;
10573
10697
  });
10574
10698
  }
10699
+ /**
10700
+ * Checks if a position has any locked liquidity.
10701
+ * @param position - Position state.
10702
+ * @returns True if the position has locked liquidity, false otherwise.
10703
+ */
10575
10704
  isLockedPosition(position) {
10576
10705
  const totalLockedLiquidity = position.vestedLiquidity.add(
10577
10706
  position.permanentLockedLiquidity
10578
10707
  );
10579
10708
  return totalLockedLiquidity.gtn(0);
10580
10709
  }
10710
+ /**
10711
+ * Checks if a position is permanently locked.
10712
+ * @param positionState - Position state.
10713
+ * @returns True if the position is permanently locked, false otherwise.
10714
+ */
10581
10715
  isPermanentLockedPosition(positionState) {
10582
10716
  return positionState.permanentLockedLiquidity.gtn(0);
10583
10717
  }
@@ -10616,6 +10750,11 @@ var CpAmm = class {
10616
10750
  }
10617
10751
  return { canUnlock: true };
10618
10752
  }
10753
+ /**
10754
+ * Checks if a pool exists.
10755
+ * @param pool - Public key of the pool.
10756
+ * @returns True if the pool exists, false otherwise.
10757
+ */
10619
10758
  isPoolExist(pool) {
10620
10759
  return __async(this, null, function* () {
10621
10760
  const poolState = yield this._program.account.pool.fetchNullable(pool);
@@ -10691,6 +10830,11 @@ var CpAmm = class {
10691
10830
  priceImpact: swapResult.priceImpact
10692
10831
  };
10693
10832
  }
10833
+ /**
10834
+ * Calculates the expected output amount or input amount for a swap depending on the swap mode.
10835
+ * @param params GetQuote2Params
10836
+ * @returns Quote2Result
10837
+ */
10694
10838
  getQuote2(params) {
10695
10839
  const {
10696
10840
  inputTokenMint,
@@ -11130,6 +11274,11 @@ var CpAmm = class {
11130
11274
  return { tx: transaction, pool, position };
11131
11275
  });
11132
11276
  }
11277
+ /**
11278
+ * Builds a transaction to create a customizable pool with dynamic config.
11279
+ * @param params InitializeCustomizeablePoolWithDynamicConfigParams
11280
+ * @returns Transaction and related addresses.
11281
+ */
11133
11282
  createCustomPoolWithDynamicConfig(params) {
11134
11283
  return __async(this, null, function* () {
11135
11284
  const {
@@ -11667,6 +11816,11 @@ var CpAmm = class {
11667
11816
  }).remainingAccounts(remainingAccounts).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
11668
11817
  });
11669
11818
  }
11819
+ /**
11820
+ * Builds a transaction to perform a swap in the pool.
11821
+ * @param params Swap2Params
11822
+ * @returns Transaction builder.
11823
+ */
11670
11824
  swap2(params) {
11671
11825
  return __async(this, null, function* () {
11672
11826
  const {
@@ -11835,6 +11989,11 @@ var CpAmm = class {
11835
11989
  return new Transaction().add(instruction);
11836
11990
  });
11837
11991
  }
11992
+ /**
11993
+ * Builds a transaction to close a position.
11994
+ * @param params ClosePositionParams
11995
+ * @returns Transaction builder.
11996
+ */
11838
11997
  closePosition(params) {
11839
11998
  return __async(this, null, function* () {
11840
11999
  const { owner, pool, position, positionNftMint, positionNftAccount } = params;
@@ -12077,6 +12236,77 @@ var CpAmm = class {
12077
12236
  return transaction;
12078
12237
  });
12079
12238
  }
12239
+ /**
12240
+ * Builds a transaction to initialize a reward for a pool.
12241
+ * @param params InitializeRewardParams
12242
+ * @returns Transaction builder.
12243
+ */
12244
+ initializeReward(params) {
12245
+ return __async(this, null, function* () {
12246
+ const {
12247
+ rewardIndex,
12248
+ rewardDuration,
12249
+ funder,
12250
+ pool,
12251
+ creator,
12252
+ payer,
12253
+ rewardMint,
12254
+ rewardMintProgram
12255
+ } = params;
12256
+ const rewardVault = deriveRewardVaultAddress(pool, rewardIndex);
12257
+ return yield this._program.methods.initializeReward(rewardIndex, rewardDuration, funder).accountsPartial({
12258
+ poolAuthority: this.poolAuthority,
12259
+ pool,
12260
+ rewardVault,
12261
+ rewardMint,
12262
+ signer: creator,
12263
+ payer,
12264
+ tokenProgram: rewardMintProgram
12265
+ }).transaction();
12266
+ });
12267
+ }
12268
+ /**
12269
+ * Builds a transaction to initialize and fund a reward for a pool.
12270
+ * @param params InitializeAndFundReward
12271
+ * @returns Transaction builder.
12272
+ */
12273
+ initializeAndFundReward(params) {
12274
+ return __async(this, null, function* () {
12275
+ const {
12276
+ rewardIndex,
12277
+ rewardDuration,
12278
+ pool,
12279
+ creator,
12280
+ payer,
12281
+ rewardMint,
12282
+ carryForward,
12283
+ amount,
12284
+ rewardMintProgram
12285
+ } = params;
12286
+ const rewardVault = deriveRewardVaultAddress(pool, rewardIndex);
12287
+ const initializeRewardTx = yield this.initializeReward({
12288
+ rewardIndex,
12289
+ rewardDuration,
12290
+ funder: payer,
12291
+ pool,
12292
+ creator,
12293
+ payer,
12294
+ rewardMint,
12295
+ rewardMintProgram
12296
+ });
12297
+ const fundRewardTx = yield this.fundReward({
12298
+ rewardIndex,
12299
+ carryForward,
12300
+ pool,
12301
+ funder: payer,
12302
+ amount,
12303
+ rewardMint,
12304
+ rewardVault,
12305
+ rewardMintProgram
12306
+ });
12307
+ return new Transaction().add(initializeRewardTx).add(fundRewardTx);
12308
+ });
12309
+ }
12080
12310
  /**
12081
12311
  * Builds a transaction to update reward duration.
12082
12312
  * @param {UpdateRewardDurationParams} params - Parameters including pool and new duration.
@@ -12084,10 +12314,10 @@ var CpAmm = class {
12084
12314
  */
12085
12315
  updateRewardDuration(params) {
12086
12316
  return __async(this, null, function* () {
12087
- const { pool, admin, rewardIndex, newDuration } = params;
12317
+ const { pool, signer, rewardIndex, newDuration } = params;
12088
12318
  return yield this._program.methods.updateRewardDuration(rewardIndex, newDuration).accountsPartial({
12089
12319
  pool,
12090
- signer: admin
12320
+ signer
12091
12321
  }).transaction();
12092
12322
  });
12093
12323
  }
@@ -12098,10 +12328,10 @@ var CpAmm = class {
12098
12328
  */
12099
12329
  updateRewardFunder(params) {
12100
12330
  return __async(this, null, function* () {
12101
- const { pool, admin, rewardIndex, newFunder } = params;
12331
+ const { pool, signer, rewardIndex, newFunder } = params;
12102
12332
  return yield this._program.methods.updateRewardFunder(rewardIndex, newFunder).accountsPartial({
12103
12333
  pool,
12104
- signer: admin
12334
+ signer
12105
12335
  }).transaction();
12106
12336
  });
12107
12337
  }
@@ -12378,9 +12608,9 @@ var CpAmm = class {
12378
12608
  position,
12379
12609
  positionNftAccount,
12380
12610
  rewardIndex,
12381
- skipReward,
12382
12611
  poolState,
12383
- positionState
12612
+ positionState,
12613
+ isSkipReward
12384
12614
  } = params;
12385
12615
  const rewardInfo = poolState.rewardInfos[rewardIndex];
12386
12616
  const tokenProgram = getTokenProgram(rewardInfo.rewardTokenFlag);
@@ -12399,6 +12629,7 @@ var CpAmm = class {
12399
12629
  const closeWrappedSOLIx = yield unwrapSOLInstruction(user);
12400
12630
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
12401
12631
  }
12632
+ const skipReward = isSkipReward ? 1 : 0;
12402
12633
  return yield this._program.methods.claimReward(rewardIndex, skipReward).accountsPartial({
12403
12634
  pool: positionState.pool,
12404
12635
  positionNftAccount,
@@ -12412,6 +12643,11 @@ var CpAmm = class {
12412
12643
  }).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
12413
12644
  });
12414
12645
  }
12646
+ /**
12647
+ * Builds a transaction to split a position into two positions.
12648
+ * @param params SplitPositionParams
12649
+ * @returns Transaction builder.
12650
+ */
12415
12651
  splitPosition(params) {
12416
12652
  return __async(this, null, function* () {
12417
12653
  const {
@@ -12448,6 +12684,11 @@ var CpAmm = class {
12448
12684
  }).transaction();
12449
12685
  });
12450
12686
  }
12687
+ /**
12688
+ * Builds a transaction to split a position into two positions.
12689
+ * @param params SplitPosition2Params
12690
+ * @returns Transaction builder.
12691
+ */
12451
12692
  splitPosition2(params) {
12452
12693
  return __async(this, null, function* () {
12453
12694
  const {
@@ -12592,6 +12833,7 @@ export {
12592
12833
  getPriceFromSqrtPrice,
12593
12834
  getPriceImpact,
12594
12835
  getRateLimiterParams,
12836
+ getRewardInfo,
12595
12837
  getSecondKey,
12596
12838
  getSimulationComputeUnits,
12597
12839
  getSqrtPriceFromPrice,
@@ -12604,7 +12846,8 @@ export {
12604
12846
  getTotalLockedLiquidity,
12605
12847
  getTotalTradingFeeFromExcludedFeeAmount,
12606
12848
  getTotalTradingFeeFromIncludedFeeAmount,
12607
- getUnClaimReward,
12849
+ getUnClaimLpFee,
12850
+ getUserRewardPending,
12608
12851
  hasPartner,
12609
12852
  isDynamicFeeEnabled,
12610
12853
  isNonZeroRateLimiter,
@@ -12613,6 +12856,7 @@ export {
12613
12856
  isVestingComplete,
12614
12857
  isZeroRateLimiter,
12615
12858
  mulDiv,
12859
+ offsetBasedFilter,
12616
12860
  parseFeeSchedulerSecondFactor,
12617
12861
  parseRateLimiterSecondFactor,
12618
12862
  positionByPoolFilter,