@meteora-ag/cp-amm-sdk 1.1.0 → 1.1.2

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
@@ -7091,10 +7091,10 @@ import {
7091
7091
  function getTokenProgram(flag) {
7092
7092
  return flag == 0 ? TOKEN_PROGRAM_ID : TOKEN_2022_PROGRAM_ID;
7093
7093
  }
7094
- var getTokenDecimals = (connection, mint) => __async(null, null, function* () {
7094
+ var getTokenDecimals = (connection, mint) => __async(void 0, null, function* () {
7095
7095
  return (yield getMint(connection, mint)).decimals;
7096
7096
  });
7097
- var getOrCreateATAInstruction = (_0, _1, _2, ..._3) => __async(null, [_0, _1, _2, ..._3], function* (connection, tokenMint, owner, payer = owner, allowOwnerOffCurve = true, tokenProgram) {
7097
+ var getOrCreateATAInstruction = (_0, _1, _2, ..._3) => __async(void 0, [_0, _1, _2, ..._3], function* (connection, tokenMint, owner, payer = owner, allowOwnerOffCurve = true, tokenProgram) {
7098
7098
  const toAccount = getAssociatedTokenAddressSync(
7099
7099
  tokenMint,
7100
7100
  owner,
@@ -7140,7 +7140,7 @@ var wrapSOLInstruction = (from, to, amount) => {
7140
7140
  })
7141
7141
  ];
7142
7142
  };
7143
- var unwrapSOLInstruction = (_0, ..._1) => __async(null, [_0, ..._1], function* (owner, receiver = owner, allowOwnerOffCurve = true) {
7143
+ var unwrapSOLInstruction = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (owner, receiver = owner, allowOwnerOffCurve = true) {
7144
7144
  const wSolATAAccount = getAssociatedTokenAddressSync(
7145
7145
  NATIVE_MINT,
7146
7146
  owner,
@@ -7419,7 +7419,10 @@ function getNextSqrtPriceFromOutput(sqrtPrice, liquidity, outAmount, isB) {
7419
7419
 
7420
7420
  // src/helpers/fee.ts
7421
7421
  import Decimal2 from "decimal.js";
7422
- function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reductionFactor) {
7422
+ function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reductionFactor, periodFrequency) {
7423
+ if (periodFrequency.eq(new BN5(0)) || period.eq(new BN5(0))) {
7424
+ return cliffFeeNumerator;
7425
+ }
7423
7426
  let feeNumerator;
7424
7427
  if (feeSchedulerMode == 0 /* Linear */) {
7425
7428
  feeNumerator = cliffFeeNumerator.sub(period.mul(reductionFactor));
@@ -7451,7 +7454,8 @@ function getFeeNumerator(currentPoint, activationPoint, numberOfPeriod, periodFr
7451
7454
  feeSchedulerMode,
7452
7455
  cliffFeeNumerator,
7453
7456
  period,
7454
- reductionFactor
7457
+ reductionFactor,
7458
+ periodFrequency
7455
7459
  );
7456
7460
  if (dynamicFeeParams) {
7457
7461
  const { volatilityAccumulator, binStep, variableFeeControl } = dynamicFeeParams;
@@ -7770,7 +7774,7 @@ import {
7770
7774
  TransactionMessage,
7771
7775
  VersionedTransaction
7772
7776
  } from "@solana/web3.js";
7773
- var getSimulationComputeUnits = (connection, instructions, payer, lookupTables, commitment = "confirmed") => __async(null, null, function* () {
7777
+ var getSimulationComputeUnits = (connection, instructions, payer, lookupTables, commitment = "confirmed") => __async(void 0, null, function* () {
7774
7778
  var _a, _b, _c;
7775
7779
  const testInstructions = [
7776
7780
  // Set an arbitrarily high number in simulation
@@ -7802,7 +7806,7 @@ var getSimulationComputeUnits = (connection, instructions, payer, lookupTables,
7802
7806
  }
7803
7807
  return rpcResponse.value.unitsConsumed || null;
7804
7808
  });
7805
- var getEstimatedComputeUnitUsageWithBuffer = (connection, instructions, feePayer, buffer) => __async(null, null, function* () {
7809
+ var getEstimatedComputeUnitUsageWithBuffer = (connection, instructions, feePayer, buffer) => __async(void 0, null, function* () {
7806
7810
  if (!buffer) {
7807
7811
  buffer = 0.1;
7808
7812
  }
@@ -7825,7 +7829,7 @@ var getEstimatedComputeUnitUsageWithBuffer = (connection, instructions, feePayer
7825
7829
  }
7826
7830
  return estimatedComputeUnitUsage + extraComputeUnitBuffer;
7827
7831
  });
7828
- var getEstimatedComputeUnitIxWithBuffer = (connection, instructions, feePayer, buffer) => __async(null, null, function* () {
7832
+ var getEstimatedComputeUnitIxWithBuffer = (connection, instructions, feePayer, buffer) => __async(void 0, null, function* () {
7829
7833
  const units = yield getEstimatedComputeUnitUsageWithBuffer(
7830
7834
  connection,
7831
7835
  instructions,
@@ -7849,13 +7853,41 @@ var getMinAmountWithSlippage = (amount, rate) => {
7849
7853
  const slippage = (100 - rate) / 100 * BASIS_POINT_MAX;
7850
7854
  return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
7851
7855
  };
7852
- var getPriceImpact = (nextSqrtPrice, currentSqrtPrice) => {
7856
+ var getPriceImpact = (amountIn, amountOut, currentSqrtPrice, aToB, tokenADecimal, tokenBDecimal) => {
7857
+ if (amountIn.eq(new BN6(0))) {
7858
+ return new Decimal3(0);
7859
+ }
7860
+ if (amountOut.eq(new BN6(0))) {
7861
+ throw new Error("Amount out must be greater than 0");
7862
+ }
7863
+ const spotPrice = getPriceFromSqrtPrice(
7864
+ currentSqrtPrice,
7865
+ tokenADecimal,
7866
+ tokenBDecimal
7867
+ );
7868
+ const executionPrice = new Decimal3(amountIn.toString()).div(new Decimal3(amountOut.toString())).mul(
7869
+ Decimal3.pow(
7870
+ 10,
7871
+ aToB ? tokenBDecimal - tokenADecimal : tokenADecimal - tokenBDecimal
7872
+ )
7873
+ );
7874
+ let priceImpact;
7875
+ let actualExecutionPrice;
7876
+ if (aToB) {
7877
+ actualExecutionPrice = new Decimal3(1).div(executionPrice);
7878
+ } else {
7879
+ actualExecutionPrice = executionPrice;
7880
+ }
7881
+ priceImpact = actualExecutionPrice.sub(spotPrice).abs().div(spotPrice).mul(100);
7882
+ return priceImpact;
7883
+ };
7884
+ var getPriceChange = (nextSqrtPrice, currentSqrtPrice) => {
7853
7885
  const diff = nextSqrtPrice.pow(new BN6(2)).sub(currentSqrtPrice.pow(new BN6(2))).abs();
7854
7886
  return new Decimal3(diff.toString()).div(new Decimal3(currentSqrtPrice.pow(new BN6(2)).toString())).mul(100).toNumber();
7855
7887
  };
7856
7888
  var getPriceFromSqrtPrice = (sqrtPrice, tokenADecimal, tokenBDecimal) => {
7857
7889
  const decimalSqrtPrice = new Decimal3(sqrtPrice.toString());
7858
- const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new Decimal3(__pow(10, tokenADecimal - tokenBDecimal))).div(Decimal3.pow(2, 128)).toString();
7890
+ const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new Decimal3(__pow(10, tokenADecimal - tokenBDecimal))).div(Decimal3.pow(2, 128));
7859
7891
  return price;
7860
7892
  };
7861
7893
  var getSqrtPriceFromPrice = (price, tokenADecimal, tokenBDecimal) => {
@@ -8837,7 +8869,14 @@ var CpAmm = class {
8837
8869
  swapOutAmount: actualAmountOut,
8838
8870
  minSwapOutAmount,
8839
8871
  totalFee,
8840
- priceImpact: getPriceImpact(nextSqrtPrice, sqrtPriceQ64)
8872
+ priceImpact: getPriceImpact(
8873
+ actualAmountIn,
8874
+ actualAmountOut,
8875
+ sqrtPriceQ64,
8876
+ aToB,
8877
+ params.tokenADecimal,
8878
+ params.tokenBDecimal
8879
+ )
8841
8880
  };
8842
8881
  }
8843
8882
  /**
@@ -8898,7 +8937,15 @@ var CpAmm = class {
8898
8937
  const maxInputAmount = new BN10(
8899
8938
  Math.ceil(actualInputAmount.toNumber() * (1 + slippage / 100))
8900
8939
  );
8901
- const priceImpact = getPriceImpact(swapResult.nextSqrtPrice, sqrtPriceQ64);
8940
+ const priceImpact = getPriceImpact(
8941
+ actualInputAmount,
8942
+ actualAmountOut,
8943
+ sqrtPriceQ64,
8944
+ !bToA,
8945
+ // aToB is the opposite of bToA
8946
+ params.tokenADecimal,
8947
+ params.tokenBDecimal
8948
+ ).toNumber();
8902
8949
  return {
8903
8950
  swapResult,
8904
8951
  inputAmount: actualInputAmount,
@@ -10538,6 +10585,7 @@ export {
10538
10585
  getNextSqrtPriceFromAmountBRoundingUp,
10539
10586
  getNextSqrtPriceFromOutput,
10540
10587
  getOrCreateATAInstruction,
10588
+ getPriceChange,
10541
10589
  getPriceFromSqrtPrice,
10542
10590
  getPriceImpact,
10543
10591
  getSecondKey,