@gainsnetwork/sdk 1.0.6-rc4 → 1.1.0-rc1

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 (75) hide show
  1. package/lib/backend/globalTrades/index.js +10 -10
  2. package/lib/backend/tradingVariables/backend.types.d.ts +11 -4
  3. package/lib/backend/tradingVariables/converter.d.ts +7 -3
  4. package/lib/backend/tradingVariables/converter.js +70 -63
  5. package/lib/backend/tradingVariables/index.js +11 -7
  6. package/lib/backend/tradingVariables/types.d.ts +4 -2
  7. package/lib/contracts/addresses.js +1 -4
  8. package/lib/contracts/index.d.ts +1 -1
  9. package/lib/contracts/index.js +3 -3
  10. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +627 -305
  11. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +1949 -257
  12. package/lib/contracts/types/generated/factories/GToken__factory.d.ts +0 -7
  13. package/lib/contracts/types/generated/factories/GToken__factory.js +0 -4
  14. package/lib/contracts/utils/borrowingFees.js +20 -9
  15. package/lib/contracts/utils/openTrades.js +20 -11
  16. package/lib/contracts/utils/pairs.d.ts +13 -2
  17. package/lib/contracts/utils/pairs.js +89 -21
  18. package/lib/index.d.ts +1 -0
  19. package/lib/index.js +1 -0
  20. package/lib/markets/forex.js +1 -1
  21. package/lib/markets/leverage/builder.js +2 -2
  22. package/lib/markets/price/index.d.ts +0 -1
  23. package/lib/markets/price/index.js +0 -1
  24. package/lib/markets/price/types.d.ts +0 -27
  25. package/lib/pricing/depthBands/converter.d.ts +65 -0
  26. package/lib/pricing/depthBands/converter.js +155 -0
  27. package/lib/pricing/depthBands/decoder.d.ts +32 -0
  28. package/lib/pricing/depthBands/decoder.js +109 -0
  29. package/lib/pricing/depthBands/encoder.d.ts +19 -0
  30. package/lib/pricing/depthBands/encoder.js +105 -0
  31. package/lib/pricing/depthBands/index.d.ts +8 -0
  32. package/lib/pricing/depthBands/index.js +26 -0
  33. package/lib/pricing/depthBands/types.d.ts +49 -0
  34. package/lib/pricing/depthBands/types.js +10 -0
  35. package/lib/pricing/depthBands/validator.d.ts +22 -0
  36. package/lib/pricing/depthBands/validator.js +113 -0
  37. package/lib/pricing/depthBands.d.ts +39 -0
  38. package/lib/pricing/depthBands.js +92 -0
  39. package/lib/pricing/index.d.ts +4 -0
  40. package/lib/pricing/index.js +20 -0
  41. package/lib/trade/fees/borrowing/builder.js +3 -2
  42. package/lib/trade/fees/borrowing/converter.js +1 -5
  43. package/lib/trade/fees/borrowing/index.js +5 -5
  44. package/lib/trade/fees/borrowingV2/builder.js +4 -3
  45. package/lib/trade/fees/borrowingV2/converter.js +1 -1
  46. package/lib/trade/fees/borrowingV2/fetcher.js +32 -26
  47. package/lib/trade/fees/borrowingV2/index.js +3 -3
  48. package/lib/trade/fees/converter.js +22 -22
  49. package/lib/trade/fees/fundingFees/builder.js +7 -6
  50. package/lib/trade/fees/fundingFees/converter.js +1 -1
  51. package/lib/trade/fees/fundingFees/fetcher.js +25 -16
  52. package/lib/trade/fees/fundingFees/index.js +3 -2
  53. package/lib/trade/fees/tiers/index.js +2 -1
  54. package/lib/trade/fees/trading/index.js +3 -5
  55. package/lib/trade/liquidation/builder.js +3 -6
  56. package/lib/trade/liquidation/index.js +6 -4
  57. package/lib/trade/oiWindows.js +2 -1
  58. package/lib/trade/pnl/builder.js +2 -1
  59. package/lib/trade/pnl/converter.js +1 -1
  60. package/lib/trade/pnl/index.js +7 -4
  61. package/lib/trade/priceImpact/close/builder.js +2 -1
  62. package/lib/trade/priceImpact/close/index.js +1 -4
  63. package/lib/trade/priceImpact/cumulVol/builder.js +11 -18
  64. package/lib/trade/priceImpact/cumulVol/converter.d.ts +63 -0
  65. package/lib/trade/priceImpact/cumulVol/converter.js +97 -1
  66. package/lib/trade/priceImpact/cumulVol/index.d.ts +3 -0
  67. package/lib/trade/priceImpact/cumulVol/index.js +123 -25
  68. package/lib/trade/priceImpact/cumulVol/types.d.ts +11 -0
  69. package/lib/trade/priceImpact/cumulVol/types.js +2 -0
  70. package/lib/trade/priceImpact/open/builder.js +2 -1
  71. package/lib/trade/priceImpact/open/index.js +1 -4
  72. package/lib/trade/priceImpact/skew/builder.js +3 -2
  73. package/lib/trade/priceImpact/skew/converter.js +1 -1
  74. package/lib/trade/priceImpact/skew/fetcher.js +33 -24
  75. package/package.json +2 -2
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @dev Depth bands encoding/decoding functions
3
+ */
4
+ /**
5
+ * Encode depth bands data into two uint256 slots
6
+ * @param totalDepthUsd Total depth in USD (must fit in uint32)
7
+ * @param bandPercentagesBps Array of 30 band percentages in basis points
8
+ * @returns Two slots as bigints
9
+ */
10
+ export declare function encodeDepthBands(totalDepthUsd: number, bandPercentagesBps: number[]): {
11
+ slot1: bigint;
12
+ slot2: bigint;
13
+ };
14
+ /**
15
+ * Decode depth bands from two uint256 slots
16
+ * @param slot1 First slot containing totalDepthUsd and bands 0-13
17
+ * @param slot2 Second slot containing bands 14-29
18
+ * @returns Total depth and array of band percentages
19
+ */
20
+ export declare function decodeDepthBands(slot1: bigint, slot2: bigint): {
21
+ totalDepthUsd: number;
22
+ bands: number[];
23
+ };
24
+ /**
25
+ * Encode depth bands mapping (global offsets for all pairs)
26
+ * @param bands Array of 30 band offset values in ppm
27
+ * @returns Two slots as bigints
28
+ */
29
+ export declare function encodeDepthBandsMapping(bands: number[]): {
30
+ slot1: bigint;
31
+ slot2: bigint;
32
+ };
33
+ /**
34
+ * Decode depth bands mapping from two uint256 slots
35
+ * @param slot1 First slot containing bands 0-13
36
+ * @param slot2 Second slot containing bands 14-29
37
+ * @returns Array of band offset values in ppm
38
+ */
39
+ export declare function decodeDepthBandsMapping(slot1: bigint, slot2: bigint): number[];
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Depth bands encoding/decoding functions
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.decodeDepthBandsMapping = exports.encodeDepthBandsMapping = exports.decodeDepthBands = exports.encodeDepthBands = void 0;
7
+ const DEPTH_BANDS_COUNT = 30;
8
+ const DEPTH_BANDS_PER_SLOT1 = 14;
9
+ /**
10
+ * Encode depth bands data into two uint256 slots
11
+ * @param totalDepthUsd Total depth in USD (must fit in uint32)
12
+ * @param bandPercentagesBps Array of 30 band percentages in basis points
13
+ * @returns Two slots as bigints
14
+ */
15
+ function encodeDepthBands(totalDepthUsd, bandPercentagesBps) {
16
+ // Pack slot1: totalDepthUsd (32 bits) + bands 0-13 (14 * 16 bits)
17
+ let slot1 = BigInt(totalDepthUsd);
18
+ for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
19
+ const shift = 32 + i * 16;
20
+ slot1 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
21
+ }
22
+ // Pack slot2: bands 14-29 (16 * 16 bits)
23
+ let slot2 = BigInt(0);
24
+ for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
25
+ const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
26
+ slot2 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
27
+ }
28
+ return { slot1, slot2 };
29
+ }
30
+ exports.encodeDepthBands = encodeDepthBands;
31
+ /**
32
+ * Decode depth bands from two uint256 slots
33
+ * @param slot1 First slot containing totalDepthUsd and bands 0-13
34
+ * @param slot2 Second slot containing bands 14-29
35
+ * @returns Total depth and array of band percentages
36
+ */
37
+ function decodeDepthBands(slot1, slot2) {
38
+ const totalDepthUsd = Number(slot1 & BigInt(0xffffffff));
39
+ const bands = [];
40
+ // Extract bands 0-13 from slot1
41
+ for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
42
+ const shift = 32 + i * 16;
43
+ bands.push(Number((slot1 >> BigInt(shift)) & BigInt(0xffff)));
44
+ }
45
+ // Extract bands 14-29 from slot2
46
+ for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
47
+ const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
48
+ bands.push(Number((slot2 >> BigInt(shift)) & BigInt(0xffff)));
49
+ }
50
+ return { totalDepthUsd, bands };
51
+ }
52
+ exports.decodeDepthBands = decodeDepthBands;
53
+ /**
54
+ * Encode depth bands mapping (global offsets for all pairs)
55
+ * @param bands Array of 30 band offset values in ppm
56
+ * @returns Two slots as bigints
57
+ */
58
+ function encodeDepthBandsMapping(bands) {
59
+ // Pack slot1: bands 0-13 (14 * 16 bits)
60
+ let slot1 = BigInt(0);
61
+ for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
62
+ slot1 |= BigInt(bands[i]) << BigInt(i * 16);
63
+ }
64
+ // Pack slot2: bands 14-29 (16 * 16 bits)
65
+ let slot2 = BigInt(0);
66
+ for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
67
+ const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
68
+ slot2 |= BigInt(bands[i]) << BigInt(shift);
69
+ }
70
+ return { slot1, slot2 };
71
+ }
72
+ exports.encodeDepthBandsMapping = encodeDepthBandsMapping;
73
+ /**
74
+ * Decode depth bands mapping from two uint256 slots
75
+ * @param slot1 First slot containing bands 0-13
76
+ * @param slot2 Second slot containing bands 14-29
77
+ * @returns Array of band offset values in ppm
78
+ */
79
+ function decodeDepthBandsMapping(slot1, slot2) {
80
+ const bands = [];
81
+ // Extract bands 0-13 from slot1
82
+ for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
83
+ bands.push(Number((slot1 >> BigInt(i * 16)) & BigInt(0xffff)));
84
+ }
85
+ // Extract bands 14-29 from slot2
86
+ for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
87
+ const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
88
+ bands.push(Number((slot2 >> BigInt(shift)) & BigInt(0xffff)));
89
+ }
90
+ return bands;
91
+ }
92
+ exports.decodeDepthBandsMapping = decodeDepthBandsMapping;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @dev Pricing module exports
3
+ */
4
+ export * from "./depthBands";
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ /**
3
+ * @dev Pricing module exports
4
+ */
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ __exportStar(require("./depthBands"), exports);
@@ -13,8 +13,9 @@ exports.buildBorrowingV1Context = void 0;
13
13
  * @returns Full borrowing context with all pairs and groups or undefined if data not available
