@gmx-io/sdk 2.2.1 → 2.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +32 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +272 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +60 -0
- package/build/cjs/src/configs/markets.js +28 -0
- package/build/cjs/src/test/mock.js +32 -4
- package/build/cjs/src/utils/markets/utils.js +45 -1
- package/build/cjs/src/utils/trade/__tests__/increaseMarginCheck.spec.js +14 -35
- package/build/cjs/src/utils/trade/__tests__/twapIncreaseMarginCheck.spec.js +368 -0
- package/build/cjs/src/utils/trade/increaseMarginCheck.js +27 -13
- package/build/cjs/src/utils/trade/twapIncreaseMarginCheck.js +180 -0
- package/build/cjs/src/utils/twap/index.js +13 -4
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +32 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +272 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +60 -0
- package/build/esm/src/configs/markets.js +28 -0
- package/build/esm/src/test/mock.js +27 -0
- package/build/esm/src/utils/markets/utils.js +44 -1
- package/build/esm/src/utils/trade/__tests__/increaseMarginCheck.spec.js +15 -36
- package/build/esm/src/utils/trade/__tests__/twapIncreaseMarginCheck.spec.js +366 -0
- package/build/esm/src/utils/trade/increaseMarginCheck.js +24 -11
- package/build/esm/src/utils/trade/twapIncreaseMarginCheck.js +172 -0
- package/build/esm/src/utils/twap/index.js +10 -3
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/codegen/prebuilt/index.d.ts +364 -0
- package/build/types/src/test/mock.d.ts +5 -0
- package/build/types/src/utils/markets/utils.d.ts +3 -0
- package/build/types/src/utils/trade/__tests__/twapIncreaseMarginCheck.spec.d.ts +1 -0
- package/build/types/src/utils/trade/increaseMarginCheck.d.ts +15 -7
- package/build/types/src/utils/trade/twapIncreaseMarginCheck.d.ts +50 -0
- package/build/types/src/utils/twap/index.d.ts +2 -0
- package/package.json +10 -1
|
@@ -9,10 +9,12 @@ 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["MaxAllowedLeverage"] = "max allowed leverage";
|
|
12
13
|
})(PositionMarginFailureReason || (PositionMarginFailureReason = {}));
|
|
13
14
|
export function getIsMaxLeverageMarginReason(reason) {
|
|
14
15
|
return (reason === PositionMarginFailureReason.MinCollateralForLeverage ||
|
|
15
|
-
reason === PositionMarginFailureReason.InsufficientCollateralUsd
|
|
16
|
+
reason === PositionMarginFailureReason.InsufficientCollateralUsd ||
|
|
17
|
+
reason === PositionMarginFailureReason.MaxAllowedLeverage);
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
18
20
|
* Port of `PositionUtils.isPositionLiquidatable` from the gmx-synthetics build deployed on Arbitrum
|
|
@@ -98,6 +100,9 @@ function withPriceOverride(token, indexToken, price) {
|
|
|
98
100
|
* then `validatePosition` on the resulting position.
|
|
99
101
|
*/
|
|
100
102
|
export function getIncreaseResultingPositionMarginState(p) {
|
|
103
|
+
return getIncreaseResultingPositionState(p)?.marginState;
|
|
104
|
+
}
|
|
105
|
+
export function getIncreaseResultingPositionState(p) {
|
|
101
106
|
const { isLong, existingPosition, sizeDeltaUsd, sizeDeltaInTokens, collateralDeltaAmount, minCollateralUsd, userReferralInfo, proDiscountFactor, indexPriceForEvaluation, } = p;
|
|
102
107
|
let { marketInfo, collateralToken } = p;
|
|
103
108
|
if (sizeDeltaUsd <= 0n || sizeDeltaInTokens <= 0n) {
|
|
@@ -149,25 +154,33 @@ export function getIncreaseResultingPositionMarginState(p) {
|
|
|
149
154
|
minCollateralFactorForOpenInterest = nextMarketInfo.minCollateralFactor;
|
|
150
155
|
}
|
|
151
156
|
const minCollateralUsdForOpenInterest = applyFactor(nextSizeInUsd, minCollateralFactorForOpenInterest);
|
|
157
|
+
const position = {
|
|
158
|
+
sizeInUsd: nextSizeInUsd,
|
|
159
|
+
sizeInTokens: nextSizeInTokens,
|
|
160
|
+
collateralAmount: nextCollateralAmountClamped,
|
|
161
|
+
pendingImpactAmount: nextPendingImpactAmount,
|
|
162
|
+
};
|
|
152
163
|
if (remainingCollateralUsdForOpenInterest < minCollateralUsdForOpenInterest) {
|
|
153
164
|
return {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
165
|
+
marginState: {
|
|
166
|
+
isLiquidatable: true,
|
|
167
|
+
reason: PositionMarginFailureReason.InsufficientCollateralUsd,
|
|
168
|
+
remainingCollateralUsd: remainingCollateralUsdForOpenInterest,
|
|
169
|
+
minCollateralUsd,
|
|
170
|
+
minCollateralUsdForLeverage: minCollateralUsdForOpenInterest,
|
|
171
|
+
},
|
|
172
|
+
position,
|
|
173
|
+
marketInfo: nextMarketInfo,
|
|
159
174
|
};
|
|
160
175
|
}
|
|
161
|
-
|
|
176
|
+
const marginState = getResultingPositionMarginState({
|
|
162
177
|
marketInfo: nextMarketInfo,
|
|
163
178
|
collateralToken,
|
|
164
|
-
|
|
165
|
-
sizeInTokens: nextSizeInTokens,
|
|
166
|
-
collateralAmount: nextCollateralAmountClamped,
|
|
167
|
-
pendingImpactAmount: nextPendingImpactAmount,
|
|
179
|
+
...position,
|
|
168
180
|
minCollateralUsd,
|
|
169
181
|
isLong,
|
|
170
182
|
userReferralInfo,
|
|
171
183
|
proDiscountFactor,
|
|
172
184
|
});
|
|
185
|
+
return { marginState, position, marketInfo: nextMarketInfo };
|
|
173
186
|
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { BASIS_POINTS_DIVISOR_BIGINT } from "../../configs/factors";
|
|
2
|
+
import { bigMath } from "../bigmath";
|
|
3
|
+
import { getBorrowingFeeRateUsd, getFundingFeeRateUsd, getPositionFee, getPriceImpactForPosition, getTotalSwapVolumeFromSwapStats, } from "../fees";
|
|
4
|
+
import { getMarketInfoWithSwapDelta, getMaxAllowedLeverage } from "../markets";
|
|
5
|
+
import { applyFactor } from "../numbers";
|
|
6
|
+
import { SwapPricingType } from "../orders/types";
|
|
7
|
+
import { getLeverage } from "../positions";
|
|
8
|
+
import { getMarkPrice } from "../prices";
|
|
9
|
+
import { getSwapPathStats } from "../swap/swapStats";
|
|
10
|
+
import { convertToTokenAmount, convertToTokenAmountForIncrease, convertToUsd } from "../tokens";
|
|
11
|
+
import { getIncreaseResultingPositionState, PositionMarginFailureReason, } from "./increaseMarginCheck";
|
|
12
|
+
export function getTwapIncreaseAggregateAmounts(increaseAmounts) {
|
|
13
|
+
return {
|
|
14
|
+
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
|
|
15
|
+
pendingFeesUsd: increaseAmounts.borrowingFeeUsd + increaseAmounts.fundingFeeUsd,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function getTwapIncreasePartAmounts({ sizeDeltaUsd, pendingFeesUsd }, numberOfParts) {
|
|
19
|
+
if (numberOfParts < 1) {
|
|
20
|
+
return { partSizeDeltaUsd: 0n, pendingFeesUsd };
|
|
21
|
+
}
|
|
22
|
+
return { partSizeDeltaUsd: sizeDeltaUsd / BigInt(numberOfParts), pendingFeesUsd };
|
|
23
|
+
}
|
|
24
|
+
export function getTwapIncreasePartsGrossCollateralUsd(p) {
|
|
25
|
+
const { swapPath, initialCollateralToken, collateralToken, initialCollateralAmountPerPart, numberOfParts, uiFeeFactor, wrappedNativeTokenAddress, } = p;
|
|
26
|
+
if (numberOfParts < 1) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
const usdIn = convertToUsd(initialCollateralAmountPerPart, initialCollateralToken.decimals, initialCollateralToken.prices.minPrice);
|
|
30
|
+
if (swapPath.length === 0) {
|
|
31
|
+
return Array.from({ length: numberOfParts }, () => usdIn);
|
|
32
|
+
}
|
|
33
|
+
let marketsInfoData = p.marketsInfoData;
|
|
34
|
+
const partGrossCollateralUsds = [];
|
|
35
|
+
for (let partIndex = 0; partIndex < numberOfParts; partIndex++) {
|
|
36
|
+
const swapPathStats = getSwapPathStats({
|
|
37
|
+
marketsInfoData,
|
|
38
|
+
swapPath,
|
|
39
|
+
initialCollateralAddress: initialCollateralToken.address,
|
|
40
|
+
wrappedNativeTokenAddress,
|
|
41
|
+
usdIn,
|
|
42
|
+
shouldUnwrapNativeToken: false,
|
|
43
|
+
shouldApplyPriceImpact: true,
|
|
44
|
+
swapPricingType: SwapPricingType.Swap,
|
|
45
|
+
});
|
|
46
|
+
if (!swapPathStats || swapPathStats.amountOut <= 0n) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const grossCollateralUsd = convertToUsd(swapPathStats.amountOut, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
50
|
+
const swapUiFeeUsd = applyFactor(getTotalSwapVolumeFromSwapStats(swapPathStats.swapSteps), uiFeeFactor);
|
|
51
|
+
partGrossCollateralUsds.push(grossCollateralUsd - swapUiFeeUsd);
|
|
52
|
+
marketsInfoData = swapPathStats.swapSteps.reduce((acc, swapStep) => ({
|
|
53
|
+
...acc,
|
|
54
|
+
[swapStep.marketAddress]: getMarketInfoWithSwapDelta(acc[swapStep.marketAddress], swapStep),
|
|
55
|
+
}), marketsInfoData);
|
|
56
|
+
}
|
|
57
|
+
return partGrossCollateralUsds;
|
|
58
|
+
}
|
|
59
|
+
function getMaxAllowedLeverageMarginState({ position, collateralToken, maxAllowedLeverage, minCollateralUsd, }) {
|
|
60
|
+
const collateralUsd = convertToUsd(position.collateralAmount, collateralToken.decimals, collateralToken.prices.minPrice);
|
|
61
|
+
const leverage = getLeverage({
|
|
62
|
+
sizeInUsd: position.sizeInUsd,
|
|
63
|
+
collateralUsd,
|
|
64
|
+
pnl: undefined,
|
|
65
|
+
pendingBorrowingFeesUsd: 0n,
|
|
66
|
+
pendingFundingFeesUsd: 0n,
|
|
67
|
+
});
|
|
68
|
+
if (maxAllowedLeverage <= 0n || leverage === undefined || leverage <= maxAllowedLeverage) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
isLiquidatable: true,
|
|
73
|
+
reason: PositionMarginFailureReason.MaxAllowedLeverage,
|
|
74
|
+
remainingCollateralUsd: collateralUsd,
|
|
75
|
+
minCollateralUsd,
|
|
76
|
+
minCollateralUsdForLeverage: bigMath.mulDiv(position.sizeInUsd, BASIS_POINTS_DIVISOR_BIGINT, maxAllowedLeverage),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function getPositionFeesAccruedUsd(marketInfo, isLong, sizeInUsd, periodSeconds) {
|
|
80
|
+
if (periodSeconds <= 0 || sizeInUsd <= 0n) {
|
|
81
|
+
return 0n;
|
|
82
|
+
}
|
|
83
|
+
const period = BigInt(periodSeconds);
|
|
84
|
+
const borrowingUsd = getBorrowingFeeRateUsd(marketInfo, isLong, sizeInUsd, period);
|
|
85
|
+
const fundingUsd = getFundingFeeRateUsd(marketInfo, isLong, sizeInUsd, period);
|
|
86
|
+
return borrowingUsd + (fundingUsd < 0n ? -fundingUsd : 0n);
|
|
87
|
+
}
|
|
88
|
+
export function getTwapIncreaseSequentialMarginState(p) {
|
|
89
|
+
const { collateralToken, isLong, numberOfParts, partDelaysSeconds, partGrossCollateralUsds, partSizeDeltaUsd, pendingFeesUsd, uiFeeFactor, minCollateralUsd, userReferralInfo, proDiscountFactor, } = p;
|
|
90
|
+
if (numberOfParts < 1 || partSizeDeltaUsd <= 0n || partGrossCollateralUsds.length < numberOfParts) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
const { indexToken } = p.marketInfo;
|
|
94
|
+
const indexPrice = getMarkPrice({ prices: indexToken.prices, isIncrease: true, isLong });
|
|
95
|
+
const collateralPrice = collateralToken.prices.minPrice;
|
|
96
|
+
if (indexPrice <= 0n || collateralPrice <= 0n) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
const partSizeDeltaInTokens = convertToTokenAmountForIncrease(partSizeDeltaUsd, indexToken.decimals, indexPrice, isLong);
|
|
100
|
+
if (partSizeDeltaInTokens <= 0n) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
const maxAllowedLeverage = BigInt(getMaxAllowedLeverage({
|
|
104
|
+
marketAddress: p.marketInfo.marketTokenAddress,
|
|
105
|
+
minCollateralFactor: p.marketInfo.minCollateralFactor,
|
|
106
|
+
minCollateralFactorForLiquidation: p.marketInfo.minCollateralFactorForLiquidation,
|
|
107
|
+
positionFeeFactorForBalanceWasNotImproved: p.marketInfo.positionFeeFactorForBalanceWasNotImproved,
|
|
108
|
+
}));
|
|
109
|
+
let marketInfo = p.marketInfo;
|
|
110
|
+
let position = p.existingPosition;
|
|
111
|
+
let contractMarginState;
|
|
112
|
+
let previousPartDelaySeconds = 0;
|
|
113
|
+
for (let partIndex = 0; partIndex < numberOfParts; partIndex++) {
|
|
114
|
+
const partDelaySeconds = Math.max(0, partDelaysSeconds[partIndex] ?? 0);
|
|
115
|
+
const accruedFeesUsd = position
|
|
116
|
+
? getPositionFeesAccruedUsd(marketInfo, isLong, position.sizeInUsd, partDelaySeconds - previousPartDelaySeconds)
|
|
117
|
+
: 0n;
|
|
118
|
+
previousPartDelaySeconds = partDelaySeconds;
|
|
119
|
+
const { balanceWasImproved } = getPriceImpactForPosition(marketInfo, partSizeDeltaUsd, isLong, {
|
|
120
|
+
fallbackToZero: true,
|
|
121
|
+
sizeDeltaInTokens: partSizeDeltaInTokens,
|
|
122
|
+
});
|
|
123
|
+
const { positionFeeUsd } = getPositionFee(marketInfo, partSizeDeltaUsd, balanceWasImproved, userReferralInfo, undefined, proDiscountFactor);
|
|
124
|
+
const uiFeeUsd = applyFactor(partSizeDeltaUsd, uiFeeFactor);
|
|
125
|
+
const settledFeesUsd = (partIndex === 0 ? pendingFeesUsd : 0n) + accruedFeesUsd;
|
|
126
|
+
const collateralDeltaUsd = partGrossCollateralUsds[partIndex] - positionFeeUsd - uiFeeUsd - settledFeesUsd;
|
|
127
|
+
const collateralDeltaAmount = convertToTokenAmount(collateralDeltaUsd, collateralToken.decimals, collateralPrice);
|
|
128
|
+
const resultingState = getIncreaseResultingPositionState({
|
|
129
|
+
marketInfo,
|
|
130
|
+
collateralToken,
|
|
131
|
+
isLong,
|
|
132
|
+
existingPosition: position,
|
|
133
|
+
sizeDeltaUsd: partSizeDeltaUsd,
|
|
134
|
+
sizeDeltaInTokens: partSizeDeltaInTokens,
|
|
135
|
+
collateralDeltaAmount,
|
|
136
|
+
minCollateralUsd,
|
|
137
|
+
userReferralInfo,
|
|
138
|
+
proDiscountFactor,
|
|
139
|
+
});
|
|
140
|
+
if (!resultingState) {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
contractMarginState = resultingState.marginState;
|
|
144
|
+
const marginState = getMaxAllowedLeverageMarginState({
|
|
145
|
+
position: resultingState.position,
|
|
146
|
+
collateralToken,
|
|
147
|
+
maxAllowedLeverage,
|
|
148
|
+
minCollateralUsd,
|
|
149
|
+
}) ?? contractMarginState;
|
|
150
|
+
if (marginState.isLiquidatable) {
|
|
151
|
+
return {
|
|
152
|
+
numberOfParts,
|
|
153
|
+
failingPartIndex: partIndex,
|
|
154
|
+
isFailingPartEligibleNow: partDelaySeconds === 0,
|
|
155
|
+
marginState,
|
|
156
|
+
contractMarginState,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
marketInfo = resultingState.marketInfo;
|
|
160
|
+
position = resultingState.position;
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
numberOfParts,
|
|
164
|
+
failingPartIndex: undefined,
|
|
165
|
+
isFailingPartEligibleNow: false,
|
|
166
|
+
marginState: contractMarginState,
|
|
167
|
+
contractMarginState: contractMarginState,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
export function getIsTwapIncreaseSequenceSafe(state) {
|
|
171
|
+
return state?.failingPartIndex === undefined;
|
|
172
|
+
}
|
|
@@ -11,11 +11,18 @@ export function getIsValidTwapParams(duration, numberOfParts) {
|
|
|
11
11
|
export function getTwapDurationInSeconds(duration) {
|
|
12
12
|
return duration.hours * 60 * 60 + duration.minutes * 60;
|
|
13
13
|
}
|
|
14
|
+
export function getTwapPartDelaySeconds(duration, numberOfParts, partIndex) {
|
|
15
|
+
if (numberOfParts <= 1) {
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
return Math.floor((getTwapDurationInSeconds(duration) / (numberOfParts - 1)) * partIndex);
|
|
19
|
+
}
|
|
14
20
|
export function getTwapValidFromTime(duration, numberOfParts, partIndex) {
|
|
15
|
-
const durationMinutes = duration.hours * 60 + duration.minutes;
|
|
16
|
-
const durationMs = durationMinutes * 60;
|
|
17
21
|
const startTime = Math.ceil(Date.now() / 1000);
|
|
18
|
-
return BigInt(
|
|
22
|
+
return BigInt(startTime + getTwapPartDelaySeconds(duration, numberOfParts, partIndex));
|
|
23
|
+
}
|
|
24
|
+
export function getTwapPartDelaysSeconds(duration, numberOfParts) {
|
|
25
|
+
return Array.from({ length: numberOfParts }, (_, partIndex) => getTwapPartDelaySeconds(duration, numberOfParts, partIndex));
|
|
19
26
|
}
|
|
20
27
|
export function changeTwapNumberOfPartsValue(value) {
|
|
21
28
|
if (value < MIN_TWAP_NUMBER_OF_PARTS) {
|