@gmx-io/sdk 2.3.2 → 2.6.0-next.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.
Files changed (209) hide show
  1. package/build/cjs/scripts/generate-synthetics-abis.js +17 -2
  2. package/build/cjs/src/abis/CustomErrors.js +98 -0
  3. package/build/cjs/src/abis/CustomErrorsV2_2_1.js +5565 -0
  4. package/build/cjs/src/abis/ExchangeRouterV2_2_1.js +1462 -0
  5. package/build/cjs/src/abis/GelatoRelayRouterV2_2_1.js +1945 -0
  6. package/build/cjs/src/abis/GlvReaderV2_2_1.js +1681 -0
  7. package/build/cjs/src/abis/MultichainClaimsRouterV2_2_1.js +1281 -0
  8. package/build/cjs/src/abis/MultichainGlvRouterV2_2_1.js +1176 -0
  9. package/build/cjs/src/abis/MultichainGmRouterV2_2_1.js +1480 -0
  10. package/build/cjs/src/abis/MultichainOrderRouterV2_2_1.js +2065 -0
  11. package/build/cjs/src/abis/MultichainSubaccountRouterV2_2_1.js +2160 -0
  12. package/build/cjs/src/abis/MultichainTransferRouterV2_2_1.js +1033 -0
  13. package/build/cjs/src/abis/RelayParamsV2_2_1.js +56 -0
  14. package/build/cjs/src/abis/RewardRouterV3.js +675 -0
  15. package/build/cjs/src/abis/SubaccountGelatoRelayRouterV2_2_1.js +2502 -0
  16. package/build/cjs/src/abis/SyntheticsReaderV2_2_1.js +5279 -0
  17. package/build/cjs/src/abis/__tests__/fixtures/arbitrumSepoliaReaders.json +8 -0
  18. package/build/cjs/src/abis/__tests__/getAbi.spec.js +84 -0
  19. package/build/cjs/src/abis/getAbi.js +42 -0
  20. package/build/cjs/src/abis/index.js +4 -1
  21. package/build/cjs/src/clients/v1/modules/orders/orders.js +5 -3
  22. package/build/cjs/src/clients/v1/modules/orders/transactions/createIncreaseOrderTxn.js +3 -1
  23. package/build/cjs/src/clients/v1/modules/orders/transactions/updateOrderTxn.js +21 -25
  24. package/build/cjs/src/clients/v1/modules/orders/utils.js +6 -7
  25. package/build/cjs/src/clients/v1/modules/trades/trades.js +4 -3
  26. package/build/cjs/src/clients/v1/multicall.js +2 -1
  27. package/build/cjs/src/clients/v2/__tests__/decrease.spec.js +0 -2
  28. package/build/cjs/src/clients/v2/__tests__/editCancel.spec.js +0 -3
  29. package/build/cjs/src/clients/v2/__tests__/globalCleanup.js +0 -1
  30. package/build/cjs/src/clients/v2/__tests__/increase.spec.js +0 -2
  31. package/build/cjs/src/clients/v2/__tests__/subaccountLifecycle.spec.js +0 -1
  32. package/build/cjs/src/clients/v2/__tests__/testUtil.js +0 -1
  33. package/build/cjs/src/configs/__tests__/markets.spec.js +8 -1
  34. package/build/cjs/src/configs/chains.js +16 -1
  35. package/build/cjs/src/configs/contracts.js +27 -18
  36. package/build/cjs/src/configs/dataStore.js +13 -3
  37. package/build/cjs/src/configs/markets.js +12 -7
  38. package/build/cjs/src/configs/tokens.js +12 -7
  39. package/build/cjs/src/utils/__tests__/address.spec.js +50 -0
  40. package/build/cjs/src/utils/__tests__/numbers.spec.js +24 -0
  41. package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +41 -4
  42. package/build/cjs/src/utils/__tests__/orderTransactions/updateOrder.spec.js +4 -0
  43. package/build/cjs/src/utils/__tests__/parseError.spec.js +61 -0
  44. package/build/cjs/src/utils/__tests__/referrals.spec.js +36 -4
  45. package/build/cjs/src/utils/address/index.js +17 -0
  46. package/build/cjs/src/utils/address/utils.js +32 -0
  47. package/build/cjs/src/utils/errors/parseError.js +10 -0
  48. package/build/cjs/src/utils/errors/transactionsErrors.js +23 -0
  49. package/build/cjs/src/utils/express/utils/batchOrderUtils.js +14 -9
  50. package/build/cjs/src/utils/express/utils/bridgeOutUtils.js +52 -17
  51. package/build/cjs/src/utils/express/utils/referralUtils.js +38 -25
  52. package/build/cjs/src/utils/express/utils/relayParamsUtils.js +11 -3
  53. package/build/cjs/src/utils/markets/__tests__/multicall.spec.js +27 -0
  54. package/build/cjs/src/utils/markets/multicall.js +5 -7
  55. package/build/cjs/src/utils/multichain/api.js +17 -8
  56. package/build/cjs/src/utils/numbers/utils.js +7 -1
  57. package/build/cjs/src/utils/orderTransactions/api.js +3 -4
  58. package/build/cjs/src/utils/orderTransactions/utils.js +8 -7
  59. package/build/cjs/src/utils/orders/utils.js +8 -1
  60. package/build/cjs/src/utils/paxos/__tests__/api.spec.js +23 -3
  61. package/build/cjs/src/utils/paxos/api.js +6 -0
  62. package/build/cjs/src/utils/referrals/utils.js +24 -15
  63. package/build/cjs/src/utils/subaccount/getEmptySubaccountApproval.js +2 -0
  64. package/build/cjs/src/utils/subaccount/getSubaccountApprovalTypedData.js +37 -15
  65. package/build/cjs/src/utils/subaccount/hashSubaccountApproval.js +32 -18
  66. package/build/cjs/src/utils/subaccount/index.js +4 -1
  67. package/build/cjs/src/utils/subaccount/sdkClient.js +1 -1
  68. package/build/cjs/src/utils/subaccount/state.js +10 -1
  69. package/build/cjs/src/utils/swap/preparedSwapData.js +6 -4
  70. package/build/cjs/src/utils/swap/swapPath.js +1 -1
  71. package/build/cjs/src/utils/trade/__tests__/decreaseMarginCheck.spec.js +224 -0
  72. package/build/cjs/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +128 -9
  73. package/build/cjs/src/utils/trade/decrease.js +198 -33
  74. package/build/cjs/src/utils/trade/decreaseMarginCheck.js +157 -0
  75. package/build/cjs/src/utils/trade/increaseMarginCheck.js +27 -15
  76. package/build/cjs/src/utils/trade/index.js +1 -0
  77. package/build/cjs/src/utils/tradeHistory/utils.js +6 -6
  78. package/build/esm/scripts/generate-synthetics-abis.js +17 -2
  79. package/build/esm/src/abis/CustomErrors.js +98 -0
  80. package/build/esm/src/abis/CustomErrorsV2_2_1.js +5563 -0
  81. package/build/esm/src/abis/ExchangeRouterV2_2_1.js +1460 -0
  82. package/build/esm/src/abis/GelatoRelayRouterV2_2_1.js +1943 -0
  83. package/build/esm/src/abis/GlvReaderV2_2_1.js +1679 -0
  84. package/build/esm/src/abis/MultichainClaimsRouterV2_2_1.js +1279 -0
  85. package/build/esm/src/abis/MultichainGlvRouterV2_2_1.js +1174 -0
  86. package/build/esm/src/abis/MultichainGmRouterV2_2_1.js +1478 -0
  87. package/build/esm/src/abis/MultichainOrderRouterV2_2_1.js +2063 -0
  88. package/build/esm/src/abis/MultichainSubaccountRouterV2_2_1.js +2158 -0
  89. package/build/esm/src/abis/MultichainTransferRouterV2_2_1.js +1031 -0
  90. package/build/esm/src/abis/RelayParamsV2_2_1.js +54 -0
  91. package/build/esm/src/abis/RewardRouterV3.js +673 -0
  92. package/build/esm/src/abis/SubaccountGelatoRelayRouterV2_2_1.js +2500 -0
  93. package/build/esm/src/abis/SyntheticsReaderV2_2_1.js +5277 -0
  94. package/build/esm/src/abis/__tests__/fixtures/arbitrumSepoliaReaders.json +8 -0
  95. package/build/esm/src/abis/__tests__/getAbi.spec.js +79 -0
  96. package/build/esm/src/abis/getAbi.js +35 -0
  97. package/build/esm/src/abis/index.js +4 -1
  98. package/build/esm/src/clients/v1/modules/orders/orders.js +5 -3
  99. package/build/esm/src/clients/v1/modules/orders/transactions/createIncreaseOrderTxn.js +3 -1
  100. package/build/esm/src/clients/v1/modules/orders/transactions/updateOrderTxn.js +21 -25
  101. package/build/esm/src/clients/v1/modules/orders/utils.js +6 -7
  102. package/build/esm/src/clients/v1/modules/trades/trades.js +4 -3
  103. package/build/esm/src/clients/v1/multicall.js +2 -1
  104. package/build/esm/src/clients/v2/__tests__/decrease.spec.js +0 -2
  105. package/build/esm/src/clients/v2/__tests__/editCancel.spec.js +0 -3
  106. package/build/esm/src/clients/v2/__tests__/globalCleanup.js +0 -1
  107. package/build/esm/src/clients/v2/__tests__/increase.spec.js +0 -2
  108. package/build/esm/src/clients/v2/__tests__/subaccountLifecycle.spec.js +0 -1
  109. package/build/esm/src/clients/v2/__tests__/testUtil.js +0 -1
  110. package/build/esm/src/configs/__tests__/markets.spec.js +9 -2
  111. package/build/esm/src/configs/chains.js +14 -0
  112. package/build/esm/src/configs/contracts.js +25 -17
  113. package/build/esm/src/configs/dataStore.js +8 -0
  114. package/build/esm/src/configs/markets.js +13 -8
  115. package/build/esm/src/configs/tokens.js +13 -8
  116. package/build/esm/src/utils/__tests__/address.spec.js +48 -0
  117. package/build/esm/src/utils/__tests__/numbers.spec.js +25 -1
  118. package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +43 -6
  119. package/build/esm/src/utils/__tests__/orderTransactions/updateOrder.spec.js +5 -1
  120. package/build/esm/src/utils/__tests__/parseError.spec.js +62 -1
  121. package/build/esm/src/utils/__tests__/referrals.spec.js +37 -5
  122. package/build/esm/src/utils/address/index.js +1 -0
  123. package/build/esm/src/utils/address/utils.js +25 -0
  124. package/build/esm/src/utils/errors/parseError.js +11 -1
  125. package/build/esm/src/utils/errors/transactionsErrors.js +23 -0
  126. package/build/esm/src/utils/express/utils/batchOrderUtils.js +14 -9
  127. package/build/esm/src/utils/express/utils/bridgeOutUtils.js +53 -18
  128. package/build/esm/src/utils/express/utils/referralUtils.js +37 -25
  129. package/build/esm/src/utils/express/utils/relayParamsUtils.js +10 -5
  130. package/build/esm/src/utils/markets/__tests__/multicall.spec.js +25 -0
  131. package/build/esm/src/utils/markets/multicall.js +5 -7
  132. package/build/esm/src/utils/multichain/api.js +15 -7
  133. package/build/esm/src/utils/numbers/utils.js +5 -0
  134. package/build/esm/src/utils/orderTransactions/api.js +3 -4
  135. package/build/esm/src/utils/orderTransactions/utils.js +9 -8
  136. package/build/esm/src/utils/orders/utils.js +6 -0
  137. package/build/esm/src/utils/paxos/__tests__/api.spec.js +23 -3
  138. package/build/esm/src/utils/paxos/api.js +6 -0
  139. package/build/esm/src/utils/referrals/utils.js +23 -15
  140. package/build/esm/src/utils/subaccount/getEmptySubaccountApproval.js +2 -0
  141. package/build/esm/src/utils/subaccount/getSubaccountApprovalTypedData.js +36 -14
  142. package/build/esm/src/utils/subaccount/hashSubaccountApproval.js +32 -18
  143. package/build/esm/src/utils/subaccount/index.js +2 -2
  144. package/build/esm/src/utils/subaccount/sdkClient.js +2 -2
  145. package/build/esm/src/utils/subaccount/state.js +8 -0
  146. package/build/esm/src/utils/swap/preparedSwapData.js +6 -4
  147. package/build/esm/src/utils/swap/swapPath.js +2 -2
  148. package/build/esm/src/utils/trade/__tests__/decreaseMarginCheck.spec.js +222 -0
  149. package/build/esm/src/utils/trade/__tests__/getMaxWithdrawAmount.spec.js +129 -10
  150. package/build/esm/src/utils/trade/decrease.js +195 -32
  151. package/build/esm/src/utils/trade/decreaseMarginCheck.js +152 -0
  152. package/build/esm/src/utils/trade/increaseMarginCheck.js +25 -14
  153. package/build/esm/src/utils/trade/index.js +1 -0
  154. package/build/esm/src/utils/tradeHistory/utils.js +6 -6
  155. package/build/tsconfig.tsbuildinfo +1 -1
  156. package/build/types/scripts/generate-synthetics-abis.d.ts +6 -0
  157. package/build/types/src/abis/CustomErrors.d.ts +160 -0
  158. package/build/types/src/abis/CustomErrorsV2_2_1.d.ts +4174 -0
  159. package/build/types/src/abis/ExchangeRouterV2_2_1.d.ts +1128 -0
  160. package/build/types/src/abis/GelatoRelayRouterV2_2_1.d.ts +1508 -0
  161. package/build/types/src/abis/GlvReaderV2_2_1.d.ts +1293 -0
  162. package/build/types/src/abis/MultichainClaimsRouterV2_2_1.d.ts +985 -0
  163. package/build/types/src/abis/MultichainGlvRouterV2_2_1.d.ts +906 -0
  164. package/build/types/src/abis/MultichainGmRouterV2_2_1.d.ts +1142 -0
  165. package/build/types/src/abis/MultichainOrderRouterV2_2_1.d.ts +1601 -0
  166. package/build/types/src/abis/MultichainSubaccountRouterV2_2_1.d.ts +1678 -0
  167. package/build/types/src/abis/MultichainTransferRouterV2_2_1.d.ts +793 -0
  168. package/build/types/src/abis/RelayParamsV2_2_1.d.ts +118 -0
  169. package/build/types/src/abis/RewardRouterV3.d.ts +1129 -0
  170. package/build/types/src/abis/SubaccountGelatoRelayRouterV2_2_1.d.ts +1946 -0
  171. package/build/types/src/abis/SyntheticsReaderV2_2_1.d.ts +4075 -0
  172. package/build/types/src/abis/__tests__/getAbi.spec.d.ts +1 -0
  173. package/build/types/src/abis/getAbi.d.ts +17051 -0
  174. package/build/types/src/abis/index.d.ts +6700 -4744
  175. package/build/types/src/clients/v1/modules/orders/transactions/createIncreaseOrderTxn.d.ts +2 -1
  176. package/build/types/src/clients/v1/modules/orders/transactions/updateOrderTxn.d.ts +8 -3
  177. package/build/types/src/configs/chains.d.ts +6 -2
  178. package/build/types/src/configs/contracts.d.ts +6 -0
  179. package/build/types/src/configs/dataStore.d.ts +4 -0
  180. package/build/types/src/configs/markets.d.ts +4 -4
  181. package/build/types/src/utils/__tests__/address.spec.d.ts +1 -0
  182. package/build/types/src/utils/address/index.d.ts +1 -0
  183. package/build/types/src/utils/address/utils.d.ts +11 -0
  184. package/build/types/src/utils/errors/transactionsErrors.d.ts +18 -1
  185. package/build/types/src/utils/express/types.d.ts +1 -0
  186. package/build/types/src/utils/express/utils/batchOrderUtils.d.ts +4 -3
  187. package/build/types/src/utils/express/utils/bridgeOutUtils.d.ts +8 -2
  188. package/build/types/src/utils/express/utils/referralUtils.d.ts +21 -0
  189. package/build/types/src/utils/markets/__tests__/multicall.spec.d.ts +1 -0
  190. package/build/types/src/utils/multichain/api.d.ts +8 -0
  191. package/build/types/src/utils/numbers/utils.d.ts +1 -0
  192. package/build/types/src/utils/orderTransactions/api.d.ts +1 -3
  193. package/build/types/src/utils/orderTransactions/utils.d.ts +5 -2
  194. package/build/types/src/utils/orders/utils.d.ts +1 -0
  195. package/build/types/src/utils/paxos/types.d.ts +2 -0
  196. package/build/types/src/utils/referrals/utils.d.ts +7 -1
  197. package/build/types/src/utils/subaccount/api.d.ts +1 -0
  198. package/build/types/src/utils/subaccount/getSubaccountApprovalTypedData.d.ts +4 -0
  199. package/build/types/src/utils/subaccount/hashSubaccountApproval.d.ts +2 -1
  200. package/build/types/src/utils/subaccount/index.d.ts +2 -2
  201. package/build/types/src/utils/subaccount/state.d.ts +2 -1
  202. package/build/types/src/utils/subaccount/types.d.ts +3 -1
  203. package/build/types/src/utils/trade/__tests__/decreaseMarginCheck.spec.d.ts +1 -0
  204. package/build/types/src/utils/trade/decrease.d.ts +18 -0
  205. package/build/types/src/utils/trade/decreaseMarginCheck.d.ts +52 -0
  206. package/build/types/src/utils/trade/increaseMarginCheck.d.ts +11 -1
  207. package/build/types/src/utils/trade/index.d.ts +1 -0
  208. package/build/types/src/utils/trade/types.d.ts +1 -0
  209. package/package.json +1 -1
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const mock_1 = require("../../../test/mock");
5
+ const numbers_1 = require("../../numbers");
6
+ const tokens_1 = require("../../tokens");
7
+ const decreaseMarginCheck_1 = require("../decreaseMarginCheck");
8
+ const increaseMarginCheck_1 = require("../increaseMarginCheck");
9
+ const tokensData = (0, mock_1.mockTokensData)();
10
+ const usdc = tokensData.USDC;
11
+ /** 1% min collateral factor → 100x, 0.5% for liquidation. Fees and impact off unless a test needs them. */
12
+ function buildMarket(overrides = {}) {
13
+ return (0, mock_1.mockMarketsInfoData)(tokensData, ["BTC-BTC-USDC"], {
14
+ "BTC-BTC-USDC": {
15
+ minCollateralFactor: (0, numbers_1.expandDecimals)(1, 28),
16
+ minCollateralFactorForLiquidation: (0, numbers_1.expandDecimals)(5, 27),
17
+ minCollateralFactorForOpenInterestLong: 0n,
18
+ minCollateralFactorForOpenInterestShort: 0n,
19
+ positionFeeFactorForBalanceWasImproved: 0n,
20
+ positionFeeFactorForBalanceWasNotImproved: 0n,
21
+ positionImpactFactorPositive: 0n,
22
+ positionImpactFactorNegative: 0n,
23
+ maxPositionImpactFactorPositive: 0n,
24
+ maxPositionImpactFactorNegative: 0n,
25
+ maxPositionImpactFactorForLiquidations: 0n,
26
+ longInterestUsd: (0, numbers_1.expandDecimals)(1000000, numbers_1.USD_DECIMALS),
27
+ shortInterestUsd: (0, numbers_1.expandDecimals)(1000000, numbers_1.USD_DECIMALS),
28
+ longInterestInTokens: (0, numbers_1.expandDecimals)(50, 8),
29
+ shortInterestInTokens: (0, numbers_1.expandDecimals)(50, 8),
30
+ ...overrides,
31
+ },
32
+ })["BTC-BTC-USDC"];
33
+ }
34
+ /** USD value with 30 decimals, accepting fractional amounts. */
35
+ function usd(value) {
36
+ return (BigInt(Math.round(value * 100)) * (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS)) / 100n;
37
+ }
38
+ function usdcAmount(value) {
39
+ return (0, tokens_1.convertToTokenAmount)(usd(value), usdc.decimals, usdc.prices.minPrice);
40
+ }
41
+ /** tokens of a 10 000 USD long that are worth `value` at the oracle price */
42
+ function sizeInTokensWorth(value) {
43
+ return (0, tokens_1.convertToTokenAmount)(usd(value), tokensData.BTC.decimals, tokensData.BTC.prices.minPrice);
44
+ }
45
+ const LOSING_2000 = sizeInTokensWorth(8000);
46
+ const FLAT = sizeInTokensWorth(10000);
47
+ const PROFITABLE_2000 = sizeInTokensWorth(12000);
48
+ /**
49
+ * A 10 000 USD long. `closeShare` closes that share of it and pays `realizedLossUsd` out of the collateral,
50
+ * the way `getDecreasePositionAmounts` reports it in `payedRemainingCollateralAmount`.
51
+ */
52
+ function params({ position, closeShare = 0, realizedLossUsd = 0, ...overrides } = {}) {
53
+ const fullPosition = {
54
+ sizeInUsd: usd(10000),
55
+ sizeInTokens: LOSING_2000,
56
+ collateralAmount: usdcAmount(2060),
57
+ pendingImpactAmount: 0n,
58
+ ...position,
59
+ };
60
+ const closeBps = BigInt(Math.round(closeShare * 10000));
61
+ return {
62
+ marketInfo: buildMarket(),
63
+ collateralToken: usdc,
64
+ isLong: true,
65
+ position: fullPosition,
66
+ sizeDeltaUsd: (fullPosition.sizeInUsd * closeBps) / 10000n,
67
+ sizeDeltaInTokens: (fullPosition.sizeInTokens * closeBps) / 10000n,
68
+ collateralDeltaAmount: 0n,
69
+ payedRemainingCollateralAmount: usdcAmount(realizedLossUsd),
70
+ priceImpactDiffUsd: 0n,
71
+ minCollateralUsd: usd(1),
72
+ minPositionSizeUsd: usd(1),
73
+ userReferralInfo: undefined,
74
+ ...overrides,
75
+ };
76
+ }
77
+ (0, vitest_1.describe)("getDecreaseResultingPositionMarginState — partial close", () => {
78
+ // collateral 2 060 with a 2 000 loss leaves 60 of margin: under the 1% regular minimum of the full
79
+ // position (100), above its 0.5% liquidation minimum (50)
80
+ (0, vitest_1.it)("rejects a partial close whose remainder stays above max leverage", () => {
81
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 0.25, realizedLossUsd: 500 }));
82
+ (0, vitest_1.expect)(state?.isLiquidatable).toBe(true);
83
+ (0, vitest_1.expect)(state?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.MinCollateralForLeverage);
84
+ (0, vitest_1.expect)(state?.remainingCollateralUsd).toBe(usd(60));
85
+ (0, vitest_1.expect)(state?.minCollateralUsdForLeverage).toBe(usd(75));
86
+ });
87
+ (0, vitest_1.it)("passes when the remaining margin equals the leverage-based minimum", () => {
88
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 0.4, realizedLossUsd: 800 }));
89
+ (0, vitest_1.expect)(state?.remainingCollateralUsd).toBe(usd(60));
90
+ (0, vitest_1.expect)(state?.minCollateralUsdForLeverage).toBe(usd(60));
91
+ (0, vitest_1.expect)(state?.isLiquidatable).toBe(false);
92
+ });
93
+ (0, vitest_1.it)("passes once a larger part is closed", () => {
94
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 0.5, realizedLossUsd: 1000 }));
95
+ (0, vitest_1.expect)(state?.isLiquidatable).toBe(false);
96
+ });
97
+ (0, vitest_1.it)("never validates a full close", () => {
98
+ (0, vitest_1.expect)((0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 1, realizedLossUsd: 2000 }))).toBeUndefined();
99
+ });
100
+ (0, vitest_1.it)("counts the costs paid out of the collateral against the remainder", () => {
101
+ // closing 50% passes with 60 of margin against a 50 minimum; 10.01 of fees on the closed part tips it over
102
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 0.5, realizedLossUsd: 1010.01 }));
103
+ (0, vitest_1.expect)(state?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.MinCollateralForLeverage);
104
+ });
105
+ (0, vitest_1.it)("charges the closing fee of the remainder", () => {
106
+ // 0.2% of the remaining 5 000 = 10 → 60 - 10 = 50, exactly the minimum; one more cent of loss fails
107
+ const marketInfo = buildMarket({
108
+ positionFeeFactorForBalanceWasImproved: (0, numbers_1.expandDecimals)(2, 27),
109
+ positionFeeFactorForBalanceWasNotImproved: (0, numbers_1.expandDecimals)(2, 27),
110
+ });
111
+ (0, vitest_1.expect)((0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ marketInfo, closeShare: 0.5, realizedLossUsd: 1000 }))
112
+ ?.isLiquidatable).toBe(false);
113
+ (0, vitest_1.expect)((0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ marketInfo, closeShare: 0.5, realizedLossUsd: 1000.01 }))
114
+ ?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.MinCollateralForLeverage);
115
+ });
116
+ (0, vitest_1.it)("does not apply the fixed minimum to the remainder", () => {
117
+ // remainder: 60 of margin, 50 leverage minimum; a 61 USD fixed minimum is only checked after an increase
118
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({
119
+ closeShare: 0.5,
120
+ realizedLossUsd: 1000,
121
+ position: { collateralAmount: usdcAmount(2061) },
122
+ minCollateralUsd: usd(61),
123
+ marketInfo: buildMarket({
124
+ positionFeeFactorForBalanceWasImproved: (0, numbers_1.expandDecimals)(2, 26),
125
+ positionFeeFactorForBalanceWasNotImproved: (0, numbers_1.expandDecimals)(2, 26),
126
+ }),
127
+ }));
128
+ (0, vitest_1.expect)(state?.remainingCollateralUsd).toBe(usd(60));
129
+ (0, vitest_1.expect)(state?.isLiquidatable).toBe(false);
130
+ });
131
+ (0, vitest_1.it)("returns a non-positive remaining margin as its own reason", () => {
132
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 0.25, realizedLossUsd: 500, position: { collateralAmount: usdcAmount(2002) } }));
133
+ // 2 of margin: over the 1 USD fixed minimum, so the contract keeps the close partial
134
+ (0, vitest_1.expect)(state?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.MinCollateralForLeverage);
135
+ const underwater = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({
136
+ closeShare: 0.25,
137
+ realizedLossUsd: 503,
138
+ position: { collateralAmount: usdcAmount(2002) },
139
+ }));
140
+ (0, vitest_1.expect)(underwater?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.NonPositiveRemainingMargin);
141
+ });
142
+ (0, vitest_1.it)("validates nothing when the contract turns the order into a full close", () => {
143
+ // estimated remainder: 2 000.5 - 500 of collateral and -1 500 of pnl → 0.5, under the 1 USD fixed minimum
144
+ (0, vitest_1.expect)((0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 0.25, realizedLossUsd: 500, position: { collateralAmount: usdcAmount(2000.5) } }))).toBeUndefined();
145
+ (0, vitest_1.expect)((0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 0.5, realizedLossUsd: 1000, minPositionSizeUsd: usd(5001) }))).toBeUndefined();
146
+ });
147
+ });
148
+ (0, vitest_1.describe)("getDecreaseResultingPositionMarginState — keep-leverage collateral withdrawal", () => {
149
+ const flat = { sizeInTokens: FLAT, collateralAmount: usdcAmount(200) };
150
+ (0, vitest_1.it)("withdraws the collateral when the preliminary gate passes", () => {
151
+ // remaining size 5 000 → 50 required without pnl; 200 - 150 = 50
152
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 0.5, position: flat, collateralDeltaAmount: usdcAmount(150) }));
153
+ (0, vitest_1.expect)(state?.remainingCollateralUsd).toBe(usd(50));
154
+ (0, vitest_1.expect)(state?.isLiquidatable).toBe(false);
155
+ });
156
+ (0, vitest_1.it)("keeps the collateral in the position when the preliminary gate fails", () => {
157
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ closeShare: 0.5, position: flat, collateralDeltaAmount: usdcAmount(150.01) }));
158
+ (0, vitest_1.expect)(state?.remainingCollateralUsd).toBe(usd(200));
159
+ (0, vitest_1.expect)(state?.isLiquidatable).toBe(false);
160
+ });
161
+ (0, vitest_1.it)("trims the withdrawal by the negative impact past the cap, as the contract does", () => {
162
+ // 30 of the close's negative impact is past the cap: it is paid out of the collateral and the contract
163
+ // withdraws 150 - 30, so 200 - 30 - 120 = 50 stays against the 50 required
164
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({
165
+ closeShare: 0.5,
166
+ position: flat,
167
+ collateralDeltaAmount: usdcAmount(150),
168
+ payedRemainingCollateralAmount: usdcAmount(30),
169
+ priceImpactDiffUsd: usd(30),
170
+ }));
171
+ (0, vitest_1.expect)(state?.remainingCollateralUsd).toBe(usd(50));
172
+ (0, vitest_1.expect)(state?.isLiquidatable).toBe(false);
173
+ });
174
+ (0, vitest_1.it)("evaluates the open-interest factor on the open interest left after the close", () => {
175
+ // 2% at 1 000 000 of open interest; closing 5 000 leaves 995 000 → 1.99% → 99.5 on the remaining 5 000
176
+ const marketInfo = buildMarket({ minCollateralFactorForOpenInterestLong: (0, numbers_1.expandDecimals)(2, 22) });
177
+ (0, vitest_1.expect)((0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ marketInfo, closeShare: 0.5, position: flat, collateralDeltaAmount: usdcAmount(100.5) }))?.remainingCollateralUsd).toBe(usd(99.5));
178
+ (0, vitest_1.expect)((0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ marketInfo, closeShare: 0.5, position: flat, collateralDeltaAmount: usdcAmount(100.51) }))?.remainingCollateralUsd).toBe(usd(200));
179
+ });
180
+ });
181
+ (0, vitest_1.describe)("getDecreaseResultingPositionMarginState — withdrawal", () => {
182
+ (0, vitest_1.it)("passes up to the leverage-based minimum with pnl and fails past it", () => {
183
+ // 2 200 of collateral and a 2 000 loss: 200 of margin against a 100 minimum
184
+ const position = { collateralAmount: usdcAmount(2200) };
185
+ const atThreshold = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ position, collateralDeltaAmount: usdcAmount(100) }));
186
+ const pastThreshold = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ position, collateralDeltaAmount: usdcAmount(100.01) }));
187
+ (0, vitest_1.expect)(atThreshold?.remainingCollateralUsd).toBe(usd(100));
188
+ (0, vitest_1.expect)(atThreshold?.isLiquidatable).toBe(false);
189
+ (0, vitest_1.expect)(pastThreshold?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.MinCollateralForLeverage);
190
+ });
191
+ (0, vitest_1.it)("uses the regular factor where the liquidation one would still pass", () => {
192
+ // 60 of margin left: above the 0.5% liquidation minimum (50), under the 1% regular one (100)
193
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ position: { collateralAmount: usdcAmount(2200) }, collateralDeltaAmount: usdcAmount(140) }));
194
+ (0, vitest_1.expect)(state?.remainingCollateralUsd).toBe(usd(60));
195
+ (0, vitest_1.expect)(state?.minCollateralUsdForLeverage).toBe(usd(100));
196
+ (0, vitest_1.expect)(state?.isLiquidatable).toBe(true);
197
+ });
198
+ (0, vitest_1.it)("settles pending fees out of the collateral before the final validation", () => {
199
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({
200
+ position: { collateralAmount: usdcAmount(2200) },
201
+ collateralDeltaAmount: usdcAmount(95),
202
+ payedRemainingCollateralAmount: usdcAmount(5.01),
203
+ }));
204
+ (0, vitest_1.expect)(state?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.MinCollateralForLeverage);
205
+ });
206
+ (0, vitest_1.it)("reverts at the preliminary gate, which ignores pnl", () => {
207
+ // profitable by 2 000, but 150 - 50.01 of collateral is under the 100 the gate requires without pnl
208
+ const position = { sizeInTokens: PROFITABLE_2000, collateralAmount: usdcAmount(150) };
209
+ (0, vitest_1.expect)((0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ position, collateralDeltaAmount: usdcAmount(50) }))
210
+ ?.isLiquidatable).toBe(false);
211
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({ position, collateralDeltaAmount: usdcAmount(50.01) }));
212
+ (0, vitest_1.expect)(state?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.UnableToWithdrawCollateral);
213
+ (0, vitest_1.expect)(state?.remainingCollateralUsd).toBe(usd(99.99));
214
+ (0, vitest_1.expect)(state?.minCollateralUsdForLeverage).toBe(usd(100));
215
+ });
216
+ (0, vitest_1.it)("reverts at the preliminary gate when collateral with pnl falls under the fixed minimum", () => {
217
+ const state = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)(params({
218
+ position: { collateralAmount: usdcAmount(2200) },
219
+ collateralDeltaAmount: usdcAmount(50),
220
+ minCollateralUsd: usd(150.01),
221
+ }));
222
+ (0, vitest_1.expect)(state?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.UnableToWithdrawCollateral);
223
+ });
224
+ });
@@ -2,15 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vitest_1 = require("vitest");
4
4
  const mock_1 = require("../../../test/mock");
