@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
@@ -1,2 +1,3 @@
1
1
  export * from "./tradingVariables";
2
2
  export * from "./globalTrades";
3
+ export { convertTradeContainer as convertTradeContainerBackend, convertPairOi as convertPairOiBackend, convertOiWindows as convertOiWindowsBackend, convertOiWindowsSettings as convertOiWindowsSettingsBackend, convertTraderFeeTiers as convertTraderFeeTiersBackend, convertTradingPairs as convertTradingPairsBackend, convertCollaterals as convertCollateralsBackend, } from "./tradingVariables/converter";
@@ -14,5 +14,15 @@ 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.convertCollateralsBackend = exports.convertTradingPairsBackend = exports.convertTraderFeeTiersBackend = exports.convertOiWindowsSettingsBackend = exports.convertOiWindowsBackend = exports.convertPairOiBackend = exports.convertTradeContainerBackend = void 0;
17
18
  __exportStar(require("./tradingVariables"), exports);
18
19
  __exportStar(require("./globalTrades"), exports);
20
+ // Re-export backend-specific converters with "Backend" suffix to avoid conflicts
21
+ var converter_1 = require("./tradingVariables/converter");
22
+ Object.defineProperty(exports, "convertTradeContainerBackend", { enumerable: true, get: function () { return converter_1.convertTradeContainer; } });
23
+ Object.defineProperty(exports, "convertPairOiBackend", { enumerable: true, get: function () { return converter_1.convertPairOi; } });
24
+ Object.defineProperty(exports, "convertOiWindowsBackend", { enumerable: true, get: function () { return converter_1.convertOiWindows; } });
25
+ Object.defineProperty(exports, "convertOiWindowsSettingsBackend", { enumerable: true, get: function () { return converter_1.convertOiWindowsSettings; } });
26
+ Object.defineProperty(exports, "convertTraderFeeTiersBackend", { enumerable: true, get: function () { return converter_1.convertTraderFeeTiers; } });
27
+ Object.defineProperty(exports, "convertTradingPairsBackend", { enumerable: true, get: function () { return converter_1.convertTradingPairs; } });
28
+ Object.defineProperty(exports, "convertCollateralsBackend", { enumerable: true, get: function () { return converter_1.convertCollaterals; } });
@@ -1,5 +1,5 @@
1
1
  import { ContestLeaderboardBackendEntry, ContestLeaderboardEntry } from "@gainsnetwork/contests";
2
- import { BorrowingFee, CollateralConfig, Fee, FeeTiers, GlobalTradeFeeParams, OiWindows, OiWindowsSettings, OpenInterest, Pair, PairDepth, PairOi, Trade, TradeContainer, TradeInfo, TradeInitialAccFees, TraderFeeTiers, TradingGroup, PairFactor } from "../../";
2
+ import { BorrowingFee, CollateralConfig, Fee, FeeTiers, GlobalTradeFeeParams, OiWindows, OiWindowsSettings, OpenInterest, Pair, PairDepth, PairOi, Trade, TradeContainer, TradeInfo, TradeInitialAccFees, TraderFeeTiers, TradingGroup, PairFactor, convertLiquidationParams } from "../../";
3
3
  import { BorrowingFeePerBlockCapBackend, CollateralBackend, FeeBackend, FeeTiersBackend, GlobalTradeFeeParamsBackend, OiWindowsBackend, OiWindowsSettingsBackend, OpenInterestBackend, PairBackend, PairDepthBackend, PairFactorBackend, PairOiBackend, PairParamsBorrowingFeesBackend, TradeBackend, TradeContainerBackend, TradeInfoBackend, TradeInitialAccFeesBackend, TraderFeeTiersBackend, TradingGroupBackend } from "./backend.types";
4
4
  import { TradingVariablesCollateral } from "./types";
5
5
  export declare const convertFees: (fees: FeeBackend[]) => Fee[];
