@meteora-ag/cp-amm-sdk 1.2.7 → 1.2.8-rc.0

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
@@ -40,7 +40,7 @@ var __async = (__this, __arguments, generator) => {
40
40
  };
41
41
 
42
42
  // src/CpAmm.ts
43
- import { Program as Program2 } from "@coral-xyz/anchor";
43
+ import { Program } from "@coral-xyz/anchor";
44
44
  import { NATIVE_MINT as NATIVE_MINT2, TOKEN_2022_PROGRAM_ID as TOKEN_2022_PROGRAM_ID3 } from "@solana/spl-token";
45
45
  import invariant from "invariant";
46
46
 
@@ -8824,11 +8824,8 @@ function getTotalFeeNumerator(poolFees, baseFeeNumerator, maxFeeNumerator) {
8824
8824
  const totalFeeNumerator = dynamicFeeNumerator.add(baseFeeNumerator);
8825
8825
  return BN6.min(totalFeeNumerator, maxFeeNumerator);
8826
8826
  }
8827
- function getTotalTradingFeeFromIncludedFeeAmount(program, poolFees, currentPoint, activationPoint, includedFeeAmount, tradeDirection, maxFeeNumerator, initSqrtPrice, currentSqrtPrice) {
8828
- const baseFeeHandler = getBaseFeeHandler(
8829
- program,
8830
- poolFees.baseFee.baseFeeInfo.data
8831
- );
8827
+ function getTotalTradingFeeFromIncludedFeeAmount(poolFees, currentPoint, activationPoint, includedFeeAmount, tradeDirection, maxFeeNumerator, initSqrtPrice, currentSqrtPrice) {
8828
+ const baseFeeHandler = getBaseFeeHandler(poolFees.baseFee.baseFeeInfo.data);
8832
8829
  const baseFeeNumerator = baseFeeHandler.getBaseFeeNumeratorFromIncludedFeeAmount(
8833
8830
  currentPoint,
8834
8831
  activationPoint,
@@ -8839,11 +8836,8 @@ function getTotalTradingFeeFromIncludedFeeAmount(program, poolFees, currentPoint
8839
8836
  );
8840
8837
  return getTotalFeeNumerator(poolFees, baseFeeNumerator, maxFeeNumerator);
8841
8838
  }
8842
- function getTotalTradingFeeFromExcludedFeeAmount(program, poolFees, currentPoint, activationPoint, excludedFeeAmount, tradeDirection, maxFeeNumerator, initSqrtPrice, currentSqrtPrice) {
8843
- const baseFeeHandler = getBaseFeeHandler(
8844
- program,
8845
- poolFees.baseFee.baseFeeInfo.data
8846
- );
8839
+ function getTotalTradingFeeFromExcludedFeeAmount(poolFees, currentPoint, activationPoint, excludedFeeAmount, tradeDirection, maxFeeNumerator, initSqrtPrice, currentSqrtPrice) {
8840
+ const baseFeeHandler = getBaseFeeHandler(poolFees.baseFee.baseFeeInfo.data);
8847
8841
  const baseFeeNumerator = baseFeeHandler.getBaseFeeNumeratorFromExcludedFeeAmount(
8848
8842
  currentPoint,
8849
8843
  activationPoint,
@@ -9192,28 +9186,6 @@ function getMaxBaseFeeNumerator(cliffFeeNumerator) {
9192
9186
  }
9193
9187
 
9194
9188
  // src/math/poolFees/feeTimeScheduler.ts
9195
- function toPodAlignedFeeTimeScheduler(borshFeeScheduler) {
9196
- return {
9197
- cliffFeeNumerator: borshFeeScheduler.cliffFeeNumerator,
9198
- baseFeeMode: borshFeeScheduler.baseFeeMode,
9199
- padding: new Array(16).fill(0),
9200
- // Assumes default/rust alignment of 16
9201
- numberOfPeriod: borshFeeScheduler.numberOfPeriod,
9202
- periodFrequency: borshFeeScheduler.periodFrequency,
9203
- reductionFactor: borshFeeScheduler.reductionFactor
9204
- };
9205
- }
9206
- function toBorshFeeTimeScheduler(podAlignedFeeScheduler) {
9207
- return {
9208
- cliffFeeNumerator: podAlignedFeeScheduler.cliffFeeNumerator,
9209
- numberOfPeriod: podAlignedFeeScheduler.numberOfPeriod,
9210
- periodFrequency: podAlignedFeeScheduler.periodFrequency,
9211
- reductionFactor: podAlignedFeeScheduler.reductionFactor,
9212
- baseFeeMode: podAlignedFeeScheduler.baseFeeMode,
9213
- padding: new Array(16).fill(0)
9214
- // Assumes default/rust alignment of 16
9215
- };
9216
- }
9217
9189
  function getFeeTimeBaseFeeNumeratorByPeriod(cliffFeeNumerator, numberOfPeriod, period, reductionFactor, feeTimeSchedulerMode) {
9218
9190
  const periodValue = BN9.min(period, new BN9(numberOfPeriod));
9219
9191
  const periodNumber = periodValue.toNumber();
@@ -9536,7 +9508,7 @@ var FeeMarketCapScheduler = class {
9536
9508
  );
9537
9509
  }
9538
9510
  };
9539
- function getBaseFeeHandler(program, rawData) {
9511
+ function getBaseFeeHandler(rawData) {
9540
9512
  const data = Buffer.from(rawData);
9541
9513
  const modeIndex = data.readUInt8(8);
9542
9514
  const baseFeeMode = modeIndex;
@@ -9724,14 +9696,13 @@ function getLiquidityDeltaFromAmountB(amountB, lowerSqrtPrice, upperSqrtPrice) {
9724
9696
  }
9725
9697
 
9726
9698
  // src/math/quote.ts
9727
- function getSwapResultFromExactInput(program, poolState, amountIn, feeMode, tradeDirection, currentPoint) {
9699
+ function getSwapResultFromExactInput(poolState, amountIn, feeMode, tradeDirection, currentPoint) {
9728
9700
  let actualProtocolFee = new BN14(0);
9729
9701
  let actualTradingFee = new BN14(0);
9730
9702
  let actualReferralFee = new BN14(0);
9731
9703
  let actualPartnerFee = new BN14(0);
9732
9704
  const maxFeeNumerator = getMaxFeeNumerator(poolState.version);
9733
9705
  const tradeFeeNumerator = getTotalTradingFeeFromIncludedFeeAmount(
9734
- program,
9735
9706
  poolState.poolFees,
9736
9707
  currentPoint,
9737
9708
  poolState.activationPoint,
@@ -9838,14 +9809,13 @@ function calculateBtoAFromAmountIn(poolState, amountIn) {
9838
9809
  amountLeft: new BN14(0)
9839
9810
  };
9840
9811
  }
9841
- function getSwapResultFromPartialInput(program, poolState, amountIn, feeMode, tradeDirection, currentPoint) {
9812
+ function getSwapResultFromPartialInput(poolState, amountIn, feeMode, tradeDirection, currentPoint) {
9842
9813
  let actualProtocolFee = new BN14(0);
9843
9814
  let actualTradingFee = new BN14(0);
9844
9815
  let actualReferralFee = new BN14(0);
9845
9816
  let actualPartnerFee = new BN14(0);
9846
9817
  const maxFeeNumerator = getMaxFeeNumerator(poolState.version);
9847
9818
  const tradeFeeNumerator = getTotalTradingFeeFromIncludedFeeAmount(
9848
- program,
9849
9819
  poolState.poolFees,
9850
9820
  currentPoint,
9851
9821
  poolState.activationPoint,
@@ -9890,7 +9860,6 @@ function getSwapResultFromPartialInput(program, poolState, amountIn, feeMode, tr
9890
9860
  actualAmountIn = actualAmountIn.sub(amountLeft);
9891
9861
  if (feeMode.feesOnInput) {
9892
9862
  const tradeFeeNumerator2 = getTotalTradingFeeFromExcludedFeeAmount(
9893
- program,
9894
9863
  poolState.poolFees,
9895
9864
  currentPoint,
9896
9865
  poolState.activationPoint,
@@ -10018,7 +9987,7 @@ function calculateBtoAFromPartialAmountIn(poolState, amountIn) {
10018
9987
  amountLeft
10019
9988
  };
10020
9989
  }
10021
- function getSwapResultFromExactOutput(program, poolState, amountOut, feeMode, tradeDirection, currentPoint) {
9990
+ function getSwapResultFromExactOutput(poolState, amountOut, feeMode, tradeDirection, currentPoint) {
10022
9991
  let actualProtocolFee = new BN14(0);
10023
9992
  let actualTradingFee = new BN14(0);
10024
9993
  let actualReferralFee = new BN14(0);
@@ -10029,7 +9998,6 @@ function getSwapResultFromExactOutput(program, poolState, amountOut, feeMode, tr
10029
9998
  includedFeeAmountOut = amountOut;
10030
9999
  } else {
10031
10000
  const tradeFeeNumerator = getTotalTradingFeeFromExcludedFeeAmount(
10032
- program,
10033
10001
  poolState.poolFees,
10034
10002
  currentPoint,
10035
10003
  poolState.activationPoint,
@@ -10071,7 +10039,6 @@ function getSwapResultFromExactOutput(program, poolState, amountOut, feeMode, tr
10071
10039
  let includedFeeInputAmount;
10072
10040
  if (feeMode.feesOnInput) {
10073
10041
  const tradeFeeNumerator = getTotalTradingFeeFromExcludedFeeAmount(
10074
- program,
10075
10042
  poolState.poolFees,
10076
10043
  currentPoint,
10077
10044
  poolState.activationPoint,
@@ -10153,7 +10120,7 @@ function calculateBtoAFromAmountOut(poolState, amountOut) {
10153
10120
  nextSqrtPrice
10154
10121
  };
10155
10122
  }
10156
- function swapQuoteExactInput(program, pool, currentPoint, amountIn, slippage, aToB, hasReferral, tokenADecimal, tokenBDecimal, inputTokenInfo, outputTokenInfo) {
10123
+ function swapQuoteExactInput(pool, currentPoint, amountIn, slippage, aToB, hasReferral, tokenADecimal, tokenBDecimal, inputTokenInfo, outputTokenInfo) {
10157
10124
  if (amountIn.lte(new BN14(0))) {
10158
10125
  throw new Error("Amount in must be greater than 0");
10159
10126
  }
@@ -10171,7 +10138,6 @@ function swapQuoteExactInput(program, pool, currentPoint, amountIn, slippage, aT
10171
10138
  ).amount;
10172
10139
  }
10173
10140
  const swapResult = getSwapResultFromExactInput(
10174
- program,
10175
10141
  pool,
10176
10142
  actualAmountIn,
10177
10143
  feeMode,
@@ -10204,7 +10170,7 @@ function swapQuoteExactInput(program, pool, currentPoint, amountIn, slippage, aT
10204
10170
  priceImpact
10205
10171
  });
10206
10172
  }
10207
- function swapQuoteExactOutput(program, pool, currentPoint, amountOut, slippage, aToB, hasReferral, tokenADecimal, tokenBDecimal, inputTokenInfo, outputTokenInfo) {
10173
+ function swapQuoteExactOutput(pool, currentPoint, amountOut, slippage, aToB, hasReferral, tokenADecimal, tokenBDecimal, inputTokenInfo, outputTokenInfo) {
10208
10174
  if (amountOut.lte(new BN14(0))) {
10209
10175
  throw new Error("Amount out must be greater than 0");
10210
10176
  }
@@ -10222,7 +10188,6 @@ function swapQuoteExactOutput(program, pool, currentPoint, amountOut, slippage,
10222
10188
  ).amount;
10223
10189
  }
10224
10190
  const swapResult = getSwapResultFromExactOutput(
10225
- program,
10226
10191
  pool,
10227
10192
  actualAmountOut,
10228
10193
  feeMode,
@@ -10255,7 +10220,7 @@ function swapQuoteExactOutput(program, pool, currentPoint, amountOut, slippage,
10255
10220
  priceImpact
10256
10221
  });
10257
10222
  }
10258
- function swapQuotePartialInput(program, pool, currentPoint, amountIn, slippage, aToB, hasReferral, tokenADecimal, tokenBDecimal, inputTokenInfo, outputTokenInfo) {
10223
+ function swapQuotePartialInput(pool, currentPoint, amountIn, slippage, aToB, hasReferral, tokenADecimal, tokenBDecimal, inputTokenInfo, outputTokenInfo) {
10259
10224
  if (amountIn.lte(new BN14(0))) {
10260
10225
  throw new Error("Amount in must be greater than 0");
10261
10226
  }
@@ -10273,7 +10238,6 @@ function swapQuotePartialInput(program, pool, currentPoint, amountIn, slippage,
10273
10238
  ).amount;
10274
10239
  }
10275
10240
  const swapResult = getSwapResultFromPartialInput(
10276
- program,
10277
10241
  pool,
10278
10242
  actualAmountIn,
10279
10243
  feeMode,
@@ -10908,7 +10872,7 @@ function getDynamicFeeParams(baseFeeBps, maxPriceChangeBps = MAX_PRICE_CHANGE_BP
10908
10872
  import BN19, { min as min2, max } from "bn.js";
10909
10873
  var CpAmm = class {
10910
10874
  constructor(connection) {
10911
- this._program = new Program2(cp_amm_default, {
10875
+ this._program = new Program(cp_amm_default, {
10912
10876
  connection
10913
10877
  });
10914
10878
  this.poolAuthority = derivePoolAuthority();
@@ -11396,9 +11360,7 @@ var CpAmm = class {
11396
11360
  */
11397
11361
  fetchConfigState(config) {
11398
11362
  return __async(this, null, function* () {
11399
- const configState = yield this._program.account.config.fetchNullable(
11400
- config
11401
- );
11363
+ const configState = yield this._program.account.config.fetchNullable(config);
11402
11364
  invariant(configState, `Config account: ${config} not found`);
11403
11365
  return configState;
11404
11366
  });
@@ -11464,13 +11426,56 @@ var CpAmm = class {
11464
11426
  */
11465
11427
  fetchPositionState(position) {
11466
11428
  return __async(this, null, function* () {
11467
- const positionState = yield this._program.account.position.fetchNullable(
11468
- position
11469
- );
11429
+ const positionState = yield this._program.account.position.fetchNullable(position);
11470
11430
  invariant(positionState, `Position account: ${position} not found`);
11471
11431
  return positionState;
11472
11432
  });
11473
11433
  }
11434
+ /**
11435
+ * Fetches multiple Config states.
11436
+ * @param configs - Array of public keys of config accounts.
11437
+ * @returns Array of parsed ConfigState.
11438
+ */
11439
+ getMultipleConfigs(configs) {
11440
+ return __async(this, null, function* () {
11441
+ const configStates = yield this._program.account.config.fetchMultiple(configs);
11442
+ configStates.forEach((configState, index) => {
11443
+ invariant(configState, `Config account: ${configs[index]} not found`);
11444
+ });
11445
+ return configStates;
11446
+ });
11447
+ }
11448
+ /**
11449
+ * Fetches multiple Pool states.
11450
+ * @param pools - Array of public keys of pool accounts.
11451
+ * @returns Array of parsed PoolState.
11452
+ */
11453
+ getMultiplePools(pools) {
11454
+ return __async(this, null, function* () {
11455
+ const poolStates = yield this._program.account.pool.fetchMultiple(pools);
11456
+ poolStates.forEach((poolState, index) => {
11457
+ invariant(poolState, `Pool account: ${pools[index]} not found`);
11458
+ });
11459
+ return poolStates;
11460
+ });
11461
+ }
11462
+ /**
11463
+ * Fetches multiple Position states.
11464
+ * @param positions - Array of public keys of position accounts.
11465
+ * @returns Array of parsed PositionState.
11466
+ */
11467
+ getMultiplePositions(positions) {
11468
+ return __async(this, null, function* () {
11469
+ const positionStates = yield this._program.account.position.fetchMultiple(positions);
11470
+ positionStates.forEach((positionState, index) => {
11471
+ invariant(
11472
+ positionState,
11473
+ `Position account: ${positions[index]} not found`
11474
+ );
11475
+ });
11476
+ return positionStates;
11477
+ });
11478
+ }
11474
11479
  /**
11475
11480
  * Retrieves all config accounts.
11476
11481
  * @returns Array of config public keys and their states.
@@ -11544,9 +11549,7 @@ var CpAmm = class {
11544
11549
  const positionAddresses = userPositionAccounts.map(
11545
11550
  (account) => derivePositionAddress(account.positionNft)
11546
11551
  );
11547
- const positionStates = yield this._program.account.position.fetchMultiple(
11548
- positionAddresses
11549
- );
11552
+ const positionStates = yield this._program.account.position.fetchMultiple(positionAddresses);
11550
11553
  const positionResult = userPositionAccounts.map((account, index) => {
11551
11554
  const positionState = positionStates[index];
11552
11555
  if (!positionState) return null;
@@ -11691,7 +11694,6 @@ var CpAmm = class {
11691
11694
  const aToB = poolState.tokenAMint.equals(inputTokenMint);
11692
11695
  const currentPoint = activationType ? new BN19(currentTime) : new BN19(currentSlot);
11693
11696
  const swapResult = swapQuoteExactInput(
11694
- this._program,
11695
11697
  poolState,
11696
11698
  currentPoint,
11697
11699
  inAmount,
@@ -11735,7 +11737,6 @@ var CpAmm = class {
11735
11737
  case 0 /* ExactIn */:
11736
11738
  if ("amountIn" in params) {
11737
11739
  return swapQuoteExactInput(
11738
- this._program,
11739
11740
  poolState,
11740
11741
  currentPoint,
11741
11742
  params.amountIn,
@@ -11752,7 +11753,6 @@ var CpAmm = class {
11752
11753
  case 2 /* ExactOut */:
11753
11754
  if ("amountOut" in params) {
11754
11755
  return swapQuoteExactOutput(
11755
- this._program,
11756
11756
  poolState,
11757
11757
  currentPoint,
11758
11758
  params.amountOut,
@@ -11769,7 +11769,6 @@ var CpAmm = class {
11769
11769
  case 1 /* PartialFill */:
11770
11770
  if ("amountIn" in params) {
11771
11771
  return swapQuotePartialInput(
11772
- this._program,
11773
11772
  poolState,
11774
11773
  currentPoint,
11775
11774
  params.amountIn,
@@ -12623,7 +12622,8 @@ var CpAmm = class {
12623
12622
  tokenBMint,
12624
12623
  tokenAProgram,
12625
12624
  tokenBProgram,
12626
- referralTokenAccount
12625
+ referralTokenAccount,
12626
+ receiver
12627
12627
  } = params;
12628
12628
  const [inputTokenProgram, outputTokenProgram] = inputTokenMint.equals(
12629
12629
  tokenAMint
@@ -12635,8 +12635,8 @@ var CpAmm = class {
12635
12635
  instructions: preInstructions
12636
12636
  } = yield this.prepareTokenAccounts({
12637
12637
  payer,
12638
- tokenAOwner: payer,
12639
- tokenBOwner: payer,
12638
+ tokenAOwner: receiver != null ? receiver : payer,
12639
+ tokenBOwner: receiver != null ? receiver : payer,
12640
12640
  tokenAMint: inputTokenMint,
12641
12641
  tokenBMint: outputTokenMint,
12642
12642
  tokenAProgram: inputTokenProgram,
@@ -12644,7 +12644,7 @@ var CpAmm = class {
12644
12644
  });
12645
12645
  if (inputTokenMint.equals(NATIVE_MINT2)) {
12646
12646
  const wrapSOLIx = wrapSOLInstruction(
12647
- payer,
12647
+ receiver != null ? receiver : payer,
12648
12648
  inputTokenAccount,
12649
12649
  BigInt(amountIn.toString())
12650
12650
  );
@@ -12654,7 +12654,7 @@ var CpAmm = class {
12654
12654
  if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
12655
12655
  NATIVE_MINT2.toBase58()
12656
12656
  )) {
12657
- const closeWrappedSOLIx = yield unwrapSOLInstruction(payer);
12657
+ const closeWrappedSOLIx = yield unwrapSOLInstruction(receiver != null ? receiver : payer);
12658
12658
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
12659
12659
  }
12660
12660
  let { poolState } = params;
@@ -12692,7 +12692,7 @@ var CpAmm = class {
12692
12692
  }).accountsPartial({
12693
12693
  poolAuthority: this.poolAuthority,
12694
12694
  pool,
12695
- payer,
12695
+ payer: receiver != null ? receiver : payer,
12696
12696
  inputTokenAccount,
12697
12697
  outputTokenAccount,
12698
12698
  tokenAVault,
@@ -12724,7 +12724,8 @@ var CpAmm = class {
12724
12724
  tokenAProgram,
12725
12725
  tokenBProgram,
12726
12726
  referralTokenAccount,
12727
- swapMode
12727
+ swapMode,
12728
+ receiver
12728
12729
  } = params;
12729
12730
  const [inputTokenProgram, outputTokenProgram] = inputTokenMint.equals(
12730
12731
  tokenAMint
@@ -12736,8 +12737,8 @@ var CpAmm = class {
12736
12737
  instructions: preInstructions
12737
12738
  } = yield this.prepareTokenAccounts({
12738
12739
  payer,
12739
- tokenAOwner: payer,
12740
- tokenBOwner: payer,
12740
+ tokenAOwner: receiver != null ? receiver : payer,
12741
+ tokenBOwner: receiver != null ? receiver : payer,
12741
12742
  tokenAMint: inputTokenMint,
12742
12743
  tokenBMint: outputTokenMint,
12743
12744
  tokenAProgram: inputTokenProgram,
@@ -12755,7 +12756,7 @@ var CpAmm = class {
12755
12756
  if (inputTokenMint.equals(NATIVE_MINT2)) {
12756
12757
  const amount = swapMode === 0 /* ExactIn */ || swapMode === 1 /* PartialFill */ ? amount0 : amount1;
12757
12758
  const wrapSOLIx = wrapSOLInstruction(
12758
- payer,
12759
+ receiver != null ? receiver : payer,
12759
12760
  inputTokenAccount,
12760
12761
  BigInt(amount.toString())
12761
12762
  );
@@ -12765,7 +12766,7 @@ var CpAmm = class {
12765
12766
  if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
12766
12767
  NATIVE_MINT2.toBase58()
12767
12768
  )) {
12768
- const closeWrappedSOLIx = yield unwrapSOLInstruction(payer);
12769
+ const closeWrappedSOLIx = yield unwrapSOLInstruction(receiver != null ? receiver : payer);
12769
12770
  closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
12770
12771
  }
12771
12772
  let { poolState } = params;
@@ -12804,7 +12805,7 @@ var CpAmm = class {
12804
12805
  }).accountsPartial({
12805
12806
  poolAuthority: this.poolAuthority,
12806
12807
  pool,
12807
- payer,
12808
+ payer: receiver != null ? receiver : payer,
12808
12809
  inputTokenAccount,
12809
12810
  outputTokenAccount,
12810
12811
  tokenAVault,
@@ -13778,9 +13779,7 @@ export {
13778
13779
  swapQuoteExactInput,
13779
13780
  swapQuoteExactOutput,
13780
13781
  swapQuotePartialInput,
13781
- toBorshFeeTimeScheduler,
13782
13782
  toNumerator,
13783
- toPodAlignedFeeTimeScheduler,
13784
13783
  unwrapSOLInstruction,
13785
13784
  validateFeeFraction,
13786
13785
  validateFeeMarketCapBaseFeeIsStatic,