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

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 (66) hide show
  1. package/lib/backend/index.d.ts +1 -0
  2. package/lib/backend/index.js +10 -0
  3. package/lib/backend/tradingVariables/backend.types.d.ts +5 -0
  4. package/lib/backend/tradingVariables/converter.d.ts +2 -1
  5. package/lib/backend/tradingVariables/converter.js +2 -1
  6. package/lib/backend/tradingVariables/index.d.ts +2 -0
  7. package/lib/backend/tradingVariables/index.js +18 -0
  8. package/lib/backend/tradingVariables/types.d.ts +2 -1
  9. package/lib/constants.d.ts +10 -0
  10. package/lib/constants.js +11 -1
  11. package/lib/contracts/utils/pairs.js +10 -0
  12. package/lib/index.d.ts +1 -0
  13. package/lib/index.js +4 -0
  14. package/lib/trade/fees/borrowing/builder.d.ts +14 -0
  15. package/lib/trade/fees/borrowing/builder.js +33 -0
  16. package/lib/trade/fees/borrowing/index.d.ts +3 -2
  17. package/lib/trade/fees/borrowing/index.js +13 -4
  18. package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
  19. package/lib/trade/fees/borrowingV2/builder.js +24 -0
  20. package/lib/trade/fees/borrowingV2/converter.d.ts +12 -3
  21. package/lib/trade/fees/borrowingV2/converter.js +29 -18
  22. package/lib/trade/fees/borrowingV2/fetcher.js +5 -13
  23. package/lib/trade/fees/borrowingV2/index.d.ts +5 -17
  24. package/lib/trade/fees/borrowingV2/index.js +9 -37
  25. package/lib/trade/fees/borrowingV2/types.d.ts +22 -6
  26. package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
  27. package/lib/trade/fees/fundingFees/builder.js +35 -0
  28. package/lib/trade/fees/fundingFees/index.d.ts +12 -2
  29. package/lib/trade/fees/fundingFees/index.js +23 -3
  30. package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
  31. package/lib/trade/fees/fundingFees/pairContext.js +17 -0
  32. package/lib/trade/fees/index.d.ts +1 -1
  33. package/lib/trade/fees/index.js +2 -3
  34. package/lib/trade/fees/trading/builder.d.ts +18 -0
  35. package/lib/trade/fees/trading/builder.js +20 -0
  36. package/lib/trade/fees/trading/converter.d.ts +2 -0
  37. package/lib/trade/fees/trading/converter.js +5 -1
  38. package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
  39. package/lib/trade/fees/trading/holdingFees.js +66 -0
  40. package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
  41. package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
  42. package/lib/trade/fees/trading/index.d.ts +19 -11
  43. package/lib/trade/fees/trading/index.js +29 -21
  44. package/lib/trade/liquidation/builder.d.ts +25 -0
  45. package/lib/trade/liquidation/builder.js +59 -0
  46. package/lib/trade/liquidation/index.d.ts +7 -22
  47. package/lib/trade/liquidation/index.js +41 -105
  48. package/lib/trade/liquidation/types.d.ts +37 -39
  49. package/lib/trade/pnl/builder.d.ts +16 -0
  50. package/lib/trade/pnl/builder.js +44 -0
  51. package/lib/trade/pnl/index.d.ts +21 -30
  52. package/lib/trade/pnl/index.js +119 -50
  53. package/lib/trade/pnl/types.d.ts +26 -36
  54. package/lib/trade/types.d.ts +11 -1
  55. package/lib/trade/types.js +10 -0
  56. package/package.json +1 -1
  57. package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +0 -75
  58. package/lib/contracts/fetch/fees/borrowingFeesV2.js +0 -193
  59. package/lib/contracts/fetch/fees/fundingFees.d.ts +0 -66
  60. package/lib/contracts/fetch/fees/fundingFees.js +0 -150
  61. package/lib/contracts/fetch/priceImpact/skew.d.ts +0 -63
  62. package/lib/contracts/fetch/priceImpact/skew.js +0 -168
  63. package/lib/trade/liquidation.d.ts +0 -12
  64. package/lib/trade/liquidation.js +0 -55
  65. package/lib/trade/pnl.d.ts +0 -10
  66. package/lib/trade/pnl.js +0 -33
