@gainsnetwork/sdk 0.0.0-v10.rc1 → 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/constants.d.ts +8 -1
- package/lib/constants.js +8 -1
- package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +75 -0
- package/lib/contracts/fetch/fees/borrowingFeesV2.js +193 -0
- package/lib/contracts/fetch/fees/fundingFees.d.ts +66 -0
- package/lib/contracts/fetch/fees/fundingFees.js +150 -0
- package/lib/contracts/fetch/priceImpact/skew.d.ts +63 -0
- package/lib/contracts/fetch/priceImpact/skew.js +168 -0
- 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/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/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/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/openTrades.d.ts +1 -0
- package/lib/contracts/utils/openTrades.js +94 -56
- package/lib/contracts/utils/pairs.js +7 -0
- 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 +22 -1
- package/lib/trade/fees/borrowing/index.js +39 -13
- package/lib/trade/fees/borrowingV2/converter.d.ts +66 -0
- package/lib/trade/fees/borrowingV2/converter.js +121 -0
- 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 +60 -0
- package/lib/trade/fees/borrowingV2/index.js +140 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +79 -0
- package/lib/trade/fees/borrowingV2/types.js +5 -0
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +110 -0
- package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
- package/lib/trade/fees/fundingFees/converter.js +196 -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 +136 -0
- package/lib/trade/fees/fundingFees/index.js +326 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +7 -2
- package/lib/trade/fees/index.js +67 -16
- 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 +18 -0
- package/lib/trade/fees/tiers/index.js +45 -1
- package/lib/trade/fees/trading/converter.d.ts +30 -0
- package/lib/trade/fees/trading/converter.js +43 -0
- package/lib/trade/fees/trading/index.d.ts +54 -0
- package/lib/trade/fees/trading/index.js +147 -0
- package/lib/trade/fees/trading/types.d.ts +48 -0
- package/lib/trade/fees/trading/types.js +5 -0
- package/lib/trade/index.d.ts +3 -2
- package/lib/trade/index.js +3 -2
- 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/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/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 +12 -0
- package/lib/trade/priceImpact/index.js +59 -0
- 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/converter.d.ts +77 -0
- package/lib/trade/priceImpact/skew/converter.js +171 -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 +58 -0
- package/lib/trade/priceImpact/skew/index.js +179 -0
- package/lib/trade/priceImpact/skew/types.d.ts +55 -0
- package/lib/trade/priceImpact/skew/types.js +5 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +34 -9
- package/lib/trade/types.js +7 -0
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/package.json +2 -1
|
@@ -27,6 +27,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = exports.getBorrowingFee = void 0;
|
|
30
|
+
/**
|
|
31
|
+
* @dev Calculates borrowing fees using v1 model (block-based with groups)
|
|
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
|
|
34
|
+
* @param posDai Position size in collateral
|
|
35
|
+
* @param pairIndex Trading pair index
|
|
36
|
+
* @param long Whether position is long
|
|
37
|
+
* @param initialAccFees Initial accumulated fees when trade was opened
|
|
38
|
+
* @param context Context with current block, fee data, and collateral price
|
|
39
|
+
* @returns Borrowing fee in collateral tokens
|
|
40
|
+
*/
|
|
30
41
|
const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
31
42
|
if (!context.groups || !context.pairs || !context.pairs[pairIndex]) {
|
|
32
43
|
return 0;
|
|
@@ -42,6 +53,7 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
42
53
|
? getPairPendingAccFee(pairIndex, context.currentBlock, long, {
|
|
43
54
|
pairs,
|
|
44
55
|
openInterest,
|
|
56
|
+
collateralPriceUsd: context.collateralPriceUsd,
|
|
45
57
|
})
|
|
46
58
|
: long
|
|
47
59
|
? firstPairGroup.pairAccFeeLong
|
|
@@ -57,6 +69,11 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
57
69
|
return (posDai * fee) / 100;
|
|
58
70
|
};
|
|
59
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
|
+
*/
|
|
60
77
|
const withinMaxGroupOi = (pairIndex, long, positionSizeCollateral, context) => {
|
|
61
78
|
const { groups, pairs } = context;
|
|
62
79
|
if (!groups || !pairs) {
|
|
@@ -75,18 +92,19 @@ const getPairGroupIndex = (pairIndex, context) => {
|
|
|
75
92
|
return pairGroups.length == 0 ? 0 : pairGroups[0].groupIndex;
|
|
76
93
|
};
|
|
77
94
|
const getPairPendingAccFees = (pairIndex, currentBlock, context) => {
|
|
78
|
-
const { pairs, openInterest: { long, short }, } = context;
|
|
95
|
+
const { pairs, openInterest: { long, short }, collateralPriceUsd, } = context;
|
|
79
96
|
const pair = pairs[pairIndex];
|
|
80
|
-
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);
|
|
81
98
|
};
|
|
82
99
|
const getPairPendingAccFee = (pairIndex, currentBlock, long, context) => {
|
|
83
100
|
const { accFeeLong, accFeeShort } = getPairPendingAccFees(pairIndex, currentBlock, context);
|
|
84
101
|
return long ? accFeeLong : accFeeShort;
|
|
85
102
|
};
|
|
86
103
|
const getGroupPendingAccFees = (groupIndex, currentBlock, context) => {
|
|
87
|
-
const { groups } = context;
|
|
104
|
+
const { groups, collateralPriceUsd } = context;
|
|
88
105
|
const group = groups[groupIndex];
|
|
89
|
-
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);
|
|
90
108
|
};
|
|
91
109
|
const getGroupPendingAccFee = (groupIndex, currentBlock, long, context) => {
|
|
92
110
|
const { accFeeLong, accFeeShort } = getGroupPendingAccFees(groupIndex, currentBlock, context);
|
|
@@ -97,14 +115,16 @@ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long,
|
|
|
97
115
|
const beforeTradeOpen = group.block < initialFees.block;
|
|
98
116
|
let deltaGroup, deltaPair;
|
|
99
117
|
if (i == pairGroups.length - 1) {
|
|
100
|
-
const { currentBlock, groups, pairs } = context;
|
|
118
|
+
const { currentBlock, groups, pairs, collateralPriceUsd } = context;
|
|
101
119
|
const openInterest = pairs[pairIndex].oi;
|
|
102
120
|
deltaGroup = getGroupPendingAccFee(group.groupIndex, currentBlock, long, {
|
|
103
121
|
groups,
|
|
122
|
+
collateralPriceUsd,
|
|
104
123
|
});
|
|
105
124
|
deltaPair = getPairPendingAccFee(pairIndex, currentBlock, long, {
|
|
106
125
|
pairs,
|
|
107
126
|
openInterest,
|
|
127
|
+
collateralPriceUsd,
|
|
108
128
|
});
|
|
109
129
|
}
|
|
110
130
|
else {
|
|
@@ -127,8 +147,8 @@ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long,
|
|
|
127
147
|
}
|
|
128
148
|
return { deltaGroup, deltaPair, beforeTradeOpen };
|
|
129
149
|
};
|
|
130
|
-
const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock, currentBlock, accLastUpdatedBlock, maxOi, feeExponent, feeCaps // as percentage: eg minP: 0.1 = 10%, maxP: 0.5 = 50%
|
|
131
|
-
) => {
|
|
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) => {
|
|
132
152
|
const moreShorts = oiLong < oiShort;
|
|
133
153
|
const blockDistance = currentBlock > accLastUpdatedBlock ? currentBlock - accLastUpdatedBlock : 0;
|
|
134
154
|
// If block distance is zero nothing changes
|
|
@@ -140,19 +160,25 @@ const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock
|
|
|
140
160
|
deltaShort: 0,
|
|
141
161
|
};
|
|
142
162
|
}
|
|
143
|
-
|
|
144
|
-
|
|
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)
|
|
145
171
|
const { minP, maxP } = getFeePerBlockCaps(feeCaps);
|
|
146
|
-
const minNetOi =
|
|
147
|
-
const maxNetOi =
|
|
172
|
+
const minNetOi = maxOiUsd * minP;
|
|
173
|
+
const maxNetOi = maxOiUsd * maxP;
|
|
148
174
|
// Calculate the minimum acc fee delta (applies to both sides)
|
|
149
175
|
const minDelta = minNetOi > 0
|
|
150
|
-
? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi,
|
|
176
|
+
? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi, maxOiUsd, feeExponent)
|
|
151
177
|
: 0;
|
|
152
178
|
// Calculate the actual acc fee (using capped oi of 100% or less)
|
|
153
179
|
const delta = netOi > minNetOi
|
|
154
180
|
? getPendingAccFeesDelta(blockDistance, feePerBlock, Math.min(netOi, maxNetOi), // if netOi > cap, use cap
|
|
155
|
-
|
|
181
|
+
maxOiUsd, feeExponent)
|
|
156
182
|
: minDelta;
|
|
157
183
|
const [deltaLong, deltaShort] = moreShorts
|
|
158
184
|
? [minDelta, delta]
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { IFundingFees } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
2
|
+
import { BorrowingFeeV2 } from ".";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Converts contract BorrowingFeeParams to SDK type
|
|
5
|
+
* @param contractParams Contract borrowing fee params from IFundingFees.BorrowingFeeParams
|
|
6
|
+
* @returns SDK BorrowingFeeParams
|
|
7
|
+
*/
|
|
8
|
+
export declare const convertBorrowingFeeParams: (contractParams: IFundingFees.BorrowingFeeParamsStructOutput) => BorrowingFeeV2.BorrowingFeeParams;
|
|
9
|
+
/**
|
|
10
|
+
* @dev Converts array of contract BorrowingFeeParams to SDK types
|
|
11
|
+
* @param contractParamsArray Array of contract borrowing fee params
|
|
12
|
+
* @returns Array of SDK BorrowingFeeParams
|
|
13
|
+
*/
|
|
14
|
+
export declare const convertBorrowingFeeParamsArray: (contractParamsArray: IFundingFees.BorrowingFeeParamsStructOutput[]) => BorrowingFeeV2.BorrowingFeeParams[];
|
|
15
|
+
/**
|
|
16
|
+
* @dev Converts contract PairBorrowingFeeData to SDK type
|
|
17
|
+
* @param contractData Contract pair borrowing fee data from IFundingFees.PairBorrowingFeeData
|
|
18
|
+
* @returns SDK PairBorrowingFeeData
|
|
19
|
+
*/
|
|
20
|
+
export declare const convertPairBorrowingFeeData: (contractData: IFundingFees.PairBorrowingFeeDataStructOutput) => BorrowingFeeV2.PairBorrowingFeeData;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts array of contract PairBorrowingFeeData to SDK types
|
|
23
|
+
* @param contractDataArray Array of contract pair borrowing fee data
|
|
24
|
+
* @returns Array of SDK PairBorrowingFeeData
|
|
25
|
+
*/
|
|
26
|
+
export declare const convertPairBorrowingFeeDataArray: (contractDataArray: IFundingFees.PairBorrowingFeeDataStructOutput[]) => BorrowingFeeV2.PairBorrowingFeeData[];
|
|
27
|
+
/**
|
|
28
|
+
* @dev Converts contract TradeFeesData to SDK TradeInitialAccFees
|
|
29
|
+
* @param contractTradeData Contract trade fees data from IFundingFees.TradeFeesData
|
|
30
|
+
* @returns SDK TradeInitialAccFees
|
|
31
|
+
*/
|
|
32
|
+
export declare const convertTradeInitialAccFees: (contractTradeData: IFundingFees.TradeFeesDataStructOutput) => BorrowingFeeV2.TradeInitialAccFees;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Converts array of contract TradeFeesData to SDK TradeInitialAccFees
|
|
35
|
+
* @param contractTradeDataArray Array of contract trade fees data
|
|
36
|
+
* @returns Array of SDK TradeInitialAccFees
|
|
37
|
+
*/
|
|
38
|
+
export declare const convertTradeInitialAccFeesArray: (contractTradeDataArray: IFundingFees.TradeFeesDataStructOutput[]) => BorrowingFeeV2.TradeInitialAccFees[];
|
|
39
|
+
/**
|
|
40
|
+
* @dev Creates a context object from contract data arrays
|
|
41
|
+
* @param collateralIndices Array of collateral indices
|
|
42
|
+
* @param pairIndices Array of pair indices
|
|
43
|
+
* @param borrowingParams Array of borrowing fee params from contract
|
|
44
|
+
* @param borrowingData Array of pair borrowing fee data from contract
|
|
45
|
+
* @param currentTimestamp Optional current timestamp
|
|
46
|
+
* @returns Complete SDK context for borrowing v2 calculations
|
|
47
|
+
*/
|
|
48
|
+
export declare const createBorrowingV2Context: (collateralIndices: number[], pairIndices: number[], borrowingParams: IFundingFees.BorrowingFeeParamsStructOutput[], borrowingData: IFundingFees.PairBorrowingFeeDataStructOutput[], currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
|
|
49
|
+
/**
|
|
50
|
+
* @dev Helper function to validate borrowing rate per second
|
|
51
|
+
* @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
|
|
52
|
+
* @returns True if rate is within valid bounds
|
|
53
|
+
*/
|
|
54
|
+
export declare const isValidBorrowingRate: (borrowingRatePerSecondP: number) => boolean;
|
|
55
|
+
/**
|
|
56
|
+
* @dev Helper function to convert borrowing rate to APR percentage
|
|
57
|
+
* @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
|
|
58
|
+
* @returns APR as percentage (e.g., 10.5 for 10.5% APR)
|
|
59
|
+
*/
|
|
60
|
+
export declare const borrowingRateToAPR: (borrowingRatePerSecondP: number) => number;
|
|
61
|
+
/**
|
|
62
|
+
* @dev Helper function to convert APR percentage to borrowing rate per second
|
|
63
|
+
* @param aprPercentage APR as percentage (e.g., 10.5 for 10.5% APR)
|
|
64
|
+
* @returns Borrowing rate per second (normalized float)
|
|
65
|
+
*/
|
|
66
|
+
export declare const aprToBorrowingRate: (aprPercentage: number) => number;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.aprToBorrowingRate = exports.borrowingRateToAPR = exports.isValidBorrowingRate = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArray = exports.convertTradeInitialAccFees = exports.convertPairBorrowingFeeDataArray = exports.convertPairBorrowingFeeData = exports.convertBorrowingFeeParamsArray = exports.convertBorrowingFeeParams = void 0;
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
/**
|
|
6
|
+
* @dev Converts contract BorrowingFeeParams to SDK type
|
|
7
|
+
* @param contractParams Contract borrowing fee params from IFundingFees.BorrowingFeeParams
|
|
8
|
+
* @returns SDK BorrowingFeeParams
|
|
9
|
+
*/
|
|
10
|
+
const convertBorrowingFeeParams = (contractParams) => ({
|
|
11
|
+
borrowingRatePerSecondP: contractParams.borrowingRatePerSecondP /
|
|
12
|
+
index_1.BORROWING_V2_PRECISION.RATE_PER_SECOND,
|
|
13
|
+
});
|
|
14
|
+
exports.convertBorrowingFeeParams = convertBorrowingFeeParams;
|
|
15
|
+
/**
|
|
16
|
+
* @dev Converts array of contract BorrowingFeeParams to SDK types
|
|
17
|
+
* @param contractParamsArray Array of contract borrowing fee params
|
|
18
|
+
* @returns Array of SDK BorrowingFeeParams
|
|
19
|
+
*/
|
|
20
|
+
const convertBorrowingFeeParamsArray = (contractParamsArray) => contractParamsArray.map(params => (0, exports.convertBorrowingFeeParams)(params));
|
|
21
|
+
exports.convertBorrowingFeeParamsArray = convertBorrowingFeeParamsArray;
|
|
22
|
+
/**
|
|
23
|
+
* @dev Converts contract PairBorrowingFeeData to SDK type
|
|
24
|
+
* @param contractData Contract pair borrowing fee data from IFundingFees.PairBorrowingFeeData
|
|
25
|
+
* @returns SDK PairBorrowingFeeData
|
|
26
|
+
*/
|
|
27
|
+
const convertPairBorrowingFeeData = (contractData) => ({
|
|
28
|
+
accBorrowingFeeP: parseFloat(contractData.accBorrowingFeeP.toString()) /
|
|
29
|
+
index_1.BORROWING_V2_PRECISION.ACC_FEE,
|
|
30
|
+
lastBorrowingUpdateTs: contractData.lastBorrowingUpdateTs,
|
|
31
|
+
});
|
|
32
|
+
exports.convertPairBorrowingFeeData = convertPairBorrowingFeeData;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Converts array of contract PairBorrowingFeeData to SDK types
|
|
35
|
+
* @param contractDataArray Array of contract pair borrowing fee data
|
|
36
|
+
* @returns Array of SDK PairBorrowingFeeData
|
|
37
|
+
*/
|
|
38
|
+
const convertPairBorrowingFeeDataArray = (contractDataArray) => contractDataArray.map(data => (0, exports.convertPairBorrowingFeeData)(data));
|
|
39
|
+
exports.convertPairBorrowingFeeDataArray = convertPairBorrowingFeeDataArray;
|
|
40
|
+
/**
|
|
41
|
+
* @dev Converts contract TradeFeesData to SDK TradeInitialAccFees
|
|
42
|
+
* @param contractTradeData Contract trade fees data from IFundingFees.TradeFeesData
|
|
43
|
+
* @returns SDK TradeInitialAccFees
|
|
44
|
+
*/
|
|
45
|
+
const convertTradeInitialAccFees = (contractTradeData) => ({
|
|
46
|
+
initialAccBorrowingFeeP: parseFloat(contractTradeData.initialAccBorrowingFeeP.toString()) /
|
|
47
|
+
index_1.BORROWING_V2_PRECISION.ACC_FEE,
|
|
48
|
+
});
|
|
49
|
+
exports.convertTradeInitialAccFees = convertTradeInitialAccFees;
|
|
50
|
+
/**
|
|
51
|
+
* @dev Converts array of contract TradeFeesData to SDK TradeInitialAccFees
|
|
52
|
+
* @param contractTradeDataArray Array of contract trade fees data
|
|
53
|
+
* @returns Array of SDK TradeInitialAccFees
|
|
54
|
+
*/
|
|
55
|
+
const convertTradeInitialAccFeesArray = (contractTradeDataArray) => contractTradeDataArray.map(data => (0, exports.convertTradeInitialAccFees)(data));
|
|
56
|
+
exports.convertTradeInitialAccFeesArray = convertTradeInitialAccFeesArray;
|
|
57
|
+
/**
|
|
58
|
+
* @dev Creates a context object from contract data arrays
|
|
59
|
+
* @param collateralIndices Array of collateral indices
|
|
60
|
+
* @param pairIndices Array of pair indices
|
|
61
|
+
* @param borrowingParams Array of borrowing fee params from contract
|
|
62
|
+
* @param borrowingData Array of pair borrowing fee data from contract
|
|
63
|
+
* @param currentTimestamp Optional current timestamp
|
|
64
|
+
* @returns Complete SDK context for borrowing v2 calculations
|
|
65
|
+
*/
|
|
66
|
+
const createBorrowingV2Context = (collateralIndices, pairIndices, borrowingParams, borrowingData, currentTimestamp) => {
|
|
67
|
+
const context = {
|
|
68
|
+
currentTimestamp,
|
|
69
|
+
borrowingParams: {},
|
|
70
|
+
borrowingData: {},
|
|
71
|
+
};
|
|
72
|
+
// Build nested objects indexed by collateralIndex and pairIndex
|
|
73
|
+
for (let i = 0; i < collateralIndices.length; i++) {
|
|
74
|
+
const collateralIndex = collateralIndices[i];
|
|
75
|
+
const pairIndex = pairIndices[i];
|
|
76
|
+
// Initialize collateral index objects if they don't exist
|
|
77
|
+
if (!context.borrowingParams[collateralIndex]) {
|
|
78
|
+
context.borrowingParams[collateralIndex] = {};
|
|
79
|
+
}
|
|
80
|
+
if (!context.borrowingData[collateralIndex]) {
|
|
81
|
+
context.borrowingData[collateralIndex] = {};
|
|
82
|
+
}
|
|
83
|
+
// Store converted data
|
|
84
|
+
context.borrowingParams[collateralIndex][pairIndex] =
|
|
85
|
+
(0, exports.convertBorrowingFeeParams)(borrowingParams[i]);
|
|
86
|
+
context.borrowingData[collateralIndex][pairIndex] =
|
|
87
|
+
(0, exports.convertPairBorrowingFeeData)(borrowingData[i]);
|
|
88
|
+
}
|
|
89
|
+
return context;
|
|
90
|
+
};
|
|
91
|
+
exports.createBorrowingV2Context = createBorrowingV2Context;
|
|
92
|
+
/**
|
|
93
|
+
* @dev Helper function to validate borrowing rate per second
|
|
94
|
+
* @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
|
|
95
|
+
* @returns True if rate is within valid bounds
|
|
96
|
+
*/
|
|
97
|
+
const isValidBorrowingRate = (borrowingRatePerSecondP) => {
|
|
98
|
+
return (borrowingRatePerSecondP >= 0 &&
|
|
99
|
+
borrowingRatePerSecondP <= 317097 / index_1.BORROWING_V2_PRECISION.RATE_PER_SECOND); // Max 1,000% APR
|
|
100
|
+
};
|
|
101
|
+
exports.isValidBorrowingRate = isValidBorrowingRate;
|
|
102
|
+
/**
|
|
103
|
+
* @dev Helper function to convert borrowing rate to APR percentage
|
|
104
|
+
* @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
|
|
105
|
+
* @returns APR as percentage (e.g., 10.5 for 10.5% APR)
|
|
106
|
+
*/
|
|
107
|
+
const borrowingRateToAPR = (borrowingRatePerSecondP) => {
|
|
108
|
+
const SECONDS_PER_YEAR = 365 * 24 * 60 * 60; // 31,536,000
|
|
109
|
+
return borrowingRatePerSecondP * SECONDS_PER_YEAR;
|
|
110
|
+
};
|
|
111
|
+
exports.borrowingRateToAPR = borrowingRateToAPR;
|
|
112
|
+
/**
|
|
113
|
+
* @dev Helper function to convert APR percentage to borrowing rate per second
|
|
114
|
+
* @param aprPercentage APR as percentage (e.g., 10.5 for 10.5% APR)
|
|
115
|
+
* @returns Borrowing rate per second (normalized float)
|
|
116
|
+
*/
|
|
117
|
+
const aprToBorrowingRate = (aprPercentage) => {
|
|
118
|
+
const SECONDS_PER_YEAR = 365 * 24 * 60 * 60; // 31,536,000
|
|
119
|
+
return aprPercentage / SECONDS_PER_YEAR;
|
|
120
|
+
};
|
|
121
|
+
exports.aprToBorrowingRate = aprToBorrowingRate;
|
|
@@ -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,60 @@
|
|
|
1
|
+
import * as BorrowingFeeV2 from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Maximum borrowing rate per second (1,000% APR)
|
|
4
|
+
*/
|
|
5
|
+
export declare const MAX_BORROWING_RATE_PER_SECOND = 0.0317097;
|
|
6
|
+
/**
|
|
7
|
+
* @dev Precision constants for borrowing v2 calculations
|
|
8
|
+
*/
|
|
9
|
+
export declare const BORROWING_V2_PRECISION: {
|
|
10
|
+
readonly RATE_PER_SECOND: 10000000000;
|
|
11
|
+
readonly ACC_FEE: 100000000000000000000;
|
|
12
|
+
readonly PERCENTAGE: 100;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* @dev Calculates pending accumulated borrowing fees for a pair
|
|
16
|
+
* @param params Borrowing fee parameters for the pair
|
|
17
|
+
* @param data Current borrowing fee data for the pair
|
|
18
|
+
* @param currentPairPrice Current price of the trading pair
|
|
19
|
+
* @param currentTimestamp Current timestamp (defaults to now)
|
|
20
|
+
* @returns Updated accumulated borrowing fee (1e20 precision)
|
|
21
|
+
*/
|
|
22
|
+
export declare const getPairPendingAccBorrowingFees: (params: BorrowingFeeV2.BorrowingFeeParams, data: BorrowingFeeV2.PairBorrowingFeeData, currentPairPrice: number, currentTimestamp?: number) => number;
|
|
23
|
+
/**
|
|
24
|
+
* @dev Calculates borrowing fees owed by a specific trade
|
|
25
|
+
* @param input Trade borrowing fee calculation input
|
|
26
|
+
* @param context Context containing borrowing parameters and data
|
|
27
|
+
* @returns Borrowing fees in collateral tokens
|
|
28
|
+
*/
|
|
29
|
+
export declare const getTradeBorrowingFeesCollateral: (input: BorrowingFeeV2.TradeBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
30
|
+
/**
|
|
31
|
+
* @dev Convenience function to calculate borrowing fees for a trade using individual parameters
|
|
32
|
+
* @param positionSizeCollateral Position size in collateral tokens
|
|
33
|
+
* @param pairIndex Index of the trading pair
|
|
34
|
+
* @param collateralIndex Index of the collateral
|
|
35
|
+
* @param openPrice Price at which the trade was opened
|
|
36
|
+
* @param currentPairPrice Current price of the trading pair
|
|
37
|
+
* @param initialAccBorrowingFeeP Initial accumulated borrowing fee when trade was opened
|
|
38
|
+
* @param context Context containing borrowing parameters and data
|
|
39
|
+
* @returns Borrowing fees in collateral tokens
|
|
40
|
+
*/
|
|
41
|
+
export declare const getBorrowingFee: (positionSizeCollateral: number, pairIndex: number, collateralIndex: number, openPrice: number, currentPairPrice: number, initialAccBorrowingFeeP: number, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
42
|
+
/**
|
|
43
|
+
* @dev Utility function to get pending accumulated borrowing fees for a pair using context
|
|
44
|
+
* @param input Pair borrowing fee calculation input
|
|
45
|
+
* @param context Context containing borrowing parameters and data
|
|
46
|
+
* @returns Updated accumulated borrowing fee (1e20 precision)
|
|
47
|
+
*/
|
|
48
|
+
export declare const getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
49
|
+
/**
|
|
50
|
+
* @dev Utility functions for working with borrowing v2 fees
|
|
51
|
+
*/
|
|
52
|
+
export declare const borrowingFeeV2Utils: {
|
|
53
|
+
getPairPendingAccBorrowingFees: (params: BorrowingFeeV2.BorrowingFeeParams, data: BorrowingFeeV2.PairBorrowingFeeData, currentPairPrice: number, currentTimestamp?: number) => number;
|
|
54
|
+
getTradeBorrowingFeesCollateral: (input: BorrowingFeeV2.TradeBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
55
|
+
getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
56
|
+
getBorrowingFee: (positionSizeCollateral: number, pairIndex: number, collateralIndex: number, openPrice: number, currentPairPrice: number, initialAccBorrowingFeeP: number, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
57
|
+
};
|
|
58
|
+
export * as BorrowingFeeV2 from "./types";
|
|
59
|
+
export * from "./converter";
|
|
60
|
+
export * from "./fetcher";
|