@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,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMarketAddressByName = exports.getMarketWithTiers = exports.composeFullMarketsInfoData = exports.hydrateMarketInfo = exports.mergeMarketsConfigValues = exports.composeRawMarketsInfoData = exports.composeRawMarketInfo = exports.getMarketTicker = exports.getMarket24Stats = exports.getIsMarketDeprecated = exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getMarketInfoWithSwapDelta = exports.getMarketInfoWithOpenInterestDelta = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getPositiveMarketPnl = exports.getMarketPnl = exports.getOiInTokensFromRawValues = exports.getOiUsdFromRawValues = exports.getMarketDivisor = exports.getReservedUsd = exports.getAvailableUsdLiquidityForCollateral = exports.getOppositeCollateralFromConfig = exports.getOppositeCollateral = exports.getMaxAllowedLeverage = exports.getMaxLeverageByMinCollateralFactor = exports.getCappedPoolPnl = exports.getPoolUsdWithoutPnl = exports.getTokenPoolType = exports.getContractMarketPrices = exports.getMarketPoolName = exports.getMarketBaseName = exports.getMarketIndexName = exports.getMarketFullName = void 0;
|
|
3
|
+
exports.getMarketAddressByName = exports.getMarketWithTiers = exports.getMarketPositionLimits = exports.composeFullMarketsInfoData = exports.hydrateMarketInfo = exports.mergeMarketsConfigValues = exports.composeRawMarketsInfoData = exports.composeRawMarketInfo = exports.getMarketTicker = exports.getMarket24Stats = exports.getIsMarketDeprecated = exports.getIsMarketAvailableForExpressSwaps = exports.getPriceForPnl = exports.getOpenInterestForBalance = exports.getMarketInfoWithSwapDelta = exports.getMarketInfoWithOpenInterestDelta = exports.getOpenInterestInTokens = exports.getOpenInterestUsd = exports.getPositiveMarketPnl = exports.getMarketPnl = exports.getOiInTokensFromRawValues = exports.getOiUsdFromRawValues = exports.getMarketDivisor = exports.getReservedUsd = exports.getAvailableUsdLiquidityForCollateral = exports.getOppositeCollateralFromConfig = exports.getOppositeCollateral = exports.getMaxAllowedLeverage = exports.getMaxLeverageByMinCollateralFactor = exports.getCappedPoolPnl = exports.getPoolUsdWithoutPnl = exports.getTokenPoolType = exports.getContractMarketPrices = exports.getMarketPoolName = exports.getMarketBaseName = exports.getMarketIndexName = exports.getMarketFullName = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
const factors_1 = require("../../configs/factors");
|
|
6
6
|
const markets_1 = require("../../configs/markets");
|
|
7
7
|
const tokens_1 = require("../../configs/tokens");
|
|
8
8
|
const bigmath_1 = require("../bigmath");
|
|
9
9
|
const fees_1 = require("../fees");
|
|
10
|
+
const gmsol_1 = require("../gmsol");
|
|
10
11
|
const numbers_1 = require("../numbers");
|
|
11
12
|
const objects_1 = require("../objects");
|
|
12
13
|
const time_1 = require("../time");
|
|
@@ -91,7 +92,8 @@ function getCappedPoolPnl(p) {
|
|
|
91
92
|
return poolPnl;
|
|
92
93
|
}
|
|
93
94
|
const maxPnlFactor = isLong ? marketInfo.maxPnlFactorForTradersLong : marketInfo.maxPnlFactorForTradersShort;
|
|
94
|
-
const
|
|
95
|
+
const maxPnlUsd = (0, numbers_1.applyFactor)(poolUsd, maxPnlFactor);
|
|
96
|
+
const maxPnl = marketInfo.tradeMath === "gmsol" ? (0, gmsol_1.getGmsolUsdAmount)(maxPnlUsd) : maxPnlUsd;
|
|
95
97
|
return poolPnl > maxPnl ? maxPnl : poolPnl;
|
|
96
98
|
}
|
|
97
99
|
exports.getCappedPoolPnl = getCappedPoolPnl;
|
|
@@ -217,7 +219,9 @@ function getMarketPnl(marketInfo, isLong, forMaxPoolValue) {
|
|
|
217
219
|
const maximize = !forMaxPoolValue;
|
|
218
220
|
const openInterestUsd = getOpenInterestUsd(marketInfo, isLong);
|
|
219
221
|
const openInterestInTokens = getOpenInterestInTokens(marketInfo, isLong);
|
|
220
|
-
if (
|
|
222
|
+
if (marketInfo.tradeMath === "gmsol"
|
|
223
|
+
? openInterestUsd === 0n && openInterestInTokens === 0n
|
|
224
|
+
: openInterestUsd === 0n || openInterestInTokens === 0n) {
|
|
221
225
|
return 0n;
|
|
222
226
|
}
|
|
223
227
|
const price = getPriceForPnl(marketInfo.indexToken.prices, isLong, maximize);
|
|
@@ -249,7 +253,9 @@ function getMarketPnlByCollateralToken(marketInfo, isLong, isLongCollateral, for
|
|
|
249
253
|
if (openInterestUsd === undefined || openInterestInTokens === undefined) {
|
|
250
254
|
return undefined;
|
|
251
255
|
}
|
|
252
|
-
if (
|
|
256
|
+
if (marketInfo.tradeMath === "gmsol"
|
|
257
|
+
? openInterestUsd === 0n && openInterestInTokens === 0n
|
|
258
|
+
: openInterestUsd === 0n || openInterestInTokens === 0n) {
|
|
253
259
|
return 0n;
|
|
254
260
|
}
|
|
255
261
|
const price = getPriceForPnl(marketInfo.indexToken.prices, isLong, maximize);
|
|
@@ -322,7 +328,7 @@ function getMarketInfoWithOpenInterestDelta({ marketInfo, collateralToken, isLon
|
|
|
322
328
|
? marketInfo.virtualInventoryForPositionsInTokens !== 0n
|
|
323
329
|
: marketInfo.virtualInventoryForPositions !== 0n;
|
|
324
330
|
if (hasVirtualInventory) {
|
|
325
|
-
if (marketInfo.useOpenInterestInTokensForBalance) {
|
|
331
|
+
if (marketInfo.tradeMath !== "gmsol" && marketInfo.useOpenInterestInTokensForBalance) {
|
|
326
332
|
next.virtualInventoryForPositionsInTokens =
|
|
327
333
|
marketInfo.virtualInventoryForPositionsInTokens + (isLong ? -sizeDeltaInTokens : sizeDeltaInTokens);
|
|
328
334
|
}
|
|
@@ -379,7 +385,7 @@ function getMarketInfoWithSwapDelta(marketInfo, swapStep) {
|
|
|
379
385
|
}
|
|
380
386
|
exports.getMarketInfoWithSwapDelta = getMarketInfoWithSwapDelta;
|
|
381
387
|
function getOpenInterestForBalance(marketInfo, isLong) {
|
|
382
|
-
if (marketInfo.useOpenInterestInTokensForBalance) {
|
|
388
|
+
if (marketInfo.tradeMath !== "gmsol" && marketInfo.useOpenInterestInTokensForBalance) {
|
|
383
389
|
const interestInTokens = isLong ? marketInfo.longInterestInTokens : marketInfo.shortInterestInTokens;
|
|
384
390
|
const indexTokenPrice = (0, tokens_2.getMidPrice)(marketInfo.indexToken.prices);
|
|
385
391
|
return (0, tokens_2.convertToUsd)(interestInTokens, marketInfo.indexToken.decimals, indexTokenPrice);
|
|
@@ -519,7 +525,10 @@ exports.mergeMarketsConfigValues = mergeMarketsConfigValues;
|
|
|
519
525
|
function hydrateMarketInfo({ chainId, rawMarketInfo, tokensData, claimableFundingData, }) {
|
|
520
526
|
const longToken = (0, objects_1.getByKey)(tokensData, rawMarketInfo.longTokenAddress);
|
|
521
527
|
const shortToken = (0, objects_1.getByKey)(tokensData, rawMarketInfo.shortTokenAddress);
|
|
522
|
-
const
|
|
528
|
+
const indexTokenAddress = chainId === undefined
|
|
529
|
+
? rawMarketInfo.indexTokenAddress
|
|
530
|
+
: (0, tokens_1.convertTokenAddress)(chainId, rawMarketInfo.indexTokenAddress, "native");
|
|
531
|
+
const indexToken = (0, objects_1.getByKey)(tokensData, indexTokenAddress);
|
|
523
532
|
if (!longToken || !shortToken || !indexToken) {
|
|
524
533
|
return undefined;
|
|
525
534
|
}
|
|
@@ -552,6 +561,13 @@ function composeFullMarketsInfoData({ chainId, marketsAddresses, rawMarketsInfoD
|
|
|
552
561
|
return data;
|
|
553
562
|
}
|
|
554
563
|
exports.composeFullMarketsInfoData = composeFullMarketsInfoData;
|
|
564
|
+
function getMarketPositionLimits(market, defaults) {
|
|
565
|
+
return {
|
|
566
|
+
minCollateralUsd: market?.minCollateralUsd ?? defaults?.minCollateralUsd,
|
|
567
|
+
minPositionSizeUsd: market?.minPositionSizeUsd ?? defaults?.minPositionSizeUsd,
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
exports.getMarketPositionLimits = getMarketPositionLimits;
|
|
555
571
|
function getMarketWithTiers(marketInfo, constants) {
|
|
556
572
|
const maxLeverage = getMaxLeverageByMinCollateralFactor(marketInfo.minCollateralFactor, marketInfo.marketTokenAddress);
|
|
557
573
|
const maxLeverageBigint = BigInt(maxLeverage);
|
|
@@ -572,8 +588,8 @@ function getMarketWithTiers(marketInfo, constants) {
|
|
|
572
588
|
listingDate: undefined,
|
|
573
589
|
isSpotOnly: marketInfo.isSpotOnly,
|
|
574
590
|
leverageTiers,
|
|
575
|
-
minPositionSizeUsd: constants.minPositionSizeUsd,
|
|
576
|
-
minCollateralUsd: constants.minCollateralUsd,
|
|
591
|
+
minPositionSizeUsd: marketInfo.minPositionSizeUsd ?? constants.minPositionSizeUsd,
|
|
592
|
+
minCollateralUsd: marketInfo.minCollateralUsd ?? constants.minCollateralUsd,
|
|
577
593
|
};
|
|
578
594
|
}
|
|
579
595
|
exports.getMarketWithTiers = getMarketWithTiers;
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fetchApiOrders = void 0;
|
|
4
|
+
const solana_1 = require("../http/solana");
|
|
4
5
|
const numbers_1 = require("../numbers");
|
|
5
6
|
async function fetchApiOrders(ctx, params) {
|
|
6
7
|
const orders = await ctx.api.fetchJson("/v1/orders", {
|
|
7
8
|
query: {
|
|
8
9
|
address: params.address,
|
|
10
|
+
minContextSlot: params.minContextSlot,
|
|
9
11
|
},
|
|
12
|
+
onResponse: (0, solana_1.getSolanaResponseHandler)(params.onResponse),
|
|
10
13
|
});
|
|
11
14
|
return orders.map((o) => (0, numbers_1.deserializeBigIntsInObject)(o, { handleInts: true }));
|
|
12
15
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const viem_1 = require("viem");
|
|
4
|
+
const vitest_1 = require("vitest");
|
|
5
|
+
const mock_1 = require("../../../test/mock");
|
|
6
|
+
const numbers_1 = require("../../numbers");
|
|
7
|
+
const positions_1 = require("..");
|
|
8
|
+
(0, vitest_1.describe)("gmsol liquidation price", () => {
|
|
9
|
+
(0, vitest_1.it)("solves index collateral funding at the liquidation price without changing EVM math FESOL-57", () => {
|
|
10
|
+
// Rust 8af1e70c: $1,000 size / 10 tokens, $5 floor, $2 borrowing and $0.60 close fee.
|
|
11
|
+
for (const [isLong, collateral, funding, expected] of [
|
|
12
|
+
[true, 10n, 1n, (0, viem_1.parseUnits)("53.03157894736842", 30)],
|
|
13
|
+
[false, 10n, 1n, (0, viem_1.parseUnits)("992.4", 30)],
|
|
14
|
+
[true, 10n, 0n, (0, viem_1.parseUnits)("50.38", 30)],
|
|
15
|
+
[false, 10n, 0n, undefined],
|
|
16
|
+
[false, 11n, 1n, undefined],
|
|
17
|
+
[false, 12n, 1n, undefined],
|
|
18
|
+
[true, 10n, 20n, undefined],
|
|
19
|
+
]) {
|
|
20
|
+
for (const price of [100n, 120n]) {
|
|
21
|
+
const marketInfo = {
|
|
22
|
+
...(0, mock_1.mockMarginCheckMarketInfo)((0, mock_1.mockTokensData)({
|
|
23
|
+
BTC: { decimals: 6, prices: { minPrice: price * numbers_1.PRECISION, maxPrice: price * numbers_1.PRECISION } },
|
|
24
|
+
}), {
|
|
25
|
+
longInterestUsd: 20000n * numbers_1.PRECISION,
|
|
26
|
+
shortInterestUsd: 10000n * numbers_1.PRECISION,
|
|
27
|
+
positionFeeFactorForBalanceWasImproved: 6n * 10n ** 26n,
|
|
28
|
+
positionFeeFactorForBalanceWasNotImproved: 6n * 10n ** 26n,
|
|
29
|
+
}),
|
|
30
|
+
tradeMath: "gmsol",
|
|
31
|
+
minCollateralUsd: 5n * numbers_1.PRECISION,
|
|
32
|
+
};
|
|
33
|
+
const params = {
|
|
34
|
+
marketInfo,
|
|
35
|
+
collateralToken: marketInfo.indexToken,
|
|
36
|
+
collateralAmount: collateral * 1000000n,
|
|
37
|
+
collateralUsd: collateral * price * numbers_1.PRECISION,
|
|
38
|
+
sizeInUsd: 1000n * numbers_1.PRECISION,
|
|
39
|
+
sizeInTokens: 10000000n,
|
|
40
|
+
pendingFundingFeesUsd: funding * price * numbers_1.PRECISION,
|
|
41
|
+
pendingBorrowingFeesUsd: 2n * numbers_1.PRECISION,
|
|
42
|
+
pendingImpactAmount: 0n,
|
|
43
|
+
minCollateralUsd: numbers_1.PRECISION,
|
|
44
|
+
isLong,
|
|
45
|
+
userReferralInfo: undefined,
|
|
46
|
+
};
|
|
47
|
+
for (const fundingFeeAmount of [undefined, funding * 1000000n]) {
|
|
48
|
+
const actual = (0, positions_1.getLiquidationPrice)({ ...params, fundingFeeAmount });
|
|
49
|
+
if (expected === undefined)
|
|
50
|
+
(0, vitest_1.expect)(actual).toBeUndefined();
|
|
51
|
+
else {
|
|
52
|
+
(0, vitest_1.expect)(actual).toBeDefined();
|
|
53
|
+
// One Rust unit-price step for a token with six decimals.
|
|
54
|
+
(0, vitest_1.expect)(actual - expected).toBeGreaterThanOrEqual(0n);
|
|
55
|
+
(0, vitest_1.expect)(actual - expected).toBeLessThan(10n ** 16n);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (isLong && collateral === 10n && funding === 1n) {
|
|
59
|
+
(0, vitest_1.expect)((0, positions_1.getLiquidationPrice)({ ...params, marketInfo: { ...marketInfo, tradeMath: undefined } })).toBe((0, viem_1.parseUnits)(price === 100n ? "55.38" : "56.38", 30));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fetchApiPositionsInfo = void 0;
|
|
4
|
+
const solana_1 = require("../http/solana");
|
|
4
5
|
const validation_1 = require("../http/validation");
|
|
5
6
|
const numbers_1 = require("../numbers");
|
|
6
7
|
async function fetchApiPositionsInfo(ctx, params) {
|
|
@@ -8,7 +9,9 @@ async function fetchApiPositionsInfo(ctx, params) {
|
|
|
8
9
|
query: {
|
|
9
10
|
address: params.address,
|
|
10
11
|
includeRelatedOrders: params.includeRelatedOrders || undefined,
|
|
12
|
+
minContextSlot: params.minContextSlot,
|
|
11
13
|
},
|
|
14
|
+
onResponse: (0, solana_1.getSolanaResponseHandler)(params.onResponse),
|
|
12
15
|
});
|
|
13
16
|
(0, validation_1.assertApiRecords)(positions, "/v1/positions");
|
|
14
17
|
return positions.map((position, index) => {
|
|
@@ -4,6 +4,7 @@ exports.getPositionInfo = exports.getContractPositionDynamicFees = exports.getMi
|
|
|
4
4
|
const factors_1 = require("../../configs/factors");
|
|
5
5
|
const bigmath_1 = require("../bigmath");
|
|
6
6
|
const fees_1 = require("../fees");
|
|
7
|
+
const gmsol_1 = require("../gmsol");
|
|
7
8
|
const markets_1 = require("../markets");
|
|
8
9
|
const numbers_1 = require("../numbers");
|
|
9
10
|
const prices_1 = require("../prices");
|
|
@@ -32,7 +33,9 @@ function getPositionPnlUsd(p) {
|
|
|
32
33
|
if (totalPnl <= 0) {
|
|
33
34
|
return totalPnl;
|
|
34
35
|
}
|
|
35
|
-
const poolPnl =
|
|
36
|
+
const poolPnl = marketInfo.tradeMath === "gmsol"
|
|
37
|
+
? (0, markets_1.getMarketPnl)(marketInfo, isLong, false)
|
|
38
|
+
: (0, markets_1.getPositiveMarketPnl)(marketInfo, isLong, false);
|
|
36
39
|
const poolUsd = (0, markets_1.getPoolUsdWithoutPnl)(marketInfo, isLong, "minPrice");
|
|
37
40
|
const cappedPnl = (0, markets_1.getCappedPoolPnl)({
|
|
38
41
|
marketInfo,
|
|
@@ -40,8 +43,11 @@ function getPositionPnlUsd(p) {
|
|
|
40
43
|
poolPnl,
|
|
41
44
|
isLong,
|
|
42
45
|
});
|
|
43
|
-
if (cappedPnl !== poolPnl && cappedPnl > 0 && poolPnl > 0) {
|
|
46
|
+
if (cappedPnl !== poolPnl && (cappedPnl > 0 || marketInfo.tradeMath === "gmsol") && poolPnl > 0) {
|
|
44
47
|
totalPnl = bigmath_1.bigMath.mulDiv(totalPnl, cappedPnl, poolPnl);
|
|
48
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
49
|
+
totalPnl = (0, gmsol_1.getGmsolUsdAmount)(totalPnl);
|
|
50
|
+
}
|
|
45
51
|
}
|
|
46
52
|
return totalPnl;
|
|
47
53
|
}
|
|
@@ -88,7 +94,10 @@ function getLeverage(p) {
|
|
|
88
94
|
exports.getLeverage = getLeverage;
|
|
89
95
|
function getLiquidationPriceImpactDeltaUsd(p) {
|
|
90
96
|
const { marketInfo, sizeInUsd, sizeInTokens, pendingImpactAmount, isLong, useMaxPriceImpact } = p;
|
|
91
|
-
|
|
97
|
+
let maxNegativePriceImpactUsd = -1n * (0, numbers_1.applyFactor)(sizeInUsd, marketInfo.maxPositionImpactFactorForLiquidations);
|
|
98
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
99
|
+
maxNegativePriceImpactUsd = (0, gmsol_1.getGmsolUsdAmount)(maxNegativePriceImpactUsd);
|
|
100
|
+
}
|
|
92
101
|
if (useMaxPriceImpact) {
|
|
93
102
|
return maxNegativePriceImpactUsd;
|
|
94
103
|
}
|
|
@@ -99,7 +108,7 @@ function getLiquidationPriceImpactDeltaUsd(p) {
|
|
|
99
108
|
if (priceImpactDeltaUsd > 0) {
|
|
100
109
|
priceImpactDeltaUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeInUsd, priceImpactDeltaUsd);
|
|
101
110
|
}
|
|
102
|
-
const pendingImpactUsd = (0, tokens_1.convertToUsd)(pendingImpactAmount, marketInfo.indexToken.decimals, pendingImpactAmount > 0 ? marketInfo.indexToken.prices.minPrice : marketInfo.indexToken.prices.maxPrice);
|
|
111
|
+
const pendingImpactUsd = (0, tokens_1.convertToUsd)(marketInfo.tradeMath === "gmsol" ? 0n : pendingImpactAmount, marketInfo.indexToken.decimals, pendingImpactAmount > 0 ? marketInfo.indexToken.prices.minPrice : marketInfo.indexToken.prices.maxPrice);
|
|
103
112
|
priceImpactDeltaUsd = priceImpactDeltaUsd + pendingImpactUsd;
|
|
104
113
|
if (priceImpactDeltaUsd > 0) {
|
|
105
114
|
return 0n;
|
|
@@ -116,7 +125,11 @@ function getLiquidationPrice(p) {
|
|
|
116
125
|
return undefined;
|
|
117
126
|
}
|
|
118
127
|
const { indexToken } = marketInfo;
|
|
119
|
-
const closingFeeUsd = (0, fees_1.getPositionFee)(marketInfo, sizeInUsd,
|
|
128
|
+
const closingFeeUsd = (0, fees_1.getPositionFee)(marketInfo, sizeInUsd, marketInfo.tradeMath === "gmsol" &&
|
|
129
|
+
(0, fees_1.getPriceImpactForPosition)(marketInfo, -sizeInUsd, isLong, {
|
|
130
|
+
fallbackToZero: true,
|
|
131
|
+
sizeDeltaInTokens: sizeInTokens,
|
|
132
|
+
}).balanceWasImproved, userReferralInfo, undefined, undefined, p.feeDiscountFactor).positionFeeUsd;
|
|
120
133
|
const totalPendingFeesUsd = getPositionPendingFeesUsd({ pendingFundingFeesUsd, pendingBorrowingFeesUsd });
|
|
121
134
|
const totalFeesUsd = totalPendingFeesUsd + closingFeeUsd;
|
|
122
135
|
const priceImpactDeltaUsd = getLiquidationPriceImpactDeltaUsd({
|
|
@@ -127,12 +140,34 @@ function getLiquidationPrice(p) {
|
|
|
127
140
|
isLong,
|
|
128
141
|
useMaxPriceImpact,
|
|
129
142
|
});
|
|
143
|
+
const effectiveMinCollateralUsd = marketInfo.tradeMath === "gmsol" ? marketInfo.minCollateralUsd ?? minCollateralUsd : minCollateralUsd;
|
|
130
144
|
let liquidationCollateralUsd = (0, numbers_1.applyFactor)(sizeInUsd, marketInfo.minCollateralFactorForLiquidation);
|
|
131
|
-
if (
|
|
132
|
-
liquidationCollateralUsd =
|
|
145
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
146
|
+
liquidationCollateralUsd = (0, gmsol_1.getGmsolUsdAmount)(liquidationCollateralUsd);
|
|
147
|
+
}
|
|
148
|
+
if (liquidationCollateralUsd < effectiveMinCollateralUsd) {
|
|
149
|
+
liquidationCollateralUsd = effectiveMinCollateralUsd;
|
|
133
150
|
}
|
|
134
151
|
let liquidationPrice;
|
|
135
|
-
if ((0, tokens_1.getIsEquivalentTokens)(collateralToken, indexToken)) {
|
|
152
|
+
if (marketInfo.tradeMath === "gmsol" && (0, tokens_1.getIsEquivalentTokens)(collateralToken, indexToken)) {
|
|
153
|
+
const fundingAmount = p.fundingFeeAmount ??
|
|
154
|
+
(0, tokens_1.convertToTokenAmount)(pendingFundingFeesUsd, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
155
|
+
if (fundingAmount === undefined || collateralUsd + priceImpactDeltaUsd - totalFeesUsd < 0n) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
// Collateral and funding both move with the index price being solved for.
|
|
159
|
+
const netCollateralAmount = collateralAmount - fundingAmount;
|
|
160
|
+
const denominator = isLong ? sizeInTokens + netCollateralAmount : sizeInTokens - netCollateralAmount;
|
|
161
|
+
if (denominator <= 0n) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const surchargeUsd = pendingBorrowingFeesUsd + closingFeeUsd - priceImpactDeltaUsd;
|
|
165
|
+
const numerator = isLong
|
|
166
|
+
? sizeInUsd + liquidationCollateralUsd + surchargeUsd
|
|
167
|
+
: sizeInUsd - liquidationCollateralUsd - surchargeUsd;
|
|
168
|
+
liquidationPrice = bigmath_1.bigMath.mulDiv(numerator, (0, numbers_1.expandDecimals)(1, indexToken.decimals), denominator);
|
|
169
|
+
}
|
|
170
|
+
else if ((0, tokens_1.getIsEquivalentTokens)(collateralToken, indexToken)) {
|
|
136
171
|
if (isLong) {
|
|
137
172
|
const denominator = sizeInTokens + collateralAmount;
|
|
138
173
|
if (denominator == 0n) {
|
|
@@ -179,7 +214,11 @@ function getMinCollateralUsdForLiquidationPrice(p) {
|
|
|
179
214
|
if (sizeInUsd <= 0 || sizeInTokens <= 0) {
|
|
180
215
|
return 0n;
|
|
181
216
|
}
|
|
182
|
-
const closingFeeUsd = (0, fees_1.getPositionFee)(marketInfo, sizeInUsd,
|
|
217
|
+
const closingFeeUsd = (0, fees_1.getPositionFee)(marketInfo, sizeInUsd, marketInfo.tradeMath === "gmsol" &&
|
|
218
|
+
(0, fees_1.getPriceImpactForPosition)(marketInfo, -sizeInUsd, isLong, {
|
|
219
|
+
fallbackToZero: true,
|
|
220
|
+
sizeDeltaInTokens: sizeInTokens,
|
|
221
|
+
}).balanceWasImproved, userReferralInfo, undefined, undefined, p.feeDiscountFactor).positionFeeUsd;
|
|
183
222
|
const priceImpactDeltaUsd = getLiquidationPriceImpactDeltaUsd({
|
|
184
223
|
marketInfo,
|
|
185
224
|
sizeInUsd,
|
|
@@ -187,9 +226,13 @@ function getMinCollateralUsdForLiquidationPrice(p) {
|
|
|
187
226
|
pendingImpactAmount,
|
|
188
227
|
isLong,
|
|
189
228
|
});
|
|
229
|
+
const effectiveMinCollateralUsd = marketInfo.tradeMath === "gmsol" ? marketInfo.minCollateralUsd ?? minCollateralUsd : minCollateralUsd;
|
|
190
230
|
let liquidationCollateralUsd = (0, numbers_1.applyFactor)(sizeInUsd, marketInfo.minCollateralFactorForLiquidation);
|
|
191
|
-
if (
|
|
192
|
-
liquidationCollateralUsd =
|
|
231
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
232
|
+
liquidationCollateralUsd = (0, gmsol_1.getGmsolUsdAmount)(liquidationCollateralUsd);
|
|
233
|
+
}
|
|
234
|
+
if (liquidationCollateralUsd < effectiveMinCollateralUsd) {
|
|
235
|
+
liquidationCollateralUsd = effectiveMinCollateralUsd;
|
|
193
236
|
}
|
|
194
237
|
return liquidationCollateralUsd - pnl - priceImpactDeltaUsd + closingFeeUsd;
|
|
195
238
|
}
|
|
@@ -203,7 +246,11 @@ function getIsPositionBelowMinCollateralForLeverage(position, collateralDeltaAmo
|
|
|
203
246
|
return true;
|
|
204
247
|
}
|
|
205
248
|
const collateralUsdAfterDelta = (0, tokens_1.convertToUsd)(collateralAmount - collateralDeltaAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
206
|
-
|
|
249
|
+
let minCollateralUsdForOpenInterest = (0, numbers_1.applyFactor)(sizeInUsd, getMinCollateralFactorForPosition(position, 0n));
|
|
250
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
251
|
+
minCollateralUsdForOpenInterest = (0, gmsol_1.getGmsolUsdAmount)(minCollateralUsdForOpenInterest);
|
|
252
|
+
}
|
|
253
|
+
if (collateralUsdAfterDelta < minCollateralUsdForOpenInterest) {
|
|
207
254
|
return true;
|
|
208
255
|
}
|
|
209
256
|
const collateralDeltaUsd = (0, tokens_1.convertToUsd)(collateralDeltaAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
@@ -215,14 +262,18 @@ function getIsPositionBelowMinCollateralForLeverage(position, collateralDeltaAmo
|
|
|
215
262
|
isLong,
|
|
216
263
|
});
|
|
217
264
|
const marginUsd = remainingCollateralUsd - collateralDeltaUsd + pnl + priceImpactDeltaUsd - closingFeeUsd;
|
|
218
|
-
|
|
265
|
+
let minCollateralUsdForLeverage = (0, numbers_1.applyFactor)(sizeInUsd, marketInfo.minCollateralFactor);
|
|
266
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
267
|
+
minCollateralUsdForLeverage = (0, gmsol_1.getGmsolUsdAmount)(minCollateralUsdForLeverage);
|
|
268
|
+
}
|
|
269
|
+
return marginUsd <= 0n || marginUsd < minCollateralUsdForLeverage;
|
|
219
270
|
}
|
|
220
271
|
exports.getIsPositionBelowMinCollateralForLeverage = getIsPositionBelowMinCollateralForLeverage;
|
|
221
272
|
function getNetPriceImpactDeltaUsdForDecrease({ marketInfo, sizeInUsd, pendingImpactAmount, priceImpactDeltaUsd, sizeDeltaUsd, }) {
|
|
222
273
|
const { proportionalPendingImpactDeltaUsd } = (0, fees_1.getProportionalPendingImpactValues)({
|
|
223
274
|
sizeInUsd,
|
|
224
275
|
sizeDeltaUsd,
|
|
225
|
-
pendingImpactAmount,
|
|
276
|
+
pendingImpactAmount: marketInfo.tradeMath === "gmsol" ? 0n : pendingImpactAmount,
|
|
226
277
|
indexToken: marketInfo.indexToken,
|
|
227
278
|
});
|
|
228
279
|
let totalImpactDeltaUsd = priceImpactDeltaUsd + proportionalPendingImpactDeltaUsd;
|
|
@@ -235,6 +286,9 @@ function getNetPriceImpactDeltaUsdForDecrease({ marketInfo, sizeInUsd, pendingIm
|
|
|
235
286
|
totalImpactDeltaUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeDeltaUsd, totalImpactDeltaUsd);
|
|
236
287
|
}
|
|
237
288
|
totalImpactDeltaUsd = (0, fees_1.capPositionImpactUsdByMaxImpactPool)(marketInfo, totalImpactDeltaUsd);
|
|
289
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
290
|
+
totalImpactDeltaUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeDeltaUsd, totalImpactDeltaUsd);
|
|
291
|
+
}
|
|
238
292
|
return {
|
|
239
293
|
totalImpactDeltaUsd,
|
|
240
294
|
proportionalPendingImpactDeltaUsd,
|
|
@@ -246,6 +300,9 @@ function getPriceImpactDiffUsd({ totalImpactDeltaUsd, marketInfo, sizeDeltaUsd,
|
|
|
246
300
|
if (totalImpactDeltaUsd > 0) {
|
|
247
301
|
return 0n;
|
|
248
302
|
}
|
|
303
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
304
|
+
return ((0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeDeltaUsd, totalImpactDeltaUsd) - totalImpactDeltaUsd);
|
|
305
|
+
}
|
|
249
306
|
const { maxNegativeImpactFactor } = (0, fees_1.getMaxPositionImpactFactors)(marketInfo);
|
|
250
307
|
const maxNegativeImpactUsd = -(0, numbers_1.applyFactor)(sizeDeltaUsd, maxNegativeImpactFactor);
|
|
251
308
|
let priceImpactDiffUsd = 0n;
|
|
@@ -263,6 +320,9 @@ function getMinCollateralFactorForPosition(position, openInterestDelta) {
|
|
|
263
320
|
? marketInfo.minCollateralFactorForOpenInterestLong
|
|
264
321
|
: marketInfo.minCollateralFactorForOpenInterestShort;
|
|
265
322
|
let minCollateralFactor = bigmath_1.bigMath.mulDiv(openInterest, minCollateralFactorMultiplier, numbers_1.PRECISION);
|
|
323
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
324
|
+
minCollateralFactor = (0, gmsol_1.getGmsolUsdAmount)(minCollateralFactor);
|
|
325
|
+
}
|
|
266
326
|
const minCollateralFactorForMarket = marketInfo.minCollateralFactor;
|
|
267
327
|
if (minCollateralFactorForMarket > minCollateralFactor) {
|
|
268
328
|
minCollateralFactor = minCollateralFactorForMarket;
|
|
@@ -275,7 +335,7 @@ function getFundingAmount(latestFundingAmountPerSize, positionFundingAmountPerSi
|
|
|
275
335
|
// fundingAmountPerSize values are stored with FLOAT_PRECISION_SQRT precision
|
|
276
336
|
return bigmath_1.bigMath.mulDiv(positionSizeInUsd, fundingDiffFactor, numbers_1.PRECISION * numbers_1.FLOAT_PRECISION_SQRT);
|
|
277
337
|
}
|
|
278
|
-
function getContractPositionDynamicFees({ position, marketInfo, marketFeeState, referralInfo, }) {
|
|
338
|
+
function getContractPositionDynamicFees({ position, marketInfo, marketFeeState, referralInfo, feeDiscountFactor, }) {
|
|
279
339
|
const { sizeInUsd, isLong, borrowingFactor } = position;
|
|
280
340
|
const isCollateralLongToken = position.collateralTokenAddress === marketInfo.longToken.address;
|
|
281
341
|
const collateralToken = isCollateralLongToken ? marketInfo.longToken : marketInfo.shortToken;
|
|
@@ -313,7 +373,7 @@ function getContractPositionDynamicFees({ position, marketInfo, marketFeeState,
|
|
|
313
373
|
fallbackToZero: true,
|
|
314
374
|
sizeDeltaInTokens: position.sizeInTokens,
|
|
315
375
|
});
|
|
316
|
-
const { positionFeeUsd, discountUsd, uiFeeUsd } = (0, fees_1.getPositionFee)(marketInfo, sizeInUsd, balanceWasImproved, referralInfo);
|
|
376
|
+
const { positionFeeUsd, discountUsd, uiFeeUsd } = (0, fees_1.getPositionFee)(marketInfo, sizeInUsd, balanceWasImproved, referralInfo, undefined, undefined, feeDiscountFactor);
|
|
317
377
|
const positionFeeAmount = (0, tokens_1.convertToTokenAmount)(positionFeeUsd, collateralToken.decimals, collateralMinPrice) ?? 0n;
|
|
318
378
|
const traderDiscountAmount = (0, tokens_1.convertToTokenAmount)(discountUsd, collateralToken.decimals, collateralMinPrice) ?? 0n;
|
|
319
379
|
const uiFeeAmount = (0, tokens_1.convertToTokenAmount)(uiFeeUsd, collateralToken.decimals, collateralMinPrice) ?? 0n;
|
|
@@ -368,8 +428,10 @@ function getPositionInfo(p) {
|
|
|
368
428
|
indexPrice: markPrice,
|
|
369
429
|
sizeDeltaUsd: position.sizeInUsd,
|
|
370
430
|
sizeDeltaInTokens: position.sizeInTokens,
|
|
431
|
+
positionSizeInUsd: position.sizeInUsd,
|
|
432
|
+
positionSizeInTokens: position.sizeInTokens,
|
|
371
433
|
});
|
|
372
|
-
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, position.sizeInUsd, closeAcceptablePriceInfo.balanceWasImproved, userReferralInfo, uiFeeFactor);
|
|
434
|
+
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, position.sizeInUsd, closeAcceptablePriceInfo.balanceWasImproved, userReferralInfo, uiFeeFactor, undefined, p.feeDiscountFactor);
|
|
373
435
|
const closingFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
374
436
|
const uiFeeUsd = positionFeeInfo.uiFeeUsd ?? 0n;
|
|
375
437
|
const netPriceImpactValues = getNetPriceImpactDeltaUsdForDecrease({
|
|
@@ -379,6 +441,9 @@ function getPositionInfo(p) {
|
|
|
379
441
|
sizeDeltaUsd: position.sizeInUsd,
|
|
380
442
|
priceImpactDeltaUsd: closeAcceptablePriceInfo.priceImpactDeltaUsd,
|
|
381
443
|
});
|
|
444
|
+
// gmsol withholds the impact beyond the cap from the close output
|
|
445
|
+
const closeCostImpactDeltaUsd = netPriceImpactValues.totalImpactDeltaUsd -
|
|
446
|
+
(marketInfo.tradeMath === "gmsol" ? netPriceImpactValues.priceImpactDiffUsd : 0n);
|
|
382
447
|
const netValue = getPositionNetValue({
|
|
383
448
|
collateralUsd,
|
|
384
449
|
pnl,
|
|
@@ -396,14 +461,14 @@ function getPositionInfo(p) {
|
|
|
396
461
|
pnl,
|
|
397
462
|
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
398
463
|
pendingFundingFeesUsd,
|
|
399
|
-
netPriceImpactDeltaUsd:
|
|
464
|
+
netPriceImpactDeltaUsd: closeCostImpactDeltaUsd,
|
|
400
465
|
closingFeeUsd,
|
|
401
466
|
});
|
|
402
467
|
const pnlAfterAllFees = getPositionPnlAfterAllFees({
|
|
403
468
|
pnl,
|
|
404
469
|
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
405
470
|
pendingFundingFeesUsd,
|
|
406
|
-
netPriceImpactDeltaUsd:
|
|
471
|
+
netPriceImpactDeltaUsd: closeCostImpactDeltaUsd,
|
|
407
472
|
closingFeeUsd,
|
|
408
473
|
});
|
|
409
474
|
const pnlAfterAllFeesPercentage = collateralUsd !== 0n ? (0, numbers_1.getBasisPoints)(pnlAfterAllFees, collateralUsd) : 0n;
|
|
@@ -444,9 +509,11 @@ function getPositionInfo(p) {
|
|
|
444
509
|
collateralAmount: position.collateralAmount,
|
|
445
510
|
pendingImpactAmount: position.pendingImpactAmount,
|
|
446
511
|
userReferralInfo,
|
|
512
|
+
feeDiscountFactor: p.feeDiscountFactor,
|
|
447
513
|
minCollateralUsd,
|
|
448
514
|
pendingBorrowingFeesUsd: position.pendingBorrowingFeesUsd,
|
|
449
515
|
pendingFundingFeesUsd,
|
|
516
|
+
fundingFeeAmount: position.fundingFeeAmount,
|
|
450
517
|
isLong: position.isLong,
|
|
451
518
|
});
|
|
452
519
|
const indexName = (0, markets_1.getMarketIndexName)({ indexToken, isSpotOnly: false });
|
|
@@ -463,6 +530,7 @@ function getPositionInfo(p) {
|
|
|
463
530
|
};
|
|
464
531
|
return {
|
|
465
532
|
...position,
|
|
533
|
+
pendingImpactAmount: marketInfo.tradeMath === "gmsol" ? 0n : position.pendingImpactAmount,
|
|
466
534
|
market,
|
|
467
535
|
marketInfo,
|
|
468
536
|
indexName,
|
|
@@ -498,6 +566,7 @@ function getPositionInfo(p) {
|
|
|
498
566
|
uiFeeUsd,
|
|
499
567
|
pendingFundingFeesUsd,
|
|
500
568
|
pendingClaimableFundingFeesUsd,
|
|
569
|
+
...(marketInfo.tradeMath === "gmsol" ? { fundingSettlement: "nextOrder" } : {}),
|
|
501
570
|
};
|
|
502
571
|
}
|
|
503
572
|
exports.getPositionInfo = getPositionInfo;
|
|
@@ -4,6 +4,7 @@ exports.getDefaultAcceptablePriceImpactBps = exports.getAcceptablePriceByPriceIm
|
|
|
4
4
|
const factors_1 = require("../../configs/factors");
|
|
5
5
|
const bigmath_1 = require("../bigmath");
|
|
6
6
|
const fees_1 = require("../fees");
|
|
7
|
+
const gmsol_1 = require("../gmsol");
|
|
7
8
|
const numbers_1 = require("../numbers");
|
|
8
9
|
const types_1 = require("../orders/types");
|
|
9
10
|
const tokens_1 = require("../tokens");
|
|
@@ -66,7 +67,16 @@ function getIncreaseEvaluationIndexPrice(p) {
|
|
|
66
67
|
}
|
|
67
68
|
exports.getIncreaseEvaluationIndexPrice = getIncreaseEvaluationIndexPrice;
|
|
68
69
|
function getAcceptablePriceInfo(p) {
|
|
69
|
-
|
|
70
|
+
if (p.marketInfo.tradeMath === "gmsol") {
|
|
71
|
+
p = { ...p, sizeDeltaUsd: (0, gmsol_1.getGmsolUsdAmount)(p.sizeDeltaUsd) };
|
|
72
|
+
}
|
|
73
|
+
const { isIncrease, isLong, indexPrice, sizeDeltaUsd, maxNegativePriceImpactBps } = p;
|
|
74
|
+
const marketInfo = p.marketInfo.tradeMath === "gmsol" && p.isLimit
|
|
75
|
+
? {
|
|
76
|
+
...p.marketInfo,
|
|
77
|
+
indexToken: { ...p.marketInfo.indexToken, prices: { minPrice: indexPrice, maxPrice: indexPrice } },
|
|
78
|
+
}
|
|
79
|
+
: p.marketInfo;
|
|
70
80
|
const { indexToken } = marketInfo;
|
|
71
81
|
const values = {
|
|
72
82
|
acceptablePrice: 0n,
|
|
@@ -102,11 +112,6 @@ function getAcceptablePriceInfo(p) {
|
|
|
102
112
|
shouldCapNegativeImpact: false,
|
|
103
113
|
sizeDeltaInTokens: p.sizeDeltaInTokens,
|
|
104
114
|
});
|
|
105
|
-
/**
|
|
106
|
-
* We display this value as price impact on action (increase or decrease)
|
|
107
|
-
* But for acceptable price calculation uncapped price impact is used
|
|
108
|
-
* Also on decrease action we calculate totalImpactUsd which will be deducted from the collateral
|
|
109
|
-
*/
|
|
110
115
|
values.priceImpactDeltaUsd = priceImpactDeltaUsd;
|
|
111
116
|
values.balanceWasImproved = balanceWasImproved;
|
|
112
117
|
if (values.priceImpactDeltaUsd > 0) {
|
|
@@ -115,24 +120,69 @@ function getAcceptablePriceInfo(p) {
|
|
|
115
120
|
else {
|
|
116
121
|
values.priceImpactDeltaAmount = (0, numbers_1.roundUpMagnitudeDivision)(values.priceImpactDeltaUsd * (0, numbers_1.expandDecimals)(1, indexToken.decimals), indexToken.prices.minPrice);
|
|
117
122
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
if (marketInfo.tradeMath === "gmsol") {
|
|
124
|
+
const priceParams = { ...p, marketInfo, priceImpactDeltaUsd };
|
|
125
|
+
const acceptablePrice = isIncrease
|
|
126
|
+
? getGmsolIncreaseAcceptablePrice(priceParams)
|
|
127
|
+
: getGmsolDecreaseAcceptablePrice(priceParams);
|
|
128
|
+
if (acceptablePrice === undefined) {
|
|
129
|
+
return values;
|
|
130
|
+
}
|
|
131
|
+
values.acceptablePrice = acceptablePrice;
|
|
132
|
+
values.acceptablePriceDeltaBps = (0, numbers_1.getBasisPoints)((values.acceptablePrice - indexPrice) * (shouldFlipPriceImpact ? -1n : 1n), indexPrice);
|
|
133
|
+
return values;
|
|
134
|
+
}
|
|
124
135
|
const acceptablePriceValues = getAcceptablePriceByPriceImpact({
|
|
125
136
|
isIncrease,
|
|
126
137
|
isLong,
|
|
127
138
|
indexPrice,
|
|
128
139
|
sizeDeltaUsd,
|
|
129
|
-
priceImpactDeltaUsd
|
|
140
|
+
priceImpactDeltaUsd,
|
|
130
141
|
});
|
|
131
142
|
values.acceptablePrice = acceptablePriceValues.acceptablePrice;
|
|
132
143
|
values.acceptablePriceDeltaBps = acceptablePriceValues.acceptablePriceDeltaBps;
|
|
133
144
|
return values;
|
|
134
145
|
}
|
|
135
146
|
exports.getAcceptablePriceInfo = getAcceptablePriceInfo;
|
|
147
|
+
function getGmsolIncreaseAcceptablePrice(p) {
|
|
148
|
+
const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, isLong, marketInfo } = p;
|
|
149
|
+
const { indexToken } = marketInfo;
|
|
150
|
+
const tokenUnit = (0, numbers_1.expandDecimals)(1, indexToken.decimals);
|
|
151
|
+
let baseSizeDeltaInTokens = p.sizeDeltaInTokens;
|
|
152
|
+
if (baseSizeDeltaInTokens == null) {
|
|
153
|
+
baseSizeDeltaInTokens = isLong
|
|
154
|
+
? bigmath_1.bigMath.mulDiv(sizeDeltaUsd, tokenUnit, indexPrice)
|
|
155
|
+
: (0, numbers_1.roundUpMagnitudeDivision)(sizeDeltaUsd * tokenUnit, indexPrice);
|
|
156
|
+
}
|
|
157
|
+
const sizeDeltaInTokens = (0, gmsol_1.getGmsolIncreaseSizeDeltaInTokens)({
|
|
158
|
+
baseSizeDeltaInTokens,
|
|
159
|
+
priceImpactDeltaUsd,
|
|
160
|
+
indexToken,
|
|
161
|
+
isLong,
|
|
162
|
+
});
|
|
163
|
+
return sizeDeltaInTokens === 0n
|
|
164
|
+
? undefined
|
|
165
|
+
: (0, gmsol_1.getGmsolUsdAmount)(bigmath_1.bigMath.mulDiv(sizeDeltaUsd, 1n, sizeDeltaInTokens)) * tokenUnit;
|
|
166
|
+
}
|
|
167
|
+
function getGmsolDecreaseAcceptablePrice(p) {
|
|
168
|
+
const { marketInfo, indexPrice, sizeDeltaUsd, sizeDeltaInTokens, positionSizeInUsd, positionSizeInTokens, isLong } = p;
|
|
169
|
+
const cappedImpactUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeDeltaUsd, p.priceImpactDeltaUsd);
|
|
170
|
+
const tokenUnit = (0, numbers_1.expandDecimals)(1, marketInfo.indexToken.decimals);
|
|
171
|
+
let priceAdjustment;
|
|
172
|
+
if (positionSizeInUsd && positionSizeInTokens) {
|
|
173
|
+
const impactAtEntrySizeUsd = (0, gmsol_1.getGmsolUsdAmount)(bigmath_1.bigMath.mulDiv(positionSizeInUsd, cappedImpactUsd, sizeDeltaUsd));
|
|
174
|
+
priceAdjustment = bigmath_1.bigMath.mulDiv(impactAtEntrySizeUsd, tokenUnit, positionSizeInTokens);
|
|
175
|
+
}
|
|
176
|
+
else if (sizeDeltaInTokens && sizeDeltaInTokens > 0n) {
|
|
177
|
+
priceAdjustment = bigmath_1.bigMath.mulDiv(cappedImpactUsd, tokenUnit, sizeDeltaInTokens);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
priceAdjustment = bigmath_1.bigMath.mulDiv(indexPrice, cappedImpactUsd, sizeDeltaUsd);
|
|
181
|
+
}
|
|
182
|
+
const priceUnit = tokenUnit * gmsol_1.GMSOL_PRECISION_SCALE;
|
|
183
|
+
priceAdjustment = bigmath_1.bigMath.mulDiv(priceAdjustment, 1n, priceUnit) * priceUnit;
|
|
184
|
+
return indexPrice + (isLong ? priceAdjustment : -priceAdjustment);
|
|
185
|
+
}
|
|
136
186
|
function getAcceptablePriceByPriceImpact(p) {
|
|
137
187
|
const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd } = p;
|
|
138
188
|
if (sizeDeltaUsd <= 0 || indexPrice == 0n) {
|
|
@@ -155,7 +205,12 @@ function getAcceptablePriceByPriceImpact(p) {
|
|
|
155
205
|
}
|
|
156
206
|
exports.getAcceptablePriceByPriceImpact = getAcceptablePriceByPriceImpact;
|
|
157
207
|
function getDefaultAcceptablePriceImpactBps(p) {
|
|
158
|
-
const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, acceptablePriceImpactBuffer = factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER, } = p;
|
|
208
|
+
const { indexPrice, sizeDeltaUsd, priceImpactDeltaUsd, acceptablePriceImpactBuffer = factors_1.DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER, executionPrice, } = p;
|
|
209
|
+
if (executionPrice !== undefined && executionPrice > 0n && indexPrice > 0n) {
|
|
210
|
+
const adversePriceDelta = (executionPrice - indexPrice) * (getShouldUseMaxPrice(p.isIncrease, p.isLong) ? 1n : -1n);
|
|
211
|
+
return ((adversePriceDelta > 0n ? (0, numbers_1.getBasisPoints)(adversePriceDelta, indexPrice, true) : 0n) +
|
|
212
|
+
BigInt(acceptablePriceImpactBuffer));
|
|
213
|
+
}
|
|
159
214
|
if (priceImpactDeltaUsd > 0) {
|
|
160
215
|
return BigInt(acceptablePriceImpactBuffer);
|
|
161
216
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fetchApiFeeDiscount = exports.parseFeeDiscount = void 0;
|
|
4
|
+
const validation_1 = require("../http/validation");
|
|
5
|
+
const objects_1 = require("../objects");
|
|
6
|
+
const FEE_DISCOUNT_ENDPOINT = "/v1/fee-discount";
|
|
7
|
+
function parseFeeDiscount(raw) {
|
|
8
|
+
if (!(0, objects_1.isRecord)(raw)) {
|
|
9
|
+
throw new Error(`${FEE_DISCOUNT_ENDPOINT}: expected an object`);
|
|
10
|
+
}
|
|
11
|
+
const record = raw;
|
|
12
|
+
const endpoint = FEE_DISCOUNT_ENDPOINT;
|
|
13
|
+
if (typeof record.attachedOnChain !== "boolean") {
|
|
14
|
+
throw new Error(`${FEE_DISCOUNT_ENDPOINT}: expected boolean field "attachedOnChain"`);
|
|
15
|
+
}
|
|
16
|
+
if (typeof record.tierId !== "number" || !Number.isSafeInteger(record.tierId) || record.tierId < 0) {
|
|
17
|
+
throw new Error(`${FEE_DISCOUNT_ENDPOINT}: expected number field "tierId"`);
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
userReferralInfo: {
|
|
21
|
+
userReferralCode: (0, validation_1.readApiString)({ record, name: "userReferralCode", endpoint }),
|
|
22
|
+
userReferralCodeString: (0, validation_1.readApiString)({ record, name: "userReferralCodeString", endpoint }),
|
|
23
|
+
referralCodeForTxn: (0, validation_1.readApiString)({ record, name: "referralCodeForTxn", endpoint }),
|
|
24
|
+
attachedOnChain: record.attachedOnChain,
|
|
25
|
+
affiliate: (0, validation_1.readApiString)({ record, name: "affiliate", endpoint }),
|
|
26
|
+
tierId: record.tierId,
|
|
27
|
+
totalRebate: (0, validation_1.readApiUnsignedBigInt)({ record, name: "totalRebate", endpoint }),
|
|
28
|
+
totalRebateFactor: (0, validation_1.readApiUnsignedBigInt)({ record, name: "totalRebateFactor", endpoint }),
|
|
29
|
+
discountShare: (0, validation_1.readApiUnsignedBigInt)({ record, name: "discountShare", endpoint }),
|
|
30
|
+
discountFactor: (0, validation_1.readApiUnsignedBigInt)({ record, name: "discountFactor", endpoint }),
|
|
31
|
+
},
|
|
32
|
+
orderFeeDiscountFactor: (0, validation_1.readApiUnsignedBigInt)({ record, name: "orderFeeDiscountFactor", endpoint }),
|
|
33
|
+
updatedAt: typeof record.updatedAt === "number" ? record.updatedAt : undefined,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
exports.parseFeeDiscount = parseFeeDiscount;
|
|
37
|
+
async function fetchApiFeeDiscount(ctx, params) {
|
|
38
|
+
const raw = await ctx.api.fetchJson(FEE_DISCOUNT_ENDPOINT, { query: { address: params.address } });
|
|
39
|
+
return parseFeeDiscount(raw);
|
|
40
|
+
}
|
|
41
|
+
exports.fetchApiFeeDiscount = fetchApiFeeDiscount;
|