@gainsnetwork/sdk 0.2.73-rc2 → 1.0.0-rc10

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 -5
  15. package/lib/constants.js +0 -5
  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 -5
  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 +34 -0
  139. package/lib/trade/fees/borrowing/index.d.ts +26 -3
  140. package/lib/trade/fees/borrowing/index.js +72 -22
  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 +9 -2
  170. package/lib/trade/fees/index.js +69 -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 +91 -0
  202. package/lib/trade/pnl/index.js +303 -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 +79 -0
  223. package/lib/trade/priceImpact/open/types.d.ts +45 -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 -14
  238. package/lib/trade/types.js +0 -5
  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,62 @@
1
+ /**
2
+ * @dev Trading fee calculations for opening and closing positions
3
+ */
4
+ import { Fee, PairIndex, Trade, TradeInfo, TradeFeesData } from "../../types";
5
+ import { GetBorrowingFeeContext } from "../borrowing";
6
+ import * as BorrowingFee from "../borrowing/types";
7
+ import { GetPairBorrowingFeeV2Context } from "../borrowingV2";
8
+ import { GetPairFundingFeeContext } from "../fundingFees/pairContext";
9
+ import { GetTradeFeesContext, GetLiquidationFeesContext, GetClosingFeeContext, TradeFeesBreakdown, TradeHoldingFees } from "./types";
10
+ import { ContractsVersion } from "../../../contracts/types";
11
+ /**
12
+ * @dev Returns the total fee for a trade in collateral tokens
13
+ * @dev Mirrors the contract's getTotalTradeFeesCollateral function
14
+ * @param collateralIndex Collateral index (not used in calculation, for consistency)
15
+ * @param trader Trader address (for fee tier lookup)
16
+ * @param pairIndex Index of the trading pair
17
+ * @param positionSizeCollateral Position size in collateral tokens
18
+ * @param isCounterTrade Whether the trade is a counter trade
19
+ * @param context Context containing fee parameters and settings
20
+ * @returns Total fee in collateral tokens
21
+ */
22
+ export declare const getTotalTradeFeesCollateral: (collateralIndex: number, trader: string, pairIndex: PairIndex, positionSizeCollateral: number, isCounterTrade: boolean, context: GetTradeFeesContext) => number;
23
+ /**
24
+ * @dev Returns the fee breakdown for a trade
25
+ * @dev Mirrors the contract's getTradeFeesCollateral function
26
+ */
27
+ export declare const getTradeFeesCollateral: (collateralIndex: number, trader: string, pairIndex: PairIndex, positionSizeCollateral: number, isCounterTrade: boolean, context: GetTradeFeesContext) => TradeFeesBreakdown;
28
+ /**
29
+ * @dev Returns total liquidation fee for a trade in collateral tokens
30
+ * @dev Mirrors the contract's getTotalTradeLiqFeesCollateral function
31
+ */
32
+ export declare const getTotalTradeLiqFeesCollateral: (collateralIndex: number, trader: string, pairIndex: PairIndex, collateralAmount: number, context: GetLiquidationFeesContext) => number;
33
+ /**
34
+ * @dev Legacy function for backward compatibility
35
+ * @deprecated Use getTotalTradeFeesCollateral instead
36
+ */
37
+ export declare const getClosingFee: (collateralAmount: number, leverage: number, pairIndex: PairIndex, pairFee: Fee | undefined, _collateralPriceUsd?: number | undefined, isCounterTrade?: boolean, trader?: string, context?: GetClosingFeeContext) => number;
38
+ /**
39
+ * @dev Context for holding fees calculation with structured sub-contexts
40
+ */
41
+ export type GetStructuredHoldingFeesContext = {
42
+ contractsVersion: ContractsVersion;
43
+ currentTimestamp: number;
44
+ collateralPriceUsd: number;
45
+ borrowingV1?: GetBorrowingFeeContext;
46
+ borrowingV2?: GetPairBorrowingFeeV2Context;
47
+ funding?: GetPairFundingFeeContext;
48
+ initialAccFees?: BorrowingFee.InitialAccFees;
49
+ };
50
+ /**
51
+ * @dev Calculates total holding fees for a trade (funding + borrowing fees)
52
+ * @param trade The trade to calculate fees for
53
+ * @param tradeInfo Trade info containing contracts version
54
+ * @param tradeFeesData Trade fees data containing initial acc fees
55
+ * @param currentPairPrice Current pair price
56
+ * @param context Structured context with sub-contexts for each fee type
57
+ * @returns Object containing all holding fee components
58
+ */
59
+ export declare const getTradePendingHoldingFeesCollateral: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: GetStructuredHoldingFeesContext) => TradeHoldingFees;
60
+ export * from "./types";
61
+ export * from "./converter";
62
+ export * from "./builder";
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Trading fee calculations for opening and closing positions
4
+ */
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.getTradePendingHoldingFeesCollateral = exports.getClosingFee = exports.getTotalTradeLiqFeesCollateral = exports.getTradeFeesCollateral = exports.getTotalTradeFeesCollateral = void 0;
21
+ const borrowing_1 = require("../borrowing");
22
+ const borrowingV2_1 = require("../borrowingV2");
23
+ const fundingFees_1 = require("../fundingFees");
24
+ const tiers_1 = require("../tiers");
25
+ const types_1 = require("../../../contracts/types");
26
+ /**
27
+ * @dev Returns the total fee for a trade in collateral tokens
28
+ * @dev Mirrors the contract's getTotalTradeFeesCollateral function
29
+ * @param collateralIndex Collateral index (not used in calculation, for consistency)
30
+ * @param trader Trader address (for fee tier lookup)
31
+ * @param pairIndex Index of the trading pair
32
+ * @param positionSizeCollateral Position size in collateral tokens
33
+ * @param isCounterTrade Whether the trade is a counter trade
34
+ * @param context Context containing fee parameters and settings
35
+ * @returns Total fee in collateral tokens
36
+ */
37
+ const getTotalTradeFeesCollateral = (collateralIndex, trader, pairIndex, positionSizeCollateral, isCounterTrade, context) => {
38
+ var _a;
39
+ const { fee, collateralPriceUsd } = context;
40
+ const { totalPositionSizeFeeP, minPositionSizeUsd } = fee;
41
+ // Get counter trade fee rate multiplier (default 1 = 1x)
42
+ const counterTradeFeeRateMultiplier = isCounterTrade && ((_a = context.counterTradeSettings) === null || _a === void 0 ? void 0 : _a[pairIndex])
43
+ ? context.counterTradeSettings[pairIndex].feeRateMultiplier
44
+ : 1;
45
+ // Apply counter trade multiplier to position size
46
+ const adjustedPositionSizeCollateral = positionSizeCollateral * counterTradeFeeRateMultiplier;
47
+ // Calculate minimum position size in collateral
48
+ const minPositionSizeCollateral = minPositionSizeUsd / collateralPriceUsd;
49
+ // Use max of adjusted position size and minimum position size
50
+ const positionSizeBasis = Math.max(adjustedPositionSizeCollateral, minPositionSizeCollateral);
51
+ // Calculate raw fee
52
+ const rawFee = totalPositionSizeFeeP * positionSizeBasis;
53
+ // Apply trader fee tier if available
54
+ return (0, tiers_1.calculateFeeAmount)(trader, rawFee, context.traderFeeMultiplier);
55
+ };
56
+ exports.getTotalTradeFeesCollateral = getTotalTradeFeesCollateral;
57
+ /**
58
+ * @dev Returns the fee breakdown for a trade
59
+ * @dev Mirrors the contract's getTradeFeesCollateral function
60
+ */
61
+ const getTradeFeesCollateral = (collateralIndex, trader, pairIndex, positionSizeCollateral, isCounterTrade, context) => {
62
+ const totalFees = (0, exports.getTotalTradeFeesCollateral)(collateralIndex, trader, pairIndex, positionSizeCollateral, isCounterTrade, context);
63
+ const { globalTradeFeeParams } = context;
64
+ const totalP = globalTradeFeeParams.referralFeeP +
65
+ globalTradeFeeParams.govFeeP +
66
+ globalTradeFeeParams.triggerOrderFeeP +
67
+ globalTradeFeeParams.gnsOtcFeeP +
68
+ globalTradeFeeParams.gTokenFeeP;
69
+ // Distribute fees proportionally
70
+ return {
71
+ referralFeeCollateral: (totalFees * globalTradeFeeParams.referralFeeP) / totalP,
72
+ govFeeCollateral: (totalFees * globalTradeFeeParams.govFeeP) / totalP,
73
+ triggerFeeCollateral: (totalFees * globalTradeFeeParams.triggerOrderFeeP) / totalP,
74
+ gnsOtcFeeCollateral: (totalFees * globalTradeFeeParams.gnsOtcFeeP) / totalP,
75
+ gTokenFeeCollateral: (totalFees * globalTradeFeeParams.gTokenFeeP) / totalP,
76
+ };
77
+ };
78
+ exports.getTradeFeesCollateral = getTradeFeesCollateral;
79
+ /**
80
+ * @dev Returns total liquidation fee for a trade in collateral tokens
81
+ * @dev Mirrors the contract's getTotalTradeLiqFeesCollateral function
82
+ */
83
+ const getTotalTradeLiqFeesCollateral = (collateralIndex, trader, pairIndex, collateralAmount, context) => {
84
+ const { totalLiqCollateralFeeP } = context;
85
+ // Calculate raw liquidation fee
86
+ const rawFee = collateralAmount * totalLiqCollateralFeeP;
87
+ // Apply trader fee tier if available
88
+ return (0, tiers_1.calculateFeeAmount)(trader, rawFee, context.traderFeeMultiplier);
89
+ };
90
+ exports.getTotalTradeLiqFeesCollateral = getTotalTradeLiqFeesCollateral;
91
+ /**
92
+ * @dev Legacy function for backward compatibility
93
+ * @deprecated Use getTotalTradeFeesCollateral instead
94
+ */
95
+ const getClosingFee = (collateralAmount, leverage, pairIndex, pairFee,
96
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
97
+ _collateralPriceUsd = 0, // Kept for backward compatibility
98
+ isCounterTrade = false, trader, context) => {
99
+ if (!pairFee || !context)
100
+ return 0;
101
+ const positionSizeCollateral = collateralAmount * leverage;
102
+ return (0, exports.getTotalTradeFeesCollateral)(0, // collateralIndex not used
103
+ trader || "", pairIndex, positionSizeCollateral, isCounterTrade, context);
104
+ };
105
+ exports.getClosingFee = getClosingFee;
106
+ /**
107
+ * @dev Calculates total holding fees for a trade (funding + borrowing fees)
108
+ * @param trade The trade to calculate fees for
109
+ * @param tradeInfo Trade info containing contracts version
110
+ * @param tradeFeesData Trade fees data containing initial acc fees
111
+ * @param currentPairPrice Current pair price
112
+ * @param context Structured context with sub-contexts for each fee type
113
+ * @returns Object containing all holding fee components
114
+ */
115
+ const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
116
+ const positionSizeCollateral = trade.collateralAmount * trade.leverage;
117
+ // Calculate funding fees (v10+ only)
118
+ let fundingFeeCollateral = 0;
119
+ if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
120
+ context.funding &&
121
+ tradeFeesData.initialAccFundingFeeP !== undefined) {
122
+ fundingFeeCollateral = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, Object.assign(Object.assign({}, context.funding), { currentTimestamp: context.currentTimestamp }));
123
+ }
124
+ // Calculate borrowing fees v2 (v10+ only)
125
+ let borrowingFeeCollateral = 0;
126
+ if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
127
+ context.borrowingV2 &&
128
+ tradeFeesData.initialAccBorrowingFeeP !== undefined) {
129
+ borrowingFeeCollateral = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
130
+ positionSizeCollateral,
131
+ openPrice: trade.openPrice,
132
+ currentPairPrice,
133
+ initialAccBorrowingFeeP: tradeFeesData.initialAccBorrowingFeeP,
134
+ currentTimestamp: context.currentTimestamp,
135
+ }, context.borrowingV2);
136
+ }
137
+ // Calculate v1 borrowing fees (some markets use v1 indefinitely)
138
+ let borrowingFeeCollateral_old = 0;
139
+ if (context.borrowingV1 && context.initialAccFees) {
140
+ borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFees, currentPairPrice, context.borrowingV1);
141
+ }
142
+ return {
143
+ fundingFeeCollateral,
144
+ borrowingFeeCollateral,
145
+ borrowingFeeCollateral_old,
146
+ totalFeeCollateral: fundingFeeCollateral +
147
+ borrowingFeeCollateral +
148
+ borrowingFeeCollateral_old,
149
+ };
150
+ };
151
+ exports.getTradePendingHoldingFeesCollateral = getTradePendingHoldingFeesCollateral;
152
+ // Export types
153
+ __exportStar(require("./types"), exports);
154
+ __exportStar(require("./converter"), exports);
155
+ __exportStar(require("./builder"), exports);
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @dev Types for trading fee calculations (open/close position fees)
3
+ */
4
+ import { Fee, CounterTradeSettings, GlobalTradeFeeParams } from "../../types";
5
+ /**
6
+ * @dev Breakdown of trading fees into components
7
+ */
8
+ export type TradeFeesBreakdown = {
9
+ referralFeeCollateral: number;
10
+ govFeeCollateral: number;
11
+ triggerFeeCollateral: number;
12
+ gnsOtcFeeCollateral: number;
13
+ gTokenFeeCollateral: number;
14
+ };
15
+ /**
16
+ * @dev Context for calculating trading fees
17
+ */
18
+ export type GetTradeFeesContext = {
19
+ fee: Fee;
20
+ collateralPriceUsd: number;
21
+ globalTradeFeeParams: GlobalTradeFeeParams;
22
+ counterTradeSettings?: {
23
+ [pairIndex: number]: CounterTradeSettings;
24
+ };
25
+ traderFeeMultiplier?: number;
26
+ };
27
+ /**
28
+ * @dev Context for calculating liquidation fees
29
+ */
30
+ export type GetLiquidationFeesContext = {
31
+ totalLiqCollateralFeeP: number;
32
+ globalTradeFeeParams: GlobalTradeFeeParams;
33
+ traderFeeMultiplier?: number;
34
+ };
35
+ /**
36
+ * @dev Legacy support
37
+ */
38
+ export type GetClosingFeeContext = GetTradeFeesContext;
39
+ /**
40
+ * @dev Holding fees breakdown (funding + borrowing)
41
+ */
42
+ export type TradeHoldingFees = {
43
+ fundingFeeCollateral: number;
44
+ borrowingFeeCollateral: number;
45
+ borrowingFeeCollateral_old: number;
46
+ totalFeeCollateral: number;
47
+ };
48
+ export type { GlobalTradeFeeParams };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Types for trading fee calculations (open/close position fees)
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,7 +1,10 @@
1
1
  export * from "./fees";
