@gainsnetwork/sdk 0.2.71-rc4 → 1.0.0-rc2

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 (247) hide show
  1. package/README.md +2 -2
  2. package/lib/backend/globalTrades/index.d.ts +11 -0
  3. package/lib/backend/globalTrades/index.js +69 -0
  4. package/lib/backend/index.d.ts +3 -0
  5. package/lib/backend/index.js +28 -0
  6. package/lib/backend/tradingVariables/backend.types.d.ts +318 -0
  7. package/lib/backend/tradingVariables/backend.types.js +2 -0
  8. package/lib/backend/tradingVariables/converter.d.ts +34 -0
  9. package/lib/backend/tradingVariables/converter.js +338 -0
  10. package/lib/backend/tradingVariables/index.d.ts +5 -0
  11. package/lib/backend/tradingVariables/index.js +96 -0
  12. package/lib/backend/tradingVariables/types.d.ts +113 -0
  13. package/lib/backend/tradingVariables/types.js +14 -0
  14. package/lib/constants.d.ts +0 -3
  15. package/lib/constants.js +7 -9
  16. package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
  17. package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
  18. package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
  19. package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
  20. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
  21. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
  22. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
  23. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
  24. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +2134 -293
  25. package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
  26. package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
  27. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
  28. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
  29. package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
  30. package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
  31. package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
  32. package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
  33. package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
  34. package/lib/contracts/types/generated/GNSTrading.js +2 -0
  35. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
  36. package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
  37. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
  38. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
  39. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
  40. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
  41. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
  42. package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
  43. package/lib/contracts/types/generated/GToken.d.ts +78 -107
  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.d.ts +90 -53
  55. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +4496 -430
  56. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  57. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  58. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
  59. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
  60. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
  61. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
  62. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
  63. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
  64. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
  65. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
  66. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
  67. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
  68. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
  69. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
  70. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
  71. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
  72. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
  73. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
  74. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
  75. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
  76. package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
  77. package/lib/contracts/types/generated/factories/GToken__factory.js +69 -142
  78. package/lib/contracts/types/index.d.ts +2 -1
  79. package/lib/contracts/types/index.js +1 -0
  80. package/lib/contracts/utils/openTrades.d.ts +1 -0
  81. package/lib/contracts/utils/openTrades.js +94 -56
  82. package/lib/contracts/utils/pairs.js +0 -3
  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 +13 -0
  120. package/lib/markets/price/marketPrice.js +35 -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 +23 -2
  140. package/lib/trade/fees/borrowing/index.js +50 -16
  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 +75 -0
  144. package/lib/trade/fees/borrowingV2/converter.js +132 -0
  145. package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
  146. package/lib/trade/fees/borrowingV2/fetcher.js +185 -0
  147. package/lib/trade/fees/borrowingV2/index.d.ts +48 -0
  148. package/lib/trade/fees/borrowingV2/index.js +112 -0
  149. package/lib/trade/fees/borrowingV2/types.d.ts +95 -0
  150. package/lib/trade/fees/borrowingV2/types.js +5 -0
  151. package/lib/trade/fees/converter.d.ts +48 -0
  152. package/lib/trade/fees/converter.js +114 -0
  153. package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
  154. package/lib/trade/fees/fundingFees/builder.js +35 -0
  155. package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
  156. package/lib/trade/fees/fundingFees/converter.js +196 -0
  157. package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
  158. package/lib/trade/fees/fundingFees/fetcher.js +150 -0
  159. package/lib/trade/fees/fundingFees/index.d.ts +124 -0
  160. package/lib/trade/fees/fundingFees/index.js +309 -0
  161. package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
  162. package/lib/trade/fees/fundingFees/pairContext.js +17 -0
  163. package/lib/trade/fees/fundingFees/types.d.ts +77 -0
  164. package/lib/trade/fees/fundingFees/types.js +5 -0
  165. package/lib/trade/fees/holdingFees/index.d.ts +46 -0
  166. package/lib/trade/fees/holdingFees/index.js +105 -0
  167. package/lib/trade/fees/holdingFees/types.d.ts +23 -0
  168. package/lib/trade/fees/holdingFees/types.js +5 -0
  169. package/lib/trade/fees/index.d.ts +8 -2
  170. package/lib/trade/fees/index.js +67 -16
  171. package/lib/trade/fees/tiers/converter.d.ts +54 -0
  172. package/lib/trade/fees/tiers/converter.js +81 -0
  173. package/lib/trade/fees/tiers/index.d.ts +18 -0
  174. package/lib/trade/fees/tiers/index.js +45 -1
  175. package/lib/trade/fees/trading/builder.d.ts +18 -0
  176. package/lib/trade/fees/trading/builder.js +20 -0
  177. package/lib/trade/fees/trading/converter.d.ts +32 -0
  178. package/lib/trade/fees/trading/converter.js +47 -0
  179. package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
  180. package/lib/trade/fees/trading/holdingFees.js +66 -0
  181. package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
  182. package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
  183. package/lib/trade/fees/trading/index.d.ts +62 -0
  184. package/lib/trade/fees/trading/index.js +155 -0
  185. package/lib/trade/fees/trading/types.d.ts +48 -0
  186. package/lib/trade/fees/trading/types.js +5 -0
  187. package/lib/trade/index.d.ts +5 -2
  188. package/lib/trade/index.js +5 -2
  189. package/lib/trade/liquidation/builder.d.ts +25 -0
  190. package/lib/trade/liquidation/builder.js +59 -0
  191. package/lib/trade/liquidation/converter.d.ts +23 -0
  192. package/lib/trade/liquidation/converter.js +46 -0
  193. package/lib/trade/liquidation/index.d.ts +29 -0
  194. package/lib/trade/liquidation/index.js +218 -0
  195. package/lib/trade/liquidation/types.d.ts +43 -0
  196. package/lib/trade/liquidation/types.js +2 -0
  197. package/lib/trade/pnl/builder.d.ts +16 -0
  198. package/lib/trade/pnl/builder.js +44 -0
  199. package/lib/trade/pnl/converter.d.ts +47 -0
  200. package/lib/trade/pnl/converter.js +72 -0
  201. package/lib/trade/pnl/index.d.ts +89 -0
  202. package/lib/trade/pnl/index.js +302 -0
  203. package/lib/trade/pnl/types.d.ts +79 -0
  204. package/lib/trade/pnl/types.js +5 -0
  205. package/lib/trade/priceImpact/close/builder.d.ts +23 -0
  206. package/lib/trade/priceImpact/close/builder.js +45 -0
  207. package/lib/trade/priceImpact/close/index.d.ts +22 -0
  208. package/lib/trade/priceImpact/close/index.js +134 -0
  209. package/lib/trade/priceImpact/close/types.d.ts +47 -0
  210. package/lib/trade/priceImpact/close/types.js +5 -0
  211. package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
  212. package/lib/trade/priceImpact/cumulVol/builder.js +43 -0
  213. package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
  214. package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
  215. package/lib/trade/priceImpact/cumulVol/index.d.ts +108 -0
  216. package/lib/trade/priceImpact/cumulVol/index.js +235 -0
  217. package/lib/trade/priceImpact/index.d.ts +21 -0
  218. package/lib/trade/priceImpact/index.js +79 -0
  219. package/lib/trade/priceImpact/open/builder.d.ts +21 -0
  220. package/lib/trade/priceImpact/open/builder.js +43 -0
  221. package/lib/trade/priceImpact/open/index.d.ts +23 -0
  222. package/lib/trade/priceImpact/open/index.js +78 -0
  223. package/lib/trade/priceImpact/open/types.d.ts +44 -0
  224. package/lib/trade/priceImpact/open/types.js +5 -0
  225. package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
  226. package/lib/trade/priceImpact/skew/builder.js +28 -0
  227. package/lib/trade/priceImpact/skew/converter.d.ts +46 -0
  228. package/lib/trade/priceImpact/skew/converter.js +81 -0
  229. package/lib/trade/priceImpact/skew/fetcher.d.ts +60 -0
  230. package/lib/trade/priceImpact/skew/fetcher.js +169 -0
  231. package/lib/trade/priceImpact/skew/index.d.ts +53 -0
  232. package/lib/trade/priceImpact/skew/index.js +148 -0
  233. package/lib/trade/priceImpact/skew/types.d.ts +44 -0
  234. package/lib/trade/priceImpact/skew/types.js +5 -0
  235. package/lib/trade/spread.d.ts +5 -18
  236. package/lib/trade/spread.js +17 -106
  237. package/lib/trade/types.d.ts +109 -12
  238. package/lib/trade/types.js +0 -3
  239. package/lib/trade/utils.d.ts +18 -0
  240. package/lib/trade/utils.js +30 -0
  241. package/lib/vault/index.d.ts +3 -1
  242. package/lib/vault/index.js +2 -2
  243. package/package.json +2 -1
  244. package/lib/trade/liquidation.d.ts +0 -12
  245. package/lib/trade/liquidation.js +0 -55
  246. package/lib/trade/pnl.d.ts +0 -10
  247. package/lib/trade/pnl.js +0 -33
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Fetchers for retrieving OI data from contracts
4
+ * @dev Consolidates the three OI storage systems into unified format
5
+ */
6
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
7
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
8
+ return new (P || (P = Promise))(function (resolve, reject) {
9
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
10
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
11
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
12
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
13
+ });
14
+ };
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.fetchOiForUseCase = exports.createOiContext = exports.fetchMultiplePairOi = exports.fetchPairOi = void 0;
17
+ const ethers_1 = require("ethers");
18
+ const contracts_1 = require("../../contracts");
19
+ const generated_1 = require("../../contracts/types/generated");
20
+ const converter_1 = require("./converter");
21
+ /**
22
+ * @dev Fetches all OI data for a single pair
23
+ * @param chainId Target chain
24
+ * @param collateralIndex Collateral type
25
+ * @param pairIndex Trading pair
26
+ * @param signer Ethers signer
27
+ * @returns Unified PairOi structure with all OI data
28
+ */
29
+ function fetchPairOi(chainId, collateralIndex, pairIndex, signer) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ const addresses = (0, contracts_1.getContractAddressesForChain)(chainId);
32
+ const diamond = generated_1.GNSMultiCollatDiamond__factory.connect(addresses.gnsMultiCollatDiamond, signer);
33
+ // Fetch all three OI types in parallel
34
+ const [beforeV10Raw, afterV10Collateral, afterV10Token, maxOi, collateral] = yield Promise.all([
35
+ diamond.getPairOisBeforeV10Collateral(collateralIndex, pairIndex),
36
+ diamond.getPairOiAfterV10Collateral(collateralIndex, pairIndex),
37
+ diamond.getPairOiAfterV10Token(collateralIndex, pairIndex),
38
+ diamond.getPairMaxOi(collateralIndex, pairIndex),
39
+ diamond.getCollateral(collateralIndex),
40
+ ]);
41
+ // Convert the beforeV10 format to match expected structure
42
+ const beforeV10 = {
43
+ long: beforeV10Raw.longOi,
44
+ short: beforeV10Raw.shortOi,
45
+ max: maxOi,
46
+ __placeholder: ethers_1.ethers.BigNumber.from(0),
47
+ };
48
+ return (0, converter_1.convertPairOi)(beforeV10, afterV10Collateral, afterV10Token, Number(collateral.precision));
49
+ });
50
+ }
51
+ exports.fetchPairOi = fetchPairOi;
52
+ /**
53
+ * @dev Fetches OI data for multiple pairs efficiently
54
+ * @param chainId Target chain
55
+ * @param collateralIndex Collateral type
56
+ * @param pairIndices Array of trading pairs
57
+ * @param signer Ethers signer
58
+ * @returns Array of unified PairOi structures
59
+ */
60
+ function fetchMultiplePairOi(chainId, collateralIndex, pairIndices, signer) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const addresses = (0, contracts_1.getContractAddressesForChain)(chainId);
63
+ const diamond = generated_1.GNSMultiCollatDiamond__factory.connect(addresses.gnsMultiCollatDiamond, signer);
64
+ // Get collateral precision once
65
+ const collateral = yield diamond.getCollateral(collateralIndex);
66
+ const precision = Number(collateral.precision);
67
+ // Batch fetch all OI data
68
+ const promises = pairIndices.map((pairIndex) => __awaiter(this, void 0, void 0, function* () {
69
+ const [beforeV10Raw, afterV10Collateral, afterV10Token, maxOi] = yield Promise.all([
70
+ diamond.getPairOisBeforeV10Collateral(collateralIndex, pairIndex),
71
+ diamond.getPairOiAfterV10Collateral(collateralIndex, pairIndex),
72
+ diamond.getPairOiAfterV10Token(collateralIndex, pairIndex),
73
+ diamond.getPairMaxOi(collateralIndex, pairIndex),
74
+ ]);
75
+ // Convert the beforeV10 format to match expected structure
76
+ const beforeV10 = {
77
+ long: beforeV10Raw.longOi,
78
+ short: beforeV10Raw.shortOi,
79
+ max: maxOi,
80
+ __placeholder: ethers_1.ethers.BigNumber.from(0),
81
+ };
82
+ return {
83
+ beforeV10: beforeV10,
84
+ collateral: afterV10Collateral,
85
+ token: afterV10Token,
86
+ };
87
+ }));
88
+ const results = yield Promise.all(promises);
89
+ return (0, converter_1.convertPairOiArray)(results, precision);
90
+ });
91
+ }
92
+ exports.fetchMultiplePairOi = fetchMultiplePairOi;
93
+ /**
94
+ * @dev Creates OI context for fee calculations
95
+ * @param chainId Target chain
96
+ * @param collateralIndex Collateral type
97
+ * @param pairIndex Trading pair
98
+ * @param signer Ethers signer
99
+ * @returns OI data formatted for SDK calculations
100
+ */
101
+ function createOiContext(chainId, collateralIndex, pairIndex, signer) {
102
+ return __awaiter(this, void 0, void 0, function* () {
103
+ // Fetch OI data
104
+ const pairOi = yield fetchPairOi(chainId, collateralIndex, pairIndex, signer);
105
+ // For now, use a placeholder price - in real usage, this would come from price feeds
106
+ // The actual price should be fetched from the price aggregator or oracle
107
+ const currentPrice = 1; // Placeholder - replace with actual price fetching
108
+ // Compute derived values
109
+ const totalDynamicOi = {
110
+ long: pairOi.beforeV10Collateral.long + pairOi.token.long * currentPrice,
111
+ short: pairOi.beforeV10Collateral.short + pairOi.token.short * currentPrice,
112
+ };
113
+ const totalStaticOi = {
114
+ long: pairOi.beforeV10Collateral.long + pairOi.collateral.long,
115
+ short: pairOi.beforeV10Collateral.short + pairOi.collateral.short,
116
+ };
117
+ const skew = pairOi.token.long - pairOi.token.short;
118
+ return {
119
+ pairOi,
120
+ currentPrice,
121
+ computed: {
122
+ totalDynamicOi,
123
+ totalStaticOi,
124
+ skew,
125
+ },
126
+ };
127
+ });
128
+ }
129
+ exports.createOiContext = createOiContext;
130
+ /**
131
+ * @dev Fetches only the OI data needed for specific use cases
132
+ * @param chainId Target chain
133
+ * @param collateralIndex Collateral type
134
+ * @param pairIndex Trading pair
135
+ * @param useCase Which OI systems to fetch
136
+ * @param signer Ethers signer
137
+ * @returns Partial OI data based on use case
138
+ */
139
+ function fetchOiForUseCase(chainId, collateralIndex, pairIndex, useCase, signer) {
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ const addresses = (0, contracts_1.getContractAddressesForChain)(chainId);
142
+ const diamond = generated_1.GNSMultiCollatDiamond__factory.connect(addresses.gnsMultiCollatDiamond, signer);
143
+ switch (useCase) {
144
+ case "skew":
145
+ case "funding": {
146
+ // Only need token OI
147
+ const tokenOi = yield diamond.getPairOiAfterV10Token(collateralIndex, pairIndex);
148
+ return {
149
+ token: (0, converter_1.convertTokenOi)(tokenOi),
150
+ };
151
+ }
152
+ case "borrowingV1": {
153
+ // Need beforeV10 and token (for dynamic calculation)
154
+ const [beforeV10Raw, tokenOi, collateral, maxOi] = yield Promise.all([
155
+ diamond.getPairOisBeforeV10Collateral(collateralIndex, pairIndex),
156
+ diamond.getPairOiAfterV10Token(collateralIndex, pairIndex),
157
+ diamond.getCollateral(collateralIndex),
158
+ diamond.getPairMaxOi(collateralIndex, pairIndex),
159
+ ]);
160
+ // Convert the beforeV10 format to match expected structure
161
+ const beforeV10 = {
162
+ long: beforeV10Raw.longOi,
163
+ short: beforeV10Raw.shortOi,
164
+ max: maxOi,
165
+ __placeholder: ethers_1.ethers.BigNumber.from(0),
166
+ };
167
+ return {
168
+ beforeV10Collateral: (0, converter_1.convertBeforeV10Collateral)(beforeV10, Number(collateral.precision)),
169
+ token: (0, converter_1.convertTokenOi)(tokenOi),
170
+ };
171
+ }
172
+ case "limits": {
173
+ // Need all OI for limit checks
174
+ return fetchPairOi(chainId, collateralIndex, pairIndex, signer);
175
+ }
176
+ default:
177
+ throw new Error(`Unknown use case: ${String(useCase)}`);
178
+ }
179
+ });
180
+ }
181
+ exports.fetchOiForUseCase = fetchOiForUseCase;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @dev Main export file for OI module
3
+ * @dev Provides unified Open Interest management functionality
4
+ */
5
+ import { GenericPairOiContext } from "./types";
6
+ export declare const getPairTotalOisCollateral: (pairIndex: number, context: GenericPairOiContext) => {
7
+ long: number;
8
+ short: number;
9
+ };
10
+ /**
11
+ * @dev Returns pair total dynamic open interest (before v10 + after v10) in collateral tokens
12
+ * @param pairIndex index of pair
13
+ * @param context contains UnifiedPairOi array and current pair price
14
+ * @returns dynamic OI for long and short sides in collateral precision
15
+ */
16
+ export declare const getPairTotalOisDynamicCollateral: (pairIndex: number, context: GenericPairOiContext & {
17
+ currentPairPrice: number;
18
+ }) => {
19
+ long: number;
20
+ short: number;
21
+ };
22
+ /**
23
+ * @dev Returns pair total dynamic open interest (before v10 + after v10) in collateral tokens on one side only
24
+ * @param pairIndex index of pair
25
+ * @param long true if long, false if short
26
+ * @param context contains UnifiedPairOi array and current pair price
27
+ * @returns dynamic OI for the specified side in collateral precision
28
+ */
29
+ export declare const getPairTotalOiDynamicCollateral: (pairIndex: number, long: boolean, context: GenericPairOiContext & {
30
+ currentPairPrice: number;
31
+ }) => number;
32
+ /**
33
+ * @dev Returns pair open interest skew (v10 only) in tokens
34
+ * @param pairIndex index of pair
35
+ * @param context contains UnifiedPairOi array
36
+ * @returns skew in token amount (positive = more longs, negative = more shorts)
37
+ */
38
+ export declare const getPairV10OiTokenSkewCollateral: (pairIndex: number, context: GenericPairOiContext) => number;
39
+ /**
40
+ * @dev Returns pair dynamic skew (v10 only) in collateral tokens
41
+ * @param pairIndex index of pair
42
+ * @param context contains UnifiedPairOi array and current pair price
43
+ * @returns dynamic skew in collateral precision
44
+ */
45
+ export declare const getPairV10OiDynamicSkewCollateral: (pairIndex: number, context: GenericPairOiContext & {
46
+ currentPairPrice: number;
47
+ }) => number;
48
+ export { UnifiedPairOi, GroupOi, ComputedOi } from "./types";
49
+ export { convertBeforeV10Collateral, convertCollateralOi, convertTokenOi, convertPairOi, convertPairOiArray, computeOiValues, } from "./converter";
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Main export file for OI module
4
+ * @dev Provides unified Open Interest management functionality
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.computeOiValues = exports.convertPairOiArray = exports.convertPairOi = exports.convertTokenOi = exports.convertCollateralOi = exports.convertBeforeV10Collateral = exports.getPairV10OiDynamicSkewCollateral = exports.getPairV10OiTokenSkewCollateral = exports.getPairTotalOiDynamicCollateral = exports.getPairTotalOisDynamicCollateral = exports.getPairTotalOisCollateral = void 0;
8
+ const getPairTotalOisCollateral = (pairIndex, context) => {
9
+ return {
10
+ long: context.pairOis[pairIndex].beforeV10Collateral.long +
11
+ context.pairOis[pairIndex].collateral.long,
12
+ short: context.pairOis[pairIndex].beforeV10Collateral.short +
13
+ context.pairOis[pairIndex].collateral.short,
14
+ };
15
+ };
16
+ exports.getPairTotalOisCollateral = getPairTotalOisCollateral;
17
+ /**
18
+ * @dev Returns pair total dynamic open interest (before v10 + after v10) in collateral tokens
19
+ * @param pairIndex index of pair
20
+ * @param context contains UnifiedPairOi array and current pair price
21
+ * @returns dynamic OI for long and short sides in collateral precision
22
+ */
23
+ const getPairTotalOisDynamicCollateral = (pairIndex, context) => {
24
+ const pairOi = context.pairOis[pairIndex];
25
+ // We have to use the initial collateral OIs for pre-v10 trades because we don't have OIs in token amount
26
+ const oiLongCollateralDynamicAfterV10 = pairOi.beforeV10Collateral.long +
27
+ pairOi.token.long * context.currentPairPrice;
28
+ const oiShortCollateralDynamicAfterV10 = pairOi.beforeV10Collateral.short +
29
+ pairOi.token.short * context.currentPairPrice;
30
+ return {
31
+ long: oiLongCollateralDynamicAfterV10,
32
+ short: oiShortCollateralDynamicAfterV10,
33
+ };
34
+ };
35
+ exports.getPairTotalOisDynamicCollateral = getPairTotalOisDynamicCollateral;
36
+ /**
37
+ * @dev Returns pair total dynamic open interest (before v10 + after v10) in collateral tokens on one side only
38
+ * @param pairIndex index of pair
39
+ * @param long true if long, false if short
40
+ * @param context contains UnifiedPairOi array and current pair price
41
+ * @returns dynamic OI for the specified side in collateral precision
42
+ */
43
+ const getPairTotalOiDynamicCollateral = (pairIndex, long, context) => {
44
+ const dynamicOis = (0, exports.getPairTotalOisDynamicCollateral)(pairIndex, context);
45
+ return long ? dynamicOis.long : dynamicOis.short;
46
+ };
47
+ exports.getPairTotalOiDynamicCollateral = getPairTotalOiDynamicCollateral;
48
+ /**
49
+ * @dev Returns pair open interest skew (v10 only) in tokens
50
+ * @param pairIndex index of pair
51
+ * @param context contains UnifiedPairOi array
52
+ * @returns skew in token amount (positive = more longs, negative = more shorts)
53
+ */
54
+ const getPairV10OiTokenSkewCollateral = (pairIndex, context) => {
55
+ const pairOi = context.pairOis[pairIndex];
56
+ return pairOi.token.long - pairOi.token.short;
57
+ };
58
+ exports.getPairV10OiTokenSkewCollateral = getPairV10OiTokenSkewCollateral;
59
+ /**
60
+ * @dev Returns pair dynamic skew (v10 only) in collateral tokens
61
+ * @param pairIndex index of pair
62
+ * @param context contains UnifiedPairOi array and current pair price
63
+ * @returns dynamic skew in collateral precision
64
+ */
65
+ const getPairV10OiDynamicSkewCollateral = (pairIndex, context) => {
66
+ return ((0, exports.getPairV10OiTokenSkewCollateral)(pairIndex, context) *
67
+ context.currentPairPrice);
68
+ };
69
+ exports.getPairV10OiDynamicSkewCollateral = getPairV10OiDynamicSkewCollateral;
70
+ // Converters
71
+ var converter_1 = require("./converter");
72
+ Object.defineProperty(exports, "convertBeforeV10Collateral", { enumerable: true, get: function () { return converter_1.convertBeforeV10Collateral; } });
73
+ Object.defineProperty(exports, "convertCollateralOi", { enumerable: true, get: function () { return converter_1.convertCollateralOi; } });
74
+ Object.defineProperty(exports, "convertTokenOi", { enumerable: true, get: function () { return converter_1.convertTokenOi; } });
75
+ Object.defineProperty(exports, "convertPairOi", { enumerable: true, get: function () { return converter_1.convertPairOi; } });
76
+ Object.defineProperty(exports, "convertPairOiArray", { enumerable: true, get: function () { return converter_1.convertPairOiArray; } });
77
+ Object.defineProperty(exports, "computeOiValues", { enumerable: true, get: function () { return converter_1.computeOiValues; } });
@@ -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;