14
14
  */
15
15
  const buildBorrowingV1Context = (globalTradingVariables, collateralIndex, currentBlock) => {
16
+ var _a;
16
17
  const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
17
- if (!collateral?.pairBorrowingFees || !collateral?.groupBorrowingFees) {
18
+ if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairBorrowingFees) || !(collateral === null || collateral === void 0 ? void 0 : collateral.groupBorrowingFees)) {
18
19
  return undefined;
19
20
  }
20
21
  const pairs = collateral.pairBorrowingFees;
@@ -26,7 +27,7 @@ const buildBorrowingV1Context = (globalTradingVariables, collateralIndex, curren
26
27
  currentBlock,
27
28
  pairs,
28
29
  groups,
29
- collateralPriceUsd: collateral.prices?.collateralPriceUsd || 1,
30
+ collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
30
31
  pairOis: collateral.pairOis,
31
32
  };
32
33
  };
@@ -14,11 +14,7 @@ const convertPairGroupBorrowingFee = (pairGroup) => ({
14
14
  block: pairGroup.block,
15
15
  });
16
16
  exports.convertPairGroupBorrowingFee = convertPairGroupBorrowingFee;
17
- const convertPairBorrowingFee = (pair, pairOi, pairGroup, feeCap) => ({
18
- ...(0, exports.convertGroupBorrowingData)(pair, pairOi),
19
- groups: pairGroup.map(value => (0, exports.convertPairGroupBorrowingFee)(value)),
20
- feePerBlockCap: (0, exports.convertFeePerBlockCap)(feeCap),
21
- });
17
+ const convertPairBorrowingFee = (pair, pairOi, pairGroup, feeCap) => (Object.assign(Object.assign({}, (0, exports.convertGroupBorrowingData)(pair, pairOi)), { groups: pairGroup.map(value => (0, exports.convertPairGroupBorrowingFee)(value)), feePerBlockCap: (0, exports.convertFeePerBlockCap)(feeCap) }));
22
18
  exports.convertPairBorrowingFee = convertPairBorrowingFee;