2
- export * from "./pnl";
2
+ export * from "./pnl/index";
3
3
  export * from "./spread";
4
4
  export * from "./liquidation";
5
- export * from "./fees";
6
5
  export * from "./types";
7
6
  export * from "./oiWindows";
7
+ export * from "./priceImpact";
8
+ export * from "./utils";
9
+ export * from "./effectiveLeverage";
10
+ export * from "./counterTrade";
@@ -15,9 +15,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./fees"), exports);
18
- __exportStar(require("./pnl"), exports);
18
+ __exportStar(require("./pnl/index"), exports);
19
19
  __exportStar(require("./spread"), exports);
20
20
  __exportStar(require("./liquidation"), exports);
21
- __exportStar(require("./fees"), exports);
22
21
  __exportStar(require("./types"), exports);
23
22
  __exportStar(require("./oiWindows"), exports);
23
+ __exportStar(require("./priceImpact"), exports);
24
+ __exportStar(require("./utils"), exports);
25
+ __exportStar(require("./effectiveLeverage"), exports);
26
+ __exportStar(require("./counterTrade"), exports);
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @dev Liquidation price context builder module
3
+ * @dev Provides builder functions for creating liquidation price contexts
4
+ */
5
+ import { GlobalTradingVariablesType } from "../../backend/tradingVariables/types";
6
+ import { TradeContainer, UserPriceImpact } from "../types";
7
+ import { GetLiquidationPriceContext } from "./types";
8
+ /**
9
+ * @dev Builds a complete context for liquidation price calculations
10
+ * @param globalTradingVariables The transformed global trading variables from backend
11
+ * @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
12
+ * @param additionalParams Additional parameters not available in trading variables
13
+ * @returns Complete context ready for getLiquidationPrice
14
+ */
15
+ export declare const buildLiquidationPriceContext: (globalTradingVariables: GlobalTradingVariablesType, tradeContainer: TradeContainer, additionalParams: {
16
+ currentBlock: number;
17
+ currentTimestamp: number;
18
+ currentPairPrice: number;
19
+ spreadP: number;
20
+ traderFeeMultiplier?: number;
21
+ additionalFeeCollateral?: number;
22
+ partialCloseMultiplier?: number;
23
+ beforeOpened?: boolean;
24
+ userPriceImpact?: UserPriceImpact;
25
+ }) => GetLiquidationPriceContext;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Liquidation price context builder module
4
+ * @dev Provides builder functions for creating liquidation price contexts
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.buildLiquidationPriceContext = void 0;
8
+ const builder_1 = require("../fees/borrowing/builder");
9
+ const builder_2 = require("../fees/borrowingV2/builder");
10
+ const builder_3 = require("../fees/fundingFees/builder");
11
+ const builder_4 = require("../fees/trading/builder");
12
+ /**
13
+ * @dev Builds a complete context for liquidation price calculations
14
+ * @param globalTradingVariables The transformed global trading variables from backend
15
+ * @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
16
+ * @param additionalParams Additional parameters not available in trading variables
17
+ * @returns Complete context ready for getLiquidationPrice
18
+ */
19
+ const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, additionalParams) => {
20
+ var _a;
21
+ const { trade, tradeInfo } = tradeContainer;
22
+ const collateralIndex = trade.collateralIndex || 1;
23
+ const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
24
+ if (!tradeContainer.liquidationParams) {
25
+ throw new Error("Liquidation params are required for liquidation price calculation");
26
+ }
27
+ return {
28
+ // Core shared context
29
+ core: {
30
+ currentBlock: additionalParams.currentBlock,
31
+ currentTimestamp: additionalParams.currentTimestamp,
32
+ collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
33
+ contractsVersion: tradeInfo.contractsVersion,
34
+ spreadP: additionalParams.spreadP,
35
+ },
36
+ // Build sub-contexts using dedicated builders
37
+ borrowingV1: (0, builder_1.buildBorrowingV1Context)(globalTradingVariables, collateralIndex, additionalParams.currentBlock),
38
+ borrowingV2: (0, builder_2.buildBorrowingV2Context)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
39
+ funding: (0, builder_3.buildFundingContext)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
40
+ trading: Object.assign(Object.assign({}, (0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier)), { counterTradeSettings: globalTradingVariables.counterTradeSettings, userPriceImpact: additionalParams.userPriceImpact }),
41
+ // Trade-specific data
42
+ tradeData: {
43
+ tradeInfo,
44
+ tradeFeesData: tradeContainer.tradeFeesData,
45
+ liquidationParams: tradeContainer.liquidationParams,
46
+ initialAccFees: tradeContainer.initialAccFees,
47
+ },
48
+ // Additional parameters for liquidation calculation
49
+ liquidationSpecific: {
50
+ currentPairPrice: additionalParams.currentPairPrice,
51
+ additionalFeeCollateral: additionalParams.additionalFeeCollateral || 0,
52
+ partialCloseMultiplier: additionalParams.partialCloseMultiplier || 1,
53
+ beforeOpened: additionalParams.beforeOpened || false,
54
+ isCounterTrade: trade.isCounterTrade || false,
55
+ userPriceImpact: additionalParams.userPriceImpact,
56
+ },
57
+ };
58
+ };
59
+ exports.buildLiquidationPriceContext = buildLiquidationPriceContext;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @dev Converters for liquidation data between contract and SDK formats
3
+ */
4
+ import { IPairsStorage } from "../../contracts/types/generated/GNSMultiCollatDiamond";
5
+ import { LiquidationParams } from "../types";
6
+ /**
7
+ * @dev Converts contract liquidation params to SDK format
8
+ * @param params Group liquidation params from contract
9
+ * @returns Normalized liquidation params
10
+ */
11
+ export declare const convertLiquidationParams: (params: IPairsStorage.GroupLiquidationParamsStructOutput) => LiquidationParams;
12
+ /**
13
+ * @dev Converts array of liquidation params from contract
14
+ * @param paramsArray Array of group liquidation params
15
+ * @returns Array of normalized liquidation params
16
+ */
17
+ export declare const convertLiquidationParamsArray: (paramsArray: IPairsStorage.GroupLiquidationParamsStructOutput[]) => LiquidationParams[];
18
+ /**
19
+ * @dev Converts liquidation params to contract format (for encoding)
20
+ * @param params SDK liquidation params
21
+ * @returns Contract-formatted liquidation params
22
+ */
23
+ export declare const encodeLiquidationParams: (params: LiquidationParams) => IPairsStorage.GroupLiquidationParamsStruct;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Converters for liquidation data between contract and SDK formats
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.encodeLiquidationParams = exports.convertLiquidationParamsArray = exports.convertLiquidationParams = void 0;
7
+ /**
8
+ * @dev Converts contract liquidation params to SDK format
9
+ * @param params Group liquidation params from contract
10
+ * @returns Normalized liquidation params
11
+ */
12
+ const convertLiquidationParams = (params) => {
13
+ const ONCHAIN_LIQ_THRESHOLD = 0.9;
14
+ return {
15
+ maxLiqSpreadP: Number(params.maxLiqSpreadP) / 1e10 / 100,
16
+ startLiqThresholdP: Number(params.startLiqThresholdP) / 1e10 / 100 || ONCHAIN_LIQ_THRESHOLD,
17
+ endLiqThresholdP: Number(params.endLiqThresholdP) / 1e10 / 100 || ONCHAIN_LIQ_THRESHOLD,
18
+ startLeverage: Number(params.startLeverage) / 1e3,
19
+ endLeverage: Number(params.endLeverage) / 1e3, // 1e3 → float
20
+ };
21
+ };
22
+ exports.convertLiquidationParams = convertLiquidationParams;
23
+ /**
24
+ * @dev Converts array of liquidation params from contract
25
+ * @param paramsArray Array of group liquidation params
26
+ * @returns Array of normalized liquidation params
27
+ */
28
+ const convertLiquidationParamsArray = (paramsArray) => {
29
+ return paramsArray.map(exports.convertLiquidationParams);
30
+ };
31
+ exports.convertLiquidationParamsArray = convertLiquidationParamsArray;
32
+ /**
33
+ * @dev Converts liquidation params to contract format (for encoding)
34
+ * @param params SDK liquidation params
35
+ * @returns Contract-formatted liquidation params
36
+ */
37
+ const encodeLiquidationParams = (params) => {
38
+ return {
39
+ maxLiqSpreadP: Math.round(params.maxLiqSpreadP * 100 * 1e10),
40
+ startLiqThresholdP: Math.round(params.startLiqThresholdP * 100 * 1e10),
41
+ endLiqThresholdP: Math.round(params.endLiqThresholdP * 100 * 1e10),
42
+ startLeverage: Math.round(params.startLeverage * 1e3),
43
+ endLeverage: Math.round(params.endLeverage * 1e3), // float → 1e3
44
+ };
45
+ };
46
+ exports.encodeLiquidationParams = encodeLiquidationParams;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @dev Main export file for liquidation module
3
+ */
4
+ import { Trade, LiquidationParams } from "..";
5
+ import { GetLiquidationPriceContext } from "./types";
6
+ /**
7
+ * @dev Calculate liquidation price with structured context
8
+ * @param trade The trade to calculate liquidation price for
9
+ * @param context Structured context with all required data
10
+ * @returns Liquidation price
11
+ */
12
+ export declare const getLiquidationPrice: (trade: Trade, context: GetLiquidationPriceContext) => number;
13
+ /**
14
+ * @dev Calculate liquidation price after a position size update
15
+ * @dev Mirrors the contract's IncreasePositionSizeUtils.sol and DecreasePositionSizeUtils.sol logic
16
+ * @param existingTrade The current trade before the update
17
+ * @param newCollateralAmount New collateral amount after the update
18
+ * @param newLeverage New leverage after the update
19
+ * @param isLeverageUpdate Whether this is a leverage update vs regular position change
20
+ * @param positionSizeCollateralDelta The absolute change in position size (in collateral terms)
21
+ * @param pnlToRealizeCollateral PnL to be realized (only relevant for leverage decrease)
22
+ * @param context Structured context with all required data (including additionalFeesCollateral for increases)
23
+ * @returns New liquidation price after the update
24
+ */
25
+ export declare const getLiquidationPriceAfterPositionUpdate: (existingTrade: Trade, newCollateralAmount: number, newLeverage: number, isLeverageUpdate: boolean, positionSizeCollateralDelta: number, pnlToRealizeCollateral: number, context: GetLiquidationPriceContext) => number;
26
+ export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
27
+ export { convertLiquidationParams, convertLiquidationParamsArray, encodeLiquidationParams, } from "./converter";
28
+ export * from "./types";
29
+ export * from "./builder";