@gainsnetwork/sdk 0.0.0-v10.rc2 → 0.0.0-v10.rc3
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.
- package/lib/backend/globalTrades/index.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +2 -0
- package/lib/backend/index.js +18 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +312 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +30 -0
- package/lib/backend/tradingVariables/converter.js +329 -0
- package/lib/backend/tradingVariables/index.d.ts +3 -0
- package/lib/backend/tradingVariables/index.js +78 -0
- package/lib/backend/tradingVariables/types.d.ts +109 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +0 -1
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +7 -20
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +7 -16
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
- package/lib/contracts/types/generated/factories/GToken__factory.js +4 -0
- package/lib/contracts/utils/index.d.ts +0 -3
- package/lib/contracts/utils/index.js +0 -3
- package/lib/contracts/utils/openTrades.js +14 -30
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/markets/collateral/converter.d.ts +5 -0
- package/lib/markets/collateral/converter.js +11 -0
- package/lib/markets/collateral/index.d.ts +1 -0
- package/lib/markets/collateral/index.js +17 -0
- package/lib/markets/collateral/types.d.ts +7 -0
- package/lib/markets/collateral/types.js +2 -0
- package/lib/markets/index.d.ts +2 -0
- package/lib/markets/index.js +2 -0
- package/lib/markets/oi/converter.d.ts +63 -0
- package/lib/markets/oi/converter.js +103 -0
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/index.d.ts +10 -0
- package/lib/markets/oi/index.js +37 -0
- package/lib/markets/oi/types.d.ts +82 -0
- package/lib/markets/oi/types.js +6 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/trade/fees/borrowing/index.d.ts +13 -2
- package/lib/trade/fees/borrowing/index.js +30 -14
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/fetcher.js +193 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +1 -0
- package/lib/trade/fees/borrowingV2/index.js +1 -0
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +110 -0
- package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
- package/lib/trade/fees/fundingFees/fetcher.js +150 -0
- package/lib/trade/fees/fundingFees/index.d.ts +1 -0
- package/lib/trade/fees/fundingFees/index.js +4 -0
- package/lib/trade/fees/index.d.ts +2 -1
- package/lib/trade/fees/index.js +44 -35
- package/lib/trade/fees/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- package/lib/trade/fees/tiers/index.d.ts +1 -0
- package/lib/trade/fees/tiers/index.js +1 -0
- package/lib/trade/fees/trading/index.d.ts +22 -2
- package/lib/trade/fees/trading/index.js +44 -1
- package/lib/trade/fees/trading/types.d.ts +9 -0
- package/lib/trade/index.d.ts +1 -1
- package/lib/trade/index.js +1 -1
- package/lib/trade/liquidation/converter.d.ts +23 -0
- package/lib/trade/liquidation/converter.js +46 -0
- package/lib/trade/liquidation/index.d.ts +31 -0
- package/lib/trade/liquidation/index.js +187 -0
- package/lib/trade/liquidation/types.d.ts +44 -0
- package/lib/trade/liquidation/types.js +2 -0
- package/lib/trade/liquidation.d.ts +2 -32
- package/lib/trade/liquidation.js +8 -93
- package/lib/trade/pnl/converter.d.ts +47 -0
- package/lib/trade/pnl/converter.js +72 -0
- package/lib/trade/pnl/index.d.ts +86 -0
- package/lib/trade/pnl/index.js +201 -0
- package/lib/trade/pnl/types.d.ts +86 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/pnl.d.ts +3 -3
- package/lib/trade/pnl.js +2 -7
- package/lib/trade/priceImpact/close/index.d.ts +21 -0
- package/lib/trade/priceImpact/close/index.js +131 -0
- package/lib/trade/priceImpact/close/types.d.ts +43 -0
- package/lib/trade/priceImpact/close/types.js +5 -0
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
- package/lib/trade/priceImpact/cumulVol/index.d.ts +107 -0
- package/lib/trade/priceImpact/cumulVol/index.js +228 -0
- package/lib/trade/priceImpact/index.d.ts +6 -2
- package/lib/trade/priceImpact/index.js +30 -3
- package/lib/trade/priceImpact/open/index.d.ts +22 -0
- package/lib/trade/priceImpact/open/index.js +76 -0
- package/lib/trade/priceImpact/open/types.d.ts +41 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +63 -0
- package/lib/trade/priceImpact/skew/fetcher.js +168 -0
- package/lib/trade/priceImpact/skew/index.d.ts +1 -0
- package/lib/trade/priceImpact/skew/index.js +4 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +9 -7
- package/package.json +2 -2
|
@@ -30,11 +30,12 @@ exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = ex
|
|
|
30
30
|
/**
|
|
31
31
|
* @dev Calculates borrowing fees using v1 model (block-based with groups)
|
|
32
32
|
* @dev Still actively used by markets that haven't migrated to v2
|
|
33
|
+
* @dev Uses dynamic collateral OI - converts OI to USD for fee calculations
|
|
33
34
|
* @param posDai Position size in collateral
|
|
34
35
|
* @param pairIndex Trading pair index
|
|
35
36
|
* @param long Whether position is long
|
|
36
37
|
* @param initialAccFees Initial accumulated fees when trade was opened
|
|
37
|
-
* @param context Context with current block and
|
|
38
|
+
* @param context Context with current block, fee data, and collateral price
|
|
38
39
|
* @returns Borrowing fee in collateral tokens
|
|
39
40
|
*/
|
|
40
41
|
const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
@@ -52,6 +53,7 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
52
53
|
? getPairPendingAccFee(pairIndex, context.currentBlock, long, {
|
|
53
54
|
pairs,
|
|
54
55
|
openInterest,
|
|
56
|
+
collateralPriceUsd: context.collateralPriceUsd,
|
|
55
57
|
})
|
|
56
58
|
: long
|
|
57
59
|
? firstPairGroup.pairAccFeeLong
|
|
@@ -67,6 +69,11 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
67
69
|
return (posDai * fee) / 100;
|
|
68
70
|
};
|
|
69
71
|
exports.getBorrowingFee = getBorrowingFee;
|
|
72
|
+
/**
|
|
73
|
+
* @deprecated Use withinMaxGroupOiDynamic from @gains-network/sdk/markets/oi instead
|
|
74
|
+
* @dev This function uses static OI which doesn't reflect current market values
|
|
75
|
+
* @dev The v10 contracts use dynamic OI (beforeV10 + afterV10Token * currentPrice)
|
|
76
|
+
*/
|
|
70
77
|
const withinMaxGroupOi = (pairIndex, long, positionSizeCollateral, context) => {
|
|
71
78
|
const { groups, pairs } = context;
|
|
72
79
|
if (!groups || !pairs) {
|
|
@@ -85,18 +92,19 @@ const getPairGroupIndex = (pairIndex, context) => {
|
|
|
85
92
|
return pairGroups.length == 0 ? 0 : pairGroups[0].groupIndex;
|
|
86
93
|
};
|
|
87
94
|
const getPairPendingAccFees = (pairIndex, currentBlock, context) => {
|
|
88
|
-
const { pairs, openInterest: { long, short }, } = context;
|
|
95
|
+
const { pairs, openInterest: { long, short }, collateralPriceUsd, } = context;
|
|
89
96
|
const pair = pairs[pairIndex];
|
|
90
|
-
return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock, pair.oi.max, pair.feeExponent, pair.feePerBlockCap);
|
|
97
|
+
return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock, pair.oi.max, pair.feeExponent, pair.feePerBlockCap, collateralPriceUsd);
|
|
91
98
|
};
|
|
92
99
|
const getPairPendingAccFee = (pairIndex, currentBlock, long, context) => {
|
|
93
100
|
const { accFeeLong, accFeeShort } = getPairPendingAccFees(pairIndex, currentBlock, context);
|
|
94
101
|
return long ? accFeeLong : accFeeShort;
|
|
95
102
|
};
|
|
96
103
|
const getGroupPendingAccFees = (groupIndex, currentBlock, context) => {
|
|
97
|
-
const { groups } = context;
|
|
104
|
+
const { groups, collateralPriceUsd } = context;
|
|
98
105
|
const group = groups[groupIndex];
|
|
99
|
-
return getPendingAccFees(group.accFeeLong, group.accFeeShort, group.oi.long, group.oi.short, group.feePerBlock, currentBlock, group.accLastUpdatedBlock, group.oi.max, group.feeExponent
|
|
106
|
+
return getPendingAccFees(group.accFeeLong, group.accFeeShort, group.oi.long, group.oi.short, group.feePerBlock, currentBlock, group.accLastUpdatedBlock, group.oi.max, group.feeExponent, undefined, // no fee caps for groups
|
|
107
|
+
collateralPriceUsd);
|
|
100
108
|
};
|
|
101
109
|
const getGroupPendingAccFee = (groupIndex, currentBlock, long, context) => {
|
|
102
110
|
const { accFeeLong, accFeeShort } = getGroupPendingAccFees(groupIndex, currentBlock, context);
|
|
@@ -107,14 +115,16 @@ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long,
|
|
|
107
115
|
const beforeTradeOpen = group.block < initialFees.block;
|
|
108
116
|
let deltaGroup, deltaPair;
|
|
109
117
|
if (i == pairGroups.length - 1) {
|
|
110
|
-
const { currentBlock, groups, pairs } = context;
|
|
118
|
+
const { currentBlock, groups, pairs, collateralPriceUsd } = context;
|
|
111
119
|
const openInterest = pairs[pairIndex].oi;
|
|
112
120
|
deltaGroup = getGroupPendingAccFee(group.groupIndex, currentBlock, long, {
|
|
113
121
|
groups,
|
|
122
|
+
collateralPriceUsd,
|
|
114
123
|
});
|
|
115
124
|
deltaPair = getPairPendingAccFee(pairIndex, currentBlock, long, {
|
|
116
125
|
pairs,
|
|
117
126
|
openInterest,
|
|
127
|
+
collateralPriceUsd,
|
|
118
128
|
});
|
|
119
129
|
}
|
|
120
130
|
else {
|
|
@@ -137,8 +147,8 @@ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long,
|
|
|
137
147
|
}
|
|
138
148
|
return { deltaGroup, deltaPair, beforeTradeOpen };
|
|
139
149
|
};
|
|
140
|
-
const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock, currentBlock, accLastUpdatedBlock, maxOi, feeExponent, feeCaps // as percentage: eg minP: 0.1 = 10%, maxP: 0.5 = 50%
|
|
141
|
-
) => {
|
|
150
|
+
const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock, currentBlock, accLastUpdatedBlock, maxOi, feeExponent, feeCaps, // as percentage: eg minP: 0.1 = 10%, maxP: 0.5 = 50%
|
|
151
|
+
collateralPriceUsd) => {
|
|
142
152
|
const moreShorts = oiLong < oiShort;
|
|
143
153
|
const blockDistance = currentBlock > accLastUpdatedBlock ? currentBlock - accLastUpdatedBlock : 0;
|
|
144
154
|
// If block distance is zero nothing changes
|
|
@@ -150,19 +160,25 @@ const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock
|
|
|
150
160
|
deltaShort: 0,
|
|
151
161
|
};
|
|
152
162
|
}
|
|
153
|
-
|
|
154
|
-
|
|
163
|
+
// Convert OI to USD if collateral price is provided (dynamic collateral OI)
|
|
164
|
+
const oiLongUsd = collateralPriceUsd ? oiLong * collateralPriceUsd : oiLong;
|
|
165
|
+
const oiShortUsd = collateralPriceUsd
|
|
166
|
+
? oiShort * collateralPriceUsd
|
|
167
|
+
: oiShort;
|
|
168
|
+
const maxOiUsd = collateralPriceUsd ? maxOi * collateralPriceUsd : maxOi;
|
|
169
|
+
const netOi = Math.abs(oiLongUsd - oiShortUsd);
|
|
170
|
+
// Calculate minimum and maximum effective oi (using USD values if available)
|
|
155
171
|
const { minP, maxP } = getFeePerBlockCaps(feeCaps);
|
|
156
|
-
const minNetOi =
|
|
157
|
-
const maxNetOi =
|
|
172
|
+
const minNetOi = maxOiUsd * minP;
|
|
173
|
+
const maxNetOi = maxOiUsd * maxP;
|
|
158
174
|
// Calculate the minimum acc fee delta (applies to both sides)
|
|
159
175
|
const minDelta = minNetOi > 0
|
|
160
|
-
? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi,
|
|
176
|
+
? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi, maxOiUsd, feeExponent)
|
|
161
177
|
: 0;
|
|
162
178
|
// Calculate the actual acc fee (using capped oi of 100% or less)
|
|
163
179
|
const delta = netOi > minNetOi
|
|
164
180
|
? getPendingAccFeesDelta(blockDistance, feePerBlock, Math.min(netOi, maxNetOi), // if netOi > cap, use cap
|
|
165
|
-
|
|
181
|
+
maxOiUsd, feeExponent)
|
|
166
182
|
: minDelta;
|
|
167
183
|
const [deltaLong, deltaShort] = moreShorts
|
|
168
184
|
? [minDelta, delta]
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { GNSMultiCollatDiamond } from "../../../contracts/types/generated";
|
|
2
|
+
import { BorrowingFeeV2 } from ".";
|
|
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>;
|
|
@@ -0,0 +1,193 @@
|
|
|
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("./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;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Converters for fee data between contract and SDK formats
|
|
3
|
+
*/
|
|
4
|
+
import { IFundingFees } from "../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
5
|
+
import { TradeFeesData, UiRealizedPnlData } from "../types";
|
|
6
|
+
import { CollateralConfig } from "../../markets/collateral/types";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Converts contract TradeFeesData to SDK format
|
|
9
|
+
* @param data Trade fees data from contract
|
|
10
|
+
* @param collateralConfig Config for the collateral (contains decimals)
|
|
11
|
+
* @returns Normalized trade fees data
|
|
12
|
+
*/
|
|
13
|
+
export declare const convertTradeFeesData: (data: IFundingFees.TradeFeesDataStructOutput, collateralConfig: CollateralConfig) => TradeFeesData;
|
|
14
|
+
/**
|
|
15
|
+
* @dev Converts array of TradeFeesData from contract
|
|
16
|
+
* @param dataArray Array of trade fees data
|
|
17
|
+
* @param collateralConfig Config for the collateral
|
|
18
|
+
* @returns Array of normalized trade fees data
|
|
19
|
+
*/
|
|
20
|
+
export declare const convertTradeFeesDataArray: (dataArray: IFundingFees.TradeFeesDataStructOutput[], collateralConfig: CollateralConfig) => TradeFeesData[];
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts contract UiRealizedPnlData to SDK format
|
|
23
|
+
* @param data UI realized PnL data from contract
|
|
24
|
+
* @param collateralConfig Config for the collateral (contains decimals)
|
|
25
|
+
* @returns Normalized UI realized PnL data
|
|
26
|
+
*/
|
|
27
|
+
export declare const convertUiRealizedPnlData: (data: IFundingFees.UiRealizedPnlDataStructOutput, collateralConfig: CollateralConfig) => UiRealizedPnlData;
|
|
28
|
+
/**
|
|
29
|
+
* @dev Converts array of UiRealizedPnlData from contract
|
|
30
|
+
* @param dataArray Array of UI realized PnL data
|
|
31
|
+
* @param collateralConfig Config for the collateral
|
|
32
|
+
* @returns Array of normalized UI realized PnL data
|
|
33
|
+
*/
|
|
34
|
+
export declare const convertUiRealizedPnlDataArray: (dataArray: IFundingFees.UiRealizedPnlDataStructOutput[], collateralConfig: CollateralConfig) => UiRealizedPnlData[];
|
|
35
|
+
/**
|
|
36
|
+
* @dev Converts TradeFeesData to contract format (for encoding)
|
|
37
|
+
* @param data SDK trade fees data
|
|
38
|
+
* @param collateralConfig Config for the collateral
|
|
39
|
+
* @returns Contract-formatted trade fees data
|
|
40
|
+
*/
|
|
41
|
+
export declare const encodeTradeFeesData: (data: TradeFeesData, collateralConfig: CollateralConfig) => IFundingFees.TradeFeesDataStruct;
|
|
42
|
+
/**
|
|
43
|
+
* @dev Converts UiRealizedPnlData to contract format (for encoding)
|
|
44
|
+
* @param data SDK UI realized PnL data
|
|
45
|
+
* @param collateralConfig Config for the collateral
|
|
46
|
+
* @returns Contract-formatted UI realized PnL data
|
|
47
|
+
*/
|
|
48
|
+
export declare const encodeUiRealizedPnlData: (data: UiRealizedPnlData, collateralConfig: CollateralConfig) => IFundingFees.UiRealizedPnlDataStruct;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Converters for fee data between contract and SDK formats
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.encodeUiRealizedPnlData = exports.encodeTradeFeesData = exports.convertUiRealizedPnlDataArray = exports.convertUiRealizedPnlData = exports.convertTradeFeesDataArray = exports.convertTradeFeesData = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Converts contract TradeFeesData to SDK format
|
|
9
|
+
* @param data Trade fees data from contract
|
|
10
|
+
* @param collateralConfig Config for the collateral (contains decimals)
|
|
11
|
+
* @returns Normalized trade fees data
|
|
12
|
+
*/
|
|
13
|
+
const convertTradeFeesData = (data, collateralConfig) => {
|
|
14
|
+
const decimals = collateralConfig.decimals || 18;
|
|
15
|
+
return {
|
|
16
|
+
realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
|
|
17
|
+
Math.pow(10, decimals),
|
|
18
|
+
realizedPnlCollateral: parseFloat(data.realizedPnlCollateral.toString()) / Math.pow(10, decimals),
|
|
19
|
+
manuallyRealizedNegativePnlCollateral: parseFloat(data.manuallyRealizedNegativePnlCollateral.toString()) /
|
|
20
|
+
Math.pow(10, decimals),
|
|
21
|
+
alreadyTransferredNegativePnlCollateral: parseFloat(data.alreadyTransferredNegativePnlCollateral.toString()) /
|
|
22
|
+
Math.pow(10, decimals),
|
|
23
|
+
virtualAvailableCollateralInDiamond: parseFloat(data.virtualAvailableCollateralInDiamond.toString()) /
|
|
24
|
+
Math.pow(10, decimals),
|
|
25
|
+
initialAccFundingFeeP: parseFloat(data.initialAccFundingFeeP.toString()) / 1e10,
|
|
26
|
+
initialAccBorrowingFeeP: parseFloat(data.initialAccBorrowingFeeP.toString()) / 1e10,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
exports.convertTradeFeesData = convertTradeFeesData;
|
|
30
|
+
/**
|
|
31
|
+
* @dev Converts array of TradeFeesData from contract
|
|
32
|
+
* @param dataArray Array of trade fees data
|
|
33
|
+
* @param collateralConfig Config for the collateral
|
|
34
|
+
* @returns Array of normalized trade fees data
|
|
35
|
+
*/
|
|
36
|
+
const convertTradeFeesDataArray = (dataArray, collateralConfig) => {
|
|
37
|
+
return dataArray.map(data => (0, exports.convertTradeFeesData)(data, collateralConfig));
|
|
38
|
+
};
|
|
39
|
+
exports.convertTradeFeesDataArray = convertTradeFeesDataArray;
|
|
40
|
+
/**
|
|
41
|
+
* @dev Converts contract UiRealizedPnlData to SDK format
|
|
42
|
+
* @param data UI realized PnL data from contract
|
|
43
|
+
* @param collateralConfig Config for the collateral (contains decimals)
|
|
44
|
+
* @returns Normalized UI realized PnL data
|
|
45
|
+
*/
|
|
46
|
+
const convertUiRealizedPnlData = (data, collateralConfig) => {
|
|
47
|
+
const decimals = collateralConfig.decimals || 18;
|
|
48
|
+
return {
|
|
49
|
+
realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
|
|
50
|
+
Math.pow(10, decimals),
|
|
51
|
+
realizedOldBorrowingFeesCollateral: parseFloat(data.realizedOldBorrowingFeesCollateral.toString()) /
|
|
52
|
+
Math.pow(10, decimals),
|
|
53
|
+
realizedNewBorrowingFeesCollateral: parseFloat(data.realizedNewBorrowingFeesCollateral.toString()) /
|
|
54
|
+
Math.pow(10, decimals),
|
|
55
|
+
realizedFundingFeesCollateral: parseFloat(data.realizedFundingFeesCollateral.toString()) /
|
|
56
|
+
Math.pow(10, decimals),
|
|
57
|
+
realizedPnlPartialCloseCollateral: parseFloat(data.realizedPnlPartialCloseCollateral.toString()) /
|
|
58
|
+
Math.pow(10, decimals),
|
|
59
|
+
pnlWithdrawnCollateral: parseFloat(data.pnlWithdrawnCollateral.toString()) / Math.pow(10, decimals),
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
exports.convertUiRealizedPnlData = convertUiRealizedPnlData;
|
|
63
|
+
/**
|
|
64
|
+
* @dev Converts array of UiRealizedPnlData from contract
|
|
65
|
+
* @param dataArray Array of UI realized PnL data
|
|
66
|
+
* @param collateralConfig Config for the collateral
|
|
67
|
+
* @returns Array of normalized UI realized PnL data
|
|
68
|
+
*/
|
|
69
|
+
const convertUiRealizedPnlDataArray = (dataArray, collateralConfig) => {
|
|
70
|
+
return dataArray.map(data => (0, exports.convertUiRealizedPnlData)(data, collateralConfig));
|
|
71
|
+
};
|
|
72
|
+
exports.convertUiRealizedPnlDataArray = convertUiRealizedPnlDataArray;
|
|
73
|
+
/**
|
|
74
|
+
* @dev Converts TradeFeesData to contract format (for encoding)
|
|
75
|
+
* @param data SDK trade fees data
|
|
76
|
+
* @param collateralConfig Config for the collateral
|
|
77
|
+
* @returns Contract-formatted trade fees data
|
|
78
|
+
*/
|
|
79
|
+
const encodeTradeFeesData = (data, collateralConfig) => {
|
|
80
|
+
const decimals = collateralConfig.decimals || 18;
|
|
81
|
+
return {
|
|
82
|
+
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * Math.pow(10, decimals)),
|
|
83
|
+
realizedPnlCollateral: Math.round(data.realizedPnlCollateral * Math.pow(10, decimals)),
|
|
84
|
+
manuallyRealizedNegativePnlCollateral: Math.round(data.manuallyRealizedNegativePnlCollateral * Math.pow(10, decimals)),
|
|
85
|
+
alreadyTransferredNegativePnlCollateral: Math.round(data.alreadyTransferredNegativePnlCollateral * Math.pow(10, decimals)),
|
|
86
|
+
virtualAvailableCollateralInDiamond: Math.round(data.virtualAvailableCollateralInDiamond * Math.pow(10, decimals)),
|
|
87
|
+
__placeholder: 0,
|
|
88
|
+
initialAccFundingFeeP: Math.round(data.initialAccFundingFeeP * 1e10),
|
|
89
|
+
initialAccBorrowingFeeP: Math.round(data.initialAccBorrowingFeeP * 1e10),
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
exports.encodeTradeFeesData = encodeTradeFeesData;
|
|
93
|
+
/**
|
|
94
|
+
* @dev Converts UiRealizedPnlData to contract format (for encoding)
|
|
95
|
+
* @param data SDK UI realized PnL data
|
|
96
|
+
* @param collateralConfig Config for the collateral
|
|
97
|
+
* @returns Contract-formatted UI realized PnL data
|
|
98
|
+
*/
|
|
99
|
+
const encodeUiRealizedPnlData = (data, collateralConfig) => {
|
|
100
|
+
const decimals = collateralConfig.decimals || 18;
|
|
101
|
+
return {
|
|
102
|
+
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * Math.pow(10, decimals)),
|
|
103
|
+
realizedOldBorrowingFeesCollateral: Math.round(data.realizedOldBorrowingFeesCollateral * Math.pow(10, decimals)),
|
|
104
|
+
realizedNewBorrowingFeesCollateral: Math.round(data.realizedNewBorrowingFeesCollateral * Math.pow(10, decimals)),
|
|
105
|
+
realizedFundingFeesCollateral: Math.round(data.realizedFundingFeesCollateral * Math.pow(10, decimals)),
|
|
106
|
+
realizedPnlPartialCloseCollateral: Math.round(data.realizedPnlPartialCloseCollateral * Math.pow(10, decimals)),
|
|
107
|
+
pnlWithdrawnCollateral: Math.round(data.pnlWithdrawnCollateral * Math.pow(10, decimals)),
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
exports.encodeUiRealizedPnlData = encodeUiRealizedPnlData;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { GNSMultiCollatDiamond } from "../../../contracts/types/generated";
|
|
2
|
+
import type { TradeInitialAccFees } from ".";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Fetches pending accumulated funding fees for a specific pair
|
|
5
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
6
|
+
* @param collateralIndex Collateral index
|
|
7
|
+
* @param pairIndex Pair index
|
|
8
|
+
* @param currentPairPrice Current pair price (1e10)
|
|
9
|
+
* @returns Promise resolving to accumulated funding fees and current rate
|
|
10
|
+
*/
|
|
11
|
+
export declare const fetchPairPendingAccFundingFees: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, currentPairPrice: bigint) => Promise<{
|
|
12
|
+
accFundingFeeLongP: number;
|
|
13
|
+
accFundingFeeShortP: number;
|
|
14
|
+
currentFundingRatePerSecondP: number;
|
|
15
|
+
}>;
|
|
16
|
+
/**
|
|
17
|
+
* @dev Fetches funding fees for a specific trade in collateral tokens
|
|
18
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
19
|
+
* @param trader Trader address
|
|
20
|
+
* @param index Trade index
|
|
21
|
+
* @param currentPairPrice Current pair price (1e10)
|
|
22
|
+
* @returns Promise resolving to funding fee in collateral tokens
|
|
23
|
+
*/
|
|
24
|
+
export declare const fetchTradeFundingFeesCollateral: (contract: GNSMultiCollatDiamond, trader: string, index: number, currentPairPrice: bigint) => Promise<number>;
|
|
25
|
+
/**
|
|
26
|
+
* @dev Fetches trade fees data for a specific trade
|
|
27
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
28
|
+
* @param trader Trader address
|
|
29
|
+
* @param index Trade index
|
|
30
|
+
* @returns Promise resolving to trade fees data
|
|
31
|
+
*/
|
|
32
|
+
export declare const fetchTradeFeesData: (contract: GNSMultiCollatDiamond, trader: string, index: number) => Promise<TradeInitialAccFees>;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Fetches trade fees data for multiple trades
|
|
35
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
36
|
+
* @param traders Array of trader addresses
|
|
37
|
+
* @param indices Array of trade indices
|
|
38
|
+
* @returns Promise resolving to array of trade fees data
|
|
39
|
+
*/
|
|
40
|
+
export declare const fetchTradeFeesDataBatch: (contract: GNSMultiCollatDiamond, traders: string[], indices: number[]) => Promise<TradeInitialAccFees[]>;
|
|
41
|
+
/**
|
|
42
|
+
* @dev Fetches pending accumulated funding fees for multiple pairs
|
|
43
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
44
|
+
* @param collateralIndices Array of collateral indices
|
|
45
|
+
* @param pairIndices Array of pair indices
|
|
46
|
+
* @param currentPairPrices Array of current pair prices (1e10)
|
|
47
|
+
* @returns Promise resolving to array of accumulated funding fees
|
|
48
|
+
*/
|
|
49
|
+
export declare const fetchPairPendingAccFundingFeesBatch: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[], currentPairPrices: bigint[]) => Promise<Array<{
|
|
50
|
+
accFundingFeeLongP: number;
|
|
51
|
+
accFundingFeeShortP: number;
|
|
52
|
+
currentFundingRatePerSecondP: number;
|
|
53
|
+
}>>;
|
|
54
|
+
/**
|
|
55
|
+
* @dev Helper to convert price from number to contract format
|
|
56
|
+
* @param price Price as number
|
|
57
|
+
* @returns Price in contract format (1e10)
|
|
58
|
+
*/
|
|
59
|
+
export declare const priceToContractFormat: (price: number) => bigint;
|
|
60
|
+
/**
|
|
61
|
+
* @dev Helper to convert collateral amount to contract format
|
|
62
|
+
* @param amount Amount as number
|
|
63
|
+
* @param decimals Collateral decimals (6 for USDC, 18 for others)
|
|
64
|
+
* @returns Amount in contract format
|
|
65
|
+
*/
|
|
66
|
+
export declare const collateralToContractFormat: (amount: number, decimals: number) => bigint;
|