23
19
  const convertPairBorrowingFees = ([pairs, pairOi, pairGroups, feeCaps]) => pairs.map((value, ix) => (0, exports.convertPairBorrowingFee)(value, pairOi[ix], pairGroups[ix], feeCaps[ix]));
24
20
  exports.convertPairBorrowingFees = convertPairBorrowingFees;
@@ -48,7 +48,7 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, currentPairPri
48
48
  return 0;
49
49
  }
50
50
  const pairGroups = pairs[pairIndex].groups;
51
- const firstPairGroup = pairGroups?.length > 0 ? pairGroups[0] : undefined;
51
+ const firstPairGroup = (pairGroups === null || pairGroups === void 0 ? void 0 : pairGroups.length) > 0 ? pairGroups[0] : undefined;
52
52
  let fee = 0;
53
53
  if (!firstPairGroup || firstPairGroup.block > initialAccFees.block) {
54
54
  const openInterest = (0, __1.getPairTotalOisDynamicCollateral)(pairIndex, {
@@ -215,13 +215,13 @@ collateralPriceUsd) => {
215
215
  };
216
216
  const getPendingAccFeesDelta = (blockDistance, feePerBlock, netOi, maxOi, feeExponent) => {
217
217
  return maxOi > 0 && feeExponent > 0
218
- ? feePerBlock * blockDistance * (netOi / maxOi) ** feeExponent
218
+ ? feePerBlock * blockDistance * Math.pow((netOi / maxOi), feeExponent)
219
219
  : 0;
220
220
  };
221
221
  const getFeePerBlockCaps = (cap) => {
222
222
  return {
223
- minP: cap?.minP || 0,
224
- maxP: cap?.maxP && cap.maxP > 0 ? cap.maxP : 1,
223
+ minP: (cap === null || cap === void 0 ? void 0 : cap.minP) || 0,
224
+ maxP: (cap === null || cap === void 0 ? void 0 : cap.maxP) && cap.maxP > 0 ? cap.maxP : 1,
225
225
  };
226
226
  };
227
227
  const getBorrowingDataActiveFeePerBlock = (val) => {
@@ -231,7 +231,7 @@ const getBorrowingDataActiveFeePerBlock = (val) => {
231
231
  // If minP > 0 then netOi has to be at least minP * maxOi
232
232
  // If maxP > 0 then netOi cannot be more than maxP * maxOi
233
233
  const effectiveOi = Math.min(Math.max(Math.abs(long - short), max * minP), max * maxP);
234
- return val.feePerBlock * (effectiveOi / max) ** val.feeExponent;
234
+ return val.feePerBlock * Math.pow((effectiveOi / max), val.feeExponent);
235
235
  };
236
236
  const getActiveFeePerBlock = (pair, group) => {
237
237
  const pairFeePerBlock = getBorrowingDataActiveFeePerBlock(pair);
@@ -5,12 +5,13 @@ exports.buildBorrowingV2Context = void 0;
5
5
  * @dev Builds borrowing v2 sub-context for a specific pair
6
6
  */
7
7
  const buildBorrowingV2Context = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
8
+ var _a, _b;
8
9
  const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
9
- if (!collateral?.pairBorrowingFeesV2) {
10
+ if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairBorrowingFeesV2)) {
10
11
  return undefined;
11
12
  }
12
- const params = collateral.pairBorrowingFeesV2.params?.[pairIndex];
13
- const data = collateral.pairBorrowingFeesV2.data?.[pairIndex];
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];
14
15
  if (!params || !data) {
15
16
  return undefined;
16
17
  }
@@ -116,7 +116,7 @@ exports.aprToBorrowingRate = aprToBorrowingRate;
116
116
  */
117
117
  const createCollateralScopedBorrowingContext = (collateralBorrowingData, currentTimestamp) => {
118
118
  const context = {
119
- currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
119
+ currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
120
120
  borrowingParams: {},
121
121
  borrowingData: {},
122
122
  };
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = void 0;
4
13
  const converter_1 = require("./converter");
@@ -9,19 +18,19 @@ const converter_1 = require("./converter");
9
18
  * @param pairIndices Array of pair indices
10
19
  * @returns Promise resolving to array of borrowing fee parameters
11
20
  */
12
- const fetchBorrowingFeeParamsV2 = async (contract, collateralIndices, pairIndices) => {
21
+ const fetchBorrowingFeeParamsV2 = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
13
22
  if (collateralIndices.length !== pairIndices.length) {
14
23
  throw new Error("Collateral indices and pair indices arrays must have the same length");
15
24
  }
16
25
  try {
17
- const contractParams = await contract.getPairBorrowingFeeParams(collateralIndices, pairIndices);
26
+ const contractParams = yield contract.getPairBorrowingFeeParams(collateralIndices, pairIndices);
18
27
  return (0, converter_1.convertBorrowingFeeParamsArray)(contractParams);
19
28
  }
20
29
  catch (error) {
21
30
  console.error("Error fetching borrowing fee params v2:", error);
22
31
  throw error;
23
32
  }
24
- };
33
+ });
25
34
  exports.fetchBorrowingFeeParamsV2 = fetchBorrowingFeeParamsV2;
26
35
  /**
27
36
  * @dev Fetches pair borrowing fee data v2 for specific pairs
@@ -30,19 +39,19 @@ exports.fetchBorrowingFeeParamsV2 = fetchBorrowingFeeParamsV2;
30
39
  * @param pairIndices Array of pair indices
31
40
  * @returns Promise resolving to array of pair borrowing fee data
32
41
  */
33
- const fetchPairBorrowingFeeDataV2 = async (contract, collateralIndices, pairIndices) => {
42
+ const fetchPairBorrowingFeeDataV2 = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
34
43
  if (collateralIndices.length !== pairIndices.length) {
35
44
  throw new Error("Collateral indices and pair indices arrays must have the same length");
36
45
  }
37
46
  try {
38
- const contractData = await contract.getPairBorrowingFeeData(collateralIndices, pairIndices);
47
+ const contractData = yield contract.getPairBorrowingFeeData(collateralIndices, pairIndices);
39
48
  return (0, converter_1.convertPairBorrowingFeeDataArray)(contractData);
40
49
  }
41
50
  catch (error) {
42
51
  console.error("Error fetching pair borrowing fee data v2:", error);
43
52
  throw error;
44
53
  }
45
- };
54
+ });
46
55
  exports.fetchPairBorrowingFeeDataV2 = fetchPairBorrowingFeeDataV2;
