@gainsnetwork/sdk 0.0.0-v10.rc1 → 0.0.0-v10.rc2
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/constants.d.ts +8 -1
- package/lib/constants.js +8 -1
- package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +76 -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/GNSMultiCollatDiamond.d.ts +20 -7
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +16 -7
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +0 -7
- package/lib/contracts/types/generated/factories/GToken__factory.js +0 -4
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/index.d.ts +3 -0
- package/lib/contracts/utils/index.js +3 -0
- package/lib/contracts/utils/openTrades.d.ts +1 -0
- package/lib/contracts/utils/openTrades.js +109 -55
- package/lib/contracts/utils/pairs.js +7 -0
- package/lib/trade/fees/borrowing/index.d.ts +10 -0
- package/lib/trade/fees/borrowing/index.js +10 -0
- package/lib/trade/fees/borrowingV2/converter.d.ts +66 -0
- package/lib/trade/fees/borrowingV2/converter.js +121 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +59 -0
- package/lib/trade/fees/borrowingV2/index.js +139 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +79 -0
- package/lib/trade/fees/borrowingV2/types.js +5 -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/index.d.ts +135 -0
- package/lib/trade/fees/fundingFees/index.js +322 -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 +6 -2
- package/lib/trade/fees/index.js +58 -16
- package/lib/trade/fees/tiers/index.d.ts +17 -0
- package/lib/trade/fees/tiers/index.js +44 -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 +34 -0
- package/lib/trade/fees/trading/index.js +104 -0
- package/lib/trade/fees/trading/types.d.ts +39 -0
- package/lib/trade/fees/trading/types.js +5 -0
- package/lib/trade/index.d.ts +2 -1
- package/lib/trade/index.js +2 -1
- package/lib/trade/liquidation.d.ts +32 -2
- package/lib/trade/liquidation.js +93 -8
- package/lib/trade/pnl.d.ts +3 -3
- package/lib/trade/pnl.js +7 -2
- package/lib/trade/priceImpact/index.d.ts +8 -0
- package/lib/trade/priceImpact/index.js +32 -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/index.d.ts +57 -0
- package/lib/trade/priceImpact/skew/index.js +175 -0
- package/lib/trade/priceImpact/skew/types.d.ts +55 -0
- package/lib/trade/priceImpact/skew/types.js +5 -0
- package/lib/trade/types.d.ts +25 -2
- 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
|
@@ -14,7 +14,7 @@ const ethcall_1 = require("ethcall");
|
|
|
14
14
|
const fetchOpenPairTrades = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
15
|
const rawTrades = yield (0, exports.fetchOpenPairTradesRaw)(contracts, overrides);
|
|
16
16
|
const collateralPrecisions = (yield contracts.gnsMultiCollatDiamond.getCollaterals()).map(({ precision }) => precision);
|
|
17
|
-
return rawTrades.map(rawTrade => _prepareTradeContainer(rawTrade.trade, rawTrade.tradeInfo, rawTrade.liquidationParams, rawTrade.initialAccFees, collateralPrecisions[parseInt(rawTrade.trade.collateralIndex.toString()) - 1]));
|
|
17
|
+
return rawTrades.map(rawTrade => _prepareTradeContainer(rawTrade.trade, rawTrade.tradeInfo, rawTrade.liquidationParams, rawTrade.initialAccFees, rawTrade.tradeFeesData, rawTrade.uiRealizedPnlData, collateralPrecisions[parseInt(rawTrade.trade.collateralIndex.toString()) - 1]));
|
|
18
18
|
});
|
|
19
19
|
exports.fetchOpenPairTrades = fetchOpenPairTrades;
|
|
20
20
|
// @todo rename
|
|
@@ -22,7 +22,7 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
|
|
|
22
22
|
if (!contracts) {
|
|
23
23
|
return [];
|
|
24
24
|
}
|
|
25
|
-
const { batchSize = 50, includeLimits = true, useMulticall = false, } = overrides;
|
|
25
|
+
const { batchSize = 50, includeLimits = true, useMulticall = false, includeUIRealizedPnlData = true, } = overrides;
|
|
26
26
|
const { gnsMultiCollatDiamond: multiCollatDiamondContract } = contracts;
|
|
27
27
|
try {
|
|
28
28
|
const multicallCtx = {
|
|
@@ -38,25 +38,52 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
|
|
|
38
38
|
let running = true;
|
|
39
39
|
let offset = 0;
|
|
40
40
|
while (running) {
|
|
41
|
-
const trades = yield
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
const [trades, tradeInfos, tradeLiquidationParams] = yield Promise.all([
|
|
42
|
+
multiCollatDiamondContract.getAllTrades(offset, offset + batchSize),
|
|
43
|
+
multiCollatDiamondContract.getAllTradeInfos(offset, offset + batchSize),
|
|
44
|
+
multiCollatDiamondContract.getAllTradesLiquidationParams(offset, offset + batchSize),
|
|
45
|
+
]);
|
|
46
|
+
const fundingFeesCallParams = [
|
|
47
|
+
[],
|
|
48
|
+
[], // indices
|
|
49
|
+
];
|
|
44
50
|
// Array is always of length `batchSize`
|
|
45
51
|
// so we need to filter out the empty trades, indexes are reliable
|
|
46
52
|
const openTrades = trades
|
|
47
53
|
.filter(t => t.collateralIndex > 0 &&
|
|
48
54
|
(includeLimits || (!includeLimits && t.tradeType === 0)))
|
|
49
|
-
.map((trade, ix) =>
|
|
50
|
-
trade
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
.map((trade, ix) => {
|
|
56
|
+
fundingFeesCallParams[0].push(trade.user);
|
|
57
|
+
fundingFeesCallParams[1].push(trade.index);
|
|
58
|
+
return {
|
|
59
|
+
trade,
|
|
60
|
+
tradeInfo: tradeInfos[ix],
|
|
61
|
+
liquidationParams: tradeLiquidationParams[ix],
|
|
62
|
+
initialAccFees: {
|
|
63
|
+
accPairFee: 0,
|
|
64
|
+
accGroupFee: 0,
|
|
65
|
+
block: 0,
|
|
66
|
+
__placeholder: 0,
|
|
67
|
+
},
|
|
68
|
+
tradeFeesData: {
|
|
69
|
+
realizedTradingFeesCollateral: 0,
|
|
70
|
+
realizedPnlCollateral: 0,
|
|
71
|
+
manuallyRealizedNegativePnlCollateral: 0,
|
|
72
|
+
alreadyTransferredNegativePnlCollateral: 0,
|
|
73
|
+
virtualAvailableCollateralInDiamond: 0,
|
|
74
|
+
initialAccFundingFeeP: 0,
|
|
75
|
+
initialAccBorrowingFeeP: 0,
|
|
76
|
+
__placeholder: 0,
|
|
77
|
+
},
|
|
78
|
+
uiRealizedPnlData: undefined,
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
const [tradeFeesData, uiRealizedPnlData] = yield Promise.all([
|
|
82
|
+
multiCollatDiamondContract.getTradeFeesDataArray(...fundingFeesCallParams),
|
|
83
|
+
includeUIRealizedPnlData
|
|
84
|
+
? multiCollatDiamondContract.getTradeUiRealizedPnlDataArray(...fundingFeesCallParams)
|
|
85
|
+
: [],
|
|
86
|
+
]);
|
|
60
87
|
const initialAccFeesPromises = openTrades
|
|
61
88
|
.map(({ trade }) => ({
|
|
62
89
|
collateralIndex: trade.collateralIndex,
|
|
@@ -71,6 +98,8 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
|
|
|
71
98
|
: Promise.all(initialAccFeesPromises));
|
|
72
99
|
initialAccFees.forEach((accFees, ix) => {
|
|
73
100
|
openTrades[ix].initialAccFees = accFees;
|
|
101
|
+
openTrades[ix].tradeFeesData = tradeFeesData[ix];
|
|
102
|
+
openTrades[ix].uiRealizedPnlData = uiRealizedPnlData === null || uiRealizedPnlData === void 0 ? void 0 : uiRealizedPnlData[ix];
|
|
74
103
|
});
|
|
75
104
|
allOpenPairTrades = allOpenPairTrades.concat(openTrades);
|
|
76
105
|
offset += batchSize + 1;
|
|
@@ -85,42 +114,67 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
|
|
|
85
114
|
}
|
|
86
115
|
});
|
|
87
116
|
exports.fetchOpenPairTradesRaw = fetchOpenPairTradesRaw;
|
|
88
|
-
const _prepareTradeContainer = (trade, tradeInfo, tradeLiquidationParams, tradeInitialAccFees, collateralPrecision) =>
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
117
|
+
const _prepareTradeContainer = (trade, tradeInfo, tradeLiquidationParams, tradeInitialAccFees, tradeFeesData, uiRealizedPnlData, collateralPrecision) => {
|
|
118
|
+
const precision = parseFloat(collateralPrecision.toString());
|
|
119
|
+
return {
|
|
120
|
+
trade: {
|
|
121
|
+
user: trade.user,
|
|
122
|
+
index: parseInt(trade.index.toString()),
|
|
123
|
+
pairIndex: parseInt(trade.pairIndex.toString()),
|
|
124
|
+
leverage: parseFloat(trade.leverage.toString()) / 1e3,
|
|
125
|
+
long: trade.long.toString() === "true",
|
|
126
|
+
isOpen: trade.isOpen.toString() === "true",
|
|
127
|
+
collateralIndex: parseInt(trade.collateralIndex.toString()),
|
|
128
|
+
tradeType: trade.tradeType,
|
|
129
|
+
collateralAmount: parseFloat(trade.collateralAmount.toString()) / precision,
|
|
130
|
+
openPrice: parseFloat(trade.openPrice.toString()) / 1e10,
|
|
131
|
+
tp: parseFloat(trade.tp.toString()) / 1e10,
|
|
132
|
+
sl: parseFloat(trade.sl.toString()) / 1e10,
|
|
133
|
+
isCounterTrade: trade.isCounterTrade,
|
|
134
|
+
positionSizeToken: parseFloat(trade.positionSizeToken.toString()) / 1e18,
|
|
135
|
+
},
|
|
136
|
+
tradeInfo: {
|
|
137
|
+
createdBlock: parseInt(tradeInfo.createdBlock.toString()),
|
|
138
|
+
tpLastUpdatedBlock: parseInt(tradeInfo.tpLastUpdatedBlock.toString()),
|
|
139
|
+
slLastUpdatedBlock: parseInt(tradeInfo.slLastUpdatedBlock.toString()),
|
|
140
|
+
maxSlippageP: parseFloat(tradeInfo.maxSlippageP.toString()) / 1e3,
|
|
141
|
+
lastOiUpdateTs: parseFloat(tradeInfo.lastOiUpdateTs),
|
|
142
|
+
collateralPriceUsd: parseFloat(tradeInfo.collateralPriceUsd.toString()) / 1e8,
|
|
143
|
+
contractsVersion: parseInt(tradeInfo.contractsVersion.toString()),
|
|
144
|
+
lastPosIncreaseBlock: parseInt(tradeInfo.lastPosIncreaseBlock.toString()),
|
|
145
|
+
},
|
|
146
|
+
liquidationParams: {
|
|
147
|
+
maxLiqSpreadP: parseFloat(tradeLiquidationParams.maxLiqSpreadP.toString()) / 1e12,
|
|
148
|
+
startLiqThresholdP: parseFloat(tradeLiquidationParams.startLiqThresholdP.toString()) / 1e12,
|
|
149
|
+
endLiqThresholdP: parseFloat(tradeLiquidationParams.endLiqThresholdP.toString()) / 1e12,
|
|
150
|
+
startLeverage: parseFloat(tradeLiquidationParams.startLeverage.toString()) / 1e3,
|
|
151
|
+
endLeverage: parseFloat(tradeLiquidationParams.endLeverage.toString()) / 1e3,
|
|
152
|
+
},
|
|
153
|
+
initialAccFees: {
|
|
154
|
+
accPairFee: parseFloat(tradeInitialAccFees.accPairFee.toString()) / 1e10,
|
|
155
|
+
accGroupFee: parseFloat(tradeInitialAccFees.accGroupFee.toString()) / 1e10,
|
|
156
|
+
block: parseFloat(tradeInitialAccFees.block.toString()),
|
|
157
|
+
},
|
|
158
|
+
tradeFeesData: {
|
|
159
|
+
realizedTradingFeesCollateral: parseFloat(tradeFeesData.realizedTradingFeesCollateral.toString()) /
|
|
160
|
+
precision,
|
|
161
|
+
realizedPnlCollateral: parseFloat(tradeFeesData.realizedPnlCollateral.toString()) / precision,
|
|
162
|
+
manuallyRealizedNegativePnlCollateral: parseFloat(tradeFeesData.manuallyRealizedNegativePnlCollateral.toString()) / precision,
|
|
163
|
+
alreadyTransferredNegativePnlCollateral: parseFloat(tradeFeesData.alreadyTransferredNegativePnlCollateral.toString()) / precision,
|
|
164
|
+
virtualAvailableCollateralInDiamond: parseFloat(tradeFeesData.virtualAvailableCollateralInDiamond.toString()) / precision,
|
|
165
|
+
initialAccFundingFeeP: parseFloat(tradeFeesData.initialAccFundingFeeP.toString()) / 1e10,
|
|
166
|
+
initialAccBorrowingFeeP: parseFloat(tradeFeesData.initialAccBorrowingFeeP.toString()) / 1e10,
|
|
167
|
+
},
|
|
168
|
+
uiRealizedPnlData: uiRealizedPnlData
|
|
169
|
+
? {
|
|
170
|
+
realizedTradingFeesCollateral: parseFloat(uiRealizedPnlData.realizedTradingFeesCollateral.toString()) / precision,
|
|
171
|
+
realizedOldBorrowingFeesCollateral: parseFloat(uiRealizedPnlData.realizedOldBorrowingFeesCollateral.toString()) / precision,
|
|
172
|
+
realizedNewBorrowingFeesCollateral: parseFloat(uiRealizedPnlData.realizedNewBorrowingFeesCollateral.toString()) / precision,
|
|
173
|
+
realizedFundingFeesCollateral: parseFloat(uiRealizedPnlData.realizedFundingFeesCollateral.toString()) / precision,
|
|
174
|
+
realizedPnlPartialCloseCollateral: parseFloat(uiRealizedPnlData.realizedPnlPartialCloseCollateral.toString()) / precision,
|
|
175
|
+
pnlWithdrawnCollateral: parseFloat(uiRealizedPnlData.pnlWithdrawnCollateral.toString()) /
|
|
176
|
+
precision,
|
|
177
|
+
}
|
|
178
|
+
: undefined,
|
|
179
|
+
};
|
|
180
|
+
};
|
|
@@ -475,4 +475,11 @@ const PAIR_INDEX_TO_DESCRIPTION = {
|
|
|
475
475
|
[types_1.PairIndex.WCTUSD]: "WalletConnect Token to US Dollar",
|
|
476
476
|
[types_1.PairIndex.BIGTIMEUSD]: "Big Time to US Dollar",
|
|
477
477
|
[types_1.PairIndex.BABYUSD]: "Babylon to US Dollar",
|
|
478
|
+
[types_1.PairIndex.COOKIEUSD]: "Cookie DAO to US Dollar",
|
|
479
|
+
[types_1.PairIndex.KMNOUSD]: "Kamino to US Dollar",
|
|
480
|
+
[types_1.PairIndex.INITUSD]: "Initia to US Dollar",
|
|
481
|
+
[types_1.PairIndex.SYRUPUSD]: "Maple Finance to US Dollar",
|
|
482
|
+
[types_1.PairIndex.SIGNUSD]: "Sign to US Dollar",
|
|
483
|
+
[types_1.PairIndex.ZORAUSD]: "ZORA to US Dollar",
|
|
484
|
+
[types_1.PairIndex.COINUSD]: "Coinbase to US Dollar",
|
|
478
485
|
};
|
|
@@ -5,6 +5,16 @@ export type GetBorrowingFeeContext = {
|
|
|
5
5
|
groups: BorrowingFee.Group[];
|
|
6
6
|
pairs: BorrowingFee.Pair[];
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* @dev Calculates borrowing fees using v1 model (block-based with groups)
|
|
10
|
+
* @dev Still actively used by markets that haven't migrated to v2
|
|
11
|
+
* @param posDai Position size in collateral
|
|
12
|
+
* @param pairIndex Trading pair index
|
|
13
|
+
* @param long Whether position is long
|
|
14
|
+
* @param initialAccFees Initial accumulated fees when trade was opened
|
|
15
|
+
* @param context Context with current block and fee data
|
|
16
|
+
* @returns Borrowing fee in collateral tokens
|
|
17
|
+
*/
|
|
8
18
|
export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
|
|
9
19
|
export declare const withinMaxGroupOi: (pairIndex: PairIndex, long: boolean, positionSizeCollateral: number, context: {
|
|
10
20
|
groups: BorrowingFee.Group[];
|
|
@@ -27,6 +27,16 @@ 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
|
+
* @param posDai Position size in collateral
|
|
34
|
+
* @param pairIndex Trading pair index
|
|
35
|
+
* @param long Whether position is long
|
|
36
|
+
* @param initialAccFees Initial accumulated fees when trade was opened
|
|
37
|
+
* @param context Context with current block and fee data
|
|
38
|
+
* @returns Borrowing fee in collateral tokens
|
|
39
|
+
*/
|
|
30
40
|
const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
31
41
|
if (!context.groups || !context.pairs || !context.pairs[pairIndex]) {
|
|
32
42
|
return 0;
|
|
@@ -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,59 @@
|
|
|
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";
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.BorrowingFeeV2 = exports.borrowingFeeV2Utils = exports.getPairBorrowingFees = exports.getBorrowingFee = exports.getTradeBorrowingFeesCollateral = exports.getPairPendingAccBorrowingFees = exports.BORROWING_V2_PRECISION = exports.MAX_BORROWING_RATE_PER_SECOND = void 0;
|
|
30
|
+
/**
|
|
31
|
+
* @dev Maximum borrowing rate per second (1,000% APR)
|
|
32
|
+
*/
|
|
33
|
+
exports.MAX_BORROWING_RATE_PER_SECOND = 0.0317097; // 317097 / 1e10
|
|
34
|
+
/**
|
|
35
|
+
* @dev Precision constants for borrowing v2 calculations
|
|
36
|
+
*/
|
|
37
|
+
exports.BORROWING_V2_PRECISION = {
|
|
38
|
+
RATE_PER_SECOND: 1e10,
|
|
39
|
+
ACC_FEE: 1e20,
|
|
40
|
+
PERCENTAGE: 100,
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* @dev Calculates pending accumulated borrowing fees for a pair
|
|
44
|
+
* @param params Borrowing fee parameters for the pair
|
|
45
|
+
* @param data Current borrowing fee data for the pair
|
|
46
|
+
* @param currentPairPrice Current price of the trading pair
|
|
47
|
+
* @param currentTimestamp Current timestamp (defaults to now)
|
|
48
|
+
* @returns Updated accumulated borrowing fee (1e20 precision)
|
|
49
|
+
*/
|
|
50
|
+
const getPairPendingAccBorrowingFees = (params, data, currentPairPrice, currentTimestamp) => {
|
|
51
|
+
const timestamp = currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000);
|
|
52
|
+
// Calculate time elapsed since last update
|
|
53
|
+
const timeElapsed = Math.max(0, timestamp - data.lastBorrowingUpdateTs);
|
|
54
|
+
// If no time elapsed, return current accumulated fee
|
|
55
|
+
if (timeElapsed === 0) {
|
|
56
|
+
return data.accBorrowingFeeP;
|
|
57
|
+
}
|
|
58
|
+
// Calculate accumulated borrowing fee delta
|
|
59
|
+
// Formula: borrowingRatePerSecondP * timeElapsed * currentPairPrice
|
|
60
|
+
const accBorrowingFeeDeltaP = params.borrowingRatePerSecondP * timeElapsed * currentPairPrice;
|
|
61
|
+
return data.accBorrowingFeeP + accBorrowingFeeDeltaP;
|
|
62
|
+
};
|
|
63
|
+
exports.getPairPendingAccBorrowingFees = getPairPendingAccBorrowingFees;
|
|
64
|
+
/**
|
|
65
|
+
* @dev Calculates borrowing fees owed by a specific trade
|
|
66
|
+
* @param input Trade borrowing fee calculation input
|
|
67
|
+
* @param context Context containing borrowing parameters and data
|
|
68
|
+
* @returns Borrowing fees in collateral tokens
|
|
69
|
+
*/
|
|
70
|
+
const getTradeBorrowingFeesCollateral = (input, context) => {
|
|
71
|
+
var _a, _b;
|
|
72
|
+
const { positionSizeCollateral, openPrice, collateralIndex, pairIndex, currentPairPrice, initialAccBorrowingFeeP, currentTimestamp, } = input;
|
|
73
|
+
// Get borrowing parameters and data for the pair
|
|
74
|
+
const params = (_a = context.borrowingParams[collateralIndex]) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
75
|
+
const data = (_b = context.borrowingData[collateralIndex]) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
76
|
+
if (!params || !data) {
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
// Calculate current accumulated borrowing fees
|
|
80
|
+
const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp);
|
|
81
|
+
// Calculate borrowing fees for this trade
|
|
82
|
+
// Formula: (positionSizeCollateral * (currentAccFee - initialAccFee)) / openPrice / 100
|
|
83
|
+
const feeDeltaP = currentAccBorrowingFeeP - initialAccBorrowingFeeP;
|
|
84
|
+
return ((positionSizeCollateral * feeDeltaP) /
|
|
85
|
+
openPrice /
|
|
86
|
+
exports.BORROWING_V2_PRECISION.PERCENTAGE);
|
|
87
|
+
};
|
|
88
|
+
exports.getTradeBorrowingFeesCollateral = getTradeBorrowingFeesCollateral;
|
|
89
|
+
/**
|
|
90
|
+
* @dev Convenience function to calculate borrowing fees for a trade using individual parameters
|
|
91
|
+
* @param positionSizeCollateral Position size in collateral tokens
|
|
92
|
+
* @param pairIndex Index of the trading pair
|
|
93
|
+
* @param collateralIndex Index of the collateral
|
|
94
|
+
* @param openPrice Price at which the trade was opened
|
|
95
|
+
* @param currentPairPrice Current price of the trading pair
|
|
96
|
+
* @param initialAccBorrowingFeeP Initial accumulated borrowing fee when trade was opened
|
|
97
|
+
* @param context Context containing borrowing parameters and data
|
|
98
|
+
* @returns Borrowing fees in collateral tokens
|
|
99
|
+
*/
|
|
100
|
+
const getBorrowingFee = (positionSizeCollateral, pairIndex, collateralIndex, openPrice, currentPairPrice, initialAccBorrowingFeeP, context) => {
|
|
101
|
+
return (0, exports.getTradeBorrowingFeesCollateral)({
|
|
102
|
+
positionSizeCollateral,
|
|
103
|
+
openPrice,
|
|
104
|
+
collateralIndex,
|
|
105
|
+
pairIndex,
|
|
106
|
+
currentPairPrice,
|
|
107
|
+
initialAccBorrowingFeeP,
|
|
108
|
+
currentTimestamp: context.currentTimestamp,
|
|
109
|
+
}, context);
|
|
110
|
+
};
|
|
111
|
+
exports.getBorrowingFee = getBorrowingFee;
|
|
112
|
+
/**
|
|
113
|
+
* @dev Utility function to get pending accumulated borrowing fees for a pair using context
|
|
114
|
+
* @param input Pair borrowing fee calculation input
|
|
115
|
+
* @param context Context containing borrowing parameters and data
|
|
116
|
+
* @returns Updated accumulated borrowing fee (1e20 precision)
|
|
117
|
+
*/
|
|
118
|
+
const getPairBorrowingFees = (input, context) => {
|
|
119
|
+
var _a, _b;
|
|
120
|
+
const { collateralIndex, pairIndex, currentPairPrice, currentTimestamp } = input;
|
|
121
|
+
const params = (_a = context.borrowingParams[collateralIndex]) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
122
|
+
const data = (_b = context.borrowingData[collateralIndex]) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
123
|
+
if (!params || !data) {
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : context.currentTimestamp);
|
|
127
|
+
};
|
|
128
|
+
exports.getPairBorrowingFees = getPairBorrowingFees;
|
|
129
|
+
/**
|
|
130
|
+
* @dev Utility functions for working with borrowing v2 fees
|
|
131
|
+
*/
|
|
132
|
+
exports.borrowingFeeV2Utils = {
|
|
133
|
+
getPairPendingAccBorrowingFees: exports.getPairPendingAccBorrowingFees,
|
|
134
|
+
getTradeBorrowingFeesCollateral: exports.getTradeBorrowingFeesCollateral,
|
|
135
|
+
getPairBorrowingFees: exports.getPairBorrowingFees,
|
|
136
|
+
getBorrowingFee: exports.getBorrowingFee,
|
|
137
|
+
};
|
|
138
|
+
exports.BorrowingFeeV2 = __importStar(require("./types"));
|
|
139
|
+
__exportStar(require("./converter"), exports);
|