@meteora-ag/cp-amm-sdk 1.1.0 → 1.1.2-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
@@ -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,
@@ -7770,7 +7770,7 @@ import {
7770
7770
  TransactionMessage,
7771
7771
  VersionedTransaction
7772
7772
  } from "@solana/web3.js";
7773
- var getSimulationComputeUnits = (connection, instructions, payer, lookupTables, commitment = "confirmed") => __async(null, null, function* () {
7773
+ var getSimulationComputeUnits = (connection, instructions, payer, lookupTables, commitment = "confirmed") => __async(void 0, null, function* () {
7774
7774
  var _a, _b, _c;
7775
7775
  const testInstructions = [
7776
7776
  // Set an arbitrarily high number in simulation
@@ -7802,7 +7802,7 @@ var getSimulationComputeUnits = (connection, instructions, payer, lookupTables,
7802
7802
  }
7803
7803
  return rpcResponse.value.unitsConsumed || null;
7804
7804
  });
7805
- var getEstimatedComputeUnitUsageWithBuffer = (connection, instructions, feePayer, buffer) => __async(null, null, function* () {
7805
+ var getEstimatedComputeUnitUsageWithBuffer = (connection, instructions, feePayer, buffer) => __async(void 0, null, function* () {
7806
7806
  if (!buffer) {
7807
7807
  buffer = 0.1;
7808
7808
  }
@@ -7825,7 +7825,7 @@ var getEstimatedComputeUnitUsageWithBuffer = (connection, instructions, feePayer
7825
7825
  }
7826
7826
  return estimatedComputeUnitUsage + extraComputeUnitBuffer;
7827
7827
  });
7828
- var getEstimatedComputeUnitIxWithBuffer = (connection, instructions, feePayer, buffer) => __async(null, null, function* () {
7828
+ var getEstimatedComputeUnitIxWithBuffer = (connection, instructions, feePayer, buffer) => __async(void 0, null, function* () {
7829
7829
  const units = yield getEstimatedComputeUnitUsageWithBuffer(
7830
7830
  connection,
7831
7831
  instructions,
@@ -7849,13 +7849,41 @@ var getMinAmountWithSlippage = (amount, rate) => {
7849
7849
  const slippage = (100 - rate) / 100 * BASIS_POINT_MAX;
7850
7850
  return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
7851
7851
  };
7852
- var getPriceImpact = (nextSqrtPrice, currentSqrtPrice) => {
7852
+ var getPriceImpact = (amountIn, amountOut, currentSqrtPrice, aToB, tokenADecimal, tokenBDecimal) => {
7853
+ if (amountIn.eq(new BN6(0))) {
7854
+ return new Decimal3(0);
7855
+ }
7856
+ if (amountOut.eq(new BN6(0))) {
7857
+ throw new Error("Amount out must be greater than 0");
7858
+ }
7859
+ const spotPrice = getPriceFromSqrtPrice(
7860
+ currentSqrtPrice,
7861
+ tokenADecimal,
7862
+ tokenBDecimal
7863
+ );
7864
+ const executionPrice = new Decimal3(amountIn.toString()).div(new Decimal3(amountOut.toString())).mul(
7865
+ Decimal3.pow(
7866
+ 10,
7867
+ aToB ? tokenBDecimal - tokenADecimal : tokenADecimal - tokenBDecimal
7868
+ )
7869
+ );
7870
+ let priceImpact;
7871
+ let actualExecutionPrice;
7872
+ if (aToB) {
7873
+ actualExecutionPrice = new Decimal3(1).div(executionPrice);
7874
+ } else {
7875
+ actualExecutionPrice = executionPrice;
7876
+ }
7877
+ priceImpact = actualExecutionPrice.sub(spotPrice).abs().div(spotPrice).mul(100);
7878
+ return priceImpact;
7879
+ };
7880
+ var getPriceChange = (nextSqrtPrice, currentSqrtPrice) => {
7853
7881
  const diff = nextSqrtPrice.pow(new BN6(2)).sub(currentSqrtPrice.pow(new BN6(2))).abs();
7854
7882
  return new Decimal3(diff.toString()).div(new Decimal3(currentSqrtPrice.pow(new BN6(2)).toString())).mul(100).toNumber();
7855
7883
  };
7856
7884
  var getPriceFromSqrtPrice = (sqrtPrice, tokenADecimal, tokenBDecimal) => {
7857
7885
  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();
7886
+ const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new Decimal3(__pow(10, tokenADecimal - tokenBDecimal))).div(Decimal3.pow(2, 128));
7859
7887
  return price;
7860
7888
  };
7861
7889
  var getSqrtPriceFromPrice = (price, tokenADecimal, tokenBDecimal) => {
@@ -8837,7 +8865,14 @@ var CpAmm = class {
8837
8865
  swapOutAmount: actualAmountOut,
8838
8866
  minSwapOutAmount,
8839
8867
  totalFee,
8840
- priceImpact: getPriceImpact(nextSqrtPrice, sqrtPriceQ64)
8868
+ priceImpact: getPriceImpact(
8869
+ actualAmountIn,
8870
+ actualAmountOut,
8871
+ sqrtPriceQ64,
8872
+ aToB,
8873
+ params.tokenADecimal,
8874
+ params.tokenBDecimal
8875
+ )
8841
8876
  };
8842
8877
  }
8843
8878
  /**
@@ -8898,7 +8933,15 @@ var CpAmm = class {
8898
8933
  const maxInputAmount = new BN10(
8899
8934
  Math.ceil(actualInputAmount.toNumber() * (1 + slippage / 100))
8900
8935
  );
8901
- const priceImpact = getPriceImpact(swapResult.nextSqrtPrice, sqrtPriceQ64);
8936
+ const priceImpact = getPriceImpact(
8937
+ actualInputAmount,
8938
+ actualAmountOut,
8939
+ sqrtPriceQ64,
8940
+ !bToA,
8941
+ // aToB is the opposite of bToA
8942
+ params.tokenADecimal,
8943
+ params.tokenBDecimal
8944
+ ).toNumber();
8902
8945
  return {
8903
8946
  swapResult,
8904
8947
  inputAmount: actualInputAmount,
@@ -9066,20 +9109,16 @@ var CpAmm = class {
9066
9109
  if (tokenAAmount.eq(new BN10(0)) && tokenBAmount.eq(new BN10(0))) {
9067
9110
  throw new Error("Invalid input amount");
9068
9111
  }
9069
- const actualAmountAIn = tokenAInfo ? tokenAAmount.sub(
9070
- calculateTransferFeeIncludedAmount(
9071
- tokenAAmount,
9072
- tokenAInfo.mint,
9073
- tokenAInfo.currentEpoch
9074
- ).transferFee
9075
- ) : tokenAAmount;
9076
- const actualAmountBIn = tokenBInfo ? tokenBAmount.sub(
9077
- calculateTransferFeeIncludedAmount(
9078
- tokenBAmount,
9079
- tokenBInfo.mint,
9080
- tokenBInfo.currentEpoch
9081
- ).transferFee
9082
- ) : tokenBAmount;
9112
+ const actualAmountAIn = tokenAInfo ? calculateTransferFeeExcludedAmount(
9113
+ tokenAAmount,
9114
+ tokenAInfo.mint,
9115
+ tokenAInfo.currentEpoch
9116
+ ).amount : tokenAAmount;
9117
+ const actualAmountBIn = tokenBInfo ? calculateTransferFeeExcludedAmount(
9118
+ tokenBAmount,
9119
+ tokenBInfo.mint,
9120
+ tokenBInfo.currentEpoch
9121
+ ).amount : tokenBAmount;
9083
9122
  const initSqrtPrice = calculateInitSqrtPrice(
9084
9123
  tokenAAmount,
9085
9124
  tokenBAmount,
@@ -10538,6 +10577,7 @@ export {
10538
10577
  getNextSqrtPriceFromAmountBRoundingUp,
10539
10578
  getNextSqrtPriceFromOutput,
10540
10579
  getOrCreateATAInstruction,
10580
+ getPriceChange,
10541
10581
  getPriceFromSqrtPrice,
10542
10582
  getPriceImpact,
10543
10583
  getSecondKey,