@gainsnetwork/sdk 0.0.0-v10.rc2 → 0.0.0-v10.rc21

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 (238) hide show
  1. package/lib/backend/globalTrades/index.d.ts +11 -0
  2. package/lib/backend/globalTrades/index.js +69 -0
  3. package/lib/backend/index.d.ts +3 -0
  4. package/lib/backend/index.js +28 -0
  5. package/lib/backend/tradingVariables/backend.types.d.ts +318 -0
  6. package/lib/backend/tradingVariables/backend.types.js +2 -0
  7. package/lib/backend/tradingVariables/converter.d.ts +34 -0
  8. package/lib/backend/tradingVariables/converter.js +338 -0
  9. package/lib/backend/tradingVariables/index.d.ts +5 -0
  10. package/lib/backend/tradingVariables/index.js +96 -0
  11. package/lib/backend/tradingVariables/types.d.ts +113 -0
  12. package/lib/backend/tradingVariables/types.js +14 -0
  13. package/lib/constants.d.ts +20 -0
  14. package/lib/constants.js +24 -3
  15. package/lib/contracts/addresses.json +20 -0
  16. package/lib/contracts/index.js +3 -1
  17. package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
  18. package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
  19. package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
  20. package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
  21. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
  22. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
  23. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
  24. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
  25. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +7 -20
  26. package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
  27. package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
  28. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
  29. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
  30. package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
  31. package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
  32. package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
  33. package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
  34. package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
  35. package/lib/contracts/types/generated/GNSTrading.js +2 -0
  36. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
  37. package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
  38. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
  39. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
  40. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
  41. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
  42. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
  43. package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
  44. package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
  45. package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
  46. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
  47. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
  48. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
  49. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
  50. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
  51. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
  52. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
  53. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
  54. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +7 -16
  55. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  56. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  57. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
  58. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
  59. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
  60. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
  61. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
  62. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
  63. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
  64. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
  65. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
  66. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
  67. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
  68. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
  69. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
  70. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
  71. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
  72. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
  73. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
  74. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
  75. package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
  76. package/lib/contracts/types/generated/factories/GToken__factory.js +4 -0
  77. package/lib/contracts/types/index.d.ts +2 -1
  78. package/lib/contracts/types/index.js +1 -0
  79. package/lib/contracts/utils/index.d.ts +0 -3
  80. package/lib/contracts/utils/index.js +0 -3
  81. package/lib/contracts/utils/openTrades.js +14 -30
  82. package/lib/contracts/utils/pairs.js +20 -0
  83. package/lib/index.d.ts +2 -0
  84. package/lib/index.js +5 -0
  85. package/lib/markets/collateral/converter.d.ts +5 -0
  86. package/lib/markets/collateral/converter.js +11 -0
  87. package/lib/markets/collateral/index.d.ts +1 -0
  88. package/lib/markets/collateral/index.js +17 -0
  89. package/lib/markets/collateral/types.d.ts +7 -0
  90. package/lib/markets/collateral/types.js +2 -0
  91. package/lib/markets/holdingFees/index.d.ts +46 -0
  92. package/lib/markets/holdingFees/index.js +105 -0
  93. package/lib/markets/holdingFees/types.d.ts +23 -0
  94. package/lib/markets/holdingFees/types.js +5 -0
  95. package/lib/markets/index.d.ts +5 -0
  96. package/lib/markets/index.js +5 -0
  97. package/lib/markets/leverage/builder.d.ts +12 -0
  98. package/lib/markets/leverage/builder.js +25 -0
  99. package/lib/markets/leverage/getMarketLeverageRestrictions.d.ts +7 -0
  100. package/lib/markets/leverage/getMarketLeverageRestrictions.js +38 -0
  101. package/lib/markets/leverage/index.d.ts +3 -0
  102. package/lib/markets/leverage/index.js +19 -0
  103. package/lib/markets/leverage/types.d.ts +15 -0
  104. package/lib/markets/leverage/types.js +2 -0
  105. package/lib/markets/oi/converter.d.ts +62 -0
  106. package/lib/markets/oi/converter.js +102 -0
  107. package/lib/markets/oi/fetcher.d.ts +58 -0
  108. package/lib/markets/oi/fetcher.js +181 -0
  109. package/lib/markets/oi/index.d.ts +49 -0
  110. package/lib/markets/oi/index.js +77 -0
  111. package/lib/markets/oi/types.d.ts +73 -0
  112. package/lib/markets/oi/types.js +6 -0
  113. package/lib/markets/oi/validation.d.ts +80 -0
  114. package/lib/markets/oi/validation.js +172 -0
  115. package/lib/markets/price/builder.d.ts +25 -0
  116. package/lib/markets/price/builder.js +69 -0
  117. package/lib/markets/price/index.d.ts +6 -0
  118. package/lib/markets/price/index.js +22 -0
  119. package/lib/markets/price/marketPrice.d.ts +12 -0
  120. package/lib/markets/price/marketPrice.js +34 -0
  121. package/lib/markets/price/types.d.ts +23 -0
  122. package/lib/markets/price/types.js +5 -0
  123. package/lib/trade/counterTrade/index.d.ts +2 -0
  124. package/lib/trade/counterTrade/index.js +18 -0
  125. package/lib/trade/counterTrade/types.d.ts +7 -0
  126. package/lib/trade/counterTrade/types.js +2 -0
  127. package/lib/trade/counterTrade/validateCounterTrade.d.ts +10 -0
  128. package/lib/trade/counterTrade/validateCounterTrade.js +29 -0
  129. package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
  130. package/lib/trade/effectiveLeverage/builder.js +30 -0
  131. package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.d.ts +23 -0
  132. package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.js +64 -0
  133. package/lib/trade/effectiveLeverage/index.d.ts +3 -0
  134. package/lib/trade/effectiveLeverage/index.js +22 -0
  135. package/lib/trade/effectiveLeverage/types.d.ts +33 -0
  136. package/lib/trade/effectiveLeverage/types.js +2 -0
  137. package/lib/trade/fees/borrowing/builder.d.ts +14 -0
  138. package/lib/trade/fees/borrowing/builder.js +33 -0
  139. package/lib/trade/fees/borrowing/index.d.ts +15 -4
  140. package/lib/trade/fees/borrowing/index.js +42 -18
  141. package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
  142. package/lib/trade/fees/borrowingV2/builder.js +24 -0
  143. package/lib/trade/fees/borrowingV2/converter.d.ts +12 -3
  144. package/lib/trade/fees/borrowingV2/converter.js +29 -18
  145. package/lib/{contracts/fetch/fees/borrowingFeesV2.d.ts → trade/fees/borrowingV2/fetcher.d.ts} +2 -3
  146. package/lib/{contracts/fetch/fees/borrowingFeesV2.js → trade/fees/borrowingV2/fetcher.js} +6 -14
  147. package/lib/trade/fees/borrowingV2/index.d.ts +6 -17
  148. package/lib/trade/fees/borrowingV2/index.js +10 -37
  149. package/lib/trade/fees/borrowingV2/types.d.ts +22 -6
  150. package/lib/trade/fees/converter.d.ts +48 -0
  151. package/lib/trade/fees/converter.js +114 -0
  152. package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
  153. package/lib/trade/fees/fundingFees/builder.js +35 -0
  154. package/lib/{contracts/fetch/fees/fundingFees.d.ts → trade/fees/fundingFees/fetcher.d.ts} +2 -2
  155. package/lib/trade/fees/fundingFees/index.d.ts +28 -39
  156. package/lib/trade/fees/fundingFees/index.js +33 -46
  157. package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
  158. package/lib/trade/fees/fundingFees/pairContext.js +17 -0
  159. package/lib/trade/fees/holdingFees/index.d.ts +46 -0
  160. package/lib/trade/fees/holdingFees/index.js +105 -0
  161. package/lib/trade/fees/holdingFees/types.d.ts +23 -0
  162. package/lib/trade/fees/holdingFees/types.js +5 -0
  163. package/lib/trade/fees/index.d.ts +4 -2
  164. package/lib/trade/fees/index.js +45 -36
  165. package/lib/trade/fees/tiers/converter.d.ts +54 -0
  166. package/lib/trade/fees/tiers/converter.js +81 -0
  167. package/lib/trade/fees/tiers/index.d.ts +1 -0
  168. package/lib/trade/fees/tiers/index.js +1 -0
  169. package/lib/trade/fees/trading/builder.d.ts +18 -0
  170. package/lib/trade/fees/trading/builder.js +20 -0
  171. package/lib/trade/fees/trading/converter.d.ts +2 -0
  172. package/lib/trade/fees/trading/converter.js +5 -1
  173. package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
  174. package/lib/trade/fees/trading/holdingFees.js +66 -0
  175. package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
  176. package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
  177. package/lib/trade/fees/trading/index.d.ts +30 -2
  178. package/lib/trade/fees/trading/index.js +52 -1
  179. package/lib/trade/fees/trading/types.d.ts +9 -0
  180. package/lib/trade/index.d.ts +3 -1
  181. package/lib/trade/index.js +3 -1
  182. package/lib/trade/liquidation/builder.d.ts +25 -0
  183. package/lib/trade/liquidation/builder.js +59 -0
  184. package/lib/trade/liquidation/converter.d.ts +23 -0
  185. package/lib/trade/liquidation/converter.js +46 -0
  186. package/lib/trade/liquidation/index.d.ts +16 -0
  187. package/lib/trade/liquidation/index.js +123 -0
  188. package/lib/trade/liquidation/types.d.ts +42 -0
  189. package/lib/trade/liquidation/types.js +2 -0
  190. package/lib/trade/pnl/builder.d.ts +16 -0
  191. package/lib/trade/pnl/builder.js +44 -0
  192. package/lib/trade/pnl/converter.d.ts +47 -0
  193. package/lib/trade/pnl/converter.js +72 -0
  194. package/lib/trade/pnl/index.d.ts +88 -0
  195. package/lib/trade/pnl/index.js +286 -0
  196. package/lib/trade/pnl/types.d.ts +75 -0
  197. package/lib/trade/pnl/types.js +5 -0
  198. package/lib/trade/priceImpact/close/builder.d.ts +23 -0
  199. package/lib/trade/priceImpact/close/builder.js +45 -0
  200. package/lib/trade/priceImpact/close/index.d.ts +22 -0
  201. package/lib/trade/priceImpact/close/index.js +119 -0
  202. package/lib/trade/priceImpact/close/types.d.ts +44 -0
  203. package/lib/trade/priceImpact/close/types.js +5 -0
  204. package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
  205. package/lib/trade/priceImpact/cumulVol/builder.js +43 -0
  206. package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
  207. package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
  208. package/lib/trade/priceImpact/cumulVol/index.d.ts +108 -0
  209. package/lib/trade/priceImpact/cumulVol/index.js +235 -0
  210. package/lib/trade/priceImpact/index.d.ts +18 -5
  211. package/lib/trade/priceImpact/index.js +57 -10
  212. package/lib/trade/priceImpact/open/builder.d.ts +21 -0
  213. package/lib/trade/priceImpact/open/builder.js +43 -0
  214. package/lib/trade/priceImpact/open/index.d.ts +23 -0
  215. package/lib/trade/priceImpact/open/index.js +74 -0
  216. package/lib/trade/priceImpact/open/types.d.ts +41 -0
  217. package/lib/trade/priceImpact/open/types.js +5 -0
  218. package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
  219. package/lib/trade/priceImpact/skew/builder.js +28 -0
  220. package/lib/trade/priceImpact/skew/converter.d.ts +11 -42
  221. package/lib/trade/priceImpact/skew/converter.js +19 -109
  222. package/lib/{contracts/fetch/priceImpact/skew.d.ts → trade/priceImpact/skew/fetcher.d.ts} +9 -12
  223. package/lib/{contracts/fetch/priceImpact/skew.js → trade/priceImpact/skew/fetcher.js} +23 -22
  224. package/lib/trade/priceImpact/skew/index.d.ts +11 -15
  225. package/lib/trade/priceImpact/skew/index.js +22 -54
  226. package/lib/trade/priceImpact/skew/types.d.ts +2 -15
  227. package/lib/trade/spread.d.ts +5 -18
  228. package/lib/trade/spread.js +17 -106
  229. package/lib/trade/types.d.ts +30 -8
  230. package/lib/trade/types.js +20 -0
  231. package/lib/vault/index.d.ts +3 -1
  232. package/lib/vault/index.js +2 -2
  233. package/package.json +2 -2
  234. package/lib/trade/liquidation.d.ts +0 -42
  235. package/lib/trade/liquidation.js +0 -140
  236. package/lib/trade/pnl.d.ts +0 -10
  237. package/lib/trade/pnl.js +0 -38
  238. /package/lib/{contracts/fetch/fees/fundingFees.js → trade/fees/fundingFees/fetcher.js} +0 -0
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @dev Consolidated OI types for unified Open Interest management
3
+ * @dev Represents the three OI storage systems in Gains Network v10
4
+ */
5
+ /**
6
+ * @dev Unified pair OI structure combining all storage systems
7
+ */
8
+ export interface UnifiedPairOi {
9
+ /**
10
+ * @dev Maximum allowed OI in collateral (applies to combined OI)
11
+ */
12
+ maxCollateral: number;
13
+ /**
14
+ * @dev Pre-v10 trades OI stored in collateral amounts
15
+ * @dev Static values from when positions were opened
16
+ */
17
+ beforeV10Collateral: {
18
+ long: number;
19
+ short: number;
20
+ };
21
+ /**
22
+ * @dev Post-v10 trades OI stored in collateral amounts
23
+ * @dev Initial collateral values when positions were opened
24
+ * @dev Used for administrative operations and static calculations
25
+ */
26
+ collateral: {
27
+ long: number;
28
+ short: number;
29
+ };
30
+ /**
31
+ * @dev Post-v10 trades OI stored in token amounts
32
+ * @dev Used for dynamic calculations (multiply by current price)
33
+ * @dev Powers skew impact and funding fees
34
+ */
35
+ token: {
36
+ long: number;
37
+ short: number;
38
+ };
39
+ }
40
+ export type GenericPairOiContext = {
41
+ pairOis: UnifiedPairOi[];
42
+ };
43
+ /**
44
+ * @dev Group OI remains unchanged - only used by borrowing v1
45
+ * @dev Re-export existing type for consistency
46
+ */
47
+ export { OpenInterest as GroupOi } from "../../trade/types";
48
+ /**
49
+ * @dev Helper type for computed OI values
50
+ */
51
+ export interface ComputedOi {
52
+ /**
53
+ * @dev Total OI using static values: beforeV10Collateral + collateral
54
+ * @dev Used for administrative operations
55
+ */
56
+ totalStaticCollateral: {
57
+ long: number;
58
+ short: number;
59
+ };
60
+ /**
61
+ * @dev Total OI using dynamic values: beforeV10Collateral + (token * price)
62
+ * @dev Used for real-time calculations like fees
63
+ */
64
+ totalDynamicCollateral: {
65
+ long: number;
66
+ short: number;
67
+ };
68
+ /**
69
+ * @dev Net skew in tokens (v10+ only)
70
+ * @dev Positive = more longs, negative = more shorts
71
+ */
72
+ skewToken: number;
73
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Consolidated OI types for unified Open Interest management
4
+ * @dev Represents the three OI storage systems in Gains Network v10
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,80 @@
1
+ /**
2
+ * @dev OI Validation module
3
+ * @dev Provides validation functions for Open Interest limits
4
+ */
5
+ import { UnifiedPairOi } from "./types";
6
+ import * as BorrowingFee from "../../trade/fees/borrowing/types";
7
+ import { PairIndex } from "../../trade/types";
8
+ /**
9
+ * @dev Check if a position would exceed per-pair OI limits
10
+ * @param pairOi Current OI data for the pair
11
+ * @param long Whether the position is long
12
+ * @param positionSizeCollateral Position size in collateral
13
+ * @param currentPrice Current collateral price in USD (required for dynamic OI)
14
+ * @returns true if within limits, false if would exceed
15
+ */
16
+ export declare const withinMaxPairOi: (pairOi: UnifiedPairOi, long: boolean, positionSizeCollateral: number, currentPrice: number) => boolean;
17
+ /**
18
+ * @dev Calculate dynamic OI for a specific side
19
+ * @param pairOi OI data for the pair
20
+ * @param currentPrice Current collateral price in USD
21
+ * @param long Whether to calculate for long side
22
+ * @returns Dynamic OI in collateral value
23
+ */
24
+ export declare const calculateDynamicOi: (pairOi: UnifiedPairOi, currentPrice: number, long: boolean) => number;
25
+ /**
26
+ * @dev Calculate remaining OI capacity for a side
27
+ * @param pairOi OI data for the pair
28
+ * @param currentPrice Current collateral price in USD
29
+ * @param long Whether to calculate for long side
30
+ * @returns Remaining capacity in collateral (0 if unlimited)
31
+ */
32
+ export declare const getRemainingOiCapacity: (pairOi: UnifiedPairOi, currentPrice: number, long: boolean) => number;
33
+ /**
34
+ * @dev Updated group OI validation using dynamic OI
35
+ * @param pairIndex Index of the trading pair
36
+ * @param long Whether the position is long
37
+ * @param positionSizeCollateral Position size in collateral
38
+ * @param currentPrice Current collateral price in USD
39
+ * @param context Context with groups, pairs, and OI data
40
+ * @returns true if within group limits, false if would exceed
41
+ */
42
+ export declare const withinMaxGroupOiDynamic: (pairIndex: PairIndex, long: boolean, positionSizeCollateral: number, currentPrice: number, context: {
43
+ groups: BorrowingFee.Group[];
44
+ pairs: BorrowingFee.Pair[];
45
+ pairOis: UnifiedPairOi[];
46
+ }) => boolean;
47
+ /**
48
+ * @dev Calculate total dynamic OI for a group
49
+ * @param groupIndex Index of the group
50
+ * @param currentPrice Current collateral price in USD
51
+ * @param context Context with pairs and OI data
52
+ * @returns Total dynamic OI for the group
53
+ */
54
+ export declare const getGroupDynamicOi: (groupIndex: number, currentPrice: number, context: {
55
+ pairs: BorrowingFee.Pair[];
56
+ pairOis: UnifiedPairOi[];
57
+ }) => {
58
+ long: number;
59
+ short: number;
60
+ total: number;
61
+ };
62
+ /**
63
+ * @dev Check both pair and group OI limits
64
+ * @param pairIndex Index of the trading pair
65
+ * @param long Whether the position is long
66
+ * @param positionSizeCollateral Position size in collateral
67
+ * @param currentPrice Current collateral price in USD
68
+ * @param context Full context with all required data
69
+ * @returns Object with validation results
70
+ */
71
+ export declare const validateOiLimits: (pairIndex: PairIndex, long: boolean, positionSizeCollateral: number, currentPrice: number, context: {
72
+ groups: BorrowingFee.Group[];
73
+ pairs: BorrowingFee.Pair[];
74
+ pairOis: UnifiedPairOi[];
75
+ }) => {
76
+ withinPairLimit: boolean;
77
+ withinGroupLimit: boolean;
78
+ pairRemainingCapacity: number;
79
+ groupRemainingCapacity: number;
80
+ };
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ /**
3
+ * @dev OI Validation module
4
+ * @dev Provides validation functions for Open Interest limits
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.validateOiLimits = exports.getGroupDynamicOi = exports.withinMaxGroupOiDynamic = exports.getRemainingOiCapacity = exports.calculateDynamicOi = exports.withinMaxPairOi = void 0;
8
+ const converter_1 = require("./converter");
9
+ /**
10
+ * @dev Check if a position would exceed per-pair OI limits
11
+ * @param pairOi Current OI data for the pair
12
+ * @param long Whether the position is long
13
+ * @param positionSizeCollateral Position size in collateral
14
+ * @param currentPrice Current collateral price in USD (required for dynamic OI)
15
+ * @returns true if within limits, false if would exceed
16
+ */
17
+ const withinMaxPairOi = (pairOi, long, positionSizeCollateral, currentPrice) => {
18
+ // If maxCollateral is 0, unlimited OI allowed
19
+ if (pairOi.maxCollateral === 0) {
20
+ return true;
21
+ }
22
+ // Calculate current dynamic OI
23
+ const computed = (0, converter_1.computeOiValues)(pairOi, currentPrice);
24
+ const currentOi = long
25
+ ? computed.totalDynamicCollateral.long
26
+ : computed.totalDynamicCollateral.short;
27
+ // Check if adding position would exceed max
28
+ const newOi = currentOi + positionSizeCollateral;
29
+ return newOi <= pairOi.maxCollateral;
30
+ };
31
+ exports.withinMaxPairOi = withinMaxPairOi;
32
+ /**
33
+ * @dev Calculate dynamic OI for a specific side
34
+ * @param pairOi OI data for the pair
35
+ * @param currentPrice Current collateral price in USD
36
+ * @param long Whether to calculate for long side
37
+ * @returns Dynamic OI in collateral value
38
+ */
39
+ const calculateDynamicOi = (pairOi, currentPrice, long) => {
40
+ const computed = (0, converter_1.computeOiValues)(pairOi, currentPrice);
41
+ return long
42
+ ? computed.totalDynamicCollateral.long
43
+ : computed.totalDynamicCollateral.short;
44
+ };
45
+ exports.calculateDynamicOi = calculateDynamicOi;
46
+ /**
47
+ * @dev Calculate remaining OI capacity for a side
48
+ * @param pairOi OI data for the pair
49
+ * @param currentPrice Current collateral price in USD
50
+ * @param long Whether to calculate for long side
51
+ * @returns Remaining capacity in collateral (0 if unlimited)
52
+ */
53
+ const getRemainingOiCapacity = (pairOi, currentPrice, long) => {
54
+ // If maxCollateral is 0, unlimited capacity
55
+ if (pairOi.maxCollateral === 0) {
56
+ return 0; // Indicates unlimited
57
+ }
58
+ const dynamicOi = (0, exports.calculateDynamicOi)(pairOi, currentPrice, long);
59
+ const remaining = pairOi.maxCollateral - dynamicOi;
60
+ // Return 0 if already at or over capacity
61
+ return Math.max(0, remaining);
62
+ };
63
+ exports.getRemainingOiCapacity = getRemainingOiCapacity;
64
+ /**
65
+ * @dev Updated group OI validation using dynamic OI
66
+ * @param pairIndex Index of the trading pair
67
+ * @param long Whether the position is long
68
+ * @param positionSizeCollateral Position size in collateral
69
+ * @param currentPrice Current collateral price in USD
70
+ * @param context Context with groups, pairs, and OI data
71
+ * @returns true if within group limits, false if would exceed
72
+ */
73
+ const withinMaxGroupOiDynamic = (pairIndex, long, positionSizeCollateral, currentPrice, context) => {
74
+ const pair = context.pairs[pairIndex];
75
+ if (!pair)
76
+ return false;
77
+ // Get group index from first group in pair's groups array
78
+ const groupIndex = pair.groups.length > 0 ? pair.groups[0].groupIndex : 0;
79
+ const group = context.groups[groupIndex];
80
+ if (!group)
81
+ return false;
82
+ // If maxOi is 0, unlimited OI allowed
83
+ if (group.oi.max === 0) {
84
+ return true;
85
+ }
86
+ // Calculate total dynamic OI for all pairs in group
87
+ let totalGroupOi = 0;
88
+ context.pairs.forEach((p, idx) => {
89
+ const pGroupIndex = p.groups.length > 0 ? p.groups[0].groupIndex : 0;
90
+ if (pGroupIndex === groupIndex && context.pairOis[idx]) {
91
+ const pairOi = context.pairOis[idx];
92
+ const computed = (0, converter_1.computeOiValues)(pairOi, currentPrice);
93
+ // Add both long and short OI for the pair
94
+ totalGroupOi +=
95
+ computed.totalDynamicCollateral.long +
96
+ computed.totalDynamicCollateral.short;
97
+ }
98
+ });
99
+ // Check if adding position would exceed group max
100
+ const newTotalOi = totalGroupOi + positionSizeCollateral;
101
+ return newTotalOi <= group.oi.max;
102
+ };
103
+ exports.withinMaxGroupOiDynamic = withinMaxGroupOiDynamic;
104
+ /**
105
+ * @dev Calculate total dynamic OI for a group
106
+ * @param groupIndex Index of the group
107
+ * @param currentPrice Current collateral price in USD
108
+ * @param context Context with pairs and OI data
109
+ * @returns Total dynamic OI for the group
110
+ */
111
+ const getGroupDynamicOi = (groupIndex, currentPrice, context) => {
112
+ let longOi = 0;
113
+ let shortOi = 0;
114
+ context.pairs.forEach((p, idx) => {
115
+ const pGroupIndex = p.groups.length > 0 ? p.groups[0].groupIndex : 0;
116
+ if (pGroupIndex === groupIndex && context.pairOis[idx]) {
117
+ const pairOi = context.pairOis[idx];
118
+ const computed = (0, converter_1.computeOiValues)(pairOi, currentPrice);
119
+ longOi += computed.totalDynamicCollateral.long;
120
+ shortOi += computed.totalDynamicCollateral.short;
121
+ }
122
+ });
123
+ return {
124
+ long: longOi,
125
+ short: shortOi,
126
+ total: longOi + shortOi,
127
+ };
128
+ };
129
+ exports.getGroupDynamicOi = getGroupDynamicOi;
130
+ /**
131
+ * @dev Check both pair and group OI limits
132
+ * @param pairIndex Index of the trading pair
133
+ * @param long Whether the position is long
134
+ * @param positionSizeCollateral Position size in collateral
135
+ * @param currentPrice Current collateral price in USD
136
+ * @param context Full context with all required data
137
+ * @returns Object with validation results
138
+ */
139
+ const validateOiLimits = (pairIndex, long, positionSizeCollateral, currentPrice, context) => {
140
+ const pairOi = context.pairOis[pairIndex];
141
+ if (!pairOi) {
142
+ return {
143
+ withinPairLimit: false,
144
+ withinGroupLimit: false,
145
+ pairRemainingCapacity: 0,
146
+ groupRemainingCapacity: 0,
147
+ };
148
+ }
149
+ // Check pair limits
150
+ const withinPairLimit = (0, exports.withinMaxPairOi)(pairOi, long, positionSizeCollateral, currentPrice);
151
+ const pairRemainingCapacity = (0, exports.getRemainingOiCapacity)(pairOi, currentPrice, long);
152
+ // Check group limits
153
+ const withinGroupLimit = (0, exports.withinMaxGroupOiDynamic)(pairIndex, long, positionSizeCollateral, currentPrice, context);
154
+ // Calculate group remaining capacity
155
+ const pair = context.pairs[pairIndex];
156
+ let groupRemainingCapacity = 0;
157
+ if (pair) {
158
+ const groupIndex = pair.groups.length > 0 ? pair.groups[0].groupIndex : 0;
159
+ const group = context.groups[groupIndex];
160
+ if (group && group.oi.max > 0) {
161
+ const groupOi = (0, exports.getGroupDynamicOi)(groupIndex, currentPrice, context);
162
+ groupRemainingCapacity = Math.max(0, group.oi.max - groupOi.total);
163
+ }
164
+ }
165
+ return {
166
+ withinPairLimit,
167
+ withinGroupLimit,
168
+ pairRemainingCapacity,
169
+ groupRemainingCapacity,
170
+ };
171
+ };
172
+ exports.validateOiLimits = validateOiLimits;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @dev Builder functions for creating market price contexts
3
+ */
4
+ import { MarketPriceContext } from "./types";
5
+ import { TradingVariablesCollateral } from "../../backend/tradingVariables/types";
6
+ /**
7
+ * @dev Builds a market price context for a specific pair from trading variables
8
+ * @param tradingVariables Trading variables for a specific collateral
9
+ * @param pairIndex Pair index to create context for
10
+ * @returns Market price context for the pair
11
+ */
12
+ export declare const buildMarketPriceContext: (tradingVariables: TradingVariablesCollateral, pairIndex: number) => MarketPriceContext;
13
+ /**
14
+ * @dev Creates an empty market price context
15
+ * @returns Empty market price context
16
+ */
17
+ export declare const createEmptyMarketPriceContext: () => MarketPriceContext;
18
+ /**
19
+ * @dev Creates a market price context with custom values
20
+ * @param skewDepth Skew depth in tokens
21
+ * @param oiLongToken Long OI in tokens
22
+ * @param oiShortToken Short OI in tokens
23
+ * @returns Market price context
24
+ */
25
+ export declare const createMarketPriceContext: (skewDepth: number, oiLongToken: number, oiShortToken: number) => MarketPriceContext;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Builder functions for creating market price contexts
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createMarketPriceContext = exports.createEmptyMarketPriceContext = exports.buildMarketPriceContext = void 0;
7
+ /**
8
+ * @dev Builds a market price context for a specific pair from trading variables
9
+ * @param tradingVariables Trading variables for a specific collateral
10
+ * @param pairIndex Pair index to create context for
11
+ * @returns Market price context for the pair
12
+ */
13
+ const buildMarketPriceContext = (tradingVariables, pairIndex) => {
14
+ // Get skew depth for the pair, default to 0 if not set
15
+ const skewDepth = tradingVariables.pairSkewDepths[pairIndex] || 0;
16
+ // Get pair OI data
17
+ const pairOi = tradingVariables.pairOis[pairIndex];
18
+ if (!pairOi) {
19
+ // Return default context if no OI data
20
+ return {
21
+ skewDepth,
22
+ pairOiToken: {
23
+ oiLongToken: 0,
24
+ oiShortToken: 0,
25
+ },
26
+ };
27
+ }
28
+ // Extract token OI from unified pair OI
29
+ const pairOiToken = {
30
+ oiLongToken: pairOi.token.long,
31
+ oiShortToken: pairOi.token.short,
32
+ };
33
+ return {
34
+ skewDepth,
35
+ pairOiToken,
36
+ };
37
+ };
38
+ exports.buildMarketPriceContext = buildMarketPriceContext;
39
+ /**
40
+ * @dev Creates an empty market price context
41
+ * @returns Empty market price context
42
+ */
43
+ const createEmptyMarketPriceContext = () => {
44
+ return {
45
+ skewDepth: 0,
46
+ pairOiToken: {
47
+ oiLongToken: 0,
48
+ oiShortToken: 0,
49
+ },
50
+ };
51
+ };
52
+ exports.createEmptyMarketPriceContext = createEmptyMarketPriceContext;
53
+ /**
54
+ * @dev Creates a market price context with custom values
55
+ * @param skewDepth Skew depth in tokens
56
+ * @param oiLongToken Long OI in tokens
57
+ * @param oiShortToken Short OI in tokens
58
+ * @returns Market price context
59
+ */
60
+ const createMarketPriceContext = (skewDepth, oiLongToken, oiShortToken) => {
61
+ return {
62
+ skewDepth,
63
+ pairOiToken: {
64
+ oiLongToken,
65
+ oiShortToken,
66
+ },
67
+ };
68
+ };
69
+ exports.createMarketPriceContext = createMarketPriceContext;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @dev Market price module exports
3
+ */
4
+ export * from "./types";
5
+ export * from "./marketPrice";
6
+ export * from "./builder";
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Market price module exports
4
+ */
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ __exportStar(require("./types"), exports);
21
+ __exportStar(require("./marketPrice"), exports);
22
+ __exportStar(require("./builder"), exports);
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @dev Current market price calculation with skew impact
3
+ */
4
+ import { MarketPriceResult, MarketPriceContext } from "./types";
5
+ /**
6
+ * @dev Calculates the current market price adjusted for skew impact
7
+ * @param pairIndex Trading pair index
8
+ * @param oraclePrice Oracle price for the pair
9
+ * @param context Market price context with depths and OI data
10
+ * @returns Current market price with skew impact applied
11
+ */
12
+ export declare const getCurrentMarketPrice: (pairIndex: number, oraclePrice: number, context: MarketPriceContext) => MarketPriceResult;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Current market price calculation with skew impact
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getCurrentMarketPrice = void 0;
7
+ const skew_1 = require("../../trade/priceImpact/skew");
8
+ /**
9
+ * @dev Calculates the current market price adjusted for skew impact
10
+ * @param pairIndex Trading pair index
11
+ * @param oraclePrice Oracle price for the pair
12
+ * @param context Market price context with depths and OI data
13
+ * @returns Current market price with skew impact applied
14
+ */
15
+ const getCurrentMarketPrice = (pairIndex, oraclePrice, context) => {
16
+ let skewImpactP = 0;
17
+ if (context.skewDepth > 0) {
18
+ const skewResult = (0, skew_1.getTradeSkewPriceImpact)({
19
+ collateralIndex: 0,
20
+ pairIndex,
21
+ long: true,
22
+ open: true,
23
+ positionSizeToken: 0, // Size 0 for current market price
24
+ }, context);
25
+ skewImpactP = skewResult.priceImpactP;
26
+ }
27
+ const marketPrice = oraclePrice * (1 + skewImpactP / 100);
28
+ return {
29
+ marketPrice,
30
+ skewImpactP,
31
+ oraclePrice,
32
+ };
33
+ };
34
+ exports.getCurrentMarketPrice = getCurrentMarketPrice;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @dev Market price types for calculating price with skew impact
3
+ */
4
+ export type MarketPriceInput = {
5
+ collateralIndex: number;
6
+ pairIndex: number;
7
+ oraclePrice: number;
8
+ long: boolean;
9
+ open: boolean;
10
+ positionSizeCollateral: number;
11
+ };
12
+ export type MarketPriceResult = {
13
+ marketPrice: number;
14
+ skewImpactP: number;
15
+ oraclePrice: number;
16
+ };
17
+ export type MarketPriceContext = {
18
+ skewDepth: number;
19
+ pairOiToken: {
20
+ oiLongToken: number;
21
+ oiShortToken: number;
22
+ };
23
+ };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Market price types for calculating price with skew impact
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export * from "./types";
2
+ export * from "./validateCounterTrade";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./validateCounterTrade"), exports);
@@ -0,0 +1,7 @@
1
+ export type ValidateCounterTradeContext = {
2
+ pairOiSkewCollateral: number;
3
+ };
4
+ export type ValidateCounterTradeResult = {
5
+ isValidated: boolean;
6
+ exceedingPositionSizeCollateral: number;
7
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { Trade } from "../types";
2
+ import { ValidateCounterTradeContext, ValidateCounterTradeResult } from "./types";
3
+ /**
4
+ * Validates a counter trade based on pair OI skew, matching the contract's validateCounterTrade logic
5
+ * @param trade Trade object
6
+ * @param positionSizeCollateral Position size in collateral tokens
7
+ * @param context Context containing the pair OI skew
8
+ * @returns Validation result with exceeding collateral amount if applicable
9
+ */
10
+ export declare function validateCounterTrade(trade: Trade, positionSizeCollateral: number, context: ValidateCounterTradeContext): ValidateCounterTradeResult;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateCounterTrade = void 0;
4
+ /**
5
+ * Validates a counter trade based on pair OI skew, matching the contract's validateCounterTrade logic
6
+ * @param trade Trade object
7
+ * @param positionSizeCollateral Position size in collateral tokens
8
+ * @param context Context containing the pair OI skew
9
+ * @returns Validation result with exceeding collateral amount if applicable
10
+ */
11
+ function validateCounterTrade(trade, positionSizeCollateral, context) {
12
+ const { pairOiSkewCollateral } = context;
13
+ // Calculate signed position size based on trade direction
14
+ const positionSizeCollateralSigned = positionSizeCollateral * (trade.long ? 1 : -1);
15
+ // Check if position improves skew (opposite signs)
16
+ if (pairOiSkewCollateral === 0 ||
17
+ (pairOiSkewCollateral > 0 && positionSizeCollateralSigned > 0) ||
18
+ (pairOiSkewCollateral < 0 && positionSizeCollateralSigned < 0)) {
19
+ return { isValidated: false, exceedingPositionSizeCollateral: 0 };
20
+ }
21
+ // Calculate maximum position size that brings skew to 0
22
+ const maxPositionSizeCollateral = Math.abs(pairOiSkewCollateral);
23
+ // Calculate exceeding amount
24
+ const exceedingPositionSizeCollateral = positionSizeCollateral > maxPositionSizeCollateral
25
+ ? positionSizeCollateral - maxPositionSizeCollateral
26
+ : 0;
27
+ return { isValidated: true, exceedingPositionSizeCollateral };
28
+ }
29
+ exports.validateCounterTrade = validateCounterTrade;
@@ -0,0 +1,23 @@
1
+ import { GlobalTradingVariablesType } from "../../backend/tradingVariables/types";
2
+ import { TradeInfo } from "..";
3
+ import { TradeEffectiveLeverageContext } from "./types";
4
+ /**
5
+ * @dev Builds a complete context for effective leverage calculations
6
+ * @dev Uses the closing price impact context builder as a sub-context
7
+ * @param globalTradingVariables The transformed global trading variables from backend
8
+ * @param collateralIndex The collateral index (1-based)
9
+ * @param pairIndex The pair index
10
+ * @param tradeInfo Trade information including createdBlock
11
+ * @param additionalParams Additional parameters for price impact calculations
12
+ * @returns Complete context ready for getTradeNewEffectiveLeverage
13
+ */
14
+ export declare const buildTradeEffectiveLeverageContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, tradeInfo: TradeInfo, additionalParams: {
15
+ currentBlock: number;
16
+ contractsVersion?: number;
17
+ isPnlPositive?: boolean;
18
+ userPriceImpact?: {
19
+ cumulVolPriceImpactMultiplier: number;
20
+ fixedSpreadP: number;
21
+ };
22
+ protectionCloseFactorWhitelist?: boolean;
23
+ }) => TradeEffectiveLeverageContext | undefined;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildTradeEffectiveLeverageContext = void 0;
4
+ const __1 = require("..");
5
+ /**
6
+ * @dev Builds a complete context for effective leverage calculations
7
+ * @dev Uses the closing price impact context builder as a sub-context
8
+ * @param globalTradingVariables The transformed global trading variables from backend
9
+ * @param collateralIndex The collateral index (1-based)
10
+ * @param pairIndex The pair index
11
+ * @param tradeInfo Trade information including createdBlock
12
+ * @param additionalParams Additional parameters for price impact calculations
13
+ * @returns Complete context ready for getTradeNewEffectiveLeverage
14
+ */
15
+ const buildTradeEffectiveLeverageContext = (globalTradingVariables, collateralIndex, pairIndex, tradeInfo, additionalParams) => {
16
+ var _a;
17
+ // Build the closing price impact context which we'll use for PnL calculations
18
+ const closingPriceImpactContext = (0, __1.buildTradeClosingPriceImpactContext)(globalTradingVariables, collateralIndex, pairIndex, tradeInfo, additionalParams);
19
+ if (!closingPriceImpactContext) {
20
+ return undefined;
21
+ }
22
+ // Extract base spread from pairs data
23
+ const pairs = globalTradingVariables.pairs;
24
+ const baseSpreadP = ((_a = pairs === null || pairs === void 0 ? void 0 : pairs[pairIndex]) === null || _a === void 0 ? void 0 : _a.spreadP) || 0;
25
+ return {
26
+ closingPriceImpactContext,
27
+ baseSpreadP,
28
+ };
29
+ };
30
+ exports.buildTradeEffectiveLeverageContext = buildTradeEffectiveLeverageContext;