@@ -18,13 +18,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
18
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.getPnl = exports.getComprehensivePnl = exports.getTradeValue = exports.getPnlPercent = void 0;
22
- const fees_1 = require("../fees");
23
- const borrowingV2_1 = require("../fees/borrowingV2");
24
- const fundingFees_1 = require("../fees/fundingFees");
21
+ exports.getPriceForTargetPnlPercentage = exports.getPnl = exports.getComprehensivePnl = exports.getTradeValue = exports.getPnlPercent = void 0;
22
+ const borrowing_1 = require("../fees/borrowing");
25
23
  const trading_1 = require("../fees/trading");
26
24
  const liquidation_1 = require("../liquidation");
27
- const types_1 = require("../../contracts/types");
28
25
  /**
29
26
  * @dev Calculates PnL percentage for a position
30
27
  * @dev Mirrors contract's getPnlPercent function
@@ -66,56 +63,41 @@ exports.getTradeValue = getTradeValue;
66
63
  * @returns Detailed PnL breakdown
67
64
  */
68
65
  const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
66
+ var _a;
69
67
  // Calculate base PnL percentage
70
68
  let pnlPercent = (0, exports.getPnlPercent)(trade.openPrice, currentPrice, trade.long, trade.leverage);
69
+ if (!context.tradeData) {
70
+ throw new Error("Trade data is undefined");
71
+ }
71
72
  // Calculate position size
72
73
  const positionSizeCollateral = trade.collateralAmount * trade.leverage;
73
- // Initialize fees
74
- let borrowingFeeV1 = 0;
75
- let borrowingFeeV2 = 0;
76
- let fundingFee = 0;
77
- // Calculate holding fees based on version
78
- if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
79
- context.tradeFeesData) {
80
- // V10: Use aggregated holding fees function
81
- const holdingFees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeFeesData, // Cast to handle partial type
82
- currentPrice, context);
83
- fundingFee = holdingFees.fundingFeeCollateral;
84
- borrowingFeeV2 = holdingFees.borrowingFeeCollateral;
85
- borrowingFeeV1 = holdingFees.borrowingFeeCollateral_old;
86
- }
87
- else {
88
- // Pre-v10: Calculate fees individually
89
- // V1 borrowing fees (still used by some markets)
90
- if (context.initialAccFees) {
91
- borrowingFeeV1 = (0, fees_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFees, context);
92
- }
93
- // V2 borrowing fees
94
- if (context.tradeFeesData && context.borrowingProviderContext) {
95
- borrowingFeeV2 = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
96
- positionSizeCollateral,
97
- openPrice: trade.openPrice,
98
- collateralIndex: trade.collateralIndex,
99
- pairIndex: trade.pairIndex,
100
- currentPairPrice: currentPrice,
101
- initialAccBorrowingFeeP: context.tradeFeesData.initialAccBorrowingFeeP,
102
- currentTimestamp: context.currentTimestamp,
103
- }, context.borrowingProviderContext);
104
- }
105
- // Funding fees (v10+)
106
- if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
107
- context.tradeFeesData) {
108
- fundingFee = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, context.tradeFeesData, // Cast to handle partial type
109
- currentPrice, context);
110
- }
111
- }
74
+ // Calculate holding fees - always use getTradePendingHoldingFeesCollateral
75
+ // This mirrors the contract's getTradeValueCollateral which always calls this function
76
+ const pendingHoldingFees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeData.tradeFeesData, currentPrice, {
77
+ contractsVersion: context.core.contractsVersion,
78
+ currentTimestamp: context.core.currentTimestamp,
79
+ collateralPriceUsd: context.core.collateralPriceUsd,
80
+ borrowingV1: context.borrowingV1,
81
+ borrowingV2: context.borrowingV2,
82
+ funding: context.funding,
83
+ initialAccFees: context.tradeData.initialAccFees,
84
+ });
85
+ const borrowingFeeV1 = pendingHoldingFees.borrowingFeeCollateral_old;
86
+ const borrowingFeeV2 = pendingHoldingFees.borrowingFeeCollateral;
87
+ const fundingFee = pendingHoldingFees.fundingFeeCollateral;
112
88
  // Calculate closing fees
