@gmx-io/sdk 1.8.0 → 1.9.1
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/README.md +1 -1
- package/build/cjs/scripts/subsquid-codegen.js +1 -1
- package/build/cjs/src/abis/ArbSys.js +11 -0
- package/build/cjs/src/abis/index.js +2 -0
- package/build/cjs/src/clients/v1/testUtil.js +1 -1
- package/build/cjs/src/clients/v2/index.js +8 -1
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +16 -88
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +136 -748
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +30 -165
- package/build/cjs/src/configs/__tests__/markets.spec.js +1 -16
- package/build/cjs/src/configs/api.js +10 -1
- package/build/cjs/src/configs/contracts.js +2 -0
- package/build/cjs/src/configs/dataStore.js +13 -3
- package/build/cjs/src/configs/express.js +1 -0
- package/build/cjs/src/configs/markets.js +14 -77
- package/build/cjs/src/configs/tokens.js +22 -90
- package/build/cjs/src/test/fixtures/arbitrumMarkets.json +0 -72
- package/build/cjs/src/test/mock.js +12 -1
- package/build/cjs/src/utils/__tests__/fees.spec.js +36 -0
- package/build/cjs/src/utils/__tests__/markets.spec.js +233 -0
- package/build/cjs/src/utils/__tests__/positionPnlCap.spec.js +30 -3
- package/build/cjs/src/utils/__tests__/positions.spec.js +5 -5
- package/build/cjs/src/utils/__tests__/prices.spec.js +97 -0
- package/build/cjs/src/utils/express/utils/gelatoRelayUtils.js +2 -0
- package/build/cjs/src/utils/express/utils/gmxRelayUtils.js +145 -0
- package/build/cjs/src/utils/express/utils/gmxRelayUtils.spec.js +163 -0
- package/build/cjs/src/utils/express/utils/index.js +1 -0
- package/build/cjs/src/utils/fees/index.js +8 -5
- package/build/cjs/src/utils/fees/priceImpact.js +4 -9
- package/build/cjs/src/utils/markets/utils.js +57 -1
- package/build/cjs/src/utils/numbers/index.js +1 -0
- package/build/cjs/src/utils/numbers/prbMath.js +154 -0
- package/build/cjs/src/utils/positions/utils.js +66 -43
- package/build/cjs/src/utils/prices/utils.js +28 -1
- package/build/cjs/src/utils/stats/__tests__/api.spec.js +100 -0
- package/build/cjs/src/utils/stats/api.js +32 -0
- package/build/cjs/src/utils/stats/types.js +4 -0
- package/build/cjs/src/utils/trade/__tests__/increase.spec.js +580 -0
- package/build/cjs/src/utils/trade/__tests__/increaseInvariants.spec.js +258 -0
- package/build/cjs/src/utils/trade/__tests__/increaseMarginCheck.spec.js +837 -0
- package/build/cjs/src/utils/trade/increase.js +255 -32
- package/build/cjs/src/utils/trade/increaseMarginCheck.js +179 -0
- package/build/cjs/src/utils/trade/index.js +1 -0
- package/build/cjs/src/utils/trade/trade.js +11 -1
- package/build/cjs/src/utils/tradeHistory/utils.js +3 -0
- package/build/cjs/src/utils/yield/api.js +9 -1
- package/build/esm/scripts/subsquid-codegen.js +1 -1
- package/build/esm/src/abis/ArbSys.js +9 -0
- package/build/esm/src/abis/index.js +2 -0
- package/build/esm/src/clients/v1/testUtil.js +1 -1
- package/build/esm/src/clients/v2/index.js +5 -1
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +16 -88
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +136 -748
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +30 -165
- package/build/esm/src/configs/__tests__/markets.spec.js +2 -17
- package/build/esm/src/configs/api.js +8 -0
- package/build/esm/src/configs/contracts.js +2 -0
- package/build/esm/src/configs/dataStore.js +8 -0
- package/build/esm/src/configs/express.js +1 -0
- package/build/esm/src/configs/markets.js +14 -77
- package/build/esm/src/configs/tokens.js +22 -90
- package/build/esm/src/test/fixtures/arbitrumMarkets.json +0 -72
- package/build/esm/src/test/mock.js +10 -0
- package/build/esm/src/utils/__tests__/fees.spec.js +37 -1
- package/build/esm/src/utils/__tests__/markets.spec.js +234 -1
- package/build/esm/src/utils/__tests__/positionPnlCap.spec.js +30 -3
- package/build/esm/src/utils/__tests__/positions.spec.js +5 -5
- package/build/esm/src/utils/__tests__/prices.spec.js +98 -1
- package/build/esm/src/utils/express/utils/gelatoRelayUtils.js +2 -0
- package/build/esm/src/utils/express/utils/gmxRelayUtils.js +137 -0
- package/build/esm/src/utils/express/utils/gmxRelayUtils.spec.js +161 -0
- package/build/esm/src/utils/express/utils/index.js +1 -0
- package/build/esm/src/utils/fees/index.js +8 -5
- package/build/esm/src/utils/fees/priceImpact.js +5 -10
- package/build/esm/src/utils/markets/utils.js +57 -2
- package/build/esm/src/utils/numbers/index.js +1 -0
- package/build/esm/src/utils/numbers/prbMath.js +149 -0
- package/build/esm/src/utils/positions/utils.js +63 -42
- package/build/esm/src/utils/prices/utils.js +25 -0
- package/build/esm/src/utils/stats/__tests__/api.spec.js +98 -0
- package/build/esm/src/utils/stats/api.js +26 -0
- package/build/esm/src/utils/stats/types.js +1 -0
- package/build/esm/src/utils/trade/__tests__/increase.spec.js +578 -0
- package/build/esm/src/utils/trade/__tests__/increaseInvariants.spec.js +256 -0
- package/build/esm/src/utils/trade/__tests__/increaseMarginCheck.spec.js +835 -0
- package/build/esm/src/utils/trade/increase.js +243 -20
- package/build/esm/src/utils/trade/increaseMarginCheck.js +173 -0
- package/build/esm/src/utils/trade/index.js +1 -0
- package/build/esm/src/utils/trade/trade.js +10 -1
- package/build/esm/src/utils/tradeHistory/utils.js +3 -0
- package/build/esm/src/utils/yield/api.js +7 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/abis/ArbSys.d.ts +12 -0
- package/build/types/src/abis/index.d.ts +11 -0
- package/build/types/src/clients/v2/index.d.ts +6 -3
- package/build/types/src/codegen/prebuilt/index.d.ts +170 -989
- package/build/types/src/configs/api.d.ts +1 -0
- package/build/types/src/configs/contracts.d.ts +2 -0
- package/build/types/src/configs/dataStore.d.ts +4 -0
- package/build/types/src/configs/express.d.ts +1 -0
- package/build/types/src/test/mock.d.ts +2 -0
- package/build/types/src/utils/buildUrl/utils.d.ts +1 -1
- package/build/types/src/utils/buyback/types.d.ts +7 -0
- package/build/types/src/utils/express/utils/gelatoRelayUtils.d.ts +2 -0
- package/build/types/src/utils/express/utils/gmxRelayUtils.d.ts +61 -0
- package/build/types/src/utils/express/utils/gmxRelayUtils.spec.d.ts +1 -0
- package/build/types/src/utils/express/utils/index.d.ts +1 -0
- package/build/types/src/utils/fees/index.d.ts +1 -1
- package/build/types/src/utils/markets/utils.d.ts +7 -0
- package/build/types/src/utils/numbers/index.d.ts +1 -0
- package/build/types/src/utils/numbers/prbMath.d.ts +7 -0
- package/build/types/src/utils/positions/utils.d.ts +9 -0
- package/build/types/src/utils/prices/utils.d.ts +10 -0
- package/build/types/src/utils/stats/__tests__/api.spec.d.ts +1 -0
- package/build/types/src/utils/stats/api.d.ts +11 -0
- package/build/types/src/utils/stats/types.d.ts +106 -0
- package/build/types/src/utils/trade/__tests__/increase.spec.d.ts +1 -0
- package/build/types/src/utils/trade/__tests__/increaseInvariants.spec.d.ts +1 -0
- package/build/types/src/utils/trade/__tests__/increaseMarginCheck.spec.d.ts +1 -0
- package/build/types/src/utils/trade/increase.d.ts +3 -0
- package/build/types/src/utils/trade/increaseMarginCheck.d.ts +61 -0
- package/build/types/src/utils/trade/index.d.ts +1 -0
- package/build/types/src/utils/trade/trade.d.ts +2 -0
- package/build/types/src/utils/tradeHistory/types.d.ts +2 -1
- package/build/types/src/utils/yield/api.d.ts +4 -1
- package/build/types/src/utils/yield/types.d.ts +18 -0
- package/package.json +3 -3
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getNextPositionValuesForIncreaseTrade = exports.getIncreasePositionPrices = exports.leverageBySizeValues = exports.getTokensRatio = exports.getIncreasePositionAmounts = void 0;
|
|
4
4
|
const viem_1 = require("viem");
|
|
5
5
|
const factors_1 = require("../../configs/factors");
|
|
6
|
+
const tokens_1 = require("../../configs/tokens");
|
|
6
7
|
const bigmath_1 = require("../bigmath");
|
|
7
8
|
const fees_1 = require("../fees");
|
|
8
9
|
const numbers_1 = require("../numbers");
|
|
@@ -10,9 +11,136 @@ const types_1 = require("../orders/types");
|
|
|
10
11
|
const positions_1 = require("../positions");
|
|
11
12
|
const prices_1 = require("../prices");
|
|
12
13
|
const swap_1 = require("../swap");
|
|
13
|
-
const
|
|
14
|
+
const tokens_2 = require("../tokens");
|
|
15
|
+
function getBaseCollateralUsdFromSwapStrategy({ swapStrategy, initialCollateralToken, collateralToken, collateralPrice, swapPathStatsAtEvaluationPrice, }) {
|
|
16
|
+
if (swapStrategy.type === "noSwap" && (0, tokens_2.getIsEquivalentTokens)(initialCollateralToken, collateralToken)) {
|
|
17
|
+
return (0, tokens_2.convertToUsd)(swapStrategy.amountIn, initialCollateralToken.decimals, collateralPrice);
|
|
18
|
+
}
|
|
19
|
+
const grossAmountOut = swapPathStatsAtEvaluationPrice?.amountOut ?? swapStrategy.swapPathStats?.amountOut ?? swapStrategy.amountOut;
|
|
20
|
+
return (0, tokens_2.convertToUsd)(grossAmountOut, collateralToken.decimals, collateralPrice);
|
|
21
|
+
}
|
|
22
|
+
function getSwapRouteAtEvaluationPrice({ swapStrategy, marketsInfoData, indexToken, initialCollateralToken, evaluationPrice, chainId, }) {
|
|
23
|
+
if (evaluationPrice === undefined || swapStrategy.type !== "internalSwap" || !marketsInfoData) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const { swapPath, tokenInAddress } = swapStrategy.swapPathStats;
|
|
27
|
+
let hasRepricedToken = false;
|
|
28
|
+
const withEvaluationPrice = (token) => {
|
|
29
|
+
if (!(0, tokens_2.getIsEquivalentTokens)(token, indexToken)) {
|
|
30
|
+
return token;
|
|
31
|
+
}
|
|
32
|
+
hasRepricedToken = true;
|
|
33
|
+
return { ...token, prices: { minPrice: evaluationPrice, maxPrice: evaluationPrice } };
|
|
34
|
+
};
|
|
35
|
+
const repricedMarketsInfoData = { ...marketsInfoData };
|
|
36
|
+
for (const marketAddress of swapPath) {
|
|
37
|
+
const marketInfo = marketsInfoData[marketAddress];
|
|
38
|
+
if (!marketInfo) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
repricedMarketsInfoData[marketAddress] = {
|
|
42
|
+
...marketInfo,
|
|
43
|
+
indexToken: withEvaluationPrice(marketInfo.indexToken),
|
|
44
|
+
longToken: withEvaluationPrice(marketInfo.longToken),
|
|
45
|
+
shortToken: withEvaluationPrice(marketInfo.shortToken),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const priceIn = (0, tokens_2.getIsEquivalentTokens)(initialCollateralToken, indexToken)
|
|
49
|
+
? evaluationPrice
|
|
50
|
+
: initialCollateralToken.prices.minPrice;
|
|
51
|
+
if (!hasRepricedToken && priceIn === initialCollateralToken.prices.minPrice) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
marketsInfoData: repricedMarketsInfoData,
|
|
56
|
+
swapPath,
|
|
57
|
+
tokenInAddress,
|
|
58
|
+
priceIn,
|
|
59
|
+
wrappedNativeTokenAddress: (0, tokens_1.getWrappedToken)(chainId).address,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function getSwapPathStatsOnRoute(route, usdIn) {
|
|
63
|
+
return (0, swap_1.getSwapPathStats)({
|
|
64
|
+
marketsInfoData: route.marketsInfoData,
|
|
65
|
+
swapPath: route.swapPath,
|
|
66
|
+
initialCollateralAddress: route.tokenInAddress,
|
|
67
|
+
wrappedNativeTokenAddress: route.wrappedNativeTokenAddress,
|
|
68
|
+
usdIn,
|
|
69
|
+
shouldUnwrapNativeToken: false,
|
|
70
|
+
shouldApplyPriceImpact: true,
|
|
71
|
+
swapPricingType: types_1.SwapPricingType.Swap,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function getSwapPathStatsAtEvaluationPrice(p) {
|
|
75
|
+
const route = getSwapRouteAtEvaluationPrice(p);
|
|
76
|
+
if (!route) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
return getSwapPathStatsOnRoute(route, (0, tokens_2.convertToUsd)(p.swapStrategy.amountIn, p.initialCollateralToken.decimals, route.priceIn));
|
|
80
|
+
}
|
|
81
|
+
const SWAP_AMOUNT_IN_AT_EVALUATION_PRICE_ITERATIONS = 2;
|
|
82
|
+
function getSwapAmountInForCollateralAtEvaluationPrice({ swapStrategy, marketsInfoData, indexToken, initialCollateralToken, collateralToken, collateralPrice, evaluationPrice, chainId, targetCollateralUsd, uiFeeFactor, }) {
|
|
83
|
+
if (targetCollateralUsd <= 0n) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
const route = getSwapRouteAtEvaluationPrice({
|
|
87
|
+
swapStrategy,
|
|
88
|
+
marketsInfoData,
|
|
89
|
+
indexToken,
|
|
90
|
+
initialCollateralToken,
|
|
91
|
+
evaluationPrice,
|
|
92
|
+
chainId,
|
|
93
|
+
});
|
|
94
|
+
if (!route || route.priceIn <= 0n) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
let usdIn = targetCollateralUsd;
|
|
98
|
+
for (let i = 0; i < SWAP_AMOUNT_IN_AT_EVALUATION_PRICE_ITERATIONS; i++) {
|
|
99
|
+
const swapPathStats = getSwapPathStatsOnRoute(route, usdIn);
|
|
100
|
+
if (!swapPathStats) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
const collateralUsd = (0, tokens_2.convertToUsd)(swapPathStats.amountOut, collateralToken.decimals, collateralPrice) -
|
|
104
|
+
(0, numbers_1.applyFactor)((0, fees_1.getTotalSwapVolumeFromSwapStats)(swapPathStats.swapSteps), uiFeeFactor);
|
|
105
|
+
if (collateralUsd <= 0n) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
usdIn = bigmath_1.bigMath.mulDiv(usdIn, targetCollateralUsd, collateralUsd);
|
|
109
|
+
}
|
|
110
|
+
return (0, numbers_1.roundUpMagnitudeDivision)(usdIn * (0, numbers_1.expandDecimals)(1, initialCollateralToken.decimals), route.priceIn);
|
|
111
|
+
}
|
|
112
|
+
function getInternalSwapStrategyForAmountIn({ swapStrategy, amountIn, marketsInfoData, initialCollateralToken, collateralToken, chainId, }) {
|
|
113
|
+
const { swapPath, tokenInAddress } = swapStrategy.swapPathStats;
|
|
114
|
+
const priceIn = initialCollateralToken.prices.minPrice;
|
|
115
|
+
const usdIn = (0, tokens_2.convertToUsd)(amountIn, initialCollateralToken.decimals, priceIn);
|
|
116
|
+
const swapPathStats = (0, swap_1.getSwapPathStats)({
|
|
117
|
+
marketsInfoData,
|
|
118
|
+
swapPath,
|
|
119
|
+
initialCollateralAddress: tokenInAddress,
|
|
120
|
+
wrappedNativeTokenAddress: (0, tokens_1.getWrappedToken)(chainId).address,
|
|
121
|
+
usdIn,
|
|
122
|
+
shouldUnwrapNativeToken: false,
|
|
123
|
+
shouldApplyPriceImpact: true,
|
|
124
|
+
swapPricingType: types_1.SwapPricingType.Swap,
|
|
125
|
+
});
|
|
126
|
+
if (!swapPathStats) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
type: "internalSwap",
|
|
131
|
+
swapPathStats,
|
|
132
|
+
externalSwapQuote: undefined,
|
|
133
|
+
amountIn,
|
|
134
|
+
amountOut: swapPathStats.amountOut,
|
|
135
|
+
usdIn,
|
|
136
|
+
usdOut: swapPathStats.usdOut,
|
|
137
|
+
priceIn,
|
|
138
|
+
priceOut: collateralToken.prices.maxPrice,
|
|
139
|
+
feesUsd: usdIn - swapPathStats.usdOut,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
14
142
|
function getIncreasePositionAmounts(p) {
|
|
15
|
-
const { marketInfo, indexToken, initialCollateralToken, collateralToken, initialCollateralAmount, indexTokenAmount, isLong, leverage, triggerPrice, limitOrderType, position, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, externalSwapQuote, findSwapPath, userReferralInfo, uiFeeFactor, strategy, marketsInfoData, chainId, externalSwapQuoteParams, isSetAcceptablePriceImpactEnabled, disabledMarkets, manualPath, } = 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;
|
|
16
144
|
const swapStrategy = {
|
|
17
145
|
type: "noSwap",
|
|
18
146
|
externalSwapQuote: undefined,
|
|
@@ -68,8 +196,11 @@ function getIncreasePositionAmounts(p) {
|
|
|
68
196
|
values.collateralPrice = prices.collateralPrice;
|
|
69
197
|
values.triggerPrice = prices.triggerPrice;
|
|
70
198
|
values.triggerThresholdType = prices.triggerThresholdType;
|
|
199
|
+
const evaluationPrice = prices.evaluationPrice;
|
|
71
200
|
values.borrowingFeeUsd = position?.pendingBorrowingFeesUsd || 0n;
|
|
72
|
-
values.fundingFeeUsd = position
|
|
201
|
+
values.fundingFeeUsd = position
|
|
202
|
+
? (0, tokens_2.convertToUsd)(position.fundingFeeAmount, collateralToken.decimals, values.collateralPrice)
|
|
203
|
+
: 0n;
|
|
73
204
|
if (values.indexPrice <= 0 || values.initialCollateralPrice <= 0 || values.collateralPrice <= 0) {
|
|
74
205
|
return values;
|
|
75
206
|
}
|
|
@@ -80,7 +211,7 @@ function getIncreasePositionAmounts(p) {
|
|
|
80
211
|
initialCollateralAmount > 0) {
|
|
81
212
|
values.estimatedLeverage = leverage;
|
|
82
213
|
values.initialCollateralAmount = initialCollateralAmount;
|
|
83
|
-
values.initialCollateralUsd = (0,
|
|
214
|
+
values.initialCollateralUsd = (0, tokens_2.convertToUsd)(initialCollateralAmount, initialCollateralToken.decimals, values.initialCollateralPrice);
|
|
84
215
|
if (externalSwapQuote) {
|
|
85
216
|
const swapStrategy = {
|
|
86
217
|
type: "externalSwap",
|
|
@@ -108,24 +239,37 @@ function getIncreasePositionAmounts(p) {
|
|
|
108
239
|
});
|
|
109
240
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
110
241
|
}
|
|
111
|
-
const
|
|
112
|
-
|
|
242
|
+
const swapPathStatsAtEvaluationPrice = getSwapPathStatsAtEvaluationPrice({
|
|
243
|
+
swapStrategy: values.swapStrategy,
|
|
244
|
+
marketsInfoData,
|
|
245
|
+
indexToken,
|
|
246
|
+
initialCollateralToken,
|
|
247
|
+
evaluationPrice,
|
|
248
|
+
chainId,
|
|
249
|
+
});
|
|
250
|
+
const baseCollateralUsd = getBaseCollateralUsdFromSwapStrategy({
|
|
251
|
+
swapStrategy: values.swapStrategy,
|
|
252
|
+
initialCollateralToken,
|
|
253
|
+
collateralToken,
|
|
254
|
+
collateralPrice: values.collateralPrice,
|
|
255
|
+
swapPathStatsAtEvaluationPrice,
|
|
256
|
+
});
|
|
113
257
|
const baseSizeDeltaUsd = bigmath_1.bigMath.mulDiv(baseCollateralUsd, leverage, factors_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
114
|
-
const baseSizeDeltaInTokens = (0,
|
|
258
|
+
const baseSizeDeltaInTokens = (0, tokens_2.convertToTokenAmountForIncrease)(baseSizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
115
259
|
const { balanceWasImproved: baseBalanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, baseSizeDeltaUsd, isLong, { sizeDeltaInTokens: baseSizeDeltaInTokens });
|
|
116
|
-
const basePositionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, baseSizeDeltaUsd, baseBalanceWasImproved, userReferralInfo);
|
|
260
|
+
const basePositionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, baseSizeDeltaUsd, baseBalanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
117
261
|
const baseUiFeeUsd = (0, numbers_1.applyFactor)(baseSizeDeltaUsd, uiFeeFactor);
|
|
118
|
-
const totalSwapVolumeUsd = (0, fees_1.getTotalSwapVolumeFromSwapStats)(values.swapStrategy.swapPathStats?.swapSteps);
|
|
262
|
+
const totalSwapVolumeUsd = (0, fees_1.getTotalSwapVolumeFromSwapStats)((swapPathStatsAtEvaluationPrice ?? values.swapStrategy.swapPathStats)?.swapSteps);
|
|
119
263
|
values.swapUiFeeUsd = (0, numbers_1.applyFactor)(totalSwapVolumeUsd, uiFeeFactor);
|
|
120
264
|
values.sizeDeltaUsd = bigmath_1.bigMath.mulDiv(baseCollateralUsd - basePositionFeeInfo.positionFeeUsd - baseUiFeeUsd - values.swapUiFeeUsd, leverage, factors_1.BASIS_POINTS_DIVISOR_BIGINT);
|
|
121
265
|
if (values.sizeDeltaUsd <= 0n) {
|
|
122
266
|
return values;
|
|
123
267
|
}
|
|
124
|
-
values.indexTokenAmount = (0,
|
|
268
|
+
values.indexTokenAmount = (0, tokens_2.convertToTokenAmountForIncrease)(values.sizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
125
269
|
const { balanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, values.sizeDeltaUsd, isLong, {
|
|
126
270
|
sizeDeltaInTokens: values.indexTokenAmount,
|
|
127
271
|
});
|
|
128
|
-
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo);
|
|
272
|
+
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
129
273
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
130
274
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
131
275
|
values.uiFeeUsd = (0, numbers_1.applyFactor)(values.sizeDeltaUsd, uiFeeFactor);
|
|
@@ -136,7 +280,7 @@ function getIncreasePositionAmounts(p) {
|
|
|
136
280
|
values.fundingFeeUsd -
|
|
137
281
|
values.uiFeeUsd -
|
|
138
282
|
values.swapUiFeeUsd;
|
|
139
|
-
values.collateralDeltaAmount = (0,
|
|
283
|
+
values.collateralDeltaAmount = (0, tokens_2.convertToTokenAmount)(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
140
284
|
}
|
|
141
285
|
else if (strategy === "leverageBySize" &&
|
|
142
286
|
leverage !== undefined &&
|
|
@@ -144,16 +288,16 @@ function getIncreasePositionAmounts(p) {
|
|
|
144
288
|
indexTokenAmount > 0) {
|
|
145
289
|
values.estimatedLeverage = leverage;
|
|
146
290
|
values.indexTokenAmount = indexTokenAmount;
|
|
147
|
-
values.sizeDeltaUsd = (0,
|
|
148
|
-
const sizeDeltaInTokensForPriceImpact = (0,
|
|
291
|
+
values.sizeDeltaUsd = (0, tokens_2.convertToUsd)(indexTokenAmount, indexToken.decimals, values.indexPrice);
|
|
292
|
+
const sizeDeltaInTokensForPriceImpact = (0, tokens_2.convertToTokenAmountForIncrease)(values.sizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
149
293
|
const { balanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, values.sizeDeltaUsd, isLong, {
|
|
150
294
|
sizeDeltaInTokens: sizeDeltaInTokensForPriceImpact,
|
|
151
295
|
});
|
|
152
|
-
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo);
|
|
296
|
+
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
153
297
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
154
298
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
155
299
|
values.uiFeeUsd = (0, numbers_1.applyFactor)(values.sizeDeltaUsd, uiFeeFactor);
|
|
156
|
-
const { collateralDeltaUsd, collateralDeltaAmount, baseCollateralAmount } = leverageBySizeValues({
|
|
300
|
+
const { collateralDeltaUsd, collateralDeltaAmount, baseCollateralUsd, baseCollateralAmount } = leverageBySizeValues({
|
|
157
301
|
collateralToken,
|
|
158
302
|
leverage,
|
|
159
303
|
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
@@ -192,27 +336,74 @@ function getIncreasePositionAmounts(p) {
|
|
|
192
336
|
manualPath,
|
|
193
337
|
});
|
|
194
338
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
339
|
+
if (values.swapStrategy.type === "internalSwap" && marketsInfoData) {
|
|
340
|
+
const amountInAtEvaluationPrice = getSwapAmountInForCollateralAtEvaluationPrice({
|
|
341
|
+
swapStrategy: values.swapStrategy,
|
|
342
|
+
marketsInfoData,
|
|
343
|
+
indexToken,
|
|
344
|
+
initialCollateralToken,
|
|
345
|
+
collateralToken,
|
|
346
|
+
collateralPrice: values.collateralPrice,
|
|
347
|
+
evaluationPrice,
|
|
348
|
+
chainId,
|
|
349
|
+
targetCollateralUsd: baseCollateralUsd - values.swapUiFeeUsd,
|
|
350
|
+
uiFeeFactor,
|
|
351
|
+
});
|
|
352
|
+
const swapStrategyAtEvaluationPrice = amountInAtEvaluationPrice === undefined
|
|
353
|
+
? undefined
|
|
354
|
+
: getInternalSwapStrategyForAmountIn({
|
|
355
|
+
swapStrategy: values.swapStrategy,
|
|
356
|
+
amountIn: amountInAtEvaluationPrice,
|
|
357
|
+
marketsInfoData,
|
|
358
|
+
initialCollateralToken,
|
|
359
|
+
collateralToken,
|
|
360
|
+
chainId,
|
|
361
|
+
});
|
|
362
|
+
if (swapStrategyAtEvaluationPrice) {
|
|
363
|
+
values.swapStrategy = swapStrategyAtEvaluationPrice;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
195
366
|
}
|
|
196
367
|
const swapAmountIn = values.swapStrategy.amountIn;
|
|
197
368
|
values.initialCollateralAmount = swapAmountIn;
|
|
198
|
-
values.initialCollateralUsd = (0,
|
|
369
|
+
values.initialCollateralUsd = (0, tokens_2.convertToUsd)(values.initialCollateralAmount, initialCollateralToken.decimals, values.initialCollateralPrice);
|
|
370
|
+
const swapPathStatsAtEvaluationPrice = getSwapPathStatsAtEvaluationPrice({
|
|
371
|
+
swapStrategy: values.swapStrategy,
|
|
372
|
+
marketsInfoData,
|
|
373
|
+
indexToken,
|
|
374
|
+
initialCollateralToken,
|
|
375
|
+
evaluationPrice,
|
|
376
|
+
chainId,
|
|
377
|
+
});
|
|
378
|
+
if (swapPathStatsAtEvaluationPrice) {
|
|
379
|
+
const baseCollateralUsdAtEvaluationPrice = (0, tokens_2.convertToUsd)(swapPathStatsAtEvaluationPrice.amountOut, collateralToken.decimals, values.collateralPrice);
|
|
380
|
+
const swapUiFeeUsdAtEvaluationPrice = (0, numbers_1.applyFactor)((0, fees_1.getTotalSwapVolumeFromSwapStats)(swapPathStatsAtEvaluationPrice.swapSteps), uiFeeFactor);
|
|
381
|
+
values.collateralDeltaUsd =
|
|
382
|
+
baseCollateralUsdAtEvaluationPrice -
|
|
383
|
+
values.positionFeeUsd -
|
|
384
|
+
values.borrowingFeeUsd -
|
|
385
|
+
values.fundingFeeUsd -
|
|
386
|
+
values.uiFeeUsd -
|
|
387
|
+
swapUiFeeUsdAtEvaluationPrice;
|
|
388
|
+
values.collateralDeltaAmount = (0, tokens_2.convertToTokenAmount)(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
389
|
+
}
|
|
199
390
|
}
|
|
200
391
|
else if (strategy === "independent") {
|
|
201
392
|
if (indexTokenAmount !== undefined && indexTokenAmount > 0) {
|
|
202
393
|
values.indexTokenAmount = indexTokenAmount;
|
|
203
|
-
values.sizeDeltaUsd = (0,
|
|
204
|
-
const sizeDeltaInTokensForPriceImpact = (0,
|
|
394
|
+
values.sizeDeltaUsd = (0, tokens_2.convertToUsd)(indexTokenAmount, indexToken.decimals, values.indexPrice);
|
|
395
|
+
const sizeDeltaInTokensForPriceImpact = (0, tokens_2.convertToTokenAmountForIncrease)(values.sizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
205
396
|
const { balanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, values.sizeDeltaUsd, isLong, {
|
|
206
397
|
sizeDeltaInTokens: sizeDeltaInTokensForPriceImpact,
|
|
207
398
|
});
|
|
208
|
-
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo);
|
|
399
|
+
const positionFeeInfo = (0, fees_1.getPositionFee)(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
209
400
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
210
401
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
211
402
|
values.uiFeeUsd = (0, numbers_1.applyFactor)(values.sizeDeltaUsd, uiFeeFactor);
|
|
212
403
|
}
|
|
213
404
|
if (initialCollateralAmount !== undefined && initialCollateralAmount > 0) {
|
|
214
405
|
values.initialCollateralAmount = initialCollateralAmount;
|
|
215
|
-
values.initialCollateralUsd = (0,
|
|
406
|
+
values.initialCollateralUsd = (0, tokens_2.convertToUsd)(initialCollateralAmount, initialCollateralToken.decimals, values.initialCollateralPrice);
|
|
216
407
|
if (externalSwapQuote) {
|
|
217
408
|
const swapStrategy = {
|
|
218
409
|
type: "externalSwap",
|
|
@@ -240,8 +431,23 @@ function getIncreasePositionAmounts(p) {
|
|
|
240
431
|
});
|
|
241
432
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
242
433
|
}
|
|
243
|
-
const
|
|
244
|
-
|
|
434
|
+
const swapPathStatsAtEvaluationPrice = getSwapPathStatsAtEvaluationPrice({
|
|
435
|
+
swapStrategy: values.swapStrategy,
|
|
436
|
+
marketsInfoData,
|
|
437
|
+
indexToken,
|
|
438
|
+
initialCollateralToken,
|
|
439
|
+
evaluationPrice,
|
|
440
|
+
chainId,
|
|
441
|
+
});
|
|
442
|
+
const baseCollateralUsd = getBaseCollateralUsdFromSwapStrategy({
|
|
443
|
+
swapStrategy: values.swapStrategy,
|
|
444
|
+
initialCollateralToken,
|
|
445
|
+
collateralToken,
|
|
446
|
+
collateralPrice: values.collateralPrice,
|
|
447
|
+
swapPathStatsAtEvaluationPrice,
|
|
448
|
+
});
|
|
449
|
+
const totalSwapVolumeUsd = (0, fees_1.getTotalSwapVolumeFromSwapStats)((swapPathStatsAtEvaluationPrice ?? values.swapStrategy.swapPathStats)?.swapSteps);
|
|
450
|
+
values.swapUiFeeUsd = (0, numbers_1.applyFactor)(totalSwapVolumeUsd, uiFeeFactor);
|
|
245
451
|
values.collateralDeltaUsd =
|
|
246
452
|
baseCollateralUsd -
|
|
247
453
|
values.positionFeeUsd -
|
|
@@ -249,7 +455,14 @@ function getIncreasePositionAmounts(p) {
|
|
|
249
455
|
values.fundingFeeUsd -
|
|
250
456
|
values.uiFeeUsd -
|
|
251
457
|
values.swapUiFeeUsd;
|
|
252
|
-
values.collateralDeltaAmount = (0,
|
|
458
|
+
values.collateralDeltaAmount = (0, tokens_2.convertToTokenAmount)(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
459
|
+
}
|
|
460
|
+
else if (values.sizeDeltaUsd > 0n) {
|
|
461
|
+
values.collateralDeltaUsd = -(values.positionFeeUsd +
|
|
462
|
+
values.borrowingFeeUsd +
|
|
463
|
+
values.fundingFeeUsd +
|
|
464
|
+
values.uiFeeUsd);
|
|
465
|
+
values.collateralDeltaAmount = (0, tokens_2.convertToTokenAmount)(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
253
466
|
}
|
|
254
467
|
values.estimatedLeverage = (0, positions_1.getLeverage)({
|
|
255
468
|
sizeInUsd: values.sizeDeltaUsd,
|
|
@@ -259,7 +472,7 @@ function getIncreasePositionAmounts(p) {
|
|
|
259
472
|
pendingFundingFeesUsd: 0n,
|
|
260
473
|
});
|
|
261
474
|
}
|
|
262
|
-
values.sizeDeltaInTokens = (0,
|
|
475
|
+
values.sizeDeltaInTokens = (0, tokens_2.convertToTokenAmountForIncrease)(values.sizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
263
476
|
const acceptablePriceInfo = (0, prices_1.getAcceptablePriceInfo)({
|
|
264
477
|
marketInfo,
|
|
265
478
|
isIncrease: true,
|
|
@@ -313,7 +526,7 @@ function getIncreasePositionAmounts(p) {
|
|
|
313
526
|
exports.getIncreasePositionAmounts = getIncreasePositionAmounts;
|
|
314
527
|
function getTokensRatio({ fromToken, toToken, triggerRatioValue, markPrice, }) {
|
|
315
528
|
const fromTokenPrice = fromToken?.prices.minPrice;
|
|
316
|
-
const markRatio = (0,
|
|
529
|
+
const markRatio = (0, tokens_2.getTokensRatioByPrice)({
|
|
317
530
|
fromToken,
|
|
318
531
|
toToken,
|
|
319
532
|
fromPrice: fromTokenPrice,
|
|
@@ -334,12 +547,12 @@ function getTokensRatio({ fromToken, toToken, triggerRatioValue, markPrice, }) {
|
|
|
334
547
|
}
|
|
335
548
|
exports.getTokensRatio = getTokensRatio;
|
|
336
549
|
function leverageBySizeValues({ collateralToken, leverage, sizeDeltaUsd, collateralPrice, positionFeeUsd, borrowingFeeUsd, uiFeeUsd, swapUiFeeUsd, fundingFeeUsd, }) {
|
|
337
|
-
const collateralDeltaUsd = bigmath_1.bigMath.mulDiv(sizeDeltaUsd, factors_1.BASIS_POINTS_DIVISOR_BIGINT, leverage);
|
|
338
|
-
const collateralDeltaAmount = (0,
|
|
550
|
+
const collateralDeltaUsd = leverage > 0n ? bigmath_1.bigMath.mulDiv(sizeDeltaUsd, factors_1.BASIS_POINTS_DIVISOR_BIGINT, leverage) : 0n;
|
|
551
|
+
const collateralDeltaAmount = (0, tokens_2.convertToTokenAmount)(collateralDeltaUsd, collateralToken.decimals, collateralPrice);
|
|
339
552
|
const baseCollateralUsd = collateralDeltaUsd !== 0n
|
|
340
553
|
? collateralDeltaUsd + positionFeeUsd + borrowingFeeUsd + fundingFeeUsd + uiFeeUsd + swapUiFeeUsd
|
|
341
554
|
: 0n;
|
|
342
|
-
const baseCollateralAmount = (0,
|
|
555
|
+
const baseCollateralAmount = (0, tokens_2.convertToTokenAmount)(baseCollateralUsd, collateralToken.decimals, collateralPrice);
|
|
343
556
|
return {
|
|
344
557
|
collateralDeltaUsd,
|
|
345
558
|
collateralDeltaAmount,
|
|
@@ -353,10 +566,14 @@ function getIncreasePositionPrices({ triggerPrice, indexToken, initialCollateral
|
|
|
353
566
|
let initialCollateralPrice;
|
|
354
567
|
let triggerThresholdType;
|
|
355
568
|
let collateralPrice;
|
|
569
|
+
let evaluationPrice;
|
|
356
570
|
if (triggerPrice !== undefined && triggerPrice > 0 && limitOrderType !== undefined) {
|
|
357
571
|
indexPrice = triggerPrice;
|
|
572
|
+
evaluationPrice = triggerPrice;
|
|
358
573
|
initialCollateralPrice = initialCollateralToken.prices.minPrice;
|
|
359
|
-
collateralPrice = collateralToken
|
|
574
|
+
collateralPrice = (0, tokens_2.getIsEquivalentTokens)(collateralToken, indexToken)
|
|
575
|
+
? triggerPrice
|
|
576
|
+
: collateralToken.prices.minPrice;
|
|
360
577
|
triggerThresholdType = (0, prices_1.getOrderThresholdType)(limitOrderType, isLong);
|
|
361
578
|
}
|
|
362
579
|
else {
|
|
@@ -368,14 +585,20 @@ function getIncreasePositionPrices({ triggerPrice, indexToken, initialCollateral
|
|
|
368
585
|
indexPrice,
|
|
369
586
|
initialCollateralPrice,
|
|
370
587
|
collateralPrice,
|
|
588
|
+
evaluationPrice,
|
|
371
589
|
triggerThresholdType,
|
|
372
590
|
triggerPrice,
|
|
373
591
|
};
|
|
374
592
|
}
|
|
375
593
|
exports.getIncreasePositionPrices = getIncreasePositionPrices;
|
|
376
594
|
function getNextPositionValuesForIncreaseTrade(p) {
|
|
377
|
-
const { existingPosition, marketInfo, collateralToken, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaUsd, collateralDeltaAmount, indexPrice, isLong, showPnlInLeverage, minCollateralUsd, userReferralInfo, positionPriceImpactDeltaUsd, } = p;
|
|
378
|
-
const
|
|
595
|
+
const { existingPosition, marketInfo, collateralToken, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaUsd, collateralDeltaAmount, indexPrice, isLong, showPnlInLeverage, minCollateralUsd, userReferralInfo, positionPriceImpactDeltaUsd, collateralPrice, } = p;
|
|
596
|
+
const existingCollateralUsd = !existingPosition
|
|
597
|
+
? 0n
|
|
598
|
+
: collateralPrice === undefined || collateralPrice <= 0n
|
|
599
|
+
? existingPosition.collateralUsd
|
|
600
|
+
: (0, tokens_2.convertToUsd)(existingPosition.collateralAmount, collateralToken.decimals, collateralPrice);
|
|
601
|
+
const nextCollateralUsd = existingCollateralUsd + collateralDeltaUsd;
|
|
379
602
|
const nextCollateralAmount = existingPosition
|
|
380
603
|
? existingPosition.collateralAmount + collateralDeltaAmount
|
|
381
604
|
: collateralDeltaAmount;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getIncreaseResultingPositionMarginState = exports.getResultingPositionMarginState = exports.getIsMaxLeverageMarginReason = exports.PositionMarginFailureReason = void 0;
|
|
4
|
+
const fees_1 = require("../fees");
|
|
5
|
+
const markets_1 = require("../markets");
|
|
6
|
+
const numbers_1 = require("../numbers");
|
|
7
|
+
const positions_1 = require("../positions");
|
|
8
|
+
const tokens_1 = require("../tokens");
|
|
9
|
+
var PositionMarginFailureReason;
|
|
10
|
+
(function (PositionMarginFailureReason) {
|
|
11
|
+
PositionMarginFailureReason["MinCollateral"] = "min collateral";
|
|
12
|
+
PositionMarginFailureReason["NonPositiveRemainingMargin"] = "< 0";
|
|
13
|
+
PositionMarginFailureReason["MinCollateralForLeverage"] = "min collateral for leverage";
|
|
14
|
+
PositionMarginFailureReason["InsufficientCollateralUsd"] = "insufficient collateral usd";
|
|
15
|
+
})(PositionMarginFailureReason || (exports.PositionMarginFailureReason = PositionMarginFailureReason = {}));
|
|
16
|
+
function getIsMaxLeverageMarginReason(reason) {
|
|
17
|
+
return (reason === PositionMarginFailureReason.MinCollateralForLeverage ||
|
|
18
|
+
reason === PositionMarginFailureReason.InsufficientCollateralUsd);
|
|
19
|
+
}
|
|
20
|
+
exports.getIsMaxLeverageMarginReason = getIsMaxLeverageMarginReason;
|
|
21
|
+
/**
|
|
22
|
+
* Port of `PositionUtils.isPositionLiquidatable` from the gmx-synthetics build deployed on Arbitrum
|
|
23
|
+
* (release_2.2.1 @ 23c9d160: release-v2.2c plus MarketUtils.getPositivePnl in the per-position pnl cap)
|
|
24
|
+
* for a position whose pending fees have just been settled — the state the contract validates right
|
|
25
|
+
* after an increase.
|
|
26
|
+
*/
|
|
27
|
+
function getResultingPositionMarginState(p) {
|
|
28
|
+
const { marketInfo, collateralToken, sizeInUsd, sizeInTokens, collateralAmount, pendingImpactAmount, minCollateralUsd, isLong, userReferralInfo, proDiscountFactor, } = p;
|
|
29
|
+
const { indexToken } = marketInfo;
|
|
30
|
+
const positionPnlUsd = (0, positions_1.getPositionPnlUsd)({
|
|
31
|
+
marketInfo,
|
|
32
|
+
sizeInUsd,
|
|
33
|
+
sizeInTokens,
|
|
34
|
+
markPrice: (0, markets_1.getPriceForPnl)(indexToken.prices, isLong, false),
|
|
35
|
+
isLong,
|
|
36
|
+
});
|
|
37
|
+
const collateralUsd = (0, tokens_1.convertToUsd)(collateralAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
38
|
+
const { priceImpactDeltaUsd, balanceWasImproved } = (0, fees_1.getPriceImpactForPosition)(marketInfo, -sizeInUsd, isLong, {
|
|
39
|
+
fallbackToZero: true,
|
|
40
|
+
sizeDeltaInTokens: sizeInTokens,
|
|
41
|
+
});
|
|
42
|
+
let totalImpactUsd = priceImpactDeltaUsd;
|
|
43
|
+
if (totalImpactUsd > 0) {
|
|
44
|
+
totalImpactUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeInUsd, totalImpactUsd);
|
|
45
|
+
}
|
|
46
|
+
const pendingImpactUsd = (0, tokens_1.convertToUsd)(pendingImpactAmount, indexToken.decimals, pendingImpactAmount > 0 ? indexToken.prices.minPrice : indexToken.prices.maxPrice);
|
|
47
|
+
totalImpactUsd = totalImpactUsd + pendingImpactUsd;
|
|
48
|
+
if (totalImpactUsd >= 0) {
|
|
49
|
+
totalImpactUsd = 0n;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
const maxNegativeImpactUsd = -(0, numbers_1.applyFactor)(sizeInUsd, marketInfo.maxPositionImpactFactorForLiquidations);
|
|
53
|
+
if (totalImpactUsd < maxNegativeImpactUsd) {
|
|
54
|
+
totalImpactUsd = maxNegativeImpactUsd;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const closingFeeFactor = balanceWasImproved
|
|
58
|
+
? marketInfo.positionFeeFactorForBalanceWasImproved
|
|
59
|
+
: marketInfo.positionFeeFactorForBalanceWasNotImproved;
|
|
60
|
+
let closingFeeAmount = (0, tokens_1.convertToTokenAmount)((0, numbers_1.applyFactor)(sizeInUsd, closingFeeFactor), collateralToken.decimals, collateralToken.prices.minPrice) ?? 0n;
|
|
61
|
+
let discountAmount = 0n;
|
|
62
|
+
if (userReferralInfo) {
|
|
63
|
+
const totalRebateAmount = (0, numbers_1.applyFactor)(closingFeeAmount, userReferralInfo.totalRebateFactor);
|
|
64
|
+
discountAmount = (0, numbers_1.applyFactor)(totalRebateAmount, userReferralInfo.discountFactor);
|
|
65
|
+
}
|
|
66
|
+
if (proDiscountFactor !== undefined && proDiscountFactor > 0n) {
|
|
67
|
+
const proDiscountAmount = (0, numbers_1.applyFactor)(closingFeeAmount, proDiscountFactor);
|
|
68
|
+
if (proDiscountAmount > discountAmount) {
|
|
69
|
+
discountAmount = proDiscountAmount;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
closingFeeAmount -= discountAmount;
|
|
73
|
+
const closingCostUsd = (0, tokens_1.convertToUsd)(closingFeeAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
74
|
+
const remainingCollateralUsd = collateralUsd + positionPnlUsd + totalImpactUsd - closingCostUsd;
|
|
75
|
+
const minCollateralUsdForLeverage = (0, numbers_1.applyFactor)(sizeInUsd, marketInfo.minCollateralFactor);
|
|
76
|
+
const state = {
|
|
77
|
+
remainingCollateralUsd,
|
|
78
|
+
minCollateralUsd,
|
|
79
|
+
minCollateralUsdForLeverage,
|
|
80
|
+
};
|
|
81
|
+
if (remainingCollateralUsd < minCollateralUsd) {
|
|
82
|
+
return { ...state, isLiquidatable: true, reason: PositionMarginFailureReason.MinCollateral };
|
|
83
|
+
}
|
|
84
|
+
if (remainingCollateralUsd <= 0) {
|
|
85
|
+
return { ...state, isLiquidatable: true, reason: PositionMarginFailureReason.NonPositiveRemainingMargin };
|
|
86
|
+
}
|
|
87
|
+
if (remainingCollateralUsd < minCollateralUsdForLeverage) {
|
|
88
|
+
return { ...state, isLiquidatable: true, reason: PositionMarginFailureReason.MinCollateralForLeverage };
|
|
89
|
+
}
|
|
90
|
+
return { ...state, isLiquidatable: false, reason: undefined };
|
|
91
|
+
}
|
|
92
|
+
exports.getResultingPositionMarginState = getResultingPositionMarginState;
|
|
93
|
+
function withPriceOverride(token, indexToken, price) {
|
|
94
|
+
if (!(0, tokens_1.getIsEquivalentTokens)(token, indexToken)) {
|
|
95
|
+
return token;
|
|
96
|
+
}
|
|
97
|
+
return { ...token, prices: { minPrice: price, maxPrice: price } };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Mirrors the post-increase gates of `IncreasePositionUtils.increasePosition` from the gmx-synthetics
|
|
101
|
+
* build deployed on Arbitrum (release_2.2.1 @ 23c9d160: release-v2.2c plus MarketUtils.getPositivePnl
|
|
102
|
+
* in the per-position pnl cap): `willPositionCollateralBeSufficient` on the updated open interest,
|
|
103
|
+
* then `validatePosition` on the resulting position.
|
|
104
|
+
*/
|
|
105
|
+
function getIncreaseResultingPositionMarginState(p) {
|
|
106
|
+
const { isLong, existingPosition, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaAmount, minCollateralUsd, userReferralInfo, proDiscountFactor, indexPriceForEvaluation, } = p;
|
|
107
|
+
let { marketInfo, collateralToken } = p;
|
|
108
|
+
if (sizeDeltaUsd <= 0n || sizeDeltaInTokens <= 0n) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
if (indexPriceForEvaluation !== undefined && indexPriceForEvaluation > 0n) {
|
|
112
|
+
const originalIndexToken = marketInfo.indexToken;
|
|
113
|
+
marketInfo = {
|
|
114
|
+
...marketInfo,
|
|
115
|
+
indexToken: withPriceOverride(marketInfo.indexToken, originalIndexToken, indexPriceForEvaluation),
|
|
116
|
+
longToken: withPriceOverride(marketInfo.longToken, originalIndexToken, indexPriceForEvaluation),
|
|
117
|
+
shortToken: withPriceOverride(marketInfo.shortToken, originalIndexToken, indexPriceForEvaluation),
|
|
118
|
+
};
|
|
119
|
+
collateralToken = withPriceOverride(collateralToken, originalIndexToken, indexPriceForEvaluation);
|
|
120
|
+
}
|
|
121
|
+
const { indexToken } = marketInfo;
|
|
122
|
+
if (indexToken.prices.minPrice <= 0n || indexToken.prices.maxPrice <= 0n || collateralToken.prices.minPrice <= 0n) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
const increaseImpact = (0, fees_1.getPriceImpactForPosition)(marketInfo, sizeDeltaUsd, isLong, {
|
|
126
|
+
fallbackToZero: true,
|
|
127
|
+
sizeDeltaInTokens,
|
|
128
|
+
});
|
|
129
|
+
let increaseImpactUsd = increaseImpact.priceImpactDeltaUsd;
|
|
130
|
+
if (increaseImpactUsd > 0n) {
|
|
131
|
+
increaseImpactUsd = (0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeDeltaUsd, increaseImpactUsd);
|
|
132
|
+
}
|
|
133
|
+
const increasePendingImpactAmount = increaseImpactUsd > 0n
|
|
134
|
+
? (0, tokens_1.convertToTokenAmount)(increaseImpactUsd, indexToken.decimals, indexToken.prices.maxPrice)
|
|
135
|
+
: (0, numbers_1.roundUpMagnitudeDivision)(increaseImpactUsd * (0, numbers_1.expandDecimals)(1, indexToken.decimals), indexToken.prices.minPrice);
|
|
136
|
+
const nextSizeInUsd = (existingPosition?.sizeInUsd ?? 0n) + sizeDeltaUsd;
|
|
137
|
+
const nextSizeInTokens = (existingPosition?.sizeInTokens ?? 0n) + sizeDeltaInTokens;
|
|
138
|
+
const nextCollateralAmount = (existingPosition?.collateralAmount ?? 0n) + collateralDeltaAmount;
|
|
139
|
+
const nextPendingImpactAmount = (existingPosition?.pendingImpactAmount ?? 0n) + increasePendingImpactAmount;
|
|
140
|
+
const nextMarketInfo = (0, markets_1.getMarketInfoWithOpenInterestDelta)({
|
|
141
|
+
marketInfo,
|
|
142
|
+
collateralToken,
|
|
143
|
+
isLong,
|
|
144
|
+
sizeDeltaUsd,
|
|
145
|
+
sizeDeltaInTokens,
|
|
146
|
+
});
|
|
147
|
+
const nextCollateralAmountClamped = nextCollateralAmount < 0n ? 0n : nextCollateralAmount;
|
|
148
|
+
const remainingCollateralUsdForOpenInterest = (0, tokens_1.convertToUsd)(nextCollateralAmountClamped, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
149
|
+
const minCollateralFactorMultiplier = isLong
|
|
150
|
+
? nextMarketInfo.minCollateralFactorForOpenInterestLong
|
|
151
|
+
: nextMarketInfo.minCollateralFactorForOpenInterestShort;
|
|
152
|
+
let minCollateralFactorForOpenInterest = (0, numbers_1.applyFactor)((0, markets_1.getOpenInterestUsd)(nextMarketInfo, isLong), minCollateralFactorMultiplier);
|
|
153
|
+
if (nextMarketInfo.minCollateralFactor > minCollateralFactorForOpenInterest) {
|
|
154
|
+
minCollateralFactorForOpenInterest = nextMarketInfo.minCollateralFactor;
|
|
155
|
+
}
|
|
156
|
+
const minCollateralUsdForOpenInterest = (0, numbers_1.applyFactor)(nextSizeInUsd, minCollateralFactorForOpenInterest);
|
|
157
|
+
if (remainingCollateralUsdForOpenInterest < minCollateralUsdForOpenInterest) {
|
|
158
|
+
return {
|
|
159
|
+
isLiquidatable: true,
|
|
160
|
+
reason: PositionMarginFailureReason.InsufficientCollateralUsd,
|
|
161
|
+
remainingCollateralUsd: remainingCollateralUsdForOpenInterest,
|
|
162
|
+
minCollateralUsd,
|
|
163
|
+
minCollateralUsdForLeverage: minCollateralUsdForOpenInterest,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return getResultingPositionMarginState({
|
|
167
|
+
marketInfo: nextMarketInfo,
|
|
168
|
+
collateralToken,
|
|
169
|
+
sizeInUsd: nextSizeInUsd,
|
|
170
|
+
sizeInTokens: nextSizeInTokens,
|
|
171
|
+
collateralAmount: nextCollateralAmountClamped,
|
|
172
|
+
pendingImpactAmount: nextPendingImpactAmount,
|
|
173
|
+
minCollateralUsd,
|
|
174
|
+
isLong,
|
|
175
|
+
userReferralInfo,
|
|
176
|
+
proDiscountFactor,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
exports.getIncreaseResultingPositionMarginState = getIncreaseResultingPositionMarginState;
|
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./types"), exports);
|
|
18
18
|
__exportStar(require("./increase"), exports);
|
|
19
|
+
__exportStar(require("./increaseMarginCheck"), exports);
|
|
19
20
|
__exportStar(require("./decrease"), exports);
|
|
20
21
|
__exportStar(require("./decreaseOutputs"), exports);
|
|
21
22
|
__exportStar(require("./trade"), exports);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createTradeFlags = exports.getSwapCount = exports.applySlippageToMinOut = exports.applySlippageToPrice = void 0;
|
|
3
|
+
exports.getLimitOrderTypeByTradeMode = exports.createTradeFlags = exports.getSwapCount = exports.applySlippageToMinOut = exports.applySlippageToPrice = void 0;
|
|
4
4
|
const factors_1 = require("../../configs/factors");
|
|
5
5
|
const bigmath_1 = require("../bigmath");
|
|
6
6
|
const types_1 = require("../orders/types");
|
|
@@ -61,3 +61,13 @@ const createTradeFlags = (tradeType, tradeMode) => {
|
|
|
61
61
|
return tradeFlags;
|
|
62
62
|
};
|
|
63
63
|
exports.createTradeFlags = createTradeFlags;
|
|
64
|
+
function getLimitOrderTypeByTradeMode(tradeMode) {
|
|
65
|
+
if (tradeMode === types_2.TradeMode.Limit) {
|
|
66
|
+
return types_1.OrderType.LimitIncrease;
|
|
67
|
+
}
|
|
68
|
+
if (tradeMode === types_2.TradeMode.StopMarket) {
|
|
69
|
+
return types_1.OrderType.StopIncrease;
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
exports.getLimitOrderTypeByTradeMode = getLimitOrderTypeByTradeMode;
|
|
@@ -117,6 +117,9 @@ function createRawTradeActionTransformer(marketsInfoData, wrappedToken, tokensDa
|
|
|
117
117
|
? (0, tokens_1.parseContractPrice)(BigInt(rawAction.indexTokenPriceMax), indexToken.decimals)
|
|
118
118
|
: undefined,
|
|
119
119
|
orderType,
|
|
120
|
+
decreasePositionSwapType: rawAction.decreasePositionSwapType === null || rawAction.decreasePositionSwapType === undefined
|
|
121
|
+
? undefined
|
|
122
|
+
: Number(rawAction.decreasePositionSwapType),
|
|
120
123
|
orderKey: rawAction.orderKey,
|
|
121
124
|
isLong: rawAction.isLong,
|
|
122
125
|
pnlUsd: rawAction.pnlUsd ? BigInt(rawAction.pnlUsd) : undefined,
|