@gainsnetwork/sdk 0.0.0-v10.rc2 → 0.0.0-v10.rc21

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 (238) 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 +3 -0
  4. package/lib/backend/index.js +28 -0
  5. package/lib/backend/tradingVariables/backend.types.d.ts +318 -0
  6. package/lib/backend/tradingVariables/backend.types.js +2 -0
  7. package/lib/backend/tradingVariables/converter.d.ts +34 -0
  8. package/lib/backend/tradingVariables/converter.js +338 -0
  9. package/lib/backend/tradingVariables/index.d.ts +5 -0
  10. package/lib/backend/tradingVariables/index.js +96 -0
  11. package/lib/backend/tradingVariables/types.d.ts +113 -0
  12. package/lib/backend/tradingVariables/types.js +14 -0
  13. package/lib/constants.d.ts +20 -0
  14. package/lib/constants.js +24 -3
  15. package/lib/contracts/addresses.json +20 -0
  16. package/lib/contracts/index.js +3 -1
  17. package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
  18. package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
  19. package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
  20. package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
  21. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
  22. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
  23. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
  24. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
  25. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +7 -20
  26. package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
  27. package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
  28. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
  29. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
  30. package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
  31. package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
  32. package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
  33. package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
  34. package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
  35. package/lib/contracts/types/generated/GNSTrading.js +2 -0
  36. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
  37. package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
  38. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
  39. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
  40. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
  41. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
  42. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
  43. package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
  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.js +7 -16
  55. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  56. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  57. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
  58. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
  59. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
  60. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
  61. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
  62. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
  63. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
  64. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
  65. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
  66. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
  67. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
  68. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
  69. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
  70. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
  71. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
  72. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
  73. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
  74. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
  75. package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
  76. package/lib/contracts/types/generated/factories/GToken__factory.js +4 -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/index.d.ts +0 -3
  80. package/lib/contracts/utils/index.js +0 -3
  81. package/lib/contracts/utils/openTrades.js +14 -30
  82. package/lib/contracts/utils/pairs.js +20 -0
  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 +12 -0
  120. package/lib/markets/price/marketPrice.js +34 -0
  121. package/lib/markets/price/types.d.ts +23 -0
  122. package/lib/markets/price/types.js +5 -0
  123. package/lib/trade/counterTrade/index.d.ts +2 -0
  124. package/lib/trade/counterTrade/index.js +18 -0
  125. package/lib/trade/counterTrade/types.d.ts +7 -0
  126. package/lib/trade/counterTrade/types.js +2 -0
  127. package/lib/trade/counterTrade/validateCounterTrade.d.ts +10 -0
  128. package/lib/trade/counterTrade/validateCounterTrade.js +29 -0
  129. package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
  130. package/lib/trade/effectiveLeverage/builder.js +30 -0
  131. package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.d.ts +23 -0
  132. package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.js +64 -0
  133. package/lib/trade/effectiveLeverage/index.d.ts +3 -0
  134. package/lib/trade/effectiveLeverage/index.js +22 -0
  135. package/lib/trade/effectiveLeverage/types.d.ts +33 -0
  136. package/lib/trade/effectiveLeverage/types.js +2 -0
  137. package/lib/trade/fees/borrowing/builder.d.ts +14 -0
  138. package/lib/trade/fees/borrowing/builder.js +33 -0
  139. package/lib/trade/fees/borrowing/index.d.ts +15 -4
  140. package/lib/trade/fees/borrowing/index.js +42 -18
  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 +12 -3
  144. package/lib/trade/fees/borrowingV2/converter.js +29 -18
  145. package/lib/{contracts/fetch/fees/borrowingFeesV2.d.ts → trade/fees/borrowingV2/fetcher.d.ts} +2 -3
  146. package/lib/{contracts/fetch/fees/borrowingFeesV2.js → trade/fees/borrowingV2/fetcher.js} +6 -14
  147. package/lib/trade/fees/borrowingV2/index.d.ts +6 -17
  148. package/lib/trade/fees/borrowingV2/index.js +10 -37
  149. package/lib/trade/fees/borrowingV2/types.d.ts +22 -6
  150. package/lib/trade/fees/converter.d.ts +48 -0
  151. package/lib/trade/fees/converter.js +114 -0
  152. package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
  153. package/lib/trade/fees/fundingFees/builder.js +35 -0
  154. package/lib/{contracts/fetch/fees/fundingFees.d.ts → trade/fees/fundingFees/fetcher.d.ts} +2 -2
  155. package/lib/trade/fees/fundingFees/index.d.ts +28 -39
  156. package/lib/trade/fees/fundingFees/index.js +33 -46
  157. package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
  158. package/lib/trade/fees/fundingFees/pairContext.js +17 -0
  159. package/lib/trade/fees/holdingFees/index.d.ts +46 -0
  160. package/lib/trade/fees/holdingFees/index.js +105 -0
  161. package/lib/trade/fees/holdingFees/types.d.ts +23 -0
  162. package/lib/trade/fees/holdingFees/types.js +5 -0
  163. package/lib/trade/fees/index.d.ts +4 -2
  164. package/lib/trade/fees/index.js +45 -36
  165. package/lib/trade/fees/tiers/converter.d.ts +54 -0
  166. package/lib/trade/fees/tiers/converter.js +81 -0
  167. package/lib/trade/fees/tiers/index.d.ts +1 -0
  168. package/lib/trade/fees/tiers/index.js +1 -0
  169. package/lib/trade/fees/trading/builder.d.ts +18 -0
  170. package/lib/trade/fees/trading/builder.js +20 -0
  171. package/lib/trade/fees/trading/converter.d.ts +2 -0
  172. package/lib/trade/fees/trading/converter.js +5 -1
  173. package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
  174. package/lib/trade/fees/trading/holdingFees.js +66 -0
  175. package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
  176. package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
  177. package/lib/trade/fees/trading/index.d.ts +30 -2
  178. package/lib/trade/fees/trading/index.js +52 -1
  179. package/lib/trade/fees/trading/types.d.ts +9 -0
  180. package/lib/trade/index.d.ts +3 -1
  181. package/lib/trade/index.js +3 -1
  182. package/lib/trade/liquidation/builder.d.ts +25 -0
  183. package/lib/trade/liquidation/builder.js +59 -0
  184. package/lib/trade/liquidation/converter.d.ts +23 -0
  185. package/lib/trade/liquidation/converter.js +46 -0
  186. package/lib/trade/liquidation/index.d.ts +16 -0
  187. package/lib/trade/liquidation/index.js +123 -0
  188. package/lib/trade/liquidation/types.d.ts +42 -0
  189. package/lib/trade/liquidation/types.js +2 -0
  190. package/lib/trade/pnl/builder.d.ts +16 -0
  191. package/lib/trade/pnl/builder.js +44 -0
  192. package/lib/trade/pnl/converter.d.ts +47 -0
  193. package/lib/trade/pnl/converter.js +72 -0
  194. package/lib/trade/pnl/index.d.ts +88 -0
  195. package/lib/trade/pnl/index.js +286 -0
  196. package/lib/trade/pnl/types.d.ts +75 -0
  197. package/lib/trade/pnl/types.js +5 -0
  198. package/lib/trade/priceImpact/close/builder.d.ts +23 -0
  199. package/lib/trade/priceImpact/close/builder.js +45 -0
  200. package/lib/trade/priceImpact/close/index.d.ts +22 -0
  201. package/lib/trade/priceImpact/close/index.js +119 -0
  202. package/lib/trade/priceImpact/close/types.d.ts +44 -0
  203. package/lib/trade/priceImpact/close/types.js +5 -0
  204. package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
  205. package/lib/trade/priceImpact/cumulVol/builder.js +43 -0
  206. package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
  207. package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
  208. package/lib/trade/priceImpact/cumulVol/index.d.ts +108 -0
  209. package/lib/trade/priceImpact/cumulVol/index.js +235 -0
  210. package/lib/trade/priceImpact/index.d.ts +18 -5
  211. package/lib/trade/priceImpact/index.js +57 -10
  212. package/lib/trade/priceImpact/open/builder.d.ts +21 -0
  213. package/lib/trade/priceImpact/open/builder.js +43 -0
  214. package/lib/trade/priceImpact/open/index.d.ts +23 -0
  215. package/lib/trade/priceImpact/open/index.js +74 -0
  216. package/lib/trade/priceImpact/open/types.d.ts +41 -0
  217. package/lib/trade/priceImpact/open/types.js +5 -0
  218. package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
  219. package/lib/trade/priceImpact/skew/builder.js +28 -0
  220. package/lib/trade/priceImpact/skew/converter.d.ts +11 -42
  221. package/lib/trade/priceImpact/skew/converter.js +19 -109
  222. package/lib/{contracts/fetch/priceImpact/skew.d.ts → trade/priceImpact/skew/fetcher.d.ts} +9 -12
  223. package/lib/{contracts/fetch/priceImpact/skew.js → trade/priceImpact/skew/fetcher.js} +23 -22
  224. package/lib/trade/priceImpact/skew/index.d.ts +11 -15
  225. package/lib/trade/priceImpact/skew/index.js +22 -54
  226. package/lib/trade/priceImpact/skew/types.d.ts +2 -15
  227. package/lib/trade/spread.d.ts +5 -18
  228. package/lib/trade/spread.js +17 -106
  229. package/lib/trade/types.d.ts +30 -8
  230. package/lib/trade/types.js +20 -0
  231. package/lib/vault/index.d.ts +3 -1
  232. package/lib/vault/index.js +2 -2
  233. package/package.json +2 -2
  234. package/lib/trade/liquidation.d.ts +0 -42
  235. package/lib/trade/liquidation.js +0 -140
  236. package/lib/trade/pnl.d.ts +0 -10
  237. package/lib/trade/pnl.js +0 -38
  238. /package/lib/{contracts/fetch/fees/fundingFees.js → trade/fees/fundingFees/fetcher.js} +0 -0
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Holding fees calculation for structured contexts
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getTradePendingHoldingFeesCollateralStructured = void 0;
7
+ const types_1 = require("../../../contracts/types");
8
+ const borrowing_1 = require("../borrowing");
9
+ const borrowingV2_1 = require("../borrowingV2");
10
+ const pairContext_1 = require("../fundingFees/pairContext");
11
+ /**
12
+ * @dev Calculates total holding fees using structured context
13
+ * @param trade The trade to calculate fees for
14
+ * @param tradeInfo Trade info containing contracts version
15
+ * @param tradeFeesData Trade fees data containing initial acc fees
16
+ * @param currentPairPrice Current pair price
17
+ * @param context Structured context with sub-contexts for each fee type
18
+ * @returns Object containing all holding fee components
19
+ */
20
+ const getTradePendingHoldingFeesCollateralStructured = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
21
+ const positionSizeCollateral = trade.collateralAmount * trade.leverage;
22
+ // Calculate funding fees (v10+ only)
23
+ let fundingFeeCollateral = 0;
24
+ if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
25
+ context.funding &&
26
+ tradeFeesData.initialAccFundingFeeP !== undefined) {
27
+ fundingFeeCollateral = (0, pairContext_1.getPairTradeFundingFeesCollateral)({
28
+ positionSizeCollateral,
29
+ openPrice: trade.openPrice,
30
+ long: trade.long,
31
+ currentPairPrice,
32
+ initialAccFundingFeeP: tradeFeesData.initialAccFundingFeeP,
33
+ }, context.funding // TODO: Fix types once funding types are properly imported
34
+ );
35
+ }
36
+ // Calculate borrowing fees v2
37
+ let borrowingFeeCollateral = 0;
38
+ if (context.borrowingV2 && tradeFeesData.initialAccBorrowingFeeP !== undefined) {
39
+ borrowingFeeCollateral = (0, borrowingV2_1.getPairTradeBorrowingFeesCollateral)({
40
+ positionSizeCollateral,
41
+ openPrice: trade.openPrice,
42
+ currentPairPrice,
43
+ initialAccBorrowingFeeP: tradeFeesData.initialAccBorrowingFeeP,
44
+ currentTimestamp: context.currentTimestamp,
45
+ }, context.borrowingV2);
46
+ }
47
+ // Calculate v1 borrowing fees (some markets use v1 indefinitely)
48
+ let borrowingFeeCollateral_old = 0;
49
+ if (context.borrowingV1) {
50
+ borrowingFeeCollateral_old = (0, borrowing_1.getPairBorrowingFee)(positionSizeCollateral, trade.long, context.borrowingV1.initialAccFees || { accPairFee: 0, accGroupFee: 0, block: 0 }, {
51
+ currentBlock: context.borrowingV1.currentBlock,
52
+ group: context.borrowingV1.group,
53
+ pair: context.borrowingV1.pair,
54
+ collateralPriceUsd: context.collateralPriceUsd,
55
+ });
56
+ }
57
+ return {
58
+ fundingFeeCollateral,
59
+ borrowingFeeCollateral,
60
+ borrowingFeeCollateral_old,
61
+ totalFeeCollateral: fundingFeeCollateral +
62
+ borrowingFeeCollateral +
63
+ borrowingFeeCollateral_old,
64
+ };
65
+ };
66
+ exports.getTradePendingHoldingFeesCollateralStructured = getTradePendingHoldingFeesCollateralStructured;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @dev Holding fees calculation for structured contexts
3
+ */
4
+ import { Trade, TradeInfo, TradeFeesData } from "../../types";
5
+ import { TradeHoldingFees } from "./types";
6
+ import { ContractsVersion } from "../../../contracts/types";
7
+ import type { BorrowingV1SubContext, BorrowingV2SubContext, FundingFeesSubContext } from "../../pnl";
8
+ /**
9
+ * @dev Context for holding fees calculation with structured sub-contexts
10
+ */
11
+ export type GetStructuredHoldingFeesContext = {
12
+ contractsVersion: ContractsVersion;
13
+ currentTimestamp: number;
14
+ collateralPriceUsd: number;
15
+ borrowingV1?: BorrowingV1SubContext;
16
+ borrowingV2?: BorrowingV2SubContext;
17
+ funding?: FundingFeesSubContext;
18
+ };
19
+ /**
20
+ * @dev Calculates total holding fees using structured context
21
+ * @param trade The trade to calculate fees for
22
+ * @param tradeInfo Trade info containing contracts version
23
+ * @param tradeFeesData Trade fees data containing initial acc fees
24
+ * @param currentPairPrice Current pair price
25
+ * @param context Structured context with sub-contexts for each fee type
26
+ * @returns Object containing all holding fee components
27
+ */
28
+ export declare const getTradePendingHoldingFeesCollateralStructured: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: GetStructuredHoldingFeesContext) => TradeHoldingFees;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Holding fees calculation for structured contexts
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getTradePendingHoldingFeesCollateralStructured = void 0;
7
+ const types_1 = require("../../../contracts/types");
8
+ const borrowing_1 = require("../borrowing");
9
+ const borrowingV2_1 = require("../borrowingV2");
10
+ const fundingFees_1 = require("../fundingFees");
11
+ /**
12
+ * @dev Calculates total holding fees using structured context
13
+ * @param trade The trade to calculate fees for
14
+ * @param tradeInfo Trade info containing contracts version
15
+ * @param tradeFeesData Trade fees data containing initial acc fees
16
+ * @param currentPairPrice Current pair price
17
+ * @param context Structured context with sub-contexts for each fee type
18
+ * @returns Object containing all holding fee components
19
+ */
20
+ const getTradePendingHoldingFeesCollateralStructured = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
21
+ const positionSizeCollateral = trade.collateralAmount * trade.leverage;
22
+ // Calculate funding fees (v10+ only)
23
+ let fundingFeeCollateral = 0;
24
+ if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
25
+ context.funding &&
26
+ tradeFeesData.initialAccFundingFeeP !== undefined) {
27
+ fundingFeeCollateral = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, Object.assign(Object.assign({}, context.funding), { currentTimestamp: context.currentTimestamp }) // TODO: Fix types once funding types are properly imported
28
+ );
29
+ }
30
+ // Calculate borrowing fees v2
31
+ let borrowingFeeCollateral = 0;
32
+ if (context.borrowingV2 &&
33
+ tradeFeesData.initialAccBorrowingFeeP !== undefined) {
34
+ borrowingFeeCollateral = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
35
+ positionSizeCollateral,
36
+ openPrice: trade.openPrice,
37
+ currentPairPrice,
38
+ initialAccBorrowingFeeP: tradeFeesData.initialAccBorrowingFeeP,
39
+ currentTimestamp: context.currentTimestamp,
40
+ }, context.borrowingV2);
41
+ }
42
+ // Calculate v1 borrowing fees (some markets use v1 indefinitely)
43
+ let borrowingFeeCollateral_old = 0;
44
+ if (context.borrowingV1) {
45
+ borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, undefined, // pairIndex not needed for pair-specific context
46
+ trade.long, context.borrowingV1.initialAccFees || {
47
+ accPairFee: 0,
48
+ accGroupFee: 0,
49
+ block: 0,
50
+ }, {
51
+ currentBlock: context.borrowingV1.currentBlock,
52
+ group: context.borrowingV1.group,
53
+ pair: context.borrowingV1.pair,
54
+ collateralPriceUsd: context.collateralPriceUsd,
55
+ });
56
+ }
57
+ return {
58
+ fundingFeeCollateral,
59
+ borrowingFeeCollateral,
60
+ borrowingFeeCollateral_old,
61
+ totalFeeCollateral: fundingFeeCollateral +
62
+ borrowingFeeCollateral +
63
+ borrowingFeeCollateral_old,
64
+ };
65
+ };
66
+ exports.getTradePendingHoldingFeesCollateralStructured = getTradePendingHoldingFeesCollateralStructured;
@@ -1,8 +1,13 @@
1
1
  /**
2
2
  * @dev Trading fee calculations for opening and closing positions
3
3
  */