@@ -28,3 +28,4 @@ export declare const convertGlobalTradeFeeParams: (fee: GlobalTradeFeeParamsBack
28
28
  export declare const convertMaxLeverages: (maxLeverages: string[]) => number[];
29
29
  export declare const convertFeePerBlockCap: (feeCap: BorrowingFeePerBlockCapBackend | undefined) => BorrowingFee.BorrowingFeePerBlockCap;
30
30
  export declare const convertPairName: (pair?: PairBackend) => string;
31
+ export { convertLiquidationParams };
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertPairName = exports.convertFeePerBlockCap = exports.convertMaxLeverages = exports.convertGlobalTradeFeeParams = exports.convertTraderFeeTiers = exports.convertFeeTiers = exports.convertCollateralConfig = exports.convertOiWindowsSettings = exports.convertOiWindows = exports.convertPairOi = exports.convertContestLeaderboardEntry = exports.generateStockPairToActiveStockSplit = exports.convertTradeInitialAccFees = exports.convertTradeInfo = exports.convertTrade = exports.convertPairFactor = exports.convertTradeContainer = exports.convertTradesAndLimitOrders = exports.convertTradingPairs = exports.convertTradingGroups = exports.convertGroupBorrowingFees = exports.convertPairBorrowingFees = exports.convertPairDepths = exports.convertOpenInterests = exports.convertCollaterals = exports.convertFees = void 0;
3
+ exports.convertLiquidationParams = exports.convertPairName = exports.convertFeePerBlockCap = exports.convertMaxLeverages = exports.convertGlobalTradeFeeParams = exports.convertTraderFeeTiers = exports.convertFeeTiers = exports.convertCollateralConfig = exports.convertOiWindowsSettings = exports.convertOiWindows = exports.convertPairOi = exports.convertContestLeaderboardEntry = exports.generateStockPairToActiveStockSplit = exports.convertTradeInitialAccFees = exports.convertTradeInfo = exports.convertTrade = exports.convertPairFactor = exports.convertTradeContainer = exports.convertTradesAndLimitOrders = exports.convertTradingPairs = exports.convertTradingGroups = exports.convertGroupBorrowingFees = exports.convertPairBorrowingFees = exports.convertPairDepths = exports.convertOpenInterests = exports.convertCollaterals = exports.convertFees = void 0;
4
4
  const __1 = require("../../");
5
+ Object.defineProperty(exports, "convertLiquidationParams", { enumerable: true, get: function () { return __1.convertLiquidationParams; } });
5
6
  const convertFees = (fees) => fees === null || fees === void 0 ? void 0 : fees.map(fee => convertFee(fee));
6
7
  exports.convertFees = convertFees;
7
8
  const convertCollateral = (collateral) => {
@@ -1,3 +1,5 @@
1
1
  import { TransformedGlobalTradingVariables } from "./types";
2
2
  import { GlobalTradingVariablesBackend } from "./backend.types";
3
3
  export declare const transformGlobalTradingVariables: (rawData: GlobalTradingVariablesBackend) => TransformedGlobalTradingVariables;
4
+ export * from "./backend.types";
5
+ export * from "./types";
@@ -1,4 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
17
  exports.transformGlobalTradingVariables = void 0;
4
18
  const converter_1 = require("./converter");
@@ -76,3 +90,6 @@ const getTradingPairs = (pairs, collaterals) => {
76
90
  }
77
91
  return tradingPairs;
78
92
  };
93
+ // Re-export everything from backend.types
94
+ __exportStar(require("./backend.types"), exports);
95
+ __exportStar(require("./types"), exports);
@@ -376,6 +376,16 @@ export declare const pairs: {
376
376
  "SIGN/USD": string;
377
377
  "ZORA/USD": string;
378
378
  "COIN/USD": string;
379
+ "HOOD/USD": string;
380
+ "MSTR/USD": string;
381
+ "NFLX/USD": string;
382
+ "LAUNCHCOIN/USD": string;
383
+ "NXPC/USD": string;
384
+ "SOPH/USD": string;
385
+ "LPT/USD": string;
386
+ "BVIV/USD": string;
387
+ "EVIV/USD": string;
388
+ "CRCL/USD": string;
379
389
  };
380
390
  export declare const syntheticPairs: Set<string>;
381
391
  export declare const parentToSyntheticPairMap: Map<string, string>;
package/lib/constants.js CHANGED
@@ -384,6 +384,16 @@ exports.pairs = {
384
384
  "SIGN/USD": CRYPTO,
385
385
  "ZORA/USD": CRYPTO,
386
386
  "COIN/USD": STOCKS,
387
+ "HOOD/USD": STOCKS,
388
+ "MSTR/USD": STOCKS,
389
+ "NFLX/USD": STOCKS,
390
+ "LAUNCHCOIN/USD": CRYPTO,
391
+ "NXPC/USD": CRYPTO,
392
+ "SOPH/USD": CRYPTO,
393
+ "LPT/USD": CRYPTO,
394
+ "BVIV/USD": CRYPTO,
395
+ "EVIV/USD": CRYPTO,
396
+ "CRCL/USD": STOCKS,
387
397
  };
388
398
  exports.syntheticPairs = new Set([
389
399
  "BTCDEGEN/USD",
@@ -436,7 +446,7 @@ exports.delistedPairIxs = new Set([
436
446
  99, 101, 106, 111, 113, 114, 116, 118, 120, 122, 123, 125, 127, 130, 147, 152,
437
447
  160, 163, 170, 179, 182, 183, 187, 188, 189, 190, 208, 209, 225, 229, 230,
438
448
  231, 238, 239, 241, 250, 253, 254, 258, 270, 275, 276, 278, 279, 282, 285,
439
- 290, 294, 296, 305, 330, 349, 352, 353, 354, 355, 357, 365, 366,
449
+ 290, 294, 296, 305, 330, 349, 352, 353, 354, 355, 357, 365, 366, 384, 385,
440
450
  ]);
441
451
  exports.delistedGroupsIxs = new Set([]);
442
452
  exports.DEFAULT_PROTECTION_CLOSE_FACTOR = 1;
@@ -482,4 +482,14 @@ const PAIR_INDEX_TO_DESCRIPTION = {
482
482
  [types_1.PairIndex.SIGNUSD]: "Sign to US Dollar",
483
483
  [types_1.PairIndex.ZORAUSD]: "ZORA to US Dollar",
484
484
  [types_1.PairIndex.COINUSD]: "Coinbase to US Dollar",
485
+ [types_1.PairIndex.HOODUSD]: "Robinhood Markets to US Dollar",
486
+ [types_1.PairIndex.MSTRUSD]: "MicroStrategy Inc to US Dollar",
487
+ [types_1.PairIndex.NFLXUSD]: "Netflix to US Dollar",
488
+ [types_1.PairIndex.LAUNCHCOINUSD]: "Launch Coin on Believe to US Dollar",
489
+ [types_1.PairIndex.NXPCUSD]: "NEXPACE to US Dollar",
490
+ [types_1.PairIndex.SOPHUSD]: "Sophon to US Dollar",
491
+ [types_1.PairIndex.LPTUSD]: "Livepeer to US Dollar",
492
+ [types_1.PairIndex.BVIVUSD]: "Bitcoin Volmex Implied Volatility 30 Day Index to US Dollar",
493
+ [types_1.PairIndex.EVIVUSD]: "Ethereum Volmex Implied Volatility 30 Day Index to US Dollar",
494
+ [types_1.PairIndex.CRCLUSD]: "Circle Internet Group to US Dollar",
485
495
  };
package/lib/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export * from "./utils";
6
6
  export * from "./vault";
7
7
  export * from "./backend";
8
8
  export * from "./trade/fees/borrowing/index";
9
+ export { ContractsVersion } from "./contracts/types";
package/lib/index.js CHANGED
@@ -14,6 +14,7 @@ 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.ContractsVersion = void 0;
17
18
  __exportStar(require("./trade"), exports);
18
19
  __exportStar(require("./contracts"), exports);
19
20
  __exportStar(require("./markets"), exports);
@@ -23,3 +24,6 @@ __exportStar(require("./vault"), exports);
23
24
  __exportStar(require("./backend"), exports);
24
25
  // Not sure why this is needed, but it is. Barrel imports are not working.
25
26
  __exportStar(require("./trade/fees/borrowing/index"), exports);
27
+ // Export ContractsVersion for frontend usage
28
+ var types_1 = require("./contracts/types");
29
+ Object.defineProperty(exports, "ContractsVersion", { enumerable: true, get: function () { return types_1.ContractsVersion; } });
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @dev Context builder for borrowing v1 fees
3
+ */
4
+ import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
5
+ import { GetBorrowingFeeContext } from "./index";
6
+ /**
7
+ * @dev Builds borrowing v1 context from global trading variables
8
+ * @dev Returns full array-based context required for v1 borrowing fee calculations
9
+ * @param globalTradingVariables The transformed global trading variables from backend
10
+ * @param collateralIndex Collateral index (1-based)
11
+ * @param currentBlock Current block number
12
+ * @returns Full borrowing context with all pairs and groups or undefined if data not available
13
+ */
14
+ export declare const buildBorrowingV1Context: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, currentBlock: number) => GetBorrowingFeeContext | undefined;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Context builder for borrowing v1 fees
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.buildBorrowingV1Context = void 0;
7
+ /**
8
+ * @dev Builds borrowing v1 context from global trading variables
9
+ * @dev Returns full array-based context required for v1 borrowing fee calculations
10
+ * @param globalTradingVariables The transformed global trading variables from backend
11
+ * @param collateralIndex Collateral index (1-based)
12
+ * @param currentBlock Current block number
13
+ * @returns Full borrowing context with all pairs and groups or undefined if data not available
14
+ */
15
+ const buildBorrowingV1Context = (globalTradingVariables, collateralIndex, currentBlock) => {
16
+ var _a;
17
+ const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
18
+ if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairBorrowingFees) || !(collateral === null || collateral === void 0 ? void 0 : collateral.groupBorrowingFees)) {
19
+ return undefined;
20
+ }
21
+ const pairs = collateral.pairBorrowingFees;
22
+ const groups = collateral.groupBorrowingFees;
23
+ if (pairs.length === 0 || groups.length === 0) {
24
+ return undefined;
25
+ }
26
+ return {
27
+ currentBlock,
28
+ pairs,
29
+ groups,
30
+ collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
31
+ };
32
+ };
33
+ exports.buildBorrowingV1Context = buildBorrowingV1Context;
@@ -11,13 +11,13 @@ export type GetBorrowingFeeContext = {
11
11
  * @dev Still actively used by markets that haven't migrated to v2
12
12
  * @dev Uses dynamic collateral OI - converts OI to USD for fee calculations
13
13
  * @param posDai Position size in collateral
14
- * @param pairIndex Trading pair index
14
+ * @param pairIndex Trading pair index (required)
15
15
  * @param long Whether position is long
16
16
  * @param initialAccFees Initial accumulated fees when trade was opened
17
17
  * @param context Context with current block, fee data, and collateral price
18
18
  * @returns Borrowing fee in collateral tokens
19
19
  */
20
- export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
20
+ export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex | undefined, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
21
21
  /**
22
22
  * @deprecated Use withinMaxGroupOiDynamic from @gains-network/sdk/markets/oi instead
23
23
  * @dev This function uses static OI which doesn't reflect current market values
@@ -77,3 +77,4 @@ export declare const borrowingFeeUtils: {
77
77
  };
78
78
  export * as BorrowingFee from "./types";
79
79
  export * from "./converter";
80
+ export * from "./builder";
@@ -32,17 +32,20 @@ exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = ex
32
32
  * @dev Still actively used by markets that haven't migrated to v2
33
33
  * @dev Uses dynamic collateral OI - converts OI to USD for fee calculations
34
34
  * @param posDai Position size in collateral
35
- * @param pairIndex Trading pair index
35
+ * @param pairIndex Trading pair index (required)
36
36
  * @param long Whether position is long
37
37
  * @param initialAccFees Initial accumulated fees when trade was opened
38
38
  * @param context Context with current block, fee data, and collateral price
39
39
  * @returns Borrowing fee in collateral tokens
40
40
  */
41
41
  const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
42
- if (!context.groups || !context.pairs || !context.pairs[pairIndex]) {
42
+ if (pairIndex === undefined) {
43
+ throw new Error("pairIndex is required for borrowing fee calculations");
44
+ }
45
+ const { pairs, groups } = context;
46
+ if (!groups || !pairs || !pairs[pairIndex]) {
43
47
  return 0;
44
48
  }
45
- const { pairs } = context;
46
49
  const pairGroups = pairs[pairIndex].groups;
47
50
  const firstPairGroup = (pairGroups === null || pairGroups === void 0 ? void 0 : pairGroups.length) > 0 ? pairGroups[0] : undefined;
48
51
  let fee = 0;
@@ -60,7 +63,12 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
60
63
  : firstPairGroup.pairAccFeeShort) - initialAccFees.accPairFee;
61
64
  }
