@gainsnetwork/sdk 1.0.6-rc3 → 1.1.0-rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/backend/globalTrades/index.js +10 -10
- package/lib/backend/tradingVariables/backend.types.d.ts +11 -4
- package/lib/backend/tradingVariables/converter.d.ts +7 -3
- package/lib/backend/tradingVariables/converter.js +70 -63
- package/lib/backend/tradingVariables/index.js +11 -7
- package/lib/backend/tradingVariables/types.d.ts +4 -2
- package/lib/contracts/addresses.js +1 -4
- package/lib/contracts/index.d.ts +1 -1
- package/lib/contracts/index.js +3 -3
- 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 +223 -40
- 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.d.ts +10 -2
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +317 -36
- 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/utils/borrowingFees.js +20 -9
- package/lib/contracts/utils/openTrades.js +20 -11
- package/lib/contracts/utils/pairs.d.ts +13 -2
- package/lib/contracts/utils/pairs.js +89 -21
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/markets/forex.js +1 -1
- package/lib/markets/leverage/builder.js +2 -2
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/markets/price/index.d.ts +0 -1
- package/lib/markets/price/index.js +0 -1
- package/lib/markets/price/signedPrices.d.ts +7 -0
- package/lib/markets/price/signedPrices.js +55 -2
- package/lib/markets/price/types.d.ts +0 -27
- package/lib/pricing/depthBands/converter.d.ts +65 -0
- package/lib/pricing/depthBands/converter.js +155 -0
- package/lib/pricing/depthBands/decoder.d.ts +32 -0
- package/lib/pricing/depthBands/decoder.js +109 -0
- package/lib/pricing/depthBands/encoder.d.ts +19 -0
- package/lib/pricing/depthBands/encoder.js +105 -0
- package/lib/pricing/depthBands/index.d.ts +8 -0
- package/lib/pricing/depthBands/index.js +26 -0
- package/lib/pricing/depthBands/types.d.ts +49 -0
- package/lib/pricing/depthBands/types.js +10 -0
- package/lib/pricing/depthBands/validator.d.ts +22 -0
- package/lib/pricing/depthBands/validator.js +113 -0
- package/lib/pricing/depthBands.d.ts +39 -0
- package/lib/pricing/depthBands.js +92 -0
- package/lib/pricing/index.d.ts +4 -0
- package/lib/pricing/index.js +20 -0
- package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
- package/lib/trade/effectiveLeverage/builder.js +30 -0
- package/lib/trade/fees/borrowing/builder.js +3 -2
- package/lib/trade/fees/borrowing/converter.js +1 -5
- package/lib/trade/fees/borrowing/index.js +5 -5
- package/lib/trade/fees/borrowingV2/builder.js +4 -3
- package/lib/trade/fees/borrowingV2/converter.js +1 -1
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +0 -1
- package/lib/trade/fees/borrowingV2/fetcher.js +32 -26
- package/lib/trade/fees/borrowingV2/index.js +3 -3
- package/lib/trade/fees/converter.js +22 -22
- package/lib/trade/fees/fundingFees/builder.js +7 -6
- package/lib/trade/fees/fundingFees/converter.js +1 -1
- package/lib/trade/fees/fundingFees/fetcher.js +25 -16
- package/lib/trade/fees/fundingFees/index.js +3 -2
- package/lib/trade/fees/holdingFees/index.d.ts +46 -0
- package/lib/trade/fees/holdingFees/index.js +105 -0
- package/lib/trade/fees/holdingFees/types.d.ts +23 -0
- package/lib/trade/fees/holdingFees/types.js +5 -0
- package/lib/trade/fees/tiers/index.js +2 -1
- package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFees.js +66 -0
- package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
- package/lib/trade/fees/trading/index.js +3 -5
- package/lib/trade/liquidation/builder.js +3 -6
- package/lib/trade/liquidation/index.js +6 -4
- package/lib/trade/oiWindows.js +2 -1
- package/lib/trade/pnl/builder.js +2 -1
- package/lib/trade/pnl/converter.js +1 -1
- package/lib/trade/pnl/index.js +7 -4
- package/lib/trade/priceImpact/close/builder.js +2 -1
- package/lib/trade/priceImpact/close/index.js +1 -4
- package/lib/trade/priceImpact/cumulVol/builder.js +11 -18
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +63 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +97 -1
- package/lib/trade/priceImpact/cumulVol/index.d.ts +3 -0
- package/lib/trade/priceImpact/cumulVol/index.js +123 -25
- package/lib/trade/priceImpact/cumulVol/types.d.ts +11 -0
- package/lib/trade/priceImpact/cumulVol/types.js +2 -0
- package/lib/trade/priceImpact/open/builder.js +2 -1
- package/lib/trade/priceImpact/open/index.js +1 -4
- package/lib/trade/priceImpact/skew/builder.js +3 -2
- package/lib/trade/priceImpact/skew/converter.js +1 -1
- package/lib/trade/priceImpact/skew/fetcher.js +33 -24
- package/package.json +2 -2
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildTradeEffectiveLeverageContext = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
/**
|
|
6
|
+
* @dev Builds a complete context for effective leverage calculations
|
|
7
|
+
* @dev Uses the closing price impact context builder as a sub-context
|
|
8
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
9
|
+
* @param collateralIndex The collateral index (1-based)
|
|
10
|
+
* @param pairIndex The pair index
|
|
11
|
+
* @param tradeInfo Trade information including createdBlock
|
|
12
|
+
* @param additionalParams Additional parameters for price impact calculations
|
|
13
|
+
* @returns Complete context ready for getTradeNewEffectiveLeverage
|
|
14
|
+
*/
|
|
15
|
+
const buildTradeEffectiveLeverageContext = (globalTradingVariables, collateralIndex, pairIndex, tradeInfo, additionalParams) => {
|
|
16
|
+
var _a;
|
|
17
|
+
// Build the closing price impact context which we'll use for PnL calculations
|
|
18
|
+
const closingPriceImpactContext = (0, __1.buildTradeClosingPriceImpactContext)(globalTradingVariables, collateralIndex, pairIndex, tradeInfo, additionalParams);
|
|
19
|
+
if (!closingPriceImpactContext) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
// Extract base spread from pairs data
|
|
23
|
+
const pairs = globalTradingVariables.pairs;
|
|
24
|
+
const baseSpreadP = ((_a = pairs === null || pairs === void 0 ? void 0 : pairs[pairIndex]) === null || _a === void 0 ? void 0 : _a.spreadP) || 0;
|
|
25
|
+
return {
|
|
26
|
+
closingPriceImpactContext,
|
|
27
|
+
baseSpreadP,
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
exports.buildTradeEffectiveLeverageContext = buildTradeEffectiveLeverageContext;
|
|
@@ -13,8 +13,9 @@ exports.buildBorrowingV1Context = void 0;
|
|
|
13
13
|
* @returns Full borrowing context with all pairs and groups or undefined if data not available
|
|
14
14
|
*/
|
|
15
15
|
const buildBorrowingV1Context = (globalTradingVariables, collateralIndex, currentBlock) => {
|
|
16
|
+
var _a;
|
|
16
17
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
17
|
-
if (!collateral
|
|
18
|
+
if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairBorrowingFees) || !(collateral === null || collateral === void 0 ? void 0 : collateral.groupBorrowingFees)) {
|
|
18
19
|
return undefined;
|
|
19
20
|
}
|
|
20
21
|
const pairs = collateral.pairBorrowingFees;
|
|
@@ -26,7 +27,7 @@ const buildBorrowingV1Context = (globalTradingVariables, collateralIndex, curren
|
|
|
26
27
|
currentBlock,
|
|
27
28
|
pairs,
|
|
28
29
|
groups,
|
|
29
|
-
collateralPriceUsd: collateral.prices
|
|
30
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
30
31
|
pairOis: collateral.pairOis,
|
|
31
32
|
};
|
|
32
33
|
};
|
|
@@ -14,11 +14,7 @@ const convertPairGroupBorrowingFee = (pairGroup) => ({
|
|
|
14
14
|
block: pairGroup.block,
|
|
15
15
|
});
|
|
16
16
|
exports.convertPairGroupBorrowingFee = convertPairGroupBorrowingFee;
|
|
17
|
-
const convertPairBorrowingFee = (pair, pairOi, pairGroup, feeCap) => ({
|
|
18
|
-
...(0, exports.convertGroupBorrowingData)(pair, pairOi),
|
|
19
|
-
groups: pairGroup.map(value => (0, exports.convertPairGroupBorrowingFee)(value)),
|
|
20
|
-
feePerBlockCap: (0, exports.convertFeePerBlockCap)(feeCap),
|
|
21
|
-
});
|
|
17
|
+
const convertPairBorrowingFee = (pair, pairOi, pairGroup, feeCap) => (Object.assign(Object.assign({}, (0, exports.convertGroupBorrowingData)(pair, pairOi)), { groups: pairGroup.map(value => (0, exports.convertPairGroupBorrowingFee)(value)), feePerBlockCap: (0, exports.convertFeePerBlockCap)(feeCap) }));
|
|
22
18
|
exports.convertPairBorrowingFee = convertPairBorrowingFee;
|
|
23
19
|
const convertPairBorrowingFees = ([pairs, pairOi, pairGroups, feeCaps]) => pairs.map((value, ix) => (0, exports.convertPairBorrowingFee)(value, pairOi[ix], pairGroups[ix], feeCaps[ix]));
|
|
24
20
|
exports.convertPairBorrowingFees = convertPairBorrowingFees;
|
|
@@ -48,7 +48,7 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, currentPairPri
|
|
|
48
48
|
return 0;
|
|
49
49
|
}
|
|
50
50
|
const pairGroups = pairs[pairIndex].groups;
|
|
51
|
-
const firstPairGroup = pairGroups
|
|
51
|
+
const firstPairGroup = (pairGroups === null || pairGroups === void 0 ? void 0 : pairGroups.length) > 0 ? pairGroups[0] : undefined;
|
|
52
52
|
let fee = 0;
|
|
53
53
|
if (!firstPairGroup || firstPairGroup.block > initialAccFees.block) {
|
|
54
54
|
const openInterest = (0, __1.getPairTotalOisDynamicCollateral)(pairIndex, {
|
|
@@ -215,13 +215,13 @@ collateralPriceUsd) => {
|
|
|
215
215
|
};
|
|
216
216
|
const getPendingAccFeesDelta = (blockDistance, feePerBlock, netOi, maxOi, feeExponent) => {
|
|
217
217
|
return maxOi > 0 && feeExponent > 0
|
|
218
|
-
? feePerBlock * blockDistance * (netOi / maxOi)
|
|
218
|
+
? feePerBlock * blockDistance * Math.pow((netOi / maxOi), feeExponent)
|
|
219
219
|
: 0;
|
|
220
220
|
};
|
|
221
221
|
const getFeePerBlockCaps = (cap) => {
|
|
222
222
|
return {
|
|
223
|
-
minP: cap
|
|
224
|
-
maxP: cap
|
|
223
|
+
minP: (cap === null || cap === void 0 ? void 0 : cap.minP) || 0,
|
|
224
|
+
maxP: (cap === null || cap === void 0 ? void 0 : cap.maxP) && cap.maxP > 0 ? cap.maxP : 1,
|
|
225
225
|
};
|
|
226
226
|
};
|
|
227
227
|
const getBorrowingDataActiveFeePerBlock = (val) => {
|
|
@@ -231,7 +231,7 @@ const getBorrowingDataActiveFeePerBlock = (val) => {
|
|
|
231
231
|
// If minP > 0 then netOi has to be at least minP * maxOi
|
|
232
232
|
// If maxP > 0 then netOi cannot be more than maxP * maxOi
|
|
233
233
|
const effectiveOi = Math.min(Math.max(Math.abs(long - short), max * minP), max * maxP);
|
|
234
|
-
return val.feePerBlock * (effectiveOi / max)
|
|
234
|
+
return val.feePerBlock * Math.pow((effectiveOi / max), val.feeExponent);
|
|
235
235
|
};
|
|
236
236
|
const getActiveFeePerBlock = (pair, group) => {
|
|
237
237
|
const pairFeePerBlock = getBorrowingDataActiveFeePerBlock(pair);
|
|
@@ -5,12 +5,13 @@ exports.buildBorrowingV2Context = void 0;
|
|
|
5
5
|
* @dev Builds borrowing v2 sub-context for a specific pair
|
|
6
6
|
*/
|
|
7
7
|
const buildBorrowingV2Context = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
|
|
8
|
+
var _a, _b;
|
|
8
9
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
9
|
-
if (!collateral
|
|
10
|
+
if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairBorrowingFeesV2)) {
|
|
10
11
|
return undefined;
|
|
11
12
|
}
|
|
12
|
-
const params = collateral.pairBorrowingFeesV2.params
|
|
13
|
-
const data = collateral.pairBorrowingFeesV2.data
|
|
13
|
+
const params = (_a = collateral.pairBorrowingFeesV2.params) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
14
|
+
const data = (_b = collateral.pairBorrowingFeesV2.data) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
14
15
|
if (!params || !data) {
|
|
15
16
|
return undefined;
|
|
16
17
|
}
|
|
@@ -116,7 +116,7 @@ exports.aprToBorrowingRate = aprToBorrowingRate;
|
|
|
116
116
|
*/
|
|
117
117
|
const createCollateralScopedBorrowingContext = (collateralBorrowingData, currentTimestamp) => {
|
|
118
118
|
const context = {
|
|
119
|
-
currentTimestamp: currentTimestamp
|
|
119
|
+
currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
|
|
120
120
|
borrowingParams: {},
|
|
121
121
|
borrowingData: {},
|
|
122
122
|
};
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = void 0;
|
|
4
13
|
const converter_1 = require("./converter");
|
|
@@ -9,19 +18,19 @@ const converter_1 = require("./converter");
|
|
|
9
18
|
* @param pairIndices Array of pair indices
|
|
10
19
|
* @returns Promise resolving to array of borrowing fee parameters
|
|
11
20
|
*/
|
|
12
|
-
const fetchBorrowingFeeParamsV2 =
|
|
21
|
+
const fetchBorrowingFeeParamsV2 = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
22
|
if (collateralIndices.length !== pairIndices.length) {
|
|
14
23
|
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
15
24
|
}
|
|
16
25
|
try {
|
|
17
|
-
const contractParams =
|
|
26
|
+
const contractParams = yield contract.getPairBorrowingFeeParams(collateralIndices, pairIndices);
|
|
18
27
|
return (0, converter_1.convertBorrowingFeeParamsArray)(contractParams);
|
|
19
28
|
}
|
|
20
29
|
catch (error) {
|
|
21
30
|
console.error("Error fetching borrowing fee params v2:", error);
|
|
22
31
|
throw error;
|
|
23
32
|
}
|
|
24
|
-
};
|
|
33
|
+
});
|
|
25
34
|
exports.fetchBorrowingFeeParamsV2 = fetchBorrowingFeeParamsV2;
|
|
26
35
|
/**
|
|
27
36
|
* @dev Fetches pair borrowing fee data v2 for specific pairs
|
|
@@ -30,19 +39,19 @@ exports.fetchBorrowingFeeParamsV2 = fetchBorrowingFeeParamsV2;
|
|
|
30
39
|
* @param pairIndices Array of pair indices
|
|
31
40
|
* @returns Promise resolving to array of pair borrowing fee data
|
|
32
41
|
*/
|
|
33
|
-
const fetchPairBorrowingFeeDataV2 =
|
|
42
|
+
const fetchPairBorrowingFeeDataV2 = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
43
|
if (collateralIndices.length !== pairIndices.length) {
|
|
35
44
|
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
36
45
|
}
|
|
37
46
|
try {
|
|
38
|
-
const contractData =
|
|
47
|
+
const contractData = yield contract.getPairBorrowingFeeData(collateralIndices, pairIndices);
|
|
39
48
|
return (0, converter_1.convertPairBorrowingFeeDataArray)(contractData);
|
|
40
49
|
}
|
|
41
50
|
catch (error) {
|
|
42
51
|
console.error("Error fetching pair borrowing fee data v2:", error);
|
|
43
52
|
throw error;
|
|
44
53
|
}
|
|
45
|
-
};
|
|
54
|
+
});
|
|
46
55
|
exports.fetchPairBorrowingFeeDataV2 = fetchPairBorrowingFeeDataV2;
|
|
47
56
|
/**
|
|
48
57
|
* @dev Fetches borrowing fees in collateral tokens for a specific trade
|
|
@@ -52,9 +61,9 @@ exports.fetchPairBorrowingFeeDataV2 = fetchPairBorrowingFeeDataV2;
|
|
|
52
61
|
* @param currentPairPrice Current price of the trading pair (1e6 precision)
|
|
53
62
|
* @returns Promise resolving to borrowing fees in collateral tokens
|
|
54
63
|
*/
|
|
55
|
-
const fetchTradeBorrowingFeesCollateralV2 =
|
|
64
|
+
const fetchTradeBorrowingFeesCollateralV2 = (contract, trader, index, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
65
|
try {
|
|
57
|
-
const feesCollateral =
|
|
66
|
+
const feesCollateral = yield contract.getTradeBorrowingFeesCollateral(trader, index, currentPairPrice);
|
|
58
67
|
// Convert BigNumber to normalized float
|
|
59
68
|
// Note: Collateral precision varies by chain, but contract returns proper precision
|
|
60
69
|
return parseFloat(feesCollateral.toString());
|
|
@@ -63,7 +72,7 @@ const fetchTradeBorrowingFeesCollateralV2 = async (contract, trader, index, curr
|
|
|
63
72
|
console.error("Error fetching trade borrowing fees collateral v2:", error);
|
|
64
73
|
throw error;
|
|
65
74
|
}
|
|
66
|
-
};
|
|
75
|
+
});
|
|
67
76
|
exports.fetchTradeBorrowingFeesCollateralV2 = fetchTradeBorrowingFeesCollateralV2;
|
|
68
77
|
/**
|
|
69
78
|
* @dev Fetches pending accumulated borrowing fees for a specific pair
|
|
@@ -73,9 +82,9 @@ exports.fetchTradeBorrowingFeesCollateralV2 = fetchTradeBorrowingFeesCollateralV
|
|
|
73
82
|
* @param currentPairPrice Current price of the trading pair (1e6 precision)
|
|
74
83
|
* @returns Promise resolving to pending accumulated borrowing fee
|
|
75
84
|
*/
|
|
76
|
-
const fetchPairPendingAccBorrowingFeesV2 =
|
|
85
|
+
const fetchPairPendingAccBorrowingFeesV2 = (contract, collateralIndex, pairIndex, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
86
|
try {
|
|
78
|
-
const accBorrowingFeeP =
|
|
87
|
+
const accBorrowingFeeP = yield contract.getPairPendingAccBorrowingFees(collateralIndex, pairIndex, currentPairPrice);
|
|
79
88
|
// Convert BigNumber to normalized float
|
|
80
89
|
return parseFloat(accBorrowingFeeP.toString()) / 1e20;
|
|
81
90
|
}
|
|
@@ -83,7 +92,7 @@ const fetchPairPendingAccBorrowingFeesV2 = async (contract, collateralIndex, pai
|
|
|
83
92
|
console.error("Error fetching pair pending acc borrowing fees v2:", error);
|
|
84
93
|
throw error;
|
|
85
94
|
}
|
|
86
|
-
};
|
|
95
|
+
});
|
|
87
96
|
exports.fetchPairPendingAccBorrowingFeesV2 = fetchPairPendingAccBorrowingFeesV2;
|
|
88
97
|
/**
|
|
89
98
|
* @dev Convenience function to fetch all borrowing v2 data for specific pairs
|
|
@@ -92,11 +101,11 @@ exports.fetchPairPendingAccBorrowingFeesV2 = fetchPairPendingAccBorrowingFeesV2;
|
|
|
92
101
|
* @param pairIndices Array of pair indices
|
|
93
102
|
* @returns Promise resolving to complete borrowing v2 data set
|
|
94
103
|
*/
|
|
95
|
-
const fetchAllBorrowingV2Data =
|
|
104
|
+
const fetchAllBorrowingV2Data = (contract, collateralIndex, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
96
105
|
const collateralIndices = new Array(pairIndices.length).fill(collateralIndex);
|
|
97
106
|
try {
|
|
98
107
|
// Fetch both parameters and data in parallel
|
|
99
|
-
const [params, data] =
|
|
108
|
+
const [params, data] = yield Promise.all([
|
|
100
109
|
(0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
|
|
101
110
|
(0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
|
|
102
111
|
]);
|
|
@@ -108,7 +117,7 @@ const fetchAllBorrowingV2Data = async (contract, collateralIndex, pairIndices) =
|
|
|
108
117
|
console.error("Error fetching all borrowing v2 data:", error);
|
|
109
118
|
throw error;
|
|
110
119
|
}
|
|
111
|
-
};
|
|
120
|
+
});
|
|
112
121
|
exports.fetchAllBorrowingV2Data = fetchAllBorrowingV2Data;
|
|
113
122
|
/**
|
|
114
123
|
* @dev Creates a complete borrowing v2 context from contract data
|
|
@@ -118,13 +127,10 @@ exports.fetchAllBorrowingV2Data = fetchAllBorrowingV2Data;
|
|
|
118
127
|
* @param currentTimestamp Optional current timestamp for calculations
|
|
119
128
|
* @returns Promise resolving to complete borrowing v2 context
|
|
120
129
|
*/
|
|
121
|
-
const createBorrowingV2ContextFromContract =
|
|
122
|
-
const { context } =
|
|
123
|
-
return {
|
|
124
|
-
|
|
125
|
-
currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
|
|
126
|
-
};
|
|
127
|
-
};
|
|
130
|
+
const createBorrowingV2ContextFromContract = (contract, collateralIndex, pairIndices, currentTimestamp) => __awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
const { context } = yield (0, exports.fetchAllBorrowingV2Data)(contract, collateralIndex, pairIndices);
|
|
132
|
+
return Object.assign(Object.assign({}, context), { currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000) });
|
|
133
|
+
});
|
|
128
134
|
exports.createBorrowingV2ContextFromContract = createBorrowingV2ContextFromContract;
|
|
129
135
|
/**
|
|
130
136
|
* @dev Helper function to create context from already fetched arrays
|
|
@@ -137,7 +143,7 @@ exports.createBorrowingV2ContextFromContract = createBorrowingV2ContextFromContr
|
|
|
137
143
|
*/
|
|
138
144
|
const createBorrowingV2ContextFromArrays = (collateralIndices, pairIndices, params, data, currentTimestamp) => {
|
|
139
145
|
const context = {
|
|
140
|
-
currentTimestamp: currentTimestamp
|
|
146
|
+
currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
|
|
141
147
|
borrowingParams: {},
|
|
142
148
|
borrowingData: {},
|
|
143
149
|
};
|
|
@@ -158,13 +164,13 @@ exports.createBorrowingV2ContextFromArrays = createBorrowingV2ContextFromArrays;
|
|
|
158
164
|
* @param pairIndices Array of pair indices (must match collateralIndices length)
|
|
159
165
|
* @returns Promise resolving to complete borrowing v2 context
|
|
160
166
|
*/
|
|
161
|
-
const fetchBorrowingV2DataForPairs =
|
|
167
|
+
const fetchBorrowingV2DataForPairs = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
162
168
|
if (collateralIndices.length !== pairIndices.length) {
|
|
163
169
|
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
164
170
|
}
|
|
165
171
|
try {
|
|
166
172
|
// Fetch both parameters and data in parallel
|
|
167
|
-
const [params, data] =
|
|
173
|
+
const [params, data] = yield Promise.all([
|
|
168
174
|
(0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
|
|
169
175
|
(0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
|
|
170
176
|
]);
|
|
@@ -175,5 +181,5 @@ const fetchBorrowingV2DataForPairs = async (contract, collateralIndices, pairInd
|
|
|
175
181
|
console.error("Error fetching borrowing v2 data for pairs:", error);
|
|
176
182
|
throw error;
|
|
177
183
|
}
|
|
178
|
-
};
|
|
184
|
+
});
|
|
179
185
|
exports.fetchBorrowingV2DataForPairs = fetchBorrowingV2DataForPairs;
|
|
@@ -48,7 +48,7 @@ exports.BORROWING_V2_PRECISION = {
|
|
|
48
48
|
* @returns Updated accumulated borrowing fee (1e20 precision)
|
|
49
49
|
*/
|
|
50
50
|
const getPairPendingAccBorrowingFees = (params, data, currentPairPrice, currentTimestamp) => {
|
|
51
|
-
const timestamp = currentTimestamp
|
|
51
|
+
const timestamp = currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000);
|
|
52
52
|
// Calculate time elapsed since last update
|
|
53
53
|
const timeElapsed = Math.max(0, timestamp - data.lastBorrowingUpdateTs);
|
|
54
54
|
// If no time elapsed, return current accumulated fee
|
|
@@ -74,7 +74,7 @@ const getTradeBorrowingFeesCollateral = (input, context) => {
|
|
|
74
74
|
return 0;
|
|
75
75
|
}
|
|
76
76
|
// Calculate current accumulated borrowing fees
|
|
77
|
-
const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp
|
|
77
|
+
const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : context.currentTimestamp);
|
|
78
78
|
// Calculate borrowing fees for this trade
|
|
79
79
|
// Formula: (positionSizeCollateral * (currentAccFee - initialAccFee)) / openPrice / 100
|
|
80
80
|
const feeDeltaP = currentAccBorrowingFeeP - initialAccBorrowingFeeP;
|
|
@@ -96,7 +96,7 @@ const getPairBorrowingFees = (input, context) => {
|
|
|
96
96
|
if (!params || !data) {
|
|
97
97
|
return 0;
|
|
98
98
|
}
|
|
99
|
-
return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp
|
|
99
|
+
return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : context.currentTimestamp);
|
|
100
100
|
};
|
|
101
101
|
exports.getPairBorrowingFees = getPairBorrowingFees;
|
|
102
102
|
/**
|
|
@@ -16,14 +16,14 @@ const convertTradeFeesData = (data, collateralConfig) => {
|
|
|
16
16
|
const decimals = collateralConfig.decimals || 18;
|
|
17
17
|
return {
|
|
18
18
|
realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
|
|
19
|
-
10
|
|
20
|
-
realizedPnlCollateral: parseFloat(data.realizedPnlCollateral.toString()) / 10
|
|
19
|
+
Math.pow(10, decimals),
|
|
20
|
+
realizedPnlCollateral: parseFloat(data.realizedPnlCollateral.toString()) / Math.pow(10, decimals),
|
|
21
21
|
manuallyRealizedNegativePnlCollateral: parseFloat(data.manuallyRealizedNegativePnlCollateral.toString()) /
|
|
22
|
-
10
|
|
22
|
+
Math.pow(10, decimals),
|
|
23
23
|
alreadyTransferredNegativePnlCollateral: parseFloat(data.alreadyTransferredNegativePnlCollateral.toString()) /
|
|
24
|
-
10
|
|
24
|
+
Math.pow(10, decimals),
|
|
25
25
|
virtualAvailableCollateralInDiamond: parseFloat(data.virtualAvailableCollateralInDiamond.toString()) /
|
|
26
|
-
10
|
|
26
|
+
Math.pow(10, decimals),
|
|
27
27
|
initialAccFundingFeeP: parseFloat(data.initialAccFundingFeeP.toString()) /
|
|
28
28
|
converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P,
|
|
29
29
|
initialAccBorrowingFeeP: parseFloat(data.initialAccBorrowingFeeP.toString()) /
|
|
@@ -51,16 +51,16 @@ const convertUiRealizedPnlData = (data, collateralConfig) => {
|
|
|
51
51
|
const decimals = collateralConfig.decimals || 18;
|
|
52
52
|
return {
|
|
53
53
|
realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
|
|
54
|
-
10
|
|
54
|
+
Math.pow(10, decimals),
|
|
55
55
|
realizedOldBorrowingFeesCollateral: parseFloat(data.realizedOldBorrowingFeesCollateral.toString()) /
|
|
56
|
-
10
|
|
56
|
+
Math.pow(10, decimals),
|
|
57
57
|
realizedNewBorrowingFeesCollateral: parseFloat(data.realizedNewBorrowingFeesCollateral.toString()) /
|
|
58
|
-
10
|
|
58
|
+
Math.pow(10, decimals),
|
|
59
59
|
realizedFundingFeesCollateral: parseFloat(data.realizedFundingFeesCollateral.toString()) /
|
|
60
|
-
10
|
|
60
|
+
Math.pow(10, decimals),
|
|
61
61
|
realizedPnlPartialCloseCollateral: parseFloat(data.realizedPnlPartialCloseCollateral.toString()) /
|
|
62
|
-
10
|
|
63
|
-
pnlWithdrawnCollateral: parseFloat(data.pnlWithdrawnCollateral.toString()) / 10
|
|
62
|
+
Math.pow(10, decimals),
|
|
63
|
+
pnlWithdrawnCollateral: parseFloat(data.pnlWithdrawnCollateral.toString()) / Math.pow(10, decimals),
|
|
64
64
|
};
|
|
65
65
|
};
|
|
66
66
|
exports.convertUiRealizedPnlData = convertUiRealizedPnlData;
|
|
@@ -83,11 +83,11 @@ exports.convertUiRealizedPnlDataArray = convertUiRealizedPnlDataArray;
|
|
|
83
83
|
const encodeTradeFeesData = (data, collateralConfig) => {
|
|
84
84
|
const decimals = collateralConfig.decimals || 18;
|
|
85
85
|
return {
|
|
86
|
-
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * 10
|
|
87
|
-
realizedPnlCollateral: Math.round(data.realizedPnlCollateral * 10
|
|
88
|
-
manuallyRealizedNegativePnlCollateral: Math.round(data.manuallyRealizedNegativePnlCollateral * 10
|
|
89
|
-
alreadyTransferredNegativePnlCollateral: Math.round(data.alreadyTransferredNegativePnlCollateral * 10
|
|
90
|
-
virtualAvailableCollateralInDiamond: Math.round(data.virtualAvailableCollateralInDiamond * 10
|
|
86
|
+
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * Math.pow(10, decimals)),
|
|
87
|
+
realizedPnlCollateral: Math.round(data.realizedPnlCollateral * Math.pow(10, decimals)),
|
|
88
|
+
manuallyRealizedNegativePnlCollateral: Math.round(data.manuallyRealizedNegativePnlCollateral * Math.pow(10, decimals)),
|
|
89
|
+
alreadyTransferredNegativePnlCollateral: Math.round(data.alreadyTransferredNegativePnlCollateral * Math.pow(10, decimals)),
|
|
90
|
+
virtualAvailableCollateralInDiamond: Math.round(data.virtualAvailableCollateralInDiamond * Math.pow(10, decimals)),
|
|
91
91
|
__placeholder: 0,
|
|
92
92
|
initialAccFundingFeeP: Math.round(data.initialAccFundingFeeP * converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P),
|
|
93
93
|
initialAccBorrowingFeeP: Math.round(data.initialAccBorrowingFeeP * borrowingV2_1.BORROWING_V2_PRECISION.ACC_FEE),
|
|
@@ -103,12 +103,12 @@ exports.encodeTradeFeesData = encodeTradeFeesData;
|
|
|
103
103
|
const encodeUiRealizedPnlData = (data, collateralConfig) => {
|
|
104
104
|
const decimals = collateralConfig.decimals || 18;
|
|
105
105
|
return {
|
|
106
|
-
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * 10
|
|
107
|
-
realizedOldBorrowingFeesCollateral: Math.round(data.realizedOldBorrowingFeesCollateral * 10
|
|
108
|
-
realizedNewBorrowingFeesCollateral: Math.round(data.realizedNewBorrowingFeesCollateral * 10
|
|
109
|
-
realizedFundingFeesCollateral: Math.round(data.realizedFundingFeesCollateral * 10
|
|
110
|
-
realizedPnlPartialCloseCollateral: Math.round(data.realizedPnlPartialCloseCollateral * 10
|
|
111
|
-
pnlWithdrawnCollateral: Math.round(data.pnlWithdrawnCollateral * 10
|
|
106
|
+
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * Math.pow(10, decimals)),
|
|
107
|
+
realizedOldBorrowingFeesCollateral: Math.round(data.realizedOldBorrowingFeesCollateral * Math.pow(10, decimals)),
|
|
108
|
+
realizedNewBorrowingFeesCollateral: Math.round(data.realizedNewBorrowingFeesCollateral * Math.pow(10, decimals)),
|
|
109
|
+
realizedFundingFeesCollateral: Math.round(data.realizedFundingFeesCollateral * Math.pow(10, decimals)),
|
|
110
|
+
realizedPnlPartialCloseCollateral: Math.round(data.realizedPnlPartialCloseCollateral * Math.pow(10, decimals)),
|
|
111
|
+
pnlWithdrawnCollateral: Math.round(data.pnlWithdrawnCollateral * Math.pow(10, decimals)),
|
|
112
112
|
};
|
|
113
113
|
};
|
|
114
114
|
exports.encodeUiRealizedPnlData = encodeUiRealizedPnlData;
|
|
@@ -9,13 +9,14 @@ const index_1 = require("../../../markets/oi/index");
|
|
|
9
9
|
* @dev Builds funding fees sub-context for a specific pair
|
|
10
10
|
*/
|
|
11
11
|
const buildFundingContext = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
|
|
12
|
+
var _a, _b, _c, _d, _e;
|
|
12
13
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
13
|
-
if (!collateral
|
|
14
|
+
if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairFundingFees)) {
|
|
14
15
|
return undefined;
|
|
15
16
|
}
|
|
16
|
-
const params = collateral.pairFundingFees.params
|
|
17
|
-
const data = collateral.pairFundingFees.data
|
|
18
|
-
const pairOi = collateral.pairOis
|
|
17
|
+
const params = (_a = collateral.pairFundingFees.params) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
18
|
+
const data = (_b = collateral.pairFundingFees.data) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
19
|
+
const pairOi = (_c = collateral.pairOis) === null || _c === void 0 ? void 0 : _c[pairIndex];
|
|
19
20
|
const netExposureToken = (0, index_1.getPairV10OiTokenSkewCollateral)(pairIndex, {
|
|
20
21
|
pairOis: collateral.pairOis,
|
|
21
22
|
});
|
|
@@ -27,8 +28,8 @@ const buildFundingContext = (globalTradingVariables, collateralIndex, pairIndex,
|
|
|
27
28
|
data,
|
|
28
29
|
pairOi: pairOi
|
|
29
30
|
? {
|
|
30
|
-
oiLongToken: pairOi.token
|
|
31
|
-
oiShortToken: pairOi.token
|
|
31
|
+
oiLongToken: ((_d = pairOi.token) === null || _d === void 0 ? void 0 : _d.long) || 0,
|
|
32
|
+
oiShortToken: ((_e = pairOi.token) === null || _e === void 0 ? void 0 : _e.short) || 0,
|
|
32
33
|
}
|
|
33
34
|
: undefined,
|
|
34
35
|
currentTimestamp,
|
|
@@ -111,7 +111,7 @@ exports.convertTradeInitialAccFundingFees = convertTradeInitialAccFundingFees;
|
|
|
111
111
|
*/
|
|
112
112
|
const createFundingFeeContext = (collateralIndices, pairIndices, params, data, globalParams, currentTimestamp) => {
|
|
113
113
|
const context = {
|
|
114
|
-
currentTimestamp: currentTimestamp
|
|
114
|
+
currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
|
|
115
115
|
fundingParams: {},
|
|
116
116
|
fundingData: {},
|
|
117
117
|
globalParams: globalParams ? {} : undefined,
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.collateralToContractFormat = exports.priceToContractFormat = exports.fetchPairPendingAccFundingFeesBatch = exports.fetchTradeFeesDataBatch = exports.fetchTradeFeesData = exports.fetchTradeFundingFeesCollateral = exports.fetchPairPendingAccFundingFees = void 0;
|
|
4
13
|
/**
|
|
@@ -9,9 +18,9 @@ exports.collateralToContractFormat = exports.priceToContractFormat = exports.fet
|
|
|
9
18
|
* @param currentPairPrice Current pair price (1e10)
|
|
10
19
|
* @returns Promise resolving to accumulated funding fees and current rate
|
|
11
20
|
*/
|
|
12
|
-
const fetchPairPendingAccFundingFees =
|
|
21
|
+
const fetchPairPendingAccFundingFees = (contract, collateralIndex, pairIndex, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
22
|
try {
|
|
14
|
-
const result =
|
|
23
|
+
const result = yield contract.getPairPendingAccFundingFees(collateralIndex, pairIndex, currentPairPrice);
|
|
15
24
|
return {
|
|
16
25
|
accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
|
|
17
26
|
accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
|
|
@@ -22,7 +31,7 @@ const fetchPairPendingAccFundingFees = async (contract, collateralIndex, pairInd
|
|
|
22
31
|
console.error("Error fetching pair pending acc funding fees:", error);
|
|
23
32
|
throw error;
|
|
24
33
|
}
|
|
25
|
-
};
|
|
34
|
+
});
|
|
26
35
|
exports.fetchPairPendingAccFundingFees = fetchPairPendingAccFundingFees;
|
|
27
36
|
/**
|
|
28
37
|
* @dev Fetches funding fees for a specific trade in collateral tokens
|
|
@@ -32,9 +41,9 @@ exports.fetchPairPendingAccFundingFees = fetchPairPendingAccFundingFees;
|
|
|
32
41
|
* @param currentPairPrice Current pair price (1e10)
|
|
33
42
|
* @returns Promise resolving to funding fee in collateral tokens
|
|
34
43
|
*/
|
|
35
|
-
const fetchTradeFundingFeesCollateral =
|
|
44
|
+
const fetchTradeFundingFeesCollateral = (contract, trader, index, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
45
|
try {
|
|
37
|
-
const fundingFeeCollateral =
|
|
46
|
+
const fundingFeeCollateral = yield contract.getTradeFundingFeesCollateral(trader, index, currentPairPrice);
|
|
38
47
|
// Convert from BigNumber to number (collateral precision already applied)
|
|
39
48
|
return Number(fundingFeeCollateral);
|
|
40
49
|
}
|
|
@@ -42,7 +51,7 @@ const fetchTradeFundingFeesCollateral = async (contract, trader, index, currentP
|
|
|
42
51
|
console.error("Error fetching trade funding fees:", error);
|
|
43
52
|
throw error;
|
|
44
53
|
}
|
|
45
|
-
};
|
|
54
|
+
});
|
|
46
55
|
exports.fetchTradeFundingFeesCollateral = fetchTradeFundingFeesCollateral;
|
|
47
56
|
/**
|
|
48
57
|
* @dev Fetches trade fees data for a specific trade
|
|
@@ -51,9 +60,9 @@ exports.fetchTradeFundingFeesCollateral = fetchTradeFundingFeesCollateral;
|
|
|
51
60
|
* @param index Trade index
|
|
52
61
|
* @returns Promise resolving to trade fees data
|
|
53
62
|
*/
|
|
54
|
-
const fetchTradeFeesData =
|
|
63
|
+
const fetchTradeFeesData = (contract, trader, index) => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
64
|
try {
|
|
56
|
-
const feesData =
|
|
65
|
+
const feesData = yield contract.getTradeFeesData(trader, index);
|
|
57
66
|
return {
|
|
58
67
|
accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
59
68
|
accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
@@ -64,7 +73,7 @@ const fetchTradeFeesData = async (contract, trader, index) => {
|
|
|
64
73
|
console.error("Error fetching trade fees data:", error);
|
|
65
74
|
throw error;
|
|
66
75
|
}
|
|
67
|
-
};
|
|
76
|
+
});
|
|
68
77
|
exports.fetchTradeFeesData = fetchTradeFeesData;
|
|
69
78
|
/**
|
|
70
79
|
* @dev Fetches trade fees data for multiple trades
|
|
@@ -73,12 +82,12 @@ exports.fetchTradeFeesData = fetchTradeFeesData;
|
|
|
73
82
|
* @param indices Array of trade indices
|
|
74
83
|
* @returns Promise resolving to array of trade fees data
|
|
75
84
|
*/
|
|
76
|
-
const fetchTradeFeesDataBatch =
|
|
85
|
+
const fetchTradeFeesDataBatch = (contract, traders, indices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
86
|
if (traders.length !== indices.length) {
|
|
78
87
|
throw new Error("Traders and indices arrays must have the same length");
|
|
79
88
|
}
|
|
80
89
|
try {
|
|
81
|
-
const feesDatas =
|
|
90
|
+
const feesDatas = yield contract.getTradeFeesDataArray(traders, indices);
|
|
82
91
|
return feesDatas.map(feesData => ({
|
|
83
92
|
accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
84
93
|
accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
@@ -89,7 +98,7 @@ const fetchTradeFeesDataBatch = async (contract, traders, indices) => {
|
|
|
89
98
|
console.error("Error fetching trade fees data batch:", error);
|
|
90
99
|
throw error;
|
|
91
100
|
}
|
|
92
|
-
};
|
|
101
|
+
});
|
|
93
102
|
exports.fetchTradeFeesDataBatch = fetchTradeFeesDataBatch;
|
|
94
103
|
/**
|
|
95
104
|
* @dev Fetches pending accumulated funding fees for multiple pairs
|
|
@@ -99,7 +108,7 @@ exports.fetchTradeFeesDataBatch = fetchTradeFeesDataBatch;
|
|
|
99
108
|
* @param currentPairPrices Array of current pair prices (1e10)
|
|
100
109
|
* @returns Promise resolving to array of accumulated funding fees
|
|
101
110
|
*/
|
|
102
|
-
const fetchPairPendingAccFundingFeesBatch =
|
|
111
|
+
const fetchPairPendingAccFundingFeesBatch = (contract, collateralIndices, pairIndices, currentPairPrices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
103
112
|
if (collateralIndices.length !== pairIndices.length ||
|
|
104
113
|
pairIndices.length !== currentPairPrices.length) {
|
|
105
114
|
throw new Error("All input arrays must have the same length");
|
|
@@ -107,7 +116,7 @@ const fetchPairPendingAccFundingFeesBatch = async (contract, collateralIndices,
|
|
|
107
116
|
try {
|
|
108
117
|
// Fetch all in parallel
|
|
109
118
|
const promises = collateralIndices.map((collateralIndex, i) => contract.getPairPendingAccFundingFees(collateralIndex, pairIndices[i], currentPairPrices[i]));
|
|
110
|
-
const results =
|
|
119
|
+
const results = yield Promise.all(promises);
|
|
111
120
|
return results.map(result => ({
|
|
112
121
|
accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
|
|
113
122
|
accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
|
|
@@ -118,7 +127,7 @@ const fetchPairPendingAccFundingFeesBatch = async (contract, collateralIndices,
|
|
|
118
127
|
console.error("Error fetching pair pending acc funding fees batch:", error);
|
|
119
128
|
throw error;
|
|
120
129
|
}
|
|
121
|
-
};
|
|
130
|
+
});
|
|
122
131
|
exports.fetchPairPendingAccFundingFeesBatch = fetchPairPendingAccFundingFeesBatch;
|
|
123
132
|
/**
|
|
124
133
|
* @dev Helper to convert price from number to contract format
|
|
@@ -136,6 +145,6 @@ exports.priceToContractFormat = priceToContractFormat;
|
|
|
136
145
|
* @returns Amount in contract format
|
|
137
146
|
*/
|
|
138
147
|
const collateralToContractFormat = (amount, decimals) => {
|
|
139
|
-
return BigInt(Math.round(amount * 10
|
|
148
|
+
return BigInt(Math.round(amount * Math.pow(10, decimals)));
|
|
140
149
|
};
|
|
141
150
|
exports.collateralToContractFormat = collateralToContractFormat;
|
|
@@ -259,9 +259,10 @@ exports.getTradeFundingFeesCollateral = getTradeFundingFeesCollateral;
|
|
|
259
259
|
* @returns Complete funding fee calculation result
|
|
260
260
|
*/
|
|
261
261
|
const getTradeFundingFees = (input, context) => {
|
|
262
|
+
var _a, _b;
|
|
262
263
|
// Get params and data from context
|
|
263
|
-
const params = context.fundingParams[input.collateralIndex]
|
|
264
|
-
const data = context.fundingData[input.collateralIndex]
|
|
264
|
+
const params = (_a = context.fundingParams[input.collateralIndex]) === null || _a === void 0 ? void 0 : _a[input.pairIndex];
|
|
265
|
+
const data = (_b = context.fundingData[input.collateralIndex]) === null || _b === void 0 ? void 0 : _b[input.pairIndex];
|
|
265
266
|
if (!params || !data) {
|
|
266
267
|
throw new Error(`Missing funding fee data for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
267
268
|
}
|