4
- import { Fee, PairIndex } from "../../types";
5
- import { GetTradeFeesContext, GetLiquidationFeesContext, GetClosingFeeContext, TradeFeesBreakdown } from "./types";
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";
6
11
  /**
7
12
  * @dev Returns the total fee for a trade in collateral tokens
8
13
  * @dev Mirrors the contract's getTotalTradeFeesCollateral function
@@ -30,5 +35,28 @@ export declare const getTotalTradeLiqFeesCollateral: (collateralIndex: number, t
30
35
  * @deprecated Use getTotalTradeFeesCollateral instead
31
36
  */
32
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;
33
60
  export * from "./types";
34
61
  export * from "./converter";
62
+ export * from "./builder";
@@ -17,8 +17,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
17
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.getClosingFee = exports.getTotalTradeLiqFeesCollateral = exports.getTradeFeesCollateral = exports.getTotalTradeFeesCollateral = void 0;
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");
21
24
  const tiers_1 = require("../tiers");
25
+ const types_1 = require("../../../contracts/types");
22
26
  /**
23
27
  * @dev Returns the total fee for a trade in collateral tokens
24
28
  * @dev Mirrors the contract's getTotalTradeFeesCollateral function
@@ -99,6 +103,53 @@ isCounterTrade = false, trader, context) => {
99
103
  trader || "", pairIndex, positionSizeCollateral, isCounterTrade, context);
100
104
  };
101
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, 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;
102
152
  // Export types
103
153
  __exportStar(require("./types"), exports);
104
154
  __exportStar(require("./converter"), exports);
155
+ __exportStar(require("./builder"), exports);
@@ -36,4 +36,13 @@ export type GetLiquidationFeesContext = {
36
36
  * @dev Legacy support
37
37
  */
