@meteora-ag/cp-amm-sdk 1.0.1-rc.32 → 1.0.1-rc.33
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 +5 -9
- package/dist/index.d.ts +5 -9
- package/dist/index.js +33 -78
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +50 -95
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -6371,10 +6371,10 @@ var FeeSchedulerMode = /* @__PURE__ */ ((FeeSchedulerMode2) => {
|
|
|
6371
6371
|
FeeSchedulerMode2[FeeSchedulerMode2["Exponential"] = 1] = "Exponential";
|
|
6372
6372
|
return FeeSchedulerMode2;
|
|
6373
6373
|
})(FeeSchedulerMode || {});
|
|
6374
|
-
var CollectFeeMode = /* @__PURE__ */ ((
|
|
6375
|
-
|
|
6376
|
-
|
|
6377
|
-
return
|
|
6374
|
+
var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode2) => {
|
|
6375
|
+
CollectFeeMode2[CollectFeeMode2["BothToken"] = 0] = "BothToken";
|
|
6376
|
+
CollectFeeMode2[CollectFeeMode2["OnlyB"] = 1] = "OnlyB";
|
|
6377
|
+
return CollectFeeMode2;
|
|
6378
6378
|
})(CollectFeeMode || {});
|
|
6379
6379
|
var TradeDirection = /* @__PURE__ */ ((TradeDirection2) => {
|
|
6380
6380
|
TradeDirection2[TradeDirection2["AtoB"] = 0] = "AtoB";
|
|
@@ -6745,14 +6745,6 @@ function pow(base, exp) {
|
|
|
6745
6745
|
// src/math/mathUtils.ts
|
|
6746
6746
|
import { BN as BN3 } from "@coral-xyz/anchor";
|
|
6747
6747
|
import Decimal from "decimal.js";
|
|
6748
|
-
function mulShr(x, y, offset, rounding) {
|
|
6749
|
-
const denominator = new BN3(1).shln(offset);
|
|
6750
|
-
return mulDiv(x, y, denominator, rounding);
|
|
6751
|
-
}
|
|
6752
|
-
function shlDiv(x, y, offset, rounding) {
|
|
6753
|
-
const scale = new BN3(1).shln(offset);
|
|
6754
|
-
return mulDiv(x, scale, y, rounding);
|
|
6755
|
-
}
|
|
6756
6748
|
function mulDiv(x, y, denominator, rounding) {
|
|
6757
6749
|
const { div, mod } = x.mul(y).divmod(denominator);
|
|
6758
6750
|
if (rounding == 0 /* Up */ && !mod.isZero()) {
|
|
@@ -6775,7 +6767,6 @@ function decimalToQ64(num) {
|
|
|
6775
6767
|
|
|
6776
6768
|
// src/helpers/curve.ts
|
|
6777
6769
|
import { BN as BN4 } from "@coral-xyz/anchor";
|
|
6778
|
-
import Decimal2 from "decimal.js";
|
|
6779
6770
|
function getNextSqrtPrice(amount, sqrtPrice, liquidity, aToB) {
|
|
6780
6771
|
let result;
|
|
6781
6772
|
if (aToB) {
|
|
@@ -6788,65 +6779,35 @@ function getNextSqrtPrice(amount, sqrtPrice, liquidity, aToB) {
|
|
|
6788
6779
|
}
|
|
6789
6780
|
return result;
|
|
6790
6781
|
}
|
|
6791
|
-
function
|
|
6792
|
-
const
|
|
6793
|
-
const
|
|
6794
|
-
|
|
6795
|
-
const result = shlDiv(prod, denominator, SCALE_OFFSET, rounding);
|
|
6796
|
-
return result.shrn(SCALE_OFFSET);
|
|
6797
|
-
}
|
|
6798
|
-
function getDeltaAmountB(lowerSqrtPrice, upperSqrtPrice, liquidity, rounding) {
|
|
6799
|
-
const deltaSqrtPrice = upperSqrtPrice.sub(lowerSqrtPrice);
|
|
6800
|
-
const prod = liquidity.mul(deltaSqrtPrice);
|
|
6801
|
-
let result;
|
|
6802
|
-
if (rounding == 0 /* Up */) {
|
|
6803
|
-
const denominator = new BN4(1).shln(SCALE_OFFSET * 2);
|
|
6804
|
-
result = divCeil(prod, denominator);
|
|
6805
|
-
} else {
|
|
6806
|
-
result = prod.shrn(SCALE_OFFSET * 2);
|
|
6807
|
-
}
|
|
6808
|
-
return result;
|
|
6809
|
-
}
|
|
6810
|
-
function getLiquidityDeltaFromAmountA(maxAmountA, lowerSqrtPrice, upperSqrtPrice) {
|
|
6811
|
-
const prod = new Decimal2(
|
|
6812
|
-
maxAmountA.mul(upperSqrtPrice.mul(lowerSqrtPrice)).toString()
|
|
6813
|
-
);
|
|
6814
|
-
const delta = new Decimal2(upperSqrtPrice.sub(lowerSqrtPrice).toString());
|
|
6815
|
-
return new BN4(prod.div(delta).floor().toFixed());
|
|
6782
|
+
function getLiquidityDeltaFromAmountA(amountA, lowerSqrtPrice, upperSqrtPrice) {
|
|
6783
|
+
const product = amountA.mul(lowerSqrtPrice).mul(upperSqrtPrice);
|
|
6784
|
+
const denominator = upperSqrtPrice.sub(lowerSqrtPrice);
|
|
6785
|
+
return product.div(denominator);
|
|
6816
6786
|
}
|
|
6817
|
-
function getLiquidityDeltaFromAmountB(
|
|
6818
|
-
const denominator =
|
|
6819
|
-
|
|
6820
|
-
);
|
|
6821
|
-
const prod = new Decimal2(maxAmountB.toString()).mul(
|
|
6822
|
-
Decimal2.pow(2, SCALE_OFFSET * 2)
|
|
6823
|
-
);
|
|
6824
|
-
return new BN4(prod.div(denominator).floor().toFixed());
|
|
6787
|
+
function getLiquidityDeltaFromAmountB(amountB, lowerSqrtPrice, upperSqrtPrice) {
|
|
6788
|
+
const denominator = upperSqrtPrice.sub(lowerSqrtPrice);
|
|
6789
|
+
const product = amountB.shln(128);
|
|
6790
|
+
return product.div(denominator);
|
|
6825
6791
|
}
|
|
6826
6792
|
function getAmountAFromLiquidityDelta(liquidity, currentSqrtPrice, maxSqrtPrice, rounding) {
|
|
6827
|
-
const
|
|
6828
|
-
new Decimal2(maxSqrtPrice.sub(currentSqrtPrice).toString())
|
|
6829
|
-
);
|
|
6793
|
+
const product = liquidity.mul(maxSqrtPrice.sub(currentSqrtPrice));
|
|
6830
6794
|
const denominator = currentSqrtPrice.mul(maxSqrtPrice);
|
|
6831
|
-
const result = prod.mul(Decimal2.pow(2, 64)).div(new Decimal2(denominator.toString()));
|
|
6832
6795
|
if (rounding == 0 /* Up */) {
|
|
6833
|
-
return
|
|
6796
|
+
return product.add(denominator.sub(new BN4(1))).div(denominator);
|
|
6834
6797
|
}
|
|
6835
|
-
return
|
|
6798
|
+
return product.div(denominator);
|
|
6836
6799
|
}
|
|
6837
6800
|
function getAmountBFromLiquidityDelta(liquidity, currentSqrtPrice, minSqrtPrice, rounding) {
|
|
6838
|
-
const
|
|
6839
|
-
const
|
|
6840
|
-
|
|
6841
|
-
);
|
|
6801
|
+
const one = new BN4(1).shln(128);
|
|
6802
|
+
const deltaPrice = currentSqrtPrice.sub(minSqrtPrice);
|
|
6803
|
+
const result = liquidity.mul(deltaPrice);
|
|
6842
6804
|
if (rounding == 0 /* Up */) {
|
|
6843
|
-
return
|
|
6805
|
+
return result.add(one.sub(new BN4(1))).div(one);
|
|
6844
6806
|
}
|
|
6845
|
-
return
|
|
6807
|
+
return result.shrn(128);
|
|
6846
6808
|
}
|
|
6847
6809
|
|
|
6848
6810
|
// src/helpers/fee.ts
|
|
6849
|
-
import Decimal3 from "decimal.js";
|
|
6850
6811
|
function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reductionFactor) {
|
|
6851
6812
|
let feeNumerator;
|
|
6852
6813
|
if (feeSchedulerMode == 0 /* Linear */) {
|
|
@@ -6860,14 +6821,12 @@ function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reduct
|
|
|
6860
6821
|
return feeNumerator;
|
|
6861
6822
|
}
|
|
6862
6823
|
function getDynamicFeeNumerator(volatilityAccumulator, binStep, variableFeeControl) {
|
|
6863
|
-
|
|
6864
|
-
|
|
6865
|
-
|
|
6866
|
-
const squareVfaBin =
|
|
6867
|
-
const vFee =
|
|
6868
|
-
return new BN5(
|
|
6869
|
-
vFee.add(99999999999).div(1e11).floor().toFixed()
|
|
6870
|
-
);
|
|
6824
|
+
if (variableFeeControl.isZero()) {
|
|
6825
|
+
return new BN5(0);
|
|
6826
|
+
}
|
|
6827
|
+
const squareVfaBin = volatilityAccumulator.mul(new BN5(binStep)).pow(new BN5(2));
|
|
6828
|
+
const vFee = variableFeeControl.mul(squareVfaBin);
|
|
6829
|
+
return vFee.add(new BN5(99999999999)).div(new BN5(1e11));
|
|
6871
6830
|
}
|
|
6872
6831
|
function getFeeNumerator(currentPoint, activationPoint, numberOfPeriod, periodFrequency, feeSchedulerMode, cliffFeeNumerator, reductionFactor, dynamicFeeParams) {
|
|
6873
6832
|
if (Number(periodFrequency) == 0) {
|
|
@@ -6918,12 +6877,12 @@ function getSwapAmount(inAmount, sqrtPrice, liquidity, tradeFeeNumerator, aToB,
|
|
|
6918
6877
|
totalFee = getTotalFeeOnAmount(inAmount, tradeFeeNumerator);
|
|
6919
6878
|
actualInAmount = inAmount.sub(totalFee);
|
|
6920
6879
|
}
|
|
6921
|
-
const outAmount = aToB ?
|
|
6880
|
+
const outAmount = aToB ? getAmountBFromLiquidityDelta(
|
|
6922
6881
|
getNextSqrtPrice(actualInAmount, sqrtPrice, liquidity, true),
|
|
6923
6882
|
sqrtPrice,
|
|
6924
6883
|
liquidity,
|
|
6925
6884
|
1 /* Down */
|
|
6926
|
-
) :
|
|
6885
|
+
) : getAmountBFromLiquidityDelta(
|
|
6927
6886
|
sqrtPrice,
|
|
6928
6887
|
getNextSqrtPrice(actualInAmount, sqrtPrice, liquidity, false),
|
|
6929
6888
|
liquidity,
|
|
@@ -7010,7 +6969,7 @@ var getEstimatedComputeUnitIxWithBuffer = (connection, instructions, feePayer, b
|
|
|
7010
6969
|
|
|
7011
6970
|
// src/helpers/utils.ts
|
|
7012
6971
|
import { BN as BN6 } from "@coral-xyz/anchor";
|
|
7013
|
-
import
|
|
6972
|
+
import Decimal2 from "decimal.js";
|
|
7014
6973
|
var getMaxAmountWithSlippage = (amount, rate) => {
|
|
7015
6974
|
const slippage = (100 + rate) / 100 * BASIS_POINT_MAX;
|
|
7016
6975
|
return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
|
|
@@ -7021,20 +6980,20 @@ var getMinAmountWithSlippage = (amount, rate) => {
|
|
|
7021
6980
|
};
|
|
7022
6981
|
var getPriceImpact = (actualAmount, idealAmount) => {
|
|
7023
6982
|
const diff = idealAmount.sub(actualAmount);
|
|
7024
|
-
return new
|
|
6983
|
+
return new Decimal2(diff.toString()).div(new Decimal2(idealAmount.toString())).mul(100).toNumber();
|
|
7025
6984
|
};
|
|
7026
6985
|
var getPriceFromSqrtPrice = (sqrtPrice, tokenADecimal, tokenBDecimal) => {
|
|
7027
|
-
const decimalSqrtPrice = new
|
|
7028
|
-
const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new
|
|
6986
|
+
const decimalSqrtPrice = new Decimal2(sqrtPrice.toString());
|
|
6987
|
+
const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new Decimal2(__pow(10, tokenADecimal - tokenBDecimal))).div(Decimal2.pow(2, 128)).toString();
|
|
7029
6988
|
return price;
|
|
7030
6989
|
};
|
|
7031
6990
|
var getSqrtPriceFromPrice = (price, tokenADecimal, tokenBDecimal) => {
|
|
7032
|
-
const decimalPrice = new
|
|
6991
|
+
const decimalPrice = new Decimal2(price);
|
|
7033
6992
|
const adjustedByDecimals = decimalPrice.div(
|
|
7034
|
-
new
|
|
6993
|
+
new Decimal2(__pow(10, tokenADecimal - tokenBDecimal))
|
|
7035
6994
|
);
|
|
7036
|
-
const sqrtValue =
|
|
7037
|
-
const sqrtValueQ64 = sqrtValue.mul(
|
|
6995
|
+
const sqrtValue = Decimal2.sqrt(adjustedByDecimals);
|
|
6996
|
+
const sqrtValueQ64 = sqrtValue.mul(Decimal2.pow(2, 64));
|
|
7038
6997
|
return new BN6(sqrtValueQ64.floor().toFixed());
|
|
7039
6998
|
};
|
|
7040
6999
|
var getUnClaimReward = (poolState, positionState) => {
|
|
@@ -7074,28 +7033,28 @@ var vestingByPositionFilter = (position) => {
|
|
|
7074
7033
|
|
|
7075
7034
|
// src/helpers/priceMath.ts
|
|
7076
7035
|
import { BN as BN7 } from "@coral-xyz/anchor";
|
|
7077
|
-
import
|
|
7036
|
+
import Decimal3 from "decimal.js";
|
|
7078
7037
|
function calculateInitSqrtPrice(tokenAAmount, tokenBAmount, minSqrtPrice, maxSqrtPrice) {
|
|
7079
7038
|
if (tokenAAmount.isZero() || tokenBAmount.isZero()) {
|
|
7080
7039
|
throw new Error("Amount cannot be zero");
|
|
7081
7040
|
}
|
|
7082
|
-
const amountADecimal = new
|
|
7083
|
-
const amountBDecimal = new
|
|
7084
|
-
const minSqrtPriceDecimal = new
|
|
7085
|
-
|
|
7041
|
+
const amountADecimal = new Decimal3(tokenAAmount.toString());
|
|
7042
|
+
const amountBDecimal = new Decimal3(tokenBAmount.toString());
|
|
7043
|
+
const minSqrtPriceDecimal = new Decimal3(minSqrtPrice.toString()).div(
|
|
7044
|
+
Decimal3.pow(2, 64)
|
|
7086
7045
|
);
|
|
7087
|
-
const maxSqrtPriceDecimal = new
|
|
7088
|
-
|
|
7046
|
+
const maxSqrtPriceDecimal = new Decimal3(maxSqrtPrice.toString()).div(
|
|
7047
|
+
Decimal3.pow(2, 64)
|
|
7089
7048
|
);
|
|
7090
|
-
const x = new
|
|
7049
|
+
const x = new Decimal3(1).div(maxSqrtPriceDecimal);
|
|
7091
7050
|
const y = amountBDecimal.div(amountADecimal);
|
|
7092
7051
|
const xy = x.mul(y);
|
|
7093
7052
|
const paMinusXY = minSqrtPriceDecimal.sub(xy);
|
|
7094
7053
|
const xyMinusPa = xy.sub(minSqrtPriceDecimal);
|
|
7095
|
-
const fourY = new
|
|
7054
|
+
const fourY = new Decimal3(4).mul(y);
|
|
7096
7055
|
const discriminant = xyMinusPa.mul(xyMinusPa).add(fourY);
|
|
7097
7056
|
const sqrtDiscriminant = discriminant.sqrt();
|
|
7098
|
-
const result = paMinusXY.add(sqrtDiscriminant).div(new
|
|
7057
|
+
const result = paMinusXY.add(sqrtDiscriminant).div(new Decimal3(2)).mul(Decimal3.pow(2, 64));
|
|
7099
7058
|
return new BN7(result.floor().toFixed());
|
|
7100
7059
|
}
|
|
7101
7060
|
|
|
@@ -7755,15 +7714,15 @@ var CpAmm = class {
|
|
|
7755
7714
|
return {
|
|
7756
7715
|
liquidityDelta,
|
|
7757
7716
|
outAmountA: tokenATokenInfo ? calculateTransferFeeExcludedAmount(
|
|
7758
|
-
|
|
7717
|
+
amountA,
|
|
7759
7718
|
tokenATokenInfo.mint,
|
|
7760
7719
|
tokenATokenInfo.currentEpoch
|
|
7761
|
-
).amount :
|
|
7720
|
+
).amount : amountA,
|
|
7762
7721
|
outAmountB: tokenBTokenInfo ? calculateTransferFeeExcludedAmount(
|
|
7763
|
-
|
|
7722
|
+
amountB,
|
|
7764
7723
|
tokenBTokenInfo.mint,
|
|
7765
7724
|
tokenBTokenInfo.currentEpoch
|
|
7766
|
-
).amount :
|
|
7725
|
+
).amount : amountB
|
|
7767
7726
|
};
|
|
7768
7727
|
});
|
|
7769
7728
|
}
|
|
@@ -8966,8 +8925,6 @@ export {
|
|
|
8966
8925
|
getAmountAFromLiquidityDelta,
|
|
8967
8926
|
getAmountBFromLiquidityDelta,
|
|
8968
8927
|
getBaseFeeNumerator,
|
|
8969
|
-
getDeltaAmountA,
|
|
8970
|
-
getDeltaAmountB,
|
|
8971
8928
|
getDynamicFeeNumerator,
|
|
8972
8929
|
getEstimatedComputeUnitIxWithBuffer,
|
|
8973
8930
|
getEstimatedComputeUnitUsageWithBuffer,
|
|
@@ -8990,11 +8947,9 @@ export {
|
|
|
8990
8947
|
getTokenProgram,
|
|
8991
8948
|
getUnClaimReward,
|
|
8992
8949
|
mulDiv,
|
|
8993
|
-
mulShr,
|
|
8994
8950
|
positionByPoolFilter,
|
|
8995
8951
|
pow,
|
|
8996
8952
|
q64ToDecimal,
|
|
8997
|
-
shlDiv,
|
|
8998
8953
|
unwrapSOLInstruction,
|
|
8999
8954
|
vestingByPositionFilter,
|
|
9000
8955
|
wrapSOLInstruction
|