47
56
  /**
48
57
  * @dev Fetches borrowing fees in collateral tokens for a specific trade
@@ -52,9 +61,9 @@ exports.fetchPairBorrowingFeeDataV2 = fetchPairBorrowingFeeDataV2;
52
61
  * @param currentPairPrice Current price of the trading pair (1e6 precision)
53
62
  * @returns Promise resolving to borrowing fees in collateral tokens
54
63
  */
55
- const fetchTradeBorrowingFeesCollateralV2 = async (contract, trader, index, currentPairPrice) => {
64
+ const fetchTradeBorrowingFeesCollateralV2 = (contract, trader, index, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
56
65
  try {
57
- const feesCollateral = await contract.getTradeBorrowingFeesCollateral(trader, index, currentPairPrice);
66
+ const feesCollateral = yield contract.getTradeBorrowingFeesCollateral(trader, index, currentPairPrice);
58
67
  // Convert BigNumber to normalized float
59
68
  // Note: Collateral precision varies by chain, but contract returns proper precision
60
69
  return parseFloat(feesCollateral.toString());
@@ -63,7 +72,7 @@ const fetchTradeBorrowingFeesCollateralV2 = async (contract, trader, index, curr
63
72
  console.error("Error fetching trade borrowing fees collateral v2:", error);
64
73
  throw error;
65
74
  }
66
- };
75
+ });
67
76
  exports.fetchTradeBorrowingFeesCollateralV2 = fetchTradeBorrowingFeesCollateralV2;
