@gainsnetwork/sdk 0.0.0-v10.rc1 → 0.0.0-v10.rc3

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 (176) 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 +2 -0
  4. package/lib/backend/index.js +18 -0
  5. package/lib/backend/tradingVariables/backend.types.d.ts +312 -0
  6. package/lib/backend/tradingVariables/backend.types.js +2 -0
  7. package/lib/backend/tradingVariables/converter.d.ts +30 -0
  8. package/lib/backend/tradingVariables/converter.js +329 -0
  9. package/lib/backend/tradingVariables/index.d.ts +3 -0
  10. package/lib/backend/tradingVariables/index.js +78 -0
  11. package/lib/backend/tradingVariables/types.d.ts +109 -0
  12. package/lib/backend/tradingVariables/types.js +14 -0
  13. package/lib/constants.d.ts +8 -1
  14. package/lib/constants.js +8 -1
  15. package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +75 -0
  16. package/lib/contracts/fetch/fees/borrowingFeesV2.js +193 -0
  17. package/lib/contracts/fetch/fees/fundingFees.d.ts +66 -0
  18. package/lib/contracts/fetch/fees/fundingFees.js +150 -0
  19. package/lib/contracts/fetch/priceImpact/skew.d.ts +63 -0
  20. package/lib/contracts/fetch/priceImpact/skew.js +168 -0
  21. package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
  22. package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
  23. package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
  24. package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
  25. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
  26. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
  27. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
  28. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
  29. package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
  30. package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
  31. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
  32. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
  33. package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
  34. package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
  35. package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
  36. package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
  37. package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
  38. package/lib/contracts/types/generated/GNSTrading.js +2 -0
  39. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
  40. package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
  41. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
  42. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
  43. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
  44. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
  45. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
  46. package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
  47. package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
  48. package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
  49. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
  50. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
  51. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
  52. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
  53. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
  54. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
  55. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
  56. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
  57. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  58. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  59. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
  60. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
  61. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
  62. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
  63. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
  64. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
  65. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
  66. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
  67. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
  68. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
  69. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
  70. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
  71. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
  72. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
  73. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
  74. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
  75. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
  76. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -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/openTrades.d.ts +1 -0
  80. package/lib/contracts/utils/openTrades.js +94 -56
  81. package/lib/contracts/utils/pairs.js +7 -0
  82. package/lib/index.d.ts +1 -0
  83. package/lib/index.js +1 -0
  84. package/lib/markets/collateral/converter.d.ts +5 -0
  85. package/lib/markets/collateral/converter.js +11 -0
  86. package/lib/markets/collateral/index.d.ts +1 -0
  87. package/lib/markets/collateral/index.js +17 -0
  88. package/lib/markets/collateral/types.d.ts +7 -0
  89. package/lib/markets/collateral/types.js +2 -0
  90. package/lib/markets/index.d.ts +2 -0
  91. package/lib/markets/index.js +2 -0
  92. package/lib/markets/oi/converter.d.ts +63 -0
  93. package/lib/markets/oi/converter.js +103 -0
  94. package/lib/markets/oi/fetcher.d.ts +58 -0
  95. package/lib/markets/oi/fetcher.js +181 -0
  96. package/lib/markets/oi/index.d.ts +10 -0
  97. package/lib/markets/oi/index.js +37 -0
  98. package/lib/markets/oi/types.d.ts +82 -0
  99. package/lib/markets/oi/types.js +6 -0
  100. package/lib/markets/oi/validation.d.ts +80 -0
  101. package/lib/markets/oi/validation.js +172 -0
  102. package/lib/trade/fees/borrowing/index.d.ts +22 -1
  103. package/lib/trade/fees/borrowing/index.js +39 -13
  104. package/lib/trade/fees/borrowingV2/converter.d.ts +66 -0
  105. package/lib/trade/fees/borrowingV2/converter.js +121 -0
  106. package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
  107. package/lib/trade/fees/borrowingV2/fetcher.js +193 -0
  108. package/lib/trade/fees/borrowingV2/index.d.ts +60 -0
  109. package/lib/trade/fees/borrowingV2/index.js +140 -0
  110. package/lib/trade/fees/borrowingV2/types.d.ts +79 -0
  111. package/lib/trade/fees/borrowingV2/types.js +5 -0
  112. package/lib/trade/fees/converter.d.ts +48 -0
  113. package/lib/trade/fees/converter.js +110 -0
  114. package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
  115. package/lib/trade/fees/fundingFees/converter.js +196 -0
  116. package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
  117. package/lib/trade/fees/fundingFees/fetcher.js +150 -0
  118. package/lib/trade/fees/fundingFees/index.d.ts +136 -0
  119. package/lib/trade/fees/fundingFees/index.js +326 -0
  120. package/lib/trade/fees/fundingFees/types.d.ts +77 -0
  121. package/lib/trade/fees/fundingFees/types.js +5 -0
  122. package/lib/trade/fees/index.d.ts +7 -2
  123. package/lib/trade/fees/index.js +67 -16
  124. package/lib/trade/fees/tiers/converter.d.ts +54 -0
  125. package/lib/trade/fees/tiers/converter.js +81 -0
  126. package/lib/trade/fees/tiers/index.d.ts +18 -0
  127. package/lib/trade/fees/tiers/index.js +45 -1
  128. package/lib/trade/fees/trading/converter.d.ts +30 -0
  129. package/lib/trade/fees/trading/converter.js +43 -0
  130. package/lib/trade/fees/trading/index.d.ts +54 -0
  131. package/lib/trade/fees/trading/index.js +147 -0
  132. package/lib/trade/fees/trading/types.d.ts +48 -0
  133. package/lib/trade/fees/trading/types.js +5 -0
  134. package/lib/trade/index.d.ts +3 -2
  135. package/lib/trade/index.js +3 -2
  136. package/lib/trade/liquidation/converter.d.ts +23 -0
  137. package/lib/trade/liquidation/converter.js +46 -0
  138. package/lib/trade/liquidation/index.d.ts +31 -0
  139. package/lib/trade/liquidation/index.js +187 -0
  140. package/lib/trade/liquidation/types.d.ts +44 -0
  141. package/lib/trade/liquidation/types.js +2 -0
  142. package/lib/trade/pnl/converter.d.ts +47 -0
  143. package/lib/trade/pnl/converter.js +72 -0
  144. package/lib/trade/pnl/index.d.ts +86 -0
  145. package/lib/trade/pnl/index.js +201 -0
  146. package/lib/trade/pnl/types.d.ts +86 -0
  147. package/lib/trade/pnl/types.js +5 -0
  148. package/lib/trade/priceImpact/close/index.d.ts +21 -0
  149. package/lib/trade/priceImpact/close/index.js +131 -0
  150. package/lib/trade/priceImpact/close/types.d.ts +43 -0
  151. package/lib/trade/priceImpact/close/types.js +5 -0
  152. package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
  153. package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
  154. package/lib/trade/priceImpact/cumulVol/index.d.ts +107 -0
  155. package/lib/trade/priceImpact/cumulVol/index.js +228 -0
  156. package/lib/trade/priceImpact/index.d.ts +12 -0
  157. package/lib/trade/priceImpact/index.js +59 -0
  158. package/lib/trade/priceImpact/open/index.d.ts +22 -0
  159. package/lib/trade/priceImpact/open/index.js +76 -0
  160. package/lib/trade/priceImpact/open/types.d.ts +41 -0
  161. package/lib/trade/priceImpact/open/types.js +5 -0
  162. package/lib/trade/priceImpact/skew/converter.d.ts +77 -0
  163. package/lib/trade/priceImpact/skew/converter.js +171 -0
  164. package/lib/trade/priceImpact/skew/fetcher.d.ts +63 -0
  165. package/lib/trade/priceImpact/skew/fetcher.js +168 -0
  166. package/lib/trade/priceImpact/skew/index.d.ts +58 -0
  167. package/lib/trade/priceImpact/skew/index.js +179 -0
  168. package/lib/trade/priceImpact/skew/types.d.ts +55 -0
  169. package/lib/trade/priceImpact/skew/types.js +5 -0
  170. package/lib/trade/spread.d.ts +5 -18
  171. package/lib/trade/spread.js +17 -106
  172. package/lib/trade/types.d.ts +34 -9
  173. package/lib/trade/types.js +7 -0
  174. package/lib/trade/utils.d.ts +18 -0
  175. package/lib/trade/utils.js +30 -0
  176. package/package.json +2 -1
