@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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,16 @@
1
+ import { TradeContainer } from "../types";
2
+ import { GlobalTradingVariablesType } from "src/backend/tradingVariables/types";
3
+ import { GetComprehensivePnlContext } from "./types";
4
+ /**
5
+ * @dev Builds a complete context for comprehensive PnL calculations
6
+ * @dev Uses sub-context builders to create properly scoped contexts
7
+ * @param globalTradingVariables The transformed global trading variables from backend
8
+ * @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
9
+ * @param additionalParams Additional parameters not available in trading variables
10
+ * @returns Complete context ready for getComprehensivePnl
11
+ */
12
+ export declare const buildComprehensivePnlContext: (globalTradingVariables: GlobalTradingVariablesType, tradeContainer: TradeContainer, additionalParams: {
13
+ currentBlock: number;
14
+ currentTimestamp: number;
15
+ traderFeeMultiplier?: number;
16
+ }) => GetComprehensivePnlContext;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildComprehensivePnlContext = void 0;
4
+ const builder_1 = require("../fees/borrowingV2/builder");
5
+ const builder_2 = require("../fees/fundingFees/builder");
6
+ const builder_3 = require("../fees/borrowing/builder");
7
+ const builder_4 = require("../fees/trading/builder");
8
+ /**
9
+ * @dev Builds a complete context for comprehensive PnL calculations
10
+ * @dev Uses sub-context builders to create properly scoped contexts
11
+ * @param globalTradingVariables The transformed global trading variables from backend
12
+ * @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
13
+ * @param additionalParams Additional parameters not available in trading variables
14
+ * @returns Complete context ready for getComprehensivePnl
15
+ */
16
+ const buildComprehensivePnlContext = (globalTradingVariables, tradeContainer, additionalParams) => {
17
+ var _a;
18
+ const { trade, tradeInfo } = tradeContainer;
19
+ const collateralIndex = trade.collateralIndex || 1;
20
+ const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
21
+ return {
22
+ // Core shared context
23
+ core: {
24
+ currentBlock: additionalParams.currentBlock,
25
+ currentTimestamp: additionalParams.currentTimestamp,
26
+ collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
27
+ contractsVersion: tradeInfo.contractsVersion,
28
+ },
29
+ // Build sub-contexts using dedicated builders
30
+ borrowingV1: (0, builder_3.buildBorrowingV1Context)(globalTradingVariables, collateralIndex, additionalParams.currentBlock),
31
+ borrowingV2: (0, builder_1.buildBorrowingV2Context)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
32
+ funding: (0, builder_2.buildFundingContext)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
33
+ trading: (0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier),
34
+ // Trade-specific data
35
+ tradeData: tradeContainer.tradeFeesData && tradeContainer.liquidationParams
36
+ ? {
37
+ tradeFeesData: tradeContainer.tradeFeesData,
38
+ liquidationParams: tradeContainer.liquidationParams,
39
+ initialAccFees: tradeContainer.initialAccFees,
40
+ }
41
+ : undefined,
42
+ };
43
+ };
44
+ exports.buildComprehensivePnlContext = buildComprehensivePnlContext;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @dev Converters for PnL data between contract and SDK formats
3
+ */
4
+ /**
5
+ * @dev Convert PnL percentage from contract precision to SDK format
6
+ * @param pnlPercentContract PnL percentage with 1e10 precision
7
+ * @returns PnL percentage as regular number (e.g., 10 = 10%)
8
+ */
9
+ export declare const convertPnlPercent: (pnlPercentContract: bigint | number) => number;
10
+ /**
11
+ * @dev Convert PnL percentage from SDK format to contract precision
12
+ * @param pnlPercent PnL percentage as regular number
13
+ * @returns PnL percentage with 1e10 precision
14
+ */
15
+ export declare const encodePnlPercent: (pnlPercent: number) => bigint;
16
+ /**
17
+ * @dev Convert collateral amount considering precision
18
+ * @param amount Amount in contract format
19
+ * @param collateralDecimals Collateral token decimals (6 or 18)
20
+ * @returns Amount as SDK float
21
+ */
22
+ export declare const convertCollateralAmount: (amount: bigint | number, collateralDecimals: number) => number;
23
+ /**
24
+ * @dev Convert price from contract format to SDK format
25
+ * @param price Price with 1e10 precision
26
+ * @returns Price as SDK float
27
+ */
28
+ export declare const convertPrice: (price: bigint | number) => number;
29
+ /**
30
+ * @dev Convert leverage from contract format to SDK format
31
+ * @param leverage Leverage with 1e3 precision
32
+ * @returns Leverage as SDK float (e.g., 10 = 10x)
33
+ */
34
+ export declare const convertLeverage: (leverage: bigint | number) => number;
35
+ /**
36
+ * @dev Batch convert PnL results from contract format
37
+ * @param results Array of PnL results from contract
38
+ * @param collateralDecimals Collateral token decimals
39
+ * @returns Array of converted PnL results
40
+ */
41
+ export declare const convertPnlResults: (results: Array<{
42
+ pnlCollateral: bigint;
43
+ pnlPercent: bigint;
44
+ }>, collateralDecimals: number) => Array<{
45
+ pnlCollateral: number;
46
+ pnlPercent: number;
47
+ }>;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Converters for PnL data between contract and SDK formats
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.convertPnlResults = exports.convertLeverage = exports.convertPrice = exports.convertCollateralAmount = exports.encodePnlPercent = exports.convertPnlPercent = void 0;
7
+ /**
8
+ * @dev Convert PnL percentage from contract precision to SDK format
9
+ * @param pnlPercentContract PnL percentage with 1e10 precision
10
+ * @returns PnL percentage as regular number (e.g., 10 = 10%)
11
+ */
12
+ const convertPnlPercent = (pnlPercentContract) => {
13
+ const value = typeof pnlPercentContract === "bigint"
14
+ ? Number(pnlPercentContract)
15
+ : pnlPercentContract;
16
+ // Contract uses 1e10 precision for percentages
17
+ return value / 1e10;
18
+ };
19
+ exports.convertPnlPercent = convertPnlPercent;
20
+ /**
21
+ * @dev Convert PnL percentage from SDK format to contract precision
22
+ * @param pnlPercent PnL percentage as regular number
23
+ * @returns PnL percentage with 1e10 precision
24
+ */
25
+ const encodePnlPercent = (pnlPercent) => {
26
+ return BigInt(Math.round(pnlPercent * 1e10));
27
+ };
28
+ exports.encodePnlPercent = encodePnlPercent;
29
+ /**
30
+ * @dev Convert collateral amount considering precision
31
+ * @param amount Amount in contract format
32
+ * @param collateralDecimals Collateral token decimals (6 or 18)
33
+ * @returns Amount as SDK float
34
+ */
35
+ const convertCollateralAmount = (amount, collateralDecimals) => {
36
+ const value = typeof amount === "bigint" ? Number(amount) : amount;
37
+ return value / Math.pow(10, collateralDecimals);
38
+ };
39
+ exports.convertCollateralAmount = convertCollateralAmount;
40
+ /**
41
+ * @dev Convert price from contract format to SDK format
42
+ * @param price Price with 1e10 precision
43
+ * @returns Price as SDK float
44
+ */
45
+ const convertPrice = (price) => {
46
+ const value = typeof price === "bigint" ? Number(price) : price;
47
+ return value / 1e10;
48
+ };
49
+ exports.convertPrice = convertPrice;
50
+ /**
51
+ * @dev Convert leverage from contract format to SDK format
52
+ * @param leverage Leverage with 1e3 precision
53
+ * @returns Leverage as SDK float (e.g., 10 = 10x)
54
+ */
55
+ const convertLeverage = (leverage) => {
56
+ const value = typeof leverage === "bigint" ? Number(leverage) : leverage;
57
+ return value / 1e3;
58
+ };
59
+ exports.convertLeverage = convertLeverage;
60
+ /**
61
+ * @dev Batch convert PnL results from contract format
62
+ * @param results Array of PnL results from contract
63
+ * @param collateralDecimals Collateral token decimals
64
+ * @returns Array of converted PnL results
65
+ */
66
+ const convertPnlResults = (results, collateralDecimals) => {
67
+ return results.map(result => ({
68
+ pnlCollateral: (0, exports.convertCollateralAmount)(result.pnlCollateral, collateralDecimals),
69
+ pnlPercent: (0, exports.convertPnlPercent)(result.pnlPercent),
70
+ }));
71
+ };
72
+ exports.convertPnlResults = convertPnlResults;
@@ -0,0 +1,88 @@
1
+ /**
2
+ * @dev PnL calculation module
3
+ * @dev Provides functions matching v10 contract implementations
4
+ */
5
+ import { Trade, TradeInfo, LiquidationParams, Fee, GlobalTradeFeeParams, TradeFeesData } from "../types";
6
+ import { ComprehensivePnlResult, GetComprehensivePnlContext } from "./types";
7
+ import { BorrowingFee } from "../fees/borrowing";
8
+ import { ContractsVersion } from "../../contracts/types";
9
+ /**
10
+ * @dev Gets trade realized PnL components from TradeFeesData
11
+ * @dev Mirrors contract's getTradeRealizedPnlCollateral function
12
+ * @param tradeFeesData Trade fees data containing realized components
13
+ * @returns Tuple of [realizedPnlCollateral, realizedTradingFeesCollateral, totalRealizedPnlCollateral]
14
+ */
15
+ export declare const getTradeRealizedPnlCollateral: (tradeFeesData: TradeFeesData) => {
16
+ realizedPnlCollateral: number;
17
+ realizedTradingFeesCollateral: number;
18
+ totalRealizedPnlCollateral: number;
19
+ };
20
+ /**
21
+ * @dev Calculates PnL percentage for a position
22
+ * @dev Mirrors contract's getPnlPercent function
23
+ * @param openPrice Trade open price
24
+ * @param currentPrice Current market price
25
+ * @param long Whether position is long
26
+ * @param leverage Position leverage
27
+ * @returns PnL percentage (e.g., 10 = 10% profit, -50 = 50% loss)
28
+ */
29
+ export declare const getPnlPercent: (openPrice: number, currentPrice: number, long: boolean, leverage: number) => number;
30
+ /**
31
+ * @dev Calculates trade value from collateral and PnL
32
+ * @dev Mirrors contract's getTradeValuePure function
33
+ * @param collateral Trade collateral amount
34
+ * @param pnlPercent PnL percentage
35
+ * @param totalFees Total fees to deduct
36
+ * @returns Trade value after PnL and fees
37
+ */
38
+ export declare const getTradeValue: (collateral: number, pnlPercent: number, totalFees: number) => number;
39
+ /**
40
+ * @dev Comprehensive PnL calculation including all fees
41
+ * @param trade The trade to calculate PnL for
42
+ * @param currentPrice Current market price
43
+ * @param tradeInfo Trade info with version and timestamps
44
+ * @param context Context with all fee parameters
45
+ * @returns Detailed PnL breakdown
46
+ */
47
+ export declare const getComprehensivePnl: (trade: Trade, currentPrice: number, tradeInfo: TradeInfo, context: GetComprehensivePnlContext) => ComprehensivePnlResult;
48
+ /**
49
+ * @dev Legacy getPnl function for backward compatibility
50
+ * @deprecated Use getComprehensivePnl for new implementations
51
+ */
52
+ export type GetPnlContext = {
53
+ currentBlock: number;
54
+ groups: BorrowingFee.Group[];
55
+ pairs: BorrowingFee.Pair[];
56
+ collateralPriceUsd: number | undefined;
57
+ contractsVersion: ContractsVersion | undefined;
58
+ feeMultiplier: number | undefined;
59
+ fee: Fee;
60
+ globalTradeFeeParams: GlobalTradeFeeParams;
61
+ traderFeeMultiplier?: number;
62
+ };
63
+ /**
64
+ * @dev Legacy PnL calculation function
65
+ * @deprecated Use getComprehensivePnl for more comprehensive calculations
66
+ * @param price Current price
67
+ * @param trade Trade object
68
+ * @param tradeInfo Trade info (not used in legacy implementation)
69
+ * @param initialAccFees Initial accumulated fees
70
+ * @param liquidationParams Liquidation parameters
71
+ * @param useFees Whether to include fees
72
+ * @param context Context with fee calculation parameters
73
+ * @returns [pnlCollateral, pnlPercentage] or undefined if no price
74
+ */
75
+ export declare const getPnl: (price: number | undefined, trade: Trade, _tradeInfo: TradeInfo, initialAccFees: BorrowingFee.InitialAccFees, liquidationParams: LiquidationParams, useFees: boolean, context: GetPnlContext) => number[] | undefined;
76
+ /**
77
+ * @dev Calculates the price needed to achieve a target PnL percentage
78
+ * @param targetPnlPercent The target PnL percentage (e.g., 50 for 50% profit, -25 for 25% loss)
79
+ * @param trade The trade to calculate for
80
+ * @param tradeInfo Trade info with timestamps
81
+ * @param context Context with fee calculation parameters
82
+ * @param netPnl Whether to include closing fees in the calculation
83
+ * @returns The price that would result in the target PnL percentage
84
+ */
85
+ export declare const getPriceForTargetPnlPercentage: (targetPnlPercent: number, trade: Trade, tradeInfo: TradeInfo, context: GetComprehensivePnlContext, netPnl?: boolean) => number;
86
+ export * from "./types";
87
+ export * from "./converter";
88
+ export * from "./builder";
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ /**
3
+ * @dev PnL calculation module
4
+ * @dev Provides functions matching v10 contract implementations
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.getPriceForTargetPnlPercentage = exports.getPnl = exports.getComprehensivePnl = exports.getTradeValue = exports.getPnlPercent = exports.getTradeRealizedPnlCollateral = void 0;
22
+ const borrowing_1 = require("../fees/borrowing");
23
+ const trading_1 = require("../fees/trading");
24
+ const liquidation_1 = require("../liquidation");
25
+ /**
26
+ * @dev Gets trade realized PnL components from TradeFeesData
27
+ * @dev Mirrors contract's getTradeRealizedPnlCollateral function
28
+ * @param tradeFeesData Trade fees data containing realized components
29
+ * @returns Tuple of [realizedPnlCollateral, realizedTradingFeesCollateral, totalRealizedPnlCollateral]
30
+ */
31
+ const getTradeRealizedPnlCollateral = (tradeFeesData) => {
32
+ const realizedPnlCollateral = tradeFeesData.realizedPnlCollateral;
33
+ const realizedTradingFeesCollateral = tradeFeesData.realizedTradingFeesCollateral;
34
+ const totalRealizedPnlCollateral = realizedPnlCollateral - realizedTradingFeesCollateral;
35
+ return {
36
+ realizedPnlCollateral,
37
+ realizedTradingFeesCollateral,
38
+ totalRealizedPnlCollateral,
39
+ };
40
+ };
41
+ exports.getTradeRealizedPnlCollateral = getTradeRealizedPnlCollateral;
42
+ /**
43
+ * @dev Calculates PnL percentage for a position
44
+ * @dev Mirrors contract's getPnlPercent function
45
+ * @param openPrice Trade open price
46
+ * @param currentPrice Current market price
47
+ * @param long Whether position is long
48
+ * @param leverage Position leverage
49
+ * @returns PnL percentage (e.g., 10 = 10% profit, -50 = 50% loss)
50
+ */
51
+ const getPnlPercent = (openPrice, currentPrice, long, leverage) => {
52
+ if (openPrice === 0)
53
+ return -100;
54
+ const priceDiff = long ? currentPrice - openPrice : openPrice - currentPrice;
55
+ const pnlPercent = (priceDiff / openPrice) * 100 * leverage;
56
+ // Cap at -100% loss
57
+ return Math.max(pnlPercent, -100);
58
+ };
59
+ exports.getPnlPercent = getPnlPercent;
60
+ /**
61
+ * @dev Calculates trade value from collateral and PnL
62
+ * @dev Mirrors contract's getTradeValuePure function
63
+ * @param collateral Trade collateral amount
64
+ * @param pnlPercent PnL percentage
65
+ * @param totalFees Total fees to deduct
66
+ * @returns Trade value after PnL and fees
67
+ */
68
+ const getTradeValue = (collateral, pnlPercent, totalFees) => {
69
+ const pnlCollateral = collateral * (pnlPercent / 100);
70
+ const value = collateral + pnlCollateral - totalFees;
71
+ return Math.max(0, value);
72
+ };
73
+ exports.getTradeValue = getTradeValue;
74
+ /**
75
+ * @dev Comprehensive PnL calculation including all fees
76
+ * @param trade The trade to calculate PnL for
77
+ * @param currentPrice Current market price
78
+ * @param tradeInfo Trade info with version and timestamps
79
+ * @param context Context with all fee parameters
80
+ * @returns Detailed PnL breakdown
81
+ */
82
+ const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
83
+ var _a;
84
+ // Calculate base PnL percentage
85
+ let pnlPercent = (0, exports.getPnlPercent)(trade.openPrice, currentPrice, trade.long, trade.leverage);
86
+ if (!context.tradeData) {
87
+ throw new Error("Trade data is undefined");
88
+ }
89
+ // Calculate position size
90
+ const positionSizeCollateral = trade.collateralAmount * trade.leverage;
91
+ // Calculate holding fees - always use getTradePendingHoldingFeesCollateral
92
+ // This mirrors the contract's getTradeValueCollateral which always calls this function
93
+ const pendingHoldingFees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeData.tradeFeesData, currentPrice, {
94
+ contractsVersion: context.core.contractsVersion,
95
+ currentTimestamp: context.core.currentTimestamp,
96
+ collateralPriceUsd: context.core.collateralPriceUsd,
97
+ borrowingV1: context.borrowingV1,
98
+ borrowingV2: context.borrowingV2,
99
+ funding: context.funding,
100
+ initialAccFees: context.tradeData.initialAccFees,
101
+ });
102
+ const borrowingFeeV1 = pendingHoldingFees.borrowingFeeCollateral_old;
103
+ const borrowingFeeV2 = pendingHoldingFees.borrowingFeeCollateral;
104
+ const fundingFee = pendingHoldingFees.fundingFeeCollateral;
105
+ // Calculate closing fees
106
+ const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(trade.collateralIndex, trade.user, trade.pairIndex, positionSizeCollateral, trade.isCounterTrade || false, {
107
+ fee: context.trading.fee,
108
+ globalTradeFeeParams: context.trading.globalTradeFeeParams,
109
+ collateralPriceUsd: context.core.collateralPriceUsd,
110
+ traderFeeMultiplier: context.trading.traderFeeMultiplier,
111
+ });
112
+ // Total fees
113
+ const totalHoldingFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee;
114
+ const totalFees = totalHoldingFees + closingFee;
115
+ // Check liquidation
116
+ const liquidationThreshold = ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.liquidationParams)
117
+ ? (0, liquidation_1.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage) *
118
+ -100
119
+ : -90; // Default 90% loss
120
+ const isLiquidated = pnlPercent <= liquidationThreshold;
121
+ // If liquidated, set PnL to -100%
122
+ if (isLiquidated) {
123
+ pnlPercent = -100;
124
+ }
125
+ // Get realized PnL components from TradeFeesData
126
+ const { totalRealizedPnlCollateral } = (0, exports.getTradeRealizedPnlCollateral)(context.tradeData.tradeFeesData);
127
+ // Calculate final trade value
128
+ const tradeValue = (0, exports.getTradeValue)(trade.collateralAmount, pnlPercent, totalFees);
129
+ // Calculate PnL in collateral
130
+ const pnlCollateral = trade.collateralAmount * (pnlPercent / 100);
131
+ // Calculate leveraged position size
132
+ const leveragedPositionSize = trade.collateralAmount * trade.leverage;
133
+ // Calculate unrealized PnL (before closing fee, after holding fees)
134
+ const uPnlCollateral = pnlCollateral - totalHoldingFees + totalRealizedPnlCollateral;
135
+ const uPnlPercent = (uPnlCollateral / trade.collateralAmount) * 100;
136
+ // Realized PnL (after all fees including closing)
137
+ const realizedPnlCollateral = pnlCollateral - totalFees + totalRealizedPnlCollateral;
138
+ const realizedPnlPercent = (realizedPnlCollateral / trade.collateralAmount) * 100;
139
+ return {
140
+ // Core PnL values
141
+ pnlPercent,
142
+ pnlCollateral,
143
+ tradeValue,
144
+ // Unrealized PnL (after holding fees, before closing fee)
145
+ uPnlCollateral,
146
+ uPnlPercent,
147
+ // Realized PnL (after all fees)
148
+ realizedPnlCollateral,
149
+ realizedPnlPercent,
150
+ // Fee breakdown
151
+ fees: {
152
+ borrowingV1: borrowingFeeV1,
153
+ borrowingV2: borrowingFeeV2,
154
+ funding: fundingFee,
155
+ closing: closingFee,
156
+ total: totalFees,
157
+ },
158
+ // Status flags
159
+ isLiquidated,
160
+ isProfitable: pnlPercent > 0,
161
+ // Additional info
162
+ leveragedPositionSize,
163
+ };
164
+ };
165
+ exports.getComprehensivePnl = getComprehensivePnl;
166
+ /**
167
+ * @dev Legacy PnL calculation function
168
+ * @deprecated Use getComprehensivePnl for more comprehensive calculations
169
+ * @param price Current price
170
+ * @param trade Trade object
171
+ * @param tradeInfo Trade info (not used in legacy implementation)
172
+ * @param initialAccFees Initial accumulated fees
173
+ * @param liquidationParams Liquidation parameters
174
+ * @param useFees Whether to include fees
175
+ * @param context Context with fee calculation parameters
176
+ * @returns [pnlCollateral, pnlPercentage] or undefined if no price
177
+ */
178
+ const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
179
+ var _a;
180
+ if (!price) {
181
+ return;
182
+ }
183
+ const posCollat = trade.collateralAmount;
184
+ const { openPrice, leverage } = trade;
185
+ let pnlCollat = trade.long
186
+ ? ((price - openPrice) / openPrice) * leverage * posCollat
187
+ : ((openPrice - price) / openPrice) * leverage * posCollat;
188
+ if (useFees &&
189
+ context.pairs &&
190
+ context.groups &&
191
+ context.currentBlock !== undefined &&
192
+ context.collateralPriceUsd !== undefined) {
193
+ pnlCollat -= (0, borrowing_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, {
194
+ currentBlock: context.currentBlock,
195
+ groups: context.groups,
196
+ pairs: context.pairs,
197
+ collateralPriceUsd: context.collateralPriceUsd,
198
+ });
199
+ }
200
+ let pnlPercentage = (pnlCollat / posCollat) * 100;
201
+ // Can be liquidated
202
+ if (pnlPercentage <=
203
+ (0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
204
+ pnlPercentage = -100;
205
+ }
206
+ else {
207
+ // Calculate closing fee using the same function as opening fees
208
+ const positionSizeCollateral = posCollat * trade.leverage;
209
+ const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(0, // collateralIndex not used
210
+ trade.user, trade.pairIndex, positionSizeCollateral, (_a = trade.isCounterTrade) !== null && _a !== void 0 ? _a : false, {
211
+ fee: context.fee,
212
+ globalTradeFeeParams: context.globalTradeFeeParams,
213
+ collateralPriceUsd: context.collateralPriceUsd || 1,
214
+ traderFeeMultiplier: context.traderFeeMultiplier,
215
+ });
216
+ pnlCollat -= closingFee;
217
+ pnlPercentage = (pnlCollat / posCollat) * 100;
218
+ }
219
+ pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
220
+ pnlCollat = (posCollat * pnlPercentage) / 100;
221
+ return [pnlCollat, pnlPercentage];
222
+ };
223
+ exports.getPnl = getPnl;
224
+ /**
225
+ * @dev Calculates the price needed to achieve a target PnL percentage
226
+ * @param targetPnlPercent The target PnL percentage (e.g., 50 for 50% profit, -25 for 25% loss)
227
+ * @param trade The trade to calculate for
228
+ * @param tradeInfo Trade info with timestamps
229
+ * @param context Context with fee calculation parameters
230
+ * @param netPnl Whether to include closing fees in the calculation
231
+ * @returns The price that would result in the target PnL percentage
232
+ */
233
+ const getPriceForTargetPnlPercentage = (targetPnlPercent, trade, tradeInfo, context, netPnl = false) => {
234
+ var _a, _b;
235
+ const { leverage, openPrice, long, collateralAmount } = trade;
236
+ const positionSizeCollateral = collateralAmount * leverage;
237
+ // Calculate holding fees - always use getTradePendingHoldingFeesCollateral
238
+ // This mirrors the contract's getTradeValueCollateral which always calls this function
239
+ const fees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.tradeFeesData) || {
240
+ realizedTradingFeesCollateral: 0,
241
+ realizedPnlCollateral: 0,
242
+ manuallyRealizedNegativePnlCollateral: 0,
243
+ alreadyTransferredNegativePnlCollateral: 0,
244
+ virtualAvailableCollateralInDiamond: 0,
245
+ initialAccFundingFeeP: 0,
246
+ initialAccBorrowingFeeP: 0,
247
+ }, openPrice, // Use open price as a baseline
248
+ {
249
+ contractsVersion: context.core.contractsVersion,
250
+ currentTimestamp: context.core.currentTimestamp,
251
+ collateralPriceUsd: context.core.collateralPriceUsd,
252
+ borrowingV1: context.borrowingV1,
253
+ borrowingV2: context.borrowingV2,
254
+ funding: context.funding,
255
+ initialAccFees: (_b = context.tradeData) === null || _b === void 0 ? void 0 : _b.initialAccFees,
256
+ });
257
+ const totalHoldingFees = fees.fundingFeeCollateral +
258
+ fees.borrowingFeeCollateral +
259
+ fees.borrowingFeeCollateral_old;
260
+ const targetPnlInCollateral = (collateralAmount * targetPnlPercent) / 100;
261
+ let targetPnlGross = targetPnlInCollateral + totalHoldingFees;
262
+ if (netPnl) {
263
+ // Include closing fees
264
+ const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(trade.collateralIndex, trade.user, trade.pairIndex, positionSizeCollateral, trade.isCounterTrade || false, {
265
+ fee: context.trading.fee,
266
+ globalTradeFeeParams: context.trading.globalTradeFeeParams,
267
+ collateralPriceUsd: context.core.collateralPriceUsd,
268
+ traderFeeMultiplier: context.trading.traderFeeMultiplier,
269
+ });
270
+ targetPnlGross += closingFee;
271
+ }
272
+ // Calculate the price
273
+ let price;
274
+ if (long) {
275
+ price = openPrice + (targetPnlGross * openPrice) / positionSizeCollateral;
276
+ }
277
+ else {
278
+ price = openPrice - (targetPnlGross * openPrice) / positionSizeCollateral;
279
+ }
280
+ return price;
281
+ };
282
+ exports.getPriceForTargetPnlPercentage = getPriceForTargetPnlPercentage;
283
+ // Re-export types
284
+ __exportStar(require("./types"), exports);
285
+ __exportStar(require("./converter"), exports);
286
+ __exportStar(require("./builder"), exports);
@@ -0,0 +1,75 @@
1
+ /**
2
+ * @dev Types for PnL calculations
3
+ */
4
+ import { ContractsVersion } from "src/contracts/types";
5
+ import { BorrowingFee, GetBorrowingFeeContext, TradingFeesSubContext } from "../fees";
6
+ import { GetPairBorrowingFeeV2Context } from "../fees/borrowingV2";
7
+ import { GetPairFundingFeeContext } from "../fees/fundingFees";
8
+ import { TradeFeesData, LiquidationParams } from "../types";
9
+ /**
10
+ * @dev Result of trade value calculation
11
+ */
12
+ export type TradeValueResult = {
13
+ tradeValue: number;
14
+ pnlPercent: number;
15
+ pnlCollateral: number;
16
+ totalFees: number;
17
+ };
18
+ /**
19
+ * @dev Detailed fee breakdown
20
+ */
21
+ export type FeeBreakdown = {
22
+ borrowingV1: number;
23
+ borrowingV2: number;
24
+ funding: number;
25
+ closing: number;
26
+ opening?: number;
27
+ total: number;
28
+ };
29
+ /**
30
+ * @dev Price impact breakdown for v10
31
+ */
32
+ export type PriceImpactBreakdown = {
33
+ fixedSpread: number;
34
+ skewImpact: number;
35
+ cumulVolImpact: number;
36
+ total: number;
37
+ priceAfterImpact: number;
38
+ };
39
+ /**
40
+ * @dev Comprehensive PnL result with all details
41
+ */
42
+ export type ComprehensivePnlResult = {
43
+ pnlPercent: number;
44
+ pnlCollateral: number;
45
+ tradeValue: number;
46
+ uPnlCollateral: number;
47
+ uPnlPercent: number;
48
+ realizedPnlCollateral: number;
49
+ realizedPnlPercent: number;
50
+ fees: FeeBreakdown;
51
+ priceImpact?: PriceImpactBreakdown;
52
+ isLiquidated: boolean;
53
+ isProfitable: boolean;
54
+ leveragedPositionSize: number;
55
+ };
56
+ /**
57
+ * @dev Context for comprehensive PnL calculations with nested sub-contexts
58
+ */
59
+ export type GetComprehensivePnlContext = {
60
+ core: {
61
+ currentBlock: number;
62
+ currentTimestamp: number;
63
+ collateralPriceUsd: number;
64
+ contractsVersion: ContractsVersion;
65
+ };
66
+ borrowingV1?: GetBorrowingFeeContext;
67
+ borrowingV2?: GetPairBorrowingFeeV2Context;
68
+ funding?: GetPairFundingFeeContext;
69
+ trading: TradingFeesSubContext;
70
+ tradeData?: {
71
+ tradeFeesData: TradeFeesData;
72
+ liquidationParams: LiquidationParams;
73
+ initialAccFees?: BorrowingFee.InitialAccFees;
74
+ };
75
+ };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Types for PnL calculations
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,23 @@
1
+ import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
2
+ import { TradeClosingPriceImpactContext } from "./types";
3
+ import { TradeInfo } from "../../types";
4
+ /**
5
+ * @dev Builds a complete context for trade closing price impact calculations
6
+ * @dev Uses sub-context builders to create properly scoped contexts
7
+ * @param globalTradingVariables The transformed global trading variables from backend
8
+ * @param collateralIndex The collateral index (1-based)
9
+ * @param pairIndex The pair index
10
+ * @param tradeInfo Trade information including createdBlock
11
+ * @param additionalParams Additional parameters not available in trading variables
12
+ * @returns Complete context ready for getTradeClosingPriceImpact
13
+ */
14
+ export declare const buildTradeClosingPriceImpactContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, tradeInfo: TradeInfo, additionalParams: {
15
+ currentBlock: number;
16
+ contractsVersion?: number;
17
+ isPnlPositive?: boolean;
18
+ userPriceImpact?: {
19
+ cumulVolPriceImpactMultiplier: number;
20
+ fixedSpreadP: number;
21
+ };
22
+ protectionCloseFactorWhitelist?: boolean;
23
+ }) => TradeClosingPriceImpactContext | undefined;