68
77
  /**
69
78
  * @dev Fetches pending accumulated borrowing fees for a specific pair
@@ -73,9 +82,9 @@ exports.fetchTradeBorrowingFeesCollateralV2 = fetchTradeBorrowingFeesCollateralV
73
82
  * @param currentPairPrice Current price of the trading pair (1e6 precision)
74
83
  * @returns Promise resolving to pending accumulated borrowing fee
75
84
  */
76
- const fetchPairPendingAccBorrowingFeesV2 = async (contract, collateralIndex, pairIndex, currentPairPrice) => {
85
+ const fetchPairPendingAccBorrowingFeesV2 = (contract, collateralIndex, pairIndex, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
77
86
  try {
78
- const accBorrowingFeeP = await contract.getPairPendingAccBorrowingFees(collateralIndex, pairIndex, currentPairPrice);
87
+ const accBorrowingFeeP = yield contract.getPairPendingAccBorrowingFees(collateralIndex, pairIndex, currentPairPrice);
79
88
  // Convert BigNumber to normalized float
80
89
  return parseFloat(accBorrowingFeeP.toString()) / 1e20;
81
90
  }
@@ -83,7 +92,7 @@ const fetchPairPendingAccBorrowingFeesV2 = async (contract, collateralIndex, pai
83
92
  console.error("Error fetching pair pending acc borrowing fees v2:", error);
84
93
  throw error;
85
94
  }
86
- };
95
+ });
87
96
  exports.fetchPairPendingAccBorrowingFeesV2 = fetchPairPendingAccBorrowingFeesV2;
88
97
  /**
89
98
  * @dev Convenience function to fetch all borrowing v2 data for specific pairs
@@ -92,11 +101,11 @@ exports.fetchPairPendingAccBorrowingFeesV2 = fetchPairPendingAccBorrowingFeesV2;
92
101
  * @param pairIndices Array of pair indices
93
102
  * @returns Promise resolving to complete borrowing v2 data set
94
103
  */