113
- const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(trade.collateralIndex, trade.user, trade.pairIndex, positionSizeCollateral, trade.isCounterTrade || false, context);
89
+ const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(trade.collateralIndex, trade.user, trade.pairIndex, positionSizeCollateral, trade.isCounterTrade || false, {
90
+ fee: context.trading.fee,
91
+ globalTradeFeeParams: context.trading.globalTradeFeeParams,
92
+ collateralPriceUsd: context.core.collateralPriceUsd,
93
+ traderFeeMultiplier: context.trading.traderFeeMultiplier,
94
+ });
114
95
  // Total fees
115
96
  const totalFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee + closingFee;
116
97
  // Check liquidation
117
- const liquidationThreshold = context.liquidationParams
118
- ? (0, liquidation_1.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage) * -100
98
+ const liquidationThreshold = ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.liquidationParams)
99
+ ? (0, liquidation_1.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage) *
100
+ -100
119
101
  : -90; // Default 90% loss
120
102
  const isLiquidated = pnlPercent <= liquidationThreshold;
121
103
  // If liquidated, set PnL to -100%
@@ -130,11 +112,24 @@ const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
130
112
  const leveragedPositionSize = trade.collateralAmount * trade.leverage;
131
113
  // Calculate net PnL after fees
132
114
  const netPnlAfterFees = pnlCollateral - totalFees;
115
+ // Calculate unrealized PnL (before closing fee, after holding fees)
116
+ const totalHoldingFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee;
117
+ const uPnlCollateral = pnlCollateral - totalHoldingFees;
118
+ const uPnlPercent = (uPnlCollateral / trade.collateralAmount) * 100;
119
+ // Realized PnL (after all fees including closing)
120
+ const realizedPnlCollateral = pnlCollateral - totalFees;
121
+ const realizedPnlPercent = (realizedPnlCollateral / trade.collateralAmount) * 100;
133
122
  return {
134
123
  // Core PnL values
135
124
  pnlPercent,
136
125
  pnlCollateral,
137
126
  tradeValue,
127
+ // Unrealized PnL (after holding fees, before closing fee)
128
+ uPnlCollateral,
129
+ uPnlPercent,
130
+ // Realized PnL (after all fees)
131
+ realizedPnlCollateral,
132
+ realizedPnlPercent,
138
133
  // Fee breakdown
139
134
  fees: {
140
135
  borrowingV1: borrowingFeeV1,
@@ -174,8 +169,17 @@ const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, use
174
169
  let pnlCollat = trade.long
175
170
  ? ((price - openPrice) / openPrice) * leverage * posCollat
176
171
  : ((openPrice - price) / openPrice) * leverage * posCollat;
177
- if (useFees) {
178
- pnlCollat -= (0, fees_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
172
+ if (useFees &&
173
+ context.pairs &&
174
+ context.groups &&
175
+ context.currentBlock !== undefined &&
176
+ context.collateralPriceUsd !== undefined) {
177
+ pnlCollat -= (0, borrowing_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, {
178
+ currentBlock: context.currentBlock,
179
+ groups: context.groups,
180
+ pairs: context.pairs,
181
+ collateralPriceUsd: context.collateralPriceUsd,
182
+ });
179
183
  }
180
184
  let pnlPercentage = (pnlCollat / posCollat) * 100;
181
185
  // Can be liquidated
@@ -187,7 +191,12 @@ const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, use
187
191
  // Calculate closing fee using the same function as opening fees
188
192
  const positionSizeCollateral = posCollat * trade.leverage;
189
193
  const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(0, // collateralIndex not used
190
- trade.user, trade.pairIndex, positionSizeCollateral, (_a = trade.isCounterTrade) !== null && _a !== void 0 ? _a : false, context);
194
+ trade.user, trade.pairIndex, positionSizeCollateral, (_a = trade.isCounterTrade) !== null && _a !== void 0 ? _a : false, {
195
+ fee: context.fee,
196
+ globalTradeFeeParams: context.globalTradeFeeParams,
197
+ collateralPriceUsd: context.collateralPriceUsd || 1,
198
+ traderFeeMultiplier: context.traderFeeMultiplier,
199
+ });
191
200
  pnlCollat -= closingFee;
192
201
  pnlPercentage = (pnlCollat / posCollat) * 100;
193
202
  }
@@ -196,6 +205,66 @@ const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, use
196
205
  return [pnlCollat, pnlPercentage];
197
206
  };
198
207
  exports.getPnl = getPnl;
208
+ /**
209
+ * @dev Calculates the price needed to achieve a target PnL percentage
210
+ * @param targetPnlPercent The target PnL percentage (e.g., 50 for 50% profit, -25 for 25% loss)
211
+ * @param trade The trade to calculate for
212
+ * @param tradeInfo Trade info with timestamps
213
+ * @param context Context with fee calculation parameters
214
+ * @param netPnl Whether to include closing fees in the calculation
215
+ * @returns The price that would result in the target PnL percentage
216
+ */
217
+ const getPriceForTargetPnlPercentage = (targetPnlPercent, trade, tradeInfo, context, netPnl = false) => {
218
+ var _a, _b;
219
+ const { leverage, openPrice, long, collateralAmount } = trade;
220
+ const positionSizeCollateral = collateralAmount * leverage;
221
+ // Calculate holding fees - always use getTradePendingHoldingFeesCollateral
222
+ // This mirrors the contract's getTradeValueCollateral which always calls this function
223
+ const fees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.tradeFeesData) || {
224
+ realizedTradingFeesCollateral: 0,
225
+ realizedPnlCollateral: 0,
226
+ manuallyRealizedNegativePnlCollateral: 0,
227
+ alreadyTransferredNegativePnlCollateral: 0,
228
+ virtualAvailableCollateralInDiamond: 0,
229
+ initialAccFundingFeeP: 0,
230
+ initialAccBorrowingFeeP: 0,
231
+ }, openPrice, // Use open price as a baseline
232
+ {
233
+ contractsVersion: context.core.contractsVersion,
234
+ currentTimestamp: context.core.currentTimestamp,
235
+ collateralPriceUsd: context.core.collateralPriceUsd,
236
+ borrowingV1: context.borrowingV1,
237
+ borrowingV2: context.borrowingV2,
238
+ funding: context.funding,
239
+ initialAccFees: (_b = context.tradeData) === null || _b === void 0 ? void 0 : _b.initialAccFees,
240
+ });
241
+ const totalHoldingFees = fees.fundingFeeCollateral +
242
+ fees.borrowingFeeCollateral +
243
+ fees.borrowingFeeCollateral_old;
244
+ const targetPnlInCollateral = (collateralAmount * targetPnlPercent) / 100;
245
+ let targetPnlGross = targetPnlInCollateral + totalHoldingFees;
246
+ if (netPnl) {
247
+ // Include closing fees
248
+ const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(trade.collateralIndex, trade.user, trade.pairIndex, positionSizeCollateral, trade.isCounterTrade || false, {
249
+ fee: context.trading.fee,
250
+ globalTradeFeeParams: context.trading.globalTradeFeeParams,
251
+ collateralPriceUsd: context.core.collateralPriceUsd,
252
+ traderFeeMultiplier: context.trading.traderFeeMultiplier,
253
+ });
254
+ targetPnlGross += closingFee;
255
+ }
256
+ // Calculate the price
257
+ let price;
258
+ if (long) {
259
+ price = openPrice + (targetPnlGross * openPrice) / positionSizeCollateral;
260
+ }
261
+ else {
262
+ price = openPrice - (targetPnlGross * openPrice) / positionSizeCollateral;
263
+ }
264
+ return price;
265
+ };
266
+ exports.getPriceForTargetPnlPercentage = getPriceForTargetPnlPercentage;
199
267
  // Re-export types
