@meteora-ag/cp-amm-sdk 1.0.1-rc.20 → 1.0.1-rc.22

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.d.mts CHANGED
@@ -6629,6 +6629,7 @@ declare function derivePositionNftAccount(positionNftMint: PublicKey, programId:
6629
6629
  declare function deriveEventAuthority(programId: PublicKey): [PublicKey, number];
6630
6630
 
6631
6631
  declare const CP_AMM_PROGRAM_ID: PublicKey;
6632
+ declare const LIQUIDITY_SCALE = 128;
6632
6633
  declare const SCALE_OFFSET = 64;
6633
6634
  declare const BASIS_POINT_MAX = 10000;
6634
6635
  declare const MAX_FEE_NUMERATOR = 500000000;
@@ -6650,7 +6651,31 @@ declare const unwrapSOLInstruction: (owner: PublicKey, allowOwnerOffCurve?: bool
6650
6651
  declare function getNftOwner(connection: Connection, nftMint: PublicKey): Promise<PublicKey>;
6651
6652
 
6652
6653
  declare function getBaseFeeNumerator(feeSchedulerMode: FeeSchedulerMode, cliffFeeNumerator: BN, period: BN, reductionFactor: BN): BN;
6654
+ /**
6655
+ * Calculates the dynamic fee numerator based on market volatility metrics
6656
+ *
6657
+ * @param volatilityAccumulator - A measure of accumulated market volatility (BN)
6658
+ * @param binStep - The size of price bins in the liquidity distribution (BN)
6659
+ * @param variableFeeControl - Parameter controlling the impact of volatility on fees (BN)
6660
+ * @returns The calculated dynamic fee numerator (BN)
6661
+ */
6653
6662
  declare function getDynamicFeeNumerator(volatilityAccumulator: BN, binStep: BN, variableFeeControl: BN): BN;
6663
+ /**
6664
+ * Calculates the fee numerator based on current market conditions and fee schedule configuration
6665
+ *
6666
+ * @param currentPoint - The current price point in the liquidity curve
6667
+ * @param activationPoint - The price point at which the fee schedule is activated (BN)
6668
+ * @param numberOfPeriod - The total number of periods in the fee schedule
6669
+ * @param periodFrequency - The frequency at which periods change (BN)
6670
+ * @param feeSchedulerMode - The mode determining how fees are calculated (0 = constant, 1 = linear, etc.)
6671
+ * @param cliffFeeNumerator - The initial fee numerator at the cliff point (BN)
6672
+ * @param reductionFactor - The factor by which fees are reduced in each period (BN)
6673
+ * @param dynamicFeeParams - Optional parameters for dynamic fee calculation
6674
+ * @param dynamicFeeParams.volatilityAccumulator - Measure of accumulated market volatility (BN)
6675
+ * @param dynamicFeeParams.binStep - Size of price bins in the liquidity distribution (BN)
6676
+ * @param dynamicFeeParams.variableFeeControl - Parameter controlling the impact of volatility (BN)
6677
+ * @returns The calculated fee numerator (BN), capped at MAX_FEE_NUMERATOR
6678
+ */
6654
6679
  declare function getFeeNumerator(currentPoint: number, activationPoint: BN, numberOfPeriod: number, periodFrequency: BN, feeSchedulerMode: number, cliffFeeNumerator: BN, reductionFactor: BN, dynamicFeeParams?: {
6655
6680
  volatilityAccumulator: BN;
6656
6681
  binStep: BN;
@@ -6731,7 +6756,7 @@ declare const getMinAmountWithSlippage: (amount: BN, rate: number) => BN;
6731
6756
  */
6732
6757
  declare const getPriceImpact: (actualAmount: BN, idealAmount: BN) => number;
6733
6758
  declare const getCurrentPrice: (sqrtPrice: BN, tokenADecimal: number, tokenBDecimal: number) => string;
6734
- declare const getUnClaimReward: (positionState: PositionState) => {
6759
+ declare const getUnClaimReward: (poolState: PoolState, positionState: PositionState) => {
6735
6760
  feeTokenA: BN;
6736
6761
  feeTokenB: BN;
6737
6762
  rewards: BN[];
@@ -12911,4 +12936,4 @@ var CpAmmIDL = {
12911
12936
  types: types
12912
12937
  };
12913
12938
 
12914
- export { ActivationPoint, ActivationType, type AddLiquidityParams, type AmmProgram, BASIS_POINT_MAX, type BaseFee, CP_AMM_PROGRAM_ID, type ClaimPartnerFeeParams, type ClaimPositionFeeParams, type ClaimRewardParams, CollectFeeMode, type ConfigState, CpAmm, type CpAmm$1 as CpAmmTypes, type CreatePoolParams, type CreatePositionParams, type DynamicFee, FEE_DENOMINATOR, type FeeMode, FeeSchedulerMode, type FundRewardParams, type GetQuoteParams, type InitializeCustomizeablePoolParams, type InitializeRewardParams, type LiquidityDeltaParams, type LockPositionParams, MAX_CU_BUFFER, MAX_FEE_NUMERATOR, MAX_SQRT_PRICE, MIN_CU_BUFFER, MIN_SQRT_PRICE, ONE, PRECISION, type PermanentLockParams, type PoolFeesParams, type PoolState, type PositionState, type PreparePoolCreationParams, type PreparedPoolCreation, type RefreshVestingParams, type RemoveLiquidityParams, type RewardInfo, Rounding, SCALE_OFFSET, type SwapParams, type SwapQuotes, type TokenBadgeState, TradeDirection, type TxBuilder, type UpdateRewardDurationParams, type UpdateRewardFunderParams, type VestingState, type WithdrawIneligibleRewardParams, calculateInitSqrtPrice, decimalToQ64, CpAmmIDL as default, deriveClaimFeeOperatorAddress, deriveConfigAddress, deriveCustomizablePoolAddress, deriveEventAuthority, derivePoolAddress, derivePoolAuthority, derivePositionAddress, derivePositionNftAccount, deriveRewardVaultAddress, deriveTokenBadgeAddress, deriveTokenVaultAddress, divCeil, getAmountAFromLiquidityDelta, getAmountBFromLiquidityDelta, getBaseFeeNumerator, getCurrentPrice, getDeltaAmountA, getDeltaAmountB, getDynamicFeeNumerator, getEstimatedComputeUnitIxWithBuffer, getEstimatedComputeUnitUsageWithBuffer, getFeeNumerator, getFirstKey, getLiquidityDeltaFromAmountA, getLiquidityDeltaFromAmountB, getMaxAmountWithSlippage, getMinAmountWithSlippage, getNextSqrtPrice, getNftOwner, getOrCreateATAInstruction, getPriceImpact, getSecondKey, getSimulationComputeUnits, getSwapAmount, getTokenDecimals, getTokenProgram, getUnClaimReward, mulDiv, mulShr, positionByPoolFilter, pow, q64ToDecimal, shlDiv, unwrapSOLInstruction, vestingByPositionFilter, wrapSOLInstruction };
12939
+ export { ActivationPoint, ActivationType, type AddLiquidityParams, type AmmProgram, BASIS_POINT_MAX, type BaseFee, CP_AMM_PROGRAM_ID, type ClaimPartnerFeeParams, type ClaimPositionFeeParams, type ClaimRewardParams, CollectFeeMode, type ConfigState, CpAmm, type CpAmm$1 as CpAmmTypes, type CreatePoolParams, type CreatePositionParams, type DynamicFee, FEE_DENOMINATOR, type FeeMode, FeeSchedulerMode, type FundRewardParams, type GetQuoteParams, type InitializeCustomizeablePoolParams, type InitializeRewardParams, LIQUIDITY_SCALE, type LiquidityDeltaParams, type LockPositionParams, MAX_CU_BUFFER, MAX_FEE_NUMERATOR, MAX_SQRT_PRICE, MIN_CU_BUFFER, MIN_SQRT_PRICE, ONE, PRECISION, type PermanentLockParams, type PoolFeesParams, type PoolState, type PositionState, type PreparePoolCreationParams, type PreparedPoolCreation, type RefreshVestingParams, type RemoveLiquidityParams, type RewardInfo, Rounding, SCALE_OFFSET, type SwapParams, type SwapQuotes, type TokenBadgeState, TradeDirection, type TxBuilder, type UpdateRewardDurationParams, type UpdateRewardFunderParams, type VestingState, type WithdrawIneligibleRewardParams, calculateInitSqrtPrice, decimalToQ64, CpAmmIDL as default, deriveClaimFeeOperatorAddress, deriveConfigAddress, deriveCustomizablePoolAddress, deriveEventAuthority, derivePoolAddress, derivePoolAuthority, derivePositionAddress, derivePositionNftAccount, deriveRewardVaultAddress, deriveTokenBadgeAddress, deriveTokenVaultAddress, divCeil, getAmountAFromLiquidityDelta, getAmountBFromLiquidityDelta, getBaseFeeNumerator, getCurrentPrice, getDeltaAmountA, getDeltaAmountB, getDynamicFeeNumerator, getEstimatedComputeUnitIxWithBuffer, getEstimatedComputeUnitUsageWithBuffer, getFeeNumerator, getFirstKey, getLiquidityDeltaFromAmountA, getLiquidityDeltaFromAmountB, getMaxAmountWithSlippage, getMinAmountWithSlippage, getNextSqrtPrice, getNftOwner, getOrCreateATAInstruction, getPriceImpact, getSecondKey, getSimulationComputeUnits, getSwapAmount, getTokenDecimals, getTokenProgram, getUnClaimReward, mulDiv, mulShr, positionByPoolFilter, pow, q64ToDecimal, shlDiv, unwrapSOLInstruction, vestingByPositionFilter, wrapSOLInstruction };
package/dist/index.d.ts CHANGED
@@ -6629,6 +6629,7 @@ declare function derivePositionNftAccount(positionNftMint: PublicKey, programId:
6629
6629
  declare function deriveEventAuthority(programId: PublicKey): [PublicKey, number];
6630
6630
 
6631
6631
  declare const CP_AMM_PROGRAM_ID: PublicKey;
6632
+ declare const LIQUIDITY_SCALE = 128;
6632
6633
  declare const SCALE_OFFSET = 64;
6633
6634
  declare const BASIS_POINT_MAX = 10000;
6634
6635
  declare const MAX_FEE_NUMERATOR = 500000000;
@@ -6650,7 +6651,31 @@ declare const unwrapSOLInstruction: (owner: PublicKey, allowOwnerOffCurve?: bool
6650
6651
  declare function getNftOwner(connection: Connection, nftMint: PublicKey): Promise<PublicKey>;
6651
6652
 
6652
6653
  declare function getBaseFeeNumerator(feeSchedulerMode: FeeSchedulerMode, cliffFeeNumerator: BN, period: BN, reductionFactor: BN): BN;
6654
+ /**
6655
+ * Calculates the dynamic fee numerator based on market volatility metrics
6656
+ *
6657
+ * @param volatilityAccumulator - A measure of accumulated market volatility (BN)
6658
+ * @param binStep - The size of price bins in the liquidity distribution (BN)
6659
+ * @param variableFeeControl - Parameter controlling the impact of volatility on fees (BN)
6660
+ * @returns The calculated dynamic fee numerator (BN)
6661
+ */
6653
6662
  declare function getDynamicFeeNumerator(volatilityAccumulator: BN, binStep: BN, variableFeeControl: BN): BN;
6663
+ /**
6664
+ * Calculates the fee numerator based on current market conditions and fee schedule configuration
6665
+ *
6666
+ * @param currentPoint - The current price point in the liquidity curve
6667
+ * @param activationPoint - The price point at which the fee schedule is activated (BN)
6668
+ * @param numberOfPeriod - The total number of periods in the fee schedule
6669
+ * @param periodFrequency - The frequency at which periods change (BN)
6670
+ * @param feeSchedulerMode - The mode determining how fees are calculated (0 = constant, 1 = linear, etc.)
6671
+ * @param cliffFeeNumerator - The initial fee numerator at the cliff point (BN)
6672
+ * @param reductionFactor - The factor by which fees are reduced in each period (BN)
6673
+ * @param dynamicFeeParams - Optional parameters for dynamic fee calculation
6674
+ * @param dynamicFeeParams.volatilityAccumulator - Measure of accumulated market volatility (BN)
6675
+ * @param dynamicFeeParams.binStep - Size of price bins in the liquidity distribution (BN)
6676
+ * @param dynamicFeeParams.variableFeeControl - Parameter controlling the impact of volatility (BN)
6677
+ * @returns The calculated fee numerator (BN), capped at MAX_FEE_NUMERATOR
6678
+ */
6654
6679
  declare function getFeeNumerator(currentPoint: number, activationPoint: BN, numberOfPeriod: number, periodFrequency: BN, feeSchedulerMode: number, cliffFeeNumerator: BN, reductionFactor: BN, dynamicFeeParams?: {
6655
6680
  volatilityAccumulator: BN;
6656
6681
  binStep: BN;
@@ -6731,7 +6756,7 @@ declare const getMinAmountWithSlippage: (amount: BN, rate: number) => BN;
6731
6756
  */
6732
6757
  declare const getPriceImpact: (actualAmount: BN, idealAmount: BN) => number;
6733
6758
  declare const getCurrentPrice: (sqrtPrice: BN, tokenADecimal: number, tokenBDecimal: number) => string;
6734
- declare const getUnClaimReward: (positionState: PositionState) => {
6759
+ declare const getUnClaimReward: (poolState: PoolState, positionState: PositionState) => {
6735
6760
  feeTokenA: BN;
6736
6761
  feeTokenB: BN;
6737
6762
  rewards: BN[];
@@ -12911,4 +12936,4 @@ var CpAmmIDL = {
12911
12936
  types: types
12912
12937
  };
12913
12938
 
12914
- export { ActivationPoint, ActivationType, type AddLiquidityParams, type AmmProgram, BASIS_POINT_MAX, type BaseFee, CP_AMM_PROGRAM_ID, type ClaimPartnerFeeParams, type ClaimPositionFeeParams, type ClaimRewardParams, CollectFeeMode, type ConfigState, CpAmm, type CpAmm$1 as CpAmmTypes, type CreatePoolParams, type CreatePositionParams, type DynamicFee, FEE_DENOMINATOR, type FeeMode, FeeSchedulerMode, type FundRewardParams, type GetQuoteParams, type InitializeCustomizeablePoolParams, type InitializeRewardParams, type LiquidityDeltaParams, type LockPositionParams, MAX_CU_BUFFER, MAX_FEE_NUMERATOR, MAX_SQRT_PRICE, MIN_CU_BUFFER, MIN_SQRT_PRICE, ONE, PRECISION, type PermanentLockParams, type PoolFeesParams, type PoolState, type PositionState, type PreparePoolCreationParams, type PreparedPoolCreation, type RefreshVestingParams, type RemoveLiquidityParams, type RewardInfo, Rounding, SCALE_OFFSET, type SwapParams, type SwapQuotes, type TokenBadgeState, TradeDirection, type TxBuilder, type UpdateRewardDurationParams, type UpdateRewardFunderParams, type VestingState, type WithdrawIneligibleRewardParams, calculateInitSqrtPrice, decimalToQ64, CpAmmIDL as default, deriveClaimFeeOperatorAddress, deriveConfigAddress, deriveCustomizablePoolAddress, deriveEventAuthority, derivePoolAddress, derivePoolAuthority, derivePositionAddress, derivePositionNftAccount, deriveRewardVaultAddress, deriveTokenBadgeAddress, deriveTokenVaultAddress, divCeil, getAmountAFromLiquidityDelta, getAmountBFromLiquidityDelta, getBaseFeeNumerator, getCurrentPrice, getDeltaAmountA, getDeltaAmountB, getDynamicFeeNumerator, getEstimatedComputeUnitIxWithBuffer, getEstimatedComputeUnitUsageWithBuffer, getFeeNumerator, getFirstKey, getLiquidityDeltaFromAmountA, getLiquidityDeltaFromAmountB, getMaxAmountWithSlippage, getMinAmountWithSlippage, getNextSqrtPrice, getNftOwner, getOrCreateATAInstruction, getPriceImpact, getSecondKey, getSimulationComputeUnits, getSwapAmount, getTokenDecimals, getTokenProgram, getUnClaimReward, mulDiv, mulShr, positionByPoolFilter, pow, q64ToDecimal, shlDiv, unwrapSOLInstruction, vestingByPositionFilter, wrapSOLInstruction };
12939
+ export { ActivationPoint, ActivationType, type AddLiquidityParams, type AmmProgram, BASIS_POINT_MAX, type BaseFee, CP_AMM_PROGRAM_ID, type ClaimPartnerFeeParams, type ClaimPositionFeeParams, type ClaimRewardParams, CollectFeeMode, type ConfigState, CpAmm, type CpAmm$1 as CpAmmTypes, type CreatePoolParams, type CreatePositionParams, type DynamicFee, FEE_DENOMINATOR, type FeeMode, FeeSchedulerMode, type FundRewardParams, type GetQuoteParams, type InitializeCustomizeablePoolParams, type InitializeRewardParams, LIQUIDITY_SCALE, type LiquidityDeltaParams, type LockPositionParams, MAX_CU_BUFFER, MAX_FEE_NUMERATOR, MAX_SQRT_PRICE, MIN_CU_BUFFER, MIN_SQRT_PRICE, ONE, PRECISION, type PermanentLockParams, type PoolFeesParams, type PoolState, type PositionState, type PreparePoolCreationParams, type PreparedPoolCreation, type RefreshVestingParams, type RemoveLiquidityParams, type RewardInfo, Rounding, SCALE_OFFSET, type SwapParams, type SwapQuotes, type TokenBadgeState, TradeDirection, type TxBuilder, type UpdateRewardDurationParams, type UpdateRewardFunderParams, type VestingState, type WithdrawIneligibleRewardParams, calculateInitSqrtPrice, decimalToQ64, CpAmmIDL as default, deriveClaimFeeOperatorAddress, deriveConfigAddress, deriveCustomizablePoolAddress, deriveEventAuthority, derivePoolAddress, derivePoolAuthority, derivePositionAddress, derivePositionNftAccount, deriveRewardVaultAddress, deriveTokenBadgeAddress, deriveTokenVaultAddress, divCeil, getAmountAFromLiquidityDelta, getAmountBFromLiquidityDelta, getBaseFeeNumerator, getCurrentPrice, getDeltaAmountA, getDeltaAmountB, getDynamicFeeNumerator, getEstimatedComputeUnitIxWithBuffer, getEstimatedComputeUnitUsageWithBuffer, getFeeNumerator, getFirstKey, getLiquidityDeltaFromAmountA, getLiquidityDeltaFromAmountB, getMaxAmountWithSlippage, getMinAmountWithSlippage, getNextSqrtPrice, getNftOwner, getOrCreateATAInstruction, getPriceImpact, getSecondKey, getSimulationComputeUnits, getSwapAmount, getTokenDecimals, getTokenProgram, getUnClaimReward, mulDiv, mulShr, positionByPoolFilter, pow, q64ToDecimal, shlDiv, unwrapSOLInstruction, vestingByPositionFilter, wrapSOLInstruction };
package/dist/index.js CHANGED
@@ -6181,6 +6181,7 @@ var _web3js = require('@solana/web3.js');
6181
6181
  var CP_AMM_PROGRAM_ID = new (0, _web3js.PublicKey)(
6182
6182
  "cpamdpZCGKUy5JxQXB4dcpGPiikHawvSWAd6mEn1sGG"
6183
6183
  );
6184
+ var LIQUIDITY_SCALE = 128;
6184
6185
  var SCALE_OFFSET = 64;
6185
6186
  var BASIS_POINT_MAX = 1e4;
6186
6187
  var MAX_FEE_NUMERATOR = 5e8;
@@ -6285,6 +6286,140 @@ function deriveEventAuthority(programId) {
6285
6286
  );
6286
6287
  }
6287
6288
 
6289
+ // src/helpers/token.ts
6290
+
6291
+
6292
+
6293
+
6294
+
6295
+
6296
+
6297
+
6298
+
6299
+
6300
+
6301
+
6302
+
6303
+
6304
+
6305
+
6306
+
6307
+ function getTokenProgram(flag) {
6308
+ return flag == 0 ? _spltoken.TOKEN_PROGRAM_ID : _spltoken.TOKEN_2022_PROGRAM_ID;
6309
+ }
6310
+ var getTokenDecimals = (connection, mint) => __async(void 0, null, function* () {
6311
+ return (yield _spltoken.getMint.call(void 0, connection, mint)).decimals;
6312
+ });
6313
+ var getOrCreateATAInstruction = (_0, _1, _2, ..._3) => __async(void 0, [_0, _1, _2, ..._3], function* (connection, tokenMint, owner, payer = owner, allowOwnerOffCurve = true, tokenProgram) {
6314
+ const toAccount = _spltoken.getAssociatedTokenAddressSync.call(void 0,
6315
+ tokenMint,
6316
+ owner,
6317
+ allowOwnerOffCurve,
6318
+ tokenProgram
6319
+ );
6320
+ try {
6321
+ yield _spltoken.getAccount.call(void 0, connection, toAccount);
6322
+ return { ataPubkey: toAccount, ix: void 0 };
6323
+ } catch (e) {
6324
+ if (e instanceof _spltoken.TokenAccountNotFoundError || e instanceof _spltoken.TokenInvalidAccountOwnerError) {
6325
+ const ix = _spltoken.createAssociatedTokenAccountIdempotentInstruction.call(void 0,
6326
+ payer,
6327
+ toAccount,
6328
+ owner,
6329
+ tokenMint,
6330
+ tokenProgram
6331
+ );
6332
+ return { ataPubkey: toAccount, ix };
6333
+ } else {
6334
+ console.error("Error::getOrCreateATAInstruction", e);
6335
+ throw e;
6336
+ }
6337
+ }
6338
+ });
6339
+ var wrapSOLInstruction = (from, to, amount) => {
6340
+ return [
6341
+ _web3js.SystemProgram.transfer({
6342
+ fromPubkey: from,
6343
+ toPubkey: to,
6344
+ lamports: amount
6345
+ }),
6346
+ new (0, _web3js.TransactionInstruction)({
6347
+ keys: [
6348
+ {
6349
+ pubkey: to,
6350
+ isSigner: false,
6351
+ isWritable: true
6352
+ }
6353
+ ],
6354
+ data: Buffer.from(new Uint8Array([17])),
6355
+ programId: _spltoken.TOKEN_PROGRAM_ID
6356
+ })
6357
+ ];
6358
+ };
6359
+ var unwrapSOLInstruction = (owner, allowOwnerOffCurve = true) => __async(void 0, null, function* () {
6360
+ const wSolATAAccount = _spltoken.getAssociatedTokenAddressSync.call(void 0,
6361
+ _spltoken.NATIVE_MINT,
6362
+ owner,
6363
+ allowOwnerOffCurve
6364
+ );
6365
+ if (wSolATAAccount) {
6366
+ const closedWrappedSolInstruction = _spltoken.createCloseAccountInstruction.call(void 0,
6367
+ wSolATAAccount,
6368
+ owner,
6369
+ owner,
6370
+ [],
6371
+ _spltoken.TOKEN_PROGRAM_ID
6372
+ );
6373
+ return closedWrappedSolInstruction;
6374
+ }
6375
+ return null;
6376
+ });
6377
+ function getNftOwner(connection, nftMint) {
6378
+ return __async(this, null, function* () {
6379
+ const largesTokenAccount = yield connection.getTokenLargestAccounts(nftMint);
6380
+ const accountInfo = yield connection.getParsedAccountInfo(
6381
+ largesTokenAccount.value[0].address
6382
+ );
6383
+ const owner = new (0, _web3js.PublicKey)(accountInfo.value.data.parsed.info.owner);
6384
+ return new (0, _web3js.PublicKey)(owner);
6385
+ });
6386
+ }
6387
+
6388
+ // src/helpers/fee.ts
6389
+
6390
+
6391
+ // src/types.ts
6392
+ var Rounding = /* @__PURE__ */ ((Rounding2) => {
6393
+ Rounding2[Rounding2["Up"] = 0] = "Up";
6394
+ Rounding2[Rounding2["Down"] = 1] = "Down";
6395
+ return Rounding2;
6396
+ })(Rounding || {});
6397
+ var ActivationPoint = /* @__PURE__ */ ((ActivationPoint2) => {
6398
+ ActivationPoint2[ActivationPoint2["Timestamp"] = 0] = "Timestamp";
6399
+ ActivationPoint2[ActivationPoint2["Slot"] = 1] = "Slot";
6400
+ return ActivationPoint2;
6401
+ })(ActivationPoint || {});
6402
+ var FeeSchedulerMode = /* @__PURE__ */ ((FeeSchedulerMode2) => {
6403
+ FeeSchedulerMode2[FeeSchedulerMode2["Linear"] = 0] = "Linear";
6404
+ FeeSchedulerMode2[FeeSchedulerMode2["Exponential"] = 1] = "Exponential";
6405
+ return FeeSchedulerMode2;
6406
+ })(FeeSchedulerMode || {});
6407
+ var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode3) => {
6408
+ CollectFeeMode3[CollectFeeMode3["BothToken"] = 0] = "BothToken";
6409
+ CollectFeeMode3[CollectFeeMode3["OnlyB"] = 1] = "OnlyB";
6410
+ return CollectFeeMode3;
6411
+ })(CollectFeeMode || {});
6412
+ var TradeDirection = /* @__PURE__ */ ((TradeDirection2) => {
6413
+ TradeDirection2[TradeDirection2["AtoB"] = 0] = "AtoB";
6414
+ TradeDirection2[TradeDirection2["BtoA"] = 1] = "BtoA";
6415
+ return TradeDirection2;
6416
+ })(TradeDirection || {});
6417
+ var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
6418
+ ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
6419
+ ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
6420
+ return ActivationType2;
6421
+ })(ActivationType || {});
6422
+
6288
6423
  // src/math/feeMath.ts
6289
6424
 
6290
6425
  var MAX_EXPONENTIAL = new (0, _anchor.BN)(524288);
@@ -6392,40 +6527,6 @@ function pow(base, exp) {
6392
6527
  // src/math/mathUtils.ts
6393
6528
 
6394
6529
  var _decimaljs = require('decimal.js'); var _decimaljs2 = _interopRequireDefault(_decimaljs);
6395
-
6396
- // src/types.ts
6397
- var Rounding = /* @__PURE__ */ ((Rounding2) => {
6398
- Rounding2[Rounding2["Up"] = 0] = "Up";
6399
- Rounding2[Rounding2["Down"] = 1] = "Down";
6400
- return Rounding2;
6401
- })(Rounding || {});
6402
- var ActivationPoint = /* @__PURE__ */ ((ActivationPoint2) => {
6403
- ActivationPoint2[ActivationPoint2["Timestamp"] = 0] = "Timestamp";
6404
- ActivationPoint2[ActivationPoint2["Slot"] = 1] = "Slot";
6405
- return ActivationPoint2;
6406
- })(ActivationPoint || {});
6407
- var FeeSchedulerMode = /* @__PURE__ */ ((FeeSchedulerMode2) => {
6408
- FeeSchedulerMode2[FeeSchedulerMode2["Linear"] = 0] = "Linear";
6409
- FeeSchedulerMode2[FeeSchedulerMode2["Exponential"] = 1] = "Exponential";
6410
- return FeeSchedulerMode2;
6411
- })(FeeSchedulerMode || {});
6412
- var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode3) => {
6413
- CollectFeeMode3[CollectFeeMode3["BothToken"] = 0] = "BothToken";
6414
- CollectFeeMode3[CollectFeeMode3["OnlyB"] = 1] = "OnlyB";
6415
- return CollectFeeMode3;
6416
- })(CollectFeeMode || {});
6417
- var TradeDirection = /* @__PURE__ */ ((TradeDirection3) => {
6418
- TradeDirection3[TradeDirection3["AtoB"] = 0] = "AtoB";
6419
- TradeDirection3[TradeDirection3["BtoA"] = 1] = "BtoA";
6420
- return TradeDirection3;
6421
- })(TradeDirection || {});
6422
- var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
6423
- ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
6424
- ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
6425
- return ActivationType2;
6426
- })(ActivationType || {});
6427
-
6428
- // src/math/mathUtils.ts
6429
6530
  function mulShr(x, y, offset, rounding) {
6430
6531
  const denominator = new (0, _anchor.BN)(1).shln(offset);
6431
6532
  return mulDiv(x, y, denominator, rounding);
@@ -6454,138 +6555,6 @@ function decimalToQ64(num) {
6454
6555
  return new (0, _anchor.BN)(num.mul(_decimaljs2.default.pow(2, 64)).floor().toFixed());
6455
6556
  }
6456
6557
 
6457
- // src/helpers/priceMath.ts
6458
-
6459
-
6460
- function calculateInitSqrtPrice(tokenAAmount, tokenBAmount, minSqrtPrice, maxSqrtPrice) {
6461
- if (tokenAAmount.isZero() || tokenBAmount.isZero()) {
6462
- throw new Error("Amount cannot be zero");
6463
- }
6464
- const amountADecimal = new (0, _decimaljs2.default)(tokenAAmount.toString());
6465
- const amountBDecimal = new (0, _decimaljs2.default)(tokenBAmount.toString());
6466
- const minSqrtPriceDecimal = new (0, _decimaljs2.default)(minSqrtPrice.toString()).div(
6467
- _decimaljs2.default.pow(2, 64)
6468
- );
6469
- const maxSqrtPriceDecimal = new (0, _decimaljs2.default)(maxSqrtPrice.toString()).div(
6470
- _decimaljs2.default.pow(2, 64)
6471
- );
6472
- const x = new (0, _decimaljs2.default)(1).div(maxSqrtPriceDecimal);
6473
- const y = amountBDecimal.div(amountADecimal);
6474
- const xy = x.mul(y);
6475
- const paMinusXY = minSqrtPriceDecimal.sub(xy);
6476
- const xyMinusPa = xy.sub(minSqrtPriceDecimal);
6477
- const fourY = new (0, _decimaljs2.default)(4).mul(y);
6478
- const discriminant = xyMinusPa.mul(xyMinusPa).add(fourY);
6479
- if (discriminant.isNeg()) {
6480
- throw new Error("Calculate sqrt price failed: negative discriminant");
6481
- }
6482
- const sqrtDiscriminant = discriminant.sqrt();
6483
- const result = paMinusXY.add(sqrtDiscriminant).div(new (0, _decimaljs2.default)(2)).mul(_decimaljs2.default.pow(2, 64));
6484
- return new (0, _anchor.BN)(result.floor().toFixed());
6485
- }
6486
-
6487
- // src/helpers/token.ts
6488
-
6489
-
6490
-
6491
-
6492
-
6493
-
6494
-
6495
-
6496
-
6497
-
6498
-
6499
-
6500
-
6501
-
6502
-
6503
-
6504
-
6505
- function getTokenProgram(flag) {
6506
- return flag == 0 ? _spltoken.TOKEN_PROGRAM_ID : _spltoken.TOKEN_2022_PROGRAM_ID;
6507
- }
6508
- var getTokenDecimals = (connection, mint) => __async(void 0, null, function* () {
6509
- return (yield _spltoken.getMint.call(void 0, connection, mint)).decimals;
6510
- });
6511
- var getOrCreateATAInstruction = (_0, _1, _2, ..._3) => __async(void 0, [_0, _1, _2, ..._3], function* (connection, tokenMint, owner, payer = owner, allowOwnerOffCurve = true, tokenProgram) {
6512
- const toAccount = _spltoken.getAssociatedTokenAddressSync.call(void 0,
6513
- tokenMint,
6514
- owner,
6515
- allowOwnerOffCurve,
6516
- tokenProgram
6517
- );
6518
- try {
6519
- yield _spltoken.getAccount.call(void 0, connection, toAccount);
6520
- return { ataPubkey: toAccount, ix: void 0 };
6521
- } catch (e) {
6522
- if (e instanceof _spltoken.TokenAccountNotFoundError || e instanceof _spltoken.TokenInvalidAccountOwnerError) {
6523
- const ix = _spltoken.createAssociatedTokenAccountIdempotentInstruction.call(void 0,
6524
- payer,
6525
- toAccount,
6526
- owner,
6527
- tokenMint,
6528
- tokenProgram
6529
- );
6530
- return { ataPubkey: toAccount, ix };
6531
- } else {
6532
- console.error("Error::getOrCreateATAInstruction", e);
6533
- throw e;
6534
- }
6535
- }
6536
- });
6537
- var wrapSOLInstruction = (from, to, amount) => {
6538
- return [
6539
- _web3js.SystemProgram.transfer({
6540
- fromPubkey: from,
6541
- toPubkey: to,
6542
- lamports: amount
6543
- }),
6544
- new (0, _web3js.TransactionInstruction)({
6545
- keys: [
6546
- {
6547
- pubkey: to,
6548
- isSigner: false,
6549
- isWritable: true
6550
- }
6551
- ],
6552
- data: Buffer.from(new Uint8Array([17])),
6553
- programId: _spltoken.TOKEN_PROGRAM_ID
6554
- })
6555
- ];
6556
- };
6557
- var unwrapSOLInstruction = (owner, allowOwnerOffCurve = true) => __async(void 0, null, function* () {
6558
- const wSolATAAccount = _spltoken.getAssociatedTokenAddressSync.call(void 0,
6559
- _spltoken.NATIVE_MINT,
6560
- owner,
6561
- allowOwnerOffCurve
6562
- );
6563
- if (wSolATAAccount) {
6564
- const closedWrappedSolInstruction = _spltoken.createCloseAccountInstruction.call(void 0,
6565
- wSolATAAccount,
6566
- owner,
6567
- owner,
6568
- [],
6569
- _spltoken.TOKEN_PROGRAM_ID
6570
- );
6571
- return closedWrappedSolInstruction;
6572
- }
6573
- return null;
6574
- });
6575
- function getNftOwner(connection, nftMint) {
6576
- return __async(this, null, function* () {
6577
- const largesTokenAccount = yield connection.getTokenLargestAccounts(nftMint);
6578
- const accountInfo = yield connection.getParsedAccountInfo(
6579
- largesTokenAccount.value[0].address
6580
- );
6581
- const owner = new (0, _web3js.PublicKey)(accountInfo.value.data.parsed.info.owner);
6582
- return new (0, _web3js.PublicKey)(owner);
6583
- });
6584
- }
6585
-
6586
- // src/helpers/fee.ts
6587
-
6588
-
6589
6558
  // src/helpers/curve.ts
6590
6559
 
6591
6560
 
@@ -6835,10 +6804,19 @@ var getCurrentPrice = (sqrtPrice, tokenADecimal, tokenBDecimal) => {
6835
6804
  const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new (0, _decimaljs2.default)(__pow(10, tokenADecimal - tokenBDecimal))).div(_decimaljs2.default.pow(2, 128)).toString();
6836
6805
  return price;
6837
6806
  };
6838
- var getUnClaimReward = (positionState) => {
6807
+ var getUnClaimReward = (poolState, positionState) => {
6808
+ const totalPositionLiquidity = positionState.unlockedLiquidity.add(positionState.vestedLiquidity).add(positionState.permanentLockedLiquidity);
6809
+ const feeAPerTokenStored = new (0, _anchor.BN)(
6810
+ Buffer.from(poolState.feeAPerLiquidity).reverse()
6811
+ ).sub(new (0, _anchor.BN)(Buffer.from(positionState.feeAPerTokenCheckpoint).reverse()));
6812
+ const feeBPerTokenStored = new (0, _anchor.BN)(
6813
+ Buffer.from(poolState.feeBPerLiquidity).reverse()
6814
+ ).sub(new (0, _anchor.BN)(Buffer.from(positionState.feeBPerTokenCheckpoint).reverse()));
6815
+ const feeA = totalPositionLiquidity.mul(feeAPerTokenStored).shrn(LIQUIDITY_SCALE);
6816
+ const feeB = totalPositionLiquidity.mul(feeBPerTokenStored).shrn(LIQUIDITY_SCALE);
6839
6817
  return {
6840
- feeTokenA: positionState.feeAPending,
6841
- feeTokenB: positionState.feeBPending,
6818
+ feeTokenA: positionState.feeAPending.add(feeA),
6819
+ feeTokenB: positionState.feeBPending.add(feeB),
6842
6820
  rewards: positionState.rewardInfos.length > 0 ? positionState.rewardInfos.map((item) => item.rewardPendings) : []
6843
6821
  };
6844
6822
  };
@@ -6861,6 +6839,33 @@ var vestingByPositionFilter = (position) => {
6861
6839
  };
6862
6840
  };
6863
6841
 
6842
+ // src/helpers/priceMath.ts
6843
+
6844
+
6845
+ function calculateInitSqrtPrice(tokenAAmount, tokenBAmount, minSqrtPrice, maxSqrtPrice) {
6846
+ if (tokenAAmount.isZero() || tokenBAmount.isZero()) {
6847
+ throw new Error("Amount cannot be zero");
6848
+ }
6849
+ const amountADecimal = new (0, _decimaljs2.default)(tokenAAmount.toString());
6850
+ const amountBDecimal = new (0, _decimaljs2.default)(tokenBAmount.toString());
6851
+ const minSqrtPriceDecimal = new (0, _decimaljs2.default)(minSqrtPrice.toString()).div(
6852
+ _decimaljs2.default.pow(2, 64)
6853
+ );
6854
+ const maxSqrtPriceDecimal = new (0, _decimaljs2.default)(maxSqrtPrice.toString()).div(
6855
+ _decimaljs2.default.pow(2, 64)
6856
+ );
6857
+ const x = new (0, _decimaljs2.default)(1).div(maxSqrtPriceDecimal);
6858
+ const y = amountBDecimal.div(amountADecimal);
6859
+ const xy = x.mul(y);
6860
+ const paMinusXY = minSqrtPriceDecimal.sub(xy);
6861
+ const xyMinusPa = xy.sub(minSqrtPriceDecimal);
6862
+ const fourY = new (0, _decimaljs2.default)(4).mul(y);
6863
+ const discriminant = xyMinusPa.mul(xyMinusPa).add(fourY);
6864
+ const sqrtDiscriminant = discriminant.sqrt();
6865
+ const result = paMinusXY.add(sqrtDiscriminant).div(new (0, _decimaljs2.default)(2)).mul(_decimaljs2.default.pow(2, 64));
6866
+ return new (0, _anchor.BN)(result.floor().toFixed());
6867
+ }
6868
+
6864
6869
  // src/CpAmm.ts
6865
6870
  var CpAmm = class {
6866
6871
  constructor(connection) {
@@ -8173,5 +8178,6 @@ var index_default = cp_amm_default;
8173
8178
 
8174
8179
 
8175
8180
 
8176
- 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.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.decimalToQ64 = decimalToQ64; exports.default = index_default; exports.deriveClaimFeeOperatorAddress = deriveClaimFeeOperatorAddress; exports.deriveConfigAddress = deriveConfigAddress; exports.deriveCustomizablePoolAddress = deriveCustomizablePoolAddress; exports.deriveEventAuthority = deriveEventAuthority; exports.derivePoolAddress = derivePoolAddress; exports.derivePoolAuthority = derivePoolAuthority; exports.derivePositionAddress = derivePositionAddress; exports.derivePositionNftAccount = derivePositionNftAccount; exports.deriveRewardVaultAddress = deriveRewardVaultAddress; exports.deriveTokenBadgeAddress = deriveTokenBadgeAddress; exports.deriveTokenVaultAddress = deriveTokenVaultAddress; exports.divCeil = divCeil; exports.getAmountAFromLiquidityDelta = getAmountAFromLiquidityDelta; exports.getAmountBFromLiquidityDelta = getAmountBFromLiquidityDelta; exports.getBaseFeeNumerator = getBaseFeeNumerator; exports.getCurrentPrice = getCurrentPrice; exports.getDeltaAmountA = getDeltaAmountA; exports.getDeltaAmountB = getDeltaAmountB; 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.getPriceImpact = getPriceImpact; exports.getSecondKey = getSecondKey; exports.getSimulationComputeUnits = getSimulationComputeUnits; exports.getSwapAmount = getSwapAmount; exports.getTokenDecimals = getTokenDecimals; exports.getTokenProgram = getTokenProgram; exports.getUnClaimReward = getUnClaimReward; exports.mulDiv = mulDiv; exports.mulShr = mulShr; exports.positionByPoolFilter = positionByPoolFilter; exports.pow = pow; exports.q64ToDecimal = q64ToDecimal; exports.shlDiv = shlDiv; exports.unwrapSOLInstruction = unwrapSOLInstruction; exports.vestingByPositionFilter = vestingByPositionFilter; exports.wrapSOLInstruction = wrapSOLInstruction;
8181
+
8182
+ 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.decimalToQ64 = decimalToQ64; exports.default = index_default; exports.deriveClaimFeeOperatorAddress = deriveClaimFeeOperatorAddress; exports.deriveConfigAddress = deriveConfigAddress; exports.deriveCustomizablePoolAddress = deriveCustomizablePoolAddress; exports.deriveEventAuthority = deriveEventAuthority; exports.derivePoolAddress = derivePoolAddress; exports.derivePoolAuthority = derivePoolAuthority; exports.derivePositionAddress = derivePositionAddress; exports.derivePositionNftAccount = derivePositionNftAccount; exports.deriveRewardVaultAddress = deriveRewardVaultAddress; exports.deriveTokenBadgeAddress = deriveTokenBadgeAddress; exports.deriveTokenVaultAddress = deriveTokenVaultAddress; exports.divCeil = divCeil; exports.getAmountAFromLiquidityDelta = getAmountAFromLiquidityDelta; exports.getAmountBFromLiquidityDelta = getAmountBFromLiquidityDelta; exports.getBaseFeeNumerator = getBaseFeeNumerator; exports.getCurrentPrice = getCurrentPrice; exports.getDeltaAmountA = getDeltaAmountA; exports.getDeltaAmountB = getDeltaAmountB; 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.getPriceImpact = getPriceImpact; exports.getSecondKey = getSecondKey; exports.getSimulationComputeUnits = getSimulationComputeUnits; exports.getSwapAmount = getSwapAmount; exports.getTokenDecimals = getTokenDecimals; exports.getTokenProgram = getTokenProgram; exports.getUnClaimReward = getUnClaimReward; exports.mulDiv = mulDiv; exports.mulShr = mulShr; exports.positionByPoolFilter = positionByPoolFilter; exports.pow = pow; exports.q64ToDecimal = q64ToDecimal; exports.shlDiv = shlDiv; exports.unwrapSOLInstruction = unwrapSOLInstruction; exports.vestingByPositionFilter = vestingByPositionFilter; exports.wrapSOLInstruction = wrapSOLInstruction;
8177
8183
  //# sourceMappingURL=index.js.map