62
65
  for (let i = pairGroups.length; i > 0; i--) {
63
- const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long, context);
66
+ const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long, {
67
+ currentBlock: context.currentBlock,
68
+ groups,
69
+ pairs,
70
+ collateralPriceUsd: context.collateralPriceUsd,
71
+ });
64
72
  fee += Math.max(deltaGroup, deltaPair);
65
73
  if (beforeTradeOpen) {
66
74
  break;
@@ -233,3 +241,4 @@ exports.borrowingFeeUtils = {
233
241
  };
234
242
  exports.BorrowingFee = __importStar(require("./types"));
235
243
  __exportStar(require("./converter"), exports);
244
+ __exportStar(require("./builder"), exports);
@@ -0,0 +1,6 @@
1
+ import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
2
+ import { GetPairBorrowingFeeV2Context } from "./types";
3
+ /**
4
+ * @dev Builds borrowing v2 sub-context for a specific pair
5
+ */
6
+ export declare const buildBorrowingV2Context: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, currentTimestamp: number) => GetPairBorrowingFeeV2Context | undefined;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildBorrowingV2Context = void 0;
4
+ /**
5
+ * @dev Builds borrowing v2 sub-context for a specific pair
6
+ */
7
+ const buildBorrowingV2Context = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
8
+ var _a, _b;
9
+ const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
10
+ if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairBorrowingFeesV2)) {
11
+ return undefined;
12
+ }
13
+ const params = (_a = collateral.pairBorrowingFeesV2.params) === null || _a === void 0 ? void 0 : _a[pairIndex];
14
+ const data = (_b = collateral.pairBorrowingFeesV2.data) === null || _b === void 0 ? void 0 : _b[pairIndex];
15
+ if (!params || !data) {
16
+ return undefined;
17
+ }
18
+ return {
19
+ params,
20
+ data,
21
+ currentTimestamp,
22
+ };
23
+ };
24
+ exports.buildBorrowingV2Context = buildBorrowingV2Context;
@@ -38,14 +38,13 @@ export declare const convertTradeInitialAccFees: (contractTradeData: IFundingFee
38
38
  export declare const convertTradeInitialAccFeesArray: (contractTradeDataArray: IFundingFees.TradeFeesDataStructOutput[]) => BorrowingFeeV2.TradeInitialAccFees[];
39
39
  /**
40
40
  * @dev Creates a context object from contract data arrays
41
- * @param collateralIndices Array of collateral indices
42
41
  * @param pairIndices Array of pair indices
43
42
  * @param borrowingParams Array of borrowing fee params from contract
44
43
  * @param borrowingData Array of pair borrowing fee data from contract
45
44
  * @param currentTimestamp Optional current timestamp
46
- * @returns Complete SDK context for borrowing v2 calculations
45
+ * @returns Complete SDK context for borrowing v2 calculations (collateral-scoped)
47
46
  */
48
- export declare const createBorrowingV2Context: (collateralIndices: number[], pairIndices: number[], borrowingParams: IFundingFees.BorrowingFeeParamsStructOutput[], borrowingData: IFundingFees.PairBorrowingFeeDataStructOutput[], currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
47
+ export declare const createBorrowingV2Context: (pairIndices: number[], borrowingParams: IFundingFees.BorrowingFeeParamsStructOutput[], borrowingData: IFundingFees.PairBorrowingFeeDataStructOutput[], currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
49
48
  /**
50
49
  * @dev Helper function to validate borrowing rate per second
51
50
  * @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
@@ -64,3 +63,13 @@ export declare const borrowingRateToAPR: (borrowingRatePerSecondP: number) => nu
64
63
  * @returns Borrowing rate per second (normalized float)
65
64
  */
66
65
  export declare const aprToBorrowingRate: (aprPercentage: number) => number;
66
+ /**
67
+ * @dev Creates a collateral-scoped context from frontend data structure
68
+ * @param collateralBorrowingData Data structure from frontend (params and data arrays)
69
+ * @param currentTimestamp Optional current timestamp
70
+ * @returns Collateral-scoped borrowing fee v2 context
71
+ */
72
+ export declare const createCollateralScopedBorrowingContext: (collateralBorrowingData: {
73
+ params: BorrowingFeeV2.BorrowingFeeParams[];
74
+ data: BorrowingFeeV2.PairBorrowingFeeData[];
75
+ }, currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.aprToBorrowingRate = exports.borrowingRateToAPR = exports.isValidBorrowingRate = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArray = exports.convertTradeInitialAccFees = exports.convertPairBorrowingFeeDataArray = exports.convertPairBorrowingFeeData = exports.convertBorrowingFeeParamsArray = exports.convertBorrowingFeeParams = void 0;
3
+ exports.createCollateralScopedBorrowingContext = exports.aprToBorrowingRate = exports.borrowingRateToAPR = exports.isValidBorrowingRate = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArray = exports.convertTradeInitialAccFees = exports.convertPairBorrowingFeeDataArray = exports.convertPairBorrowingFeeData = exports.convertBorrowingFeeParamsArray = exports.convertBorrowingFeeParams = void 0;
4
4
  const index_1 = require("./index");
5
5
  /**
6
6
  * @dev Converts contract BorrowingFeeParams to SDK type
@@ -56,35 +56,24 @@ const convertTradeInitialAccFeesArray = (contractTradeDataArray) => contractTrad
56
56
  exports.convertTradeInitialAccFeesArray = convertTradeInitialAccFeesArray;
57
57
  /**
58
58
  * @dev Creates a context object from contract data arrays
59
- * @param collateralIndices Array of collateral indices
60
59
  * @param pairIndices Array of pair indices
61
60
  * @param borrowingParams Array of borrowing fee params from contract
62
61
  * @param borrowingData Array of pair borrowing fee data from contract
63
62
  * @param currentTimestamp Optional current timestamp
64
- * @returns Complete SDK context for borrowing v2 calculations
63
+ * @returns Complete SDK context for borrowing v2 calculations (collateral-scoped)
65
64
  */
66
- const createBorrowingV2Context = (collateralIndices, pairIndices, borrowingParams, borrowingData, currentTimestamp) => {
65
+ const createBorrowingV2Context = (pairIndices, borrowingParams, borrowingData, currentTimestamp) => {
67
66
  const context = {
68
67
  currentTimestamp,
69
68
  borrowingParams: {},
70
69
  borrowingData: {},
71
70
  };
72
- // Build nested objects indexed by collateralIndex and pairIndex
73
- for (let i = 0; i < collateralIndices.length; i++) {
74
- const collateralIndex = collateralIndices[i];
71
+ // Build objects indexed by pairIndex
72
+ for (let i = 0; i < pairIndices.length; i++) {
75
73
  const pairIndex = pairIndices[i];
76
- // Initialize collateral index objects if they don't exist
77
- if (!context.borrowingParams[collateralIndex]) {
78
- context.borrowingParams[collateralIndex] = {};
79
- }
80
- if (!context.borrowingData[collateralIndex]) {
81
- context.borrowingData[collateralIndex] = {};
82
- }
83
74
  // Store converted data
84
- context.borrowingParams[collateralIndex][pairIndex] =
85
- (0, exports.convertBorrowingFeeParams)(borrowingParams[i]);
86
- context.borrowingData[collateralIndex][pairIndex] =
87
- (0, exports.convertPairBorrowingFeeData)(borrowingData[i]);
75
+ context.borrowingParams[pairIndex] = (0, exports.convertBorrowingFeeParams)(borrowingParams[i]);
76
+ context.borrowingData[pairIndex] = (0, exports.convertPairBorrowingFeeData)(borrowingData[i]);
88
77
  }
89
78
  return context;
90
79
  };
@@ -119,3 +108,25 @@ const aprToBorrowingRate = (aprPercentage) => {
119
108
  return aprPercentage / SECONDS_PER_YEAR;
120
109
  };
121
110
  exports.aprToBorrowingRate = aprToBorrowingRate;
111
+ /**
112
+ * @dev Creates a collateral-scoped context from frontend data structure
113
+ * @param collateralBorrowingData Data structure from frontend (params and data arrays)
114
+ * @param currentTimestamp Optional current timestamp
115
+ * @returns Collateral-scoped borrowing fee v2 context
116
+ */
117
+ const createCollateralScopedBorrowingContext = (collateralBorrowingData, currentTimestamp) => {
118
+ const context = {
119
+ currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
120
+ borrowingParams: {},
121
+ borrowingData: {},
122
+ };
123
+ // Map arrays to objects indexed by array position (pairIndex)
124
+ collateralBorrowingData.params.forEach((param, index) => {
125
+ context.borrowingParams[index] = param;
126
+ });
127
+ collateralBorrowingData.data.forEach((data, index) => {
128
+ context.borrowingData[index] = data;
129
+ });
130
+ return context;
131
+ };
132
+ exports.createCollateralScopedBorrowingContext = createCollateralScopedBorrowingContext;
@@ -147,20 +147,12 @@ const createBorrowingV2ContextFromArrays = (collateralIndices, pairIndices, para
147
147
  borrowingParams: {},
148
148
  borrowingData: {},
149
149
  };
150
- // Build nested objects indexed by collateralIndex and pairIndex
151
- for (let i = 0; i < collateralIndices.length; i++) {
152
- const collateralIndex = collateralIndices[i];
150
+ // Build objects indexed by pairIndex (collateral-scoped)
151
+ for (let i = 0; i < pairIndices.length; i++) {
153
152
  const pairIndex = pairIndices[i];
154
- // Initialize collateral index objects if they don't exist
155
- if (!context.borrowingParams[collateralIndex]) {
156
- context.borrowingParams[collateralIndex] = {};
157
- }
158
- if (!context.borrowingData[collateralIndex]) {
159
- context.borrowingData[collateralIndex] = {};
160
- }
161
- // Store data
162
- context.borrowingParams[collateralIndex][pairIndex] = params[i];
163
- context.borrowingData[collateralIndex][pairIndex] = data[i];
153
+ // Store data indexed by pairIndex
154
+ context.borrowingParams[pairIndex] = params[i];
155
+ context.borrowingData[pairIndex] = data[i];
164
156
  }
165
157
  return context;
166
158
  };
@@ -22,23 +22,11 @@ export declare const BORROWING_V2_PRECISION: {
22
22
  export declare const getPairPendingAccBorrowingFees: (params: BorrowingFeeV2.BorrowingFeeParams, data: BorrowingFeeV2.PairBorrowingFeeData, currentPairPrice: number, currentTimestamp?: number) => number;
23
23
  /**
24
24
  * @dev Calculates borrowing fees owed by a specific trade
25
- * @param input Trade borrowing fee calculation input
26
- * @param context Context containing borrowing parameters and data
27
- * @returns Borrowing fees in collateral tokens
28
- */
29
- export declare const getTradeBorrowingFeesCollateral: (input: BorrowingFeeV2.TradeBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
30
- /**
31
- * @dev Convenience function to calculate borrowing fees for a trade using individual parameters
32
- * @param positionSizeCollateral Position size in collateral tokens
33
- * @param pairIndex Index of the trading pair
34
- * @param collateralIndex Index of the collateral
35
- * @param openPrice Price at which the trade was opened
36
- * @param currentPairPrice Current price of the trading pair
37
- * @param initialAccBorrowingFeeP Initial accumulated borrowing fee when trade was opened
38
- * @param context Context containing borrowing parameters and data
25
+ * @param input Trade borrowing fee calculation input (without pairIndex)
26
+ * @param context Pair-specific borrowing context
39
27
  * @returns Borrowing fees in collateral tokens
40
28
  */
41
- export declare const getBorrowingFee: (positionSizeCollateral: number, pairIndex: number, collateralIndex: number, openPrice: number, currentPairPrice: number, initialAccBorrowingFeeP: number, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
29
+ export declare const getTradeBorrowingFeesCollateral: (input: Omit<BorrowingFeeV2.TradeBorrowingFeeInput, "pairIndex">, context: BorrowingFeeV2.GetPairBorrowingFeeV2Context) => number;
42
30
  /**
43
31
  * @dev Utility function to get pending accumulated borrowing fees for a pair using context
44
32
  * @param input Pair borrowing fee calculation input
@@ -51,10 +39,10 @@ export declare const getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingF
51
39
  */
52
40
  export declare const borrowingFeeV2Utils: {
53
41
  getPairPendingAccBorrowingFees: (params: BorrowingFeeV2.BorrowingFeeParams, data: BorrowingFeeV2.PairBorrowingFeeData, currentPairPrice: number, currentTimestamp?: number) => number;
54
- getTradeBorrowingFeesCollateral: (input: BorrowingFeeV2.TradeBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
42
+ getTradeBorrowingFeesCollateral: (input: Omit<BorrowingFeeV2.TradeBorrowingFeeInput, "pairIndex">, context: BorrowingFeeV2.GetPairBorrowingFeeV2Context) => number;
55
43
  getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
56
- getBorrowingFee: (positionSizeCollateral: number, pairIndex: number, collateralIndex: number, openPrice: number, currentPairPrice: number, initialAccBorrowingFeeP: number, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
57
44
  };
58
45
  export * as BorrowingFeeV2 from "./types";
46
+ export { GetPairBorrowingFeeV2Context } from "./types";
59
47
  export * from "./converter";
60
48
  export * from "./fetcher";
@@ -26,7 +26,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.BorrowingFeeV2 = exports.borrowingFeeV2Utils = exports.getPairBorrowingFees = exports.getBorrowingFee = exports.getTradeBorrowingFeesCollateral = exports.getPairPendingAccBorrowingFees = exports.BORROWING_V2_PRECISION = exports.MAX_BORROWING_RATE_PER_SECOND = void 0;
29
+ exports.BorrowingFeeV2 = exports.borrowingFeeV2Utils = exports.getPairBorrowingFees = exports.getTradeBorrowingFeesCollateral = exports.getPairPendingAccBorrowingFees = exports.BORROWING_V2_PRECISION = exports.MAX_BORROWING_RATE_PER_SECOND = void 0;
30
30
  /**
31
31
  * @dev Maximum borrowing rate per second (1,000% APR)
32
32
  */
@@ -63,21 +63,18 @@ const getPairPendingAccBorrowingFees = (params, data, currentPairPrice, currentT
63
63
  exports.getPairPendingAccBorrowingFees = getPairPendingAccBorrowingFees;
64
64
  /**
65
65
  * @dev Calculates borrowing fees owed by a specific trade
66
- * @param input Trade borrowing fee calculation input
67
- * @param context Context containing borrowing parameters and data
66
+ * @param input Trade borrowing fee calculation input (without pairIndex)
67
+ * @param context Pair-specific borrowing context
68
68
  * @returns Borrowing fees in collateral tokens
69
69
  */
70
70
  const getTradeBorrowingFeesCollateral = (input, context) => {
71
- var _a, _b;
72
- const { positionSizeCollateral, openPrice, collateralIndex, pairIndex, currentPairPrice, initialAccBorrowingFeeP, currentTimestamp, } = input;
73
- // Get borrowing parameters and data for the pair
74
- const params = (_a = context.borrowingParams[collateralIndex]) === null || _a === void 0 ? void 0 : _a[pairIndex];
75
- const data = (_b = context.borrowingData[collateralIndex]) === null || _b === void 0 ? void 0 : _b[pairIndex];
71
+ const { positionSizeCollateral, openPrice, currentPairPrice, initialAccBorrowingFeeP, currentTimestamp, } = input;
72
+ const { params, data } = context;
76
73
  if (!params || !data) {
77
74
  return 0;
78
75
  }
79
76
  // Calculate current accumulated borrowing fees
80
- const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp);
77
+ const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : context.currentTimestamp);
81
78
  // Calculate borrowing fees for this trade
82
79
  // Formula: (positionSizeCollateral * (currentAccFee - initialAccFee)) / openPrice / 100
83
80
  const feeDeltaP = currentAccBorrowingFeeP - initialAccBorrowingFeeP;
@@ -86,29 +83,6 @@ const getTradeBorrowingFeesCollateral = (input, context) => {
86
83
  exports.BORROWING_V2_PRECISION.PERCENTAGE);
87
84
  };
88
85
  exports.getTradeBorrowingFeesCollateral = getTradeBorrowingFeesCollateral;
89
- /**
90
- * @dev Convenience function to calculate borrowing fees for a trade using individual parameters
91
- * @param positionSizeCollateral Position size in collateral tokens
92
- * @param pairIndex Index of the trading pair
93
- * @param collateralIndex Index of the collateral
94
- * @param openPrice Price at which the trade was opened
95
- * @param currentPairPrice Current price of the trading pair
96
- * @param initialAccBorrowingFeeP Initial accumulated borrowing fee when trade was opened
97
- * @param context Context containing borrowing parameters and data
98
- * @returns Borrowing fees in collateral tokens
99
- */
100
- const getBorrowingFee = (positionSizeCollateral, pairIndex, collateralIndex, openPrice, currentPairPrice, initialAccBorrowingFeeP, context) => {
101
- return (0, exports.getTradeBorrowingFeesCollateral)({
102
- positionSizeCollateral,
103
- openPrice,
104
- collateralIndex,
105
- pairIndex,
106
- currentPairPrice,
107
- initialAccBorrowingFeeP,
108
- currentTimestamp: context.currentTimestamp,
109
- }, context);
110
- };
111
- exports.getBorrowingFee = getBorrowingFee;
112
86
  /**
113
87
  * @dev Utility function to get pending accumulated borrowing fees for a pair using context
114
88
  * @param input Pair borrowing fee calculation input
@@ -116,10 +90,9 @@ exports.getBorrowingFee = getBorrowingFee;
116
90
  * @returns Updated accumulated borrowing fee (1e20 precision)
117
91
  */
118
92
  const getPairBorrowingFees = (input, context) => {
119
- var _a, _b;
120
- const { collateralIndex, pairIndex, currentPairPrice, currentTimestamp } = input;
121
- const params = (_a = context.borrowingParams[collateralIndex]) === null || _a === void 0 ? void 0 : _a[pairIndex];
122
- const data = (_b = context.borrowingData[collateralIndex]) === null || _b === void 0 ? void 0 : _b[pairIndex];
93
+ const { pairIndex, currentPairPrice, currentTimestamp } = input;
94
+ const params = context.borrowingParams[pairIndex];
95
+ const data = context.borrowingData[pairIndex];
123
96
  if (!params || !data) {
124
97
  return 0;
125
98
  }
@@ -133,7 +106,6 @@ exports.borrowingFeeV2Utils = {
133
106
  getPairPendingAccBorrowingFees: exports.getPairPendingAccBorrowingFees,
134
107
  getTradeBorrowingFeesCollateral: exports.getTradeBorrowingFeesCollateral,
135
108
  getPairBorrowingFees: exports.getPairBorrowingFees,
136
- getBorrowingFee: exports.getBorrowingFee,
137
109
  };
138
110
  exports.BorrowingFeeV2 = __importStar(require("./types"));
139
111
  __exportStar(require("./converter"), exports);
@@ -39,6 +39,7 @@ export type TradeInitialAccFees = {
39
39
  };
40
40
  /**
41
41
  * @dev Context required for borrowing v2 fee calculations
42
+ * @dev Context is already scoped to a specific collateral
42
43
  */
43
44
  export type GetBorrowingFeeV2Context = {
44
45
  /**
@@ -47,20 +48,36 @@ export type GetBorrowingFeeV2Context = {
47
48
  currentTimestamp?: number;
48
49
  /**
49
50
  * @dev Borrowing fee parameters for pairs
50
- * @dev Indexed by: params[collateralIndex][pairIndex]
51
+ * @dev Indexed by: params[pairIndex]
51
52
  */
52
- borrowingParams: Record<number, Record<number, BorrowingFeeParams>>;
53
+ borrowingParams: Record<number, BorrowingFeeParams>;
53
54
  /**
54
55
  * @dev Borrowing fee data for pairs
55
- * @dev Indexed by: data[collateralIndex][pairIndex]
56
+ * @dev Indexed by: data[pairIndex]
56
57
  */
57
- borrowingData: Record<number, Record<number, PairBorrowingFeeData>>;
58
+ borrowingData: Record<number, PairBorrowingFeeData>;
59
+ };
60
+ /**
61
+ * @dev Context for pair-specific borrowing v2 fee calculations
62
+ */
63
+ export type GetPairBorrowingFeeV2Context = {
64
+ /**
65
+ * @dev Current timestamp (defaults to Date.now() / 1000)
66
+ */
67
+ currentTimestamp?: number;
68
+ /**
69
+ * @dev Borrowing fee parameters for this specific pair
70
+ */
71
+ params: BorrowingFeeParams;
72
+ /**
73
+ * @dev Borrowing fee data for this specific pair
74
+ */
75
+ data: PairBorrowingFeeData;
58
76
  };
59
77
  /**
60
78
  * @dev Input for calculating pending borrowing fees for a pair
61
79
  */
62
80
  export type PairBorrowingFeeInput = {
63
- collateralIndex: number;
64
81
  pairIndex: number;
65
82
  currentPairPrice: number;
66
83
  currentTimestamp?: number;
@@ -71,7 +88,6 @@ export type PairBorrowingFeeInput = {
71
88
  export type TradeBorrowingFeeInput = {
72
89
  positionSizeCollateral: number;
73
90
  openPrice: number;
74
- collateralIndex: number;
75
91
  pairIndex: number;
76
92
  currentPairPrice: number;
77
93
  initialAccBorrowingFeeP: number;