200
268
  __exportStar(require("./types"), exports);
201
269
  __exportStar(require("./converter"), exports);
270
+ __exportStar(require("./builder"), exports);
@@ -1,21 +1,11 @@
1
1
  /**
2
2
  * @dev Types for PnL calculations
3
3
  */
4
- import { Trade, TradeInfo, TradeFeesData } from "../types";
5
- /**
6
- * @dev Input for trade value calculation with all fees
7
- * @dev Mirrors contract's TradeValueInput struct
8
- */
9
- export type TradeValueInput = {
10
- trade: Trade;
11
- currentPrice: number;
12
- collateralPriceUsd: number;
13
- fees: {
14
- openingFeeCollateral: number;
15
- closingFeeCollateral: number;
16
- holdingFeesCollateral: number;
17
- };
18
- };
4
+ import { ContractsVersion } from "src/contracts/types";
5
+ import { BorrowingFee, GetBorrowingFeeContext, TradingFeesSubContext } from "../fees";
6
+ import { GetPairBorrowingFeeV2Context } from "../fees/borrowingV2";
7
+ import { GetPairFundingFeeContext } from "../fees/fundingFees";
8
+ import { TradeFeesData, LiquidationParams } from "../types";
19
9
  /**
20
10
  * @dev Result of trade value calculation
21
11
  */