5
+ const markets_1 = require("../../markets");
5
6
  const numbers_1 = require("../../numbers");
6
7
  const positions_1 = require("../../positions");
7
8
  const decrease_1 = require("../decrease");
9
+ const decreaseMarginCheck_1 = require("../decreaseMarginCheck");
10
+ const increaseMarginCheck_1 = require("../increaseMarginCheck");
8
11
  (0, vitest_1.describe)("getMaxWithdrawAmount", () => {
9
- const ETH_PRICE = (0, numbers_1.expandDecimals)(11952, 29); // $1195.20
12
+ const LOSING_ETH_PRICE = (0, numbers_1.expandDecimals)(11952, 29); // $1195.20
13
+ const PROFITABLE_ETH_PRICE = (0, numbers_1.expandDecimals)(1250, 30);
10
14
  const USDC_PRICE = (0, numbers_1.expandDecimals)(1, 30);
11
- function buildScenario() {
15
+ function buildScenario({ ethPrice = LOSING_ETH_PRICE, minCollateralFactorForLiquidation = (0, numbers_1.expandDecimals)(4, 27), } = {}) {
12
16
  const tokensData = (0, mock_1.mockTokensData)({
13
- ETH: { prices: { minPrice: ETH_PRICE, maxPrice: ETH_PRICE } },
17
+ ETH: { prices: { minPrice: ethPrice, maxPrice: ethPrice } },
14
18
  USDC: { prices: { minPrice: USDC_PRICE, maxPrice: USDC_PRICE } },
15
19
  });
16
20
  const marketKey = "ETH-ETH-USDC";
@@ -21,7 +25,7 @@ const decrease_1 = require("../decrease");
21
25
  longInterestInTokens: (0, mock_1.usdToToken)(100000000, tokensData.ETH),
22
26
  shortInterestInTokens: (0, mock_1.usdToToken)(100000000, tokensData.ETH),
23
27
  minCollateralFactor: (0, numbers_1.expandDecimals)(5, 27),
24
- minCollateralFactorForLiquidation: (0, numbers_1.expandDecimals)(4, 27),
28
+ minCollateralFactorForLiquidation,
25
29
  minCollateralFactorForOpenInterestLong: 0n,
26
30
  minCollateralFactorForOpenInterestShort: 0n,
27
31
  positionFeeFactorForBalanceWasNotImproved: (0, numbers_1.expandDecimals)(5, 26),
@@ -36,6 +40,7 @@ const decrease_1 = require("../decrease");
36
40
  const collateralUsd = (0, numbers_1.expandDecimals)(4000, numbers_1.USD_DECIMALS);
37
41
  const sizeInTokens = (sizeInUsd * (0, numbers_1.expandDecimals)(1, ethToken.decimals)) / (0, numbers_1.expandDecimals)(1200, numbers_1.USD_DECIMALS);
38
42
  const collateralAmount = (collateralUsd * (0, numbers_1.expandDecimals)(1, usdcToken.decimals)) / USDC_PRICE;
43
+ const pnl = (sizeInTokens * ethPrice) / (0, numbers_1.expandDecimals)(1, ethToken.decimals) - sizeInUsd;
39
44
  const position = {
40
45
  key: "test-position",
41
46
  contractKey: "test-position",
@@ -52,7 +57,7 @@ const decrease_1 = require("../decrease");
52
57
  fundingFeeAmount: 0n,
53
58
  claimableLongTokenAmount: 0n,
54
59
  claimableShortTokenAmount: 0n,
55
- pnl: -(0, numbers_1.expandDecimals)(200, numbers_1.USD_DECIMALS),
60
+ pnl,
56
61
  positionFeeAmount: 0n,
57
62
  traderDiscountAmount: 0n,
58
63
  uiFeeAmount: 0n,
@@ -67,7 +72,7 @@ const decrease_1 = require("../decrease");
67
72
  poolName: "USDC",
68
73
  collateralToken: usdcToken,
69
74
  pnlToken: usdcToken,
70
- markPrice: ETH_PRICE,
75
+ markPrice: ethPrice,
71
76
  entryPrice: (0, numbers_1.expandDecimals)(1200, numbers_1.USD_DECIMALS),
72
77
  liquidationPrice: undefined,
73
78
  collateralUsd,
@@ -75,7 +80,7 @@ const decrease_1 = require("../decrease");
75
80
  remainingCollateralAmount: collateralAmount,
76
81
  hasLowCollateral: false,
77
82
  pnlPercentage: 0n,
78
- pnlAfterFees: -(0, numbers_1.expandDecimals)(200, numbers_1.USD_DECIMALS),
83
+ pnlAfterFees: pnl,
79
84
  pnlAfterFeesPercentage: 0n,
80
85
  pendingFundingFeesUsd: 0n,
81
86
  pendingClaimableFundingFeesUsd: 0n,
@@ -118,8 +123,8 @@ const decrease_1 = require("../decrease");
118
123
  (0, vitest_1.expect)((0, numbers_1.applyFactor)(position.sizeInUsd, position.marketInfo.minCollateralFactorForLiquidation)).toBeGreaterThan(0n);
119
124
  (0, vitest_1.expect)((0, numbers_1.applyFactor)(position.sizeInUsd, position.marketInfo.minCollateralFactor)).toBeGreaterThan(0n);
120
125
  });
121
- (0, vitest_1.it)("max withdraw equals the collateral above the min required collateral", () => {
122
- const { position, usdcToken } = buildScenario();
126
+ (0, vitest_1.it)("max withdraw equals the collateral above the min required collateral where both factors are equal", () => {
127
+ const { position, usdcToken } = buildScenario({ minCollateralFactorForLiquidation: (0, numbers_1.expandDecimals)(5, 27) });
123
128
  const minCollateralUsd = (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS);
124
129
  const collateralPrice = usdcToken.prices.minPrice;
125
130
  const minRequiredCollateralUsd = (0, decrease_1.getMinRequiredCollateralUsdForPosition)({
@@ -162,4 +167,118 @@ const decrease_1 = require("../decrease");
162
167
  userReferralInfo: undefined,
163
168
  })).toBe(0n);
164
169
  });
170
+ (0, vitest_1.it)("returns zero instead of throwing when the max allowed leverage rounds down to zero", () => {
171
+ const { position, usdcToken } = buildScenario();
172
+ // a 25% min collateral factor allows under 5x, and the max allowed leverage is floored to 5x steps
173
+ const lowLeveragePosition = {
174
+ ...position,
175
+ marketInfo: { ...position.marketInfo, minCollateralFactor: (0, numbers_1.expandDecimals)(25, 28) },
176
+ };
177
+ (0, vitest_1.expect)((0, decrease_1.getMaxWithdrawAmount)({
178
+ position: lowLeveragePosition,
179
+ minCollateralUsd: (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS),
180
+ collateralPrice: usdcToken.prices.minPrice,
181
+ collateralDecimals: usdcToken.decimals,
182
+ userReferralInfo: undefined,
183
+ })).toBe(0n);
184
+ });
185
+ (0, vitest_1.it)("reserves the regular-factor minimum where it is above the liquidation one", () => {
186
+ const { position, usdcToken } = buildScenario();
187
+ const minCollateralUsd = (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS);
188
+ const collateralPrice = usdcToken.prices.minPrice;
189
+ const maxWithdraw = (0, decrease_1.getMaxWithdrawAmount)({
190
+ position,
191
+ minCollateralUsd,
192
+ collateralPrice,
193
+ collateralDecimals: usdcToken.decimals,
194
+ userReferralInfo: undefined,
195
+ });
196
+ const maxWithdrawUsd = (maxWithdraw * collateralPrice) / (0, numbers_1.expandDecimals)(1, usdcToken.decimals);
197
+ const liquidationBasedMaxUsd = position.collateralUsd -
198
+ (0, decrease_1.getMinRequiredCollateralUsdForPosition)({ position, minCollateralUsd, userReferralInfo: undefined });
199
+ const factorGapUsd = (0, numbers_1.applyFactor)(position.sizeInUsd, position.marketInfo.minCollateralFactor - position.marketInfo.minCollateralFactorForLiquidation);
200
+ const reservedOnTopUsd = liquidationBasedMaxUsd - maxWithdrawUsd;
201
+ (0, vitest_1.expect)(factorGapUsd).toBe((0, numbers_1.expandDecimals)(50, numbers_1.USD_DECIMALS));
202
+ (0, vitest_1.expect)(reservedOnTopUsd).toBeGreaterThanOrEqual(factorGapUsd);
203
+ (0, vitest_1.expect)(reservedOnTopUsd - factorGapUsd).toBeLessThan((0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS - usdcToken.decimals));
204
+ });
205
+ vitest_1.it.each([
206
+ ["losing", LOSING_ETH_PRICE],
207
+ ["profitable", PROFITABLE_ETH_PRICE],
208
+ ])("is the largest withdrawal the contract check accepts for a %s position", (_name, ethPrice) => {
209
+ const { position, usdcToken } = buildScenario({ ethPrice });
210
+ const minCollateralUsd = (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS);
211
+ const maxWithdraw = (0, decrease_1.getMaxWithdrawAmount)({
212
+ position,
213
+ minCollateralUsd,
214
+ collateralPrice: usdcToken.prices.minPrice,
215
+ collateralDecimals: usdcToken.decimals,
216
+ userReferralInfo: undefined,
217
+ });
218
+ const getMarginState = (collateralDeltaAmount) => (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)({
219
+ marketInfo: position.marketInfo,
220
+ collateralToken: position.collateralToken,
221
+ isLong: position.isLong,
222
+ position,
223
+ sizeDeltaUsd: 0n,
224
+ sizeDeltaInTokens: 0n,
225
+ collateralDeltaAmount,
226
+ payedRemainingCollateralAmount: 0n,
227
+ priceImpactDiffUsd: 0n,
228
+ minCollateralUsd,
229
+ minPositionSizeUsd: (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS),
230
+ userReferralInfo: undefined,
231
+ });
232
+ (0, vitest_1.expect)(maxWithdraw).toBeGreaterThan(0n);
233
+ (0, vitest_1.expect)(getMarginState(maxWithdraw)?.isLiquidatable).toBe(false);
234
+ });
235
+ (0, vitest_1.it)("stops at the contract check for a losing position", () => {
236
+ const { position, usdcToken } = buildScenario();
237
+ const minCollateralUsd = (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS);
238
+ const maxWithdraw = (0, decrease_1.getMaxWithdrawAmount)({
239
+ position,
240
+ minCollateralUsd,
241
+ collateralPrice: usdcToken.prices.minPrice,
242
+ collateralDecimals: usdcToken.decimals,
243
+ userReferralInfo: undefined,
244
+ });
245
+ const marginState = (0, decreaseMarginCheck_1.getDecreaseResultingPositionMarginState)({
246
+ marketInfo: position.marketInfo,
247
+ collateralToken: position.collateralToken,
248
+ isLong: position.isLong,
249
+ position,
250
+ sizeDeltaUsd: 0n,
251
+ sizeDeltaInTokens: 0n,
252
+ collateralDeltaAmount: maxWithdraw + 1n,
253
+ payedRemainingCollateralAmount: 0n,
254
+ priceImpactDiffUsd: 0n,
255
+ minCollateralUsd,
256
+ minPositionSizeUsd: (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS),
257
+ userReferralInfo: undefined,
258
+ });
259
+ (0, vitest_1.expect)(marginState?.reason).toBe(increaseMarginCheck_1.PositionMarginFailureReason.MinCollateralForLeverage);
260
+ });
261
+ (0, vitest_1.it)("stops at the max allowed leverage for a profitable position", () => {
262
+ const { position, usdcToken } = buildScenario({ ethPrice: PROFITABLE_ETH_PRICE });
263
+ const collateralPrice = usdcToken.prices.minPrice;
264
+ const maxWithdraw = (0, decrease_1.getMaxWithdrawAmount)({
265
+ position,
266
+ minCollateralUsd: (0, numbers_1.expandDecimals)(1, numbers_1.USD_DECIMALS),
267
+ collateralPrice,
268
+ collateralDecimals: usdcToken.decimals,
269
+ userReferralInfo: undefined,
270
+ });
271
+ const maxAllowedLeverage = BigInt((0, markets_1.getMaxAllowedLeverage)({
272
+ marketAddress: position.marketInfo.marketTokenAddress,
273
+ minCollateralFactor: position.marketInfo.minCollateralFactor,
274
+ minCollateralFactorForLiquidation: position.marketInfo.minCollateralFactorForLiquidation,
275
+ positionFeeFactorForBalanceWasNotImproved: position.marketInfo.positionFeeFactorForBalanceWasNotImproved,
276
+ }));
277
+ const getNextLeverage = (withdrawAmount) => {
278
+ const withdrawUsd = (withdrawAmount * collateralPrice) / (0, numbers_1.expandDecimals)(1, usdcToken.decimals);
279
+ return (position.sizeInUsd * numbers_1.BASIS_POINTS_DIVISOR_BIGINT) / (position.collateralUsd - withdrawUsd);
280
+ };
281
+ (0, vitest_1.expect)(getNextLeverage(maxWithdraw)).toBe(maxAllowedLeverage);
282
+ (0, vitest_1.expect)(getNextLeverage(maxWithdraw + (0, numbers_1.expandDecimals)(1, usdcToken.decimals))).toBeGreaterThan(maxAllowedLeverage);
283
+ });
165
284
  });