@gmx-io/sdk 2.3.2 → 2.4.0
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/configs/chains.js +13 -1
- package/build/cjs/src/configs/markets.js +12 -7
- package/build/cjs/src/configs/tokens.js +12 -7
- package/build/cjs/src/utils/__tests__/numbers.spec.js +24 -0
- package/build/cjs/src/utils/__tests__/parseError.spec.js +61 -0
- package/build/cjs/src/utils/errors/parseError.js +10 -0
- package/build/cjs/src/utils/errors/transactionsErrors.js +6 -0
- package/build/cjs/src/utils/markets/__tests__/multicall.spec.js +27 -0
- package/build/cjs/src/utils/markets/multicall.js +5 -7
- package/build/cjs/src/utils/numbers/utils.js +7 -1
- package/build/cjs/src/utils/orders/utils.js +8 -1
- package/build/cjs/src/utils/paxos/__tests__/api.spec.js +23 -3
- package/build/cjs/src/utils/paxos/api.js +6 -0
- package/build/cjs/src/utils/swap/preparedSwapData.js +6 -4
- package/build/cjs/src/utils/swap/swapPath.js +1 -1
- package/build/cjs/src/utils/trade/__tests__/decreaseMarginCheck.spec.js +224 -0
- package/build/cjs/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +128 -9
- package/build/cjs/src/utils/trade/decrease.js +198 -33
- package/build/cjs/src/utils/trade/decreaseMarginCheck.js +157 -0
- package/build/cjs/src/utils/trade/increaseMarginCheck.js +27 -15
- package/build/cjs/src/utils/trade/index.js +1 -0
- package/build/esm/src/configs/chains.js +11 -0
- package/build/esm/src/configs/markets.js +13 -8
- package/build/esm/src/configs/tokens.js +13 -8
- package/build/esm/src/utils/__tests__/numbers.spec.js +25 -1
- package/build/esm/src/utils/__tests__/parseError.spec.js +62 -1
- package/build/esm/src/utils/errors/parseError.js +11 -1
- package/build/esm/src/utils/errors/transactionsErrors.js +6 -0
- package/build/esm/src/utils/markets/__tests__/multicall.spec.js +25 -0
- package/build/esm/src/utils/markets/multicall.js +5 -7
- package/build/esm/src/utils/numbers/utils.js +5 -0
- package/build/esm/src/utils/orders/utils.js +6 -0
- package/build/esm/src/utils/paxos/__tests__/api.spec.js +23 -3
- package/build/esm/src/utils/paxos/api.js +6 -0
- package/build/esm/src/utils/swap/preparedSwapData.js +6 -4
- package/build/esm/src/utils/swap/swapPath.js +2 -2
- package/build/esm/src/utils/trade/__tests__/decreaseMarginCheck.spec.js +222 -0
- package/build/esm/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +129 -10
- package/build/esm/src/utils/trade/decrease.js +195 -32
- package/build/esm/src/utils/trade/decreaseMarginCheck.js +152 -0
- package/build/esm/src/utils/trade/increaseMarginCheck.js +25 -14
- package/build/esm/src/utils/trade/index.js +1 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/configs/chains.d.ts +6 -2
- package/build/types/src/configs/markets.d.ts +4 -4
- package/build/types/src/utils/markets/__tests__/multicall.spec.d.ts +1 -0
- package/build/types/src/utils/numbers/utils.d.ts +1 -0
- package/build/types/src/utils/orderTransactions/api.d.ts +1 -0
- package/build/types/src/utils/orders/utils.d.ts +1 -0
- package/build/types/src/utils/paxos/types.d.ts +2 -0
- package/build/types/src/utils/trade/__tests__/decreaseMarginCheck.spec.d.ts +1 -0
- package/build/types/src/utils/trade/decrease.d.ts +18 -0
- package/build/types/src/utils/trade/decreaseMarginCheck.d.ts +52 -0
- package/build/types/src/utils/trade/increaseMarginCheck.d.ts +11 -1
- package/build/types/src/utils/trade/index.d.ts +1 -0
- package/build/types/src/utils/trade/types.d.ts +1 -0
- package/package.json +1 -1
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { DEFAULT_ACCEPTABLE_PRICE_IMPACT_BUFFER } from "../../configs/factors";
|
|
2
2
|
import { bigMath } from "../bigmath";
|
|
3
3
|
import { getPositionFee } from "../fees";
|
|
4
|
+
import { getMaxAllowedLeverage } from "../markets";
|
|
4
5
|
import { applyFactor, BASIS_POINTS_DIVISOR_BIGINT, expandDecimals, getBasisPoints, roundUpDivision, USD_DECIMALS, MaxUint256, } from "../numbers";
|
|
5
6
|
import { DecreasePositionSwapType, OrderType, SwapPricingType } from "../orders/types";
|
|
6
7
|
import { getLeverage, getLiquidationPrice, getMinCollateralFactorForPosition, getMinCollateralUsdForLiquidationPrice, getNetPriceImpactDeltaUsdForDecrease, getPositionPnlUsd, } from "../positions";
|
|
7
8
|
import { getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getMarkPrice, getOrderThresholdType, } from "../prices";
|
|
8
9
|
import { getSwapAmountsByFromValue, getSwapStats } from "../swap";
|
|
9
10
|
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens } from "../tokens";
|
|
11
|
+
import { getResultingPositionMarginState } from "./increaseMarginCheck";
|
|
10
12
|
export function getDecreasePositionSizeDeltaInTokens(p) {
|
|
11
13
|
const { sizeInUsd, sizeInTokens, sizeDeltaUsd, isLong } = p;
|
|
12
14
|
if (sizeInUsd <= 0n || sizeInTokens <= 0n || sizeDeltaUsd <= 0n) {
|
|
@@ -20,7 +22,7 @@ export function getDecreasePositionSizeDeltaInTokens(p) {
|
|
|
20
22
|
: bigMath.mulDiv(sizeInTokens, sizeDeltaUsd, sizeInUsd);
|
|
21
23
|
}
|
|
22
24
|
export function getDecreasePositionAmounts(p) {
|
|
23
|
-
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType: orderType, receiveToken: receiveTokenArg, isSetAcceptablePriceImpactEnabled, forceDecreaseSwapType, } = p;
|
|
25
|
+
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, proDiscountFactor, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType: orderType, receiveToken: receiveTokenArg, isSetAcceptablePriceImpactEnabled, forceDecreaseSwapType, isTwap, } = p;
|
|
24
26
|
const { indexToken } = marketInfo;
|
|
25
27
|
const receiveToken = receiveTokenArg ?? collateralToken;
|
|
26
28
|
const values = {
|
|
@@ -55,6 +57,7 @@ export function getDecreasePositionAmounts(p) {
|
|
|
55
57
|
payedOutputUsd: 0n,
|
|
56
58
|
payedRemainingCollateralUsd: 0n,
|
|
57
59
|
payedRemainingCollateralAmount: 0n,
|
|
60
|
+
unpaidCostUsd: 0n,
|
|
58
61
|
receiveTokenAmount: 0n,
|
|
59
62
|
receiveUsd: 0n,
|
|
60
63
|
primaryOutput: { tokenAddress: "", amount: 0n, usd: 0n },
|
|
@@ -67,6 +70,7 @@ export function getDecreasePositionAmounts(p) {
|
|
|
67
70
|
values.decreaseSwapType = forceDecreaseSwapType ?? getDecreaseSwapType(pnlToken, collateralToken, receiveToken);
|
|
68
71
|
const markPrice = getMarkPrice({ prices: indexToken.prices, isIncrease: false, isLong });
|
|
69
72
|
const isTrigger = orderType !== undefined;
|
|
73
|
+
const isMarketDecrease = !isTrigger && !isTwap;
|
|
70
74
|
if (orderType) {
|
|
71
75
|
values.triggerPrice = triggerPrice;
|
|
72
76
|
values.indexPrice = triggerPrice ?? markPrice;
|
|
@@ -96,7 +100,7 @@ export function getDecreasePositionAmounts(p) {
|
|
|
96
100
|
isSetAcceptablePriceImpactEnabled,
|
|
97
101
|
});
|
|
98
102
|
}
|
|
99
|
-
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, values.balanceWasImproved, userReferralInfo);
|
|
103
|
+
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, values.balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
100
104
|
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
101
105
|
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
102
106
|
values.uiFeeUsd = applyFactor(values.sizeDeltaUsd, uiFeeFactor);
|
|
@@ -166,33 +170,49 @@ export function getDecreasePositionAmounts(p) {
|
|
|
166
170
|
if (values.totalPendingImpactDeltaUsd > 0) {
|
|
167
171
|
profitUsd = profitUsd + values.totalPendingImpactDeltaUsd;
|
|
168
172
|
}
|
|
169
|
-
const profitAmount =
|
|
173
|
+
const profitAmount = isMarketDecrease
|
|
174
|
+
? getMarketProfitAmountInCollateralToken({
|
|
175
|
+
profitUsd,
|
|
176
|
+
pnlToken,
|
|
177
|
+
collateralToken,
|
|
178
|
+
decreaseSwapType: values.decreaseSwapType,
|
|
179
|
+
})
|
|
180
|
+
: convertToTokenAmount(profitUsd, collateralToken.decimals, values.collateralPrice);
|
|
170
181
|
// Fees
|
|
171
|
-
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, values.balanceWasImproved, userReferralInfo);
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
values.positionFeeUsd = estimatedPositionFeeCost.usd;
|
|
175
|
-
values.feeDiscountUsd = estimatedDiscountCost.usd;
|
|
182
|
+
const positionFeeInfo = getPositionFee(marketInfo, values.sizeDeltaUsd, values.balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
183
|
+
values.positionFeeUsd = positionFeeInfo.positionFeeUsd;
|
|
184
|
+
values.feeDiscountUsd = positionFeeInfo.discountUsd;
|
|
176
185
|
values.uiFeeUsd = applyFactor(values.sizeDeltaUsd, uiFeeFactor);
|
|
177
|
-
|
|
178
|
-
values.borrowingFeeUsd = borrowFeeCost.usd;
|
|
186
|
+
values.borrowingFeeUsd = position.pendingBorrowingFeesUsd;
|
|
179
187
|
const fundingFeeCost = estimateCollateralCost(position.pendingFundingFeesUsd, collateralToken, values.collateralPrice);
|
|
180
188
|
values.fundingFeeUsd = fundingFeeCost.usd;
|
|
189
|
+
const isMarketCollateralToPnlSwap = isMarketDecrease && values.decreaseSwapType === DecreasePositionSwapType.SwapCollateralTokenToPnlToken;
|
|
190
|
+
let isProfitSwapReverted = false;
|
|
181
191
|
if (profitUsd > 0 &&
|
|
192
|
+
!isMarketCollateralToPnlSwap &&
|
|
182
193
|
(values.decreaseSwapType === DecreasePositionSwapType.SwapPnlTokenToCollateralToken ||
|
|
183
194
|
values.decreaseSwapType === DecreasePositionSwapType.SwapCollateralTokenToPnlToken)) {
|
|
184
195
|
const isSwapToCollateral = values.decreaseSwapType === DecreasePositionSwapType.SwapPnlTokenToCollateralToken;
|
|
185
196
|
const swapProfitStats = getSwapStats({
|
|
186
|
-
marketInfo,
|
|
197
|
+
marketInfo: isSwapToCollateral ? getMarketInfoAfterProfitPayout({ marketInfo, isLong, profitUsd }) : marketInfo,
|
|
187
198
|
tokenInAddress: isSwapToCollateral ? pnlToken.address : collateralToken.address,
|
|
188
199
|
tokenOutAddress: isSwapToCollateral ? collateralToken.address : pnlToken.address,
|
|
189
200
|
usdIn: profitUsd,
|
|
190
201
|
shouldApplyPriceImpact: true,
|
|
191
202
|
swapPricingType: SwapPricingType.Swap,
|
|
192
203
|
});
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
204
|
+
const isPnlTokenPoolOverCap = isLong
|
|
205
|
+
? marketInfo.longPoolAmount > marketInfo.maxLongPoolAmount
|
|
206
|
+
: marketInfo.shortPoolAmount > marketInfo.maxShortPoolAmount;
|
|
207
|
+
isProfitSwapReverted =
|
|
208
|
+
isMarketDecrease &&
|
|
209
|
+
isSwapToCollateral &&
|
|
210
|
+
Boolean(swapProfitStats.isOutLiquidity || swapProfitStats.isOutMaxFactor || isPnlTokenPoolOverCap);
|
|
211
|
+
if (!isProfitSwapReverted) {
|
|
212
|
+
values.swapProfitFeeUsd = swapProfitStats.swapFeeUsd - swapProfitStats.priceImpactDeltaUsd;
|
|
213
|
+
values.swapProfitUsdIn = swapProfitStats.usdIn;
|
|
214
|
+
values.swapUiFeeUsd = applyFactor(swapProfitStats.usdIn, uiFeeFactor);
|
|
215
|
+
}
|
|
196
216
|
}
|
|
197
217
|
else {
|
|
198
218
|
values.swapProfitFeeUsd = 0n;
|
|
@@ -208,16 +228,33 @@ export function getDecreasePositionAmounts(p) {
|
|
|
208
228
|
pnlUsd: values.realizedPnl,
|
|
209
229
|
totalPendingImpactDeltaUsd: values.totalPendingImpactDeltaUsd,
|
|
210
230
|
});
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
231
|
+
const isProfitKeptInPnlToken = isMarketDecrease &&
|
|
232
|
+
getIsProfitKeptInPnlToken({
|
|
233
|
+
pnlToken,
|
|
234
|
+
collateralToken,
|
|
235
|
+
decreaseSwapType: values.decreaseSwapType,
|
|
236
|
+
isProfitSwapReverted,
|
|
237
|
+
});
|
|
238
|
+
const payedInfo = isProfitKeptInPnlToken
|
|
239
|
+
? payForCollateralCostBeforePnlTokenProfit({
|
|
240
|
+
initialCostUsd: totalFeesUsd,
|
|
241
|
+
profitUsd,
|
|
242
|
+
pnlToken,
|
|
243
|
+
collateralToken,
|
|
244
|
+
collateralPrice: values.collateralPrice,
|
|
245
|
+
remainingCollateralAmount: position.collateralAmount,
|
|
246
|
+
})
|
|
247
|
+
: payForCollateralCost({
|
|
248
|
+
initialCostUsd: totalFeesUsd,
|
|
249
|
+
collateralToken,
|
|
250
|
+
collateralPrice: values.collateralPrice,
|
|
251
|
+
outputAmount: profitAmount,
|
|
252
|
+
remainingCollateralAmount: position.collateralAmount,
|
|
253
|
+
});
|
|
218
254
|
values.payedOutputUsd = convertToUsd(payedInfo.paidOutputAmount, collateralToken.decimals, values.collateralPrice);
|
|
219
255
|
values.payedRemainingCollateralAmount = payedInfo.paidRemainingCollateralAmount;
|
|
220
256
|
values.payedRemainingCollateralUsd = convertToUsd(payedInfo.paidRemainingCollateralAmount, collateralToken.decimals, values.collateralPrice);
|
|
257
|
+
values.unpaidCostUsd = convertToUsd(payedInfo.unpaidCostAmount, collateralToken.decimals, values.collateralPrice);
|
|
221
258
|
let netCollateralOutputAmount = 0n;
|
|
222
259
|
let netCollateralOutputUsd = 0n;
|
|
223
260
|
// Collateral delta
|
|
@@ -249,10 +286,22 @@ export function getDecreasePositionAmounts(p) {
|
|
|
249
286
|
leverageWithoutPnl !== undefined && leverageWithoutPnl !== 0n
|
|
250
287
|
? remainingCollateralUsd - bigMath.mulDiv(nextSizeInUsd, BASIS_POINTS_DIVISOR_BIGINT, leverageWithoutPnl)
|
|
251
288
|
: 0n;
|
|
289
|
+
if (isMarketDecrease && values.collateralDeltaUsd < 0n) {
|
|
290
|
+
values.collateralDeltaUsd = 0n;
|
|
291
|
+
}
|
|
252
292
|
values.collateralDeltaAmount = convertToTokenAmount(values.collateralDeltaUsd, collateralToken.decimals, values.collateralPrice);
|
|
253
|
-
netCollateralOutputAmount =
|
|
254
|
-
|
|
255
|
-
|
|
293
|
+
netCollateralOutputAmount = isMarketDecrease
|
|
294
|
+
? getCollateralDeltaAmountAfterPriceImpactDiff({
|
|
295
|
+
collateralDeltaAmount: values.collateralDeltaAmount,
|
|
296
|
+
priceImpactDiffUsd: values.priceImpactDiffUsd,
|
|
297
|
+
collateralToken,
|
|
298
|
+
})
|
|
299
|
+
: values.collateralDeltaAmount;
|
|
300
|
+
netCollateralOutputUsd =
|
|
301
|
+
netCollateralOutputAmount === values.collateralDeltaAmount
|
|
302
|
+
? values.collateralDeltaUsd
|
|
303
|
+
: convertToUsd(netCollateralOutputAmount, collateralToken.decimals, values.collateralPrice);
|
|
304
|
+
values.receiveTokenAmount = payedInfo.outputAmount + netCollateralOutputAmount;
|
|
256
305
|
}
|
|
257
306
|
else {
|
|
258
307
|
values.collateralDeltaUsd = 0n;
|
|
@@ -263,7 +312,7 @@ export function getDecreasePositionAmounts(p) {
|
|
|
263
312
|
// SwapCollateralTokenToPnlToken → primaryOutput (pnl token), otherwise → secondaryOutput (collateral token)
|
|
264
313
|
const pnlTokenPrice = pnlToken.prices.minPrice;
|
|
265
314
|
const remainingPnlOutputUsd = convertToUsd(payedInfo.outputAmount, collateralToken.decimals, values.collateralPrice) ?? 0n;
|
|
266
|
-
if (values.decreaseSwapType === DecreasePositionSwapType.NoSwap &&
|
|
315
|
+
if ((values.decreaseSwapType === DecreasePositionSwapType.NoSwap || isProfitSwapReverted) &&
|
|
267
316
|
!getIsEquivalentTokens(pnlToken, collateralToken)) {
|
|
268
317
|
values.primaryOutput = {
|
|
269
318
|
tokenAddress: pnlToken.address,
|
|
@@ -276,6 +325,31 @@ export function getDecreasePositionAmounts(p) {
|
|
|
276
325
|
usd: netCollateralOutputUsd,
|
|
277
326
|
};
|
|
278
327
|
}
|
|
328
|
+
else if (isMarketCollateralToPnlSwap) {
|
|
329
|
+
const swapStats = netCollateralOutputUsd > 0n
|
|
330
|
+
? getSwapStats({
|
|
331
|
+
marketInfo,
|
|
332
|
+
tokenInAddress: collateralToken.address,
|
|
333
|
+
tokenOutAddress: pnlToken.address,
|
|
334
|
+
usdIn: netCollateralOutputUsd,
|
|
335
|
+
shouldApplyPriceImpact: true,
|
|
336
|
+
swapPricingType: SwapPricingType.Swap,
|
|
337
|
+
})
|
|
338
|
+
: undefined;
|
|
339
|
+
values.swapProfitFeeUsd = swapStats ? swapStats.swapFeeUsd - swapStats.priceImpactDeltaUsd : 0n;
|
|
340
|
+
values.swapProfitUsdIn = swapStats?.usdIn ?? 0n;
|
|
341
|
+
values.swapUiFeeUsd = applyFactor(values.swapProfitUsdIn, uiFeeFactor);
|
|
342
|
+
const swappedOutputUsd = bigMath.max(netCollateralOutputUsd - values.swapProfitFeeUsd - values.swapUiFeeUsd, 0n);
|
|
343
|
+
values.receiveUsd = remainingPnlOutputUsd + swappedOutputUsd;
|
|
344
|
+
values.receiveTokenAmount = convertToTokenAmount(values.receiveUsd, collateralToken.decimals, values.collateralPrice);
|
|
345
|
+
values.primaryOutput = {
|
|
346
|
+
tokenAddress: pnlToken.address,
|
|
347
|
+
amount: (convertToTokenAmount(remainingPnlOutputUsd, pnlToken.decimals, pnlTokenPrice) ?? 0n) +
|
|
348
|
+
(convertToTokenAmount(swappedOutputUsd, pnlToken.decimals, pnlToken.prices.maxPrice) ?? 0n),
|
|
349
|
+
usd: values.receiveUsd,
|
|
350
|
+
};
|
|
351
|
+
values.secondaryOutput = { tokenAddress: collateralToken.address, amount: 0n, usd: 0n };
|
|
352
|
+
}
|
|
279
353
|
else if (values.decreaseSwapType === DecreasePositionSwapType.SwapCollateralTokenToPnlToken) {
|
|
280
354
|
values.primaryOutput = {
|
|
281
355
|
tokenAddress: pnlToken.address,
|
|
@@ -356,18 +430,95 @@ export function getMinRequiredCollateralUsdForPosition(p) {
|
|
|
356
430
|
return (minRequiredNextCollateralUsd + (position.pendingBorrowingFeesUsd ?? 0n) + (position.pendingFundingFeesUsd ?? 0n));
|
|
357
431
|
}
|
|
358
432
|
export function getMaxWithdrawAmount(p) {
|
|
359
|
-
const { position, minCollateralUsd, collateralPrice, collateralDecimals, userReferralInfo } = p;
|
|
433
|
+
const { position, minCollateralUsd, collateralPrice, collateralDecimals, userReferralInfo, proDiscountFactor } = p;
|
|
360
434
|
const minRequiredCollateralUsd = getMinRequiredCollateralUsdForPosition({
|
|
361
435
|
position,
|
|
362
436
|
minCollateralUsd,
|
|
363
437
|
userReferralInfo,
|
|
364
438
|
});
|
|
365
|
-
|
|
439
|
+
const remainingPositionMarginState = getResultingPositionMarginState({
|
|
440
|
+
marketInfo: position.marketInfo,
|
|
441
|
+
collateralToken: position.collateralToken,
|
|
442
|
+
sizeInUsd: position.sizeInUsd,
|
|
443
|
+
sizeInTokens: position.sizeInTokens,
|
|
444
|
+
collateralAmount: position.remainingCollateralAmount,
|
|
445
|
+
pendingImpactAmount: position.pendingImpactAmount,
|
|
446
|
+
minCollateralUsd: minCollateralUsd ?? 0n,
|
|
447
|
+
isLong: position.isLong,
|
|
448
|
+
userReferralInfo,
|
|
449
|
+
proDiscountFactor,
|
|
450
|
+
shouldValidateMinCollateralUsd: false,
|
|
451
|
+
});
|
|
452
|
+
const maxAllowedLeverage = getMaxAllowedLeverage({
|
|
453
|
+
marketAddress: position.marketInfo.marketTokenAddress,
|
|
454
|
+
minCollateralFactor: position.marketInfo.minCollateralFactor,
|
|
455
|
+
minCollateralFactorForLiquidation: position.marketInfo.minCollateralFactorForLiquidation,
|
|
456
|
+
positionFeeFactorForBalanceWasNotImproved: position.marketInfo.positionFeeFactorForBalanceWasNotImproved,
|
|
457
|
+
});
|
|
458
|
+
if (maxAllowedLeverage <= 0) {
|
|
459
|
+
return 0n;
|
|
460
|
+
}
|
|
461
|
+
const minCollateralUsdForMaxAllowedLeverage = roundUpDivision(position.sizeInUsd * BASIS_POINTS_DIVISOR_BIGINT, BigInt(maxAllowedLeverage));
|
|
462
|
+
const maxWithdrawUsd = bigMath.min(position.collateralUsd - minRequiredCollateralUsd, remainingPositionMarginState.remainingCollateralUsd - remainingPositionMarginState.minCollateralUsdForLeverage, position.remainingCollateralUsd - minCollateralUsdForMaxAllowedLeverage);
|
|
463
|
+
if (maxWithdrawUsd <= 0n) {
|
|
366
464
|
return 0n;
|
|
367
465
|
}
|
|
368
|
-
const maxWithdrawUsd = position.collateralUsd - minRequiredCollateralUsd;
|
|
369
466
|
return convertToTokenAmount(maxWithdrawUsd, collateralDecimals, collateralPrice) ?? 0n;
|
|
370
467
|
}
|
|
468
|
+
function getMarketProfitAmountInCollateralToken(p) {
|
|
469
|
+
const { profitUsd, pnlToken, collateralToken, decreaseSwapType } = p;
|
|
470
|
+
if (getIsEquivalentTokens(pnlToken, collateralToken)) {
|
|
471
|
+
return convertToTokenAmount(profitUsd, collateralToken.decimals, pnlToken.prices.maxPrice) ?? 0n;
|
|
472
|
+
}
|
|
473
|
+
if (decreaseSwapType === DecreasePositionSwapType.SwapPnlTokenToCollateralToken) {
|
|
474
|
+
return (convertToTokenAmount(getPnlTokenProfitValueUsd(profitUsd, pnlToken), collateralToken.decimals, collateralToken.prices.maxPrice) ?? 0n);
|
|
475
|
+
}
|
|
476
|
+
return convertToTokenAmount(profitUsd, collateralToken.decimals, collateralToken.prices.minPrice) ?? 0n;
|
|
477
|
+
}
|
|
478
|
+
function getPnlTokenProfitValueUsd(profitUsd, pnlToken) {
|
|
479
|
+
const profitInPnlToken = convertToTokenAmount(profitUsd, pnlToken.decimals, pnlToken.prices.maxPrice);
|
|
480
|
+
return convertToUsd(profitInPnlToken, pnlToken.decimals, pnlToken.prices.minPrice) ?? 0n;
|
|
481
|
+
}
|
|
482
|
+
export function getIsProfitKeptInPnlToken(p) {
|
|
483
|
+
return (!getIsEquivalentTokens(p.pnlToken, p.collateralToken) &&
|
|
484
|
+
(p.decreaseSwapType !== DecreasePositionSwapType.SwapPnlTokenToCollateralToken || Boolean(p.isProfitSwapReverted)));
|
|
485
|
+
}
|
|
486
|
+
function getMarketInfoAfterProfitPayout(p) {
|
|
487
|
+
const { marketInfo, isLong, profitUsd } = p;
|
|
488
|
+
const pnlToken = isLong ? marketInfo.longToken : marketInfo.shortToken;
|
|
489
|
+
const payoutAmount = convertToTokenAmount(profitUsd, pnlToken.decimals, pnlToken.prices.maxPrice);
|
|
490
|
+
return isLong
|
|
491
|
+
? { ...marketInfo, longPoolAmount: marketInfo.longPoolAmount - payoutAmount }
|
|
492
|
+
: { ...marketInfo, shortPoolAmount: marketInfo.shortPoolAmount - payoutAmount };
|
|
493
|
+
}
|
|
494
|
+
export function getCollateralDeltaAmountAfterPriceImpactDiff(p) {
|
|
495
|
+
const { collateralDeltaAmount, priceImpactDiffUsd, collateralToken } = p;
|
|
496
|
+
if (collateralDeltaAmount <= 0n || priceImpactDiffUsd <= 0n) {
|
|
497
|
+
return collateralDeltaAmount;
|
|
498
|
+
}
|
|
499
|
+
const priceImpactDiffAmount = convertToTokenAmount(priceImpactDiffUsd, collateralToken.decimals, collateralToken.prices.minPrice) ?? 0n;
|
|
500
|
+
return collateralDeltaAmount > priceImpactDiffAmount ? collateralDeltaAmount - priceImpactDiffAmount : 0n;
|
|
501
|
+
}
|
|
502
|
+
function payForCollateralCostBeforePnlTokenProfit(p) {
|
|
503
|
+
const { initialCostUsd, profitUsd, pnlToken, collateralToken, collateralPrice, remainingCollateralAmount } = p;
|
|
504
|
+
const collateralPayedInfo = payForCollateralCost({
|
|
505
|
+
initialCostUsd,
|
|
506
|
+
collateralToken,
|
|
507
|
+
collateralPrice,
|
|
508
|
+
outputAmount: 0n,
|
|
509
|
+
remainingCollateralAmount,
|
|
510
|
+
});
|
|
511
|
+
const costAfterCollateralUsd = convertToUsd(collateralPayedInfo.unpaidCostAmount, collateralToken.decimals, collateralPrice);
|
|
512
|
+
const profitValueUsd = getPnlTokenProfitValueUsd(profitUsd, pnlToken);
|
|
513
|
+
const paidFromProfitUsd = bigMath.min(profitValueUsd, costAfterCollateralUsd);
|
|
514
|
+
return {
|
|
515
|
+
outputAmount: convertToTokenAmount(profitValueUsd - paidFromProfitUsd, collateralToken.decimals, collateralPrice),
|
|
516
|
+
remainingCollateralAmount: collateralPayedInfo.remainingCollateralAmount,
|
|
517
|
+
paidOutputAmount: convertToTokenAmount(paidFromProfitUsd, collateralToken.decimals, collateralPrice),
|
|
518
|
+
paidRemainingCollateralAmount: collateralPayedInfo.paidRemainingCollateralAmount,
|
|
519
|
+
unpaidCostAmount: convertToTokenAmount(costAfterCollateralUsd - paidFromProfitUsd, collateralToken.decimals, collateralPrice),
|
|
520
|
+
};
|
|
521
|
+
}
|
|
371
522
|
export function payForCollateralCost(p) {
|
|
372
523
|
const { initialCostUsd, collateralToken, collateralPrice, outputAmount, remainingCollateralAmount } = p;
|
|
373
524
|
const values = {
|
|
@@ -375,6 +526,7 @@ export function payForCollateralCost(p) {
|
|
|
375
526
|
remainingCollateralAmount: BigInt(remainingCollateralAmount),
|
|
376
527
|
paidOutputAmount: 0n,
|
|
377
528
|
paidRemainingCollateralAmount: 0n,
|
|
529
|
+
unpaidCostAmount: 0n,
|
|
378
530
|
};
|
|
379
531
|
let remainingCostAmount = convertToTokenAmount(initialCostUsd, collateralToken.decimals, collateralPrice);
|
|
380
532
|
if (remainingCostAmount == 0n) {
|
|
@@ -405,6 +557,7 @@ export function payForCollateralCost(p) {
|
|
|
405
557
|
values.paidRemainingCollateralAmount = values.remainingCollateralAmount;
|
|
406
558
|
values.remainingCollateralAmount = 0n;
|
|
407
559
|
}
|
|
560
|
+
values.unpaidCostAmount = remainingCostAmount;
|
|
408
561
|
return values;
|
|
409
562
|
}
|
|
410
563
|
function applyAcceptablePrice(p) {
|
|
@@ -487,17 +640,25 @@ export function getTotalFeesUsdForDecrease({ positionFeeUsd, borrowingFeeUsd, fu
|
|
|
487
640
|
return totalFeesUsd;
|
|
488
641
|
}
|
|
489
642
|
export function getNextPositionValuesForDecreaseTrade(p) {
|
|
490
|
-
const { existingPosition, marketInfo, collateralToken, sizeDeltaUsd, sizeDeltaInTokens, realizedPnl, estimatedPnl, collateralDeltaUsd, collateralDeltaAmount, payedRemainingCollateralUsd, payedRemainingCollateralAmount, showPnlInLeverage, proportionalPendingImpactDeltaUsd, isLong, minCollateralUsd, userReferralInfo, } = p;
|
|
643
|
+
const { existingPosition, marketInfo, collateralToken, sizeDeltaUsd, sizeDeltaInTokens, realizedPnl, estimatedPnl, collateralDeltaUsd, collateralDeltaAmount, payedRemainingCollateralUsd, payedRemainingCollateralAmount, showPnlInLeverage, proportionalPendingImpactDeltaUsd, isLong, minCollateralUsd, userReferralInfo, priceImpactDiffUsd, } = p;
|
|
644
|
+
const executedCollateralDeltaAmount = getCollateralDeltaAmountAfterPriceImpactDiff({
|
|
645
|
+
collateralDeltaAmount,
|
|
646
|
+
priceImpactDiffUsd: priceImpactDiffUsd ?? 0n,
|
|
647
|
+
collateralToken,
|
|
648
|
+
});
|
|
649
|
+
const executedCollateralDeltaUsd = executedCollateralDeltaAmount === collateralDeltaAmount
|
|
650
|
+
? collateralDeltaUsd
|
|
651
|
+
: convertToUsd(executedCollateralDeltaAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
491
652
|
const nextSizeUsd = existingPosition ? existingPosition.sizeInUsd - sizeDeltaUsd : 0n;
|
|
492
653
|
const nextSizeInTokens = existingPosition ? existingPosition.sizeInTokens - sizeDeltaInTokens : 0n;
|
|
493
654
|
let nextCollateralUsd = existingPosition
|
|
494
|
-
? existingPosition.collateralUsd -
|
|
655
|
+
? existingPosition.collateralUsd - executedCollateralDeltaUsd - payedRemainingCollateralUsd
|
|
495
656
|
: 0n;
|
|
496
657
|
if (nextCollateralUsd < 0) {
|
|
497
658
|
nextCollateralUsd = 0n;
|
|
498
659
|
}
|
|
499
660
|
let nextCollateralAmount = existingPosition
|
|
500
|
-
? existingPosition.collateralAmount -
|
|
661
|
+
? existingPosition.collateralAmount - executedCollateralDeltaAmount - payedRemainingCollateralAmount
|
|
501
662
|
: 0n;
|
|
502
663
|
if (nextCollateralAmount < 0) {
|
|
503
664
|
nextCollateralAmount = 0n;
|
|
@@ -565,7 +726,7 @@ export function selectBetterDecreaseSwapType(p) {
|
|
|
565
726
|
return p.pathBUsdOut > p.pathAUsdOut ? "pathB" : "pathA";
|
|
566
727
|
}
|
|
567
728
|
export function getOptimalDecreaseAndSwapAmounts(p) {
|
|
568
|
-
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType, isSetAcceptablePriceImpactEnabled, receiveToken, forceDecreaseSwapType, findSwapPath, findSwapPathFromPnl, marketsInfoData, chainId, } = p;
|
|
729
|
+
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, proDiscountFactor, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType, isSetAcceptablePriceImpactEnabled, receiveToken, forceDecreaseSwapType, isTwap, findSwapPath, findSwapPathFromPnl, marketsInfoData, chainId, } = p;
|
|
569
730
|
const decreaseBaseParams = {
|
|
570
731
|
marketInfo,
|
|
571
732
|
collateralToken,
|
|
@@ -577,12 +738,14 @@ export function getOptimalDecreaseAndSwapAmounts(p) {
|
|
|
577
738
|
fixedAcceptablePriceImpactBps,
|
|
578
739
|
acceptablePriceImpactBuffer,
|
|
579
740
|
userReferralInfo,
|
|
741
|
+
proDiscountFactor,
|
|
580
742
|
minCollateralUsd,
|
|
581
743
|
minPositionSizeUsd,
|
|
582
744
|
uiFeeFactor,
|
|
583
745
|
triggerOrderType,
|
|
584
746
|
isSetAcceptablePriceImpactEnabled,
|
|
585
747
|
receiveToken,
|
|
748
|
+
isTwap,
|
|
586
749
|
};
|
|
587
750
|
if (forceDecreaseSwapType !== undefined) {
|
|
588
751
|
return {
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { bigMath } from "../bigmath";
|
|
2
|
+
import { getProportionalPendingImpactValues } from "../fees";
|
|
3
|
+
import { getMarketInfoWithOpenInterestDelta, getOpenInterestUsd, getPriceForPnl } from "../markets";
|
|
4
|
+
import { applyFactor } from "../numbers";
|
|
5
|
+
import { getPositionPnlUsd } from "../positions";
|
|
6
|
+
import { convertToTokenAmount, convertToUsd } from "../tokens";
|
|
7
|
+
import { getCollateralDeltaAmountAfterPriceImpactDiff, getIsProfitKeptInPnlToken } from "./decrease";
|
|
8
|
+
import { getMarketInfoAtIndexPrice, getResultingPositionMarginState, PositionMarginFailureReason, } from "./increaseMarginCheck";
|
|
9
|
+
/**
|
|
10
|
+
* Mirrors the gates of `DecreasePositionUtils.decreasePosition` from the gmx-synthetics build deployed on
|
|
11
|
+
* Arbitrum (release_2.2.1 @ 23c9d160) for a market decrease, a trigger decrease at `indexPriceForEvaluation`
|
|
12
|
+
* or a collateral withdrawal that leaves a position open.
|
|
13
|
+
*/
|
|
14
|
+
export function getDecreaseResultingPositionMarginState(p) {
|
|
15
|
+
const { isLong, position, sizeDeltaUsd, sizeDeltaInTokens, payedRemainingCollateralAmount, priceImpactDiffUsd, minCollateralUsd, minPositionSizeUsd, userReferralInfo, proDiscountFactor, } = p;
|
|
16
|
+
const { marketInfo, collateralToken } = getMarketInfoAtIndexPrice({
|
|
17
|
+
marketInfo: p.marketInfo,
|
|
18
|
+
collateralToken: p.collateralToken,
|
|
19
|
+
indexPrice: p.indexPriceForEvaluation,
|
|
20
|
+
});
|
|
21
|
+
const { indexToken } = marketInfo;
|
|
22
|
+
if (position.sizeInUsd <= 0n || position.sizeInTokens <= 0n || sizeDeltaUsd < 0n) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
if (sizeDeltaUsd >= position.sizeInUsd) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
if (indexToken.prices.minPrice <= 0n || indexToken.prices.maxPrice <= 0n || collateralToken.prices.minPrice <= 0n) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
let collateralDeltaAmount = bigMath.min(p.collateralDeltaAmount, position.collateralAmount);
|
|
32
|
+
if (collateralDeltaAmount < 0n) {
|
|
33
|
+
collateralDeltaAmount = 0n;
|
|
34
|
+
}
|
|
35
|
+
const estimatedPositionPnlUsd = getPositionPnlUsd({
|
|
36
|
+
marketInfo,
|
|
37
|
+
sizeInUsd: position.sizeInUsd,
|
|
38
|
+
sizeInTokens: position.sizeInTokens,
|
|
39
|
+
markPrice: getPriceForPnl(indexToken.prices, isLong, false),
|
|
40
|
+
isLong,
|
|
41
|
+
});
|
|
42
|
+
const estimatedRealizedPnlUsd = bigMath.mulDiv(estimatedPositionPnlUsd, sizeDeltaUsd, position.sizeInUsd);
|
|
43
|
+
const estimatedRemainingPnlUsd = estimatedPositionPnlUsd - estimatedRealizedPnlUsd;
|
|
44
|
+
const nextSizeInUsd = position.sizeInUsd - sizeDeltaUsd;
|
|
45
|
+
let estimatedRemainingCollateralUsd = convertToUsd(position.collateralAmount - collateralDeltaAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
46
|
+
if (estimatedRealizedPnlUsd < 0n) {
|
|
47
|
+
estimatedRemainingCollateralUsd += estimatedRealizedPnlUsd;
|
|
48
|
+
}
|
|
49
|
+
const minCollateralFactorMultiplier = isLong
|
|
50
|
+
? marketInfo.minCollateralFactorForOpenInterestLong
|
|
51
|
+
: marketInfo.minCollateralFactorForOpenInterestShort;
|
|
52
|
+
let minCollateralFactorForOpenInterest = applyFactor(getOpenInterestUsd(marketInfo, isLong) - sizeDeltaUsd, minCollateralFactorMultiplier);
|
|
53
|
+
if (marketInfo.minCollateralFactor > minCollateralFactorForOpenInterest) {
|
|
54
|
+
minCollateralFactorForOpenInterest = marketInfo.minCollateralFactor;
|
|
55
|
+
}
|
|
56
|
+
const minCollateralUsdForOpenInterest = applyFactor(nextSizeInUsd, minCollateralFactorForOpenInterest);
|
|
57
|
+
const willCollateralBeSufficient = estimatedRemainingCollateralUsd >= 0n && estimatedRemainingCollateralUsd >= minCollateralUsdForOpenInterest;
|
|
58
|
+
let isCollateralWithdrawalCancelled = false;
|
|
59
|
+
if (!willCollateralBeSufficient || estimatedRemainingCollateralUsd + estimatedRemainingPnlUsd < minCollateralUsd) {
|
|
60
|
+
if (sizeDeltaUsd === 0n) {
|
|
61
|
+
return {
|
|
62
|
+
isLiquidatable: true,
|
|
63
|
+
reason: PositionMarginFailureReason.UnableToWithdrawCollateral,
|
|
64
|
+
remainingCollateralUsd: estimatedRemainingCollateralUsd,
|
|
65
|
+
minCollateralUsd,
|
|
66
|
+
minCollateralUsdForLeverage: minCollateralUsdForOpenInterest,
|
|
67
|
+
isCollateralWithdrawalCancelled: false,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
isCollateralWithdrawalCancelled = collateralDeltaAmount > 0n;
|
|
71
|
+
estimatedRemainingCollateralUsd += convertToUsd(collateralDeltaAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
72
|
+
collateralDeltaAmount = 0n;
|
|
73
|
+
}
|
|
74
|
+
if (estimatedRemainingCollateralUsd + estimatedRemainingPnlUsd < minCollateralUsd) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
if (nextSizeInUsd < minPositionSizeUsd) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
const { proportionalPendingImpactDeltaAmount } = getProportionalPendingImpactValues({
|
|
81
|
+
sizeInUsd: position.sizeInUsd,
|
|
82
|
+
pendingImpactAmount: position.pendingImpactAmount,
|
|
83
|
+
sizeDeltaUsd,
|
|
84
|
+
indexToken,
|
|
85
|
+
});
|
|
86
|
+
const collateralAfterCostsAmount = position.collateralAmount - payedRemainingCollateralAmount;
|
|
87
|
+
collateralDeltaAmount = getCollateralDeltaAmountAfterPriceImpactDiff({
|
|
88
|
+
collateralDeltaAmount,
|
|
89
|
+
priceImpactDiffUsd,
|
|
90
|
+
collateralToken,
|
|
91
|
+
});
|
|
92
|
+
if (collateralDeltaAmount > collateralAfterCostsAmount) {
|
|
93
|
+
collateralDeltaAmount = collateralAfterCostsAmount > 0n ? collateralAfterCostsAmount : 0n;
|
|
94
|
+
}
|
|
95
|
+
const nextCollateralAmount = collateralAfterCostsAmount - collateralDeltaAmount;
|
|
96
|
+
const marginState = getResultingPositionMarginState({
|
|
97
|
+
marketInfo: getMarketInfoWithOpenInterestDelta({
|
|
98
|
+
marketInfo,
|
|
99
|
+
collateralToken,
|
|
100
|
+
isLong,
|
|
101
|
+
sizeDeltaUsd: -sizeDeltaUsd,
|
|
102
|
+
sizeDeltaInTokens: -sizeDeltaInTokens,
|
|
103
|
+
}),
|
|
104
|
+
collateralToken,
|
|
105
|
+
sizeInUsd: nextSizeInUsd,
|
|
106
|
+
sizeInTokens: position.sizeInTokens - sizeDeltaInTokens,
|
|
107
|
+
collateralAmount: nextCollateralAmount < 0n ? 0n : nextCollateralAmount,
|
|
108
|
+
pendingImpactAmount: position.pendingImpactAmount - proportionalPendingImpactDeltaAmount,
|
|
109
|
+
minCollateralUsd,
|
|
110
|
+
isLong,
|
|
111
|
+
userReferralInfo,
|
|
112
|
+
proDiscountFactor,
|
|
113
|
+
shouldValidateMinCollateralUsd: false,
|
|
114
|
+
});
|
|
115
|
+
return { ...marginState, isCollateralWithdrawalCancelled };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The same gates for a Take-Profit or Stop-Loss at its trigger price: `decreaseAmounts` are its amounts at that
|
|
119
|
+
* price and `collateralDeltaAmount` is the collateral withdrawal the order stores.
|
|
120
|
+
*/
|
|
121
|
+
export function getTriggerDecreaseRemainingPositionMarginState(p) {
|
|
122
|
+
const { marketInfo, collateralToken, isLong, position, decreaseAmounts } = p;
|
|
123
|
+
const { triggerPrice } = decreaseAmounts;
|
|
124
|
+
if (decreaseAmounts.isFullClose || triggerPrice === undefined || triggerPrice <= 0n) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
const isProfitKeptInPnlToken = getIsProfitKeptInPnlToken({
|
|
128
|
+
pnlToken: isLong ? marketInfo.longToken : marketInfo.shortToken,
|
|
129
|
+
collateralToken,
|
|
130
|
+
decreaseSwapType: decreaseAmounts.decreaseSwapType,
|
|
131
|
+
});
|
|
132
|
+
const costsPaidFromProfitAmount = isProfitKeptInPnlToken
|
|
133
|
+
? convertToTokenAmount(decreaseAmounts.payedOutputUsd, collateralToken.decimals, decreaseAmounts.collateralPrice) ??
|
|
134
|
+
0n
|
|
135
|
+
: 0n;
|
|
136
|
+
return getDecreaseResultingPositionMarginState({
|
|
137
|
+
marketInfo,
|
|
138
|
+
collateralToken,
|
|
139
|
+
isLong,
|
|
140
|
+
position,
|
|
141
|
+
sizeDeltaUsd: decreaseAmounts.sizeDeltaUsd,
|
|
142
|
+
sizeDeltaInTokens: decreaseAmounts.sizeDeltaInTokens,
|
|
143
|
+
collateralDeltaAmount: p.collateralDeltaAmount,
|
|
144
|
+
payedRemainingCollateralAmount: bigMath.min(decreaseAmounts.payedRemainingCollateralAmount + costsPaidFromProfitAmount, position.collateralAmount),
|
|
145
|
+
priceImpactDiffUsd: decreaseAmounts.priceImpactDiffUsd,
|
|
146
|
+
minCollateralUsd: p.minCollateralUsd,
|
|
147
|
+
minPositionSizeUsd: p.minPositionSizeUsd,
|
|
148
|
+
userReferralInfo: p.userReferralInfo,
|
|
149
|
+
proDiscountFactor: p.proDiscountFactor,
|
|
150
|
+
indexPriceForEvaluation: triggerPrice,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
@@ -9,6 +9,7 @@ export var PositionMarginFailureReason;
|
|
|
9
9
|
PositionMarginFailureReason["NonPositiveRemainingMargin"] = "< 0";
|
|
10
10
|
PositionMarginFailureReason["MinCollateralForLeverage"] = "min collateral for leverage";
|
|
11
11
|
PositionMarginFailureReason["InsufficientCollateralUsd"] = "insufficient collateral usd";
|
|
12
|
+
PositionMarginFailureReason["UnableToWithdrawCollateral"] = "unable to withdraw collateral";
|
|
12
13
|
PositionMarginFailureReason["MaxAllowedLeverage"] = "max allowed leverage";
|
|
13
14
|
})(PositionMarginFailureReason || (PositionMarginFailureReason = {}));
|
|
14
15
|
export function getIsMaxLeverageMarginReason(reason) {
|
|
@@ -20,10 +21,10 @@ export function getIsMaxLeverageMarginReason(reason) {
|
|
|
20
21
|
* Port of `PositionUtils.isPositionLiquidatable` from the gmx-synthetics build deployed on Arbitrum
|
|
21
22
|
* (release_2.2.1 @ 23c9d160: release-v2.2c plus MarketUtils.getPositivePnl in the per-position pnl cap)
|
|
22
23
|
* for a position whose pending fees have just been settled — the state the contract validates right
|
|
23
|
-
* after an increase.
|
|
24
|
+
* after an increase or a decrease.
|
|
24
25
|
*/
|
|
25
26
|
export function getResultingPositionMarginState(p) {
|
|
26
|
-
const { marketInfo, collateralToken, sizeInUsd, sizeInTokens, collateralAmount, pendingImpactAmount, minCollateralUsd, isLong, userReferralInfo, proDiscountFactor, } = p;
|
|
27
|
+
const { marketInfo, collateralToken, sizeInUsd, sizeInTokens, collateralAmount, pendingImpactAmount, minCollateralUsd, isLong, userReferralInfo, proDiscountFactor, shouldValidateMinCollateralUsd = true, } = p;
|
|
27
28
|
const { indexToken } = marketInfo;
|
|
28
29
|
const positionPnlUsd = getPositionPnlUsd({
|
|
29
30
|
marketInfo,
|
|
@@ -76,7 +77,7 @@ export function getResultingPositionMarginState(p) {
|
|
|
76
77
|
minCollateralUsd,
|
|
77
78
|
minCollateralUsdForLeverage,
|
|
78
79
|
};
|
|
79
|
-
if (remainingCollateralUsd < minCollateralUsd) {
|
|
80
|
+
if (shouldValidateMinCollateralUsd && remainingCollateralUsd < minCollateralUsd) {
|
|
80
81
|
return { ...state, isLiquidatable: true, reason: PositionMarginFailureReason.MinCollateral };
|
|
81
82
|
}
|
|
82
83
|
if (remainingCollateralUsd <= 0) {
|
|
@@ -93,6 +94,22 @@ function withPriceOverride(token, indexToken, price) {
|
|
|
93
94
|
}
|
|
94
95
|
return { ...token, prices: { minPrice: price, maxPrice: price } };
|
|
95
96
|
}
|
|
97
|
+
export function getMarketInfoAtIndexPrice(p) {
|
|
98
|
+
const { marketInfo, collateralToken, indexPrice } = p;
|
|
99
|
+
if (indexPrice === undefined || indexPrice <= 0n) {
|
|
100
|
+
return { marketInfo, collateralToken };
|
|
101
|
+
}
|
|
102
|
+
const { indexToken } = marketInfo;
|
|
103
|
+
return {
|
|
104
|
+
marketInfo: {
|
|
105
|
+
...marketInfo,
|
|
106
|
+
indexToken: withPriceOverride(indexToken, indexToken, indexPrice),
|
|
107
|
+
longToken: withPriceOverride(marketInfo.longToken, indexToken, indexPrice),
|
|
108
|
+
shortToken: withPriceOverride(marketInfo.shortToken, indexToken, indexPrice),
|
|
109
|
+
},
|
|
110
|
+
collateralToken: withPriceOverride(collateralToken, indexToken, indexPrice),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
96
113
|
/**
|
|
97
114
|
* Mirrors the post-increase gates of `IncreasePositionUtils.increasePosition` from the gmx-synthetics
|
|
98
115
|
* build deployed on Arbitrum (release_2.2.1 @ 23c9d160: release-v2.2c plus MarketUtils.getPositivePnl
|
|
@@ -104,20 +121,14 @@ export function getIncreaseResultingPositionMarginState(p) {
|
|
|
104
121
|
}
|
|
105
122
|
export function getIncreaseResultingPositionState(p) {
|
|
106
123
|
const { isLong, existingPosition, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaAmount, minCollateralUsd, userReferralInfo, proDiscountFactor, indexPriceForEvaluation, } = p;
|
|
107
|
-
let { marketInfo, collateralToken } = p;
|
|
108
124
|
if (sizeDeltaUsd <= 0n || sizeDeltaInTokens <= 0n) {
|
|
109
125
|
return undefined;
|
|
110
126
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
longToken: withPriceOverride(marketInfo.longToken, originalIndexToken, indexPriceForEvaluation),
|
|
117
|
-
shortToken: withPriceOverride(marketInfo.shortToken, originalIndexToken, indexPriceForEvaluation),
|
|
118
|
-
};
|
|
119
|
-
collateralToken = withPriceOverride(collateralToken, originalIndexToken, indexPriceForEvaluation);
|
|
120
|
-
}
|
|
127
|
+
const { marketInfo, collateralToken } = getMarketInfoAtIndexPrice({
|
|
128
|
+
marketInfo: p.marketInfo,
|
|
129
|
+
collateralToken: p.collateralToken,
|
|
130
|
+
indexPrice: indexPriceForEvaluation,
|
|
131
|
+
});
|
|
121
132
|
const { indexToken } = marketInfo;
|
|
122
133
|
if (indexToken.prices.minPrice <= 0n || indexToken.prices.maxPrice <= 0n || collateralToken.prices.minPrice <= 0n) {
|
|
123
134
|
return undefined;
|