95
- const fetchAllBorrowingV2Data = async (contract, collateralIndex, pairIndices) => {
104
+ const fetchAllBorrowingV2Data = (contract, collateralIndex, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
96
105
  const collateralIndices = new Array(pairIndices.length).fill(collateralIndex);
97
106
  try {
98
107
  // Fetch both parameters and data in parallel
99
- const [params, data] = await Promise.all([
108
+ const [params, data] = yield Promise.all([
100
109
  (0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
101
110
  (0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
102
111
  ]);
@@ -108,7 +117,7 @@ const fetchAllBorrowingV2Data = async (contract, collateralIndex, pairIndices) =
108
117
  console.error("Error fetching all borrowing v2 data:", error);
109
118
  throw error;
110
119
  }
111
- };
120
+ });
112
121
  exports.fetchAllBorrowingV2Data = fetchAllBorrowingV2Data;
113
122
  /**
114
123
  * @dev Creates a complete borrowing v2 context from contract data
@@ -118,13 +127,10 @@ exports.fetchAllBorrowingV2Data = fetchAllBorrowingV2Data;
118
127
  * @param currentTimestamp Optional current timestamp for calculations
119
128
  * @returns Promise resolving to complete borrowing v2 context
120
129
  */
121
- const createBorrowingV2ContextFromContract = async (contract, collateralIndex, pairIndices, currentTimestamp) => {
122
- const { context } = await (0, exports.fetchAllBorrowingV2Data)(contract, collateralIndex, pairIndices);
123
- return {
124
- ...context,
125
- currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
126
- };
127
- };
130
+ const createBorrowingV2ContextFromContract = (contract, collateralIndex, pairIndices, currentTimestamp) => __awaiter(void 0, void 0, void 0, function* () {
131
+ const { context } = yield (0, exports.fetchAllBorrowingV2Data)(contract, collateralIndex, pairIndices);
132
+ return Object.assign(Object.assign({}, context), { currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000) });
133
+ });
128
134
  exports.createBorrowingV2ContextFromContract = createBorrowingV2ContextFromContract;
129
135
  /**
130
136
  * @dev Helper function to create context from already fetched arrays
@@ -137,7 +143,7 @@ exports.createBorrowingV2ContextFromContract = createBorrowingV2ContextFromContr
137
143
  */
138
144
  const createBorrowingV2ContextFromArrays = (collateralIndices, pairIndices, params, data, currentTimestamp) => {
139
145
  const context = {
140
- currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
146
+ currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
141
147
  borrowingParams: {},
142
148
  borrowingData: {},
143
149
  };
@@ -158,13 +164,13 @@ exports.createBorrowingV2ContextFromArrays = createBorrowingV2ContextFromArrays;
158
164
  * @param pairIndices Array of pair indices (must match collateralIndices length)
159
165
  * @returns Promise resolving to complete borrowing v2 context
160
166
  */
161
- const fetchBorrowingV2DataForPairs = async (contract, collateralIndices, pairIndices) => {
167
+ const fetchBorrowingV2DataForPairs = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
162
168
  if (collateralIndices.length !== pairIndices.length) {
163
169
  throw new Error("Collateral indices and pair indices arrays must have the same length");
164
170
  }
165
171
  try {
166
172
  // Fetch both parameters and data in parallel
167
- const [params, data] = await Promise.all([
173
+ const [params, data] = yield Promise.all([
168
174
  (0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
169
175
  (0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
170
176
  ]);
@@ -175,5 +181,5 @@ const fetchBorrowingV2DataForPairs = async (contract, collateralIndices, pairInd
175
181
  console.error("Error fetching borrowing v2 data for pairs:", error);
176
182
  throw error;
177
183
  }
178
- };
184
+ });
179
185
  exports.fetchBorrowingV2DataForPairs = fetchBorrowingV2DataForPairs;
@@ -48,7 +48,7 @@ exports.BORROWING_V2_PRECISION = {
48
48
  * @returns Updated accumulated borrowing fee (1e20 precision)
49
49
  */
50
50
  const getPairPendingAccBorrowingFees = (params, data, currentPairPrice, currentTimestamp) => {
51
- const timestamp = currentTimestamp ?? Math.floor(Date.now() / 1000);
51
+ const timestamp = currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000);
52
52
  // Calculate time elapsed since last update
53
53
  const timeElapsed = Math.max(0, timestamp - data.lastBorrowingUpdateTs);
54
54
  // If no time elapsed, return current accumulated fee
@@ -74,7 +74,7 @@ const getTradeBorrowingFeesCollateral = (input, context) => {
74
74
  return 0;
75
75
  }
76
76
  // Calculate current accumulated borrowing fees
77
- const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp ?? context.currentTimestamp);
77
+ const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : context.currentTimestamp);
78
78
  // Calculate borrowing fees for this trade
79
79
  // Formula: (positionSizeCollateral * (currentAccFee - initialAccFee)) / openPrice / 100
80
80
  const feeDeltaP = currentAccBorrowingFeeP - initialAccBorrowingFeeP;
@@ -96,7 +96,7 @@ const getPairBorrowingFees = (input, context) => {
96
96
  if (!params || !data) {
97
97
  return 0;
98
98
  }
99
- return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp ?? context.currentTimestamp);
99
+ return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : context.currentTimestamp);
100
100
  };
101
101
  exports.getPairBorrowingFees = getPairBorrowingFees;
102
102
  /**
@@ -16,14 +16,14 @@ const convertTradeFeesData = (data, collateralConfig) => {
16
16
  const decimals = collateralConfig.decimals || 18;
17
17
  return {
18
18
  realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
19
- 10 ** decimals,
20
- realizedPnlCollateral: parseFloat(data.realizedPnlCollateral.toString()) / 10 ** decimals,
19
+ Math.pow(10, decimals),
20
+ realizedPnlCollateral: parseFloat(data.realizedPnlCollateral.toString()) / Math.pow(10, decimals),
21
21
  manuallyRealizedNegativePnlCollateral: parseFloat(data.manuallyRealizedNegativePnlCollateral.toString()) /
22
- 10 ** decimals,
22
+ Math.pow(10, decimals),
23
23
  alreadyTransferredNegativePnlCollateral: parseFloat(data.alreadyTransferredNegativePnlCollateral.toString()) /
24
- 10 ** decimals,
24
+ Math.pow(10, decimals),
25
25
  virtualAvailableCollateralInDiamond: parseFloat(data.virtualAvailableCollateralInDiamond.toString()) /
26
- 10 ** decimals,
26
+ Math.pow(10, decimals),
27
27
  initialAccFundingFeeP: parseFloat(data.initialAccFundingFeeP.toString()) /
28
28
  converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P,
29
29
  initialAccBorrowingFeeP: parseFloat(data.initialAccBorrowingFeeP.toString()) /
@@ -51,16 +51,16 @@ const convertUiRealizedPnlData = (data, collateralConfig) => {
51
51
  const decimals = collateralConfig.decimals || 18;
52
52
  return {
53
53
  realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
54
- 10 ** decimals,
54
+ Math.pow(10, decimals),
55
55
  realizedOldBorrowingFeesCollateral: parseFloat(data.realizedOldBorrowingFeesCollateral.toString()) /
56
- 10 ** decimals,
56
+ Math.pow(10, decimals),
57
57
  realizedNewBorrowingFeesCollateral: parseFloat(data.realizedNewBorrowingFeesCollateral.toString()) /
58
- 10 ** decimals,
58
+ Math.pow(10, decimals),
59
59
  realizedFundingFeesCollateral: parseFloat(data.realizedFundingFeesCollateral.toString()) /
60
- 10 ** decimals,
60
+ Math.pow(10, decimals),
61
61
  realizedPnlPartialCloseCollateral: parseFloat(data.realizedPnlPartialCloseCollateral.toString()) /
62
- 10 ** decimals,
63
- pnlWithdrawnCollateral: parseFloat(data.pnlWithdrawnCollateral.toString()) / 10 ** decimals,
62
+ Math.pow(10, decimals),
63
+ pnlWithdrawnCollateral: parseFloat(data.pnlWithdrawnCollateral.toString()) / Math.pow(10, decimals),
64
64
  };
65
65
  };
66
66
  exports.convertUiRealizedPnlData = convertUiRealizedPnlData;
@@ -83,11 +83,11 @@ exports.convertUiRealizedPnlDataArray = convertUiRealizedPnlDataArray;
83
83
  const encodeTradeFeesData = (data, collateralConfig) => {
84
84
  const decimals = collateralConfig.decimals || 18;
85
85
  return {
86
- realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * 10 ** decimals),
87
- realizedPnlCollateral: Math.round(data.realizedPnlCollateral * 10 ** decimals),
88
- manuallyRealizedNegativePnlCollateral: Math.round(data.manuallyRealizedNegativePnlCollateral * 10 ** decimals),
89
- alreadyTransferredNegativePnlCollateral: Math.round(data.alreadyTransferredNegativePnlCollateral * 10 ** decimals),
90
- virtualAvailableCollateralInDiamond: Math.round(data.virtualAvailableCollateralInDiamond * 10 ** decimals),
86
+ realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * Math.pow(10, decimals)),
87
+ realizedPnlCollateral: Math.round(data.realizedPnlCollateral * Math.pow(10, decimals)),
88
+ manuallyRealizedNegativePnlCollateral: Math.round(data.manuallyRealizedNegativePnlCollateral * Math.pow(10, decimals)),
89
+ alreadyTransferredNegativePnlCollateral: Math.round(data.alreadyTransferredNegativePnlCollateral * Math.pow(10, decimals)),
90
+ virtualAvailableCollateralInDiamond: Math.round(data.virtualAvailableCollateralInDiamond * Math.pow(10, decimals)),
91
91
  __placeholder: 0,
92
92
  initialAccFundingFeeP: Math.round(data.initialAccFundingFeeP * converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P),
93
93
  initialAccBorrowingFeeP: Math.round(data.initialAccBorrowingFeeP * borrowingV2_1.BORROWING_V2_PRECISION.ACC_FEE),
@@ -103,12 +103,12 @@ exports.encodeTradeFeesData = encodeTradeFeesData;
103
103
  const encodeUiRealizedPnlData = (data, collateralConfig) => {
104
104
  const decimals = collateralConfig.decimals || 18;
105
105
  return {
106
- realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * 10 ** decimals),
107
- realizedOldBorrowingFeesCollateral: Math.round(data.realizedOldBorrowingFeesCollateral * 10 ** decimals),
108
- realizedNewBorrowingFeesCollateral: Math.round(data.realizedNewBorrowingFeesCollateral * 10 ** decimals),
109
- realizedFundingFeesCollateral: Math.round(data.realizedFundingFeesCollateral * 10 ** decimals),
110
- realizedPnlPartialCloseCollateral: Math.round(data.realizedPnlPartialCloseCollateral * 10 ** decimals),
111
- pnlWithdrawnCollateral: Math.round(data.pnlWithdrawnCollateral * 10 ** decimals),
106
+ realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * Math.pow(10, decimals)),
107
+ realizedOldBorrowingFeesCollateral: Math.round(data.realizedOldBorrowingFeesCollateral * Math.pow(10, decimals)),
108
+ realizedNewBorrowingFeesCollateral: Math.round(data.realizedNewBorrowingFeesCollateral * Math.pow(10, decimals)),
109
+ realizedFundingFeesCollateral: Math.round(data.realizedFundingFeesCollateral * Math.pow(10, decimals)),
110
+ realizedPnlPartialCloseCollateral: Math.round(data.realizedPnlPartialCloseCollateral * Math.pow(10, decimals)),
111
+ pnlWithdrawnCollateral: Math.round(data.pnlWithdrawnCollateral * Math.pow(10, decimals)),
112
112
  };
113
113
  };
114
114
  exports.encodeUiRealizedPnlData = encodeUiRealizedPnlData;
@@ -9,13 +9,14 @@ const index_1 = require("../../../markets/oi/index");
9
9
  * @dev Builds funding fees sub-context for a specific pair
10
10
  */
11
11
  const buildFundingContext = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
12
+ var _a, _b, _c, _d, _e;
12
13
  const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
13
- if (!collateral?.pairFundingFees) {
14
+ if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairFundingFees)) {
14
15
  return undefined;
15
16
  }
16
- const params = collateral.pairFundingFees.params?.[pairIndex];
17
- const data = collateral.pairFundingFees.data?.[pairIndex];
18
- const pairOi = collateral.pairOis?.[pairIndex];
17
+ const params = (_a = collateral.pairFundingFees.params) === null || _a === void 0 ? void 0 : _a[pairIndex];
18
+ const data = (_b = collateral.pairFundingFees.data) === null || _b === void 0 ? void 0 : _b[pairIndex];
19
+ const pairOi = (_c = collateral.pairOis) === null || _c === void 0 ? void 0 : _c[pairIndex];
19
20
  const netExposureToken = (0, index_1.getPairV10OiTokenSkewCollateral)(pairIndex, {
20
21
  pairOis: collateral.pairOis,
21
22
  });
@@ -27,8 +28,8 @@ const buildFundingContext = (globalTradingVariables, collateralIndex, pairIndex,
27
28
  data,
28
29
  pairOi: pairOi
29
30
  ? {
30
- oiLongToken: pairOi.token?.long || 0,
31
- oiShortToken: pairOi.token?.short || 0,
31
+ oiLongToken: ((_d = pairOi.token) === null || _d === void 0 ? void 0 : _d.long) || 0,
32
+ oiShortToken: ((_e = pairOi.token) === null || _e === void 0 ? void 0 : _e.short) || 0,
32
33
  }
33
34
  : undefined,
34
35
  currentTimestamp,
@@ -111,7 +111,7 @@ exports.convertTradeInitialAccFundingFees = convertTradeInitialAccFundingFees;
111
111
  */
112
112
  const createFundingFeeContext = (collateralIndices, pairIndices, params, data, globalParams, currentTimestamp) => {
113
113
  const context = {
114
- currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
114
+ currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
115
115
  fundingParams: {},
116
116
  fundingData: {},
117
117
  globalParams: globalParams ? {} : undefined,