@@ -0,0 +1,140 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.BorrowingFeeV2 = exports.borrowingFeeV2Utils = exports.getPairBorrowingFees = exports.getBorrowingFee = exports.getTradeBorrowingFeesCollateral = exports.getPairPendingAccBorrowingFees = exports.BORROWING_V2_PRECISION = exports.MAX_BORROWING_RATE_PER_SECOND = void 0;
30
+ /**
31
+ * @dev Maximum borrowing rate per second (1,000% APR)
32
+ */
33
+ exports.MAX_BORROWING_RATE_PER_SECOND = 0.0317097; // 317097 / 1e10
34
+ /**
35
+ * @dev Precision constants for borrowing v2 calculations
36
+ */
37
+ exports.BORROWING_V2_PRECISION = {
38
+ RATE_PER_SECOND: 1e10,
39
+ ACC_FEE: 1e20,
40
+ PERCENTAGE: 100,
41
+ };
42
+ /**
43
+ * @dev Calculates pending accumulated borrowing fees for a pair
44
+ * @param params Borrowing fee parameters for the pair
45
+ * @param data Current borrowing fee data for the pair
46
+ * @param currentPairPrice Current price of the trading pair
47
+ * @param currentTimestamp Current timestamp (defaults to now)
48
+ * @returns Updated accumulated borrowing fee (1e20 precision)
49
+ */
50
+ const getPairPendingAccBorrowingFees = (params, data, currentPairPrice, currentTimestamp) => {
51
+ const timestamp = currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000);
52
+ // Calculate time elapsed since last update
53
+ const timeElapsed = Math.max(0, timestamp - data.lastBorrowingUpdateTs);
54
+ // If no time elapsed, return current accumulated fee
55
+ if (timeElapsed === 0) {
56
+ return data.accBorrowingFeeP;
57
+ }
58
+ // Calculate accumulated borrowing fee delta
59
+ // Formula: borrowingRatePerSecondP * timeElapsed * currentPairPrice
60
+ const accBorrowingFeeDeltaP = params.borrowingRatePerSecondP * timeElapsed * currentPairPrice;
61
+ return data.accBorrowingFeeP + accBorrowingFeeDeltaP;
62
+ };
63
+ exports.getPairPendingAccBorrowingFees = getPairPendingAccBorrowingFees;
64
+ /**
65
+ * @dev Calculates borrowing fees owed by a specific trade
66
+ * @param input Trade borrowing fee calculation input
67
+ * @param context Context containing borrowing parameters and data
68
+ * @returns Borrowing fees in collateral tokens
69
+ */
70
+ const getTradeBorrowingFeesCollateral = (input, context) => {
71
+ var _a, _b;
72
+ const { positionSizeCollateral, openPrice, collateralIndex, pairIndex, currentPairPrice, initialAccBorrowingFeeP, currentTimestamp, } = input;
73
+ // Get borrowing parameters and data for the pair
74
+ const params = (_a = context.borrowingParams[collateralIndex]) === null || _a === void 0 ? void 0 : _a[pairIndex];
75
+ const data = (_b = context.borrowingData[collateralIndex]) === null || _b === void 0 ? void 0 : _b[pairIndex];
76
+ if (!params || !data) {
77
+ return 0;
78
+ }
79
+ // Calculate current accumulated borrowing fees
80
+ const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp);
81
+ // Calculate borrowing fees for this trade
82
+ // Formula: (positionSizeCollateral * (currentAccFee - initialAccFee)) / openPrice / 100
83
+ const feeDeltaP = currentAccBorrowingFeeP - initialAccBorrowingFeeP;
84
+ return ((positionSizeCollateral * feeDeltaP) /
85
+ openPrice /
86
+ exports.BORROWING_V2_PRECISION.PERCENTAGE);
87
+ };
88
+ exports.getTradeBorrowingFeesCollateral = getTradeBorrowingFeesCollateral;
89
+ /**
90
+ * @dev Convenience function to calculate borrowing fees for a trade using individual parameters
91
+ * @param positionSizeCollateral Position size in collateral tokens
92
+ * @param pairIndex Index of the trading pair
93
+ * @param collateralIndex Index of the collateral
94
+ * @param openPrice Price at which the trade was opened
95
+ * @param currentPairPrice Current price of the trading pair
96
+ * @param initialAccBorrowingFeeP Initial accumulated borrowing fee when trade was opened
97
+ * @param context Context containing borrowing parameters and data
98
+ * @returns Borrowing fees in collateral tokens
99
+ */
100
+ const getBorrowingFee = (positionSizeCollateral, pairIndex, collateralIndex, openPrice, currentPairPrice, initialAccBorrowingFeeP, context) => {
101
+ return (0, exports.getTradeBorrowingFeesCollateral)({
102
+ positionSizeCollateral,
103
+ openPrice,
104
+ collateralIndex,
105
+ pairIndex,
106
+ currentPairPrice,
107
+ initialAccBorrowingFeeP,
108
+ currentTimestamp: context.currentTimestamp,
109
+ }, context);
110
+ };
111
+ exports.getBorrowingFee = getBorrowingFee;
112
+ /**
113
+ * @dev Utility function to get pending accumulated borrowing fees for a pair using context
114
+ * @param input Pair borrowing fee calculation input
115
+ * @param context Context containing borrowing parameters and data
116
+ * @returns Updated accumulated borrowing fee (1e20 precision)
117
+ */
118
+ const getPairBorrowingFees = (input, context) => {
119
+ var _a, _b;
120
+ const { collateralIndex, pairIndex, currentPairPrice, currentTimestamp } = input;
121
+ const params = (_a = context.borrowingParams[collateralIndex]) === null || _a === void 0 ? void 0 : _a[pairIndex];
122
+ const data = (_b = context.borrowingData[collateralIndex]) === null || _b === void 0 ? void 0 : _b[pairIndex];
123
+ if (!params || !data) {
124
+ return 0;
125
+ }
126
+ return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : context.currentTimestamp);
127
+ };
128
+ exports.getPairBorrowingFees = getPairBorrowingFees;
129
+ /**
130
+ * @dev Utility functions for working with borrowing v2 fees
131
+ */
132
+ exports.borrowingFeeV2Utils = {
133
+ getPairPendingAccBorrowingFees: exports.getPairPendingAccBorrowingFees,
134
+ getTradeBorrowingFeesCollateral: exports.getTradeBorrowingFeesCollateral,
135
+ getPairBorrowingFees: exports.getPairBorrowingFees,
136
+ getBorrowingFee: exports.getBorrowingFee,
137
+ };
138
+ exports.BorrowingFeeV2 = __importStar(require("./types"));
139
+ __exportStar(require("./converter"), exports);
140
+ __exportStar(require("./fetcher"), exports);
@@ -0,0 +1,79 @@
1
+ /**
2
+ * @dev Types for borrowing v2 fees system (simplified rate-based model)
3
+ */
4
+ /**
5
+ * @dev Borrowing fee parameters for a specific pair/collateral combination
6
+ */
7
+ export type BorrowingFeeParams = {
8
+ /**
9
+ * @dev Borrowing rate % per second
10
+ * @dev Raw contract value divided by 1e10
11
+ * @dev Max: ~0.0000317097 => 1,000% APR
12
+ */
13
+ borrowingRatePerSecondP: number;
14
+ };
15
+ /**
16
+ * @dev Accumulated borrowing fee data for a specific pair/collateral combination
17
+ */
18
+ export type PairBorrowingFeeData = {
19
+ /**
20
+ * @dev Accumulated borrowing fees % weighted by pair price
21
+ * @dev Unit: fee collateral per 100 units of OI in pair amount
22
+ */
23
+ accBorrowingFeeP: number;
24
+ /**
25
+ * @dev Timestamp of last accumulated borrowing fees update
26
+ */
27
+ lastBorrowingUpdateTs: number;
28
+ };
29
+ /**
30
+ * @dev Trade-specific borrowing fee data stored when position is opened
31
+ */
32
+ export type TradeInitialAccFees = {
33
+ /**
34
+ * @dev Initial accumulated borrowing fee when trade was opened (normalized float)
35
+ * @dev Raw contract value divided by 1e20
36
+ * @dev Used to calculate how much borrowing fees the trade owes
37
+ */
38
+ initialAccBorrowingFeeP: number;
39
+ };
40
+ /**
41
+ * @dev Context required for borrowing v2 fee calculations
42
+ */
43
+ export type GetBorrowingFeeV2Context = {
44
+ /**
45
+ * @dev Current timestamp (defaults to Date.now() / 1000)
46
+ */
47
+ currentTimestamp?: number;
48
+ /**
49
+ * @dev Borrowing fee parameters for pairs
50
+ * @dev Indexed by: params[collateralIndex][pairIndex]
51
+ */
52
+ borrowingParams: Record<number, Record<number, BorrowingFeeParams>>;
53
+ /**
54
+ * @dev Borrowing fee data for pairs
55
+ * @dev Indexed by: data[collateralIndex][pairIndex]
56
+ */
57
+ borrowingData: Record<number, Record<number, PairBorrowingFeeData>>;
58
+ };
59
+ /**
60
+ * @dev Input for calculating pending borrowing fees for a pair
61
+ */
62
+ export type PairBorrowingFeeInput = {
63
+ collateralIndex: number;
64
+ pairIndex: number;
65
+ currentPairPrice: number;
66
+ currentTimestamp?: number;
67
+ };
68
+ /**
69
+ * @dev Input for calculating borrowing fees for a specific trade
70
+ */
71
+ export type TradeBorrowingFeeInput = {
72
+ positionSizeCollateral: number;
73
+ openPrice: number;
74
+ collateralIndex: number;
75
+ pairIndex: number;
76
+ currentPairPrice: number;
77
+ initialAccBorrowingFeeP: number;
78
+ currentTimestamp?: number;
79
+ };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Types for borrowing v2 fees system (simplified rate-based model)
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @dev Converters for fee data between contract and SDK formats
3
+ */
4
+ import { IFundingFees } from "../../contracts/types/generated/GNSMultiCollatDiamond";
5
+ import { TradeFeesData, UiRealizedPnlData } from "../types";
6
+ import { CollateralConfig } from "../../markets/collateral/types";
7
+ /**
8
+ * @dev Converts contract TradeFeesData to SDK format
9
+ * @param data Trade fees data from contract
10
+ * @param collateralConfig Config for the collateral (contains decimals)
11
+ * @returns Normalized trade fees data
12
+ */
13
+ export declare const convertTradeFeesData: (data: IFundingFees.TradeFeesDataStructOutput, collateralConfig: CollateralConfig) => TradeFeesData;
14
+ /**
15
+ * @dev Converts array of TradeFeesData from contract
16
+ * @param dataArray Array of trade fees data
17
+ * @param collateralConfig Config for the collateral
18
+ * @returns Array of normalized trade fees data
19
+ */
20
+ export declare const convertTradeFeesDataArray: (dataArray: IFundingFees.TradeFeesDataStructOutput[], collateralConfig: CollateralConfig) => TradeFeesData[];
21
+ /**
22
+ * @dev Converts contract UiRealizedPnlData to SDK format
23
+ * @param data UI realized PnL data from contract
24
+ * @param collateralConfig Config for the collateral (contains decimals)
25
+ * @returns Normalized UI realized PnL data
26
+ */
27
+ export declare const convertUiRealizedPnlData: (data: IFundingFees.UiRealizedPnlDataStructOutput, collateralConfig: CollateralConfig) => UiRealizedPnlData;
28
+ /**
29
+ * @dev Converts array of UiRealizedPnlData from contract
30
+ * @param dataArray Array of UI realized PnL data
31
+ * @param collateralConfig Config for the collateral
32
+ * @returns Array of normalized UI realized PnL data
33
+ */
34
+ export declare const convertUiRealizedPnlDataArray: (dataArray: IFundingFees.UiRealizedPnlDataStructOutput[], collateralConfig: CollateralConfig) => UiRealizedPnlData[];
35
+ /**
36
+ * @dev Converts TradeFeesData to contract format (for encoding)
37
+ * @param data SDK trade fees data
38
+ * @param collateralConfig Config for the collateral
39
+ * @returns Contract-formatted trade fees data
40
+ */
41
+ export declare const encodeTradeFeesData: (data: TradeFeesData, collateralConfig: CollateralConfig) => IFundingFees.TradeFeesDataStruct;
42
+ /**
43
+ * @dev Converts UiRealizedPnlData to contract format (for encoding)
44
+ * @param data SDK UI realized PnL data
45
+ * @param collateralConfig Config for the collateral
46
+ * @returns Contract-formatted UI realized PnL data
47
+ */
48
+ export declare const encodeUiRealizedPnlData: (data: UiRealizedPnlData, collateralConfig: CollateralConfig) => IFundingFees.UiRealizedPnlDataStruct;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Converters for fee data between contract and SDK formats
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.encodeUiRealizedPnlData = exports.encodeTradeFeesData = exports.convertUiRealizedPnlDataArray = exports.convertUiRealizedPnlData = exports.convertTradeFeesDataArray = exports.convertTradeFeesData = void 0;
7
+ /**
8
+ * @dev Converts contract TradeFeesData to SDK format
9
+ * @param data Trade fees data from contract
10
+ * @param collateralConfig Config for the collateral (contains decimals)
11
+ * @returns Normalized trade fees data
12
+ */
13
+ const convertTradeFeesData = (data, collateralConfig) => {
14
+ const decimals = collateralConfig.decimals || 18;
15
+ return {
16
+ realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
17
+ Math.pow(10, decimals),
18
+ realizedPnlCollateral: parseFloat(data.realizedPnlCollateral.toString()) / Math.pow(10, decimals),
19
+ manuallyRealizedNegativePnlCollateral: parseFloat(data.manuallyRealizedNegativePnlCollateral.toString()) /
20
+ Math.pow(10, decimals),
21
+ alreadyTransferredNegativePnlCollateral: parseFloat(data.alreadyTransferredNegativePnlCollateral.toString()) /
22
+ Math.pow(10, decimals),
23
+ virtualAvailableCollateralInDiamond: parseFloat(data.virtualAvailableCollateralInDiamond.toString()) /
24
+ Math.pow(10, decimals),
25
+ initialAccFundingFeeP: parseFloat(data.initialAccFundingFeeP.toString()) / 1e10,
26
+ initialAccBorrowingFeeP: parseFloat(data.initialAccBorrowingFeeP.toString()) / 1e10,
27
+ };
28
+ };
29
+ exports.convertTradeFeesData = convertTradeFeesData;
30
+ /**
31
+ * @dev Converts array of TradeFeesData from contract
32
+ * @param dataArray Array of trade fees data
33
+ * @param collateralConfig Config for the collateral
34
+ * @returns Array of normalized trade fees data
35
+ */
36
+ const convertTradeFeesDataArray = (dataArray, collateralConfig) => {
37
+ return dataArray.map(data => (0, exports.convertTradeFeesData)(data, collateralConfig));
38
+ };
39
+ exports.convertTradeFeesDataArray = convertTradeFeesDataArray;
40
+ /**
41
+ * @dev Converts contract UiRealizedPnlData to SDK format
42
+ * @param data UI realized PnL data from contract
43
+ * @param collateralConfig Config for the collateral (contains decimals)
44
+ * @returns Normalized UI realized PnL data
45
+ */
46
+ const convertUiRealizedPnlData = (data, collateralConfig) => {
47
+ const decimals = collateralConfig.decimals || 18;
48
+ return {
49
+ realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
50
+ Math.pow(10, decimals),
51
+ realizedOldBorrowingFeesCollateral: parseFloat(data.realizedOldBorrowingFeesCollateral.toString()) /
52
+ Math.pow(10, decimals),
53
+ realizedNewBorrowingFeesCollateral: parseFloat(data.realizedNewBorrowingFeesCollateral.toString()) /
54
+ Math.pow(10, decimals),
55
+ realizedFundingFeesCollateral: parseFloat(data.realizedFundingFeesCollateral.toString()) /
56
+ Math.pow(10, decimals),
57
+ realizedPnlPartialCloseCollateral: parseFloat(data.realizedPnlPartialCloseCollateral.toString()) /
58
+ Math.pow(10, decimals),
59
+ pnlWithdrawnCollateral: parseFloat(data.pnlWithdrawnCollateral.toString()) / Math.pow(10, decimals),
60
+ };
61
+ };
62
+ exports.convertUiRealizedPnlData = convertUiRealizedPnlData;
63
+ /**
64
+ * @dev Converts array of UiRealizedPnlData from contract
65
+ * @param dataArray Array of UI realized PnL data
66
+ * @param collateralConfig Config for the collateral
67
+ * @returns Array of normalized UI realized PnL data
68
+ */
69
+ const convertUiRealizedPnlDataArray = (dataArray, collateralConfig) => {
70
+ return dataArray.map(data => (0, exports.convertUiRealizedPnlData)(data, collateralConfig));
71
+ };
72
+ exports.convertUiRealizedPnlDataArray = convertUiRealizedPnlDataArray;
73
+ /**
74
+ * @dev Converts TradeFeesData to contract format (for encoding)
75
+ * @param data SDK trade fees data
76
+ * @param collateralConfig Config for the collateral
77
+ * @returns Contract-formatted trade fees data
78
+ */
79
+ const encodeTradeFeesData = (data, collateralConfig) => {
80
+ const decimals = collateralConfig.decimals || 18;
81
+ return {
82
+ realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * Math.pow(10, decimals)),
83
+ realizedPnlCollateral: Math.round(data.realizedPnlCollateral * Math.pow(10, decimals)),
84
+ manuallyRealizedNegativePnlCollateral: Math.round(data.manuallyRealizedNegativePnlCollateral * Math.pow(10, decimals)),
85
+ alreadyTransferredNegativePnlCollateral: Math.round(data.alreadyTransferredNegativePnlCollateral * Math.pow(10, decimals)),
86
+ virtualAvailableCollateralInDiamond: Math.round(data.virtualAvailableCollateralInDiamond * Math.pow(10, decimals)),
87
+ __placeholder: 0,
88
+ initialAccFundingFeeP: Math.round(data.initialAccFundingFeeP * 1e10),
89
+ initialAccBorrowingFeeP: Math.round(data.initialAccBorrowingFeeP * 1e10),
90
+ };
91
+ };
92
+ exports.encodeTradeFeesData = encodeTradeFeesData;
93
+ /**
94
+ * @dev Converts UiRealizedPnlData to contract format (for encoding)
95
+ * @param data SDK UI realized PnL data
96
+ * @param collateralConfig Config for the collateral
97
+ * @returns Contract-formatted UI realized PnL data
98
+ */
99
+ const encodeUiRealizedPnlData = (data, collateralConfig) => {
100
+ const decimals = collateralConfig.decimals || 18;
101
+ return {
102
+ realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * Math.pow(10, decimals)),
103
+ realizedOldBorrowingFeesCollateral: Math.round(data.realizedOldBorrowingFeesCollateral * Math.pow(10, decimals)),
104
+ realizedNewBorrowingFeesCollateral: Math.round(data.realizedNewBorrowingFeesCollateral * Math.pow(10, decimals)),
105
+ realizedFundingFeesCollateral: Math.round(data.realizedFundingFeesCollateral * Math.pow(10, decimals)),
106
+ realizedPnlPartialCloseCollateral: Math.round(data.realizedPnlPartialCloseCollateral * Math.pow(10, decimals)),
107
+ pnlWithdrawnCollateral: Math.round(data.pnlWithdrawnCollateral * Math.pow(10, decimals)),
108
+ };
109
+ };
110
+ exports.encodeUiRealizedPnlData = encodeUiRealizedPnlData;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * @dev Converters for funding fees data between contract and SDK formats
3
+ * @dev All BigNumber values are normalized to floats with appropriate precision
4
+ */
5
+ import { IFundingFees } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
6
+ import { FundingFeeParams, PairFundingFeeData, PairGlobalParams, TradeInitialAccFundingFees, GetFundingFeeContext } from "./types";
7
+ export declare const FUNDING_FEES_PRECISION: {
8
+ SKEW_COEFFICIENT_PER_YEAR: number;
9
+ ABSOLUTE_VELOCITY_PER_YEAR_CAP: number;
10
+ ABSOLUTE_RATE_PER_SECOND_CAP: number;
11
+ ACC_FUNDING_FEE_P: number;
12
+ FUNDING_RATE_PER_SECOND_P: number;
13
+ };
14
+ /**
15
+ * @dev Converts contract funding fee params to SDK format
16
+ * @param contractParams Contract funding fee params struct
17
+ * @returns Normalized funding fee params
18
+ */
19
+ export declare const convertFundingFeeParams: (contractParams: IFundingFees.FundingFeeParamsStruct) => FundingFeeParams;
20
+ /**
21
+ * @dev Converts array of contract funding fee params to SDK format
22
+ * @param contractParamsArray Array of contract funding fee params
23
+ * @returns Array of normalized funding fee params
24
+ */
25
+ export declare const convertFundingFeeParamsArray: (contractParamsArray: IFundingFees.FundingFeeParamsStruct[]) => FundingFeeParams[];
26
+ /**
27
+ * @dev Converts contract pair funding fee data to SDK format
28
+ * @param contractData Contract pair funding fee data struct
29
+ * @returns Normalized pair funding fee data
30
+ */
31
+ export declare const convertPairFundingFeeData: (contractData: IFundingFees.PairFundingFeeDataStruct) => PairFundingFeeData;
32
+ /**
33
+ * @dev Converts array of contract pair funding fee data to SDK format
34
+ * @param contractDataArray Array of contract pair funding fee data
35
+ * @returns Array of normalized pair funding fee data
36
+ */
37
+ export declare const convertPairFundingFeeDataArray: (contractDataArray: IFundingFees.PairFundingFeeDataStruct[]) => PairFundingFeeData[];
38
+ /**
39
+ * @dev Converts contract pair global params to SDK format
40
+ * @param contractParams Contract pair global params struct
41
+ * @returns Normalized pair global params
42
+ */
43
+ export declare const convertPairGlobalParams: (contractParams: IFundingFees.PairGlobalParamsStruct) => PairGlobalParams;
44
+ /**
45
+ * @dev Converts array of contract pair global params to SDK format
46
+ * @param contractParamsArray Array of contract pair global params
47
+ * @returns Array of normalized pair global params
48
+ */
49
+ export declare const convertPairGlobalParamsArray: (contractParamsArray: IFundingFees.PairGlobalParamsStruct[]) => PairGlobalParams[];
50
+ /**
51
+ * @dev Converts contract trade initial acc funding fees to SDK format
52
+ * @param contractFees Contract trade fees data (only funding fee part)
53
+ * @returns Normalized trade initial acc funding fees
54
+ */
55
+ export declare const convertTradeInitialAccFundingFees: (contractFees: {
56
+ initialAccFundingFeeP: bigint | number | string;
57
+ }) => TradeInitialAccFundingFees;
58
+ /**
59
+ * @dev Creates a funding fee context from arrays of data
60
+ * @param collateralIndices Array of collateral indices
61
+ * @param pairIndices Array of pair indices
62
+ * @param params Array of funding fee parameters
63
+ * @param data Array of pair funding fee data
64
+ * @param globalParams Optional array of global parameters
65
+ * @param currentTimestamp Optional current timestamp
66
+ * @returns Complete funding fee context
67
+ */
68
+ export declare const createFundingFeeContext: (collateralIndices: number[], pairIndices: number[], params: FundingFeeParams[], data: PairFundingFeeData[], globalParams?: PairGlobalParams[], currentTimestamp?: number) => GetFundingFeeContext;
69
+ /**
70
+ * @dev Validates funding rate is within allowed bounds
71
+ * @param absoluteRatePerSecondCap Rate cap (normalized)
72
+ * @returns Whether the rate is valid
73
+ */
74
+ export declare const isValidFundingRate: (absoluteRatePerSecondCap: number) => boolean;
75
+ /**
76
+ * @dev Converts funding rate per second to APR
77
+ * @param ratePerSecondP Funding rate per second (normalized)
78
+ * @returns APR as percentage
79
+ */
80
+ export declare const fundingRateToAPR: (ratePerSecondP: number) => number;
81
+ /**
82
+ * @dev Converts APR to funding rate per second
83
+ * @param apr APR as percentage
84
+ * @returns Funding rate per second (normalized)
85
+ */
86
+ export declare const aprToFundingRate: (apr: number) => number;
87
+ /**
88
+ * @dev Calculates velocity per year from skew coefficient
89
+ * @param skewRatio Current skew ratio (net exposure / total OI)
90
+ * @param skewCoefficientPerYear Skew coefficient per year (normalized)
91
+ * @returns Velocity per year
92
+ */
93
+ export declare const calculateVelocityFromSkew: (skewRatio: number, skewCoefficientPerYear: number) => number;
94
+ /**
95
+ * @dev Creates a GetFundingFeeContext from arrays (alias for consistency)
96
+ * @param collateralIndices Array of collateral indices
97
+ * @param pairIndices Array of pair indices
98
+ * @param params Array of funding fee parameters
99
+ * @param data Array of pair funding fee data
100
+ * @returns Complete funding fee context
101
+ */
102
+ export declare const createGetFundingFeeContext: (collateralIndices: number[], pairIndices: number[], params: FundingFeeParams[], data: PairFundingFeeData[]) => GetFundingFeeContext;