38
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
+ };
39
48
  export type { GlobalTradeFeeParams };
@@ -1,8 +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
5
  export * from "./types";
6
6
  export * from "./oiWindows";
7
7
  export * from "./priceImpact";
8
8
  export * from "./utils";
9
+ export * from "./effectiveLeverage";
10
+ export * from "./counterTrade";
@@ -15,10 +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
21
  __exportStar(require("./types"), exports);
22
22
  __exportStar(require("./oiWindows"), exports);
23
23
  __exportStar(require("./priceImpact"), exports);
24
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,16 @@
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
+ export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
14
+ export { convertLiquidationParams, convertLiquidationParamsArray, encodeLiquidationParams, } from "./converter";
15
+ export * from "./types";
16
+ export * from "./builder";
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Main export file for liquidation module
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.encodeLiquidationParams = exports.convertLiquidationParamsArray = exports.convertLiquidationParams = exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
21
+ const types_1 = require("../../contracts/types");
22
+ const __1 = require("..");
23
+ /**
24
+ * @dev Calculate liquidation price with structured context
25
+ * @param trade The trade to calculate liquidation price for
26
+ * @param context Structured context with all required data
27
+ * @returns Liquidation price
28
+ */
29
+ const getLiquidationPrice = (trade, context) => {
30
+ var _a, _b;
31
+ // Extract parameters from structured context
32
+ const { currentPairPrice, additionalFeeCollateral = 0, partialCloseMultiplier = 1, beforeOpened = false, isCounterTrade = false, } = context.liquidationSpecific;
33
+ // 1. Calculate closing fees
34
+ const closingFee = (0, __1.getTotalTradeFeesCollateral)(trade.collateralIndex, "", // No fee tiers applied for liquidation calculation
35
+ trade.pairIndex, trade.collateralAmount * trade.leverage, isCounterTrade, {
36
+ fee: context.trading.fee,
37
+ collateralPriceUsd: context.core.collateralPriceUsd,
38
+ globalTradeFeeParams: context.trading.globalTradeFeeParams,
39
+ traderFeeMultiplier: 1,
40
+ counterTradeSettings: context.trading.counterTradeSettings,
41
+ });
42
+ // 2. Calculate holding fees and realized PnL for opened trades
43
+ let holdingFeesTotal = 0;
44
+ let totalRealizedPnlCollateral = 0;
45
+ if (!beforeOpened) {
46
+ // Calculate holding fees
47
+ const holdingFees = (0, __1.getTradePendingHoldingFeesCollateral)(trade, context.tradeData.tradeInfo, context.tradeData.tradeFeesData, currentPairPrice, {
48
+ contractsVersion: context.core.contractsVersion,
49
+ currentTimestamp: context.core.currentTimestamp,
50
+ collateralPriceUsd: context.core.collateralPriceUsd,
51
+ borrowingV1: context.borrowingV1,
52
+ borrowingV2: context.borrowingV2,
53
+ funding: context.funding,
54
+ initialAccFees: context.tradeData.initialAccFees,
55
+ });
56
+ holdingFeesTotal = holdingFees.totalFeeCollateral;
57
+ // Calculate total realized PnL (realized PnL minus realized trading fees)
58
+ totalRealizedPnlCollateral =
59
+ context.tradeData.tradeFeesData.realizedPnlCollateral -
60
+ context.tradeData.tradeFeesData.realizedTradingFeesCollateral;
61
+ }
62
+ // 3. Apply unified formula for all trades
63
+ const totalFeesCollateral = closingFee +
64
+ (holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
65
+ additionalFeeCollateral;
66
+ // 4. Calculate liquidation threshold
67
+ const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage);
68
+ // 5. Calculate liquidation price distance
69
+ const collateralLiqNegativePnl = trade.collateralAmount * liqThresholdP;
70
+ let liqPriceDistance = (trade.openPrice * (collateralLiqNegativePnl - totalFeesCollateral)) /
71
+ trade.collateralAmount /
72
+ trade.leverage;
73
+ // 6. Apply closing spread for v9.2+
74
+ if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
75
+ ((((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
76
+ context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
77
+ (((_b = context.liquidationSpecific.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !==
78
+ undefined &&
79
+ context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
80
+ const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
81
+ liqPriceDistance -= trade.openPrice * closingSpreadP;
82
+ }
83
+ // 7. Calculate final liquidation price
84
+ return trade.long
85
+ ? Math.max(trade.openPrice - liqPriceDistance, 0)
86
+ : Math.max(trade.openPrice + liqPriceDistance, 0);
87
+ };
88
+ exports.getLiquidationPrice = getLiquidationPrice;
89
+ const getLiqPnlThresholdP = (liquidationParams, leverage) => {
90
+ if (liquidationParams === undefined ||
91
+ leverage === undefined ||
92
+ liquidationParams.maxLiqSpreadP === 0 ||
93
+ liquidationParams.startLiqThresholdP === 0 ||
94
+ liquidationParams.endLiqThresholdP === 0 ||
95
+ liquidationParams.startLeverage === 0 ||
96
+ liquidationParams.endLeverage === 0) {
97
+ return 0.9;
98
+ }
99
+ if (leverage < liquidationParams.startLeverage) {
100
+ return liquidationParams.startLiqThresholdP;
101
+ }
102
+ if (leverage > liquidationParams.endLeverage) {
103
+ return liquidationParams.endLiqThresholdP;
104
+ }
105
+ if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
106
+ return liquidationParams.endLiqThresholdP;
107
+ }
108
+ return (liquidationParams.startLiqThresholdP -
109
+ ((leverage - liquidationParams.startLeverage) *
110
+ (liquidationParams.startLiqThresholdP -
111
+ liquidationParams.endLiqThresholdP)) /
112
+ (liquidationParams.endLeverage - liquidationParams.startLeverage));
113
+ };
114
+ exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
115
+ // Converters
116
+ var converter_1 = require("./converter");
117
+ Object.defineProperty(exports, "convertLiquidationParams", { enumerable: true, get: function () { return converter_1.convertLiquidationParams; } });
118
+ Object.defineProperty(exports, "convertLiquidationParamsArray", { enumerable: true, get: function () { return converter_1.convertLiquidationParamsArray; } });
119
+ Object.defineProperty(exports, "encodeLiquidationParams", { enumerable: true, get: function () { return converter_1.encodeLiquidationParams; } });
120
+ // Types
121
+ __exportStar(require("./types"), exports);
122
+ // Builder
123
+ __exportStar(require("./builder"), exports);
@@ -0,0 +1,42 @@
1
+ import { GetBorrowingFeeContext, BorrowingFee } from "./../fees";
2
+ import { LiquidationParams, UserPriceImpact, TradeFeesData, TradeInfo, CounterTradeSettings } from "./../types";
3
+ import { ContractsVersion } from "../../contracts/types";
4
+ import { GetPairBorrowingFeeV2Context } from "../fees/borrowingV2";
5
+ import { GetPairFundingFeeContext } from "../fees/fundingFees";
6
+ import { TradingFeesSubContext } from "../fees/trading/builder";
7
+ /**
8
+ * @dev Structured context for liquidation price calculations
9
+ * @dev Follows the same pattern as GetComprehensivePnlContext
10
+ */
11
+ export type GetLiquidationPriceContext = {
12
+ core: {
13
+ currentBlock: number;
14
+ currentTimestamp: number;
15
+ collateralPriceUsd: number;
16
+ contractsVersion: ContractsVersion;
17
+ spreadP: number;
18
+ };
19
+ borrowingV1?: GetBorrowingFeeContext;
20
+ borrowingV2?: GetPairBorrowingFeeV2Context;
21
+ funding?: GetPairFundingFeeContext;
22
+ trading: TradingFeesSubContext & {
23
+ userPriceImpact?: UserPriceImpact;
24
+ counterTradeSettings?: {
25
+ [pairIndex: number]: CounterTradeSettings;
26
+ };
27
+ };
28
+ tradeData: {
29
+ tradeInfo: TradeInfo;
30
+ tradeFeesData: TradeFeesData;
31
+ liquidationParams: LiquidationParams;
32
+ initialAccFees?: BorrowingFee.InitialAccFees;
33
+ };
34
+ liquidationSpecific: {
35
+ currentPairPrice: number;
36
+ additionalFeeCollateral: number;
37
+ partialCloseMultiplier: number;
38
+ beforeOpened: boolean;
39
+ isCounterTrade: boolean;
40
+ userPriceImpact?: UserPriceImpact;
41
+ };
42
+ };