@gmx-io/sdk 2.9.1 → 2.9.3
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/build/cjs/src/clients/v2/index.js +61 -41
- package/build/cjs/src/clients/v2/solana.spec.js +188 -0
- package/build/cjs/src/configs/api.js +4 -0
- package/build/cjs/src/utils/fees/index.js +7 -1
- package/build/cjs/src/utils/fees/priceImpact.js +118 -18
- package/build/cjs/src/utils/gmsol/__tests__/fixtures/nativeMath.json +3171 -0
- package/build/cjs/src/utils/gmsol/__tests__/nativeMath.spec.js +213 -0
- package/build/cjs/src/utils/gmsol/constants.js +12 -0
- package/build/cjs/src/utils/gmsol/fees.js +19 -0
- package/build/cjs/src/utils/gmsol/index.js +66 -0
- package/build/cjs/src/utils/gmsol/markets.js +206 -0
- package/build/cjs/src/utils/gmsol/numbers.js +230 -0
- package/build/cjs/src/utils/gmsol/positions.js +152 -0
- package/build/cjs/src/utils/gmsol/types.js +2 -0
- package/build/cjs/src/utils/http/http.js +3 -4
- package/build/cjs/src/utils/http/httpFallback.js +11 -1
- package/build/cjs/src/utils/http/solana.js +17 -0
- package/build/cjs/src/utils/http/validation.js +21 -1
- package/build/cjs/src/utils/http/validation.spec.js +45 -0
- package/build/cjs/src/utils/markets/__tests__/api.spec.js +53 -0
- package/build/cjs/src/utils/markets/api.js +5 -0
- package/build/cjs/src/utils/markets/utils.js +25 -9
- package/build/cjs/src/utils/orders/api.js +3 -0
- package/build/cjs/src/utils/positions/__tests__/liquidationPrice.spec.js +64 -0
- package/build/cjs/src/utils/positions/api.js +3 -0
- package/build/cjs/src/utils/positions/utils.js +88 -19
- package/build/cjs/src/utils/prices/utils.js +69 -14
- package/build/cjs/src/utils/referrals/api.js +41 -0
- package/build/cjs/src/utils/swap/swapRouting.js +4 -0
- package/build/cjs/src/utils/swap/swapStats.js +6 -1
- package/build/cjs/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1012 -0
- package/build/cjs/src/utils/trade/decrease.js +266 -56
- package/build/cjs/src/utils/trade/decreaseMarginCheck.js +23 -4
- package/build/cjs/src/utils/trade/increase.js +86 -28
- package/build/cjs/src/utils/trade/increaseMarginCheck.js +51 -9
- package/build/cjs/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
- package/build/esm/src/clients/v2/index.js +39 -19
- package/build/esm/src/clients/v2/solana.spec.js +160 -0
- package/build/esm/src/configs/api.js +5 -1
- package/build/esm/src/utils/fees/index.js +7 -1
- package/build/esm/src/utils/fees/priceImpact.js +111 -12
- package/build/esm/src/utils/gmsol/__tests__/fixtures/nativeMath.json +3171 -0
- package/build/esm/src/utils/gmsol/__tests__/nativeMath.spec.js +208 -0
- package/build/esm/src/utils/gmsol/constants.js +9 -0
- package/build/esm/src/utils/gmsol/fees.js +15 -0
- package/build/esm/src/utils/gmsol/index.js +46 -0
- package/build/esm/src/utils/gmsol/markets.js +193 -0
- package/build/esm/src/utils/gmsol/numbers.js +226 -0
- package/build/esm/src/utils/gmsol/positions.js +148 -0
- package/build/esm/src/utils/gmsol/types.js +1 -0
- package/build/esm/src/utils/http/http.js +3 -4
- package/build/esm/src/utils/http/httpFallback.js +11 -1
- package/build/esm/src/utils/http/solana.js +12 -0
- package/build/esm/src/utils/http/validation.js +18 -0
- package/build/esm/src/utils/http/validation.spec.js +43 -0
- package/build/esm/src/utils/markets/__tests__/api.spec.js +53 -0
- package/build/esm/src/utils/markets/api.js +6 -1
- package/build/esm/src/utils/markets/utils.js +23 -8
- package/build/esm/src/utils/orders/api.js +3 -0
- package/build/esm/src/utils/positions/__tests__/liquidationPrice.spec.js +62 -0
- package/build/esm/src/utils/positions/api.js +3 -0
- package/build/esm/src/utils/positions/utils.js +89 -20
- package/build/esm/src/utils/prices/utils.js +70 -15
- package/build/esm/src/utils/referrals/api.js +36 -0
- package/build/esm/src/utils/swap/swapRouting.js +4 -0
- package/build/esm/src/utils/swap/swapStats.js +7 -2
- package/build/esm/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1010 -0
- package/build/esm/src/utils/trade/decrease.js +266 -56
- package/build/esm/src/utils/trade/decreaseMarginCheck.js +23 -4
- package/build/esm/src/utils/trade/increase.js +86 -28
- package/build/esm/src/utils/trade/increaseMarginCheck.js +53 -11
- package/build/esm/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/clients/v2/index.d.ts +21 -12
- package/build/types/src/clients/v2/solana.spec.d.ts +1 -0
- package/build/types/src/utils/fees/index.d.ts +1 -1
- package/build/types/src/utils/fees/priceImpact.d.ts +7 -0
- package/build/types/src/utils/gmsol/__tests__/nativeMath.spec.d.ts +1 -0
- package/build/types/src/utils/gmsol/constants.d.ts +9 -0
- package/build/types/src/utils/gmsol/fees.d.ts +6 -0
- package/build/types/src/utils/gmsol/index.d.ts +38 -0
- package/build/types/src/utils/gmsol/markets.d.ts +49 -0
- package/build/types/src/utils/gmsol/numbers.d.ts +11 -0
- package/build/types/src/utils/gmsol/positions.d.ts +7 -0
- package/build/types/src/utils/gmsol/types.d.ts +105 -0
- package/build/types/src/utils/http/http.d.ts +2 -5
- package/build/types/src/utils/http/httpFallback.d.ts +2 -5
- package/build/types/src/utils/http/solana.d.ts +4 -0
- package/build/types/src/utils/http/types.d.ts +17 -4
- package/build/types/src/utils/http/validation.d.ts +8 -0
- package/build/types/src/utils/http/validation.spec.d.ts +1 -0
- package/build/types/src/utils/markets/hashKeys.d.ts +1 -1
- package/build/types/src/utils/markets/types.d.ts +3 -2
- package/build/types/src/utils/markets/utils.d.ts +6 -2
- package/build/types/src/utils/orders/api.d.ts +2 -2
- package/build/types/src/utils/positions/__tests__/liquidationPrice.spec.d.ts +1 -0
- package/build/types/src/utils/positions/api.d.ts +2 -2
- package/build/types/src/utils/positions/types.d.ts +1 -0
- package/build/types/src/utils/positions/utils.d.ts +6 -1
- package/build/types/src/utils/prices/types.d.ts +16 -0
- package/build/types/src/utils/prices/utils.d.ts +3 -11
- package/build/types/src/utils/referrals/api.d.ts +8 -0
- package/build/types/src/utils/referrals/types.d.ts +5 -0
- package/build/types/src/utils/rpc/createViemRpc.d.ts +25 -3
- package/build/types/src/utils/trade/__tests__/gmsolTradeMath.spec.d.ts +1 -0
- package/build/types/src/utils/trade/decrease.d.ts +5 -0
- package/build/types/src/utils/trade/decreaseMarginCheck.d.ts +2 -0
- package/build/types/src/utils/trade/increase.d.ts +2 -0
- package/build/types/src/utils/trade/increaseMarginCheck.d.ts +2 -0
- package/build/types/src/utils/trade/twapIncreaseMarginCheck.d.ts +1 -0
- package/build/types/src/utils/trade/types.d.ts +6 -0
- package/package.json +10 -1
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMaxNegativeImpactBps = exports.getCappedPriceImpactPercentageFromFees = exports.applyImpactFactor = exports.calculateImpactForCrossoverRebalance = exports.calculateImpactForSameSideRebalance = exports.getPriceImpactUsd = exports.getNextPoolAmountsParams = exports.getPriceImpactForSwap = exports.getProportionalPendingImpactValues = exports.getPriceImpactForPosition = exports.getMaxPositionImpactFactors = exports.capPositionImpactUsdByMaxPriceImpactFactor = exports.capPositionImpactUsdByMaxImpactPool = exports.getCappedPositionImpactUsd = exports.applySwapImpactWithCap = exports.getPriceImpactByAcceptablePrice = void 0;
|
|
3
|
+
exports.getMaxNegativeImpactBps = exports.getCappedPriceImpactPercentageFromFees = exports.applyImpactFactor = exports.calculateImpactForCrossoverRebalance = exports.calculateImpactForSameSideRebalance = exports.getPriceImpactUsd = exports.getNextPoolAmountsParams = exports.getPriceImpactForSwap = exports.getProportionalPendingImpactValues = exports.getPriceImpactForPosition = exports.getMaxPositionImpactFactors = exports.capPositionImpactUsdByMaxPriceImpactFactor = exports.capPositionImpactUsdByMaxImpactPool = exports.getCappedPositionImpactUsd = exports.applySwapImpactWithCap = exports.getPriceImpactByAcceptablePrice = exports.GmsolPriceImpactError = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
const bigmath_1 = require("../bigmath");
|
|
6
|
+
const gmsol_1 = require("../gmsol");
|
|
7
|
+
const constants_1 = require("../gmsol/constants");
|
|
8
|
+
const numbers_1 = require("../gmsol/numbers");
|
|
6
9
|
const markets_1 = require("../markets");
|
|
7
|
-
const
|
|
10
|
+
const numbers_2 = require("../numbers");
|
|
8
11
|
const utils_1 = require("../tokens/utils");
|
|
12
|
+
class GmsolPriceImpactError extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super(...arguments);
|
|
15
|
+
this.name = "GmsolPriceImpactError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.GmsolPriceImpactError = GmsolPriceImpactError;
|
|
9
19
|
function getPriceImpactByAcceptablePrice(p) {
|
|
10
20
|
const { sizeDeltaUsd, acceptablePrice, indexPrice: markPrice, isLong, isIncrease } = p;
|
|
11
21
|
const shouldFlipPriceDiff = isIncrease ? !isLong : isLong;
|
|
12
22
|
const priceDelta = (markPrice - acceptablePrice) * (shouldFlipPriceDiff ? -1n : 1n);
|
|
13
|
-
const acceptablePriceDeltaBps = markPrice === 0n ? 0n : (0,
|
|
23
|
+
const acceptablePriceDeltaBps = markPrice === 0n ? 0n : (0, numbers_2.getBasisPoints)(priceDelta, markPrice);
|
|
14
24
|
const priceImpactDeltaUsd = acceptablePrice === 0n ? 0n : (sizeDeltaUsd * priceDelta) / acceptablePrice;
|
|
15
25
|
const priceImpactDeltaAmount = markPrice === 0n ? 0n : priceImpactDeltaUsd / markPrice;
|
|
16
26
|
return {
|
|
@@ -37,24 +47,30 @@ function applySwapImpactWithCap(marketInfo, token, priceImpactDeltaUsd) {
|
|
|
37
47
|
? marketInfo.swapImpactPoolAmountLong
|
|
38
48
|
: marketInfo.swapImpactPoolAmountShort;
|
|
39
49
|
if (impactDeltaAmount > maxImpactAmount) {
|
|
40
|
-
cappedDiffUsd = bigmath_1.bigMath.mulDiv(impactDeltaAmount - maxImpactAmount, price, (0,
|
|
50
|
+
cappedDiffUsd = bigmath_1.bigMath.mulDiv(impactDeltaAmount - maxImpactAmount, price, (0, numbers_2.expandDecimals)(1, token.decimals));
|
|
41
51
|
impactDeltaAmount = maxImpactAmount;
|
|
42
52
|
}
|
|
43
53
|
}
|
|
44
54
|
else {
|
|
45
55
|
// round negative impactAmount up, this will be deducted from the user
|
|
46
|
-
impactDeltaAmount = (0,
|
|
56
|
+
impactDeltaAmount = (0, numbers_2.roundUpMagnitudeDivision)(priceImpactDeltaUsd * (0, numbers_2.expandDecimals)(1, token.decimals), price);
|
|
47
57
|
}
|
|
48
58
|
return { impactDeltaAmount, cappedDiffUsd };
|
|
49
59
|
}
|
|
50
60
|
exports.applySwapImpactWithCap = applySwapImpactWithCap;
|
|
51
61
|
function getCappedPositionImpactUsd(marketInfo, sizeDeltaUsd, isLong, isIncrease, opts = {}) {
|
|
62
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
63
|
+
sizeDeltaUsd = (0, gmsol_1.getGmsolUsdAmount)(sizeDeltaUsd);
|
|
64
|
+
}
|
|
52
65
|
sizeDeltaUsd = isIncrease ? sizeDeltaUsd : sizeDeltaUsd * -1n;
|
|
53
66
|
const { priceImpactDeltaUsd, balanceWasImproved } = getPriceImpactForPosition(marketInfo, sizeDeltaUsd, isLong, opts);
|
|
54
67
|
if (priceImpactDeltaUsd < 0 && !opts.shouldCapNegativeImpact) {
|
|
55
68
|
return { priceImpactDeltaUsd, balanceWasImproved };
|
|
56
69
|
}
|
|
57
|
-
|
|
70
|
+
let cappedImpactUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeDeltaUsd, priceImpactDeltaUsd);
|
|
71
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
72
|
+
cappedImpactUsd = capPositionImpactUsdByMaxImpactPool(marketInfo, cappedImpactUsd);
|
|
73
|
+
}
|
|
58
74
|
return {
|
|
59
75
|
priceImpactDeltaUsd: cappedImpactUsd,
|
|
60
76
|
balanceWasImproved,
|
|
@@ -77,7 +93,8 @@ exports.capPositionImpactUsdByMaxImpactPool = capPositionImpactUsdByMaxImpactPoo
|
|
|
77
93
|
function capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeDeltaUsd, positionImpactDeltaUsd) {
|
|
78
94
|
const { maxPositiveImpactFactor, maxNegativeImpactFactor } = getMaxPositionImpactFactors(marketInfo);
|
|
79
95
|
const maxPriceImapctFactor = positionImpactDeltaUsd > 0 ? maxPositiveImpactFactor : maxNegativeImpactFactor;
|
|
80
|
-
const
|
|
96
|
+
const maxImpactUsd = (0, numbers_2.applyFactor)(bigmath_1.bigMath.abs(sizeDeltaUsd), maxPriceImapctFactor);
|
|
97
|
+
const maxPriceImpactUsdBasedOnMaxPriceImpactFactor = marketInfo.tradeMath === "gmsol" ? (0, gmsol_1.getGmsolUsdAmount)(maxImpactUsd) : maxImpactUsd;
|
|
81
98
|
if (bigmath_1.bigMath.abs(positionImpactDeltaUsd) > maxPriceImpactUsdBasedOnMaxPriceImpactFactor) {
|
|
82
99
|
positionImpactDeltaUsd = maxPriceImpactUsdBasedOnMaxPriceImpactFactor * (positionImpactDeltaUsd > 0 ? 1n : -1n);
|
|
83
100
|
}
|
|
@@ -87,17 +104,20 @@ exports.capPositionImpactUsdByMaxPriceImpactFactor = capPositionImpactUsdByMaxPr
|
|
|
87
104
|
function getMaxPositionImpactFactors(marketInfo) {
|
|
88
105
|
let maxPositiveImpactFactor = marketInfo.maxPositionImpactFactorPositive;
|
|
89
106
|
const maxNegativeImpactFactor = marketInfo.maxPositionImpactFactorNegative;
|
|
90
|
-
if (maxPositiveImpactFactor > maxNegativeImpactFactor) {
|
|
107
|
+
if (marketInfo.tradeMath !== "gmsol" && maxPositiveImpactFactor > maxNegativeImpactFactor) {
|
|
91
108
|
maxPositiveImpactFactor = maxNegativeImpactFactor;
|
|
92
109
|
}
|
|
93
110
|
return { maxPositiveImpactFactor, maxNegativeImpactFactor };
|
|
94
111
|
}
|
|
95
112
|
exports.getMaxPositionImpactFactors = getMaxPositionImpactFactors;
|
|
96
113
|
function getPriceImpactForPosition(marketInfo, sizeDeltaUsd, isLong, opts = {}) {
|
|
114
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
115
|
+
sizeDeltaUsd = (0, gmsol_1.getGmsolUsdAmount)(sizeDeltaUsd);
|
|
116
|
+
}
|
|
97
117
|
const longInterestUsd = (0, markets_1.getOpenInterestForBalance)(marketInfo, true);
|
|
98
118
|
const shortInterestUsd = (0, markets_1.getOpenInterestForBalance)(marketInfo, false);
|
|
99
119
|
let effectiveUsdDelta = sizeDeltaUsd;
|
|
100
|
-
if (marketInfo.useOpenInterestInTokensForBalance) {
|
|
120
|
+
if (marketInfo.tradeMath !== "gmsol" && marketInfo.useOpenInterestInTokensForBalance) {
|
|
101
121
|
if (sizeDeltaUsd < 0n && opts.sizeDeltaInTokens === undefined) {
|
|
102
122
|
throw new Error(`Missing sizeDeltaInTokens for token-OI decrease market ${marketInfo.marketTokenAddress}`);
|
|
103
123
|
}
|
|
@@ -130,6 +150,7 @@ function getPriceImpactForPosition(marketInfo, sizeDeltaUsd, isLong, opts = {})
|
|
|
130
150
|
exponentFactorPositive: marketInfo.positionImpactExponentFactorPositive,
|
|
131
151
|
exponentFactorNegative: marketInfo.positionImpactExponentFactorNegative,
|
|
132
152
|
fallbackToZero: opts.fallbackToZero,
|
|
153
|
+
tradeMath: marketInfo.tradeMath,
|
|
133
154
|
});
|
|
134
155
|
if (priceImpactDeltaUsd >= 0n) {
|
|
135
156
|
return {
|
|
@@ -159,6 +180,7 @@ function getPriceImpactForPosition(marketInfo, sizeDeltaUsd, isLong, opts = {})
|
|
|
159
180
|
exponentFactorPositive: marketInfo.positionImpactExponentFactorPositive,
|
|
160
181
|
exponentFactorNegative: marketInfo.positionImpactExponentFactorNegative,
|
|
161
182
|
fallbackToZero: opts.fallbackToZero,
|
|
183
|
+
tradeMath: marketInfo.tradeMath,
|
|
162
184
|
});
|
|
163
185
|
if (priceImpactUsdForVirtualInventory < priceImpactDeltaUsd) {
|
|
164
186
|
return {
|
|
@@ -178,7 +200,7 @@ function getVirtualInventoryForPositionImpact(marketInfo) {
|
|
|
178
200
|
return { hasVirtualInventory: false, virtualInventory: 0n };
|
|
179
201
|
}
|
|
180
202
|
let virtualInventory = marketInfo.virtualInventoryForPositions;
|
|
181
|
-
if (marketInfo.useOpenInterestInTokensForBalance) {
|
|
203
|
+
if (marketInfo.tradeMath !== "gmsol" && marketInfo.useOpenInterestInTokensForBalance) {
|
|
182
204
|
if (typeof marketInfo.virtualInventoryForPositionsInTokens !== "bigint") {
|
|
183
205
|
throw new Error(`Missing virtualInventoryForPositionsInTokens for token-OI market ${marketInfo.marketTokenAddress}`);
|
|
184
206
|
}
|
|
@@ -214,6 +236,7 @@ function getPriceImpactForSwap(marketInfo, tokenA, tokenB, usdDeltaTokenA, usdDe
|
|
|
214
236
|
shortPoolAmount: marketInfo.shortPoolAmount,
|
|
215
237
|
longDeltaUsd,
|
|
216
238
|
shortDeltaUsd,
|
|
239
|
+
tradeMath: marketInfo.tradeMath,
|
|
217
240
|
});
|
|
218
241
|
const { priceImpactDeltaUsd, balanceWasImproved } = getPriceImpactUsd({
|
|
219
242
|
currentLongUsd: longPoolUsd,
|
|
@@ -225,6 +248,7 @@ function getPriceImpactForSwap(marketInfo, tokenA, tokenB, usdDeltaTokenA, usdDe
|
|
|
225
248
|
exponentFactorPositive: marketInfo.swapImpactExponentFactor,
|
|
226
249
|
exponentFactorNegative: marketInfo.swapImpactExponentFactor,
|
|
227
250
|
fallbackToZero: opts.fallbackToZero,
|
|
251
|
+
tradeMath: marketInfo.tradeMath,
|
|
228
252
|
});
|
|
229
253
|
if (priceImpactDeltaUsd > 0) {
|
|
230
254
|
return {
|
|
@@ -247,6 +271,7 @@ function getPriceImpactForSwap(marketInfo, tokenA, tokenB, usdDeltaTokenA, usdDe
|
|
|
247
271
|
shortPoolAmount: virtualInventoryShort,
|
|
248
272
|
longDeltaUsd,
|
|
249
273
|
shortDeltaUsd,
|
|
274
|
+
tradeMath: marketInfo.tradeMath,
|
|
250
275
|
});
|
|
251
276
|
const { priceImpactDeltaUsd: priceImpactUsdForVirtualInventory } = getPriceImpactUsd({
|
|
252
277
|
currentLongUsd: virtualInventoryParams.longPoolUsd,
|
|
@@ -258,6 +283,7 @@ function getPriceImpactForSwap(marketInfo, tokenA, tokenB, usdDeltaTokenA, usdDe
|
|
|
258
283
|
exponentFactorPositive: marketInfo.swapImpactExponentFactor,
|
|
259
284
|
exponentFactorNegative: marketInfo.swapImpactExponentFactor,
|
|
260
285
|
fallbackToZero: opts.fallbackToZero,
|
|
286
|
+
tradeMath: marketInfo.tradeMath,
|
|
261
287
|
});
|
|
262
288
|
return {
|
|
263
289
|
priceImpactDeltaUsd: priceImpactUsdForVirtualInventory < priceImpactDeltaUsd
|
|
@@ -308,8 +334,8 @@ function getNextOpenInterestParams(p) {
|
|
|
308
334
|
}
|
|
309
335
|
function getNextPoolAmountsParams(p) {
|
|
310
336
|
const { longToken, shortToken, longPoolAmount, shortPoolAmount, longDeltaUsd, shortDeltaUsd } = p;
|
|
311
|
-
const longPrice = (0, utils_1.getMidPrice)(longToken.prices);
|
|
312
|
-
const shortPrice = (0, utils_1.getMidPrice)(shortToken.prices);
|
|
337
|
+
const longPrice = p.tradeMath === "gmsol" ? (0, gmsol_1.getGmsolMidPrice)(longToken) : (0, utils_1.getMidPrice)(longToken.prices);
|
|
338
|
+
const shortPrice = p.tradeMath === "gmsol" ? (0, gmsol_1.getGmsolMidPrice)(shortToken) : (0, utils_1.getMidPrice)(shortToken.prices);
|
|
313
339
|
const longPoolUsd = (0, utils_1.convertToUsd)(longPoolAmount, longToken.decimals, longPrice);
|
|
314
340
|
const shortPoolUsd = (0, utils_1.convertToUsd)(shortPoolAmount, shortToken.decimals, shortPrice);
|
|
315
341
|
const nextLongPoolUsd = longPoolUsd + longDeltaUsd;
|
|
@@ -353,6 +379,7 @@ function getPriceImpactUsd(p) {
|
|
|
353
379
|
hasPositiveImpact,
|
|
354
380
|
factor,
|
|
355
381
|
exponentFactor,
|
|
382
|
+
tradeMath: p.tradeMath,
|
|
356
383
|
});
|
|
357
384
|
}
|
|
358
385
|
else {
|
|
@@ -363,6 +390,7 @@ function getPriceImpactUsd(p) {
|
|
|
363
390
|
factorNegative: p.factorNegative,
|
|
364
391
|
exponentFactorPositive: p.exponentFactorPositive,
|
|
365
392
|
exponentFactorNegative: p.exponentFactorNegative,
|
|
393
|
+
tradeMath: p.tradeMath,
|
|
366
394
|
});
|
|
367
395
|
}
|
|
368
396
|
return {
|
|
@@ -376,9 +404,22 @@ exports.getPriceImpactUsd = getPriceImpactUsd;
|
|
|
376
404
|
*/
|
|
377
405
|
function calculateImpactForSameSideRebalance(p) {
|
|
378
406
|
const { currentDiff, nextDiff, hasPositiveImpact, factor, exponentFactor } = p;
|
|
379
|
-
const currentImpact =
|
|
380
|
-
|
|
407
|
+
const currentImpact = applyMarketImpactFactor({
|
|
408
|
+
diff: currentDiff,
|
|
409
|
+
factor,
|
|
410
|
+
exponent: exponentFactor,
|
|
411
|
+
tradeMath: p.tradeMath,
|
|
412
|
+
});
|
|
413
|
+
const nextImpact = applyMarketImpactFactor({
|
|
414
|
+
diff: nextDiff,
|
|
415
|
+
factor,
|
|
416
|
+
exponent: exponentFactor,
|
|
417
|
+
tradeMath: p.tradeMath,
|
|
418
|
+
});
|
|
381
419
|
const deltaDiff = bigmath_1.bigMath.abs(currentImpact - nextImpact);
|
|
420
|
+
if (p.tradeMath === "gmsol" && deltaDiff > constants_1.GMSOL_MAX_SIGNED * constants_1.GMSOL_PRECISION_SCALE) {
|
|
421
|
+
throw new GmsolPriceImpactError("Solana price impact exceeds native signed range");
|
|
422
|
+
}
|
|
382
423
|
return hasPositiveImpact ? deltaDiff : -deltaDiff;
|
|
383
424
|
}
|
|
384
425
|
exports.calculateImpactForSameSideRebalance = calculateImpactForSameSideRebalance;
|
|
@@ -387,15 +428,74 @@ exports.calculateImpactForSameSideRebalance = calculateImpactForSameSideRebalanc
|
|
|
387
428
|
*/
|
|
388
429
|
function calculateImpactForCrossoverRebalance(p) {
|
|
389
430
|
const { currentDiff, nextDiff, factorNegative, factorPositive, exponentFactorPositive, exponentFactorNegative } = p;
|
|
390
|
-
const positiveImpact =
|
|
391
|
-
|
|
431
|
+
const positiveImpact = applyMarketImpactFactor({
|
|
432
|
+
diff: currentDiff,
|
|
433
|
+
factor: factorPositive,
|
|
434
|
+
exponent: exponentFactorPositive,
|
|
435
|
+
tradeMath: p.tradeMath,
|
|
436
|
+
});
|
|
437
|
+
const negativeImpactUsd = applyMarketImpactFactor({
|
|
438
|
+
diff: nextDiff,
|
|
439
|
+
factor: factorNegative,
|
|
440
|
+
exponent: exponentFactorNegative,
|
|
441
|
+
tradeMath: p.tradeMath,
|
|
442
|
+
});
|
|
392
443
|
const deltaDiffUsd = bigmath_1.bigMath.abs(positiveImpact - negativeImpactUsd);
|
|
444
|
+
if (p.tradeMath === "gmsol" && deltaDiffUsd > constants_1.GMSOL_MAX_SIGNED * constants_1.GMSOL_PRECISION_SCALE) {
|
|
445
|
+
throw new GmsolPriceImpactError("Solana price impact exceeds native signed range");
|
|
446
|
+
}
|
|
393
447
|
return positiveImpact > negativeImpactUsd ? deltaDiffUsd : -deltaDiffUsd;
|
|
394
448
|
}
|
|
395
449
|
exports.calculateImpactForCrossoverRebalance = calculateImpactForCrossoverRebalance;
|
|
450
|
+
function applyMarketImpactFactor(p) {
|
|
451
|
+
if (p.tradeMath !== "gmsol") {
|
|
452
|
+
return applyImpactFactor(p.diff, p.factor, p.exponent);
|
|
453
|
+
}
|
|
454
|
+
for (const value of [p.diff, p.factor, p.exponent]) {
|
|
455
|
+
if (value < 0n || value % constants_1.GMSOL_PRECISION_SCALE !== 0n || value > constants_1.GMSOL_MAX_UNSIGNED * constants_1.GMSOL_PRECISION_SCALE) {
|
|
456
|
+
throw new GmsolPriceImpactError("Solana impact input is outside native precision or range");
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
const base = bigmath_1.bigMath.mulDiv(p.diff, 1n, constants_1.GMSOL_PRECISION_SCALE);
|
|
460
|
+
const factor = bigmath_1.bigMath.mulDiv(p.factor, 1n, constants_1.GMSOL_PRECISION_SCALE);
|
|
461
|
+
const power = getGmsolImpactPower(base, p.exponent);
|
|
462
|
+
const impact = bigmath_1.bigMath.mulDiv(power, factor, constants_1.GMSOL_PRECISION);
|
|
463
|
+
if (impact > constants_1.GMSOL_MAX_UNSIGNED) {
|
|
464
|
+
throw new GmsolPriceImpactError("Solana impact factor exceeds native range");
|
|
465
|
+
}
|
|
466
|
+
return impact * constants_1.GMSOL_PRECISION_SCALE;
|
|
467
|
+
}
|
|
468
|
+
function getGmsolImpactPower(base, exponent) {
|
|
469
|
+
if (base < constants_1.GMSOL_PRECISION) {
|
|
470
|
+
return 0n;
|
|
471
|
+
}
|
|
472
|
+
if (base === constants_1.GMSOL_PRECISION || exponent === 0n) {
|
|
473
|
+
return constants_1.GMSOL_PRECISION;
|
|
474
|
+
}
|
|
475
|
+
const hasFractionalExponent = exponent % numbers_2.PRECISION !== 0n;
|
|
476
|
+
if (hasFractionalExponent) {
|
|
477
|
+
try {
|
|
478
|
+
return (0, numbers_1.getGmsolFractionalPower)(base, exponent / constants_1.GMSOL_PRECISION_SCALE);
|
|
479
|
+
}
|
|
480
|
+
catch (error) {
|
|
481
|
+
throw new GmsolPriceImpactError(error instanceof Error ? error.message : "Solana fractional power failed");
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (exponent > constants_1.GMSOL_MAX_IMPACT_EXPONENT) {
|
|
485
|
+
throw new GmsolPriceImpactError("Solana impact exponent is unsupported for exact calculation");
|
|
486
|
+
}
|
|
487
|
+
let power = constants_1.GMSOL_PRECISION;
|
|
488
|
+
for (let remaining = exponent; remaining > 0n; remaining -= numbers_2.PRECISION) {
|
|
489
|
+
power = bigmath_1.bigMath.mulDiv(power, base, constants_1.GMSOL_PRECISION);
|
|
490
|
+
if (power > constants_1.GMSOL_MAX_UNSIGNED) {
|
|
491
|
+
throw new GmsolPriceImpactError("Solana impact power exceeds native range");
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return power;
|
|
495
|
+
}
|
|
396
496
|
function applyImpactFactor(diff, factor, exponent) {
|
|
397
497
|
try {
|
|
398
|
-
return (0,
|
|
498
|
+
return (0, numbers_2.applyFactor)((0, numbers_2.applyExponentFactor)(diff, exponent), factor);
|
|
399
499
|
}
|
|
400
500
|
catch {
|
|
401
501
|
return 0n;
|
|
@@ -411,6 +511,6 @@ function getCappedPriceImpactPercentageFromFees({ fees, isSwap, }) {
|
|
|
411
511
|
exports.getCappedPriceImpactPercentageFromFees = getCappedPriceImpactPercentageFromFees;
|
|
412
512
|
function getMaxNegativeImpactBps(marketInfo) {
|
|
413
513
|
const factor = marketInfo.maxPositionImpactFactorNegative;
|
|
414
|
-
return factor / (0,
|
|
514
|
+
return factor / (0, numbers_2.expandDecimals)(1, numbers_2.PRECISION_DECIMALS - numbers_2.BASIS_POINTS_DECIMALS);
|
|
415
515
|
}
|
|
416
516
|
exports.getMaxNegativeImpactBps = getMaxNegativeImpactBps;
|