@gainsnetwork/sdk 1.3.0-rc7 → 1.4.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 (153) hide show
  1. package/lib/backend/globalTrades/index.js +10 -10
  2. package/lib/backend/tradingVariables/backend.types.d.ts +4 -11
  3. package/lib/backend/tradingVariables/converter.d.ts +3 -7
  4. package/lib/backend/tradingVariables/converter.js +63 -71
  5. package/lib/backend/tradingVariables/index.js +8 -10
  6. package/lib/backend/tradingVariables/types.d.ts +2 -4
  7. package/lib/constants.js +1 -1
  8. package/lib/contracts/addresses.d.ts +1 -1
  9. package/lib/contracts/addresses.js +4 -8
  10. package/lib/contracts/addresses.json +0 -29
  11. package/lib/contracts/index.d.ts +1 -1
  12. package/lib/contracts/index.js +3 -3
  13. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +299 -608
  14. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +10 -2
  15. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +317 -1995
  16. package/lib/contracts/utils/borrowingFees.js +20 -9
  17. package/lib/contracts/utils/openTrades.js +20 -11
  18. package/lib/contracts/utils/pairs.d.ts +2 -13
  19. package/lib/contracts/utils/pairs.js +30 -80
  20. package/lib/index.d.ts +0 -1
  21. package/lib/index.js +0 -1
  22. package/lib/markets/forex.js +1 -1
  23. package/lib/markets/leverage/builder.js +2 -2
  24. package/lib/markets/price/index.d.ts +0 -1
  25. package/lib/markets/price/index.js +0 -1
  26. package/lib/markets/price/types.d.ts +0 -27
  27. package/lib/trade/fees/borrowing/builder.js +3 -2
  28. package/lib/trade/fees/borrowing/converter.js +1 -5
  29. package/lib/trade/fees/borrowing/index.js +5 -5
  30. package/lib/trade/fees/borrowingV2/builder.js +4 -3
  31. package/lib/trade/fees/borrowingV2/converter.js +1 -1
  32. package/lib/trade/fees/borrowingV2/fetcher.js +32 -26
  33. package/lib/trade/fees/borrowingV2/index.js +3 -3
  34. package/lib/trade/fees/converter.js +22 -22
  35. package/lib/trade/fees/fundingFees/builder.js +7 -6
  36. package/lib/trade/fees/fundingFees/converter.js +1 -1
  37. package/lib/trade/fees/fundingFees/fetcher.js +25 -16
  38. package/lib/trade/fees/fundingFees/index.js +3 -2
  39. package/lib/trade/fees/tiers/index.js +2 -1
  40. package/lib/trade/fees/trading/index.js +3 -5
  41. package/lib/trade/liquidation/builder.js +2 -1
  42. package/lib/trade/liquidation/index.js +6 -4
  43. package/lib/trade/liquidation.d.ts +12 -0
  44. package/lib/trade/liquidation.js +55 -0
  45. package/lib/trade/oiWindows.js +2 -1
  46. package/lib/trade/pnl/builder.js +2 -1
  47. package/lib/trade/pnl/converter.js +1 -1
  48. package/lib/trade/pnl/index.js +7 -4
  49. package/lib/trade/pnl.d.ts +10 -0
  50. package/lib/trade/pnl.js +33 -0
  51. package/lib/trade/priceImpact/close/builder.js +2 -1
  52. package/lib/trade/priceImpact/close/index.js +1 -4
  53. package/lib/trade/priceImpact/cumulVol/builder.js +11 -21
  54. package/lib/trade/priceImpact/cumulVol/converter.d.ts +0 -63
  55. package/lib/trade/priceImpact/cumulVol/converter.js +1 -97
  56. package/lib/trade/priceImpact/cumulVol/index.d.ts +6 -7
  57. package/lib/trade/priceImpact/cumulVol/index.js +41 -149
  58. package/lib/trade/priceImpact/open/builder.js +2 -1
  59. package/lib/trade/priceImpact/open/index.js +1 -7
  60. package/lib/trade/priceImpact/skew/builder.js +3 -2
  61. package/lib/trade/priceImpact/skew/converter.js +1 -1
  62. package/lib/trade/priceImpact/skew/fetcher.js +33 -24
  63. package/package.json +2 -2
  64. package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +0 -1911
  65. package/lib/contracts/types/generated/GFarmTradingStorageV5.js +0 -2
  66. package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +0 -1067
  67. package/lib/contracts/types/generated/GNSBorrowingFees.js +0 -2
  68. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +0 -979
  69. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +0 -2
  70. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +0 -1058
  71. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +0 -2
  72. package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +0 -533
  73. package/lib/contracts/types/generated/GNSNftRewardsV6.js +0 -2
  74. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +0 -613
  75. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +0 -2
  76. package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +0 -911
  77. package/lib/contracts/types/generated/GNSPairInfosV6_1.js +0 -2
  78. package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +0 -660
  79. package/lib/contracts/types/generated/GNSPairsStorageV6.js +0 -2
  80. package/lib/contracts/types/generated/GNSTrading.d.ts +0 -758
  81. package/lib/contracts/types/generated/GNSTrading.js +0 -2
  82. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +0 -875
  83. package/lib/contracts/types/generated/GNSTradingCallbacks.js +0 -2
  84. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +0 -806
  85. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +0 -2
  86. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +0 -821
  87. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +0 -2
  88. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +0 -1387
  89. package/lib/contracts/types/generated/GNSTradingStorage.js +0 -2
  90. package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +0 -1838
  91. package/lib/contracts/types/generated/GTokenV6_3_2.js +0 -2
  92. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +0 -83
  93. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +0 -2691
  94. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +0 -88
  95. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +0 -1654
  96. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +0 -113
  97. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +0 -1742
  98. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +0 -124
  99. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +0 -1784
  100. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +0 -100
  101. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +0 -1116
  102. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +0 -100
  103. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +0 -1003
  104. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +0 -98
  105. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +0 -1485
  106. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +0 -117
  107. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +0 -1265
  108. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +0 -82
  109. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +0 -1273
  110. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +0 -82
  111. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +0 -1326
  112. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +0 -113
  113. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +0 -1428
  114. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +0 -96
  115. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +0 -2241
  116. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +0 -95
  117. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +0 -1071
  118. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +0 -110
  119. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +0 -2682
  120. package/lib/markets/oi/fetcher.d.ts +0 -58
  121. package/lib/markets/oi/fetcher.js +0 -181
  122. package/lib/markets/oi/validation.d.ts +0 -80
  123. package/lib/markets/oi/validation.js +0 -172
  124. package/lib/markets/price/signedPrices.d.ts +0 -36
  125. package/lib/markets/price/signedPrices.js +0 -181
  126. package/lib/pricing/depthBands/converter.d.ts +0 -65
  127. package/lib/pricing/depthBands/converter.js +0 -155
  128. package/lib/pricing/depthBands/decoder.d.ts +0 -32
  129. package/lib/pricing/depthBands/decoder.js +0 -109
  130. package/lib/pricing/depthBands/encoder.d.ts +0 -19
  131. package/lib/pricing/depthBands/encoder.js +0 -105
  132. package/lib/pricing/depthBands/index.d.ts +0 -8
  133. package/lib/pricing/depthBands/index.js +0 -26
  134. package/lib/pricing/depthBands/types.d.ts +0 -49
  135. package/lib/pricing/depthBands/types.js +0 -10
  136. package/lib/pricing/depthBands/validator.d.ts +0 -22
  137. package/lib/pricing/depthBands/validator.js +0 -113
  138. package/lib/pricing/depthBands.d.ts +0 -39
  139. package/lib/pricing/depthBands.js +0 -94
  140. package/lib/pricing/index.d.ts +0 -4
  141. package/lib/pricing/index.js +0 -20
  142. package/lib/trade/effectiveLeverage/builder.d.ts +0 -23
  143. package/lib/trade/effectiveLeverage/builder.js +0 -30
  144. package/lib/trade/fees/holdingFees/index.d.ts +0 -46
  145. package/lib/trade/fees/holdingFees/index.js +0 -105
  146. package/lib/trade/fees/holdingFees/types.d.ts +0 -23
  147. package/lib/trade/fees/holdingFees/types.js +0 -5
  148. package/lib/trade/fees/trading/holdingFees.d.ts +0 -28
  149. package/lib/trade/fees/trading/holdingFees.js +0 -66
  150. package/lib/trade/fees/trading/holdingFeesStructured.d.ts +0 -28
  151. package/lib/trade/fees/trading/holdingFeesStructured.js +0 -66
  152. package/lib/trade/priceImpact/cumulVol/types.d.ts +0 -11
  153. package/lib/trade/priceImpact/cumulVol/types.js +0 -2
