@gmx-io/sdk 2.9.1 → 2.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/build/cjs/src/clients/v2/index.js +61 -41
  2. package/build/cjs/src/clients/v2/solana.spec.js +188 -0
  3. package/build/cjs/src/configs/api.js +4 -0
  4. package/build/cjs/src/utils/fees/index.js +7 -1
  5. package/build/cjs/src/utils/fees/priceImpact.js +118 -18
  6. package/build/cjs/src/utils/gmsol/__tests__/fixtures/nativeMath.json +3171 -0
  7. package/build/cjs/src/utils/gmsol/__tests__/nativeMath.spec.js +213 -0
  8. package/build/cjs/src/utils/gmsol/constants.js +12 -0
  9. package/build/cjs/src/utils/gmsol/fees.js +19 -0
  10. package/build/cjs/src/utils/gmsol/index.js +66 -0
  11. package/build/cjs/src/utils/gmsol/markets.js +206 -0
  12. package/build/cjs/src/utils/gmsol/numbers.js +230 -0
  13. package/build/cjs/src/utils/gmsol/positions.js +152 -0
  14. package/build/cjs/src/utils/gmsol/types.js +2 -0
  15. package/build/cjs/src/utils/http/http.js +3 -4
  16. package/build/cjs/src/utils/http/httpFallback.js +11 -1
  17. package/build/cjs/src/utils/http/solana.js +17 -0
  18. package/build/cjs/src/utils/http/validation.js +21 -1
  19. package/build/cjs/src/utils/http/validation.spec.js +45 -0
  20. package/build/cjs/src/utils/markets/__tests__/api.spec.js +53 -0
  21. package/build/cjs/src/utils/markets/api.js +5 -0
  22. package/build/cjs/src/utils/markets/utils.js +25 -9
  23. package/build/cjs/src/utils/orders/api.js +3 -0
  24. package/build/cjs/src/utils/positions/__tests__/liquidationPrice.spec.js +64 -0
  25. package/build/cjs/src/utils/positions/api.js +3 -0
  26. package/build/cjs/src/utils/positions/utils.js +88 -19
  27. package/build/cjs/src/utils/prices/utils.js +69 -14
  28. package/build/cjs/src/utils/referrals/api.js +41 -0
  29. package/build/cjs/src/utils/swap/swapRouting.js +4 -0
  30. package/build/cjs/src/utils/swap/swapStats.js +6 -1
  31. package/build/cjs/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1012 -0
  32. package/build/cjs/src/utils/trade/decrease.js +266 -56
  33. package/build/cjs/src/utils/trade/decreaseMarginCheck.js +23 -4
  34. package/build/cjs/src/utils/trade/increase.js +86 -28
  35. package/build/cjs/src/utils/trade/increaseMarginCheck.js +51 -9
  36. package/build/cjs/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
  37. package/build/esm/src/clients/v2/index.js +39 -19
  38. package/build/esm/src/clients/v2/solana.spec.js +160 -0
  39. package/build/esm/src/configs/api.js +5 -1
  40. package/build/esm/src/utils/fees/index.js +7 -1
  41. package/build/esm/src/utils/fees/priceImpact.js +111 -12
  42. package/build/esm/src/utils/gmsol/__tests__/fixtures/nativeMath.json +3171 -0
  43. package/build/esm/src/utils/gmsol/__tests__/nativeMath.spec.js +208 -0
  44. package/build/esm/src/utils/gmsol/constants.js +9 -0
  45. package/build/esm/src/utils/gmsol/fees.js +15 -0
  46. package/build/esm/src/utils/gmsol/index.js +46 -0
  47. package/build/esm/src/utils/gmsol/markets.js +193 -0
  48. package/build/esm/src/utils/gmsol/numbers.js +226 -0
  49. package/build/esm/src/utils/gmsol/positions.js +148 -0
  50. package/build/esm/src/utils/gmsol/types.js +1 -0
  51. package/build/esm/src/utils/http/http.js +3 -4
  52. package/build/esm/src/utils/http/httpFallback.js +11 -1
  53. package/build/esm/src/utils/http/solana.js +12 -0
  54. package/build/esm/src/utils/http/validation.js +18 -0
  55. package/build/esm/src/utils/http/validation.spec.js +43 -0
  56. package/build/esm/src/utils/markets/__tests__/api.spec.js +53 -0
  57. package/build/esm/src/utils/markets/api.js +6 -1
  58. package/build/esm/src/utils/markets/utils.js +23 -8
  59. package/build/esm/src/utils/orders/api.js +3 -0
  60. package/build/esm/src/utils/positions/__tests__/liquidationPrice.spec.js +62 -0
  61. package/build/esm/src/utils/positions/api.js +3 -0
  62. package/build/esm/src/utils/positions/utils.js +89 -20
  63. package/build/esm/src/utils/prices/utils.js +70 -15
  64. package/build/esm/src/utils/referrals/api.js +36 -0
  65. package/build/esm/src/utils/swap/swapRouting.js +4 -0
  66. package/build/esm/src/utils/swap/swapStats.js +7 -2
  67. package/build/esm/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1010 -0
  68. package/build/esm/src/utils/trade/decrease.js +266 -56
  69. package/build/esm/src/utils/trade/decreaseMarginCheck.js +23 -4
  70. package/build/esm/src/utils/trade/increase.js +86 -28
  71. package/build/esm/src/utils/trade/increaseMarginCheck.js +53 -11
  72. package/build/esm/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
  73. package/build/tsconfig.tsbuildinfo +1 -1
  74. package/build/types/src/clients/v2/index.d.ts +21 -12
  75. package/build/types/src/clients/v2/solana.spec.d.ts +1 -0
  76. package/build/types/src/utils/fees/index.d.ts +1 -1
  77. package/build/types/src/utils/fees/priceImpact.d.ts +7 -0
  78. package/build/types/src/utils/gmsol/__tests__/nativeMath.spec.d.ts +1 -0
  79. package/build/types/src/utils/gmsol/constants.d.ts +9 -0
  80. package/build/types/src/utils/gmsol/fees.d.ts +6 -0
  81. package/build/types/src/utils/gmsol/index.d.ts +38 -0
  82. package/build/types/src/utils/gmsol/markets.d.ts +49 -0
  83. package/build/types/src/utils/gmsol/numbers.d.ts +11 -0
  84. package/build/types/src/utils/gmsol/positions.d.ts +7 -0
  85. package/build/types/src/utils/gmsol/types.d.ts +105 -0
  86. package/build/types/src/utils/http/http.d.ts +2 -5
  87. package/build/types/src/utils/http/httpFallback.d.ts +2 -5
  88. package/build/types/src/utils/http/solana.d.ts +4 -0
  89. package/build/types/src/utils/http/types.d.ts +17 -4
  90. package/build/types/src/utils/http/validation.d.ts +8 -0
  91. package/build/types/src/utils/http/validation.spec.d.ts +1 -0
  92. package/build/types/src/utils/markets/hashKeys.d.ts +1 -1
  93. package/build/types/src/utils/markets/types.d.ts +3 -2
  94. package/build/types/src/utils/markets/utils.d.ts +6 -2
  95. package/build/types/src/utils/orders/api.d.ts +2 -2
  96. package/build/types/src/utils/positions/__tests__/liquidationPrice.spec.d.ts +1 -0
  97. package/build/types/src/utils/positions/api.d.ts +2 -2
  98. package/build/types/src/utils/positions/types.d.ts +1 -0
  99. package/build/types/src/utils/positions/utils.d.ts +6 -1
  100. package/build/types/src/utils/prices/types.d.ts +16 -0
  101. package/build/types/src/utils/prices/utils.d.ts +3 -11
  102. package/build/types/src/utils/referrals/api.d.ts +8 -0
  103. package/build/types/src/utils/referrals/types.d.ts +5 -0
  104. package/build/types/src/utils/rpc/createViemRpc.d.ts +25 -3
  105. package/build/types/src/utils/trade/__tests__/gmsolTradeMath.spec.d.ts +1 -0
  106. package/build/types/src/utils/trade/decrease.d.ts +5 -0
  107. package/build/types/src/utils/trade/decreaseMarginCheck.d.ts +2 -0
  108. package/build/types/src/utils/trade/increase.d.ts +2 -0
  109. package/build/types/src/utils/trade/increaseMarginCheck.d.ts +2 -0
  110. package/build/types/src/utils/trade/twapIncreaseMarginCheck.d.ts +1 -0
  111. package/build/types/src/utils/trade/types.d.ts +6 -0
  112. package/package.json +10 -1
