@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
@@ -1,5 +1,7 @@
1
1
  import { FeeTiers, TraderFeeTiers } from "../../types";
2
2
  import { FeeTier } from "./types";
3
+ export * from "./types";
4
+ export * from "./converter";
3
5
  export declare const TRAILING_PERIOD_DAYS = 30;
4
6
  export declare const FEE_MULTIPLIER_SCALE = 1;
5
7
  export declare const MAX_FEE_TIERS = 8;
@@ -10,3 +12,19 @@ export declare const computeFeeMultiplier: (feeTiers: FeeTiers, traderFeeTiers:
10
12
  feeMultiplier: number;
11
13
  trailingPoints: number;
12
14
  };
15
+ /**
16
+ * @dev Calculates the final fee amount after applying the trader's fee tier discount
17
+ * @dev Mirrors the contract's calculateFeeAmount function
18
+ * @param trader The address of the trader (not used in SDK, for consistency)
19
+ * @param normalFeeAmountCollateral The base fee amount before any discounts
20
+ * @param feeMultiplier The trader's fee multiplier (e.g., 0.8 = 80% of normal fee)
21
+ * @returns The final fee amount after applying discount
22
+ */
23
+ export declare const calculateFeeAmount: (trader: string, normalFeeAmountCollateral: number, feeMultiplier?: number) => number;
24
+ /**
25
+ * @dev Helper function to get trader's fee multiplier from fee tiers data
26
+ * @param feeTiers System fee tiers configuration
27
+ * @param traderFeeTiers Trader's fee tier data
28
+ * @returns Fee multiplier (1e3 precision)
29
+ */
30
+ export declare const getTraderFeeMultiplier: (feeTiers: FeeTiers, traderFeeTiers: TraderFeeTiers) => number;
@@ -1,7 +1,23 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.computeFeeMultiplier = exports.getFeeMultiplier = exports.getFeeTiersCount = exports.getCurrentDay = exports.MAX_FEE_TIERS = exports.FEE_MULTIPLIER_SCALE = exports.TRAILING_PERIOD_DAYS = void 0;
17
+ exports.getTraderFeeMultiplier = exports.calculateFeeAmount = exports.computeFeeMultiplier = exports.getFeeMultiplier = exports.getFeeTiersCount = exports.getCurrentDay = exports.MAX_FEE_TIERS = exports.FEE_MULTIPLIER_SCALE = exports.TRAILING_PERIOD_DAYS = void 0;
4
18
  const types_1 = require("./types");
19
+ __exportStar(require("./types"), exports);
20
+ __exportStar(require("./converter"), exports);
5
21
  exports.TRAILING_PERIOD_DAYS = 30;
6
22
  exports.FEE_MULTIPLIER_SCALE = 1;
7
23
  exports.MAX_FEE_TIERS = 8;
@@ -52,3 +68,31 @@ const computeFeeMultiplier = (feeTiers, traderFeeTiers) => {
52
68
  };
53
69
  };
54
70
  exports.computeFeeMultiplier = computeFeeMultiplier;
