@gainsnetwork/sdk 0.0.0-v10.rc3 → 0.0.0-v10.rc4

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 (62) hide show
  1. package/lib/backend/index.d.ts +1 -0
  2. package/lib/backend/index.js +10 -0
  3. package/lib/backend/tradingVariables/converter.d.ts +2 -1
  4. package/lib/backend/tradingVariables/converter.js +2 -1
  5. package/lib/backend/tradingVariables/index.d.ts +2 -0
  6. package/lib/backend/tradingVariables/index.js +17 -0
  7. package/lib/constants.d.ts +10 -0
  8. package/lib/constants.js +11 -1
  9. package/lib/contracts/utils/pairs.js +10 -0
  10. package/lib/index.d.ts +1 -0
  11. package/lib/index.js +4 -0
  12. package/lib/trade/fees/borrowing/builder.d.ts +14 -0
  13. package/lib/trade/fees/borrowing/builder.js +33 -0
  14. package/lib/trade/fees/borrowing/index.d.ts +3 -2
  15. package/lib/trade/fees/borrowing/index.js +13 -4
  16. package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
  17. package/lib/trade/fees/borrowingV2/builder.js +24 -0
  18. package/lib/trade/fees/borrowingV2/converter.d.ts +12 -3
  19. package/lib/trade/fees/borrowingV2/converter.js +29 -18
  20. package/lib/trade/fees/borrowingV2/fetcher.js +5 -13
  21. package/lib/trade/fees/borrowingV2/index.d.ts +5 -17
  22. package/lib/trade/fees/borrowingV2/index.js +9 -37
  23. package/lib/trade/fees/borrowingV2/types.d.ts +22 -6
  24. package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
  25. package/lib/trade/fees/fundingFees/builder.js +35 -0
  26. package/lib/trade/fees/fundingFees/index.d.ts +12 -2
  27. package/lib/trade/fees/fundingFees/index.js +23 -3
  28. package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
  29. package/lib/trade/fees/fundingFees/pairContext.js +17 -0
  30. package/lib/trade/fees/index.d.ts +1 -1
  31. package/lib/trade/fees/index.js +2 -3
  32. package/lib/trade/fees/trading/builder.d.ts +18 -0
  33. package/lib/trade/fees/trading/builder.js +20 -0
  34. package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
  35. package/lib/trade/fees/trading/holdingFees.js +66 -0
  36. package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
  37. package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
  38. package/lib/trade/fees/trading/index.d.ts +19 -11
  39. package/lib/trade/fees/trading/index.js +29 -21
  40. package/lib/trade/liquidation/builder.d.ts +25 -0
  41. package/lib/trade/liquidation/builder.js +59 -0
  42. package/lib/trade/liquidation/index.d.ts +8 -13
  43. package/lib/trade/liquidation/index.js +43 -88
  44. package/lib/trade/liquidation/types.d.ts +36 -21
  45. package/lib/trade/pnl/builder.d.ts +16 -0
  46. package/lib/trade/pnl/builder.js +44 -0
  47. package/lib/trade/pnl/index.d.ts +21 -30
  48. package/lib/trade/pnl/index.js +119 -50
  49. package/lib/trade/pnl/types.d.ts +29 -1
  50. package/lib/trade/types.d.ts +11 -1
  51. package/lib/trade/types.js +10 -0
  52. package/package.json +1 -1
  53. package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +0 -75
  54. package/lib/contracts/fetch/fees/borrowingFeesV2.js +0 -193
  55. package/lib/contracts/fetch/fees/fundingFees.d.ts +0 -66
  56. package/lib/contracts/fetch/fees/fundingFees.js +0 -150
  57. package/lib/contracts/fetch/priceImpact/skew.d.ts +0 -63
  58. package/lib/contracts/fetch/priceImpact/skew.js +0 -168
  59. package/lib/trade/liquidation.d.ts +0 -12
  60. package/lib/trade/liquidation.js +0 -55
  61. package/lib/trade/pnl.d.ts +0 -10
  62. package/lib/trade/pnl.js +0 -33
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @dev Builder functions for funding fees context
3
+ */
4
+ import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
5
+ import { GetPairFundingFeeContext } from "./pairContext";
6
+ /**
7
+ * @dev Builds funding fees sub-context for a specific pair
8
+ */
9
+ export declare const buildFundingContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, currentTimestamp: number) => GetPairFundingFeeContext | undefined;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Builder functions for funding fees context
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.buildFundingContext = void 0;
7
+ /**
8
+ * @dev Builds funding fees sub-context for a specific pair
9
+ */
10
+ const buildFundingContext = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
11
+ var _a, _b, _c, _d, _e;
12
+ const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
13
+ if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairFundingFees)) {
14
+ return undefined;
15
+ }
16
+ const params = (_a = collateral.pairFundingFees.params) === null || _a === void 0 ? void 0 : _a[pairIndex];
17
+ const data = (_b = collateral.pairFundingFees.data) === null || _b === void 0 ? void 0 : _b[pairIndex];
18
+ const pairOi = (_c = collateral.pairOis) === null || _c === void 0 ? void 0 : _c[pairIndex];
19
+ if (!params || !data) {
20
+ return undefined;
21
+ }
22
+ return {
23
+ params,
24
+ data,
25
+ pairOi: pairOi
26
+ ? {
27
+ oiLongToken: ((_d = pairOi.token) === null || _d === void 0 ? void 0 : _d.long) || 0,
28
+ oiShortToken: ((_e = pairOi.token) === null || _e === void 0 ? void 0 : _e.short) || 0,
29
+ }
30
+ : undefined,
31
+ currentTimestamp,
32
+ // TODO: Add net exposure when available
33
+ };
34
+ };
35
+ exports.buildFundingContext = buildFundingContext;
@@ -62,7 +62,7 @@ export declare const getPairPendingAccFundingFees: (params: FundingFeeParams, da
62
62
  * @param tradeInfo Trade info (contracts version)
63
63
  * @param tradeFeesData Trade fees data containing initial acc funding fee
64
64
  * @param currentPairPrice Current pair price
65
- * @param context Optional context with funding fee data (if not provided, uses simple calculation)
65
+ * @param context Optional context with funding fee data (full or pair-specific)
66
66
  * @returns Funding fee in collateral tokens
67
67
  */
68
68
  export declare const getTradeFundingFeesCollateral: (trade: {
@@ -92,7 +92,14 @@ export declare const getTradeFundingFeesCollateral: (trade: {
92
92
  [pairIndex: number]: number;
93
93
  };
94
94
  } | undefined;
95
- }) | undefined) => number;
95
+ }) | {
96
+ currentTimestamp: number;
97
+ params: FundingFeeParams;
98
+ data: PairFundingFeeData;
99
+ pairOi?: PairOiAfterV10 | undefined;
100
+ netExposureToken?: number | undefined;
101
+ netExposureUsd?: number | undefined;
102
+ } | undefined) => number;
96
103
  /**
97
104
  * @dev Main function to calculate funding fees for a trade within context
98
105
  * @param context Funding fee context with params and data
@@ -133,4 +140,7 @@ export declare const getTradeFundingFeesCollateralSimple: (trade: {
133
140
  }, initialAccFundingFeeP: number, currentAccFundingFeeP: number) => number;
134
141
  export * as FundingFees from "./types";
135
142
  export type { FundingFeeParams, PairFundingFeeData, PairGlobalParams, TradeInitialAccFundingFees, PairOiAfterV10, FundingRateCalculation, GetFundingFeeContext, TradeFundingFeeResult, PairPendingAccFundingFeesResult, PairAccumulatedFees, TradeInitialAccFees, } from "./types";
143
+ export type { GetPairFundingFeeContext } from "./pairContext";
136
144
  export * from "./fetcher";
145
+ export * from "./pairContext";
146
+ export * from "./builder";
@@ -230,7 +230,7 @@ exports.getPairPendingAccFundingFees = getPairPendingAccFundingFees;
230
230
  * @param tradeInfo Trade info (contracts version)
231
231
  * @param tradeFeesData Trade fees data containing initial acc funding fee
232
232
  * @param currentPairPrice Current pair price
233
- * @param context Optional context with funding fee data (if not provided, uses simple calculation)
233
+ * @param context Optional context with funding fee data (full or pair-specific)
234
234
  * @returns Funding fee in collateral tokens
235
235
  */