@@ -0,0 +1,1012 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const chainIds_1 = require("../../../configs/chainIds");
5
+ const mock_1 = require("../../../test/mock");
6
+ const fees_1 = require("../../fees");
7
+ const numbers_1 = require("../../numbers");
8
+ const types_1 = require("../../orders/types");
9
+ const positions_1 = require("../../positions");
10
+ const prices_1 = require("../../prices");
11
+ const swapRouting_1 = require("../../swap/swapRouting");
12
+ const swapStats_1 = require("../../swap/swapStats");
13
+ const decrease_1 = require("../decrease");
14
+ const decreaseMarginCheck_1 = require("../decreaseMarginCheck");
15
+ const increase_1 = require("../increase");
16
+ const increaseMarginCheck_1 = require("../increaseMarginCheck");
17
+ const twapIncreaseMarginCheck_1 = require("../twapIncreaseMarginCheck");
18
+ const P = numbers_1.PRECISION;
19
+ function market(p) {
20
+ const tokens = (0, mock_1.mockTokensData)({
21
+ BTC: { decimals: p.decimals, prices: { minPrice: p.minPrice, maxPrice: p.maxPrice } },
22
+ });
23
+ return {
24
+ ...(0, mock_1.mockMarginCheckMarketInfo)(tokens, { positionImpactPoolAmount: 0n, ...p.overrides }),
25
+ tradeMath: p.tradeMath,
26
+ minCollateralUsd: 0n,
27
+ minPositionSizeUsd: 0n,
28
+ };
29
+ }
30
+ function increase(marketInfo, isLong, collateralAmount, trigger, fixed) {
31
+ return (0, increase_1.getIncreasePositionAmounts)({
32
+ marketInfo,
33
+ indexToken: marketInfo.indexToken,
34
+ initialCollateralToken: marketInfo.shortToken,
35
+ collateralToken: marketInfo.shortToken,
36
+ isLong,
37
+ initialCollateralAmount: collateralAmount,
38
+ indexTokenAmount: undefined,
39
+ leverage: 10000n,
40
+ position: undefined,
41
+ externalSwapQuote: undefined,
42
+ userReferralInfo: undefined,
43
+ strategy: "leverageByCollateral",
44
+ findSwapPath: () => undefined,
45
+ uiFeeFactor: 0n,
46
+ marketsInfoData: { [marketInfo.marketTokenAddress]: marketInfo },
47
+ chainId: chainIds_1.ARBITRUM,
48
+ externalSwapQuoteParams: undefined,
49
+ limitOrderType: trigger === undefined ? undefined : types_1.OrderType.LimitIncrease,
50
+ triggerPrice: trigger,
51
+ fixedAcceptablePriceImpactBps: fixed,
52
+ acceptablePriceImpactBuffer: 30,
53
+ isSetAcceptablePriceImpactEnabled: true,
54
+ });
55
+ }
56
+ function positionInfo(marketInfo, sizeInUsd, sizeInTokens, isLong = true) {
57
+ const value = (sizeInTokens * (isLong ? marketInfo.indexToken.prices.minPrice : marketInfo.indexToken.prices.maxPrice)) /
58
+ 10n ** BigInt(marketInfo.indexToken.decimals);
59
+ return (0, positions_1.getPositionInfo)({
60
+ marketInfo,
61
+ minCollateralUsd: 0n,
62
+ position: {
63
+ key: "position",
64
+ contractKey: "position",
65
+ account: "account",
66
+ marketAddress: marketInfo.marketTokenAddress,
67
+ collateralTokenAddress: marketInfo.shortToken.address,
68
+ sizeInUsd,
69
+ sizeInTokens,
70
+ collateralAmount: 100000000n,
71
+ pendingBorrowingFeesUsd: 0n,
72
+ increasedAtTime: 0n,
73
+ decreasedAtTime: 0n,
74
+ isLong,
75
+ fundingFeeAmount: 0n,
76
+ claimableLongTokenAmount: 0n,
77
+ claimableShortTokenAmount: 0n,
78
+ pnl: isLong ? value - sizeInUsd : sizeInUsd - value,
79
+ positionValueInUsd: value,
80
+ positionFeeAmount: 0n,
81
+ traderDiscountAmount: 0n,
82
+ uiFeeAmount: 0n,
83
+ pendingImpactAmount: 0n,
84
+ data: "",
85
+ },
86
+ });
87
+ }
88
+ (0, vitest_1.describe)("gmsol trade math", () => {
89
+ (0, vitest_1.it)("accepts the native minimum margin boundary FESOL-57", () => {
90
+ const sizeInUsd = 10000000198019801980199n * 10n ** 10n;
91
+ const marketInfo = market({
92
+ decimals: 9,
93
+ minPrice: P,
94
+ maxPrice: P,
95
+ tradeMath: "gmsol",
96
+ overrides: { longInterestUsd: sizeInUsd, minCollateralFactor: P / 100n },
97
+ });
98
+ for (const collateralDelta of [-1n, 0n, 1n]) {
99
+ const margin = (0, increaseMarginCheck_1.getResultingPositionMarginState)({
100
+ marketInfo,
101
+ collateralToken: marketInfo.shortToken,
102
+ sizeInUsd,
103
+ sizeInTokens: 100n * 10n ** 9n,
104
+ collateralAmount: 1000002n + collateralDelta,
105
+ pendingImpactAmount: 0n,
106
+ minCollateralUsd: 0n,
107
+ isLong: true,
108
+ userReferralInfo: undefined,
109
+ });
110
+ (0, vitest_1.expect)(margin.minCollateralUsdForLeverage).toBe(100000001980198019801n * 10n ** 10n);
111
+ (0, vitest_1.expect)(margin.isLiquidatable).toBe(collateralDelta < 0n);
112
+ }
113
+ });
114
+ (0, vitest_1.it)("quantizes capped PnL before realizing a partial close FESOL-57", () => {
115
+ for (const isLong of [true, false]) {
116
+ const marketInfo = market({
117
+ decimals: 9,
118
+ minPrice: P,
119
+ maxPrice: P,
120
+ tradeMath: "gmsol",
121
+ overrides: {
122
+ longInterestUsd: 100n * P,
123
+ shortInterestUsd: 100n * P,
124
+ longInterestInTokens: 103n * 10n ** 9n,
125
+ shortInterestInTokens: 97n * 10n ** 9n,
126
+ longPoolAmount: 500000n * 10n ** 9n,
127
+ shortPoolAmount: 500000n * 10n ** 6n,
128
+ maxPnlFactorForTradersLong: P / 1000000n,
129
+ maxPnlFactorForTradersShort: P / 1000000n,
130
+ },
131
+ });
132
+ const position = positionInfo(marketInfo, 100n * P, (isLong ? 101n : 99n) * 10n ** 9n, isLong);
133
+ const close = (0, decrease_1.getDecreasePositionAmounts)({
134
+ marketInfo,
135
+ collateralToken: marketInfo.shortToken,
136
+ isLong,
137
+ position,
138
+ closeSizeUsd: 33n * P,
139
+ keepLeverage: false,
140
+ userReferralInfo: undefined,
141
+ minCollateralUsd: 0n,
142
+ minPositionSizeUsd: 0n,
143
+ uiFeeFactor: 0n,
144
+ isSetAcceptablePriceImpactEnabled: false,
145
+ });
146
+ (0, vitest_1.expect)(close.estimatedPnl).toBe(16666666666666666666n * 10n ** 10n);
147
+ (0, vitest_1.expect)(close.realizedPnl).toBe(5499999999999999999n * 10n ** 10n);
148
+ }
149
+ });
150
+ (0, vitest_1.it)("quantizes impact caps before settling collateral atoms FESOL-57", () => {
151
+ const nativeUnit = 10n ** 10n;
152
+ const canonicalSize = 100n * P + 3n * nativeUnit;
153
+ const cappedMarket = market({
154
+ decimals: 9,
155
+ minPrice: P,
156
+ maxPrice: P,
157
+ tradeMath: "gmsol",
158
+ overrides: {
159
+ longInterestUsd: 1000n * P,
160
+ shortInterestUsd: 1000n * P,
161
+ positionImpactFactorNegative: P,
162
+ maxPositionImpactFactorNegative: (3n * P) / 10n,
163
+ },
164
+ });
165
+ for (const sign of [-1n, 1n]) {
166
+ const requested = sign * (canonicalSize + nativeUnit - 1n);
167
+ (0, vitest_1.expect)((0, fees_1.getPriceImpactForPosition)(cappedMarket, requested, true)).toEqual((0, fees_1.getPriceImpactForPosition)(cappedMarket, sign * canonicalSize, true));
168
+ (0, vitest_1.expect)((0, fees_1.getCappedPositionImpactUsd)(cappedMarket, requested * sign, true, sign > 0n, {
169
+ shouldCapNegativeImpact: true,
170
+ }).priceImpactDeltaUsd).toBe(-30n * P);
171
+ }
172
+ (0, vitest_1.expect)(() => (0, fees_1.getPriceImpactForPosition)({ ...cappedMarket, longInterestUsd: 1000n * P + 1n }, P, true)).toThrow(fees_1.GmsolPriceImpactError);
173
+ const evmMarket = {
174
+ ...cappedMarket,
175
+ tradeMath: undefined,
176
+ useOpenInterestInTokensForBalance: false,
177
+ positionImpactExponentFactorNegative: P,
178
+ };
179
+ (0, vitest_1.expect)((0, fees_1.getPriceImpactForPosition)(evmMarket, canonicalSize + nativeUnit - 1n, true).priceImpactDeltaUsd).not.toBe((0, fees_1.getPriceImpactForPosition)(evmMarket, canonicalSize, true).priceImpactDeltaUsd);
180
+ for (const isLong of [true, false])
181
+ for (const dust of [0n, 10n ** 10n]) {
182
+ const marketInfo = market({
183
+ decimals: 9,
184
+ minPrice: P,
185
+ maxPrice: P,
186
+ tradeMath: "gmsol",
187
+ overrides: {
188
+ longInterestUsd: (isLong ? 100n : 200n) * P,
189
+ shortInterestUsd: (isLong ? 200n : 100n) * P,
190
+ positionImpactFactorNegative: 21n * 10n ** 25n,
191
+ maxPositionImpactFactorNegative: P / 100n,
192
+ maxPositionImpactFactorPositive: P / 100n,
193
+ },
194
+ });
195
+ const sizeDeltaUsd = 50n * P + dust;
196
+ const position = positionInfo(marketInfo, 100n * P, 100n * 10n ** 9n, isLong);
197
+ const close = (0, decrease_1.getDecreasePositionAmounts)({
198
+ marketInfo,
199
+ collateralToken: marketInfo.shortToken,
200
+ isLong,
201
+ position,
202
+ closeSizeUsd: sizeDeltaUsd,
203
+ keepLeverage: false,
204
+ userReferralInfo: undefined,
205
+ minCollateralUsd: 0n,
206
+ minPositionSizeUsd: 0n,
207
+ uiFeeFactor: 0n,
208
+ isSetAcceptablePriceImpactEnabled: false,
209
+ });
210
+ (0, vitest_1.expect)(close.totalPendingImpactDeltaUsd).toBe(-P / 2n);
211
+ (0, vitest_1.expect)(close.priceImpactDiffUsd).toBe((17n * P) / 8n);
212
+ (0, vitest_1.expect)(position.collateralAmount - close.payedRemainingCollateralAmount).toBe(97375000n);
213
+ for (const sign of [-1n, 1n]) {
214
+ (0, vitest_1.expect)((0, fees_1.capPositionImpactUsdByMaxPriceImpactFactor)(marketInfo, sizeDeltaUsd, sign * P)).toBe((sign * P) / 2n);
215
+ }
216
+ }
217
+ });
218
+ (0, vitest_1.it)("deducts increase fees in collateral token atoms FESOL-57", () => {
219
+ const marketInfo = market({
220
+ decimals: 8,
221
+ minPrice: 60000n * P,
222
+ maxPrice: 60000n * P,
223
+ tradeMath: "gmsol",
224
+ overrides: {
225
+ positionFeeFactorForBalanceWasImproved: P / 2500n,
226
+ positionFeeFactorForBalanceWasNotImproved: P / 2500n,
227
+ },
228
+ });
229
+ const result = (0, increase_1.getIncreasePositionAmounts)({
230
+ marketInfo,
231
+ indexToken: marketInfo.indexToken,
232
+ initialCollateralToken: marketInfo.longToken,
233
+ collateralToken: marketInfo.longToken,
234
+ isLong: true,
235
+ initialCollateralAmount: 100000n,
236
+ indexTokenAmount: 1667083n,
237
+ position: undefined,
238
+ externalSwapQuote: undefined,
239
+ userReferralInfo: undefined,
240
+ strategy: "independent",
241
+ findSwapPath: () => undefined,
242
+ uiFeeFactor: 0n,
243
+ marketsInfoData: { [marketInfo.marketTokenAddress]: marketInfo },
244
+ chainId: chainIds_1.ARBITRUM,
245
+ externalSwapQuoteParams: undefined,
246
+ isSetAcceptablePriceImpactEnabled: false,
247
+ });
248
+ (0, vitest_1.expect)(result.sizeDeltaUsd).toBe((10002498n * P) / 10000n);
249
+ (0, vitest_1.expect)(result.positionFeeUsd).toBe((40009992n * P) / 100000000n);
250
+ (0, vitest_1.expect)(result.collateralDeltaAmount).toBe(99334n);
251
+ (0, vitest_1.expect)(result.collateralDeltaUsd).toBe((596004n * P) / 10000n);
252
+ });
253
+ (0, vitest_1.it)("values trigger collateral and funding at the execution price FESOL-57", () => {
254
+ const marketInfo = market({ decimals: 8, minPrice: 50000n * P, maxPrice: 50000n * P, tradeMath: "gmsol" });
255
+ const position = (0, positions_1.getPositionInfo)({
256
+ marketInfo,
257
+ minCollateralUsd: 0n,
258
+ position: {
259
+ ...positionInfo(marketInfo, 600n * P, 1000000n),
260
+ collateralTokenAddress: marketInfo.longTokenAddress,
261
+ collateralAmount: 100000n,
262
+ fundingFeeAmount: 1000n,
263
+ },
264
+ });
265
+ const result = (0, decrease_1.getDecreasePositionAmounts)({
266
+ marketInfo,
267
+ collateralToken: marketInfo.longToken,
268
+ isLong: true,
269
+ position,
270
+ closeSizeUsd: position.sizeInUsd,
271
+ keepLeverage: false,
272
+ userReferralInfo: undefined,
273
+ minCollateralUsd: 0n,
274
+ minPositionSizeUsd: 0n,
275
+ uiFeeFactor: 0n,
276
+ isSetAcceptablePriceImpactEnabled: false,
277
+ forceDecreaseSwapType: types_1.DecreasePositionSwapType.NoSwap,
278
+ triggerOrderType: types_1.OrderType.LimitDecrease,
279
+ triggerPrice: 60000n * P,
280
+ });
281
+ (0, vitest_1.expect)(result.fundingFeeUsd).toBe((6n * P) / 10n);
282
+ (0, vitest_1.expect)(result.collateralDeltaUsd).toBe(60n * P);
283
+ (0, vitest_1.expect)(result.receiveTokenAmount).toBe(99000n);
284
+ (0, vitest_1.expect)(result.receiveUsd).toBe((594n * P) / 10n);
285
+ (0, vitest_1.expect)(position.pendingFundingFeesUsd).toBe(P / 2n);
286
+ });
287
+ (0, vitest_1.it)("settles each collateral shortfall in PnL token atoms FESOL-57", () => {
288
+ const size = (250125n * P) / 100n;
289
+ for (const [markPrice, triggerPrice, isTwap, fundingFeeAmount, feeFactor, pnlAmount, collateralAmount, pnlUsd, unpaidUsd,] of [
290
+ [60000n * P, undefined, false, 10000300n, P / 2500n, 829583n, 0n, (4977498n * P) / 10000n, 0n],
291
+ [60000n * P, 60000n * P, false, 10000300n, P / 2500n, 829583n, 0n, (4977498n * P) / 10000n, 0n],
292
+ [60000n * P, undefined, true, 10000300n, P / 2500n, 829583n, 0n, (4977498n * P) / 10000n, 0n],
293
+ [50000n * P, 60000n * P, false, 0n, 0n, 831250n, 10000000n, (49875n * P) / 100n, 0n],
294
+ [60000n * P, undefined, false, 509750000n, 0n, 0n, 0n, 0n, (9996n * P) / 10000n],
295
+ ]) {
296
+ const marketInfo = market({
297
+ decimals: 8,
298
+ minPrice: markPrice,
299
+ maxPrice: markPrice,
300
+ tradeMath: "gmsol",
301
+ overrides: {
302
+ longInterestUsd: size,
303
+ shortInterestUsd: size,
304
+ longInterestInTokens: 5000000n,
305
+ shortInterestInTokens: 5000000n,
306
+ longPoolAmount: 100n * 10n ** 8n,
307
+ shortPoolAmount: 1000000n * 10n ** 6n,
308
+ maxPnlFactorForTradersLong: P,
309
+ positionFeeFactorForBalanceWasImproved: feeFactor,
310
+ positionFeeFactorForBalanceWasNotImproved: feeFactor,
311
+ },
312
+ });
313
+ const position = (0, positions_1.getPositionInfo)({
314
+ marketInfo,
315
+ minCollateralUsd: 0n,
316
+ position: {
317
+ ...positionInfo(marketInfo, size, 5000000n),
318
+ collateralAmount: 10000000n,
319
+ fundingFeeAmount,
320
+ },
321
+ });
322
+ const result = (0, decrease_1.getDecreasePositionAmounts)({
323
+ marketInfo,
324
+ collateralToken: marketInfo.shortToken,
325
+ isLong: true,
326
+ position,
327
+ closeSizeUsd: size,
328
+ keepLeverage: false,
329
+ userReferralInfo: undefined,
330
+ minCollateralUsd: 0n,
331
+ minPositionSizeUsd: 0n,
332
+ uiFeeFactor: 0n,
333
+ isSetAcceptablePriceImpactEnabled: false,
334
+ forceDecreaseSwapType: types_1.DecreasePositionSwapType.NoSwap,
335
+ triggerOrderType: triggerPrice === undefined ? undefined : types_1.OrderType.LimitDecrease,
336
+ triggerPrice,
337
+ isTwap,
338
+ });
339
+ (0, vitest_1.expect)(result.primaryOutput).toEqual({
340
+ tokenAddress: marketInfo.longTokenAddress,
341
+ amount: pnlAmount,
342
+ usd: pnlUsd,
343
+ });
344
+ (0, vitest_1.expect)(result.secondaryOutput.amount).toBe(collateralAmount);
345
+ (0, vitest_1.expect)(result.payedRemainingCollateralAmount).toBe(10000000n - collateralAmount);
346
+ (0, vitest_1.expect)(result.unpaidCostUsd).toBe(unpaidUsd);
347
+ (0, vitest_1.expect)(marketInfo.indexToken.prices.minPrice).toBe(markPrice);
348
+ }
349
+ });
350
+ (0, vitest_1.it)("values a gmsol close with the impact withheld beyond the cap FESOL-57", () => {
351
+ for (const tradeMath of ["gmsol", undefined]) {
352
+ const marketInfo = market({
353
+ decimals: 8,
354
+ minPrice: P,
355
+ maxPrice: P,
356
+ tradeMath,
357
+ overrides: {
358
+ longInterestUsd: 100n * P,
359
+ shortInterestUsd: 100n * P,
360
+ longInterestInTokens: 100n * 10n ** 8n,
361
+ shortInterestInTokens: 100n * 10n ** 8n,
362
+ positionImpactFactorNegative: 2n * 10n ** 27n,
363
+ maxPositionImpactFactorNegative: 5n * 10n ** 28n,
364
+ },
365
+ });
366
+ const position = positionInfo(marketInfo, 100n * P, 100n * 10n ** 8n);
367
+ const close = (0, decrease_1.getDecreasePositionAmounts)({
368
+ marketInfo,
369
+ collateralToken: marketInfo.shortToken,
370
+ isLong: true,
371
+ position,
372
+ closeSizeUsd: position.sizeInUsd,
373
+ keepLeverage: false,
374
+ userReferralInfo: undefined,
375
+ minCollateralUsd: 0n,
376
+ minPositionSizeUsd: 0n,
377
+ uiFeeFactor: 0n,
378
+ isSetAcceptablePriceImpactEnabled: false,
379
+ });
380
+ (0, vitest_1.expect)(position.closePriceImpactDeltaUsd).toBe(close.closePriceImpactDeltaUsd);
381
+ (0, vitest_1.expect)(position.closePriceImpactDeltaUsd).toBeLessThan(-19n * P);
382
+ (0, vitest_1.expect)(position.netPriceImapctDeltaUsd).toBe(tradeMath ? -5n * P : position.closePriceImpactDeltaUsd);
383
+ (0, vitest_1.expect)(position.netValueAfterAllFees).toBe(position.collateralUsd + position.closePriceImpactDeltaUsd);
384
+ (0, vitest_1.expect)(position.pnlAfterAllFees).toBe(position.closePriceImpactDeltaUsd);
385
+ (0, vitest_1.expect)(position.netValueAfterAllFees - close.receiveUsd).toBeLessThanOrEqual(10n ** 24n);
386
+ (0, vitest_1.expect)(close.receiveUsd - position.netValueAfterAllFees).toBeLessThanOrEqual(10n ** 24n);
387
+ }
388
+ });
389
+ (0, vitest_1.it)("rejects gmsol increases that fill no index token atoms FESOL-57", () => {
390
+ const imbalanced = {
391
+ longInterestUsd: 1000n * P,
392
+ longInterestInTokens: 5n * 10n ** 6n,
393
+ positionImpactFactorNegative: 10n ** 25n,
394
+ positionImpactExponentFactorNegative: P,
395
+ };
396
+ for (const { name, overrides, collateralAmount } of [
397
+ { name: "below one atom", overrides: {}, collateralAmount: 100n },
398
+ { name: "one atom consumed by impact", overrides: imbalanced, collateralAmount: 300n },
399
+ { name: "below one atom with impact", overrides: imbalanced, collateralAmount: 100n },
400
+ ]) {
401
+ const marketInfo = market({
402
+ decimals: 8,
403
+ minPrice: 20000n * P,
404
+ maxPrice: 20000n * P,
405
+ overrides,
406
+ tradeMath: "gmsol",
407
+ });
408
+ const values = increase(marketInfo, true, collateralAmount);
409
+ const sizeDeltaUsd = collateralAmount * 10n ** 24n;
410
+ (0, vitest_1.expect)([name, values.sizeDeltaUsd, values.sizeDeltaInTokens, values.acceptablePrice]).toEqual([name, 0n, 0n, 0n]);
411
+ (0, vitest_1.expect)((0, increaseMarginCheck_1.getIncreaseResultingPositionState)({
412
+ marketInfo,
413
+ collateralToken: marketInfo.shortToken,
414
+ isLong: true,
415
+ existingPosition: {
416
+ sizeInUsd: 100n * P,
417
+ sizeInTokens: 5n * 10n ** 5n,
418
+ collateralAmount: 100n * 10n ** 6n,
419
+ pendingImpactAmount: 0n,
420
+ },
421
+ sizeDeltaUsd,
422
+ sizeDeltaInTokens: 1n,
423
+ collateralDeltaAmount: collateralAmount,
424
+ minCollateralUsd: 0n,
425
+ userReferralInfo: undefined,
426
+ })).toBeUndefined();
427
+ }
428
+ });
429
+ (0, vitest_1.it)("rounds decrease execution adjustments in native price units FESOL-57", () => {
430
+ for (const { isLong, improved, cap, expectedUnitPrice } of [
431
+ { isLong: true, improved: false, cap: P, expectedUnitPrice: 74750000000n },
432
+ { isLong: false, improved: true, cap: P, expectedUnitPrice: 76850000000n },
433
+ { isLong: true, improved: false, cap: P / 100n, expectedUnitPrice: 79000000000n },
434
+ { isLong: false, improved: true, cap: P / 100n, expectedUnitPrice: 79000000000n },
435
+ ]) {
436
+ const sideOi = improved ? 200n : 100n;
437
+ const otherOi = improved ? 100n : 200n;
438
+ const marketInfo = market({
439
+ decimals: 9,
440
+ minPrice: (4n * P) / 5n,
441
+ maxPrice: (4n * P) / 5n,
442
+ tradeMath: "gmsol",
443
+ overrides: {
444
+ longInterestUsd: (isLong ? sideOi : otherOi) * P,
445
+ shortInterestUsd: (isLong ? otherOi : sideOi) * P,
446
+ positionImpactFactorPositive: (21n * P) / 100000n,
447
+ positionImpactFactorNegative: (21n * P) / 100000n,
448
+ positionImpactExponentFactorPositive: 2n * P,
449
+ positionImpactExponentFactorNegative: 2n * P,
450
+ positionImpactPoolAmount: 10000000000n,
451
+ maxPositionImpactFactorPositive: cap,
452
+ maxPositionImpactFactorNegative: cap,
453
+ },
454
+ });
455
+ for (const sizeDeltaInTokens of [undefined, 0n, 50000000000n]) {
456
+ const result = (0, prices_1.getAcceptablePriceInfo)({
457
+ marketInfo,
458
+ isIncrease: false,
459
+ isLimit: false,
460
+ isLong,
461
+ indexPrice: (4n * P) / 5n,
462
+ sizeDeltaUsd: 50n * P,
463
+ sizeDeltaInTokens,
464
+ positionSizeInUsd: 100n * P,
465
+ positionSizeInTokens: 99999999999n,
466
+ });
467
+ (0, vitest_1.expect)(result.acceptablePrice).toBe(expectedUnitPrice * 10n ** 19n);
468
+ }
469
+ }
470
+ });
471
+ (0, vitest_1.it)("evaluates gmsol limit increases at the trigger and covers the execution FESOL-57", () => {
472
+ for (const isLong of [false, true]) {
473
+ const marketInfo = market({
474
+ decimals: 9,
475
+ minPrice: P,
476
+ maxPrice: P,
477
+ tradeMath: "gmsol",
478
+ overrides: { positionImpactFactorNegative: 0n, positionImpactFactorPositive: 0n },
479
+ });
480
+ for (const dust of [0n, 1n, 9999999999n]) {
481
+ (0, vitest_1.expect)((0, prices_1.getAcceptablePriceInfo)({
482
+ marketInfo,
483
+ sizeDeltaUsd: P + dust,
484
+ indexPrice: P,
485
+ isIncrease: true,
486
+ isLimit: false,
487
+ isLong,
488
+ }).acceptablePrice).toBe(P);
489
+ }
490
+ }
491
+ for (const { isLong, isLimit, indexPrice, nativeUnitPrice } of [
492
+ { isLong: true, isLimit: false, indexPrice: 110n * P, nativeUnitPrice: 111361079865016872890n },
493
+ { isLong: false, isLimit: false, indexPrice: 90n * P, nativeUnitPrice: 89108910891089108910n },
494
+ { isLong: true, isLimit: true, indexPrice: 100n * P, nativeUnitPrice: 101020408163265306122n },
495
+ { isLong: false, isLimit: true, indexPrice: 100n * P, nativeUnitPrice: 99000000000000000000n },
496
+ ]) {
497
+ const marketInfo = market({
498
+ decimals: 2,
499
+ minPrice: 90n * P,
500
+ maxPrice: 110n * P,
501
+ tradeMath: "gmsol",
502
+ overrides: {
503
+ longInterestUsd: 100n * P,
504
+ shortInterestUsd: 100n * P,
505
+ positionImpactFactorNegative: P / 100n,
506
+ positionImpactExponentFactorNegative: P,
507
+ },
508
+ });
509
+ const result = (0, prices_1.getAcceptablePriceInfo)({
510
+ marketInfo,
511
+ isIncrease: true,
512
+ isLimit,
513
+ isLong,
514
+ indexPrice,
515
+ sizeDeltaUsd: 990n * P,
516
+ });
517
+ (0, vitest_1.expect)(result.acceptablePrice).toBe(nativeUnitPrice * 10n ** 12n);
518
+ (0, vitest_1.expect)(marketInfo.indexToken.prices).toEqual({ minPrice: 90n * P, maxPrice: 110n * P });
519
+ }
520
+ for (const { tradeMath, isLong, decimals, price, factor, exponent, minBps } of [
521
+ { tradeMath: "gmsol", isLong: true, decimals: 6, price: P, factor: P / 1000n, exponent: 2n * P, minBps: 1142n },
522
+ { tradeMath: "gmsol", isLong: false, decimals: 6, price: P, factor: P / 1000n, exponent: 2n * P, minBps: 940n },
523
+ { tradeMath: "gmsol", isLong: true, decimals: 2, price: 3n * P, factor: 0n, exponent: P, minBps: 132n },
524
+ { tradeMath: "gmsol", isLong: false, decimals: 2, price: 3n * P, factor: 0n, exponent: P, minBps: 227n },
525
+ { tradeMath: undefined, isLong: true, decimals: 2, price: 3n * P, factor: 0n, exponent: P, minBps: 30n },
526
+ ]) {
527
+ const marketInfo = market({
528
+ decimals,
529
+ minPrice: price,
530
+ maxPrice: price,
531
+ tradeMath,
532
+ overrides: {
533
+ longInterestUsd: 100n * P,
534
+ shortInterestUsd: 100n * P,
535
+ positionImpactFactorNegative: factor,
536
+ positionImpactExponentFactorNegative: exponent,
537
+ maxPositionImpactFactorNegative: P / 2n,
538
+ },
539
+ });
540
+ const sizeDeltaUsd = (decimals === 6 ? 100n : 1n) * P;
541
+ const values = increase(marketInfo, isLong, sizeDeltaUsd / 10n ** 24n, price);
542
+ const execution = (values.sizeDeltaUsd * 10n ** BigInt(decimals)) / values.sizeDeltaInTokens;
543
+ const bufferUsd = (price * 30n) / 10000n;
544
+ (0, vitest_1.expect)(values.recommendedAcceptablePriceDeltaBps).toBe(minBps);
545
+ if (tradeMath) {
546
+ (0, vitest_1.expect)(isLong ? values.acceptablePrice - execution : execution - values.acceptablePrice).toBeGreaterThanOrEqual(bufferUsd);
547
+ }
548
+ }
549
+ const fixedMarket = market({
550
+ decimals: 6,
551
+ minPrice: P,
552
+ maxPrice: P,
553
+ tradeMath: "gmsol",
554
+ overrides: {
555
+ longInterestUsd: 100n * P,
556
+ shortInterestUsd: 100n * P,
557
+ positionImpactFactorNegative: P / 1000n,
558
+ positionImpactExponentFactorNegative: 2n * P,
559
+ },
560
+ });
561
+ const fixed = increase(fixedMarket, true, 100000000n, P, 500n);
562
+ (0, vitest_1.expect)(fixed.acceptablePrice).toBe((105n * P) / 100n);
563
+ });
564
+ (0, vitest_1.it)("preserves native partial-close boundaries and zero UI fees FESOL-57", () => {
565
+ for (const { tradeMath, price, size, closeSize, units, minimum, full, withdrawal } of [
566
+ {
567
+ tradeMath: "gmsol",
568
+ price: 94n * P,
569
+ size: 1000n * P,
570
+ closeSize: 500n * P,
571
+ units: 10000000000n,
572
+ minimum: 0n,
573
+ full: false,
574
+ withdrawal: 20n * P,
575
+ },
576
+ {
577
+ tradeMath: "gmsol",
578
+ price: 92n * P,
579
+ size: 1000n * P,
580
+ closeSize: 500n * P,
581
+ units: 10000000000n,
582
+ minimum: 0n,
583
+ full: false,
584
+ withdrawal: 10n * P,
585
+ },
586
+ {
587
+ tradeMath: "gmsol",
588
+ price: (91998n * P) / 1000n,
589
+ size: 1000n * P,
590
+ closeSize: 500n * P,
591
+ units: 10000000000n,
592
+ minimum: 0n,
593
+ full: true,
594
+ withdrawal: 100n * P,
595
+ },
596
+ {
597
+ tradeMath: undefined,
598
+ price: 94n * P,
599
+ size: 1000n * P,
600
+ closeSize: 500n * P,
601
+ units: 10000000000n,
602
+ minimum: 0n,
603
+ full: false,
604
+ withdrawal: 20n * P,
605
+ },
606
+ {
607
+ tradeMath: "gmsol",
608
+ price: 100n * P,
609
+ size: 10n * P,
610
+ closeSize: (95n * P) / 10n,
611
+ units: 100000000n,
612
+ minimum: P / 10n,
613
+ full: false,
614
+ withdrawal: 0n,
615
+ },
616
+ {
617
+ tradeMath: undefined,
618
+ price: 100n * P,
619
+ size: 10n * P,
620
+ closeSize: (95n * P) / 10n,
621
+ units: 100000000n,
622
+ minimum: P / 10n,
623
+ full: true,
624
+ withdrawal: 100n * P,
625
+ },
626
+ ]) {
627
+ const marketInfo = market({
628
+ decimals: 9,
629
+ minPrice: price,
630
+ maxPrice: price,
631
+ tradeMath,
632
+ overrides: { minCollateralFactor: P / 50n },
633
+ });
634
+ marketInfo.minCollateralUsd = 10n * P;
635
+ marketInfo.minPositionSizeUsd = minimum;
636
+ const position = positionInfo(marketInfo, size, units);
637
+ const result = (0, decrease_1.getDecreasePositionAmounts)({
638
+ marketInfo,
639
+ collateralToken: marketInfo.shortToken,
640
+ isLong: true,
641
+ position,
642
+ closeSizeUsd: closeSize,
643
+ keepLeverage: size > 10n * P,
644
+ userReferralInfo: undefined,
645
+ minCollateralUsd: 10n * P,
646
+ minPositionSizeUsd: minimum,
647
+ uiFeeFactor: 0n,
648
+ isSetAcceptablePriceImpactEnabled: false,
649
+ forceDecreaseSwapType: types_1.DecreasePositionSwapType.NoSwap,
650
+ });
651
+ (0, vitest_1.expect)([result.isFullClose, result.sizeDeltaUsd, result.collateralDeltaUsd]).toEqual([
652
+ full,
653
+ full ? size : closeSize,
654
+ withdrawal,
655
+ ]);
656
+ }
657
+ for (const triggerOrderType of [undefined, types_1.OrderType.LimitDecrease]) {
658
+ const marketInfo = market({
659
+ decimals: 8,
660
+ minPrice: 100n * P,
661
+ maxPrice: 100n * P,
662
+ tradeMath: "gmsol",
663
+ overrides: {
664
+ longInterestUsd: 1000n * P,
665
+ shortInterestUsd: 1000n * P,
666
+ positionImpactFactorNegative: P / 25n,
667
+ positionImpactExponentFactorNegative: P,
668
+ maxPositionImpactFactorNegative: P / 50n,
669
+ },
670
+ });
671
+ const position = positionInfo(marketInfo, 1000n * P, 10n * 10n ** 8n);
672
+ const result = (0, decrease_1.getDecreasePositionAmounts)({
673
+ marketInfo,
674
+ collateralToken: marketInfo.shortToken,
675
+ isLong: true,
676
+ position,
677
+ closeSizeUsd: 500n * P,
678
+ keepLeverage: true,
679
+ userReferralInfo: undefined,
680
+ minCollateralUsd: 0n,
681
+ minPositionSizeUsd: 0n,
682
+ uiFeeFactor: 0n,
683
+ triggerOrderType,
684
+ triggerPrice: 100n * P,
685
+ isSetAcceptablePriceImpactEnabled: false,
686
+ forceDecreaseSwapType: types_1.DecreasePositionSwapType.NoSwap,
687
+ });
688
+ (0, vitest_1.expect)(result.isFullClose).toBe(false);
689
+ (0, vitest_1.expect)(result.closePriceImpactDeltaUsd).toBe(-20n * P);
690
+ (0, vitest_1.expect)(result.priceImpactDiffUsd).toBe(10n * P);
691
+ (0, vitest_1.expect)(result.collateralDeltaUsd).toBe(30n * P);
692
+ (0, vitest_1.expect)(result.payedRemainingCollateralUsd).toBe(20n * P);
693
+ (0, vitest_1.expect)(result.receiveUsd).toBe(20n * P);
694
+ }
695
+ for (const tradeMath of ["gmsol", undefined]) {
696
+ const marketInfo = market({
697
+ decimals: 8,
698
+ minPrice: P,
699
+ maxPrice: P,
700
+ tradeMath,
701
+ overrides: {
702
+ swapFeeFactorForBalanceWasImproved: 0n,
703
+ swapFeeFactorForBalanceWasNotImproved: 0n,
704
+ swapImpactFactorPositive: 0n,
705
+ swapImpactFactorNegative: 0n,
706
+ },
707
+ });
708
+ const position = positionInfo(marketInfo, 100n * P, 100n * 10n ** 8n, false);
709
+ const marketsInfoData = { [marketInfo.marketTokenAddress]: marketInfo };
710
+ const findSwapPath = (usdIn) => (0, swapStats_1.getSwapPathStats)({
711
+ marketsInfoData,
712
+ swapPath: [marketInfo.marketTokenAddress],
713
+ initialCollateralAddress: marketInfo.shortToken.address,
714
+ wrappedNativeTokenAddress: "WETH",
715
+ usdIn,
716
+ shouldUnwrapNativeToken: false,
717
+ shouldApplyPriceImpact: true,
718
+ swapPricingType: types_1.SwapPricingType.Swap,
719
+ });
720
+ for (const uiFeeFactor of [0n, P / 100n]) {
721
+ const result = (0, decrease_1.getOptimalDecreaseAndSwapAmounts)({
722
+ marketInfo,
723
+ collateralToken: marketInfo.shortToken,
724
+ isLong: false,
725
+ position,
726
+ closeSizeUsd: position.sizeInUsd,
727
+ keepLeverage: false,
728
+ userReferralInfo: undefined,
729
+ minCollateralUsd: 0n,
730
+ minPositionSizeUsd: 0n,
731
+ uiFeeFactor,
732
+ isSetAcceptablePriceImpactEnabled: false,
733
+ receiveToken: marketInfo.indexToken,
734
+ findSwapPath,
735
+ findSwapPathFromPnl: findSwapPath,
736
+ marketsInfoData,
737
+ chainId: chainIds_1.ARBITRUM,
738
+ });
739
+ (0, vitest_1.expect)(result.swapAmounts?.usdOut).toBe(tradeMath || uiFeeFactor === 0n ? 100n * P : (9801n * P) / 100n);
740
+ (0, vitest_1.expect)(result.decreaseAmounts.uiFeeUsd).toBe(tradeMath || uiFeeFactor === 0n ? 0n : P);
741
+ }
742
+ }
743
+ const evm = market({
744
+ decimals: 8,
745
+ minPrice: P,
746
+ maxPrice: P,
747
+ overrides: {
748
+ swapFeeFactorForBalanceWasImproved: 0n,
749
+ swapFeeFactorForBalanceWasNotImproved: 0n,
750
+ swapImpactFactorPositive: 0n,
751
+ swapImpactFactorNegative: 0n,
752
+ },
753
+ });
754
+ const marketsInfoData = {
755
+ evm: { ...evm, marketTokenAddress: "evm" },
756
+ gmsol: { ...evm, marketTokenAddress: "gmsol", tradeMath: "gmsol" },
757
+ };
758
+ for (const [swapPath, collateralToken, partUsd] of [
759
+ [["evm"], evm.shortToken, 99n * P],
760
+ [["gmsol"], evm.shortToken, 100n * P],
761
+ [["gmsol", "evm"], evm.longToken, 99n * P],
762
+ ]) {
763
+ const parts = (0, twapIncreaseMarginCheck_1.getTwapIncreasePartsGrossCollateralUsd)({
764
+ marketsInfoData,
765
+ swapPath: [...swapPath],
766
+ initialCollateralToken: evm.longToken,
767
+ collateralToken,
768
+ initialCollateralAmountPerPart: 100n * 10n ** 8n,
769
+ numberOfParts: 2,
770
+ uiFeeFactor: P / 100n,
771
+ wrappedNativeTokenAddress: "WETH",
772
+ });
773
+ (0, vitest_1.expect)([swapPath, parts]).toEqual([swapPath, [partUsd, partUsd]]);
774
+ }
775
+ });
776
+ (0, vitest_1.it)("settles gmsol closes, close margins and closing fees with native rounding FESOL-57", () => {
777
+ for (const [tradeMath, priceDelta, longOi, impactFactor, capFactor, exponent, receive] of [
778
+ ["gmsol", 0n, 100n, 200000005n * 10n ** 21n, P / 20n, 1n, 79999999n],
779
+ [undefined, 0n, 100n, 200000005n * 10n ** 21n, P / 20n, 1n, 80000000n],
780
+ ["gmsol", 0n, 100n, 20000001n * 10n ** 22n, 50000005n * 10n ** 21n, 1n, 79999998n],
781
+ ["gmsol", -6n * 10n ** 21n, 100n, 6n * 10n ** 21n, P, 1n, 99999998n],
782
+ ["gmsol", 6n * 10n ** 21n, 200n, 6n * 10n ** 21n, P, 1n, 100000000n],
783
+ ["gmsol", 0n, 200n, 21n * 10n ** 25n, P, 2n, 102100000n],
784
+ ["gmsol", 0n, 200n, 21n * 10n ** 23n, P, 3n, 102100000n],
785
+ ["gmsol", 0n, 200n, 21n * 10n ** 21n, P, 4n, 102100000n],
786
+ [undefined, 0n, 200n, 21n * 10n ** 25n, P, 2n, 102099999n],
787
+ ]) {
788
+ const marketInfo = market({
789
+ decimals: 9,
790
+ minPrice: P + priceDelta,
791
+ maxPrice: P + priceDelta,
792
+ tradeMath,
793
+ overrides: {
794
+ longInterestUsd: longOi * P,
795
+ shortInterestUsd: 100n * P,
796
+ longInterestInTokens: longOi * 10n ** 9n,
797
+ shortInterestInTokens: 100n * 10n ** 9n,
798
+ positionImpactPoolAmount: 10n * 10n ** 9n,
799
+ positionImpactFactorPositive: impactFactor,
800
+ positionImpactFactorNegative: impactFactor,
801
+ positionImpactExponentFactorPositive: exponent * P,
802
+ positionImpactExponentFactorNegative: exponent * P,
803
+ maxPositionImpactFactorPositive: P,
804
+ maxPositionImpactFactorNegative: capFactor,
805
+ longPoolAmount: 1000000000000n,
806
+ shortPoolAmount: 1000000000000n,
807
+ maxPnlFactorForTradersLong: P,
808
+ },
809
+ });
810
+ marketInfo.longToken = marketInfo.shortToken;
811
+ marketInfo.longTokenAddress = marketInfo.shortTokenAddress;
812
+ marketInfo.isSameCollaterals = true;
813
+ const position = positionInfo(marketInfo, 100n * P, 100n * 10n ** 9n);
814
+ const close = (0, decrease_1.getDecreasePositionAmounts)({
815
+ marketInfo,
816
+ collateralToken: marketInfo.shortToken,
817
+ isLong: true,
818
+ position,
819
+ closeSizeUsd: position.sizeInUsd,
820
+ keepLeverage: false,
821
+ userReferralInfo: undefined,
822
+ minCollateralUsd: 0n,
823
+ minPositionSizeUsd: 0n,
824
+ uiFeeFactor: 0n,
825
+ isSetAcceptablePriceImpactEnabled: false,
826
+ forceDecreaseSwapType: types_1.DecreasePositionSwapType.NoSwap,
827
+ });
828
+ (0, vitest_1.expect)([priceDelta, impactFactor, close.receiveTokenAmount]).toEqual([priceDelta, impactFactor, receive]);
829
+ }
830
+ for (const [tradeMath, minCollateralFactor, sizeDeltaUsd, withdrawal, expected] of [
831
+ ["gmsol", P / 100n, 500n * P, 50000000n, undefined],
832
+ [undefined, P / 100n, 500n * P, 50000000n, [false, true]],
833
+ ["gmsol", P / 100n, 500n * P, 20000000n, [false, false]],
834
+ ["gmsol", (15n * P) / 100n, 500n * P, 50000000n, [false, true]],
835
+ ["gmsol", P / 100n, 0n, 50000000n, [true, false]],
836
+ ]) {
837
+ const marketInfo = market({
838
+ decimals: 9,
839
+ minPrice: P,
840
+ maxPrice: P,
841
+ tradeMath,
842
+ overrides: { minCollateralFactor, longInterestUsd: 1000n * P, shortInterestUsd: 1000n * P },
843
+ });
844
+ marketInfo.minCollateralUsd = 75n * P;
845
+ const params = {
846
+ marketInfo,
847
+ collateralToken: marketInfo.shortToken,
848
+ isLong: true,
849
+ position: positionInfo(marketInfo, 1000n * P, 1000n * 10n ** 9n),
850
+ minCollateralUsd: 75n * P,
851
+ minPositionSizeUsd: 0n,
852
+ userReferralInfo: undefined,
853
+ };
854
+ const direct = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)({
855
+ ...params,
856
+ sizeDeltaUsd,
857
+ sizeDeltaInTokens: sizeDeltaUsd / 10n ** 21n,
858
+ collateralDeltaAmount: withdrawal,
859
+ payedRemainingCollateralAmount: 0n,
860
+ priceImpactDiffUsd: 0n,
861
+ });
862
+ const row = [tradeMath, minCollateralFactor, sizeDeltaUsd, withdrawal];
863
+ (0, vitest_1.expect)([row, direct && [direct.isLiquidatable, direct.isCollateralWithdrawalCancelled]]).toEqual([row, expected]);
864
+ if (sizeDeltaUsd > 0n) {
865
+ const decreaseAmounts = (0, decrease_1.getDecreasePositionAmounts)({
866
+ ...params,
867
+ closeSizeUsd: sizeDeltaUsd,
868
+ keepLeverage: false,
869
+ uiFeeFactor: 0n,
870
+ triggerOrderType: types_1.OrderType.LimitDecrease,
871
+ triggerPrice: P,
872
+ isSetAcceptablePriceImpactEnabled: false,
873
+ });
874
+ const stored = (0, decreaseMarginCheck_1.getTriggerDecreaseRemainingPositionMarginState)({
875
+ ...params,
876
+ decreaseAmounts,
877
+ collateralDeltaAmount: withdrawal,
878
+ });
879
+ (0, vitest_1.expect)([row, stored]).toEqual([row, direct]);
880
+ }
881
+ }
882
+ for (const [tradeMath, collateralAmount, remainingCollateralUsd, isLiquidatable] of [
883
+ ["gmsol", 1005150n, (100015n * P) / 100000n, false],
884
+ ["gmsol", 1005149n, (1000149n * P) / 1000000n, true],
885
+ [undefined, 1005150n, (1000149n * P) / 1000000n, true],
886
+ ]) {
887
+ const marketInfo = market({
888
+ decimals: 9,
889
+ minPrice: P,
890
+ maxPrice: P,
891
+ tradeMath,
892
+ overrides: {
893
+ positionFeeFactorForBalanceWasImproved: 10n ** 26n,
894
+ positionFeeFactorForBalanceWasNotImproved: 10n ** 26n,
895
+ },
896
+ });
897
+ const state = (0, increaseMarginCheck_1.getResultingPositionMarginState)({
898
+ marketInfo,
899
+ collateralToken: marketInfo.shortToken,
900
+ sizeInUsd: (100015n * P) / 1000n,
901
+ sizeInTokens: 100015000000n,
902
+ collateralAmount,
903
+ pendingImpactAmount: 0n,
904
+ minCollateralUsd: 0n,
905
+ isLong: true,
906
+ userReferralInfo: undefined,
907
+ proDiscountFactor: P / 2n,
908
+ feeDiscountFactor: P / 2n,
909
+ });
910
+ (0, vitest_1.expect)([collateralAmount, state.remainingCollateralUsd, state.isLiquidatable]).toEqual([
911
+ collateralAmount,
912
+ remainingCollateralUsd,
913
+ isLiquidatable,
914
+ ]);
915
+ const fee = (0, fees_1.getPositionFee)(marketInfo, P + 15000n * 10n ** 10n, false, undefined, undefined, P / 2n, P / 2n);
916
+ (0, vitest_1.expect)([fee.positionFeeUsd, fee.discountUsd]).toEqual(tradeMath === "gmsol"
917
+ ? [50000000000000010000000000n, 50000000000000000000000000n]
918
+ : [50000000000000007500000000n, 50000000000000007500000000n]);
919
+ }
920
+ });
921
+ (0, vitest_1.it)("keeps native impact precision and propagates unavailable Solana quotes FESOL-57", () => {
922
+ const calculate = (diff, exponent, next = 0n) => (0, fees_1.getPriceImpactUsd)({
923
+ currentLongUsd: diff,
924
+ currentShortUsd: 0n,
925
+ nextLongUsd: next,
926
+ nextShortUsd: 0n,
927
+ factorPositive: P,
928
+ factorNegative: P,
929
+ exponentFactorPositive: exponent,
930
+ exponentFactorNegative: exponent,
931
+ tradeMath: "gmsol",
932
+ }).priceImpactDeltaUsd;
933
+ for (const [diff, exponent, expected] of [
934
+ [P - 10n ** 10n, 0n, 0n],
935
+ [P, P / 2n, P],
936
+ [2n * P, P / 2n, 1414213557n * 10n ** 21n],
937
+ [100n * P, (3n * P) / 2n, 999999998638n * 10n ** 21n],
938
+ [(123456n * P) / 10n, (11n * P) / 10n, 31670982733137n * 10n ** 21n],
939
+ [123456789123456789123n * 10n ** 10n, 2n * P + 10n ** 10n, 1524157877n * 10n ** 21n],
940
+ [P + 10n ** 21n, (257n * P) / 2n, 1000000129n * 10n ** 21n],
941
+ [2n * P - 10n ** 10n, 10n ** 10n, P],
942
+ [9223372036854775807n * 10n ** 21n, P / 2n, 96035880027389n * 10n ** 21n],
943
+ [18446744073709551615n * 10n ** 21n, P + 10n ** 10n, 18446744073709551615n * 10n ** 21n],
944
+ [(13n * P) / 10n + 10n ** 10n, 4n * P, 285610000000000000007n * 10n ** 10n],
945
+ [P + 10n ** 10n, 128n * P, P + 128n * 10n ** 10n],
946
+ ])
947
+ (0, vitest_1.expect)(calculate(diff, exponent)).toBe(expected);
948
+ for (const [diff, exponent] of [
949
+ [10000000000n * P, P / 2n],
950
+ [2n * P, (257n * P) / 2n],
951
+ [P + 10n ** 10n, 9223372036854775808n * 10n ** 21n],
952
+ [P + 10n ** 10n, 129n * P],
953
+ [P + 1n, P],
954
+ [(1n << 127n) * 10n ** 10n, P],
955
+ [((1n << 128n) - 1n) * 10n ** 10n, 2n * P],
956
+ ])
957
+ (0, vitest_1.expect)(() => calculate(diff, exponent)).toThrow(fees_1.GmsolPriceImpactError);
958
+ for (const tradeMath of ["gmsol", undefined]) {
959
+ const m = market({
960
+ decimals: 8,
961
+ minPrice: P,
962
+ maxPrice: P,
963
+ tradeMath,
964
+ overrides: {
965
+ swapImpactExponentFactor: 129n * P,
966
+ longPoolAmount: 1000000n * 10n ** 8n,
967
+ shortPoolAmount: 1000000n * 10n ** 6n,
968
+ swapImpactFactorPositive: P / 1000n,
969
+ swapImpactFactorNegative: P / 1000n,
970
+ },
971
+ });
972
+ const marketsInfoData = { [m.marketTokenAddress]: m };
973
+ const route = [{ marketAddress: m.marketTokenAddress, from: m.longTokenAddress, to: m.shortTokenAddress }];
974
+ const quote = () => (0, swapStats_1.getSwapPathStats)({
975
+ marketsInfoData,
976
+ swapPath: [m.marketTokenAddress],
977
+ initialCollateralAddress: m.longTokenAddress,
978
+ wrappedNativeTokenAddress: "WETH",
979
+ usdIn: 100n * P,
980
+ shouldUnwrapNativeToken: false,
981
+ shouldApplyPriceImpact: true,
982
+ swapPricingType: types_1.SwapPricingType.Swap,
983
+ });
984
+ const best = () => (0, swapRouting_1.getBestSwapPath)({
985
+ routes: [route],
986
+ usdIn: 100n * P,
987
+ estimator: (0, swapRouting_1.createSwapEstimator)(marketsInfoData, types_1.SwapPricingType.Swap),
988
+ });
989
+ if (tradeMath) {
990
+ (0, vitest_1.expect)(quote).toThrow(fees_1.GmsolPriceImpactError);
991
+ (0, vitest_1.expect)(best).toThrow(fees_1.GmsolPriceImpactError);
992
+ }
993
+ else {
994
+ (0, vitest_1.expect)(quote).not.toThrow();
995
+ (0, vitest_1.expect)(best()).toEqual(route);
996
+ }
997
+ m.longToken.prices = { minPrice: P, maxPrice: 2n * P };
998
+ Object.assign(m, {
999
+ swapImpactExponentFactor: 2n * P,
1000
+ longPoolAmount: 1000n * 10n ** BigInt(m.longToken.decimals),
1001
+ shortPoolAmount: 1500n * 10n ** BigInt(m.shortToken.decimals),
1002
+ swapImpactFactorPositive: P / 1000000n,
1003
+ swapImpactFactorNegative: P / 1000000n,
1004
+ longInterestUsd: 0n,
1005
+ shortInterestUsd: 0n,
1006
+ longInterestInTokens: 0n,
1007
+ shortInterestInTokens: 0n,
1008
+ });
1009
+ (0, vitest_1.expect)(quote()?.totalSwapPriceImpactDeltaUsd).toBe((-(tradeMath ? 9n : 4n) * P) / 100n);
1010
+ }
1011
+ });
1012
+ });