@gmx-io/sdk 1.8.2 → 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 +0 -88
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +0 -748
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +0 -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/markets.js +0 -77
- package/build/cjs/src/configs/tokens.js +0 -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/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/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 +0 -88
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +0 -748
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +0 -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/markets.js +0 -77
- package/build/esm/src/configs/tokens.js +0 -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/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/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 +73 -1074
- 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/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/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/yield/api.d.ts +4 -1
- package/build/types/src/utils/yield/types.d.ts +18 -0
- package/package.json +3 -3
|
@@ -1,15 +1,143 @@
|
|
|
1
1
|
import { maxUint256 } from "viem";
|
|
2
2
|
import { BASIS_POINTS_DIVISOR_BIGINT, DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER } from "../../configs/factors";
|
|
3
|
+
import { getWrappedToken } from "../../configs/tokens";
|
|
3
4
|
import { bigMath } from "../bigmath";
|
|
4
5
|
import { capPositionImpactUsdByMaxImpactPool, capPositionImpactUsdByMaxPriceImpactFactor, getPositionFee, getPriceImpactForPosition, getTotalSwapVolumeFromSwapStats, } from "../fees";
|
|
5
|
-
import { applyFactor } from "../numbers";
|
|
6
|
-
import { OrderType } from "../orders/types";
|
|
6
|
+
import { applyFactor, expandDecimals, roundUpMagnitudeDivision } from "../numbers";
|
|
7
|
+
import { OrderType, SwapPricingType } from "../orders/types";
|
|
7
8
|
import { getEntryPrice, getLeverage, getLiquidationPrice, getPositionPnlUsd, getPriceImpactDiffUsd, } from "../positions";
|
|
8
9
|
import { getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getMarkPrice, getOrderThresholdType, } from "../prices";
|
|
9
|
-
import { getSwapAmountsByFromValue, getSwapAmountsByToValue } from "../swap";
|
|
10
|
-
import { convertToTokenAmount, convertToTokenAmountForIncrease, convertToUsd, getTokensRatioByPrice, } from "../tokens";
|
|
10
|
+
import { getSwapAmountsByFromValue, getSwapAmountsByToValue, getSwapPathStats } from "../swap";
|
|
11
|
+
import { convertToTokenAmount, convertToTokenAmountForIncrease, convertToUsd, getIsEquivalentTokens, getTokensRatioByPrice, } from "../tokens";
|
|
12
|
+
function getBaseCollateralUsdFromSwapStrategy({ swapStrategy, initialCollateralToken, collateralToken, collateralPrice, swapPathStatsAtEvaluationPrice, }) {
|
|
13
|
+
if (swapStrategy.type === "noSwap" && getIsEquivalentTokens(initialCollateralToken, collateralToken)) {
|
|
14
|
+
return convertToUsd(swapStrategy.amountIn, initialCollateralToken.decimals, collateralPrice);
|
|
15
|
+
}
|
|
16
|
+
const grossAmountOut = swapPathStatsAtEvaluationPrice?.amountOut ?? swapStrategy.swapPathStats?.amountOut ?? swapStrategy.amountOut;
|
|
17
|
+
return convertToUsd(grossAmountOut, collateralToken.decimals, collateralPrice);
|
|
18
|
+
}
|
|
19
|
+
function getSwapRouteAtEvaluationPrice({ swapStrategy, marketsInfoData, indexToken, initialCollateralToken, evaluationPrice, chainId, }) {
|
|
20
|
+
if (evaluationPrice === undefined || swapStrategy.type !== "internalSwap" || !marketsInfoData) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const { swapPath, tokenInAddress } = swapStrategy.swapPathStats;
|
|
24
|
+
let hasRepricedToken = false;
|
|
25
|
+
const withEvaluationPrice = (token) => {
|
|
26
|
+
if (!getIsEquivalentTokens(token, indexToken)) {
|
|
27
|
+
return token;
|
|
28
|
+
}
|
|
29
|
+
hasRepricedToken = true;
|
|
30
|
+
return { ...token, prices: { minPrice: evaluationPrice, maxPrice: evaluationPrice } };
|
|
31
|
+
};
|
|
32
|
+
const repricedMarketsInfoData = { ...marketsInfoData };
|
|
33
|
+
for (const marketAddress of swapPath) {
|
|
34
|
+
const marketInfo = marketsInfoData[marketAddress];
|
|
35
|
+
if (!marketInfo) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
repricedMarketsInfoData[marketAddress] = {
|
|
39
|
+
...marketInfo,
|
|
40
|
+
indexToken: withEvaluationPrice(marketInfo.indexToken),
|
|
41
|
+
longToken: withEvaluationPrice(marketInfo.longToken),
|
|
42
|
+
shortToken: withEvaluationPrice(marketInfo.shortToken),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const priceIn = getIsEquivalentTokens(initialCollateralToken, indexToken)
|
|
46
|
+
? evaluationPrice
|
|
47
|
+
: initialCollateralToken.prices.minPrice;
|
|
48
|
+
if (!hasRepricedToken && priceIn === initialCollateralToken.prices.minPrice) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
marketsInfoData: repricedMarketsInfoData,
|
|
53
|
+
swapPath,
|
|
54
|
+
tokenInAddress,
|
|
55
|
+
priceIn,
|
|
56
|
+
wrappedNativeTokenAddress: getWrappedToken(chainId).address,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function getSwapPathStatsOnRoute(route, usdIn) {
|
|
60
|
+
return getSwapPathStats({
|
|
61
|
+
marketsInfoData: route.marketsInfoData,
|
|
62
|
+
swapPath: route.swapPath,
|
|
63
|
+
initialCollateralAddress: route.tokenInAddress,
|
|
64
|
+
wrappedNativeTokenAddress: route.wrappedNativeTokenAddress,
|
|
65
|
+
usdIn,
|
|
66
|
+
shouldUnwrapNativeToken: false,
|
|
67
|
+
shouldApplyPriceImpact: true,
|
|
68
|
+
swapPricingType: SwapPricingType.Swap,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function getSwapPathStatsAtEvaluationPrice(p) {
|
|
72
|
+
const route = getSwapRouteAtEvaluationPrice(p);
|
|
73
|
+
if (!route) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
return getSwapPathStatsOnRoute(route, convertToUsd(p.swapStrategy.amountIn, p.initialCollateralToken.decimals, route.priceIn));
|
|
77
|
+
}
|
|
78
|
+
const SWAP_AMOUNT_IN_AT_EVALUATION_PRICE_ITERATIONS = 2;
|
|
79
|
+
function getSwapAmountInForCollateralAtEvaluationPrice({ swapStrategy, marketsInfoData, indexToken, initialCollateralToken, collateralToken, collateralPrice, evaluationPrice, chainId, targetCollateralUsd, uiFeeFactor, }) {
|
|
80
|
+
if (targetCollateralUsd <= 0n) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const route = getSwapRouteAtEvaluationPrice({
|
|
84
|
+
swapStrategy,
|
|
85
|
+
marketsInfoData,
|
|
86
|
+
indexToken,
|
|
87
|
+
initialCollateralToken,
|
|
88
|
+
evaluationPrice,
|
|
89
|
+
chainId,
|
|
90
|
+
});
|
|
91
|
+
if (!route || route.priceIn <= 0n) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
let usdIn = targetCollateralUsd;
|
|
95
|
+
for (let i = 0; i < SWAP_AMOUNT_IN_AT_EVALUATION_PRICE_ITERATIONS; i++) {
|
|
96
|
+
const swapPathStats = getSwapPathStatsOnRoute(route, usdIn);
|
|
97
|
+
if (!swapPathStats) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
const collateralUsd = convertToUsd(swapPathStats.amountOut, collateralToken.decimals, collateralPrice) -
|
|
101
|
+
applyFactor(getTotalSwapVolumeFromSwapStats(swapPathStats.swapSteps), uiFeeFactor);
|
|
102
|
+
if (collateralUsd <= 0n) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
usdIn = bigMath.mulDiv(usdIn, targetCollateralUsd, collateralUsd);
|
|
106
|
+
}
|
|
107
|
+
return roundUpMagnitudeDivision(usdIn * expandDecimals(1, initialCollateralToken.decimals), route.priceIn);
|
|
108
|
+
}
|
|
109
|
+
function getInternalSwapStrategyForAmountIn({ swapStrategy, amountIn, marketsInfoData, initialCollateralToken, collateralToken, chainId, }) {
|
|
110
|
+
const { swapPath, tokenInAddress } = swapStrategy.swapPathStats;
|
|
111
|
+
const priceIn = initialCollateralToken.prices.minPrice;
|
|
112
|
+
const usdIn = convertToUsd(amountIn, initialCollateralToken.decimals, priceIn);
|
|
113
|
+
const swapPathStats = getSwapPathStats({
|
|
114
|
+
marketsInfoData,
|
|
115
|
+
swapPath,
|
|
116
|
+
initialCollateralAddress: tokenInAddress,
|
|
117
|
+
wrappedNativeTokenAddress: getWrappedToken(chainId).address,
|
|
118
|
+
usdIn,
|
|
119
|
+
shouldUnwrapNativeToken: false,
|
|
120
|
+
shouldApplyPriceImpact: true,
|
|
121
|
+
swapPricingType: SwapPricingType.Swap,
|
|
122
|
+
});
|
|
123
|
+
if (!swapPathStats) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
type: "internalSwap",
|
|
128
|
+
swapPathStats,
|
|
129
|
+
externalSwapQuote: undefined,
|
|
130
|
+
amountIn,
|
|
131
|
+
amountOut: swapPathStats.amountOut,
|
|
132
|
+
usdIn,
|
|
133
|
+
usdOut: swapPathStats.usdOut,
|
|
134
|
+
priceIn,
|
|
135
|
+
priceOut: collateralToken.prices.maxPrice,
|
|
136
|
+
feesUsd: usdIn - swapPathStats.usdOut,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
11
139
|
export function getIncreasePositionAmounts(p) {
|
|
12
|
-
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;
|
|
140
|
+
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;
|
|
13
141
|
const swapStrategy = {
|
|
14
142
|
type: "noSwap",
|
|
15
143
|
externalSwapQuote: undefined,
|
|
@@ -65,8 +193,11 @@ export function getIncreasePositionAmounts(p) {
|
|
|
65
193
|
values.collateralPrice = prices.collateralPrice;
|
|
66
194
|
values.triggerPrice = prices.triggerPrice;
|
|
67
195
|
values.triggerThresholdType = prices.triggerThresholdType;
|
|
196
|
+
const evaluationPrice = prices.evaluationPrice;
|
|
68
197
|
values.borrowingFeeUsd = position?.pendingBorrowingFeesUsd || 0n;
|
|
69
|
-
values.fundingFeeUsd = position
|
|
198
|
+
values.fundingFeeUsd = position
|
|
199
|
+
? convertToUsd(position.fundingFeeAmount, collateralToken.decimals, values.collateralPrice)
|
|
200
|
+
: 0n;
|
|
70
201
|
if (values.indexPrice <= 0 || values.initialCollateralPrice <= 0 || values.collateralPrice <= 0) {
|
|
71
202
|
return values;
|
|
72
203
|
}
|
|
@@ -105,14 +236,27 @@ export function getIncreasePositionAmounts(p) {
|
|
|
105
236
|
});
|
|
106
237
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
107
238
|
}
|
|
108
|
-
const
|
|
109
|
-
|
|
239
|
+
const swapPathStatsAtEvaluationPrice = getSwapPathStatsAtEvaluationPrice({
|
|
240
|
+
swapStrategy: values.swapStrategy,
|
|
241
|
+
marketsInfoData,
|
|
242
|
+
indexToken,
|
|
243
|
+
initialCollateralToken,
|
|
244
|
+
evaluationPrice,
|
|
245
|
+
chainId,
|
|
246
|
+
});
|
|
247
|
+
const baseCollateralUsd = getBaseCollateralUsdFromSwapStrategy({
|
|
248
|
+
swapStrategy: values.swapStrategy,
|
|
249
|
+
initialCollateralToken,
|
|
250
|
+
collateralToken,
|
|
251
|
+
collateralPrice: values.collateralPrice,
|
|
252
|
+
swapPathStatsAtEvaluationPrice,
|
|
253
|
+
});
|
|
110
254
|
const baseSizeDeltaUsd = bigMath.mulDiv(baseCollateralUsd, leverage, BASIS_POINTS_DIVISOR_BIGINT);
|
|
111
255
|
const baseSizeDeltaInTokens = convertToTokenAmountForIncrease(baseSizeDeltaUsd, indexToken.decimals, values.indexPrice, isLong);
|
|
112
256
|
const { balanceWasImproved: baseBalanceWasImproved } = getPriceImpactForPosition(marketInfo, baseSizeDeltaUsd, isLong, { sizeDeltaInTokens: baseSizeDeltaInTokens });
|
|
113
|
-
const basePositionFeeInfo = getPositionFee(marketInfo, baseSizeDeltaUsd, baseBalanceWasImproved, userReferralInfo);
|
|
257
|
+
const basePositionFeeInfo = getPositionFee(marketInfo, baseSizeDeltaUsd, baseBalanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
114
258
|
const baseUiFeeUsd = applyFactor(baseSizeDeltaUsd, uiFeeFactor);
|
|
115
|
-
const totalSwapVolumeUsd = getTotalSwapVolumeFromSwapStats(values.swapStrategy.swapPathStats?.swapSteps);
|
|
259
|
+
const totalSwapVolumeUsd = getTotalSwapVolumeFromSwapStats((swapPathStatsAtEvaluationPrice ?? values.swapStrategy.swapPathStats)?.swapSteps);
|
|
116
260
|
values.swapUiFeeUsd = applyFactor(totalSwapVolumeUsd, uiFeeFactor);
|
|
117
261
|
values.sizeDeltaUsd = bigMath.mulDiv(baseCollateralUsd - basePositionFeeInfo.positionFeeUsd - baseUiFeeUsd - values.swapUiFeeUsd, leverage, BASIS_POINTS_DIVISOR_BIGINT);
|
|
118
262
|
if (values.sizeDeltaUsd <= 0n) {
|
|
@@ -122,7 +266,7 @@ export function getIncreasePositionAmounts(p) {
|
|
|
122
266
|
const { balanceWasImproved } = getPriceImpactForPosition(marketInfo, values.sizeDeltaUsd, isLong, {
|
|
123
267
|
sizeDeltaInTokens: values.indexTokenAmount,
|
|
124
268
|
});
|
|
125
|
-
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo);
|
|
269
|
+
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
126
270
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
127
271
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
128
272
|
values.uiFeeUsd = applyFactor(values.sizeDeltaUsd, uiFeeFactor);
|
|
@@ -146,11 +290,11 @@ export function getIncreasePositionAmounts(p) {
|
|
|
146
290
|
const { balanceWasImproved } = getPriceImpactForPosition(marketInfo, values.sizeDeltaUsd, isLong, {
|
|
147
291
|
sizeDeltaInTokens: sizeDeltaInTokensForPriceImpact,
|
|
148
292
|
});
|
|
149
|
-
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo);
|
|
293
|
+
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
150
294
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
151
295
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
152
296
|
values.uiFeeUsd = applyFactor(values.sizeDeltaUsd, uiFeeFactor);
|
|
153
|
-
const { collateralDeltaUsd, collateralDeltaAmount, baseCollateralAmount } = leverageBySizeValues({
|
|
297
|
+
const { collateralDeltaUsd, collateralDeltaAmount, baseCollateralUsd, baseCollateralAmount } = leverageBySizeValues({
|
|
154
298
|
collateralToken,
|
|
155
299
|
leverage,
|
|
156
300
|
sizeDeltaUsd: values.sizeDeltaUsd,
|
|
@@ -189,10 +333,57 @@ export function getIncreasePositionAmounts(p) {
|
|
|
189
333
|
manualPath,
|
|
190
334
|
});
|
|
191
335
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
336
|
+
if (values.swapStrategy.type === "internalSwap" && marketsInfoData) {
|
|
337
|
+
const amountInAtEvaluationPrice = getSwapAmountInForCollateralAtEvaluationPrice({
|
|
338
|
+
swapStrategy: values.swapStrategy,
|
|
339
|
+
marketsInfoData,
|
|
340
|
+
indexToken,
|
|
341
|
+
initialCollateralToken,
|
|
342
|
+
collateralToken,
|
|
343
|
+
collateralPrice: values.collateralPrice,
|
|
344
|
+
evaluationPrice,
|
|
345
|
+
chainId,
|
|
346
|
+
targetCollateralUsd: baseCollateralUsd - values.swapUiFeeUsd,
|
|
347
|
+
uiFeeFactor,
|
|
348
|
+
});
|
|
349
|
+
const swapStrategyAtEvaluationPrice = amountInAtEvaluationPrice === undefined
|
|
350
|
+
? undefined
|
|
351
|
+
: getInternalSwapStrategyForAmountIn({
|
|
352
|
+
swapStrategy: values.swapStrategy,
|
|
353
|
+
amountIn: amountInAtEvaluationPrice,
|
|
354
|
+
marketsInfoData,
|
|
355
|
+
initialCollateralToken,
|
|
356
|
+
collateralToken,
|
|
357
|
+
chainId,
|
|
358
|
+
});
|
|
359
|
+
if (swapStrategyAtEvaluationPrice) {
|
|
360
|
+
values.swapStrategy = swapStrategyAtEvaluationPrice;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
192
363
|
}
|
|
193
364
|
const swapAmountIn = values.swapStrategy.amountIn;
|
|
194
365
|
values.initialCollateralAmount = swapAmountIn;
|
|
195
366
|
values.initialCollateralUsd = convertToUsd(values.initialCollateralAmount, initialCollateralToken.decimals, values.initialCollateralPrice);
|
|
367
|
+
const swapPathStatsAtEvaluationPrice = getSwapPathStatsAtEvaluationPrice({
|
|
368
|
+
swapStrategy: values.swapStrategy,
|
|
369
|
+
marketsInfoData,
|
|
370
|
+
indexToken,
|
|
371
|
+
initialCollateralToken,
|
|
372
|
+
evaluationPrice,
|
|
373
|
+
chainId,
|
|
374
|
+
});
|
|
375
|
+
if (swapPathStatsAtEvaluationPrice) {
|
|
376
|
+
const baseCollateralUsdAtEvaluationPrice = convertToUsd(swapPathStatsAtEvaluationPrice.amountOut, collateralToken.decimals, values.collateralPrice);
|
|
377
|
+
const swapUiFeeUsdAtEvaluationPrice = applyFactor(getTotalSwapVolumeFromSwapStats(swapPathStatsAtEvaluationPrice.swapSteps), uiFeeFactor);
|
|
378
|
+
values.collateralDeltaUsd =
|
|
379
|
+
baseCollateralUsdAtEvaluationPrice -
|
|
380
|
+
values.positionFeeUsd -
|
|
381
|
+
values.borrowingFeeUsd -
|
|
382
|
+
values.fundingFeeUsd -
|
|
383
|
+
values.uiFeeUsd -
|
|
384
|
+
swapUiFeeUsdAtEvaluationPrice;
|
|
385
|
+
values.collateralDeltaAmount = convertToTokenAmount(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
386
|
+
}
|
|
196
387
|
}
|
|
197
388
|
else if (strategy === "independent") {
|
|
198
389
|
if (indexTokenAmount !== undefined && indexTokenAmount > 0) {
|
|
@@ -202,7 +393,7 @@ export function getIncreasePositionAmounts(p) {
|
|
|
202
393
|
const { balanceWasImproved } = getPriceImpactForPosition(marketInfo, values.sizeDeltaUsd, isLong, {
|
|
203
394
|
sizeDeltaInTokens: sizeDeltaInTokensForPriceImpact,
|
|
204
395
|
});
|
|
205
|
-
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo);
|
|
396
|
+
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
206
397
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
207
398
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
208
399
|
values.uiFeeUsd = applyFactor(values.sizeDeltaUsd, uiFeeFactor);
|
|
@@ -237,8 +428,23 @@ export function getIncreasePositionAmounts(p) {
|
|
|
237
428
|
});
|
|
238
429
|
values.swapStrategy = swapAmounts.swapStrategy;
|
|
239
430
|
}
|
|
240
|
-
const
|
|
241
|
-
|
|
431
|
+
const swapPathStatsAtEvaluationPrice = getSwapPathStatsAtEvaluationPrice({
|
|
432
|
+
swapStrategy: values.swapStrategy,
|
|
433
|
+
marketsInfoData,
|
|
434
|
+
indexToken,
|
|
435
|
+
initialCollateralToken,
|
|
436
|
+
evaluationPrice,
|
|
437
|
+
chainId,
|
|
438
|
+
});
|
|
439
|
+
const baseCollateralUsd = getBaseCollateralUsdFromSwapStrategy({
|
|
440
|
+
swapStrategy: values.swapStrategy,
|
|
441
|
+
initialCollateralToken,
|
|
442
|
+
collateralToken,
|
|
443
|
+
collateralPrice: values.collateralPrice,
|
|
444
|
+
swapPathStatsAtEvaluationPrice,
|
|
445
|
+
});
|
|
446
|
+
const totalSwapVolumeUsd = getTotalSwapVolumeFromSwapStats((swapPathStatsAtEvaluationPrice ?? values.swapStrategy.swapPathStats)?.swapSteps);
|
|
447
|
+
values.swapUiFeeUsd = applyFactor(totalSwapVolumeUsd, uiFeeFactor);
|
|
242
448
|
values.collateralDeltaUsd =
|
|
243
449
|
baseCollateralUsd -
|
|
244
450
|
values.positionFeeUsd -
|
|
@@ -248,6 +454,13 @@ export function getIncreasePositionAmounts(p) {
|
|
|
248
454
|
values.swapUiFeeUsd;
|
|
249
455
|
values.collateralDeltaAmount = convertToTokenAmount(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
250
456
|
}
|
|
457
|
+
else if (values.sizeDeltaUsd > 0n) {
|
|
458
|
+
values.collateralDeltaUsd = -(values.positionFeeUsd +
|
|
459
|
+
values.borrowingFeeUsd +
|
|
460
|
+
values.fundingFeeUsd +
|
|
461
|
+
values.uiFeeUsd);
|
|
462
|
+
values.collateralDeltaAmount = convertToTokenAmount(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
463
|
+
}
|
|
251
464
|
values.estimatedLeverage = getLeverage({
|
|
252
465
|
sizeInUsd: values.sizeDeltaUsd,
|
|
253
466
|
collateralUsd: values.collateralDeltaUsd,
|
|
@@ -329,7 +542,7 @@ export function getTokensRatio({ fromToken, toToken, triggerRatioValue, markPric
|
|
|
329
542
|
};
|
|
330
543
|
}
|
|
331
544
|
export function leverageBySizeValues({ collateralToken, leverage, sizeDeltaUsd, collateralPrice, positionFeeUsd, borrowingFeeUsd, uiFeeUsd, swapUiFeeUsd, fundingFeeUsd, }) {
|
|
332
|
-
const collateralDeltaUsd = bigMath.mulDiv(sizeDeltaUsd, BASIS_POINTS_DIVISOR_BIGINT, leverage);
|
|
545
|
+
const collateralDeltaUsd = leverage > 0n ? bigMath.mulDiv(sizeDeltaUsd, BASIS_POINTS_DIVISOR_BIGINT, leverage) : 0n;
|
|
333
546
|
const collateralDeltaAmount = convertToTokenAmount(collateralDeltaUsd, collateralToken.decimals, collateralPrice);
|
|
334
547
|
const baseCollateralUsd = collateralDeltaUsd !== 0n
|
|
335
548
|
? collateralDeltaUsd + positionFeeUsd + borrowingFeeUsd + fundingFeeUsd + uiFeeUsd + swapUiFeeUsd
|
|
@@ -347,10 +560,14 @@ export function getIncreasePositionPrices({ triggerPrice, indexToken, initialCol
|
|
|
347
560
|
let initialCollateralPrice;
|
|
348
561
|
let triggerThresholdType;
|
|
349
562
|
let collateralPrice;
|
|
563
|
+
let evaluationPrice;
|
|
350
564
|
if (triggerPrice !== undefined && triggerPrice > 0 && limitOrderType !== undefined) {
|
|
351
565
|
indexPrice = triggerPrice;
|
|
566
|
+
evaluationPrice = triggerPrice;
|
|
352
567
|
initialCollateralPrice = initialCollateralToken.prices.minPrice;
|
|
353
|
-
collateralPrice = collateralToken
|
|
568
|
+
collateralPrice = getIsEquivalentTokens(collateralToken, indexToken)
|
|
569
|
+
? triggerPrice
|
|
570
|
+
: collateralToken.prices.minPrice;
|
|
354
571
|
triggerThresholdType = getOrderThresholdType(limitOrderType, isLong);
|
|
355
572
|
}
|
|
356
573
|
else {
|
|
@@ -362,13 +579,19 @@ export function getIncreasePositionPrices({ triggerPrice, indexToken, initialCol
|
|
|
362
579
|
indexPrice,
|
|
363
580
|
initialCollateralPrice,
|
|
364
581
|
collateralPrice,
|
|
582
|
+
evaluationPrice,
|
|
365
583
|
triggerThresholdType,
|
|
366
584
|
triggerPrice,
|
|
367
585
|
};
|
|
368
586
|
}
|
|
369
587
|
export function getNextPositionValuesForIncreaseTrade(p) {
|
|
370
|
-
const { existingPosition, marketInfo, collateralToken, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaUsd, collateralDeltaAmount, indexPrice, isLong, showPnlInLeverage, minCollateralUsd, userReferralInfo, positionPriceImpactDeltaUsd, } = p;
|
|
371
|
-
const
|
|
588
|
+
const { existingPosition, marketInfo, collateralToken, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaUsd, collateralDeltaAmount, indexPrice, isLong, showPnlInLeverage, minCollateralUsd, userReferralInfo, positionPriceImpactDeltaUsd, collateralPrice, } = p;
|
|
589
|
+
const existingCollateralUsd = !existingPosition
|
|
590
|
+
? 0n
|
|
591
|
+
: collateralPrice === undefined || collateralPrice <= 0n
|
|
592
|
+
? existingPosition.collateralUsd
|
|
593
|
+
: convertToUsd(existingPosition.collateralAmount, collateralToken.decimals, collateralPrice);
|
|
594
|
+
const nextCollateralUsd = existingCollateralUsd + collateralDeltaUsd;
|
|
372
595
|
const nextCollateralAmount = existingPosition
|
|
373
596
|
? existingPosition.collateralAmount + collateralDeltaAmount
|
|
374
597
|
: collateralDeltaAmount;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { capPositionImpactUsdByMaxPriceImpactFactor, getPriceImpactForPosition } from "../fees";
|
|
2
|
+
import { getMarketInfoWithOpenInterestDelta, getOpenInterestUsd, getPriceForPnl } from "../markets";
|
|
3
|
+
import { applyFactor, expandDecimals, roundUpMagnitudeDivision } from "../numbers";
|
|
4
|
+
import { getPositionPnlUsd } from "../positions";
|
|
5
|
+
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens } from "../tokens";
|
|
6
|
+
export var PositionMarginFailureReason;
|
|
7
|
+
(function (PositionMarginFailureReason) {
|
|
8
|
+
PositionMarginFailureReason["MinCollateral"] = "min collateral";
|
|
9
|
+
PositionMarginFailureReason["NonPositiveRemainingMargin"] = "< 0";
|
|
10
|
+
PositionMarginFailureReason["MinCollateralForLeverage"] = "min collateral for leverage";
|
|
11
|
+
PositionMarginFailureReason["InsufficientCollateralUsd"] = "insufficient collateral usd";
|
|
12
|
+
})(PositionMarginFailureReason || (PositionMarginFailureReason = {}));
|
|
13
|
+
export function getIsMaxLeverageMarginReason(reason) {
|
|
14
|
+
return (reason === PositionMarginFailureReason.MinCollateralForLeverage ||
|
|
15
|
+
reason === PositionMarginFailureReason.InsufficientCollateralUsd);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Port of `PositionUtils.isPositionLiquidatable` from the gmx-synthetics build deployed on Arbitrum
|
|
19
|
+
* (release_2.2.1 @ 23c9d160: release-v2.2c plus MarketUtils.getPositivePnl in the per-position pnl cap)
|
|
20
|
+
* for a position whose pending fees have just been settled — the state the contract validates right
|
|
21
|
+
* after an increase.
|
|
22
|
+
*/
|
|
23
|
+
export function getResultingPositionMarginState(p) {
|
|
24
|
+
const { marketInfo, collateralToken, sizeInUsd, sizeInTokens, collateralAmount, pendingImpactAmount, minCollateralUsd, isLong, userReferralInfo, proDiscountFactor, } = p;
|
|
25
|
+
const { indexToken } = marketInfo;
|
|
26
|
+
const positionPnlUsd = getPositionPnlUsd({
|
|
27
|
+
marketInfo,
|
|
28
|
+
sizeInUsd,
|
|
29
|
+
sizeInTokens,
|
|
30
|
+
markPrice: getPriceForPnl(indexToken.prices, isLong, false),
|
|
31
|
+
isLong,
|
|
32
|
+
});
|
|
33
|
+
const collateralUsd = convertToUsd(collateralAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
34
|
+
const { priceImpactDeltaUsd, balanceWasImproved } = getPriceImpactForPosition(marketInfo, -sizeInUsd, isLong, {
|
|
35
|
+
fallbackToZero: true,
|
|
36
|
+
sizeDeltaInTokens: sizeInTokens,
|
|
37
|
+
});
|
|
38
|
+
let totalImpactUsd = priceImpactDeltaUsd;
|
|
39
|
+
if (totalImpactUsd > 0) {
|
|
40
|
+
totalImpactUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeInUsd, totalImpactUsd);
|
|
41
|
+
}
|
|
42
|
+
const pendingImpactUsd = convertToUsd(pendingImpactAmount, indexToken.decimals, pendingImpactAmount > 0 ? indexToken.prices.minPrice : indexToken.prices.maxPrice);
|
|
43
|
+
totalImpactUsd = totalImpactUsd + pendingImpactUsd;
|
|
44
|
+
if (totalImpactUsd >= 0) {
|
|
45
|
+
totalImpactUsd = 0n;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const maxNegativeImpactUsd = -applyFactor(sizeInUsd, marketInfo.maxPositionImpactFactorForLiquidations);
|
|
49
|
+
if (totalImpactUsd < maxNegativeImpactUsd) {
|
|
50
|
+
totalImpactUsd = maxNegativeImpactUsd;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const closingFeeFactor = balanceWasImproved
|
|
54
|
+
? marketInfo.positionFeeFactorForBalanceWasImproved
|
|
55
|
+
: marketInfo.positionFeeFactorForBalanceWasNotImproved;
|
|
56
|
+
let closingFeeAmount = convertToTokenAmount(applyFactor(sizeInUsd, closingFeeFactor), collateralToken.decimals, collateralToken.prices.minPrice) ?? 0n;
|
|
57
|
+
let discountAmount = 0n;
|
|
58
|
+
if (userReferralInfo) {
|
|
59
|
+
const totalRebateAmount = applyFactor(closingFeeAmount, userReferralInfo.totalRebateFactor);
|
|
60
|
+
discountAmount = applyFactor(totalRebateAmount, userReferralInfo.discountFactor);
|
|
61
|
+
}
|
|
62
|
+
if (proDiscountFactor !== undefined && proDiscountFactor > 0n) {
|
|
63
|
+
const proDiscountAmount = applyFactor(closingFeeAmount, proDiscountFactor);
|
|
64
|
+
if (proDiscountAmount > discountAmount) {
|
|
65
|
+
discountAmount = proDiscountAmount;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
closingFeeAmount -= discountAmount;
|
|
69
|
+
const closingCostUsd = convertToUsd(closingFeeAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
70
|
+
const remainingCollateralUsd = collateralUsd + positionPnlUsd + totalImpactUsd - closingCostUsd;
|
|
71
|
+
const minCollateralUsdForLeverage = applyFactor(sizeInUsd, marketInfo.minCollateralFactor);
|
|
72
|
+
const state = {
|
|
73
|
+
remainingCollateralUsd,
|
|
74
|
+
minCollateralUsd,
|
|
75
|
+
minCollateralUsdForLeverage,
|
|
76
|
+
};
|
|
77
|
+
if (remainingCollateralUsd < minCollateralUsd) {
|
|
78
|
+
return { ...state, isLiquidatable: true, reason: PositionMarginFailureReason.MinCollateral };
|
|
79
|
+
}
|
|
80
|
+
if (remainingCollateralUsd <= 0) {
|
|
81
|
+
return { ...state, isLiquidatable: true, reason: PositionMarginFailureReason.NonPositiveRemainingMargin };
|
|
82
|
+
}
|
|
83
|
+
if (remainingCollateralUsd < minCollateralUsdForLeverage) {
|
|
84
|
+
return { ...state, isLiquidatable: true, reason: PositionMarginFailureReason.MinCollateralForLeverage };
|
|
85
|
+
}
|
|
86
|
+
return { ...state, isLiquidatable: false, reason: undefined };
|
|
87
|
+
}
|
|
88
|
+
function withPriceOverride(token, indexToken, price) {
|
|
89
|
+
if (!getIsEquivalentTokens(token, indexToken)) {
|
|
90
|
+
return token;
|
|
91
|
+
}
|
|
92
|
+
return { ...token, prices: { minPrice: price, maxPrice: price } };
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Mirrors the post-increase gates of `IncreasePositionUtils.increasePosition` from the gmx-synthetics
|
|
96
|
+
* build deployed on Arbitrum (release_2.2.1 @ 23c9d160: release-v2.2c plus MarketUtils.getPositivePnl
|
|
97
|
+
* in the per-position pnl cap): `willPositionCollateralBeSufficient` on the updated open interest,
|
|
98
|
+
* then `validatePosition` on the resulting position.
|
|
99
|
+
*/
|
|
100
|
+
export function getIncreaseResultingPositionMarginState(p) {
|
|
101
|
+
const { isLong, existingPosition, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaAmount, minCollateralUsd, userReferralInfo, proDiscountFactor, indexPriceForEvaluation, } = p;
|
|
102
|
+
let { marketInfo, collateralToken } = p;
|
|
103
|
+
if (sizeDeltaUsd <= 0n || sizeDeltaInTokens <= 0n) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
if (indexPriceForEvaluation !== undefined && indexPriceForEvaluation > 0n) {
|
|
107
|
+
const originalIndexToken = marketInfo.indexToken;
|
|
108
|
+
marketInfo = {
|
|
109
|
+
...marketInfo,
|
|
110
|
+
indexToken: withPriceOverride(marketInfo.indexToken, originalIndexToken, indexPriceForEvaluation),
|
|
111
|
+
longToken: withPriceOverride(marketInfo.longToken, originalIndexToken, indexPriceForEvaluation),
|
|
112
|
+
shortToken: withPriceOverride(marketInfo.shortToken, originalIndexToken, indexPriceForEvaluation),
|
|
113
|
+
};
|
|
114
|
+
collateralToken = withPriceOverride(collateralToken, originalIndexToken, indexPriceForEvaluation);
|
|
115
|
+
}
|
|
116
|
+
const { indexToken } = marketInfo;
|
|
117
|
+
if (indexToken.prices.minPrice <= 0n || indexToken.prices.maxPrice <= 0n || collateralToken.prices.minPrice <= 0n) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
const increaseImpact = getPriceImpactForPosition(marketInfo, sizeDeltaUsd, isLong, {
|
|
121
|
+
fallbackToZero: true,
|
|
122
|
+
sizeDeltaInTokens,
|
|
123
|
+
});
|
|
124
|
+
let increaseImpactUsd = increaseImpact.priceImpactDeltaUsd;
|
|
125
|
+
if (increaseImpactUsd > 0n) {
|
|
126
|
+
increaseImpactUsd = capPositionImpactUsdByMaxPriceImpactFactor(marketInfo, sizeDeltaUsd, increaseImpactUsd);
|
|
127
|
+
}
|
|
128
|
+
const increasePendingImpactAmount = increaseImpactUsd > 0n
|
|
129
|
+
? convertToTokenAmount(increaseImpactUsd, indexToken.decimals, indexToken.prices.maxPrice)
|
|
130
|
+
: roundUpMagnitudeDivision(increaseImpactUsd * expandDecimals(1, indexToken.decimals), indexToken.prices.minPrice);
|
|
131
|
+
const nextSizeInUsd = (existingPosition?.sizeInUsd ?? 0n) + sizeDeltaUsd;
|
|
132
|
+
const nextSizeInTokens = (existingPosition?.sizeInTokens ?? 0n) + sizeDeltaInTokens;
|
|
133
|
+
const nextCollateralAmount = (existingPosition?.collateralAmount ?? 0n) + collateralDeltaAmount;
|
|
134
|
+
const nextPendingImpactAmount = (existingPosition?.pendingImpactAmount ?? 0n) + increasePendingImpactAmount;
|
|
135
|
+
const nextMarketInfo = getMarketInfoWithOpenInterestDelta({
|
|
136
|
+
marketInfo,
|
|
137
|
+
collateralToken,
|
|
138
|
+
isLong,
|
|
139
|
+
sizeDeltaUsd,
|
|
140
|
+
sizeDeltaInTokens,
|
|
141
|
+
});
|
|
142
|
+
const nextCollateralAmountClamped = nextCollateralAmount < 0n ? 0n : nextCollateralAmount;
|
|
143
|
+
const remainingCollateralUsdForOpenInterest = convertToUsd(nextCollateralAmountClamped, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
144
|
+
const minCollateralFactorMultiplier = isLong
|
|
145
|
+
? nextMarketInfo.minCollateralFactorForOpenInterestLong
|
|
146
|
+
: nextMarketInfo.minCollateralFactorForOpenInterestShort;
|
|
147
|
+
let minCollateralFactorForOpenInterest = applyFactor(getOpenInterestUsd(nextMarketInfo, isLong), minCollateralFactorMultiplier);
|
|
148
|
+
if (nextMarketInfo.minCollateralFactor > minCollateralFactorForOpenInterest) {
|
|
149
|
+
minCollateralFactorForOpenInterest = nextMarketInfo.minCollateralFactor;
|
|
150
|
+
}
|
|
151
|
+
const minCollateralUsdForOpenInterest = applyFactor(nextSizeInUsd, minCollateralFactorForOpenInterest);
|
|
152
|
+
if (remainingCollateralUsdForOpenInterest < minCollateralUsdForOpenInterest) {
|
|
153
|
+
return {
|
|
154
|
+
isLiquidatable: true,
|
|
155
|
+
reason: PositionMarginFailureReason.InsufficientCollateralUsd,
|
|
156
|
+
remainingCollateralUsd: remainingCollateralUsdForOpenInterest,
|
|
157
|
+
minCollateralUsd,
|
|
158
|
+
minCollateralUsdForLeverage: minCollateralUsdForOpenInterest,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return getResultingPositionMarginState({
|
|
162
|
+
marketInfo: nextMarketInfo,
|
|
163
|
+
collateralToken,
|
|
164
|
+
sizeInUsd: nextSizeInUsd,
|
|
165
|
+
sizeInTokens: nextSizeInTokens,
|
|
166
|
+
collateralAmount: nextCollateralAmountClamped,
|
|
167
|
+
pendingImpactAmount: nextPendingImpactAmount,
|
|
168
|
+
minCollateralUsd,
|
|
169
|
+
isLong,
|
|
170
|
+
userReferralInfo,
|
|
171
|
+
proDiscountFactor,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BASIS_POINTS_DIVISOR, BASIS_POINTS_DIVISOR_BIGINT } from "../../configs/factors";
|
|
2
2
|
import { bigMath } from "../bigmath";
|
|
3
|
-
import { DecreasePositionSwapType } from "../orders/types";
|
|
3
|
+
import { DecreasePositionSwapType, OrderType } from "../orders/types";
|
|
4
4
|
import { getShouldUseMaxPrice } from "../prices";
|
|
5
5
|
import { TradeMode, TradeType, } from "./types";
|
|
6
6
|
export function applySlippageToPrice(allowedSlippage, price, isIncrease, isLong) {
|
|
@@ -54,3 +54,12 @@ export const createTradeFlags = (tradeType, tradeMode) => {
|
|
|
54
54
|
};
|
|
55
55
|
return tradeFlags;
|
|
56
56
|
};
|
|
57
|
+
export function getLimitOrderTypeByTradeMode(tradeMode) {
|
|
58
|
+
if (tradeMode === TradeMode.Limit) {
|
|
59
|
+
return OrderType.LimitIncrease;
|
|
60
|
+
}
|
|
61
|
+
if (tradeMode === TradeMode.StopMarket) {
|
|
62
|
+
return OrderType.StopIncrease;
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|