@gainsnetwork/sdk 0.2.73-rc2 → 1.0.0-rc10
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/README.md +2 -2
- package/lib/backend/globalTrades/index.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +3 -0
- package/lib/backend/index.js +28 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +318 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +34 -0
- package/lib/backend/tradingVariables/converter.js +338 -0
- package/lib/backend/tradingVariables/index.d.ts +5 -0
- package/lib/backend/tradingVariables/index.js +96 -0
- package/lib/backend/tradingVariables/types.d.ts +113 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.d.ts +0 -5
- package/lib/constants.js +0 -5
- 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 +2134 -293
- 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/GToken.d.ts +78 -107
- 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 +90 -53
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +4496 -430
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
- package/lib/contracts/types/generated/factories/GToken__factory.js +69 -142
- 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 +0 -5
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -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/holdingFees/index.d.ts +46 -0
- package/lib/markets/holdingFees/index.js +105 -0
- package/lib/markets/holdingFees/types.d.ts +23 -0
- package/lib/markets/holdingFees/types.js +5 -0
- package/lib/markets/index.d.ts +5 -0
- package/lib/markets/index.js +5 -0
- package/lib/markets/leverage/builder.d.ts +12 -0
- package/lib/markets/leverage/builder.js +25 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.d.ts +7 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.js +38 -0
- package/lib/markets/leverage/index.d.ts +3 -0
- package/lib/markets/leverage/index.js +19 -0
- package/lib/markets/leverage/types.d.ts +15 -0
- package/lib/markets/leverage/types.js +2 -0
- package/lib/markets/oi/converter.d.ts +62 -0
- package/lib/markets/oi/converter.js +102 -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 +49 -0
- package/lib/markets/oi/index.js +77 -0
- package/lib/markets/oi/types.d.ts +73 -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/markets/price/builder.d.ts +25 -0
- package/lib/markets/price/builder.js +69 -0
- package/lib/markets/price/index.d.ts +6 -0
- package/lib/markets/price/index.js +22 -0
- package/lib/markets/price/marketPrice.d.ts +13 -0
- package/lib/markets/price/marketPrice.js +35 -0
- package/lib/markets/price/types.d.ts +23 -0
- package/lib/markets/price/types.js +5 -0
- package/lib/trade/counterTrade/index.d.ts +2 -0
- package/lib/trade/counterTrade/index.js +18 -0
- package/lib/trade/counterTrade/types.d.ts +7 -0
- package/lib/trade/counterTrade/types.js +2 -0
- package/lib/trade/counterTrade/validateCounterTrade.d.ts +10 -0
- package/lib/trade/counterTrade/validateCounterTrade.js +29 -0
- package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
- package/lib/trade/effectiveLeverage/builder.js +30 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.d.ts +23 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.js +64 -0
- package/lib/trade/effectiveLeverage/index.d.ts +3 -0
- package/lib/trade/effectiveLeverage/index.js +22 -0
- package/lib/trade/effectiveLeverage/types.d.ts +33 -0
- package/lib/trade/effectiveLeverage/types.js +2 -0
- package/lib/trade/fees/borrowing/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +34 -0
- package/lib/trade/fees/borrowing/index.d.ts +26 -3
- package/lib/trade/fees/borrowing/index.js +72 -22
- package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
- package/lib/trade/fees/borrowingV2/builder.js +24 -0
- package/lib/trade/fees/borrowingV2/converter.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/converter.js +132 -0
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/fetcher.js +185 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +48 -0
- package/lib/trade/fees/borrowingV2/index.js +112 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +95 -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 +114 -0
- package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
- package/lib/trade/fees/fundingFees/builder.js +35 -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 +124 -0
- package/lib/trade/fees/fundingFees/index.js +309 -0
- package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
- package/lib/trade/fees/fundingFees/pairContext.js +17 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- 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/index.d.ts +9 -2
- package/lib/trade/fees/index.js +69 -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/builder.d.ts +18 -0
- package/lib/trade/fees/trading/builder.js +20 -0
- package/lib/trade/fees/trading/converter.d.ts +32 -0
- package/lib/trade/fees/trading/converter.js +47 -0
- 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.d.ts +62 -0
- package/lib/trade/fees/trading/index.js +155 -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 +5 -2
- package/lib/trade/index.js +5 -2
- package/lib/trade/liquidation/builder.d.ts +25 -0
- package/lib/trade/liquidation/builder.js +59 -0
- package/lib/trade/liquidation/converter.d.ts +23 -0
- package/lib/trade/liquidation/converter.js +46 -0
- package/lib/trade/liquidation/index.d.ts +29 -0
- package/lib/trade/liquidation/index.js +218 -0
- package/lib/trade/liquidation/types.d.ts +43 -0
- package/lib/trade/liquidation/types.js +2 -0
- package/lib/trade/pnl/builder.d.ts +16 -0
- package/lib/trade/pnl/builder.js +44 -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 +91 -0
- package/lib/trade/pnl/index.js +303 -0
- package/lib/trade/pnl/types.d.ts +79 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/builder.d.ts +23 -0
- package/lib/trade/priceImpact/close/builder.js +45 -0
- package/lib/trade/priceImpact/close/index.d.ts +22 -0
- package/lib/trade/priceImpact/close/index.js +134 -0
- package/lib/trade/priceImpact/close/types.d.ts +47 -0
- package/lib/trade/priceImpact/close/types.js +5 -0
- package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
- package/lib/trade/priceImpact/cumulVol/builder.js +43 -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 +108 -0
- package/lib/trade/priceImpact/cumulVol/index.js +235 -0
- package/lib/trade/priceImpact/index.d.ts +21 -0
- package/lib/trade/priceImpact/index.js +79 -0
- package/lib/trade/priceImpact/open/builder.d.ts +21 -0
- package/lib/trade/priceImpact/open/builder.js +43 -0
- package/lib/trade/priceImpact/open/index.d.ts +23 -0
- package/lib/trade/priceImpact/open/index.js +79 -0
- package/lib/trade/priceImpact/open/types.d.ts +45 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
- package/lib/trade/priceImpact/skew/builder.js +28 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +46 -0
- package/lib/trade/priceImpact/skew/converter.js +81 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +60 -0
- package/lib/trade/priceImpact/skew/fetcher.js +169 -0
- package/lib/trade/priceImpact/skew/index.d.ts +53 -0
- package/lib/trade/priceImpact/skew/index.js +148 -0
- package/lib/trade/priceImpact/skew/types.d.ts +44 -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 +109 -14
- package/lib/trade/types.js +0 -5
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +2 -2
- package/package.json +2 -1
- package/lib/trade/liquidation.d.ts +0 -12
- package/lib/trade/liquidation.js +0 -55
- package/lib/trade/pnl.d.ts +0 -10
- package/lib/trade/pnl.js +0 -33
|
@@ -0,0 +1,185 @@
|
|
|
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 objects indexed by pairIndex (collateral-scoped)
|
|
151
|
+
for (let i = 0; i < pairIndices.length; i++) {
|
|
152
|
+
const pairIndex = pairIndices[i];
|
|
153
|
+
// Store data indexed by pairIndex
|
|
154
|
+
context.borrowingParams[pairIndex] = params[i];
|
|
155
|
+
context.borrowingData[pairIndex] = data[i];
|
|
156
|
+
}
|
|
157
|
+
return context;
|
|
158
|
+
};
|
|
159
|
+
exports.createBorrowingV2ContextFromArrays = createBorrowingV2ContextFromArrays;
|
|
160
|
+
/**
|
|
161
|
+
* @dev Fetches borrowing v2 data for multiple collateral/pair combinations
|
|
162
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
163
|
+
* @param collateralIndices Array of collateral indices
|
|
164
|
+
* @param pairIndices Array of pair indices (must match collateralIndices length)
|
|
165
|
+
* @returns Promise resolving to complete borrowing v2 context
|
|
166
|
+
*/
|
|
167
|
+
const fetchBorrowingV2DataForPairs = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
168
|
+
if (collateralIndices.length !== pairIndices.length) {
|
|
169
|
+
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
// Fetch both parameters and data in parallel
|
|
173
|
+
const [params, data] = yield Promise.all([
|
|
174
|
+
(0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
|
|
175
|
+
(0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
|
|
176
|
+
]);
|
|
177
|
+
// Create and return context
|
|
178
|
+
return (0, exports.createBorrowingV2ContextFromArrays)(collateralIndices, pairIndices, params, data);
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
console.error("Error fetching borrowing v2 data for pairs:", error);
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
exports.fetchBorrowingV2DataForPairs = fetchBorrowingV2DataForPairs;
|
|
@@ -0,0 +1,48 @@
|
|
|
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 (without pairIndex)
|
|
26
|
+
* @param context Pair-specific borrowing context
|
|
27
|
+
* @returns Borrowing fees in collateral tokens
|
|
28
|
+
*/
|
|
29
|
+
export declare const getTradeBorrowingFeesCollateral: (input: Omit<BorrowingFeeV2.TradeBorrowingFeeInput, "pairIndex">, context: BorrowingFeeV2.GetPairBorrowingFeeV2Context) => number;
|
|
30
|
+
/**
|
|
31
|
+
* @dev Utility function to get pending accumulated borrowing fees for a pair using context
|
|
32
|
+
* @param input Pair borrowing fee calculation input
|
|
33
|
+
* @param context Context containing borrowing parameters and data
|
|
34
|
+
* @returns Updated accumulated borrowing fee (1e20 precision)
|
|
35
|
+
*/
|
|
36
|
+
export declare const getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
37
|
+
/**
|
|
38
|
+
* @dev Utility functions for working with borrowing v2 fees
|
|
39
|
+
*/
|
|
40
|
+
export declare const borrowingFeeV2Utils: {
|
|
41
|
+
getPairPendingAccBorrowingFees: (params: BorrowingFeeV2.BorrowingFeeParams, data: BorrowingFeeV2.PairBorrowingFeeData, currentPairPrice: number, currentTimestamp?: number) => number;
|
|
42
|
+
getTradeBorrowingFeesCollateral: (input: Omit<BorrowingFeeV2.TradeBorrowingFeeInput, "pairIndex">, context: BorrowingFeeV2.GetPairBorrowingFeeV2Context) => number;
|
|
43
|
+
getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
44
|
+
};
|
|
45
|
+
export * as BorrowingFeeV2 from "./types";
|
|
46
|
+
export { GetPairBorrowingFeeV2Context } from "./types";
|
|
47
|
+
export * from "./converter";
|
|
48
|
+
export * from "./fetcher";
|
|
@@ -0,0 +1,112 @@
|
|
|
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.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 (without pairIndex)
|
|
67
|
+
* @param context Pair-specific borrowing context
|
|
68
|
+
* @returns Borrowing fees in collateral tokens
|
|
69
|
+
*/
|
|
70
|
+
const getTradeBorrowingFeesCollateral = (input, context) => {
|
|
71
|
+
const { positionSizeCollateral, openPrice, currentPairPrice, initialAccBorrowingFeeP, currentTimestamp, } = input;
|
|
72
|
+
const { params, data } = context;
|
|
73
|
+
if (!params || !data) {
|
|
74
|
+
return 0;
|
|
75
|
+
}
|
|
76
|
+
// Calculate current accumulated borrowing fees
|
|
77
|
+
const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : context.currentTimestamp);
|
|
78
|
+
// Calculate borrowing fees for this trade
|
|
79
|
+
// Formula: (positionSizeCollateral * (currentAccFee - initialAccFee)) / openPrice / 100
|
|
80
|
+
const feeDeltaP = currentAccBorrowingFeeP - initialAccBorrowingFeeP;
|
|
81
|
+
return ((positionSizeCollateral * feeDeltaP) /
|
|
82
|
+
openPrice /
|
|
83
|
+
exports.BORROWING_V2_PRECISION.PERCENTAGE);
|
|
84
|
+
};
|
|
85
|
+
exports.getTradeBorrowingFeesCollateral = getTradeBorrowingFeesCollateral;
|
|
86
|
+
/**
|
|
87
|
+
* @dev Utility function to get pending accumulated borrowing fees for a pair using context
|
|
88
|
+
* @param input Pair borrowing fee calculation input
|
|
89
|
+
* @param context Context containing borrowing parameters and data
|
|
90
|
+
* @returns Updated accumulated borrowing fee (1e20 precision)
|
|
91
|
+
*/
|
|
92
|
+
const getPairBorrowingFees = (input, context) => {
|
|
93
|
+
const { pairIndex, currentPairPrice, currentTimestamp } = input;
|
|
94
|
+
const params = context.borrowingParams[pairIndex];
|
|
95
|
+
const data = context.borrowingData[pairIndex];
|
|
96
|
+
if (!params || !data) {
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : context.currentTimestamp);
|
|
100
|
+
};
|
|
101
|
+
exports.getPairBorrowingFees = getPairBorrowingFees;
|
|
102
|
+
/**
|
|
103
|
+
* @dev Utility functions for working with borrowing v2 fees
|
|
104
|
+
*/
|
|
105
|
+
exports.borrowingFeeV2Utils = {
|
|
106
|
+
getPairPendingAccBorrowingFees: exports.getPairPendingAccBorrowingFees,
|
|
107
|
+
getTradeBorrowingFeesCollateral: exports.getTradeBorrowingFeesCollateral,
|
|
108
|
+
getPairBorrowingFees: exports.getPairBorrowingFees,
|
|
109
|
+
};
|
|
110
|
+
exports.BorrowingFeeV2 = __importStar(require("./types"));
|
|
111
|
+
__exportStar(require("./converter"), exports);
|
|
112
|
+
__exportStar(require("./fetcher"), exports);
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Types for borrowing v2 fees system (simplified rate-based model)
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @dev Borrowing fee parameters for a specific pair/collateral combination
|
|
6
|
+
*/
|
|
7
|
+
export type BorrowingFeeParams = {
|
|
8
|
+
/**
|
|
9
|
+
* @dev Borrowing rate % per second
|
|
10
|
+
* @dev Raw contract value divided by 1e10
|
|
11
|
+
* @dev Max: ~0.0000317097 => 1,000% APR
|
|
12
|
+
*/
|
|
13
|
+
borrowingRatePerSecondP: number;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* @dev Accumulated borrowing fee data for a specific pair/collateral combination
|
|
17
|
+
*/
|
|
18
|
+
export type PairBorrowingFeeData = {
|
|
19
|
+
/**
|
|
20
|
+
* @dev Accumulated borrowing fees % weighted by pair price
|
|
21
|
+
* @dev Unit: fee collateral per 100 units of OI in pair amount
|
|
22
|
+
*/
|
|
23
|
+
accBorrowingFeeP: number;
|
|
24
|
+
/**
|
|
25
|
+
* @dev Timestamp of last accumulated borrowing fees update
|
|
26
|
+
*/
|
|
27
|
+
lastBorrowingUpdateTs: number;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* @dev Trade-specific borrowing fee data stored when position is opened
|
|
31
|
+
*/
|
|
32
|
+
export type TradeInitialAccFees = {
|
|
33
|
+
/**
|
|
34
|
+
* @dev Initial accumulated borrowing fee when trade was opened (normalized float)
|
|
35
|
+
* @dev Raw contract value divided by 1e20
|
|
36
|
+
* @dev Used to calculate how much borrowing fees the trade owes
|
|
37
|
+
*/
|
|
38
|
+
initialAccBorrowingFeeP: number;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* @dev Context required for borrowing v2 fee calculations
|
|
42
|
+
* @dev Context is already scoped to a specific collateral
|
|
43
|
+
*/
|
|
44
|
+
export type GetBorrowingFeeV2Context = {
|
|
45
|
+
/**
|
|
46
|
+
* @dev Current timestamp (defaults to Date.now() / 1000)
|
|
47
|
+
*/
|
|
48
|
+
currentTimestamp?: number;
|
|
49
|
+
/**
|
|
50
|
+
* @dev Borrowing fee parameters for pairs
|
|
51
|
+
* @dev Indexed by: params[pairIndex]
|
|
52
|
+
*/
|
|
53
|
+
borrowingParams: Record<number, BorrowingFeeParams>;
|
|
54
|
+
/**
|
|
55
|
+
* @dev Borrowing fee data for pairs
|
|
56
|
+
* @dev Indexed by: data[pairIndex]
|
|
57
|
+
*/
|
|
58
|
+
borrowingData: Record<number, PairBorrowingFeeData>;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* @dev Context for pair-specific borrowing v2 fee calculations
|
|
62
|
+
*/
|
|
63
|
+
export type GetPairBorrowingFeeV2Context = {
|
|
64
|
+
/**
|
|
65
|
+
* @dev Current timestamp (defaults to Date.now() / 1000)
|
|
66
|
+
*/
|
|
67
|
+
currentTimestamp?: number;
|
|
68
|
+
/**
|
|
69
|
+
* @dev Borrowing fee parameters for this specific pair
|
|
70
|
+
*/
|
|
71
|
+
params: BorrowingFeeParams;
|
|
72
|
+
/**
|
|
73
|
+
* @dev Borrowing fee data for this specific pair
|
|
74
|
+
*/
|
|
75
|
+
data: PairBorrowingFeeData;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* @dev Input for calculating pending borrowing fees for a pair
|
|
79
|
+
*/
|
|
80
|
+
export type PairBorrowingFeeInput = {
|
|
81
|
+
pairIndex: number;
|
|
82
|
+
currentPairPrice: number;
|
|
83
|
+
currentTimestamp?: number;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* @dev Input for calculating borrowing fees for a specific trade
|
|
87
|
+
*/
|
|
88
|
+
export type TradeBorrowingFeeInput = {
|
|
89
|
+
positionSizeCollateral: number;
|
|
90
|
+
openPrice: number;
|
|
91
|
+
pairIndex: number;
|
|
92
|
+
currentPairPrice: number;
|
|
93
|
+
initialAccBorrowingFeeP: number;
|
|
94
|
+
currentTimestamp?: number;
|
|
95
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Converters for fee data between contract and SDK formats
|
|
3
|
+
*/
|
|
4
|
+
import { IFundingFees } from "../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
5
|
+
import { TradeFeesData, UiRealizedPnlData } from "../types";
|
|
6
|
+
import { CollateralConfig } from "../../markets/collateral/types";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Converts contract TradeFeesData to SDK format
|
|
9
|
+
* @param data Trade fees data from contract
|
|
10
|
+
* @param collateralConfig Config for the collateral (contains decimals)
|
|
11
|
+
* @returns Normalized trade fees data
|
|
12
|
+
*/
|
|
13
|
+
export declare const convertTradeFeesData: (data: IFundingFees.TradeFeesDataStructOutput, collateralConfig: CollateralConfig) => TradeFeesData;
|
|
14
|
+
/**
|
|
15
|
+
* @dev Converts array of TradeFeesData from contract
|
|
16
|
+
* @param dataArray Array of trade fees data
|
|
17
|
+
* @param collateralConfig Config for the collateral
|
|
18
|
+
* @returns Array of normalized trade fees data
|
|
19
|
+
*/
|
|
20
|
+
export declare const convertTradeFeesDataArray: (dataArray: IFundingFees.TradeFeesDataStructOutput[], collateralConfig: CollateralConfig) => TradeFeesData[];
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts contract UiRealizedPnlData to SDK format
|
|
23
|
+
* @param data UI realized PnL data from contract
|
|
24
|
+
* @param collateralConfig Config for the collateral (contains decimals)
|
|
25
|
+
* @returns Normalized UI realized PnL data
|
|
26
|
+
*/
|
|
27
|
+
export declare const convertUiRealizedPnlData: (data: IFundingFees.UiRealizedPnlDataStructOutput, collateralConfig: CollateralConfig) => UiRealizedPnlData;
|
|
28
|
+
/**
|
|
29
|
+
* @dev Converts array of UiRealizedPnlData from contract
|
|
30
|
+
* @param dataArray Array of UI realized PnL data
|
|
31
|
+
* @param collateralConfig Config for the collateral
|
|
32
|
+
* @returns Array of normalized UI realized PnL data
|
|
33
|
+
*/
|
|
34
|
+
export declare const convertUiRealizedPnlDataArray: (dataArray: IFundingFees.UiRealizedPnlDataStructOutput[], collateralConfig: CollateralConfig) => UiRealizedPnlData[];
|
|
35
|
+
/**
|
|
36
|
+
* @dev Converts TradeFeesData to contract format (for encoding)
|
|
37
|
+
* @param data SDK trade fees data
|
|
38
|
+
* @param collateralConfig Config for the collateral
|
|
39
|
+
* @returns Contract-formatted trade fees data
|
|
40
|
+
*/
|
|
41
|
+
export declare const encodeTradeFeesData: (data: TradeFeesData, collateralConfig: CollateralConfig) => IFundingFees.TradeFeesDataStruct;
|
|
42
|
+
/**
|
|
43
|
+
* @dev Converts UiRealizedPnlData to contract format (for encoding)
|
|
44
|
+
* @param data SDK UI realized PnL data
|
|
45
|
+
* @param collateralConfig Config for the collateral
|
|
46
|
+
* @returns Contract-formatted UI realized PnL data
|
|
47
|
+
*/
|
|
48
|
+
export declare const encodeUiRealizedPnlData: (data: UiRealizedPnlData, collateralConfig: CollateralConfig) => IFundingFees.UiRealizedPnlDataStruct;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Converters for fee data between contract and SDK formats
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.encodeUiRealizedPnlData = exports.encodeTradeFeesData = exports.convertUiRealizedPnlDataArray = exports.convertUiRealizedPnlData = exports.convertTradeFeesDataArray = exports.convertTradeFeesData = void 0;
|
|
7
|
+
const borrowingV2_1 = require("./borrowingV2");
|
|
8
|
+
const converter_1 = require("./fundingFees/converter");
|
|
9
|
+
/**
|
|
10
|
+
* @dev Converts contract TradeFeesData to SDK format
|
|
11
|
+
* @param data Trade fees data from contract
|
|
12
|
+
* @param collateralConfig Config for the collateral (contains decimals)
|
|
13
|
+
* @returns Normalized trade fees data
|
|
14
|
+
*/
|
|
15
|
+
const convertTradeFeesData = (data, collateralConfig) => {
|
|
16
|
+
const decimals = collateralConfig.decimals || 18;
|
|
17
|
+
return {
|
|
18
|
+
realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
|
|
19
|
+
Math.pow(10, decimals),
|
|
20
|
+
realizedPnlCollateral: parseFloat(data.realizedPnlCollateral.toString()) / Math.pow(10, decimals),
|
|
21
|
+
manuallyRealizedNegativePnlCollateral: parseFloat(data.manuallyRealizedNegativePnlCollateral.toString()) /
|
|
22
|
+
Math.pow(10, decimals),
|
|
23
|
+
alreadyTransferredNegativePnlCollateral: parseFloat(data.alreadyTransferredNegativePnlCollateral.toString()) /
|
|
24
|
+
Math.pow(10, decimals),
|
|
25
|
+
virtualAvailableCollateralInDiamond: parseFloat(data.virtualAvailableCollateralInDiamond.toString()) /
|
|
26
|
+
Math.pow(10, decimals),
|
|
27
|
+
initialAccFundingFeeP: parseFloat(data.initialAccFundingFeeP.toString()) /
|
|
28
|
+
converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P,
|
|
29
|
+
initialAccBorrowingFeeP: parseFloat(data.initialAccBorrowingFeeP.toString()) /
|
|
30
|
+
borrowingV2_1.BORROWING_V2_PRECISION.ACC_FEE,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
exports.convertTradeFeesData = convertTradeFeesData;
|
|
34
|
+
/**
|
|
35
|
+
* @dev Converts array of TradeFeesData from contract
|
|
36
|
+
* @param dataArray Array of trade fees data
|
|
37
|
+
* @param collateralConfig Config for the collateral
|
|
38
|
+
* @returns Array of normalized trade fees data
|
|
39
|
+
*/
|
|
40
|
+
const convertTradeFeesDataArray = (dataArray, collateralConfig) => {
|
|
41
|
+
return dataArray.map(data => (0, exports.convertTradeFeesData)(data, collateralConfig));
|
|
42
|
+
};
|
|
43
|
+
exports.convertTradeFeesDataArray = convertTradeFeesDataArray;
|
|
44
|
+
/**
|
|
45
|
+
* @dev Converts contract UiRealizedPnlData to SDK format
|
|
46
|
+
* @param data UI realized PnL data from contract
|
|
47
|
+
* @param collateralConfig Config for the collateral (contains decimals)
|
|
48
|
+
* @returns Normalized UI realized PnL data
|
|
49
|
+
*/
|
|
50
|
+
const convertUiRealizedPnlData = (data, collateralConfig) => {
|
|
51
|
+
const decimals = collateralConfig.decimals || 18;
|
|
52
|
+
return {
|
|
53
|
+
realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
|
|
54
|
+
Math.pow(10, decimals),
|
|
55
|
+
realizedOldBorrowingFeesCollateral: parseFloat(data.realizedOldBorrowingFeesCollateral.toString()) /
|
|
56
|
+
Math.pow(10, decimals),
|
|
57
|
+
realizedNewBorrowingFeesCollateral: parseFloat(data.realizedNewBorrowingFeesCollateral.toString()) /
|
|
58
|
+
Math.pow(10, decimals),
|
|
59
|
+
realizedFundingFeesCollateral: parseFloat(data.realizedFundingFeesCollateral.toString()) /
|
|
60
|
+
Math.pow(10, decimals),
|
|
61
|
+
realizedPnlPartialCloseCollateral: parseFloat(data.realizedPnlPartialCloseCollateral.toString()) /
|
|
62
|
+
Math.pow(10, decimals),
|
|
63
|
+
pnlWithdrawnCollateral: parseFloat(data.pnlWithdrawnCollateral.toString()) / Math.pow(10, decimals),
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
exports.convertUiRealizedPnlData = convertUiRealizedPnlData;
|
|
67
|
+
/**
|
|
68
|
+
* @dev Converts array of UiRealizedPnlData from contract
|
|
69
|
+
* @param dataArray Array of UI realized PnL data
|
|
70
|
+
* @param collateralConfig Config for the collateral
|
|
71
|
+
* @returns Array of normalized UI realized PnL data
|
|
72
|
+
*/
|
|
73
|
+
const convertUiRealizedPnlDataArray = (dataArray, collateralConfig) => {
|
|
74
|
+
return dataArray.map(data => (0, exports.convertUiRealizedPnlData)(data, collateralConfig));
|
|
75
|
+
};
|
|
76
|
+
exports.convertUiRealizedPnlDataArray = convertUiRealizedPnlDataArray;
|
|
77
|
+
/**
|
|
78
|
+
* @dev Converts TradeFeesData to contract format (for encoding)
|
|
79
|
+
* @param data SDK trade fees data
|
|
80
|
+
* @param collateralConfig Config for the collateral
|
|
81
|
+
* @returns Contract-formatted trade fees data
|
|
82
|
+
*/
|
|
83
|
+
const encodeTradeFeesData = (data, collateralConfig) => {
|
|
84
|
+
const decimals = collateralConfig.decimals || 18;
|
|
85
|
+
return {
|
|
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
|
+
__placeholder: 0,
|
|
92
|
+
initialAccFundingFeeP: Math.round(data.initialAccFundingFeeP * converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P),
|
|
93
|
+
initialAccBorrowingFeeP: Math.round(data.initialAccBorrowingFeeP * borrowingV2_1.BORROWING_V2_PRECISION.ACC_FEE),
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
exports.encodeTradeFeesData = encodeTradeFeesData;
|
|
97
|
+
/**
|
|
98
|
+
* @dev Converts UiRealizedPnlData to contract format (for encoding)
|
|
99
|
+
* @param data SDK UI realized PnL data
|
|
100
|
+
* @param collateralConfig Config for the collateral
|
|
101
|
+
* @returns Contract-formatted UI realized PnL data
|
|
102
|
+
*/
|
|
103
|
+
const encodeUiRealizedPnlData = (data, collateralConfig) => {
|
|
104
|
+
const decimals = collateralConfig.decimals || 18;
|
|
105
|
+
return {
|
|
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
|
+
};
|
|
113
|
+
};
|
|
114
|
+
exports.encodeUiRealizedPnlData = encodeUiRealizedPnlData;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Builder functions for funding fees context
|
|
3
|
+
*/
|
|
4
|
+
import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
|
|
5
|
+
import { GetPairFundingFeeContext } from "./pairContext";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Builds funding fees sub-context for a specific pair
|
|
8
|
+
*/
|
|
9
|
+
export declare const buildFundingContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, currentTimestamp: number) => GetPairFundingFeeContext | undefined;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Builder functions for funding fees context
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.buildFundingContext = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Builds funding fees sub-context for a specific pair
|
|
9
|
+
*/
|
|
10
|
+
const buildFundingContext = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
|
|
11
|
+
var _a, _b, _c, _d, _e;
|
|
12
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
13
|
+
if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairFundingFees)) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const params = (_a = collateral.pairFundingFees.params) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
17
|
+
const data = (_b = collateral.pairFundingFees.data) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
18
|
+
const pairOi = (_c = collateral.pairOis) === null || _c === void 0 ? void 0 : _c[pairIndex];
|
|
19
|
+
if (!params || !data) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
params,
|
|
24
|
+
data,
|
|
25
|
+
pairOi: pairOi
|
|
26
|
+
? {
|
|
27
|
+
oiLongToken: ((_d = pairOi.token) === null || _d === void 0 ? void 0 : _d.long) || 0,
|
|
28
|
+
oiShortToken: ((_e = pairOi.token) === null || _e === void 0 ? void 0 : _e.short) || 0,
|
|
29
|
+
}
|
|
30
|
+
: undefined,
|
|
31
|
+
currentTimestamp,
|
|
32
|
+
// TODO: Add net exposure when available
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
exports.buildFundingContext = buildFundingContext;
|