@@ -53,6 +43,10 @@ export type ComprehensivePnlResult = {
53
43
  pnlPercent: number;
54
44
  pnlCollateral: number;
55
45
  tradeValue: number;
46
+ uPnlCollateral: number;
47
+ uPnlPercent: number;
48
+ realizedPnlCollateral: number;
49
+ realizedPnlPercent: number;
56
50
  fees: FeeBreakdown;
57
51
  priceImpact?: PriceImpactBreakdown;
58
52
  isLiquidated: boolean;
@@ -61,26 +55,22 @@ export type ComprehensivePnlResult = {
61
55
  netPnlAfterFees: number;
62
56
  };
63
57
  /**
64
- * @dev Simple PnL result for backward compatibility
65
- */
66
- export type SimplePnlResult = [pnlCollateral: number, pnlPercent: number];
67
- /**
68
- * @dev Input for PnL calculation with price impact
58
+ * @dev Context for comprehensive PnL calculations with nested sub-contexts
69
59
  */
70
- export type PnlWithPriceImpactInput = {
71
- trade: Trade;
72
- tradeInfo: TradeInfo;
73
- oraclePrice: number;
74
- currentPairPrice: number;
75
- usePriceImpact: boolean;
76
- includeOpeningFees?: boolean;
77
- };
78
- /**
79
- * @dev Context for v10 PnL calculations
80
- */
81
- export type V10PnlContext = {
82
- tradeFeesData: TradeFeesData;
83
- priceImpactContext?: any;
84
- skewContext?: any;
85
- cumulVolContext?: any;
60
+ export type GetComprehensivePnlContext = {
61
+ core: {
62
+ currentBlock: number;
63
+ currentTimestamp: number;
64
+ collateralPriceUsd: number;
65
+ contractsVersion: ContractsVersion;
66
+ };
67
+ borrowingV1?: GetBorrowingFeeContext;
68
+ borrowingV2?: GetPairBorrowingFeeV2Context;
69
+ funding?: GetPairFundingFeeContext;
70
+ trading: TradingFeesSubContext;
71
+ tradeData?: {
72
+ tradeFeesData: TradeFeesData;
73
+ liquidationParams: LiquidationParams;
74
+ initialAccFees?: BorrowingFee.InitialAccFees;
75
+ };
86
76
  };
@@ -619,5 +619,15 @@ export declare enum PairIndex {
619
619
  SYRUPUSD = 373,
620
620
  SIGNUSD = 374,
621
621
  ZORAUSD = 375,
622
- COINUSD = 376
622
+ COINUSD = 376,
623
+ HOODUSD = 377,
624
+ MSTRUSD = 378,
625
+ NFLXUSD = 379,
626
+ LAUNCHCOINUSD = 380,
627
+ NXPCUSD = 381,
628
+ SOPHUSD = 382,
629
+ LPTUSD = 383,
630
+ BVIVUSD = 384,
631
+ EVIVUSD = 385,
632
+ CRCLUSD = 386
623
633
  }
@@ -406,4 +406,14 @@ var PairIndex;
406
406
  PairIndex[PairIndex["SIGNUSD"] = 374] = "SIGNUSD";
407
407
  PairIndex[PairIndex["ZORAUSD"] = 375] = "ZORAUSD";
408
408
  PairIndex[PairIndex["COINUSD"] = 376] = "COINUSD";
409
+ PairIndex[PairIndex["HOODUSD"] = 377] = "HOODUSD";
410
+ PairIndex[PairIndex["MSTRUSD"] = 378] = "MSTRUSD";
411
+ PairIndex[PairIndex["NFLXUSD"] = 379] = "NFLXUSD";
412
+ PairIndex[PairIndex["LAUNCHCOINUSD"] = 380] = "LAUNCHCOINUSD";
413
+ PairIndex[PairIndex["NXPCUSD"] = 381] = "NXPCUSD";
414
+ PairIndex[PairIndex["SOPHUSD"] = 382] = "SOPHUSD";
415
+ PairIndex[PairIndex["LPTUSD"] = 383] = "LPTUSD";
416
+ PairIndex[PairIndex["BVIVUSD"] = 384] = "BVIVUSD";
417
+ PairIndex[PairIndex["EVIVUSD"] = 385] = "EVIVUSD";
418
+ PairIndex[PairIndex["CRCLUSD"] = 386] = "CRCLUSD";
409
419
  })(PairIndex = exports.PairIndex || (exports.PairIndex = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.0.0-v10.rc3",
3
+ "version": "0.0.0-v10.rc5",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -1,75 +0,0 @@
1
- import type { GNSMultiCollatDiamond } from "../../types/generated";
2
- import { BorrowingFeeV2 } from "../../../trade/fees/borrowingV2";
3
- /**
4
- * @dev Fetches borrowing fee parameters v2 for specific pairs
5
- * @param contract GNSMultiCollatDiamond contract instance
6
- * @param collateralIndices Array of collateral indices
7
- * @param pairIndices Array of pair indices
8
- * @returns Promise resolving to array of borrowing fee parameters
9
- */
10
- export declare const fetchBorrowingFeeParamsV2: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<BorrowingFeeV2.BorrowingFeeParams[]>;
11
- /**
12
- * @dev Fetches pair borrowing fee data v2 for specific pairs
13
- * @param contract GNSMultiCollatDiamond contract instance
14
- * @param collateralIndices Array of collateral indices
15
- * @param pairIndices Array of pair indices
16
- * @returns Promise resolving to array of pair borrowing fee data
17
- */
18
- export declare const fetchPairBorrowingFeeDataV2: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<BorrowingFeeV2.PairBorrowingFeeData[]>;
19
- /**
20
- * @dev Fetches borrowing fees in collateral tokens for a specific trade
21
- * @param contract GNSMultiCollatDiamond contract instance
22
- * @param trader Address of the trader
23
- * @param index Trade index
24
- * @param currentPairPrice Current price of the trading pair (1e6 precision)
25
- * @returns Promise resolving to borrowing fees in collateral tokens
26
- */
27
- export declare const fetchTradeBorrowingFeesCollateralV2: (contract: GNSMultiCollatDiamond, trader: string, index: number, currentPairPrice: number) => Promise<number>;
28
- /**
29
- * @dev Fetches pending accumulated borrowing fees for a specific pair
30
- * @param contract GNSMultiCollatDiamond contract instance
31
- * @param collateralIndex Index of the collateral
32
- * @param pairIndex Index of the trading pair
33
- * @param currentPairPrice Current price of the trading pair (1e6 precision)
34
- * @returns Promise resolving to pending accumulated borrowing fee
35
- */
36
- export declare const fetchPairPendingAccBorrowingFeesV2: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, currentPairPrice: number) => Promise<number>;
37
- /**
38
- * @dev Convenience function to fetch all borrowing v2 data for specific pairs
39
- * @param contract GNSMultiCollatDiamond contract instance
40
- * @param collateralIndex Index of the collateral
41
- * @param pairIndices Array of pair indices
42
- * @returns Promise resolving to complete borrowing v2 data set
43
- */
44
- export declare const fetchAllBorrowingV2Data: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndices: number[]) => Promise<{
45
- params: BorrowingFeeV2.BorrowingFeeParams[];
46
- data: BorrowingFeeV2.PairBorrowingFeeData[];
47
- context: BorrowingFeeV2.GetBorrowingFeeV2Context;
48
- }>;
49
- /**
50
- * @dev Creates a complete borrowing v2 context from contract data
51
- * @param contract GNSMultiCollatDiamond contract instance
52
- * @param collateralIndex Index of the collateral
53
- * @param pairIndices Array of pair indices
54
- * @param currentTimestamp Optional current timestamp for calculations
55
- * @returns Promise resolving to complete borrowing v2 context
56
- */
57
- export declare const createBorrowingV2ContextFromContract: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndices: number[], currentTimestamp?: number) => Promise<BorrowingFeeV2.GetBorrowingFeeV2Context>;
58
- /**
59
- * @dev Helper function to create context from already fetched arrays
60
- * @param collateralIndices Array of collateral indices
61
- * @param pairIndices Array of pair indices
62
- * @param params Array of borrowing fee parameters
63
- * @param data Array of pair borrowing fee data
64
- * @param currentTimestamp Optional current timestamp
65
- * @returns Complete borrowing v2 context
66
- */
67
- export declare const createBorrowingV2ContextFromArrays: (collateralIndices: number[], pairIndices: number[], params: BorrowingFeeV2.BorrowingFeeParams[], data: BorrowingFeeV2.PairBorrowingFeeData[], currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
68
- /**
69
- * @dev Fetches borrowing v2 data for multiple collateral/pair combinations
70
- * @param contract GNSMultiCollatDiamond contract instance
71
- * @param collateralIndices Array of collateral indices
72
- * @param pairIndices Array of pair indices (must match collateralIndices length)
73
- * @returns Promise resolving to complete borrowing v2 context
74
- */
75
- export declare const fetchBorrowingV2DataForPairs: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<BorrowingFeeV2.GetBorrowingFeeV2Context>;
@@ -1,193 +0,0 @@
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
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = void 0;
13
- const converter_1 = require("../../../trade/fees/borrowingV2/converter");
14
- /**
15
- * @dev Fetches borrowing fee parameters v2 for specific pairs
16
- * @param contract GNSMultiCollatDiamond contract instance
17
- * @param collateralIndices Array of collateral indices
18
- * @param pairIndices Array of pair indices
19
- * @returns Promise resolving to array of borrowing fee parameters
20
- */
21
- const fetchBorrowingFeeParamsV2 = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
22
- if (collateralIndices.length !== pairIndices.length) {
23
- throw new Error("Collateral indices and pair indices arrays must have the same length");
24
- }
25
- try {
26
- const contractParams = yield contract.getPairBorrowingFeeParams(collateralIndices, pairIndices);
27
- return (0, converter_1.convertBorrowingFeeParamsArray)(contractParams);
28
- }
29
- catch (error) {
30
- console.error("Error fetching borrowing fee params v2:", error);
31
- throw error;
32
- }
33
- });
34
- exports.fetchBorrowingFeeParamsV2 = fetchBorrowingFeeParamsV2;
35
- /**
36
- * @dev Fetches pair borrowing fee data v2 for specific pairs
37
- * @param contract GNSMultiCollatDiamond contract instance
38
- * @param collateralIndices Array of collateral indices
39
- * @param pairIndices Array of pair indices
40
- * @returns Promise resolving to array of pair borrowing fee data
41
- */
42
- const fetchPairBorrowingFeeDataV2 = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
43
- if (collateralIndices.length !== pairIndices.length) {
44
- throw new Error("Collateral indices and pair indices arrays must have the same length");
45
- }
46
- try {
47
- const contractData = yield contract.getPairBorrowingFeeData(collateralIndices, pairIndices);
48
- return (0, converter_1.convertPairBorrowingFeeDataArray)(contractData);
49
- }
50
- catch (error) {
51
- console.error("Error fetching pair borrowing fee data v2:", error);
52
- throw error;
53
- }
54
- });
55
- exports.fetchPairBorrowingFeeDataV2 = fetchPairBorrowingFeeDataV2;
56
- /**
57
- * @dev Fetches borrowing fees in collateral tokens for a specific trade
58
- * @param contract GNSMultiCollatDiamond contract instance
59
- * @param trader Address of the trader
60
- * @param index Trade index
61
- * @param currentPairPrice Current price of the trading pair (1e6 precision)
62
- * @returns Promise resolving to borrowing fees in collateral tokens
63
- */
64
- const fetchTradeBorrowingFeesCollateralV2 = (contract, trader, index, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
65
- try {
66
- const feesCollateral = yield contract.getTradeBorrowingFeesCollateral(trader, index, currentPairPrice);
67
- // Convert BigNumber to normalized float
68
- // Note: Collateral precision varies by chain, but contract returns proper precision
69
- return parseFloat(feesCollateral.toString());
70
- }
71
- catch (error) {
72
- console.error("Error fetching trade borrowing fees collateral v2:", error);
73
- throw error;
74
- }
75
- });
76
- exports.fetchTradeBorrowingFeesCollateralV2 = fetchTradeBorrowingFeesCollateralV2;
77
- /**
78
- * @dev Fetches pending accumulated borrowing fees for a specific pair
79
- * @param contract GNSMultiCollatDiamond contract instance
80
- * @param collateralIndex Index of the collateral
81
- * @param pairIndex Index of the trading pair
82
- * @param currentPairPrice Current price of the trading pair (1e6 precision)
83
- * @returns Promise resolving to pending accumulated borrowing fee
84
- */
85
- const fetchPairPendingAccBorrowingFeesV2 = (contract, collateralIndex, pairIndex, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
86
- try {
87
- const accBorrowingFeeP = yield contract.getPairPendingAccBorrowingFees(collateralIndex, pairIndex, currentPairPrice);
88
- // Convert BigNumber to normalized float
89
- return parseFloat(accBorrowingFeeP.toString()) / 1e20;
90
- }
91
- catch (error) {
92
- console.error("Error fetching pair pending acc borrowing fees v2:", error);
93
- throw error;
94
- }
95
- });
96
- exports.fetchPairPendingAccBorrowingFeesV2 = fetchPairPendingAccBorrowingFeesV2;
97
- /**
98
- * @dev Convenience function to fetch all borrowing v2 data for specific pairs
99
- * @param contract GNSMultiCollatDiamond contract instance
100
- * @param collateralIndex Index of the collateral
101
- * @param pairIndices Array of pair indices
102
- * @returns Promise resolving to complete borrowing v2 data set
103
- */
104
- const fetchAllBorrowingV2Data = (contract, collateralIndex, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
105
- const collateralIndices = new Array(pairIndices.length).fill(collateralIndex);
106
- try {
107
- // Fetch both parameters and data in parallel
108
- const [params, data] = yield Promise.all([
109
- (0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
110
- (0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
111
- ]);
112
- // Create context from fetched data
113
- const context = (0, exports.createBorrowingV2ContextFromArrays)(collateralIndices, pairIndices, params, data);
114
- return { params, data, context };
115
- }
116
- catch (error) {
117
- console.error("Error fetching all borrowing v2 data:", error);
118
- throw error;
119
- }
120
- });
121
- exports.fetchAllBorrowingV2Data = fetchAllBorrowingV2Data;
122
- /**
123
- * @dev Creates a complete borrowing v2 context from contract data
124
- * @param contract GNSMultiCollatDiamond contract instance
125
- * @param collateralIndex Index of the collateral
126
- * @param pairIndices Array of pair indices
127
- * @param currentTimestamp Optional current timestamp for calculations
128
- * @returns Promise resolving to complete borrowing v2 context
129
- */
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
- });
134
- exports.createBorrowingV2ContextFromContract = createBorrowingV2ContextFromContract;
135
- /**
136
- * @dev Helper function to create context from already fetched arrays
137
- * @param collateralIndices Array of collateral indices
138
- * @param pairIndices Array of pair indices
139
- * @param params Array of borrowing fee parameters
140
- * @param data Array of pair borrowing fee data
141
- * @param currentTimestamp Optional current timestamp
142
- * @returns Complete borrowing v2 context
143
- */
144
- const createBorrowingV2ContextFromArrays = (collateralIndices, pairIndices, params, data, currentTimestamp) => {
145
- const context = {
146
- currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
147
- borrowingParams: {},
148
- borrowingData: {},
149
- };
150
- // Build nested objects indexed by collateralIndex and pairIndex
151
- for (let i = 0; i < collateralIndices.length; i++) {
152
- const collateralIndex = collateralIndices[i];
153
- 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];
164
- }
165
- return context;
166
- };
167
- exports.createBorrowingV2ContextFromArrays = createBorrowingV2ContextFromArrays;
168
- /**
169
- * @dev Fetches borrowing v2 data for multiple collateral/pair combinations
170
- * @param contract GNSMultiCollatDiamond contract instance
171
- * @param collateralIndices Array of collateral indices
172
- * @param pairIndices Array of pair indices (must match collateralIndices length)
173
- * @returns Promise resolving to complete borrowing v2 context
174
- */
175
- const fetchBorrowingV2DataForPairs = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
176
- if (collateralIndices.length !== pairIndices.length) {
177
- throw new Error("Collateral indices and pair indices arrays must have the same length");
178
- }
179
- try {
180
- // Fetch both parameters and data in parallel
181
- const [params, data] = yield Promise.all([
182
- (0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
183
- (0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
184
- ]);
185
- // Create and return context
186
- return (0, exports.createBorrowingV2ContextFromArrays)(collateralIndices, pairIndices, params, data);
187
- }
188
- catch (error) {
189
- console.error("Error fetching borrowing v2 data for pairs:", error);
190
- throw error;
191
- }
192
- });
193
- exports.fetchBorrowingV2DataForPairs = fetchBorrowingV2DataForPairs;