@gainsnetwork/sdk 0.0.0-me-rc1

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 (236) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/lib/backend/globalTrades/index.d.ts +11 -0
  4. package/lib/backend/globalTrades/index.js +69 -0
  5. package/lib/backend/index.d.ts +3 -0
  6. package/lib/backend/index.js +28 -0
  7. package/lib/backend/tradingVariables/backend.types.d.ts +337 -0
  8. package/lib/backend/tradingVariables/backend.types.js +2 -0
  9. package/lib/backend/tradingVariables/converter.d.ts +38 -0
  10. package/lib/backend/tradingVariables/converter.js +359 -0
  11. package/lib/backend/tradingVariables/index.d.ts +5 -0
  12. package/lib/backend/tradingVariables/index.js +98 -0
  13. package/lib/backend/tradingVariables/types.d.ts +115 -0
  14. package/lib/backend/tradingVariables/types.js +14 -0
  15. package/lib/constants.d.ts +483 -0
  16. package/lib/constants.js +516 -0
  17. package/lib/contracts/addresses.d.ts +3 -0
  18. package/lib/contracts/addresses.js +35 -0
  19. package/lib/contracts/addresses.json +266 -0
  20. package/lib/contracts/index.d.ts +14 -0
  21. package/lib/contracts/index.js +92 -0
  22. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +9327 -0
  23. package/lib/contracts/types/generated/GNSMultiCollatDiamond.js +2 -0
  24. package/lib/contracts/types/generated/GToken.d.ts +1917 -0
  25. package/lib/contracts/types/generated/GToken.js +2 -0
  26. package/lib/contracts/types/generated/GTokenOpenPnlFeed.d.ts +557 -0
  27. package/lib/contracts/types/generated/GTokenOpenPnlFeed.js +2 -0
  28. package/lib/contracts/types/generated/common.d.ts +22 -0
  29. package/lib/contracts/types/generated/common.js +2 -0
  30. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +276 -0
  31. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +21557 -0
  32. package/lib/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.d.ts +59 -0
  33. package/lib/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.js +765 -0
  34. package/lib/contracts/types/generated/factories/GToken__factory.d.ts +121 -0
  35. package/lib/contracts/types/generated/factories/GToken__factory.js +2867 -0
  36. package/lib/contracts/types/generated/factories/index.d.ts +3 -0
  37. package/lib/contracts/types/generated/factories/index.js +12 -0
  38. package/lib/contracts/types/generated/index.d.ts +7 -0
  39. package/lib/contracts/types/generated/index.js +33 -0
  40. package/lib/contracts/types/index.d.ts +37 -0
  41. package/lib/contracts/types/index.js +30 -0
  42. package/lib/contracts/utils/borrowingFees.d.ts +9 -0
  43. package/lib/contracts/utils/borrowingFees.js +43 -0
  44. package/lib/contracts/utils/index.d.ts +3 -0
  45. package/lib/contracts/utils/index.js +19 -0
  46. package/lib/contracts/utils/openTrades.d.ts +12 -0
  47. package/lib/contracts/utils/openTrades.js +172 -0
  48. package/lib/contracts/utils/pairs.d.ts +18 -0
  49. package/lib/contracts/utils/pairs.js +602 -0
  50. package/lib/index.d.ts +10 -0
  51. package/lib/index.js +30 -0
  52. package/lib/markets/collateral/converter.d.ts +5 -0
  53. package/lib/markets/collateral/converter.js +11 -0
  54. package/lib/markets/collateral/index.d.ts +1 -0
  55. package/lib/markets/collateral/index.js +17 -0
  56. package/lib/markets/collateral/types.d.ts +7 -0
  57. package/lib/markets/collateral/types.js +2 -0
  58. package/lib/markets/commodities.d.ts +1 -0
  59. package/lib/markets/commodities.js +7 -0
  60. package/lib/markets/crypto.d.ts +1 -0
  61. package/lib/markets/crypto.js +6 -0
  62. package/lib/markets/forex.d.ts +3 -0
  63. package/lib/markets/forex.js +8 -0
  64. package/lib/markets/holdingFees/index.d.ts +46 -0
  65. package/lib/markets/holdingFees/index.js +104 -0
  66. package/lib/markets/holdingFees/types.d.ts +23 -0
  67. package/lib/markets/holdingFees/types.js +5 -0
  68. package/lib/markets/index.d.ts +11 -0
  69. package/lib/markets/index.js +27 -0
  70. package/lib/markets/indices.d.ts +1 -0
  71. package/lib/markets/indices.js +6 -0
  72. package/lib/markets/leverage/builder.d.ts +12 -0
  73. package/lib/markets/leverage/builder.js +25 -0
  74. package/lib/markets/leverage/getMarketLeverageRestrictions.d.ts +7 -0
  75. package/lib/markets/leverage/getMarketLeverageRestrictions.js +38 -0
  76. package/lib/markets/leverage/index.d.ts +3 -0
  77. package/lib/markets/leverage/index.js +19 -0
  78. package/lib/markets/leverage/types.d.ts +15 -0
  79. package/lib/markets/leverage/types.js +2 -0
  80. package/lib/markets/oi/converter.d.ts +62 -0
  81. package/lib/markets/oi/converter.js +111 -0
  82. package/lib/markets/oi/index.d.ts +49 -0
  83. package/lib/markets/oi/index.js +77 -0
  84. package/lib/markets/oi/types.d.ts +89 -0
  85. package/lib/markets/oi/types.js +6 -0
  86. package/lib/markets/price/builder.d.ts +25 -0
  87. package/lib/markets/price/builder.js +69 -0
  88. package/lib/markets/price/index.d.ts +7 -0
  89. package/lib/markets/price/index.js +23 -0
  90. package/lib/markets/price/marketPrice.d.ts +13 -0
  91. package/lib/markets/price/marketPrice.js +35 -0
  92. package/lib/markets/price/signedPrices.d.ts +36 -0
  93. package/lib/markets/price/signedPrices.js +181 -0
  94. package/lib/markets/price/types.d.ts +50 -0
  95. package/lib/markets/price/types.js +5 -0
  96. package/lib/markets/schedules/builders.d.ts +7 -0
  97. package/lib/markets/schedules/builders.js +81 -0
  98. package/lib/markets/schedules/checkers.d.ts +7 -0
  99. package/lib/markets/schedules/checkers.js +36 -0
  100. package/lib/markets/schedules/holidays.d.ts +4 -0
  101. package/lib/markets/schedules/holidays.js +77 -0
  102. package/lib/markets/schedules/index.d.ts +9 -0
  103. package/lib/markets/schedules/index.js +45 -0
  104. package/lib/markets/schedules/types.d.ts +43 -0
  105. package/lib/markets/schedules/types.js +37 -0
  106. package/lib/markets/stocks.d.ts +3 -0
  107. package/lib/markets/stocks.js +15 -0
  108. package/lib/pricing/depthBands.d.ts +39 -0
  109. package/lib/pricing/depthBands.js +94 -0
  110. package/lib/pricing/index.d.ts +4 -0
  111. package/lib/pricing/index.js +20 -0
  112. package/lib/trade/counterTrade/index.d.ts +2 -0
  113. package/lib/trade/counterTrade/index.js +18 -0
  114. package/lib/trade/counterTrade/types.d.ts +7 -0
  115. package/lib/trade/counterTrade/types.js +2 -0
  116. package/lib/trade/counterTrade/validateCounterTrade.d.ts +10 -0
  117. package/lib/trade/counterTrade/validateCounterTrade.js +29 -0
  118. package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.d.ts +20 -0
  119. package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.js +42 -0
  120. package/lib/trade/effectiveLeverage/index.d.ts +2 -0
  121. package/lib/trade/effectiveLeverage/index.js +21 -0
  122. package/lib/trade/effectiveLeverage/types.d.ts +30 -0
  123. package/lib/trade/effectiveLeverage/types.js +2 -0
  124. package/lib/trade/fees/borrowing/builder.d.ts +14 -0
  125. package/lib/trade/fees/borrowing/builder.js +33 -0
  126. package/lib/trade/fees/borrowing/converter.d.ts +17 -0
  127. package/lib/trade/fees/borrowing/converter.js +46 -0
  128. package/lib/trade/fees/borrowing/index.d.ts +81 -0
  129. package/lib/trade/fees/borrowing/index.js +259 -0
  130. package/lib/trade/fees/borrowing/types.d.ts +36 -0
  131. package/lib/trade/fees/borrowing/types.js +2 -0
  132. package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
  133. package/lib/trade/fees/borrowingV2/builder.js +23 -0
  134. package/lib/trade/fees/borrowingV2/converter.d.ts +75 -0
  135. package/lib/trade/fees/borrowingV2/converter.js +132 -0
  136. package/lib/trade/fees/borrowingV2/fetcher.d.ts +76 -0
  137. package/lib/trade/fees/borrowingV2/fetcher.js +179 -0
  138. package/lib/trade/fees/borrowingV2/index.d.ts +48 -0
  139. package/lib/trade/fees/borrowingV2/index.js +112 -0
  140. package/lib/trade/fees/borrowingV2/types.d.ts +95 -0
  141. package/lib/trade/fees/borrowingV2/types.js +5 -0
  142. package/lib/trade/fees/converter.d.ts +48 -0
  143. package/lib/trade/fees/converter.js +114 -0
  144. package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
  145. package/lib/trade/fees/fundingFees/builder.js +38 -0
  146. package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
  147. package/lib/trade/fees/fundingFees/converter.js +196 -0
  148. package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
  149. package/lib/trade/fees/fundingFees/fetcher.js +141 -0
  150. package/lib/trade/fees/fundingFees/index.d.ts +124 -0
  151. package/lib/trade/fees/fundingFees/index.js +309 -0
  152. package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
  153. package/lib/trade/fees/fundingFees/pairContext.js +17 -0
  154. package/lib/trade/fees/fundingFees/types.d.ts +77 -0
  155. package/lib/trade/fees/fundingFees/types.js +5 -0
  156. package/lib/trade/fees/index.d.ts +11 -0
  157. package/lib/trade/fees/index.js +87 -0
  158. package/lib/trade/fees/tiers/converter.d.ts +64 -0
  159. package/lib/trade/fees/tiers/converter.js +100 -0
  160. package/lib/trade/fees/tiers/index.d.ts +46 -0
  161. package/lib/trade/fees/tiers/index.js +135 -0
  162. package/lib/trade/fees/tiers/types.d.ts +22 -0
  163. package/lib/trade/fees/tiers/types.js +8 -0
  164. package/lib/trade/fees/trading/builder.d.ts +19 -0
  165. package/lib/trade/fees/trading/builder.js +21 -0
  166. package/lib/trade/fees/trading/converter.d.ts +32 -0
  167. package/lib/trade/fees/trading/converter.js +47 -0
  168. package/lib/trade/fees/trading/index.d.ts +62 -0
  169. package/lib/trade/fees/trading/index.js +157 -0
  170. package/lib/trade/fees/trading/types.d.ts +46 -0
  171. package/lib/trade/fees/trading/types.js +5 -0
  172. package/lib/trade/index.d.ts +10 -0
  173. package/lib/trade/index.js +26 -0
  174. package/lib/trade/liquidation/builder.d.ts +25 -0
  175. package/lib/trade/liquidation/builder.js +58 -0
  176. package/lib/trade/liquidation/converter.d.ts +23 -0
  177. package/lib/trade/liquidation/converter.js +46 -0
  178. package/lib/trade/liquidation/index.d.ts +29 -0
  179. package/lib/trade/liquidation/index.js +216 -0
  180. package/lib/trade/liquidation/types.d.ts +38 -0
  181. package/lib/trade/liquidation/types.js +2 -0
  182. package/lib/trade/oiWindows.d.ts +3 -0
  183. package/lib/trade/oiWindows.js +19 -0
  184. package/lib/trade/pnl/builder.d.ts +16 -0
  185. package/lib/trade/pnl/builder.js +43 -0
  186. package/lib/trade/pnl/converter.d.ts +47 -0
  187. package/lib/trade/pnl/converter.js +72 -0
  188. package/lib/trade/pnl/index.d.ts +91 -0
  189. package/lib/trade/pnl/index.js +301 -0
  190. package/lib/trade/pnl/types.d.ts +79 -0
  191. package/lib/trade/pnl/types.js +5 -0
  192. package/lib/trade/priceImpact/close/builder.d.ts +23 -0
  193. package/lib/trade/priceImpact/close/builder.js +44 -0
  194. package/lib/trade/priceImpact/close/index.d.ts +22 -0
  195. package/lib/trade/priceImpact/close/index.js +137 -0
  196. package/lib/trade/priceImpact/close/types.d.ts +47 -0
  197. package/lib/trade/priceImpact/close/types.js +5 -0
  198. package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
  199. package/lib/trade/priceImpact/cumulVol/builder.js +53 -0
  200. package/lib/trade/priceImpact/cumulVol/converter.d.ts +94 -0
  201. package/lib/trade/priceImpact/cumulVol/converter.js +155 -0
  202. package/lib/trade/priceImpact/cumulVol/index.d.ts +109 -0
  203. package/lib/trade/priceImpact/cumulVol/index.js +316 -0
  204. package/lib/trade/priceImpact/cumulVol/types.d.ts +11 -0
  205. package/lib/trade/priceImpact/cumulVol/types.js +2 -0
  206. package/lib/trade/priceImpact/index.d.ts +21 -0
  207. package/lib/trade/priceImpact/index.js +79 -0
  208. package/lib/trade/priceImpact/open/builder.d.ts +21 -0
  209. package/lib/trade/priceImpact/open/builder.js +42 -0
  210. package/lib/trade/priceImpact/open/index.d.ts +23 -0
  211. package/lib/trade/priceImpact/open/index.js +82 -0
  212. package/lib/trade/priceImpact/open/types.d.ts +45 -0
  213. package/lib/trade/priceImpact/open/types.js +5 -0
  214. package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
  215. package/lib/trade/priceImpact/skew/builder.js +27 -0
  216. package/lib/trade/priceImpact/skew/converter.d.ts +46 -0
  217. package/lib/trade/priceImpact/skew/converter.js +81 -0
  218. package/lib/trade/priceImpact/skew/fetcher.d.ts +60 -0
  219. package/lib/trade/priceImpact/skew/fetcher.js +160 -0
  220. package/lib/trade/priceImpact/skew/index.d.ts +53 -0
  221. package/lib/trade/priceImpact/skew/index.js +148 -0
  222. package/lib/trade/priceImpact/skew/types.d.ts +44 -0
  223. package/lib/trade/priceImpact/skew/types.js +5 -0
  224. package/lib/trade/spread.d.ts +5 -0
  225. package/lib/trade/spread.js +19 -0
  226. package/lib/trade/types.d.ts +777 -0
  227. package/lib/trade/types.js +478 -0
  228. package/lib/trade/utils.d.ts +18 -0
  229. package/lib/trade/utils.js +30 -0
  230. package/lib/utils/index.d.ts +1 -0
  231. package/lib/utils/index.js +17 -0
  232. package/lib/utils/packing.d.ts +2 -0
  233. package/lib/utils/packing.js +39 -0
  234. package/lib/vault/index.d.ts +10 -0
  235. package/lib/vault/index.js +10 -0
  236. package/package.json +106 -0