236
236
  const getTradeFundingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
@@ -240,8 +240,26 @@ const getTradeFundingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentP
240
240
  return 0;
241
241
  }
242
242
  const positionSizeCollateral = trade.collateralAmount * trade.leverage;
243
- // If we have full context, calculate current accumulated funding fee
244
- if (context &&
243
+ if (!context) {
244
+ return 0; // Cannot calculate without context
245
+ }
246
+ // Check if we have a pair-specific context
247
+ if ("params" in context && "data" in context) {
248
+ // Pair-specific context
249
+ const { params, data, pairOi, netExposureToken, netExposureUsd } = context;
250
+ if (!params.fundingFeesEnabled) {
251
+ return 0;
252
+ }
253
+ // Calculate pending accumulated fees
254
+ const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(params, data, currentPairPrice, pairOi || { oiLongToken: 0, oiShortToken: 0 }, netExposureToken || 0, netExposureUsd || 0, context.currentTimestamp);
255
+ const currentAccFundingFeeP = trade.long
256
+ ? accFundingFeeLongP
257
+ : accFundingFeeShortP;
258
+ const fundingFeeDelta = currentAccFundingFeeP - tradeFeesData.initialAccFundingFeeP;
259
+ return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice;
260
+ }
261
+ // Full context - original logic
262
+ if ("fundingParams" in context &&
245
263
  trade.collateralIndex !== undefined &&
246
264
  trade.pairIndex !== undefined) {
247
265
  const params = (_a = context.fundingParams[trade.collateralIndex]) === null || _a === void 0 ? void 0 : _a[trade.pairIndex];
@@ -324,3 +342,5 @@ exports.getTradeFundingFeesCollateralSimple = getTradeFundingFeesCollateralSimpl
324
342
  // Export namespace for types
325
343
  exports.FundingFees = __importStar(require("./types"));
326
344
  __exportStar(require("./fetcher"), exports);
345
+ __exportStar(require("./pairContext"), exports);
346
+ __exportStar(require("./builder"), exports);
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @dev Pair-specific funding fee types and utilities
3
+ */
4
+ import { FundingFeeParams, PairFundingFeeData, PairGlobalParams, PairOiAfterV10, PairPendingAccFundingFeesResult } from "./types";
5
+ /**
6
+ * @dev Context for pair-specific funding fee calculations
7
+ */
8
+ export type GetPairFundingFeeContext = {
9
+ currentTimestamp: number;
10
+ params: FundingFeeParams;
11
+ data: PairFundingFeeData;
12
+ globalParams?: PairGlobalParams;
13
+ pairOi?: PairOiAfterV10;
14
+ netExposureToken?: number;
15
+ netExposureUsd?: number;
16
+ };
17
+ /**
18
+ * @dev Input for pair-specific trade funding fee calculation
19
+ */
20
+ export type PairTradeFundingFeeInput = {
21
+ positionSizeCollateral: number;
22
+ openPrice: number;
23
+ long: boolean;
24
+ currentPairPrice: number;
25
+ initialAccFundingFeeP: number;
26
+ };
27
+ /**
28
+ * @dev Calculate pending accumulated funding fees for a pair using pair-specific context
29
+ * @param currentPairPrice Current price of the pair
30
+ * @param context Pair-specific funding context
31
+ * @returns Pending accumulated funding fees
32
+ */
33
+ export declare const getPairPendingAccFundingFees: (currentPairPrice: number, context: GetPairFundingFeeContext) => PairPendingAccFundingFeesResult;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Pair-specific funding fee types and utilities
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getPairPendingAccFundingFees = void 0;
7
+ const index_1 = require("./index");
8
+ /**
9
+ * @dev Calculate pending accumulated funding fees for a pair using pair-specific context
10
+ * @param currentPairPrice Current price of the pair
11
+ * @param context Pair-specific funding context
12
+ * @returns Pending accumulated funding fees
13
+ */
14
+ const getPairPendingAccFundingFees = (currentPairPrice, context) => {
15
+ return (0, index_1.getPairPendingAccFundingFees)(context.params, context.data, currentPairPrice, context.pairOi || { oiLongToken: 0, oiShortToken: 0 }, context.netExposureToken || 0, context.netExposureUsd || 0, context.currentTimestamp);
16
+ };
17
+ exports.getPairPendingAccFundingFees = getPairPendingAccFundingFees;
@@ -2,7 +2,7 @@ export * from "./borrowing";
2
2
  export * from "./tiers";
3
3
  export * from "./trading";
4
4
  export { convertTradeFeesData, convertTradeFeesDataArray, convertUiRealizedPnlData, convertUiRealizedPnlDataArray, encodeTradeFeesData, encodeUiRealizedPnlData, } from "./converter";
5
- export { BorrowingFeeV2, borrowingFeeV2Utils, getPairPendingAccBorrowingFees as getPairPendingAccBorrowingFeesV2, getTradeBorrowingFeesCollateral as getTradeBorrowingFeesCollateralV2, getBorrowingFee as getBorrowingFeeV2, getPairBorrowingFees as getPairBorrowingFeesV2, MAX_BORROWING_RATE_PER_SECOND as MAX_BORROWING_RATE_PER_SECOND_V2, BORROWING_V2_PRECISION, } from "./borrowingV2";
5
+ export { BorrowingFeeV2, borrowingFeeV2Utils, getPairPendingAccBorrowingFees as getPairPendingAccBorrowingFeesV2, getTradeBorrowingFeesCollateral as getTradeBorrowingFeesCollateralV2, getPairBorrowingFees as getPairBorrowingFeesV2, MAX_BORROWING_RATE_PER_SECOND as MAX_BORROWING_RATE_PER_SECOND_V2, BORROWING_V2_PRECISION, } from "./borrowingV2";
6
6
  export { convertBorrowingFeeParams as convertBorrowingFeeParamsV2, convertBorrowingFeeParamsArray as convertBorrowingFeeParamsArrayV2, convertPairBorrowingFeeData as convertPairBorrowingFeeDataV2, convertPairBorrowingFeeDataArray as convertPairBorrowingFeeDataArrayV2, convertTradeInitialAccFees as convertTradeInitialAccFeesV2, convertTradeInitialAccFeesArray as convertTradeInitialAccFeesArrayV2, createBorrowingV2Context, isValidBorrowingRate as isValidBorrowingRateV2, borrowingRateToAPR as borrowingRateToAPRV2, aprToBorrowingRate as aprToBorrowingRateV2, } from "./borrowingV2/converter";
7
7
  export { fetchBorrowingFeeParamsV2, fetchPairBorrowingFeeDataV2, fetchTradeBorrowingFeesCollateralV2, fetchPairPendingAccBorrowingFeesV2, fetchAllBorrowingV2Data, createBorrowingV2ContextFromContract, createBorrowingV2ContextFromArrays, fetchBorrowingV2DataForPairs, } from "./borrowingV2/fetcher";
8
8
  export { FundingFees, getCurrentFundingVelocityPerYear, getSecondsToReachZeroRate, getAvgFundingRatePerSecondP, getLongShortAprMultiplier, getPairPendingAccFundingFees, getTradeFundingFeesCollateral, getTradeFundingFeesCollateralSimple, getTradeFundingFees, } from "./fundingFees";
@@ -14,8 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.isValidFundingRate = exports.createFundingFeeContext = exports.convertTradeInitialAccFundingFees = exports.convertPairGlobalParamsArray = exports.convertPairGlobalParams = exports.convertPairFundingFeeDataArray = exports.convertPairFundingFeeData = exports.convertFundingFeeParamsArray = exports.convertFundingFeeParams = exports.getTradeFundingFees = exports.getTradeFundingFeesCollateralSimple = exports.getTradeFundingFeesCollateral = exports.getPairPendingAccFundingFees = exports.getLongShortAprMultiplier = exports.getAvgFundingRatePerSecondP = exports.getSecondsToReachZeroRate = exports.getCurrentFundingVelocityPerYear = exports.FundingFees = exports.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = exports.aprToBorrowingRateV2 = exports.borrowingRateToAPRV2 = exports.isValidBorrowingRateV2 = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArrayV2 = exports.convertTradeInitialAccFeesV2 = exports.convertPairBorrowingFeeDataArrayV2 = exports.convertPairBorrowingFeeDataV2 = exports.convertBorrowingFeeParamsArrayV2 = exports.convertBorrowingFeeParamsV2 = exports.BORROWING_V2_PRECISION = exports.MAX_BORROWING_RATE_PER_SECOND_V2 = exports.getPairBorrowingFeesV2 = exports.getBorrowingFeeV2 = exports.getTradeBorrowingFeesCollateralV2 = exports.getPairPendingAccBorrowingFeesV2 = exports.borrowingFeeV2Utils = exports.BorrowingFeeV2 = exports.encodeUiRealizedPnlData = exports.encodeTradeFeesData = exports.convertUiRealizedPnlDataArray = exports.convertUiRealizedPnlData = exports.convertTradeFeesDataArray = exports.convertTradeFeesData = void 0;
18
- exports.FUNDING_FEES_PRECISION = exports.calculateVelocityFromSkew = exports.aprToFundingRate = exports.fundingRateToAPR = void 0;
17
+ exports.fundingRateToAPR = exports.isValidFundingRate = exports.createFundingFeeContext = exports.convertTradeInitialAccFundingFees = exports.convertPairGlobalParamsArray = exports.convertPairGlobalParams = exports.convertPairFundingFeeDataArray = exports.convertPairFundingFeeData = exports.convertFundingFeeParamsArray = exports.convertFundingFeeParams = exports.getTradeFundingFees = exports.getTradeFundingFeesCollateralSimple = exports.getTradeFundingFeesCollateral = exports.getPairPendingAccFundingFees = exports.getLongShortAprMultiplier = exports.getAvgFundingRatePerSecondP = exports.getSecondsToReachZeroRate = exports.getCurrentFundingVelocityPerYear = exports.FundingFees = exports.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = exports.aprToBorrowingRateV2 = exports.borrowingRateToAPRV2 = exports.isValidBorrowingRateV2 = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArrayV2 = exports.convertTradeInitialAccFeesV2 = exports.convertPairBorrowingFeeDataArrayV2 = exports.convertPairBorrowingFeeDataV2 = exports.convertBorrowingFeeParamsArrayV2 = exports.convertBorrowingFeeParamsV2 = exports.BORROWING_V2_PRECISION = exports.MAX_BORROWING_RATE_PER_SECOND_V2 = exports.getPairBorrowingFeesV2 = exports.getTradeBorrowingFeesCollateralV2 = exports.getPairPendingAccBorrowingFeesV2 = exports.borrowingFeeV2Utils = exports.BorrowingFeeV2 = exports.encodeUiRealizedPnlData = exports.encodeTradeFeesData = exports.convertUiRealizedPnlDataArray = exports.convertUiRealizedPnlData = exports.convertTradeFeesDataArray = exports.convertTradeFeesData = void 0;
18
+ exports.FUNDING_FEES_PRECISION = exports.calculateVelocityFromSkew = exports.aprToFundingRate = void 0;
19
19
  __exportStar(require("./borrowing"), exports);
20
20
  __exportStar(require("./tiers"), exports);
21
21
  __exportStar(require("./trading"), exports);
@@ -33,7 +33,6 @@ Object.defineProperty(exports, "BorrowingFeeV2", { enumerable: true, get: functi
33
33
  Object.defineProperty(exports, "borrowingFeeV2Utils", { enumerable: true, get: function () { return borrowingV2_1.borrowingFeeV2Utils; } });
34
34
  Object.defineProperty(exports, "getPairPendingAccBorrowingFeesV2", { enumerable: true, get: function () { return borrowingV2_1.getPairPendingAccBorrowingFees; } });
35
35
  Object.defineProperty(exports, "getTradeBorrowingFeesCollateralV2", { enumerable: true, get: function () { return borrowingV2_1.getTradeBorrowingFeesCollateral; } });
36
- Object.defineProperty(exports, "getBorrowingFeeV2", { enumerable: true, get: function () { return borrowingV2_1.getBorrowingFee; } });
37
36
  Object.defineProperty(exports, "getPairBorrowingFeesV2", { enumerable: true, get: function () { return borrowingV2_1.getPairBorrowingFees; } });
38
37
  Object.defineProperty(exports, "MAX_BORROWING_RATE_PER_SECOND_V2", { enumerable: true, get: function () { return borrowingV2_1.MAX_BORROWING_RATE_PER_SECOND; } });
39
38
  Object.defineProperty(exports, "BORROWING_V2_PRECISION", { enumerable: true, get: function () { return borrowingV2_1.BORROWING_V2_PRECISION; } });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @dev Trading fees context builder module
3
+ * @dev Provides builder functions for creating trading fee contexts
4
+ */
5
+ import { Fee, GlobalTradeFeeParams } from "../../types";
6
+ import { GlobalTradingVariablesType } from "src/backend/tradingVariables/types";
7
+ /**
8
+ * @dev Sub-context for trading fees
9
+ */
10
+ export type TradingFeesSubContext = {
11
+ fee: Fee;
12
+ globalTradeFeeParams: GlobalTradeFeeParams;
13
+ traderFeeMultiplier?: number;
14
+ };
15
+ /**
16
+ * @dev Builds trading fees sub-context
17
+ */
18
+ export declare const buildTradingFeesContext: (globalTradingVariables: GlobalTradingVariablesType, pairIndex: number, traderFeeMultiplier?: number) => TradingFeesSubContext;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Trading fees context builder module
4
+ * @dev Provides builder functions for creating trading fee contexts
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.buildTradingFeesContext = void 0;
8
+ /**
9
+ * @dev Builds trading fees sub-context
10
+ */
11
+ const buildTradingFeesContext = (globalTradingVariables, pairIndex, traderFeeMultiplier) => {
12
+ const { fees, pairs, globalTradeFeeParams } = globalTradingVariables;
13
+ const feeIndex = pairs[pairIndex].feeIndex;
14
+ return {
15
+ fee: fees[feeIndex],
16
+ globalTradeFeeParams: globalTradeFeeParams,
17
+ traderFeeMultiplier,
18
+ };
19
+ };
20
+ exports.buildTradingFeesContext = buildTradingFeesContext;
@@ -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 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;
@@ -2,9 +2,12 @@
2
2
  * @dev Trading fee calculations for opening and closing positions
3
3
  */
4
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";
5
9
  import { GetTradeFeesContext, GetLiquidationFeesContext, GetClosingFeeContext, TradeFeesBreakdown, TradeHoldingFees } from "./types";
6
10
  import { ContractsVersion } from "../../../contracts/types";
7
- import * as BorrowingFee from "../borrowing/types";
8
11
  /**
9
12
  * @dev Returns the total fee for a trade in collateral tokens
10
13
  * @dev Mirrors the contract's getTotalTradeFeesCollateral function
@@ -32,23 +35,28 @@ export declare const getTotalTradeLiqFeesCollateral: (collateralIndex: number, t
32
35
  * @deprecated Use getTotalTradeFeesCollateral instead
33
36
  */
34
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
+ initialAccFeesV1?: BorrowingFee.InitialAccFees;
49
+ };
35
50
  /**
36
51
  * @dev Calculates total holding fees for a trade (funding + borrowing fees)
37
52
  * @param trade The trade to calculate fees for
38
53
  * @param tradeInfo Trade info containing contracts version
39
54
  * @param tradeFeesData Trade fees data containing initial acc fees
40
55
  * @param currentPairPrice Current pair price
41
- * @param context Context with fee parameters
56
+ * @param context Structured context with sub-contexts for each fee type
42
57
  * @returns Object containing all holding fee components
43
58
  */
44
- export declare const getTradePendingHoldingFeesCollateral: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: {
45
- [key: string]: any;
46
- contractsVersion?: ContractsVersion | undefined;
47
- currentBlock?: number | undefined;
48
- groups?: BorrowingFee.Group[] | undefined;
49
- pairs?: BorrowingFee.Pair[] | undefined;
50
- collateralPriceUsd?: number | undefined;
51
- initialAccFees?: BorrowingFee.InitialAccFees | undefined;
52
- }) => TradeHoldingFees;
59
+ export declare const getTradePendingHoldingFeesCollateral: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: GetStructuredHoldingFeesContext) => TradeHoldingFees;
53
60
  export * from "./types";
54
61
  export * from "./converter";
62
+ export * from "./builder";
@@ -18,10 +18,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.getTradePendingHoldingFeesCollateral = exports.getClosingFee = exports.getTotalTradeLiqFeesCollateral = exports.getTradeFeesCollateral = exports.getTotalTradeFeesCollateral = void 0;
21
- const tiers_1 = require("../tiers");
22
- const fundingFees_1 = require("../fundingFees");
23
- const borrowingV2_1 = require("../borrowingV2");
24
21
  const borrowing_1 = require("../borrowing");
22
+ const borrowingV2_1 = require("../borrowingV2");
23
+ const fundingFees_1 = require("../fundingFees");
24
+ const tiers_1 = require("../tiers");
25
25
  const types_1 = require("../../../contracts/types");
26
26
  /**
27
27
  * @dev Returns the total fee for a trade in collateral tokens
@@ -109,29 +109,36 @@ exports.getClosingFee = getClosingFee;
109
109
  * @param tradeInfo Trade info containing contracts version
110
110
  * @param tradeFeesData Trade fees data containing initial acc fees
111
111
  * @param currentPairPrice Current pair price
112
- * @param context Context with fee parameters
112
+ * @param context Structured context with sub-contexts for each fee type
113
113
  * @returns Object containing all holding fee components
114
114
  */
115
115
  const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
116
- var _a;
116
+ const positionSizeCollateral = trade.collateralAmount * trade.leverage;
117
117
  // Calculate funding fees (v10+ only)
118
- const fundingFeeCollateral = ((_a = context.contractsVersion) !== null && _a !== void 0 ? _a : tradeInfo.contractsVersion) >=
119
- types_1.ContractsVersion.V10
120
- ? (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, context)
121
- : 0;
122
- // Calculate borrowing fees v2
123
- const borrowingFeeCollateral = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
124
- positionSizeCollateral: trade.collateralAmount * trade.leverage,
125
- openPrice: trade.openPrice,
126
- collateralIndex: trade.collateralIndex,
127
- pairIndex: trade.pairIndex,
128
- currentPairPrice,
129
- initialAccBorrowingFeeP: tradeFeesData.initialAccBorrowingFeeP,
130
- currentTimestamp: context.currentTimestamp,
131
- }, context);
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
+ }
132
137
  // Calculate v1 borrowing fees (some markets use v1 indefinitely)
133
- const borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, context.initialAccFees || { accPairFee: 0, accGroupFee: 0, block: 0 }, // Use context initial fees or empty
134
- context);
138
+ let borrowingFeeCollateral_old = 0;
139
+ if (context.borrowingV1 && context.initialAccFeesV1) {
140
+ borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFeesV1, context.borrowingV1);
141
+ }
135
142
  return {
136
143
  fundingFeeCollateral,
137
144
  borrowingFeeCollateral,
@@ -145,3 +152,4 @@ exports.getTradePendingHoldingFeesCollateral = getTradePendingHoldingFeesCollate
145
152
  // Export types
146
153
  __exportStar(require("./types"), exports);
147
154
  __exportStar(require("./converter"), exports);
155
+ __exportStar(require("./builder"), 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;