@gmx-io/sdk 2.9.1 → 2.9.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/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/constants.js +9 -0
- package/build/cjs/src/utils/gmsol/index.js +53 -0
- package/build/cjs/src/utils/gmsol/numbers.js +176 -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/constants.js +6 -0
- package/build/esm/src/utils/gmsol/index.js +43 -0
- package/build/esm/src/utils/gmsol/numbers.js +172 -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/constants.d.ts +5 -0
- package/build/types/src/utils/gmsol/index.d.ts +35 -0
- package/build/types/src/utils/gmsol/numbers.d.ts +1 -0
- package/build/types/src/utils/gmsol/types.d.ts +9 -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
|
@@ -6,6 +6,8 @@ const factors_1 = require("../../configs/factors");
|
|
|
6
6
|
const tokens_1 = require("../../configs/tokens");
|
|
7
7
|
const bigmath_1 = require("../bigmath");
|
|
8
8
|
const fees_1 = require("../fees");
|
|
9
|
+
const gmsol_1 = require("../gmsol");
|
|
10
|
+
const markets_1 = require("../markets");
|
|
9
11
|
const numbers_1 = require("../numbers");
|
|
10
12
|
const types_1 = require("../orders/types");
|
|
11
13
|
const positions_1 = require("../positions");
|
|
@@ -140,7 +142,8 @@ function getInternalSwapStrategyForAmountIn({ swapStrategy, amountIn, marketsInf
|
|
|
140
142
|
};
|
|
141
143
|
}
|
|
142
144
|
function getIncreasePositionAmounts(p) {
|
|
143
|
-
const { marketInfo, indexToken, initialCollateralToken, collateralToken, initialCollateralAmount, indexTokenAmount, isLong, leverage, triggerPrice, limitOrderType, position, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, externalSwapQuote, findSwapPath, userReferralInfo, proDiscountFactor, uiFeeFactor, strategy, marketsInfoData, chainId, externalSwapQuoteParams, isSetAcceptablePriceImpactEnabled, disabledMarkets, manualPath, } = p;
|
|
145
|
+
const { marketInfo, indexToken, initialCollateralToken, collateralToken, initialCollateralAmount, indexTokenAmount, isLong, leverage, triggerPrice, limitOrderType, position, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, externalSwapQuote, findSwapPath, userReferralInfo, proDiscountFactor, feeDiscountFactor, uiFeeFactor: suppliedUiFeeFactor, strategy, marketsInfoData, chainId, externalSwapQuoteParams, isSetAcceptablePriceImpactEnabled, disabledMarkets, manualPath, } = p;
|
|
146
|
+
const uiFeeFactor = p.marketInfo.tradeMath === "gmsol" ? 0n : suppliedUiFeeFactor;
|
|
144
147
|
const swapStrategy = {
|
|
145
148
|
type: "noSwap",
|
|
146
149
|
externalSwapQuote: undefined,
|
|
@@ -154,6 +157,14 @@ function getIncreasePositionAmounts(p) {
|
|
|
154
157
|
feesUsd: 0n,
|
|
155
158
|
};
|
|
156
159
|
const values = {
|
|
160
|
+
...(marketInfo.tradeMath === "gmsol"
|
|
161
|
+
? {
|
|
162
|
+
fundingPayout: {
|
|
163
|
+
longTokenAmount: position?.claimableLongTokenAmount ?? 0n,
|
|
164
|
+
shortTokenAmount: position?.claimableShortTokenAmount ?? 0n,
|
|
165
|
+
},
|
|
166
|
+
}
|
|
167
|
+
: {}),
|
|
157
168
|
initialCollateralAmount: 0n,
|
|
158
169
|
initialCollateralUsd: 0n,
|
|
159
170
|
collateralDeltaAmount: 0n,
|
|
@@ -204,6 +215,9 @@ function getIncreasePositionAmounts(p) {
|
|
|
204
215
|
if (values.indexPrice <= 0 || values.initialCollateralPrice <= 0 || values.collateralPrice <= 0) {
|
|
205
216
|
return values;
|
|
206
217
|
}
|
|
218
|
+
const getCollateralFeeUsd = (feeUsd) => marketInfo.tradeMath === "gmsol"
|
|
219
|
+
? (0, tokens_2.convertToUsd)((0, tokens_2.convertToTokenAmount)(feeUsd, collateralToken.decimals, values.collateralPrice), collateralToken.decimals, values.collateralPrice)
|
|
220
|
+
: feeUsd;
|
|
207
221
|
// Size and collateral
|
|
208
222
|
if (strategy === "leverageByCollateral" &&
|
|
209
223
|
leverage !== undefined &&
|
|
@@ -254,14 +268,18 @@ function getIncreasePositionAmounts(p) {
|
|
|
254
268
|
collateralPrice: values.collateralPrice,
|
|
255
269
|
swapPathStatsAtEvaluationPrice,
|
|
256
270
|
});
|
|
257
|
-
const
|
|
271
|
+
const requestedBaseSizeDeltaUsd = bigmath_1.bigMath.mulDiv(baseCollateralUsd, leverage, factors_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
272
|
+
const baseSizeDeltaUsd = marketInfo.tradeMath === "gmsol" ? (0, gmsol_1.getGmsolUsdAmount)(requestedBaseSizeDeltaUsd) : requestedBaseSizeDeltaUsd;
|
|
258
273
|
const baseSizeDeltaInTokens = (0, tokens_2.convertToTokenAmountForIncrease)(baseSizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
259
274
|
const { balanceWasImproved: baseBalanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, baseSizeDeltaUsd, isLong, { sizeDeltaInTokens: baseSizeDeltaInTokens });
|
|
260
|
-
const basePositionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, baseSizeDeltaUsd, baseBalanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
275
|
+
const basePositionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, baseSizeDeltaUsd, baseBalanceWasImproved, userReferralInfo, undefined, proDiscountFactor, feeDiscountFactor);
|
|
261
276
|
const baseUiFeeUsd = (0, numbers_1.applyFactor)(baseSizeDeltaUsd, uiFeeFactor);
|
|
262
277
|
const totalSwapVolumeUsd = (0, fees_1.getTotalSwapVolumeFromSwapStats)((swapPathStatsAtEvaluationPrice ?? values.swapStrategy.swapPathStats)?.swapSteps);
|
|
263
278
|
values.swapUiFeeUsd = (0, numbers_1.applyFactor)(totalSwapVolumeUsd, uiFeeFactor);
|
|
264
|
-
values.sizeDeltaUsd = bigmath_1.bigMath.mulDiv(baseCollateralUsd - basePositionFeeInfo.positionFeeUsd - baseUiFeeUsd - values.swapUiFeeUsd, leverage, factors_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
279
|
+
values.sizeDeltaUsd = bigmath_1.bigMath.mulDiv(baseCollateralUsd - getCollateralFeeUsd(basePositionFeeInfo.positionFeeUsd) - baseUiFeeUsd - values.swapUiFeeUsd, leverage, factors_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
280
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
281
|
+
values.sizeDeltaUsd = (0, gmsol_1.getGmsolUsdAmount)(values.sizeDeltaUsd);
|
|
282
|
+
}
|
|
265
283
|
if (values.sizeDeltaUsd <= 0n) {
|
|
266
284
|
return values;
|
|
267
285
|
}
|
|
@@ -269,14 +287,14 @@ function getIncreasePositionAmounts(p) {
|
|
|
269
287
|
const { balanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, values.sizeDeltaUsd, isLong, {
|
|
270
288
|
sizeDeltaInTokens: values.indexTokenAmount,
|
|
271
289
|
});
|
|
272
|
-
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
290
|
+
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor, feeDiscountFactor);
|
|
273
291
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
274
292
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
275
293
|
values.uiFeeUsd = (0, numbers_1.applyFactor)(values.sizeDeltaUsd, uiFeeFactor);
|
|
276
294
|
values.collateralDeltaUsd =
|
|
277
295
|
baseCollateralUsd -
|
|
278
|
-
values.positionFeeUsd -
|
|
279
|
-
values.borrowingFeeUsd -
|
|
296
|
+
getCollateralFeeUsd(values.positionFeeUsd) -
|
|
297
|
+
getCollateralFeeUsd(values.borrowingFeeUsd) -
|
|
280
298
|
values.fundingFeeUsd -
|
|
281
299
|
values.uiFeeUsd -
|
|
282
300
|
values.swapUiFeeUsd;
|
|
@@ -289,11 +307,14 @@ function getIncreasePositionAmounts(p) {
|
|
|
289
307
|
values.estimatedLeverage = leverage;
|
|
290
308
|
values.indexTokenAmount = indexTokenAmount;
|
|
291
309
|
values.sizeDeltaUsd = (0, tokens_2.convertToUsd)(indexTokenAmount, indexToken.decimals, values.indexPrice);
|
|
310
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
311
|
+
values.sizeDeltaUsd = (0, gmsol_1.getGmsolUsdAmount)(values.sizeDeltaUsd);
|
|
312
|
+
}
|
|
292
313
|
const sizeDeltaInTokensForPriceImpact = (0, tokens_2.convertToTokenAmountForIncrease)(values.sizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
293
314
|
const { balanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, values.sizeDeltaUsd, isLong, {
|
|
294
315
|
sizeDeltaInTokens: sizeDeltaInTokensForPriceImpact,
|
|
295
316
|
});
|
|
296
|
-
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
317
|
+
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor, feeDiscountFactor);
|
|
297
318
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
298
319
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
299
320
|
values.uiFeeUsd = (0, numbers_1.applyFactor)(values.sizeDeltaUsd, uiFeeFactor);
|
|
@@ -303,8 +324,8 @@ function getIncreasePositionAmounts(p) {
|
|
|
303
324
|
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
304
325
|
collateralPrice: values.collateralPrice,
|
|
305
326
|
uiFeeFactor,
|
|
306
|
-
positionFeeUsd: values.positionFeeUsd,
|
|
307
|
-
borrowingFeeUsd: values.borrowingFeeUsd,
|
|
327
|
+
positionFeeUsd: getCollateralFeeUsd(values.positionFeeUsd),
|
|
328
|
+
borrowingFeeUsd: getCollateralFeeUsd(values.borrowingFeeUsd),
|
|
308
329
|
fundingFeeUsd: values.fundingFeeUsd,
|
|
309
330
|
uiFeeUsd: values.uiFeeUsd,
|
|
310
331
|
swapUiFeeUsd: values.swapUiFeeUsd,
|
|
@@ -380,8 +401,8 @@ function getIncreasePositionAmounts(p) {
|
|
|
380
401
|
const swapUiFeeUsdAtEvaluationPrice = (0, numbers_1.applyFactor)((0, fees_1.getTotalSwapVolumeFromSwapStats)(swapPathStatsAtEvaluationPrice.swapSteps), uiFeeFactor);
|
|
381
402
|
values.collateralDeltaUsd =
|
|
382
403
|
baseCollateralUsdAtEvaluationPrice -
|
|
383
|
-
values.positionFeeUsd -
|
|
384
|
-
values.borrowingFeeUsd -
|
|
404
|
+
getCollateralFeeUsd(values.positionFeeUsd) -
|
|
405
|
+
getCollateralFeeUsd(values.borrowingFeeUsd) -
|
|
385
406
|
values.fundingFeeUsd -
|
|
386
407
|
values.uiFeeUsd -
|
|
387
408
|
swapUiFeeUsdAtEvaluationPrice;
|
|
@@ -392,11 +413,14 @@ function getIncreasePositionAmounts(p) {
|
|
|
392
413
|
if (indexTokenAmount !== undefined && indexTokenAmount > 0) {
|
|
393
414
|
values.indexTokenAmount = indexTokenAmount;
|
|
394
415
|
values.sizeDeltaUsd = (0, tokens_2.convertToUsd)(indexTokenAmount, indexToken.decimals, values.indexPrice);
|
|
416
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
417
|
+
values.sizeDeltaUsd = (0, gmsol_1.getGmsolUsdAmount)(values.sizeDeltaUsd);
|
|
418
|
+
}
|
|
395
419
|
const sizeDeltaInTokensForPriceImpact = (0, tokens_2.convertToTokenAmountForIncrease)(values.sizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
396
420
|
const { balanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, values.sizeDeltaUsd, isLong, {
|
|
397
421
|
sizeDeltaInTokens: sizeDeltaInTokensForPriceImpact,
|
|
398
422
|
});
|
|
399
|
-
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
423
|
+
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor, feeDiscountFactor);
|
|
400
424
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
401
425
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
402
426
|
values.uiFeeUsd = (0, numbers_1.applyFactor)(values.sizeDeltaUsd, uiFeeFactor);
|
|
@@ -450,16 +474,16 @@ function getIncreasePositionAmounts(p) {
|
|
|
450
474
|
values.swapUiFeeUsd = (0, numbers_1.applyFactor)(totalSwapVolumeUsd, uiFeeFactor);
|
|
451
475
|
values.collateralDeltaUsd =
|
|
452
476
|
baseCollateralUsd -
|
|
453
|
-
values.positionFeeUsd -
|
|
454
|
-
values.borrowingFeeUsd -
|
|
477
|
+
getCollateralFeeUsd(values.positionFeeUsd) -
|
|
478
|
+
getCollateralFeeUsd(values.borrowingFeeUsd) -
|
|
455
479
|
values.fundingFeeUsd -
|
|
456
480
|
values.uiFeeUsd -
|
|
457
481
|
values.swapUiFeeUsd;
|
|
458
482
|
values.collateralDeltaAmount = (0, tokens_2.convertToTokenAmount)(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
459
483
|
}
|
|
460
484
|
else if (values.sizeDeltaUsd > 0n) {
|
|
461
|
-
values.collateralDeltaUsd = -(values.positionFeeUsd +
|
|
462
|
-
values.borrowingFeeUsd +
|
|
485
|
+
values.collateralDeltaUsd = -(getCollateralFeeUsd(values.positionFeeUsd) +
|
|
486
|
+
getCollateralFeeUsd(values.borrowingFeeUsd) +
|
|
463
487
|
values.fundingFeeUsd +
|
|
464
488
|
values.uiFeeUsd);
|
|
465
489
|
values.collateralDeltaAmount = (0, tokens_2.convertToTokenAmount)(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
@@ -473,8 +497,14 @@ function getIncreasePositionAmounts(p) {
|
|
|
473
497
|
});
|
|
474
498
|
}
|
|
475
499
|
values.sizeDeltaInTokens = (0, tokens_2.convertToTokenAmountForIncrease)(values.sizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
500
|
+
const impactMarketInfo = marketInfo.tradeMath === "gmsol" && evaluationPrice !== undefined
|
|
501
|
+
? {
|
|
502
|
+
...marketInfo,
|
|
503
|
+
indexToken: { ...marketInfo.indexToken, prices: { minPrice: evaluationPrice, maxPrice: evaluationPrice } },
|
|
504
|
+
}
|
|
505
|
+
: marketInfo;
|
|
476
506
|
const acceptablePriceInfo = (0, prices_1.getAcceptablePriceInfo)({
|
|
477
|
-
marketInfo,
|
|
507
|
+
marketInfo: impactMarketInfo,
|
|
478
508
|
isIncrease: true,
|
|
479
509
|
isLimit,
|
|
480
510
|
isLong,
|
|
@@ -483,11 +513,14 @@ function getIncreasePositionAmounts(p) {
|
|
|
483
513
|
sizeDeltaInTokens: values.sizeDeltaInTokens,
|
|
484
514
|
});
|
|
485
515
|
values.positionPriceImpactDeltaUsd = acceptablePriceInfo.priceImpactDeltaUsd;
|
|
486
|
-
values.potentialPriceImpactDiffUsd =
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
516
|
+
values.potentialPriceImpactDiffUsd =
|
|
517
|
+
marketInfo.tradeMath === "gmsol"
|
|
518
|
+
? 0n
|
|
519
|
+
: (0, positions_1.getPriceImpactDiffUsd)({
|
|
520
|
+
totalImpactDeltaUsd: values.positionPriceImpactDeltaUsd,
|
|
521
|
+
marketInfo,
|
|
522
|
+
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
523
|
+
});
|
|
491
524
|
values.acceptablePrice = acceptablePriceInfo.acceptablePrice;
|
|
492
525
|
values.acceptablePriceDeltaBps = acceptablePriceInfo.acceptablePriceDeltaBps;
|
|
493
526
|
if (isLimit) {
|
|
@@ -503,6 +536,7 @@ function getIncreasePositionAmounts(p) {
|
|
|
503
536
|
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
504
537
|
priceImpactDeltaUsd: values.positionPriceImpactDeltaUsd,
|
|
505
538
|
acceptablePriceImpactBuffer: acceptablePriceImpactBuffer || factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER,
|
|
539
|
+
executionPrice: marketInfo.tradeMath === "gmsol" ? acceptablePriceInfo.acceptablePrice : undefined,
|
|
506
540
|
});
|
|
507
541
|
if (maxNegativePriceImpactBps === undefined) {
|
|
508
542
|
maxNegativePriceImpactBps = values.recommendedAcceptablePriceDeltaBps;
|
|
@@ -521,6 +555,18 @@ function getIncreasePositionAmounts(p) {
|
|
|
521
555
|
values.acceptablePriceDeltaBps = limitAcceptablePriceInfo.acceptablePriceDeltaBps;
|
|
522
556
|
}
|
|
523
557
|
}
|
|
558
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
559
|
+
values.collateralDeltaUsd = (0, tokens_2.convertToUsd)(values.collateralDeltaAmount, collateralToken.decimals, values.collateralPrice);
|
|
560
|
+
values.sizeDeltaInTokens = (0, gmsol_1.getGmsolIncreaseSizeDeltaInTokens)({
|
|
561
|
+
baseSizeDeltaInTokens: values.sizeDeltaInTokens,
|
|
562
|
+
priceImpactDeltaUsd: values.positionPriceImpactDeltaUsd,
|
|
563
|
+
indexToken: impactMarketInfo.indexToken,
|
|
564
|
+
isLong,
|
|
565
|
+
});
|
|
566
|
+
if (values.sizeDeltaInTokens === 0n) {
|
|
567
|
+
values.sizeDeltaUsd = 0n;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
524
570
|
return values;
|
|
525
571
|
}
|
|
526
572
|
exports.getIncreasePositionAmounts = getIncreasePositionAmounts;
|
|
@@ -592,7 +638,16 @@ function getIncreasePositionPrices({ triggerPrice, indexToken, initialCollateral
|
|
|
592
638
|
}
|
|
593
639
|
exports.getIncreasePositionPrices = getIncreasePositionPrices;
|
|
594
640
|
function getNextPositionValuesForIncreaseTrade(p) {
|
|
595
|
-
const { existingPosition, marketInfo, collateralToken, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaUsd, collateralDeltaAmount, indexPrice, isLong, showPnlInLeverage, minCollateralUsd, userReferralInfo, positionPriceImpactDeltaUsd, collateralPrice, } = p;
|
|
641
|
+
const { existingPosition, marketInfo: suppliedMarketInfo, collateralToken, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaUsd, collateralDeltaAmount, indexPrice, isLong, showPnlInLeverage, minCollateralUsd, userReferralInfo, feeDiscountFactor, positionPriceImpactDeltaUsd, collateralPrice, } = p;
|
|
642
|
+
const marketInfo = suppliedMarketInfo.tradeMath === "gmsol"
|
|
643
|
+
? (0, markets_1.getMarketInfoWithOpenInterestDelta)({
|
|
644
|
+
marketInfo: suppliedMarketInfo,
|
|
645
|
+
collateralToken,
|
|
646
|
+
isLong,
|
|
647
|
+
sizeDeltaUsd,
|
|
648
|
+
sizeDeltaInTokens,
|
|
649
|
+
})
|
|
650
|
+
: suppliedMarketInfo;
|
|
596
651
|
const existingCollateralUsd = !existingPosition
|
|
597
652
|
? 0n
|
|
598
653
|
: collateralPrice === undefined || collateralPrice <= 0n
|
|
@@ -609,7 +664,7 @@ function getNextPositionValuesForIncreaseTrade(p) {
|
|
|
609
664
|
sizeInTokens: nextSizeInTokens,
|
|
610
665
|
indexToken: marketInfo.indexToken,
|
|
611
666
|
}) ?? indexPrice;
|
|
612
|
-
const nextPnl = existingPosition
|
|
667
|
+
const nextPnl = existingPosition || marketInfo.tradeMath === "gmsol"
|
|
613
668
|
? (0, positions_1.getPositionPnlUsd)({
|
|
614
669
|
marketInfo,
|
|
615
670
|
sizeInUsd: nextSizeUsd,
|
|
@@ -638,10 +693,13 @@ function getNextPositionValuesForIncreaseTrade(p) {
|
|
|
638
693
|
pendingImpactAmount: existingPosition?.pendingImpactAmount ?? 0n,
|
|
639
694
|
isLong: isLong,
|
|
640
695
|
userReferralInfo,
|
|
696
|
+
feeDiscountFactor,
|
|
641
697
|
});
|
|
642
|
-
let nextPendingImpactDeltaUsd =
|
|
643
|
-
?
|
|
644
|
-
:
|
|
698
|
+
let nextPendingImpactDeltaUsd = marketInfo.tradeMath === "gmsol"
|
|
699
|
+
? 0n
|
|
700
|
+
: existingPosition?.pendingImpactUsd !== undefined
|
|
701
|
+
? existingPosition.pendingImpactUsd + positionPriceImpactDeltaUsd
|
|
702
|
+
: positionPriceImpactDeltaUsd;
|
|
645
703
|
const potentialPriceImpactDiffUsd = (0, positions_1.getPriceImpactDiffUsd)({
|
|
646
704
|
totalImpactDeltaUsd: nextPendingImpactDeltaUsd,
|
|
647
705
|
marketInfo,
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getIncreaseResultingPositionState = exports.getIncreaseResultingPositionMarginState = exports.getMarketInfoAtIndexPrice = exports.getResultingPositionMarginState = exports.getIsMaxLeverageMarginReason = exports.PositionMarginFailureReason = void 0;
|
|
4
4
|
const fees_1 = require("../fees");
|
|
5
|
+
const gmsol_1 = require("../gmsol");
|
|
5
6
|
const markets_1 = require("../markets");
|
|
6
7
|
const numbers_1 = require("../numbers");
|
|
7
8
|
const positions_1 = require("../positions");
|
|
@@ -28,8 +29,11 @@ exports.getIsMaxLeverageMarginReason = getIsMaxLeverageMarginReason;
|
|
|
28
29
|
* after an increase or a decrease.
|
|
29
30
|
*/
|
|
30
31
|
function getResultingPositionMarginState(p) {
|
|
31
|
-
const { marketInfo, collateralToken, sizeInUsd, sizeInTokens, collateralAmount, pendingImpactAmount, minCollateralUsd, isLong, userReferralInfo, proDiscountFactor, shouldValidateMinCollateralUsd = true, } = p;
|
|
32
|
+
const { marketInfo, collateralToken, sizeInUsd, sizeInTokens, collateralAmount, pendingImpactAmount, minCollateralUsd: suppliedMinCollateralUsd, isLong, userReferralInfo, proDiscountFactor, feeDiscountFactor, shouldValidateMinCollateralUsd = true, } = p;
|
|
32
33
|
const { indexToken } = marketInfo;
|
|
34
|
+
const minCollateralUsd = marketInfo.tradeMath === "gmsol"
|
|
35
|
+
? marketInfo.minCollateralUsd ?? suppliedMinCollateralUsd
|
|
36
|
+
: suppliedMinCollateralUsd;
|
|
33
37
|
const positionPnlUsd = (0, positions_1.getPositionPnlUsd)({
|
|
34
38
|
marketInfo,
|
|
35
39
|
sizeInUsd,
|
|
@@ -46,13 +50,16 @@ function getResultingPositionMarginState(p) {
|
|
|
46
50
|
if (totalImpactUsd > 0) {
|
|
47
51
|
totalImpactUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeInUsd, totalImpactUsd);
|
|
48
52
|
}
|
|
49
|
-
const pendingImpactUsd = (0, tokens_1.convertToUsd)(pendingImpactAmount, indexToken.decimals, pendingImpactAmount > 0 ? indexToken.prices.minPrice : indexToken.prices.maxPrice);
|
|
53
|
+
const pendingImpactUsd = (0, tokens_1.convertToUsd)(marketInfo.tradeMath === "gmsol" ? 0n : pendingImpactAmount, indexToken.decimals, pendingImpactAmount > 0 ? indexToken.prices.minPrice : indexToken.prices.maxPrice);
|
|
50
54
|
totalImpactUsd = totalImpactUsd + pendingImpactUsd;
|
|
51
55
|
if (totalImpactUsd >= 0) {
|
|
52
56
|
totalImpactUsd = 0n;
|
|
53
57
|
}
|
|
54
58
|
else {
|
|
55
|
-
|
|
59
|
+
let maxNegativeImpactUsd = -(0, numbers_1.applyFactor)(sizeInUsd, marketInfo.maxPositionImpactFactorForLiquidations);
|
|
60
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
61
|
+
maxNegativeImpactUsd = (0, gmsol_1.getGmsolUsdAmount)(maxNegativeImpactUsd);
|
|
62
|
+
}
|
|
56
63
|
if (totalImpactUsd < maxNegativeImpactUsd) {
|
|
57
64
|
totalImpactUsd = maxNegativeImpactUsd;
|
|
58
65
|
}
|
|
@@ -73,9 +80,17 @@ function getResultingPositionMarginState(p) {
|
|
|
73
80
|
}
|
|
74
81
|
}
|
|
75
82
|
closingFeeAmount -= discountAmount;
|
|
83
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
84
|
+
const { positionFeeUsd } = (0, fees_1.getPositionFee)(marketInfo, sizeInUsd, balanceWasImproved, undefined, undefined, undefined, feeDiscountFactor);
|
|
85
|
+
closingFeeAmount =
|
|
86
|
+
(0, tokens_1.convertToTokenAmount)(positionFeeUsd, collateralToken.decimals, collateralToken.prices.minPrice) ?? 0n;
|
|
87
|
+
}
|
|
76
88
|
const closingCostUsd = (0, tokens_1.convertToUsd)(closingFeeAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
77
89
|
const remainingCollateralUsd = collateralUsd + positionPnlUsd + totalImpactUsd - closingCostUsd;
|
|
78
|
-
|
|
90
|
+
let minCollateralUsdForLeverage = (0, numbers_1.applyFactor)(sizeInUsd, marketInfo.minCollateralFactor);
|
|
91
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
92
|
+
minCollateralUsdForLeverage = (0, gmsol_1.getGmsolUsdAmount)(minCollateralUsdForLeverage);
|
|
93
|
+
}
|
|
79
94
|
const state = {
|
|
80
95
|
remainingCollateralUsd,
|
|
81
96
|
minCollateralUsd,
|
|
@@ -127,7 +142,10 @@ function getIncreaseResultingPositionMarginState(p) {
|
|
|
127
142
|
}
|
|
128
143
|
exports.getIncreaseResultingPositionMarginState = getIncreaseResultingPositionMarginState;
|
|
129
144
|
function getIncreaseResultingPositionState(p) {
|
|
130
|
-
const { isLong, existingPosition, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaAmount, minCollateralUsd, userReferralInfo, proDiscountFactor, indexPriceForEvaluation, } = p;
|
|
145
|
+
const { isLong, existingPosition, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaAmount, minCollateralUsd: suppliedMinCollateralUsd, userReferralInfo, proDiscountFactor, feeDiscountFactor, indexPriceForEvaluation, } = p;
|
|
146
|
+
const minCollateralUsd = p.marketInfo.tradeMath === "gmsol"
|
|
147
|
+
? p.marketInfo.minCollateralUsd ?? suppliedMinCollateralUsd
|
|
148
|
+
: suppliedMinCollateralUsd;
|
|
131
149
|
if (sizeDeltaUsd <= 0n || sizeDeltaInTokens <= 0n) {
|
|
132
150
|
return undefined;
|
|
133
151
|
}
|
|
@@ -148,30 +166,53 @@ function getIncreaseResultingPositionState(p) {
|
|
|
148
166
|
if (increaseImpactUsd > 0n) {
|
|
149
167
|
increaseImpactUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeDeltaUsd, increaseImpactUsd);
|
|
150
168
|
}
|
|
169
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
170
|
+
increaseImpactUsd = (0, fees_1.capPositionImpactUsdByMaxImpactPool)(marketInfo, increaseImpactUsd);
|
|
171
|
+
}
|
|
151
172
|
const increasePendingImpactAmount = increaseImpactUsd > 0n
|
|
152
173
|
? (0, tokens_1.convertToTokenAmount)(increaseImpactUsd, indexToken.decimals, indexToken.prices.maxPrice)
|
|
153
174
|
: (0, numbers_1.roundUpMagnitudeDivision)(increaseImpactUsd * (0, numbers_1.expandDecimals)(1, indexToken.decimals), indexToken.prices.minPrice);
|
|
175
|
+
const executedSizeDeltaInTokens = marketInfo.tradeMath === "gmsol"
|
|
176
|
+
? (0, gmsol_1.getGmsolIncreaseSizeDeltaInTokens)({
|
|
177
|
+
baseSizeDeltaInTokens: (0, tokens_1.convertToTokenAmountForIncrease)(sizeDeltaUsd, indexToken.decimals, isLong ? indexToken.prices.maxPrice : indexToken.prices.minPrice, isLong),
|
|
178
|
+
priceImpactDeltaUsd: increaseImpactUsd,
|
|
179
|
+
indexToken,
|
|
180
|
+
isLong,
|
|
181
|
+
})
|
|
182
|
+
: sizeDeltaInTokens;
|
|
183
|
+
if (executedSizeDeltaInTokens <= 0n) {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
154
186
|
const nextSizeInUsd = (existingPosition?.sizeInUsd ?? 0n) + sizeDeltaUsd;
|
|
155
|
-
const nextSizeInTokens = (existingPosition?.sizeInTokens ?? 0n) +
|
|
187
|
+
const nextSizeInTokens = (existingPosition?.sizeInTokens ?? 0n) + executedSizeDeltaInTokens;
|
|
156
188
|
const nextCollateralAmount = (existingPosition?.collateralAmount ?? 0n) + collateralDeltaAmount;
|
|
157
|
-
const nextPendingImpactAmount = (existingPosition?.pendingImpactAmount ?? 0n) + increasePendingImpactAmount;
|
|
189
|
+
const nextPendingImpactAmount = marketInfo.tradeMath === "gmsol" ? 0n : (existingPosition?.pendingImpactAmount ?? 0n) + increasePendingImpactAmount;
|
|
158
190
|
const nextMarketInfo = (0, markets_1.getMarketInfoWithOpenInterestDelta)({
|
|
159
191
|
marketInfo,
|
|
160
192
|
collateralToken,
|
|
161
193
|
isLong,
|
|
162
194
|
sizeDeltaUsd,
|
|
163
|
-
sizeDeltaInTokens,
|
|
195
|
+
sizeDeltaInTokens: executedSizeDeltaInTokens,
|
|
164
196
|
});
|
|
197
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
198
|
+
nextMarketInfo.positionImpactPoolAmount -= increasePendingImpactAmount;
|
|
199
|
+
}
|
|
165
200
|
const nextCollateralAmountClamped = nextCollateralAmount < 0n ? 0n : nextCollateralAmount;
|
|
166
201
|
const remainingCollateralUsdForOpenInterest = (0, tokens_1.convertToUsd)(nextCollateralAmountClamped, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
167
202
|
const minCollateralFactorMultiplier = isLong
|
|
168
203
|
? nextMarketInfo.minCollateralFactorForOpenInterestLong
|
|
169
204
|
: nextMarketInfo.minCollateralFactorForOpenInterestShort;
|
|
170
205
|
let minCollateralFactorForOpenInterest = (0, numbers_1.applyFactor)((0, markets_1.getOpenInterestUsd)(nextMarketInfo, isLong), minCollateralFactorMultiplier);
|
|
206
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
207
|
+
minCollateralFactorForOpenInterest = (0, gmsol_1.getGmsolUsdAmount)(minCollateralFactorForOpenInterest);
|
|
208
|
+
}
|
|
171
209
|
if (nextMarketInfo.minCollateralFactor > minCollateralFactorForOpenInterest) {
|
|
172
210
|
minCollateralFactorForOpenInterest = nextMarketInfo.minCollateralFactor;
|
|
173
211
|
}
|
|
174
|
-
|
|
212
|
+
let minCollateralUsdForOpenInterest = (0, numbers_1.applyFactor)(nextSizeInUsd, minCollateralFactorForOpenInterest);
|
|
213
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
214
|
+
minCollateralUsdForOpenInterest = (0, gmsol_1.getGmsolUsdAmount)(minCollateralUsdForOpenInterest);
|
|
215
|
+
}
|
|
175
216
|
const position = {
|
|
176
217
|
sizeInUsd: nextSizeInUsd,
|
|
177
218
|
sizeInTokens: nextSizeInTokens,
|
|
@@ -199,6 +240,7 @@ function getIncreaseResultingPositionState(p) {
|
|
|
199
240
|
isLong,
|
|
200
241
|
userReferralInfo,
|
|
201
242
|
proDiscountFactor,
|
|
243
|
+
feeDiscountFactor,
|
|
202
244
|
});
|
|
203
245
|
return { marginState, position, marketInfo: nextMarketInfo };
|
|
204
246
|
}
|
|
@@ -52,7 +52,7 @@ function getTwapIncreasePartsGrossCollateralUsd(p) {
|
|
|
52
52
|
return undefined;
|
|
53
53
|
}
|
|
54
54
|
const grossCollateralUsd = (0, tokens_1.convertToUsd)(swapPathStats.amountOut, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
55
|
-
const swapUiFeeUsd = (0, numbers_1.applyFactor)((0, fees_1.getTotalSwapVolumeFromSwapStats)(swapPathStats.swapSteps), uiFeeFactor);
|
|
55
|
+
const swapUiFeeUsd = (0, numbers_1.applyFactor)((0, fees_1.getTotalSwapVolumeFromSwapStats)(swapPathStats.swapSteps.filter((swapStep) => marketsInfoData[swapStep.marketAddress].tradeMath !== "gmsol")), uiFeeFactor);
|
|
56
56
|
partGrossCollateralUsds.push(grossCollateralUsd - swapUiFeeUsd);
|
|
57
57
|
marketsInfoData = swapPathStats.swapSteps.reduce((acc, swapStep) => ({
|
|
58
58
|
...acc,
|
|
@@ -92,7 +92,11 @@ function getPositionFeesAccruedUsd(marketInfo, isLong, sizeInUsd, periodSeconds)
|
|
|
92
92
|
return borrowingUsd + (fundingUsd < 0n ? -fundingUsd : 0n);
|
|
93
93
|
}
|
|
94
94
|
function getTwapIncreaseSequentialMarginState(p) {
|
|
95
|
-
const { collateralToken, isLong, numberOfParts, partDelaysSeconds, partGrossCollateralUsds, partSizeDeltaUsd, pendingFeesUsd, uiFeeFactor, minCollateralUsd, userReferralInfo, proDiscountFactor, } = p;
|
|
95
|
+
const { collateralToken, isLong, numberOfParts, partDelaysSeconds, partGrossCollateralUsds, partSizeDeltaUsd, pendingFeesUsd, uiFeeFactor: suppliedUiFeeFactor, minCollateralUsd: suppliedMinCollateralUsd, userReferralInfo, proDiscountFactor, feeDiscountFactor, } = p;
|
|
96
|
+
const minCollateralUsd = p.marketInfo.tradeMath === "gmsol"
|
|
97
|
+
? p.marketInfo.minCollateralUsd ?? suppliedMinCollateralUsd
|
|
98
|
+
: suppliedMinCollateralUsd;
|
|
99
|
+
const uiFeeFactor = p.marketInfo.tradeMath === "gmsol" ? 0n : suppliedUiFeeFactor;
|
|
96
100
|
if (numberOfParts < 1 || partSizeDeltaUsd <= 0n || partGrossCollateralUsds.length < numberOfParts) {
|
|
97
101
|
return undefined;
|
|
98
102
|
}
|
|
@@ -126,7 +130,7 @@ function getTwapIncreaseSequentialMarginState(p) {
|
|
|
126
130
|
fallbackToZero: true,
|
|
127
131
|
sizeDeltaInTokens: partSizeDeltaInTokens,
|
|
128
132
|
});
|
|
129
|
-
const { positionFeeUsd } = (0, fees_1.getPositionFee)(marketInfo, partSizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
133
|
+
const { positionFeeUsd } = (0, fees_1.getPositionFee)(marketInfo, partSizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor, feeDiscountFactor);
|
|
130
134
|
const uiFeeUsd = (0, numbers_1.applyFactor)(partSizeDeltaUsd, uiFeeFactor);
|
|
131
135
|
const settledFeesUsd = (partIndex === 0 ? pendingFeesUsd : 0n) + accruedFeesUsd;
|
|
132
136
|
const collateralDeltaUsd = partGrossCollateralUsds[partIndex] - positionFeeUsd - uiFeeUsd - settledFeesUsd;
|
|
@@ -142,6 +146,7 @@ function getTwapIncreaseSequentialMarginState(p) {
|
|
|
142
146
|
minCollateralUsd,
|
|
143
147
|
userReferralInfo,
|
|
144
148
|
proDiscountFactor,
|
|
149
|
+
feeDiscountFactor,
|
|
145
150
|
});
|
|
146
151
|
if (!resultingState) {
|
|
147
152
|
return undefined;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getApiUrl, getApiFallbackUrls } from "../../configs/api";
|
|
2
|
+
import { isContractsChain } from "../../configs/chains";
|
|
2
3
|
import { isSettlementChain } from "../../configs/multichain";
|
|
3
4
|
import { fetchApiApy } from "../../utils/apy/api";
|
|
4
5
|
import { fetchWalletBalances as fetchWalletBalancesRaw, fetchAllowances as fetchAllowancesRaw, buildApproveTransaction, buildErc20ApproveTxn, executeErc20Approve, } from "../../utils/balances/api";
|
|
@@ -17,6 +18,7 @@ import { fetchApiPerformanceAnnualized, fetchApiPerformanceSnapshots } from "../
|
|
|
17
18
|
import { fetchApiPositionsInfo } from "../../utils/positions/api";
|
|
18
19
|
import { fetchApiOhlcv, fetchApiTokenPrices, parseApiTokenPrices } from "../../utils/prices/api";
|
|
19
20
|
import { fetchApiRates } from "../../utils/rates/api";
|
|
21
|
+
import { fetchApiFeeDiscount } from "../../utils/referrals/api";
|
|
20
22
|
import { fetchApiStakingPower } from "../../utils/staking/api";
|
|
21
23
|
import { WsStreamClient, createChannelSubscription, toStreamUrl } from "../../utils/stream";
|
|
22
24
|
import { fetchSubaccountStatus, prepareSubaccountApproval, signSubaccountApproval } from "../../utils/subaccount/api";
|
|
@@ -33,28 +35,36 @@ export { HttpError } from "../../utils/http/http";
|
|
|
33
35
|
export { HttpClientWithFallback } from "../../utils/http/httpFallback";
|
|
34
36
|
export { getGasPaymentTokens } from "../../configs/express";
|
|
35
37
|
export class GmxApiSdk {
|
|
36
|
-
constructor(
|
|
38
|
+
constructor(options) {
|
|
37
39
|
this.preparedSubaccountApprovals = new Map();
|
|
40
|
+
const { chainId, apiUrl, api, streamUrl, webSocketImpl } = options;
|
|
38
41
|
this.streamUrlOverride = streamUrl;
|
|
39
42
|
this.webSocketImpl = webSocketImpl;
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
return;
|
|
43
|
+
if (chainId === undefined) {
|
|
44
|
+
throw new Error("chainId is required");
|
|
43
45
|
}
|
|
44
|
-
|
|
45
|
-
if (!
|
|
46
|
-
|
|
46
|
+
let resolvedApi = api;
|
|
47
|
+
if (!resolvedApi) {
|
|
48
|
+
const resolvedApiUrl = apiUrl ?? getApiUrl(chainId);
|
|
49
|
+
if (!resolvedApiUrl) {
|
|
50
|
+
throw new Error("api is not supported for current chainId");
|
|
51
|
+
}
|
|
52
|
+
const fallbackUrls = apiUrl ? [] : getApiFallbackUrls(chainId);
|
|
53
|
+
const allUrls = [resolvedApiUrl, ...fallbackUrls];
|
|
54
|
+
resolvedApi = allUrls.length > 1 ? new HttpClientWithFallback(allUrls) : new HttpClient(resolvedApiUrl);
|
|
47
55
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
56
|
+
this.ctx = { chainId, api: resolvedApi };
|
|
57
|
+
}
|
|
58
|
+
requireEvmContext() {
|
|
59
|
+
const { chainId, api } = this.ctx;
|
|
60
|
+
if (!isContractsChain(chainId, true)) {
|
|
61
|
+
throw new Error("EVM operations require an EVM chainId");
|
|
62
|
+
}
|
|
63
|
+
return { chainId, api };
|
|
54
64
|
}
|
|
55
65
|
get subaccountClient() {
|
|
56
66
|
return {
|
|
57
|
-
ctx: this.
|
|
67
|
+
ctx: this.requireEvmContext(),
|
|
58
68
|
owner: this,
|
|
59
69
|
preparedSubaccountApprovals: this.preparedSubaccountApprovals,
|
|
60
70
|
getSubaccount: () => this._subaccount,
|
|
@@ -121,6 +131,9 @@ export class GmxApiSdk {
|
|
|
121
131
|
fetchOrders(params) {
|
|
122
132
|
return fetchApiOrders(this.ctx, params);
|
|
123
133
|
}
|
|
134
|
+
fetchFeeDiscount(params) {
|
|
135
|
+
return fetchApiFeeDiscount(this.ctx, params);
|
|
136
|
+
}
|
|
124
137
|
fetchTrades(params) {
|
|
125
138
|
return fetchApiTrades(this.ctx, params);
|
|
126
139
|
}
|
|
@@ -191,19 +204,21 @@ export class GmxApiSdk {
|
|
|
191
204
|
return fetchAllowancesRaw(this.ctx, params);
|
|
192
205
|
}
|
|
193
206
|
buildApproveTransaction(params) {
|
|
194
|
-
return buildApproveTransaction(this.
|
|
207
|
+
return buildApproveTransaction(this.requireEvmContext(), params);
|
|
195
208
|
}
|
|
196
209
|
buildErc20ApproveTxn(params) {
|
|
197
210
|
return buildErc20ApproveTxn(params);
|
|
198
211
|
}
|
|
199
212
|
executeErc20Approve(signer, params) {
|
|
213
|
+
this.requireEvmContext();
|
|
200
214
|
return executeErc20Approve(signer, params);
|
|
201
215
|
}
|
|
202
216
|
requireSettlementChainId() {
|
|
203
|
-
|
|
204
|
-
|
|
217
|
+
const { chainId } = this.requireEvmContext();
|
|
218
|
+
if (!isSettlementChain(chainId)) {
|
|
219
|
+
throw new Error(`chainId ${chainId} is not a settlement chain; multichain ops are unsupported`);
|
|
205
220
|
}
|
|
206
|
-
return
|
|
221
|
+
return chainId;
|
|
207
222
|
}
|
|
208
223
|
buildSameChainDepositTxn(params) {
|
|
209
224
|
return buildSameChainDepositTxn({ ...params, chainId: this.requireSettlementChainId() });
|
|
@@ -228,15 +243,19 @@ export class GmxApiSdk {
|
|
|
228
243
|
return executeCrossChainDeposit(this.ctx, signer, request);
|
|
229
244
|
}
|
|
230
245
|
prepareCrossChainDeposit(request) {
|
|
246
|
+
this.requireEvmContext();
|
|
231
247
|
return prepareCrossChainDeposit(this.ctx, request);
|
|
232
248
|
}
|
|
233
249
|
prepareCrossChainWithdraw(request) {
|
|
250
|
+
this.requireEvmContext();
|
|
234
251
|
return prepareCrossChainWithdraw(this.ctx, request);
|
|
235
252
|
}
|
|
236
253
|
signCrossChainWithdraw(prepared, signer) {
|
|
254
|
+
this.requireEvmContext();
|
|
237
255
|
return signCrossChainWithdrawPrepared(prepared, signer);
|
|
238
256
|
}
|
|
239
257
|
submitCrossChainWithdraw(request) {
|
|
258
|
+
this.requireEvmContext();
|
|
240
259
|
return submitCrossChainWithdraw(this.ctx, request);
|
|
241
260
|
}
|
|
242
261
|
executeCrossChainWithdraw(signer, request) {
|
|
@@ -277,11 +296,12 @@ export class GmxApiSdk {
|
|
|
277
296
|
return refreshSdkSubaccountState(this.subaccountClient, account);
|
|
278
297
|
}
|
|
279
298
|
prepareSubaccountApproval(request) {
|
|
299
|
+
this.requireEvmContext();
|
|
280
300
|
return prepareSubaccountApproval(this.ctx, request);
|
|
281
301
|
}
|
|
282
302
|
signSubaccountApproval(prepared, signer) {
|
|
283
303
|
return signSubaccountApproval(prepared, signer, {
|
|
284
|
-
chainId: this.
|
|
304
|
+
chainId: this.requireEvmContext().chainId,
|
|
285
305
|
expectedSubaccountAddress: this._subaccount?.address,
|
|
286
306
|
});
|
|
287
307
|
}
|