@@ -0,0 +1,75 @@
1
+ import type { IFundingFees } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
2
+ import { BorrowingFeeV2 } from ".";
3
+ /**
4
+ * @dev Converts contract BorrowingFeeParams to SDK type
5
+ * @param contractParams Contract borrowing fee params from IFundingFees.BorrowingFeeParams
6
+ * @returns SDK BorrowingFeeParams
7
+ */
8
+ export declare const convertBorrowingFeeParams: (contractParams: IFundingFees.BorrowingFeeParamsStructOutput) => BorrowingFeeV2.BorrowingFeeParams;
9
+ /**
10
+ * @dev Converts array of contract BorrowingFeeParams to SDK types
11
+ * @param contractParamsArray Array of contract borrowing fee params
12
+ * @returns Array of SDK BorrowingFeeParams
13
+ */
14
+ export declare const convertBorrowingFeeParamsArray: (contractParamsArray: IFundingFees.BorrowingFeeParamsStructOutput[]) => BorrowingFeeV2.BorrowingFeeParams[];
15
+ /**
16
+ * @dev Converts contract PairBorrowingFeeData to SDK type
17
+ * @param contractData Contract pair borrowing fee data from IFundingFees.PairBorrowingFeeData
18
+ * @returns SDK PairBorrowingFeeData
19
+ */
20
+ export declare const convertPairBorrowingFeeData: (contractData: IFundingFees.PairBorrowingFeeDataStructOutput) => BorrowingFeeV2.PairBorrowingFeeData;
21
+ /**
22
+ * @dev Converts array of contract PairBorrowingFeeData to SDK types
23
+ * @param contractDataArray Array of contract pair borrowing fee data
24
+ * @returns Array of SDK PairBorrowingFeeData
25
+ */
26
+ export declare const convertPairBorrowingFeeDataArray: (contractDataArray: IFundingFees.PairBorrowingFeeDataStructOutput[]) => BorrowingFeeV2.PairBorrowingFeeData[];
27
+ /**
28
+ * @dev Converts contract TradeFeesData to SDK TradeInitialAccFees
29
+ * @param contractTradeData Contract trade fees data from IFundingFees.TradeFeesData
30
+ * @returns SDK TradeInitialAccFees
31
+ */
32
+ export declare const convertTradeInitialAccFees: (contractTradeData: IFundingFees.TradeFeesDataStructOutput) => BorrowingFeeV2.TradeInitialAccFees;
33
+ /**
34
+ * @dev Converts array of contract TradeFeesData to SDK TradeInitialAccFees
35
+ * @param contractTradeDataArray Array of contract trade fees data
36
+ * @returns Array of SDK TradeInitialAccFees
37
+ */
38
+ export declare const convertTradeInitialAccFeesArray: (contractTradeDataArray: IFundingFees.TradeFeesDataStructOutput[]) => BorrowingFeeV2.TradeInitialAccFees[];
39
+ /**
40
+ * @dev Creates a context object from contract data arrays
41
+ * @param pairIndices Array of pair indices
42
+ * @param borrowingParams Array of borrowing fee params from contract
43
+ * @param borrowingData Array of pair borrowing fee data from contract
44
+ * @param currentTimestamp Optional current timestamp
45
+ * @returns Complete SDK context for borrowing v2 calculations (collateral-scoped)
46
+ */
47
+ export declare const createBorrowingV2Context: (pairIndices: number[], borrowingParams: IFundingFees.BorrowingFeeParamsStructOutput[], borrowingData: IFundingFees.PairBorrowingFeeDataStructOutput[], currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
48
+ /**
49
+ * @dev Helper function to validate borrowing rate per second
50
+ * @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
51
+ * @returns True if rate is within valid bounds
52
+ */
53
+ export declare const isValidBorrowingRate: (borrowingRatePerSecondP: number) => boolean;
54
+ /**
55
+ * @dev Helper function to convert borrowing rate to APR percentage
56
+ * @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
57
+ * @returns APR as percentage (e.g., 10.5 for 10.5% APR)
58
+ */
59
+ export declare const borrowingRateToAPR: (borrowingRatePerSecondP: number) => number;
60
+ /**
61
+ * @dev Helper function to convert APR percentage to borrowing rate per second
62
+ * @param aprPercentage APR as percentage (e.g., 10.5 for 10.5% APR)
63
+ * @returns Borrowing rate per second (normalized float)
64
+ */
65
+ export declare const aprToBorrowingRate: (aprPercentage: number) => number;
66
+ /**
67
+ * @dev Creates a collateral-scoped context from frontend data structure
68
+ * @param collateralBorrowingData Data structure from frontend (params and data arrays)
69
+ * @param currentTimestamp Optional current timestamp
70
+ * @returns Collateral-scoped borrowing fee v2 context
71
+ */
72
+ export declare const createCollateralScopedBorrowingContext: (collateralBorrowingData: {
73
+ params: BorrowingFeeV2.BorrowingFeeParams[];
74
+ data: BorrowingFeeV2.PairBorrowingFeeData[];
75
+ }, currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCollateralScopedBorrowingContext = exports.aprToBorrowingRate = exports.borrowingRateToAPR = exports.isValidBorrowingRate = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArray = exports.convertTradeInitialAccFees = exports.convertPairBorrowingFeeDataArray = exports.convertPairBorrowingFeeData = exports.convertBorrowingFeeParamsArray = exports.convertBorrowingFeeParams = void 0;
4
+ const index_1 = require("./index");
5
+ /**
6
+ * @dev Converts contract BorrowingFeeParams to SDK type
7
+ * @param contractParams Contract borrowing fee params from IFundingFees.BorrowingFeeParams
8
+ * @returns SDK BorrowingFeeParams
9
+ */
10
+ const convertBorrowingFeeParams = (contractParams) => ({
11
+ borrowingRatePerSecondP: contractParams.borrowingRatePerSecondP /
12
+ index_1.BORROWING_V2_PRECISION.RATE_PER_SECOND,
13
+ });
14
+ exports.convertBorrowingFeeParams = convertBorrowingFeeParams;
15
+ /**
16
+ * @dev Converts array of contract BorrowingFeeParams to SDK types
17
+ * @param contractParamsArray Array of contract borrowing fee params
18
+ * @returns Array of SDK BorrowingFeeParams
19
+ */
20
+ const convertBorrowingFeeParamsArray = (contractParamsArray) => contractParamsArray.map(params => (0, exports.convertBorrowingFeeParams)(params));
21
+ exports.convertBorrowingFeeParamsArray = convertBorrowingFeeParamsArray;
22
+ /**
23
+ * @dev Converts contract PairBorrowingFeeData to SDK type
24
+ * @param contractData Contract pair borrowing fee data from IFundingFees.PairBorrowingFeeData
25
+ * @returns SDK PairBorrowingFeeData
26
+ */
27
+ const convertPairBorrowingFeeData = (contractData) => ({
28
+ accBorrowingFeeP: parseFloat(contractData.accBorrowingFeeP.toString()) /
29
+ index_1.BORROWING_V2_PRECISION.ACC_FEE,
30
+ lastBorrowingUpdateTs: contractData.lastBorrowingUpdateTs,
31
+ });
32
+ exports.convertPairBorrowingFeeData = convertPairBorrowingFeeData;
33
+ /**
34
+ * @dev Converts array of contract PairBorrowingFeeData to SDK types
35
+ * @param contractDataArray Array of contract pair borrowing fee data
36
+ * @returns Array of SDK PairBorrowingFeeData
37
+ */
38
+ const convertPairBorrowingFeeDataArray = (contractDataArray) => contractDataArray.map(data => (0, exports.convertPairBorrowingFeeData)(data));
39
+ exports.convertPairBorrowingFeeDataArray = convertPairBorrowingFeeDataArray;
40
+ /**
41
+ * @dev Converts contract TradeFeesData to SDK TradeInitialAccFees
42
+ * @param contractTradeData Contract trade fees data from IFundingFees.TradeFeesData
43
+ * @returns SDK TradeInitialAccFees
44
+ */
45
+ const convertTradeInitialAccFees = (contractTradeData) => ({
46
+ initialAccBorrowingFeeP: parseFloat(contractTradeData.initialAccBorrowingFeeP.toString()) /
47
+ index_1.BORROWING_V2_PRECISION.ACC_FEE,
48
+ });
49
+ exports.convertTradeInitialAccFees = convertTradeInitialAccFees;
50
+ /**
51
+ * @dev Converts array of contract TradeFeesData to SDK TradeInitialAccFees
52
+ * @param contractTradeDataArray Array of contract trade fees data
53
+ * @returns Array of SDK TradeInitialAccFees
54
+ */
55
+ const convertTradeInitialAccFeesArray = (contractTradeDataArray) => contractTradeDataArray.map(data => (0, exports.convertTradeInitialAccFees)(data));
56
+ exports.convertTradeInitialAccFeesArray = convertTradeInitialAccFeesArray;
57
+ /**
58
+ * @dev Creates a context object from contract data arrays
59
+ * @param pairIndices Array of pair indices
60
+ * @param borrowingParams Array of borrowing fee params from contract
61
+ * @param borrowingData Array of pair borrowing fee data from contract
62
+ * @param currentTimestamp Optional current timestamp
63
+ * @returns Complete SDK context for borrowing v2 calculations (collateral-scoped)
64
+ */
65
+ const createBorrowingV2Context = (pairIndices, borrowingParams, borrowingData, currentTimestamp) => {
66
+ const context = {
67
+ currentTimestamp,
68
+ borrowingParams: {},
69
+ borrowingData: {},
70
+ };
71
+ // Build objects indexed by pairIndex
72
+ for (let i = 0; i < pairIndices.length; i++) {
73
+ const pairIndex = pairIndices[i];
74
+ // Store converted data
75
+ context.borrowingParams[pairIndex] = (0, exports.convertBorrowingFeeParams)(borrowingParams[i]);
76
+ context.borrowingData[pairIndex] = (0, exports.convertPairBorrowingFeeData)(borrowingData[i]);
77
+ }
78
+ return context;
79
+ };
80
+ exports.createBorrowingV2Context = createBorrowingV2Context;
81
+ /**
82
+ * @dev Helper function to validate borrowing rate per second
83
+ * @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
84
+ * @returns True if rate is within valid bounds
85
+ */
86
+ const isValidBorrowingRate = (borrowingRatePerSecondP) => {
87
+ return (borrowingRatePerSecondP >= 0 &&
88
+ borrowingRatePerSecondP <= 317097 / index_1.BORROWING_V2_PRECISION.RATE_PER_SECOND); // Max 1,000% APR
89
+ };
90
+ exports.isValidBorrowingRate = isValidBorrowingRate;
91
+ /**
92
+ * @dev Helper function to convert borrowing rate to APR percentage
93
+ * @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
94
+ * @returns APR as percentage (e.g., 10.5 for 10.5% APR)
95
+ */
96
+ const borrowingRateToAPR = (borrowingRatePerSecondP) => {
97
+ const SECONDS_PER_YEAR = 365 * 24 * 60 * 60; // 31,536,000
98
+ return borrowingRatePerSecondP * SECONDS_PER_YEAR;
99
+ };
100
+ exports.borrowingRateToAPR = borrowingRateToAPR;
101
+ /**
102
+ * @dev Helper function to convert APR percentage to borrowing rate per second
103
+ * @param aprPercentage APR as percentage (e.g., 10.5 for 10.5% APR)
104
+ * @returns Borrowing rate per second (normalized float)
105
+ */
106
+ const aprToBorrowingRate = (aprPercentage) => {
107
+ const SECONDS_PER_YEAR = 365 * 24 * 60 * 60; // 31,536,000
108
+ return aprPercentage / SECONDS_PER_YEAR;
109
+ };
110
+ exports.aprToBorrowingRate = aprToBorrowingRate;
111
+ /**
112
+ * @dev Creates a collateral-scoped context from frontend data structure
113
+ * @param collateralBorrowingData Data structure from frontend (params and data arrays)
114
+ * @param currentTimestamp Optional current timestamp
115
+ * @returns Collateral-scoped borrowing fee v2 context
116
+ */
117
+ const createCollateralScopedBorrowingContext = (collateralBorrowingData, currentTimestamp) => {
118
+ const context = {
119
+ currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
120
+ borrowingParams: {},
121
+ borrowingData: {},
122
+ };
123
+ // Map arrays to objects indexed by array position (pairIndex)
124
+ collateralBorrowingData.params.forEach((param, index) => {
125
+ context.borrowingParams[index] = param;
126
+ });
127
+ collateralBorrowingData.data.forEach((data, index) => {
128
+ context.borrowingData[index] = data;
129
+ });
130
+ return context;
131
+ };
132
+ exports.createCollateralScopedBorrowingContext = createCollateralScopedBorrowingContext;
@@ -0,0 +1,76 @@
1
+ /// <reference types="mocha" />
2
+ import type { GNSMultiCollatDiamond } from "../../../contracts/types/generated";
3
+ import { BorrowingFeeV2 } from ".";
4
+ /**
5
+ * @dev Fetches borrowing fee parameters v2 for specific pairs
6
+ * @param contract GNSMultiCollatDiamond contract instance
7
+ * @param collateralIndices Array of collateral indices
8
+ * @param pairIndices Array of pair indices
9
+ * @returns Promise resolving to array of borrowing fee parameters
10
+ */
11
+ export declare const fetchBorrowingFeeParamsV2: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<BorrowingFeeV2.BorrowingFeeParams[]>;
12
+ /**
13
+ * @dev Fetches pair borrowing fee data v2 for specific pairs
14
+ * @param contract GNSMultiCollatDiamond contract instance
15
+ * @param collateralIndices Array of collateral indices
16
+ * @param pairIndices Array of pair indices
17
+ * @returns Promise resolving to array of pair borrowing fee data
18
+ */
19
+ export declare const fetchPairBorrowingFeeDataV2: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<BorrowingFeeV2.PairBorrowingFeeData[]>;
20
+ /**
21
+ * @dev Fetches borrowing fees in collateral tokens for a specific trade
22
+ * @param contract GNSMultiCollatDiamond contract instance
23
+ * @param trader Address of the trader
24
+ * @param index Trade index
25
+ * @param currentPairPrice Current price of the trading pair (1e6 precision)
26
+ * @returns Promise resolving to borrowing fees in collateral tokens
27
+ */
28
+ export declare const fetchTradeBorrowingFeesCollateralV2: (contract: GNSMultiCollatDiamond, trader: string, index: number, currentPairPrice: number) => Promise<number>;
29
+ /**
30
+ * @dev Fetches pending accumulated borrowing fees for a specific pair
31
+ * @param contract GNSMultiCollatDiamond contract instance
32
+ * @param collateralIndex Index of the collateral
33
+ * @param pairIndex Index of the trading pair
34
+ * @param currentPairPrice Current price of the trading pair (1e6 precision)
35
+ * @returns Promise resolving to pending accumulated borrowing fee
36
+ */
37
+ export declare const fetchPairPendingAccBorrowingFeesV2: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, currentPairPrice: number) => Promise<number>;
38
+ /**
39
+ * @dev Convenience function to fetch all borrowing v2 data for specific pairs
40
+ * @param contract GNSMultiCollatDiamond contract instance
41
+ * @param collateralIndex Index of the collateral
42
+ * @param pairIndices Array of pair indices
43
+ * @returns Promise resolving to complete borrowing v2 data set
44
+ */
45
+ export declare const fetchAllBorrowingV2Data: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndices: number[]) => Promise<{
46
+ params: BorrowingFeeV2.BorrowingFeeParams[];
47
+ data: BorrowingFeeV2.PairBorrowingFeeData[];
48
+ context: BorrowingFeeV2.GetBorrowingFeeV2Context;
49
+ }>;
50
+ /**
51
+ * @dev Creates a complete borrowing v2 context from contract data
52
+ * @param contract GNSMultiCollatDiamond contract instance
53
+ * @param collateralIndex Index of the collateral
54
+ * @param pairIndices Array of pair indices
55
+ * @param currentTimestamp Optional current timestamp for calculations
56
+ * @returns Promise resolving to complete borrowing v2 context
57
+ */
58
+ export declare const createBorrowingV2ContextFromContract: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndices: number[], currentTimestamp?: number) => Promise<BorrowingFeeV2.GetBorrowingFeeV2Context>;
59
+ /**
60
+ * @dev Helper function to create context from already fetched arrays
61
+ * @param collateralIndices Array of collateral indices
62
+ * @param pairIndices Array of pair indices
63
+ * @param params Array of borrowing fee parameters
64
+ * @param data Array of pair borrowing fee data
65
+ * @param currentTimestamp Optional current timestamp
66
+ * @returns Complete borrowing v2 context
67
+ */
68
+ export declare const createBorrowingV2ContextFromArrays: (collateralIndices: number[], pairIndices: number[], params: BorrowingFeeV2.BorrowingFeeParams[], data: BorrowingFeeV2.PairBorrowingFeeData[], currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
69
+ /**
70
+ * @dev Fetches borrowing v2 data for multiple collateral/pair combinations
71
+ * @param contract GNSMultiCollatDiamond contract instance
72
+ * @param collateralIndices Array of collateral indices
73
+ * @param pairIndices Array of pair indices (must match collateralIndices length)
74
+ * @returns Promise resolving to complete borrowing v2 context
75
+ */
76
+ export declare const fetchBorrowingV2DataForPairs: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<BorrowingFeeV2.GetBorrowingFeeV2Context>;
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = void 0;
4
+ const converter_1 = require("./converter");
5
+ /**
6
+ * @dev Fetches borrowing fee parameters v2 for specific pairs
7
+ * @param contract GNSMultiCollatDiamond contract instance
8
+ * @param collateralIndices Array of collateral indices
9
+ * @param pairIndices Array of pair indices
10
+ * @returns Promise resolving to array of borrowing fee parameters
11
+ */
12
+ const fetchBorrowingFeeParamsV2 = async (contract, collateralIndices, pairIndices) => {
13
+ if (collateralIndices.length !== pairIndices.length) {
14
+ throw new Error("Collateral indices and pair indices arrays must have the same length");
15
+ }
16
+ try {
17
+ const contractParams = await contract.getPairBorrowingFeeParams(collateralIndices, pairIndices);
18
+ return (0, converter_1.convertBorrowingFeeParamsArray)(contractParams);
19
+ }
20
+ catch (error) {
21
+ console.error("Error fetching borrowing fee params v2:", error);
22
+ throw error;
23
+ }
24
+ };
25
+ exports.fetchBorrowingFeeParamsV2 = fetchBorrowingFeeParamsV2;
26
+ /**
27
+ * @dev Fetches pair borrowing fee data v2 for specific pairs
28
+ * @param contract GNSMultiCollatDiamond contract instance
29
+ * @param collateralIndices Array of collateral indices
30
+ * @param pairIndices Array of pair indices
31
+ * @returns Promise resolving to array of pair borrowing fee data
32
+ */
33
+ const fetchPairBorrowingFeeDataV2 = async (contract, collateralIndices, pairIndices) => {
34
+ if (collateralIndices.length !== pairIndices.length) {
35
+ throw new Error("Collateral indices and pair indices arrays must have the same length");
36
+ }
37
+ try {
38
+ const contractData = await contract.getPairBorrowingFeeData(collateralIndices, pairIndices);
39
+ return (0, converter_1.convertPairBorrowingFeeDataArray)(contractData);
40
+ }
41
+ catch (error) {
42
+ console.error("Error fetching pair borrowing fee data v2:", error);
43
+ throw error;
44
+ }
45
+ };
46
+ exports.fetchPairBorrowingFeeDataV2 = fetchPairBorrowingFeeDataV2;
47
+ /**
48
+ * @dev Fetches borrowing fees in collateral tokens for a specific trade
49
+ * @param contract GNSMultiCollatDiamond contract instance
50
+ * @param trader Address of the trader
51
+ * @param index Trade index
52
+ * @param currentPairPrice Current price of the trading pair (1e6 precision)
53
+ * @returns Promise resolving to borrowing fees in collateral tokens
54
+ */
55
+ const fetchTradeBorrowingFeesCollateralV2 = async (contract, trader, index, currentPairPrice) => {
56
+ try {
57
+ const feesCollateral = await contract.getTradeBorrowingFeesCollateral(trader, index, currentPairPrice);
58
+ // Convert BigNumber to normalized float
59
+ // Note: Collateral precision varies by chain, but contract returns proper precision
60
+ return parseFloat(feesCollateral.toString());
61
+ }
62
+ catch (error) {
63
+ console.error("Error fetching trade borrowing fees collateral v2:", error);
64
+ throw error;
65
+ }
66
+ };
67
+ exports.fetchTradeBorrowingFeesCollateralV2 = fetchTradeBorrowingFeesCollateralV2;
68
+ /**
69
+ * @dev Fetches pending accumulated borrowing fees for a specific pair
70
+ * @param contract GNSMultiCollatDiamond contract instance
71
+ * @param collateralIndex Index of the collateral
72
+ * @param pairIndex Index of the trading pair
73
+ * @param currentPairPrice Current price of the trading pair (1e6 precision)
74
+ * @returns Promise resolving to pending accumulated borrowing fee
75
+ */
76
+ const fetchPairPendingAccBorrowingFeesV2 = async (contract, collateralIndex, pairIndex, currentPairPrice) => {
77
+ try {
78
+ const accBorrowingFeeP = await contract.getPairPendingAccBorrowingFees(collateralIndex, pairIndex, currentPairPrice);
79
+ // Convert BigNumber to normalized float
80
+ return parseFloat(accBorrowingFeeP.toString()) / 1e20;
81
+ }
82
+ catch (error) {
83
+ console.error("Error fetching pair pending acc borrowing fees v2:", error);
84
+ throw error;
85
+ }
86
+ };
87
+ exports.fetchPairPendingAccBorrowingFeesV2 = fetchPairPendingAccBorrowingFeesV2;
88
+ /**
89
+ * @dev Convenience function to fetch all borrowing v2 data for specific pairs
90
+ * @param contract GNSMultiCollatDiamond contract instance
91
+ * @param collateralIndex Index of the collateral
92
+ * @param pairIndices Array of pair indices
93
+ * @returns Promise resolving to complete borrowing v2 data set
94
+ */
95
+ const fetchAllBorrowingV2Data = async (contract, collateralIndex, pairIndices) => {
96
+ const collateralIndices = new Array(pairIndices.length).fill(collateralIndex);
97
+ try {
98
+ // Fetch both parameters and data in parallel
99
+ const [params, data] = await Promise.all([
100
+ (0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
101
+ (0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
102
+ ]);
103
+ // Create context from fetched data
104
+ const context = (0, exports.createBorrowingV2ContextFromArrays)(collateralIndices, pairIndices, params, data);
105
+ return { params, data, context };
106
+ }
107
+ catch (error) {
108
+ console.error("Error fetching all borrowing v2 data:", error);
109
+ throw error;
110
+ }
111
+ };
112
+ exports.fetchAllBorrowingV2Data = fetchAllBorrowingV2Data;
113
+ /**
114
+ * @dev Creates a complete borrowing v2 context from contract data
115
+ * @param contract GNSMultiCollatDiamond contract instance
116
+ * @param collateralIndex Index of the collateral
117
+ * @param pairIndices Array of pair indices
118
+ * @param currentTimestamp Optional current timestamp for calculations
119
+ * @returns Promise resolving to complete borrowing v2 context
120
+ */
121
+ const createBorrowingV2ContextFromContract = async (contract, collateralIndex, pairIndices, currentTimestamp) => {
122
+ const { context } = await (0, exports.fetchAllBorrowingV2Data)(contract, collateralIndex, pairIndices);
123
+ return {
124
+ ...context,
125
+ currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
126
+ };
127
+ };
128
+ exports.createBorrowingV2ContextFromContract = createBorrowingV2ContextFromContract;
129
+ /**
130
+ * @dev Helper function to create context from already fetched arrays
131
+ * @param collateralIndices Array of collateral indices
132
+ * @param pairIndices Array of pair indices
133
+ * @param params Array of borrowing fee parameters
134
+ * @param data Array of pair borrowing fee data
135
+ * @param currentTimestamp Optional current timestamp
136
+ * @returns Complete borrowing v2 context
137
+ */
138
+ const createBorrowingV2ContextFromArrays = (collateralIndices, pairIndices, params, data, currentTimestamp) => {
139
+ const context = {
140
+ currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
141
+ borrowingParams: {},
142
+ borrowingData: {},
143
+ };
144
+ // Build objects indexed by pairIndex (collateral-scoped)
145
+ for (let i = 0; i < pairIndices.length; i++) {
146
+ const pairIndex = pairIndices[i];
147
+ // Store data indexed by pairIndex
148
+ context.borrowingParams[pairIndex] = params[i];
149
+ context.borrowingData[pairIndex] = data[i];
150
+ }
151
+ return context;
152
+ };
153
+ exports.createBorrowingV2ContextFromArrays = createBorrowingV2ContextFromArrays;
154
+ /**
155
+ * @dev Fetches borrowing v2 data for multiple collateral/pair combinations
156
+ * @param contract GNSMultiCollatDiamond contract instance
157
+ * @param collateralIndices Array of collateral indices
158
+ * @param pairIndices Array of pair indices (must match collateralIndices length)
159
+ * @returns Promise resolving to complete borrowing v2 context
160
+ */
161
+ const fetchBorrowingV2DataForPairs = async (contract, collateralIndices, pairIndices) => {
162
+ if (collateralIndices.length !== pairIndices.length) {
163
+ throw new Error("Collateral indices and pair indices arrays must have the same length");
164
+ }
165
+ try {
166
+ // Fetch both parameters and data in parallel
167
+ const [params, data] = await Promise.all([
168
+ (0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
169
+ (0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
170
+ ]);
171
+ // Create and return context
172
+ return (0, exports.createBorrowingV2ContextFromArrays)(collateralIndices, pairIndices, params, data);
173
+ }
174
+ catch (error) {
175
+ console.error("Error fetching borrowing v2 data for pairs:", error);
176
+ throw error;
177
+ }
178
+ };
179
+ exports.fetchBorrowingV2DataForPairs = fetchBorrowingV2DataForPairs;
@@ -0,0 +1,48 @@
1
+ import * as BorrowingFeeV2 from "./types";
2
+ /**
3
+ * @dev Maximum borrowing rate per second (1,000% APR)
4
+ */
5
+ export declare const MAX_BORROWING_RATE_PER_SECOND = 0.0317097;
6
+ /**
7
+ * @dev Precision constants for borrowing v2 calculations
8
+ */
9
+ export declare const BORROWING_V2_PRECISION: {
10
+ readonly RATE_PER_SECOND: 10000000000;
11
+ readonly ACC_FEE: 100000000000000000000;
12
+ readonly PERCENTAGE: 100;
13
+ };
14
+ /**
15
+ * @dev Calculates pending accumulated borrowing fees for a pair
16
+ * @param params Borrowing fee parameters for the pair
17
+ * @param data Current borrowing fee data for the pair
18
+ * @param currentPairPrice Current price of the trading pair
19
+ * @param currentTimestamp Current timestamp (defaults to now)
20
+ * @returns Updated accumulated borrowing fee (1e20 precision)
21
+ */
22
+ export declare const getPairPendingAccBorrowingFees: (params: BorrowingFeeV2.BorrowingFeeParams, data: BorrowingFeeV2.PairBorrowingFeeData, currentPairPrice: number, currentTimestamp?: number) => number;
23
+ /**
24
+ * @dev Calculates borrowing fees owed by a specific trade
25
+ * @param input Trade borrowing fee calculation input (without pairIndex)
26
+ * @param context Pair-specific borrowing context
27
+ * @returns Borrowing fees in collateral tokens
28
+ */
29
+ export declare const getTradeBorrowingFeesCollateral: (input: Omit<BorrowingFeeV2.TradeBorrowingFeeInput, "pairIndex">, context: BorrowingFeeV2.GetPairBorrowingFeeV2Context) => number;
30
+ /**
31
+ * @dev Utility function to get pending accumulated borrowing fees for a pair using context
32
+ * @param input Pair borrowing fee calculation input
33
+ * @param context Context containing borrowing parameters and data
34
+ * @returns Updated accumulated borrowing fee (1e20 precision)
35
+ */
36
+ export declare const getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
37
+ /**
38
+ * @dev Utility functions for working with borrowing v2 fees
39
+ */
40
+ export declare const borrowingFeeV2Utils: {
41
+ getPairPendingAccBorrowingFees: (params: BorrowingFeeV2.BorrowingFeeParams, data: BorrowingFeeV2.PairBorrowingFeeData, currentPairPrice: number, currentTimestamp?: number) => number;
42
+ getTradeBorrowingFeesCollateral: (input: Omit<BorrowingFeeV2.TradeBorrowingFeeInput, "pairIndex">, context: BorrowingFeeV2.GetPairBorrowingFeeV2Context) => number;
43
+ getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
44
+ };
45
+ export * as BorrowingFeeV2 from "./types";
46
+ export { GetPairBorrowingFeeV2Context } from "./types";
47
+ export * from "./converter";
48
+ export * from "./fetcher";
@@ -0,0 +1,112 @@
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.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 ?? 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 (without pairIndex)
67
+ * @param context Pair-specific borrowing context
68
+ * @returns Borrowing fees in collateral tokens
69
+ */
70
+ const getTradeBorrowingFeesCollateral = (input, context) => {
71
+ const { positionSizeCollateral, openPrice, currentPairPrice, initialAccBorrowingFeeP, currentTimestamp, } = input;
72
+ const { params, data } = context;
73
+ if (!params || !data) {
74
+ return 0;
75
+ }
76
+ // Calculate current accumulated borrowing fees
77
+ const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp ?? context.currentTimestamp);
78
+ // Calculate borrowing fees for this trade
79
+ // Formula: (positionSizeCollateral * (currentAccFee - initialAccFee)) / openPrice / 100
80
+ const feeDeltaP = currentAccBorrowingFeeP - initialAccBorrowingFeeP;
81
+ return ((positionSizeCollateral * feeDeltaP) /
82
+ openPrice /
83
+ exports.BORROWING_V2_PRECISION.PERCENTAGE);
84
+ };
85
+ exports.getTradeBorrowingFeesCollateral = getTradeBorrowingFeesCollateral;
86
+ /**
87
+ * @dev Utility function to get pending accumulated borrowing fees for a pair using context
88
+ * @param input Pair borrowing fee calculation input
89
+ * @param context Context containing borrowing parameters and data
90
+ * @returns Updated accumulated borrowing fee (1e20 precision)
91
+ */
92
+ const getPairBorrowingFees = (input, context) => {
93
+ const { pairIndex, currentPairPrice, currentTimestamp } = input;
94
+ const params = context.borrowingParams[pairIndex];
95
+ const data = context.borrowingData[pairIndex];
96
+ if (!params || !data) {
97
+ return 0;
98
+ }
99
+ return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp ?? context.currentTimestamp);
100
+ };
101
+ exports.getPairBorrowingFees = getPairBorrowingFees;
102
+ /**
103
+ * @dev Utility functions for working with borrowing v2 fees
104
+ */
105
+ exports.borrowingFeeV2Utils = {
106
+ getPairPendingAccBorrowingFees: exports.getPairPendingAccBorrowingFees,
107
+ getTradeBorrowingFeesCollateral: exports.getTradeBorrowingFeesCollateral,
108
+ getPairBorrowingFees: exports.getPairBorrowingFees,
109
+ };
110
+ exports.BorrowingFeeV2 = __importStar(require("./types"));
111
+ __exportStar(require("./converter"), exports);
112
+ __exportStar(require("./fetcher"), exports);