71
+ /**
72
+ * @dev Calculates the final fee amount after applying the trader's fee tier discount
73
+ * @dev Mirrors the contract's calculateFeeAmount function
74
+ * @param trader The address of the trader (not used in SDK, for consistency)
75
+ * @param normalFeeAmountCollateral The base fee amount before any discounts
76
+ * @param feeMultiplier The trader's fee multiplier (e.g., 0.8 = 80% of normal fee)
77
+ * @returns The final fee amount after applying discount
78
+ */
79
+ const calculateFeeAmount = (trader, normalFeeAmountCollateral, feeMultiplier) => {
80
+ // If no fee multiplier provided or it's 0, return normal fee
81
+ if (!feeMultiplier || feeMultiplier === 0) {
82
+ return normalFeeAmountCollateral;
83
+ }
84
+ // Apply fee multiplier discount
85
+ return (feeMultiplier * normalFeeAmountCollateral) / exports.FEE_MULTIPLIER_SCALE;
86
+ };
87
+ exports.calculateFeeAmount = calculateFeeAmount;
88
+ /**
89
+ * @dev Helper function to get trader's fee multiplier from fee tiers data
90
+ * @param feeTiers System fee tiers configuration
91
+ * @param traderFeeTiers Trader's fee tier data
92
+ * @returns Fee multiplier (1e3 precision)
93
+ */
94
+ const getTraderFeeMultiplier = (feeTiers, traderFeeTiers) => {
95
+ const { feeMultiplier } = (0, exports.computeFeeMultiplier)(feeTiers, traderFeeTiers);
96
+ return feeMultiplier;
97
+ };
98
+ exports.getTraderFeeMultiplier = getTraderFeeMultiplier;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @dev Converters for trading fee data between contract and SDK formats
3
+ */
4
+ import { CounterTradeSettings } from "../../types";
5
+ import { GlobalTradeFeeParams } from "./types";
6
+ /**
7
+ * @dev Converts contract counter trade settings to SDK format
8
+ * @param feeRateMultiplier Fee rate multiplier from contract (1e3 precision)
9
+ * @param maxLeverage Max leverage from contract (1e3 precision)
10
+ * @returns Normalized counter trade settings
11
+ */
12
+ export declare const convertCounterTradeSettings: (feeRateMultiplier: number, maxLeverage: number) => CounterTradeSettings;
13
+ /**
14
+ * @dev Converts array of counter trade fee rate multipliers from contract
15
+ * @param multipliers Array of fee rate multipliers (1e3 precision)
16
+ * @returns Array of normalized multipliers
17
+ */
18
+ export declare const convertCounterTradeFeeRateMultipliers: (multipliers: number[]) => number[];
19
+ /**
20
+ * @dev Converts global trade fee params from contract to SDK format
21
+ * @param contractParams Global trade fee params from contract
22
+ * @returns Normalized global trade fee params
23
+ */
24
+ export declare const convertGlobalTradeFeeParams: (contractParams: {
25
+ referralFeeP: number;
26
+ govFeeP: number;
27
+ triggerOrderFeeP: number;
28
+ gnsOtcFeeP: number;
29
+ gTokenFeeP: number;
30
+ }) => GlobalTradeFeeParams;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Converters for trading fee data between contract and SDK formats
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.convertGlobalTradeFeeParams = exports.convertCounterTradeFeeRateMultipliers = exports.convertCounterTradeSettings = void 0;
7
+ /**
8
+ * @dev Converts contract counter trade settings to SDK format
9
+ * @param feeRateMultiplier Fee rate multiplier from contract (1e3 precision)
10
+ * @param maxLeverage Max leverage from contract (1e3 precision)
11
+ * @returns Normalized counter trade settings
12
+ */
13
+ const convertCounterTradeSettings = (feeRateMultiplier, maxLeverage) => {
14
+ return {
15
+ feeRateMultiplier: feeRateMultiplier / 1000,
16
+ maxLeverage: maxLeverage / 1000, // 1e3 → float
17
+ };
18
+ };
19
+ exports.convertCounterTradeSettings = convertCounterTradeSettings;
20
+ /**
21
+ * @dev Converts array of counter trade fee rate multipliers from contract
22
+ * @param multipliers Array of fee rate multipliers (1e3 precision)
23
+ * @returns Array of normalized multipliers
24
+ */
25
+ const convertCounterTradeFeeRateMultipliers = (multipliers) => {
26
+ return multipliers.map(m => m / 1000);
27
+ };
28
+ exports.convertCounterTradeFeeRateMultipliers = convertCounterTradeFeeRateMultipliers;
29
+ /**
30
+ * @dev Converts global trade fee params from contract to SDK format
31
+ * @param contractParams Global trade fee params from contract
32
+ * @returns Normalized global trade fee params
33
+ */
34
+ const convertGlobalTradeFeeParams = (contractParams) => {
35
+ return {
36
+ referralFeeP: contractParams.referralFeeP / 1e10 / 100,
37
+ govFeeP: contractParams.govFeeP / 1e10 / 100,
38
+ triggerOrderFeeP: contractParams.triggerOrderFeeP / 1e10 / 100,
39
+ gnsOtcFeeP: contractParams.gnsOtcFeeP / 1e10 / 100,
40
+ gTokenFeeP: contractParams.gTokenFeeP / 1e10 / 100,
41
+ };
42
+ };
43
+ exports.convertGlobalTradeFeeParams = convertGlobalTradeFeeParams;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * @dev Trading fee calculations for opening and closing positions
3
+ */
4
+ import { Fee, PairIndex, Trade, TradeInfo, TradeFeesData } from "../../types";
5
+ import { GetTradeFeesContext, GetLiquidationFeesContext, GetClosingFeeContext, TradeFeesBreakdown, TradeHoldingFees } from "./types";
6
+ import { ContractsVersion } from "../../../contracts/types";
7
+ import * as BorrowingFee from "../borrowing/types";
8
+ /**
9
+ * @dev Returns the total fee for a trade in collateral tokens
10
+ * @dev Mirrors the contract's getTotalTradeFeesCollateral function
11
+ * @param collateralIndex Collateral index (not used in calculation, for consistency)
12
+ * @param trader Trader address (for fee tier lookup)
13
+ * @param pairIndex Index of the trading pair
14
+ * @param positionSizeCollateral Position size in collateral tokens
15
+ * @param isCounterTrade Whether the trade is a counter trade
16
+ * @param context Context containing fee parameters and settings
17
+ * @returns Total fee in collateral tokens
18
+ */
19
+ export declare const getTotalTradeFeesCollateral: (collateralIndex: number, trader: string, pairIndex: PairIndex, positionSizeCollateral: number, isCounterTrade: boolean, context: GetTradeFeesContext) => number;
20
+ /**
21
+ * @dev Returns the fee breakdown for a trade
22
+ * @dev Mirrors the contract's getTradeFeesCollateral function
23
+ */
24
+ export declare const getTradeFeesCollateral: (collateralIndex: number, trader: string, pairIndex: PairIndex, positionSizeCollateral: number, isCounterTrade: boolean, context: GetTradeFeesContext) => TradeFeesBreakdown;
25
+ /**
26
+ * @dev Returns total liquidation fee for a trade in collateral tokens
27
+ * @dev Mirrors the contract's getTotalTradeLiqFeesCollateral function
28
+ */
29
+ export declare const getTotalTradeLiqFeesCollateral: (collateralIndex: number, trader: string, pairIndex: PairIndex, collateralAmount: number, context: GetLiquidationFeesContext) => number;
30
+ /**
31
+ * @dev Legacy function for backward compatibility
32
+ * @deprecated Use getTotalTradeFeesCollateral instead
33
+ */
34
+ export declare const getClosingFee: (collateralAmount: number, leverage: number, pairIndex: PairIndex, pairFee: Fee | undefined, _collateralPriceUsd?: number | undefined, isCounterTrade?: boolean, trader?: string, context?: GetClosingFeeContext) => number;
35
+ /**
36
+ * @dev Calculates total holding fees for a trade (funding + borrowing fees)
37
+ * @param trade The trade to calculate fees for
38
+ * @param tradeInfo Trade info containing contracts version
39
+ * @param tradeFeesData Trade fees data containing initial acc fees
40
+ * @param currentPairPrice Current pair price
41
+ * @param context Context with fee parameters
42
+ * @returns Object containing all holding fee components
43
+ */
44
+ export declare const getTradePendingHoldingFeesCollateral: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: {
45
+ [key: string]: any;
46
+ contractsVersion?: ContractsVersion | undefined;
47
+ currentBlock?: number | undefined;
48
+ groups?: BorrowingFee.Group[] | undefined;
49
+ pairs?: BorrowingFee.Pair[] | undefined;
50
+ collateralPriceUsd?: number | undefined;
51
+ initialAccFees?: BorrowingFee.InitialAccFees | undefined;
52
+ }) => TradeHoldingFees;
53
+ export * from "./types";
54
+ export * from "./converter";
@@ -0,0 +1,147 @@
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 tiers_1 = require("../tiers");
22
+ const fundingFees_1 = require("../fundingFees");
23
+ const borrowingV2_1 = require("../borrowingV2");
24
+ const borrowing_1 = require("../borrowing");
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 Context with fee parameters
113
+ * @returns Object containing all holding fee components
114
+ */
115
+ const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
116
+ var _a;
117
+ // Calculate funding fees (v10+ only)
118
+ const fundingFeeCollateral = ((_a = context.contractsVersion) !== null && _a !== void 0 ? _a : tradeInfo.contractsVersion) >=
119
+ types_1.ContractsVersion.V10
120
+ ? (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, context)
121
+ : 0;
122
+ // Calculate borrowing fees v2
123
+ const borrowingFeeCollateral = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
124
+ positionSizeCollateral: trade.collateralAmount * trade.leverage,
125
+ openPrice: trade.openPrice,
126
+ collateralIndex: trade.collateralIndex,
127
+ pairIndex: trade.pairIndex,
128
+ currentPairPrice,
129
+ initialAccBorrowingFeeP: tradeFeesData.initialAccBorrowingFeeP,
130
+ currentTimestamp: context.currentTimestamp,
131
+ }, context);
132
+ // Calculate v1 borrowing fees (some markets use v1 indefinitely)
133
+ const borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, context.initialAccFees || { accPairFee: 0, accGroupFee: 0, block: 0 }, // Use context initial fees or empty
134
+ context);
135
+ return {
136
+ fundingFeeCollateral,
137
+ borrowingFeeCollateral,
138
+ borrowingFeeCollateral_old,
139
+ totalFeeCollateral: fundingFeeCollateral +
140
+ borrowingFeeCollateral +
141
+ borrowingFeeCollateral_old,
142
+ };
143
+ };
144
+ exports.getTradePendingHoldingFeesCollateral = getTradePendingHoldingFeesCollateral;
145
+ // Export types
146
+ __exportStar(require("./types"), exports);
147
+ __exportStar(require("./converter"), 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,8 @@
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";
@@ -15,9 +15,10 @@ 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);
@@ -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,31 @@
1
+ /**
2
+ * @dev Main export file for liquidation module
3
+ */
4
+ import { Trade, Fee, BorrowingFee, LiquidationParams } from "..";
5
+ import { GetLiquidationPriceContext, LiqPriceInput } from "./types";
6
+ /**
7
+ * @dev Wrapper function that mirrors the contract's getTradeLiquidationPrice signature
8
+ * @dev This is the main entry point matching the contract interface
9
+ * @param input Liquidation price input parameters
10
+ * @param context Additional context for SDK calculations
11
+ * @returns Liquidation price
12
+ */
13
+ export declare const getTradeLiquidationPrice: (input: LiqPriceInput, context: Omit<GetLiquidationPriceContext, "currentPairPrice" | "isCounterTrade" | "partialCloseMultiplier" | "additionalFeeCollateral" | "beforeOpened" | "liquidationParams"> & {
14
+ fee: Fee;
15
+ }) => number;
16
+ export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
17
+ export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
18
+ /**
19
+ * @dev Simplified wrapper for getTradeLiquidationPrice
20
+ * @dev Mirrors the contract's simplified overload
21
+ * @param trade The trade to calculate liquidation price for
22
+ * @param additionalFeeCollateral Additional fees to consider
23
+ * @param currentPairPrice Current pair price
24
+ * @param context Context with all required data
25
+ * @returns Liquidation price
26
+ */
27
+ export declare const getTradeLiquidationPriceSimple: (trade: Trade, additionalFeeCollateral: number, currentPairPrice: number, context: Omit<GetLiquidationPriceContext, "currentPairPrice" | "additionalFeeCollateral"> & {
28
+ fee: Fee;
29
+ }) => number;
30
+ export { convertLiquidationParams, convertLiquidationParamsArray, encodeLiquidationParams, } from "./converter";
31
+ export * from "./types";