@@ -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,
@@ -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.collateralToContractFormat = exports.priceToContractFormat = exports.fetchPairPendingAccFundingFeesBatch = exports.fetchTradeFeesDataBatch = exports.fetchTradeFeesData = exports.fetchTradeFundingFeesCollateral = exports.fetchPairPendingAccFundingFees = void 0;
4
13
  /**
@@ -9,9 +18,9 @@ exports.collateralToContractFormat = exports.priceToContractFormat = exports.fet
9
18
  * @param currentPairPrice Current pair price (1e10)
10
19
  * @returns Promise resolving to accumulated funding fees and current rate
11
20
  */
12
- const fetchPairPendingAccFundingFees = async (contract, collateralIndex, pairIndex, currentPairPrice) => {
21
+ const fetchPairPendingAccFundingFees = (contract, collateralIndex, pairIndex, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
13
22
  try {
14
- const result = await contract.getPairPendingAccFundingFees(collateralIndex, pairIndex, currentPairPrice);
23
+ const result = yield contract.getPairPendingAccFundingFees(collateralIndex, pairIndex, currentPairPrice);
15
24
  return {
16
25
  accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
17
26
  accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
@@ -22,7 +31,7 @@ const fetchPairPendingAccFundingFees = async (contract, collateralIndex, pairInd
22
31
  console.error("Error fetching pair pending acc funding fees:", error);
23
32
  throw error;
24
33
  }
25
- };
34
+ });
26
35
  exports.fetchPairPendingAccFundingFees = fetchPairPendingAccFundingFees;
27
36
  /**
28
37
  * @dev Fetches funding fees for a specific trade in collateral tokens
@@ -32,9 +41,9 @@ exports.fetchPairPendingAccFundingFees = fetchPairPendingAccFundingFees;
32
41
  * @param currentPairPrice Current pair price (1e10)
33
42
  * @returns Promise resolving to funding fee in collateral tokens
34
43
  */
35
- const fetchTradeFundingFeesCollateral = async (contract, trader, index, currentPairPrice) => {
44
+ const fetchTradeFundingFeesCollateral = (contract, trader, index, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
36
45
  try {
37
- const fundingFeeCollateral = await contract.getTradeFundingFeesCollateral(trader, index, currentPairPrice);
46
+ const fundingFeeCollateral = yield contract.getTradeFundingFeesCollateral(trader, index, currentPairPrice);
38
47
  // Convert from BigNumber to number (collateral precision already applied)
39
48
  return Number(fundingFeeCollateral);
40
49
  }
@@ -42,7 +51,7 @@ const fetchTradeFundingFeesCollateral = async (contract, trader, index, currentP
42
51
  console.error("Error fetching trade funding fees:", error);
43
52
  throw error;
44
53
  }
45
- };
54
+ });
46
55
  exports.fetchTradeFundingFeesCollateral = fetchTradeFundingFeesCollateral;
47
56
  /**
48
57
  * @dev Fetches trade fees data for a specific trade
@@ -51,9 +60,9 @@ exports.fetchTradeFundingFeesCollateral = fetchTradeFundingFeesCollateral;
51
60
  * @param index Trade index
52
61
  * @returns Promise resolving to trade fees data
53
62
  */
54
- const fetchTradeFeesData = async (contract, trader, index) => {
63
+ const fetchTradeFeesData = (contract, trader, index) => __awaiter(void 0, void 0, void 0, function* () {
55
64
  try {
56
- const feesData = await contract.getTradeFeesData(trader, index);
65
+ const feesData = yield contract.getTradeFeesData(trader, index);
57
66
  return {
58
67
  accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
59
68
  accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
@@ -64,7 +73,7 @@ const fetchTradeFeesData = async (contract, trader, index) => {
64
73
  console.error("Error fetching trade fees data:", error);
65
74
  throw error;
66
75
  }
67
- };
76
+ });
68
77
  exports.fetchTradeFeesData = fetchTradeFeesData;
69
78
  /**
70
79
  * @dev Fetches trade fees data for multiple trades
@@ -73,12 +82,12 @@ exports.fetchTradeFeesData = fetchTradeFeesData;
73
82
  * @param indices Array of trade indices
74
83
  * @returns Promise resolving to array of trade fees data
75
84
  */
76
- const fetchTradeFeesDataBatch = async (contract, traders, indices) => {
85
+ const fetchTradeFeesDataBatch = (contract, traders, indices) => __awaiter(void 0, void 0, void 0, function* () {
77
86
  if (traders.length !== indices.length) {
78
87
  throw new Error("Traders and indices arrays must have the same length");
79
88
  }
80
89
  try {
81
- const feesDatas = await contract.getTradeFeesDataArray(traders, indices);
90
+ const feesDatas = yield contract.getTradeFeesDataArray(traders, indices);
82
91
  return feesDatas.map(feesData => ({
83
92
  accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
84
93
  accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
@@ -89,7 +98,7 @@ const fetchTradeFeesDataBatch = async (contract, traders, indices) => {
89
98
  console.error("Error fetching trade fees data batch:", error);
90
99
  throw error;
91
100
  }
92
- };
101
+ });
93
102
  exports.fetchTradeFeesDataBatch = fetchTradeFeesDataBatch;
94
103
  /**
95
104
  * @dev Fetches pending accumulated funding fees for multiple pairs
@@ -99,7 +108,7 @@ exports.fetchTradeFeesDataBatch = fetchTradeFeesDataBatch;
99
108
  * @param currentPairPrices Array of current pair prices (1e10)
100
109
  * @returns Promise resolving to array of accumulated funding fees
101
110
  */
102
- const fetchPairPendingAccFundingFeesBatch = async (contract, collateralIndices, pairIndices, currentPairPrices) => {
111
+ const fetchPairPendingAccFundingFeesBatch = (contract, collateralIndices, pairIndices, currentPairPrices) => __awaiter(void 0, void 0, void 0, function* () {
103
112
  if (collateralIndices.length !== pairIndices.length ||
104
113
  pairIndices.length !== currentPairPrices.length) {
105
114
  throw new Error("All input arrays must have the same length");
@@ -107,7 +116,7 @@ const fetchPairPendingAccFundingFeesBatch = async (contract, collateralIndices,
107
116
  try {
108
117
  // Fetch all in parallel
109
118
  const promises = collateralIndices.map((collateralIndex, i) => contract.getPairPendingAccFundingFees(collateralIndex, pairIndices[i], currentPairPrices[i]));
110
- const results = await Promise.all(promises);
119
+ const results = yield Promise.all(promises);
111
120
  return results.map(result => ({
112
121
  accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
113
122
  accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
@@ -118,7 +127,7 @@ const fetchPairPendingAccFundingFeesBatch = async (contract, collateralIndices,
118
127
  console.error("Error fetching pair pending acc funding fees batch:", error);
119
128
  throw error;
120
129
  }
121
- };
130
+ });
122
131
  exports.fetchPairPendingAccFundingFeesBatch = fetchPairPendingAccFundingFeesBatch;
123
132
  /**
124
133
  * @dev Helper to convert price from number to contract format
@@ -136,6 +145,6 @@ exports.priceToContractFormat = priceToContractFormat;
136
145
  * @returns Amount in contract format
137
146
  */
138
147
  const collateralToContractFormat = (amount, decimals) => {
139
- return BigInt(Math.round(amount * 10 ** decimals));
148
+ return BigInt(Math.round(amount * Math.pow(10, decimals)));
140
149
  };
141
150
  exports.collateralToContractFormat = collateralToContractFormat;
@@ -259,9 +259,10 @@ exports.getTradeFundingFeesCollateral = getTradeFundingFeesCollateral;
259
259
  * @returns Complete funding fee calculation result
260
260
  */
261
261
  const getTradeFundingFees = (input, context) => {
262
+ var _a, _b;
262
263
  // Get params and data from context
263
- const params = context.fundingParams[input.collateralIndex]?.[input.pairIndex];
264
- const data = context.fundingData[input.collateralIndex]?.[input.pairIndex];
264
+ const params = (_a = context.fundingParams[input.collateralIndex]) === null || _a === void 0 ? void 0 : _a[input.pairIndex];
265
+ const data = (_b = context.fundingData[input.collateralIndex]) === null || _b === void 0 ? void 0 : _b[input.pairIndex];
265
266
  if (!params || !data) {
266
267
  throw new Error(`Missing funding fee data for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
267
268
  }
@@ -24,8 +24,9 @@ exports.MAX_FEE_TIERS = 8;
24
24
  const getCurrentDay = () => Math.floor(Date.now() / 1000 / 60 / 60 / 24);
25
25
  exports.getCurrentDay = getCurrentDay;
26
26
  const getFeeTiersCount = (feeTiers) => {
27
+ var _a;
27
28
  for (let i = exports.MAX_FEE_TIERS; i > 0; --i) {
28
- if (feeTiers[i - 1]?.feeMultiplier > 0) {
29
+ if (((_a = feeTiers[i - 1]) === null || _a === void 0 ? void 0 : _a.feeMultiplier) > 0) {
29
30
  return i;
30
31
  }
31
32
  }
@@ -35,10 +35,11 @@ const types_1 = require("../../../contracts/types");
35
35
  * @returns Total fee in collateral tokens
36
36
  */
37
37
  const getTotalTradeFeesCollateral = (collateralIndex, trader, pairIndex, positionSizeCollateral, isCounterTrade, context) => {
38
+ var _a;
38
39
  const { fee, collateralPriceUsd } = context;
39
40
  const { totalPositionSizeFeeP, minPositionSizeUsd } = fee;
40
41
  // Get counter trade fee rate multiplier (default 1 = 1x)
41
- const counterTradeFeeRateMultiplier = isCounterTrade && context.counterTradeSettings?.[pairIndex]
42
+ const counterTradeFeeRateMultiplier = isCounterTrade && ((_a = context.counterTradeSettings) === null || _a === void 0 ? void 0 : _a[pairIndex])
42
43
  ? context.counterTradeSettings[pairIndex].feeRateMultiplier
43
44
  : 1;
44
45
  // Apply counter trade multiplier to position size
@@ -118,10 +119,7 @@ const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, c
118
119
  if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
119
120
  context.funding &&
120
121
  tradeFeesData.initialAccFundingFeeP !== undefined) {
121
- fundingFeeCollateral = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, {
122
- ...context.funding,
123
- currentTimestamp: context.currentTimestamp,
124
- });
122
+ fundingFeeCollateral = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, Object.assign(Object.assign({}, context.funding), { currentTimestamp: context.currentTimestamp }));
125
123
  }
126
124
  // Calculate borrowing fees v2 (v10+ only)
127
125
  let borrowingFeeCollateral = 0;
@@ -17,6 +17,7 @@ const builder_4 = require("../fees/trading/builder");
17
17
  * @returns Complete context ready for getLiquidationPrice
18
18
  */
19
19
  const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, additionalParams) => {
20
+ var _a;
20
21
  const { trade, tradeInfo } = tradeContainer;
21
22
  const collateralIndex = trade.collateralIndex || 1;
22
23
  const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
@@ -28,7 +29,7 @@ const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, ad
28
29
  core: {
29
30
  currentBlock: additionalParams.currentBlock,
30
31
  currentTimestamp: additionalParams.currentTimestamp,
31
- collateralPriceUsd: collateral.prices?.collateralPriceUsd || 1,
32
+ collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
32
33
  contractsVersion: tradeInfo.contractsVersion,
33
34
  spreadP: additionalParams.spreadP,
34
35
  },
@@ -27,6 +27,7 @@ const __1 = require("..");
27
27
  * @returns Liquidation price
28
28
  */
29
29
  const getLiquidationPrice = (trade, context) => {
30
+ var _a, _b;
30
31
  // Extract parameters from structured context
31
32
  const { currentPairPrice, additionalFeeCollateral = 0, partialCloseMultiplier = 1, beforeOpened = false, isCounterTrade = false, } = context.liquidationSpecific;
32
33
  // 1. Calculate closing fees
@@ -71,9 +72,9 @@ const getLiquidationPrice = (trade, context) => {
71
72
  trade.leverage;
72
73
  // 6. Apply closing spread for v9.2+
73
74
  if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
74
- ((context.tradeData.liquidationParams?.maxLiqSpreadP !== undefined &&
75
+ ((((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
75
76
  context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
76
- (context.liquidationSpecific.userPriceImpact?.fixedSpreadP !==
77
+ (((_b = context.liquidationSpecific.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !==
77
78
  undefined &&
78
79
  context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
79
80
  const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
@@ -98,6 +99,7 @@ exports.getLiquidationPrice = getLiquidationPrice;
98
99
  * @returns New liquidation price after the update
99
100
  */
100
101
  const getLiquidationPriceAfterPositionUpdate = (existingTrade, newCollateralAmount, newLeverage, isLeverageUpdate, positionSizeCollateralDelta, pnlToRealizeCollateral, context) => {
102
+ var _a, _b;
101
103
  const { currentPairPrice, isCounterTrade = false } = context.liquidationSpecific;
102
104
  // 1. Calculate closing fees on the new position size
103
105
  const closingFeeCollateral = (0, __1.getTotalTradeFeesCollateral)(existingTrade.collateralIndex, "", // No fee tiers applied for liquidation calculation
@@ -165,9 +167,9 @@ const getLiquidationPriceAfterPositionUpdate = (existingTrade, newCollateralAmou
165
167
  newLeverage;
166
168
  // 9. Apply closing spread for v9.2+
167
169
  if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
168
- ((context.tradeData.liquidationParams?.maxLiqSpreadP !== undefined &&
170
+ ((((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
169
171
  context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
170
- (context.liquidationSpecific.userPriceImpact?.fixedSpreadP !==
172
+ (((_b = context.liquidationSpecific.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !==
171
173
  undefined &&
172
174
  context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
173
175
  const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
@@ -0,0 +1,12 @@
1
+ import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
2
+ import { Fee, LiquidationParams, Trade, UserPriceImpact } from "./types";
3
+ import { ContractsVersion } from "../contracts/types";
4
+ export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
5
+ liquidationParams: LiquidationParams | undefined;
6
+ pairSpreadP: number | undefined;
7
+ collateralPriceUsd: number | undefined;
8
+ contractsVersion: ContractsVersion | undefined;
9
+ userPriceImpact?: UserPriceImpact | undefined;
10
+ };
11
+ export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
12
+ export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
4
+ const fees_1 = require("./fees");
5
+ const spread_1 = require("./spread");
6
+ const types_1 = require("../contracts/types");
7
+ const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
8
+ var _a, _b;
9
+ const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd);
10
+ const borrowingFee = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
11
+ const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
12
+ let liqPriceDistance = (trade.openPrice *
13
+ (trade.collateralAmount * liqThresholdP - (borrowingFee + closingFee))) /
14
+ trade.collateralAmount /
15
+ trade.leverage;
16
+ if ((context === null || context === void 0 ? void 0 : context.contractsVersion) !== undefined &&
17
+ context.contractsVersion >= types_1.ContractsVersion.V9_2 &&
18
+ ((((_a = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
19
+ context.liquidationParams.maxLiqSpreadP > 0) ||
20
+ (((_b = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !== undefined &&
21
+ context.userPriceImpact.fixedSpreadP > 0))) {
22
+ const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams, context.userPriceImpact);
23
+ liqPriceDistance -= trade.openPrice * closingSpreadP;
24
+ }
25
+ return trade.long
26
+ ? Math.max(trade.openPrice - liqPriceDistance, 0)
27
+ : Math.max(trade.openPrice + liqPriceDistance, 0);
28
+ };
29
+ exports.getLiquidationPrice = getLiquidationPrice;
30
+ const getLiqPnlThresholdP = (liquidationParams, leverage) => {
31
+ if (liquidationParams === undefined ||
32
+ leverage === undefined ||
33
+ liquidationParams.maxLiqSpreadP === 0 ||
34
+ liquidationParams.startLiqThresholdP === 0 ||
35
+ liquidationParams.endLiqThresholdP === 0 ||
36
+ liquidationParams.startLeverage === 0 ||
37
+ liquidationParams.endLeverage === 0) {
38
+ return 0.9;
39
+ }
40
+ if (leverage < liquidationParams.startLeverage) {
41
+ return liquidationParams.startLiqThresholdP;
42
+ }
43
+ if (leverage > liquidationParams.endLeverage) {
44
+ return liquidationParams.endLiqThresholdP;
45
+ }
46
+ if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
47
+ return liquidationParams.endLiqThresholdP;
48
+ }
49
+ return (liquidationParams.startLiqThresholdP -
50
+ ((leverage - liquidationParams.startLeverage) *
51
+ (liquidationParams.startLiqThresholdP -
52
+ liquidationParams.endLiqThresholdP)) /
53
+ (liquidationParams.endLeverage - liquidationParams.startLeverage));
54
+ };
55
+ exports.getLiqPnlThresholdP = getLiqPnlThresholdP;