@gainsnetwork/sdk 0.0.0-v10.rc2 → 0.0.0-v10.rc4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/backend/globalTrades/index.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +3 -0
- package/lib/backend/index.js +28 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +312 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +31 -0
- package/lib/backend/tradingVariables/converter.js +330 -0
- package/lib/backend/tradingVariables/index.d.ts +5 -0
- package/lib/backend/tradingVariables/index.js +95 -0
- package/lib/backend/tradingVariables/types.d.ts +109 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.d.ts +10 -0
- package/lib/constants.js +11 -1
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +7 -20
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +7 -16
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
- package/lib/contracts/types/generated/factories/GToken__factory.js +4 -0
- package/lib/contracts/utils/index.d.ts +0 -3
- package/lib/contracts/utils/index.js +0 -3
- package/lib/contracts/utils/openTrades.js +14 -30
- package/lib/contracts/utils/pairs.js +10 -0
- 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/index.d.ts +2 -0
- package/lib/markets/index.js +2 -0
- package/lib/markets/oi/converter.d.ts +63 -0
- package/lib/markets/oi/converter.js +103 -0
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/index.d.ts +10 -0
- package/lib/markets/oi/index.js +37 -0
- package/lib/markets/oi/types.d.ts +82 -0
- package/lib/markets/oi/types.js +6 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/trade/fees/borrowing/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +33 -0
- package/lib/trade/fees/borrowing/index.d.ts +16 -4
- package/lib/trade/fees/borrowing/index.js +43 -18
- 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 +12 -3
- package/lib/trade/fees/borrowingV2/converter.js +29 -18
- package/lib/{contracts/fetch/fees/borrowingFeesV2.d.ts → trade/fees/borrowingV2/fetcher.d.ts} +2 -3
- package/lib/{contracts/fetch/fees/borrowingFeesV2.js → trade/fees/borrowingV2/fetcher.js} +6 -14
- package/lib/trade/fees/borrowingV2/index.d.ts +6 -17
- package/lib/trade/fees/borrowingV2/index.js +10 -37
- package/lib/trade/fees/borrowingV2/types.d.ts +22 -6
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +110 -0
- package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
- package/lib/trade/fees/fundingFees/builder.js +35 -0
- package/lib/{contracts/fetch/fees/fundingFees.d.ts → trade/fees/fundingFees/fetcher.d.ts} +2 -2
- package/lib/trade/fees/fundingFees/index.d.ts +13 -2
- package/lib/trade/fees/fundingFees/index.js +27 -3
- package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
- package/lib/trade/fees/fundingFees/pairContext.js +17 -0
- package/lib/trade/fees/index.d.ts +3 -2
- package/lib/trade/fees/index.js +44 -36
- package/lib/trade/fees/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- package/lib/trade/fees/tiers/index.d.ts +1 -0
- package/lib/trade/fees/tiers/index.js +1 -0
- package/lib/trade/fees/trading/builder.d.ts +18 -0
- package/lib/trade/fees/trading/builder.js +20 -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 +30 -2
- package/lib/trade/fees/trading/index.js +52 -1
- package/lib/trade/fees/trading/types.d.ts +9 -0
- package/lib/trade/index.d.ts +1 -1
- package/lib/trade/index.js +1 -1
- package/lib/trade/liquidation/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 +26 -0
- package/lib/trade/liquidation/index.js +142 -0
- package/lib/trade/liquidation/types.d.ts +59 -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 +77 -0
- package/lib/trade/pnl/index.js +270 -0
- package/lib/trade/pnl/types.d.ts +114 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/index.d.ts +21 -0
- package/lib/trade/priceImpact/close/index.js +131 -0
- package/lib/trade/priceImpact/close/types.d.ts +43 -0
- package/lib/trade/priceImpact/close/types.js +5 -0
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
- package/lib/trade/priceImpact/cumulVol/index.d.ts +107 -0
- package/lib/trade/priceImpact/cumulVol/index.js +228 -0
- package/lib/trade/priceImpact/index.d.ts +6 -2
- package/lib/trade/priceImpact/index.js +30 -3
- package/lib/trade/priceImpact/open/index.d.ts +22 -0
- package/lib/trade/priceImpact/open/index.js +76 -0
- package/lib/trade/priceImpact/open/types.d.ts +41 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/{contracts/fetch/priceImpact/skew.d.ts → trade/priceImpact/skew/fetcher.d.ts} +2 -2
- package/lib/{contracts/fetch/priceImpact/skew.js → trade/priceImpact/skew/fetcher.js} +6 -6
- package/lib/trade/priceImpact/skew/index.d.ts +1 -0
- package/lib/trade/priceImpact/skew/index.js +4 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +20 -8
- package/lib/trade/types.js +10 -0
- package/package.json +2 -2
- package/lib/trade/liquidation.d.ts +0 -42
- package/lib/trade/liquidation.js +0 -140
- package/lib/trade/pnl.d.ts +0 -10
- package/lib/trade/pnl.js +0 -38
- /package/lib/{contracts/fetch/fees/fundingFees.js → trade/fees/fundingFees/fetcher.js} +0 -0
|
@@ -4,18 +4,25 @@ export type GetBorrowingFeeContext = {
|
|
|
4
4
|
currentBlock: number;
|
|
5
5
|
groups: BorrowingFee.Group[];
|
|
6
6
|
pairs: BorrowingFee.Pair[];
|
|
7
|
+
collateralPriceUsd: number;
|
|
7
8
|
};
|
|
8
9
|
/**
|
|
9
10
|
* @dev Calculates borrowing fees using v1 model (block-based with groups)
|
|
10
11
|
* @dev Still actively used by markets that haven't migrated to v2
|
|
12
|
+
* @dev Uses dynamic collateral OI - converts OI to USD for fee calculations
|
|
11
13
|
* @param posDai Position size in collateral
|
|
12
|
-
* @param pairIndex Trading pair index
|
|
14
|
+
* @param pairIndex Trading pair index (required)
|
|
13
15
|
* @param long Whether position is long
|
|
14
16
|
* @param initialAccFees Initial accumulated fees when trade was opened
|
|
15
|
-
* @param context Context with current block and
|
|
17
|
+
* @param context Context with current block, fee data, and collateral price
|
|
16
18
|
* @returns Borrowing fee in collateral tokens
|
|
17
19
|
*/
|
|
18
|
-
export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
|
|
20
|
+
export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex | undefined, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated Use withinMaxGroupOiDynamic from @gains-network/sdk/markets/oi instead
|
|
23
|
+
* @dev This function uses static OI which doesn't reflect current market values
|
|
24
|
+
* @dev The v10 contracts use dynamic OI (beforeV10 + afterV10Token * currentPrice)
|
|
25
|
+
*/
|
|
19
26
|
export declare const withinMaxGroupOi: (pairIndex: PairIndex, long: boolean, positionSizeCollateral: number, context: {
|
|
20
27
|
groups: BorrowingFee.Group[];
|
|
21
28
|
pairs: BorrowingFee.Pair[];
|
|
@@ -29,6 +36,7 @@ export declare const borrowingFeeUtils: {
|
|
|
29
36
|
getPairPendingAccFees: (pairIndex: PairIndex, currentBlock: number, context: {
|
|
30
37
|
pairs: BorrowingFee.Pair[];
|
|
31
38
|
openInterest: OpenInterest;
|
|
39
|
+
collateralPriceUsd: number;
|
|
32
40
|
}) => {
|
|
33
41
|
accFeeLong: number;
|
|
34
42
|
accFeeShort: number;
|
|
@@ -38,9 +46,11 @@ export declare const borrowingFeeUtils: {
|
|
|
38
46
|
getPairPendingAccFee: (pairIndex: PairIndex, currentBlock: number, long: boolean, context: {
|
|
39
47
|
pairs: BorrowingFee.Pair[];
|
|
40
48
|
openInterest: OpenInterest;
|
|
49
|
+
collateralPriceUsd: number;
|
|
41
50
|
}) => number;
|
|
42
51
|
getGroupPendingAccFees: (groupIndex: number, currentBlock: number, context: {
|
|
43
52
|
groups: BorrowingFee.Group[];
|
|
53
|
+
collateralPriceUsd: number;
|
|
44
54
|
}) => {
|
|
45
55
|
accFeeLong: number;
|
|
46
56
|
accFeeShort: number;
|
|
@@ -49,8 +59,9 @@ export declare const borrowingFeeUtils: {
|
|
|
49
59
|
};
|
|
50
60
|
getGroupPendingAccFee: (groupIndex: number, currentBlock: number, long: boolean, context: {
|
|
51
61
|
groups: BorrowingFee.Group[];
|
|
62
|
+
collateralPriceUsd: number;
|
|
52
63
|
}) => number;
|
|
53
|
-
getPendingAccFees: (accFeeLong: number, accFeeShort: number, oiLong: number, oiShort: number, feePerBlock: number, currentBlock: number, accLastUpdatedBlock: number, maxOi: number, feeExponent: number, feeCaps?: BorrowingFee.BorrowingFeePerBlockCap) => {
|
|
64
|
+
getPendingAccFees: (accFeeLong: number, accFeeShort: number, oiLong: number, oiShort: number, feePerBlock: number, currentBlock: number, accLastUpdatedBlock: number, maxOi: number, feeExponent: number, feeCaps?: BorrowingFee.BorrowingFeePerBlockCap, collateralPriceUsd?: number) => {
|
|
54
65
|
accFeeLong: number;
|
|
55
66
|
accFeeShort: number;
|
|
56
67
|
deltaLong: number;
|
|
@@ -66,3 +77,4 @@ export declare const borrowingFeeUtils: {
|
|
|
66
77
|
};
|
|
67
78
|
export * as BorrowingFee from "./types";
|
|
68
79
|
export * from "./converter";
|
|
80
|
+
export * from "./builder";
|
|
@@ -30,18 +30,22 @@ exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = ex
|
|
|
30
30
|
/**
|
|
31
31
|
* @dev Calculates borrowing fees using v1 model (block-based with groups)
|
|
32
32
|
* @dev Still actively used by markets that haven't migrated to v2
|
|
33
|
+
* @dev Uses dynamic collateral OI - converts OI to USD for fee calculations
|
|
33
34
|
* @param posDai Position size in collateral
|
|
34
|
-
* @param pairIndex Trading pair index
|
|
35
|
+
* @param pairIndex Trading pair index (required)
|
|
35
36
|
* @param long Whether position is long
|
|
36
37
|
* @param initialAccFees Initial accumulated fees when trade was opened
|
|
37
|
-
* @param context Context with current block and
|
|
38
|
+
* @param context Context with current block, fee data, and collateral price
|
|
38
39
|
* @returns Borrowing fee in collateral tokens
|
|
39
40
|
*/
|
|
40
41
|
const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
41
|
-
if (
|
|
42
|
+
if (pairIndex === undefined) {
|
|
43
|
+
throw new Error("pairIndex is required for borrowing fee calculations");
|
|
44
|
+
}
|
|
45
|
+
const { pairs, groups } = context;
|
|
46
|
+
if (!groups || !pairs || !pairs[pairIndex]) {
|
|
42
47
|
return 0;
|
|
43
48
|
}
|
|
44
|
-
const { pairs } = context;
|
|
45
49
|
const pairGroups = pairs[pairIndex].groups;
|
|
46
50
|
const firstPairGroup = (pairGroups === null || pairGroups === void 0 ? void 0 : pairGroups.length) > 0 ? pairGroups[0] : undefined;
|
|
47
51
|
let fee = 0;
|
|
@@ -52,13 +56,19 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
52
56
|
? getPairPendingAccFee(pairIndex, context.currentBlock, long, {
|
|
53
57
|
pairs,
|
|
54
58
|
openInterest,
|
|
59
|
+
collateralPriceUsd: context.collateralPriceUsd,
|
|
55
60
|
})
|
|
56
61
|
: long
|
|
57
62
|
? firstPairGroup.pairAccFeeLong
|
|
58
63
|
: firstPairGroup.pairAccFeeShort) - initialAccFees.accPairFee;
|
|
59
64
|
}
|
|
60
65
|
for (let i = pairGroups.length; i > 0; i--) {
|
|
61
|
-
const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long,
|
|
66
|
+
const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long, {
|
|
67
|
+
currentBlock: context.currentBlock,
|
|
68
|
+
groups,
|
|
69
|
+
pairs,
|
|
70
|
+
collateralPriceUsd: context.collateralPriceUsd,
|
|
71
|
+
});
|
|
62
72
|
fee += Math.max(deltaGroup, deltaPair);
|
|
63
73
|
if (beforeTradeOpen) {
|
|
64
74
|
break;
|
|
@@ -67,6 +77,11 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
67
77
|
return (posDai * fee) / 100;
|
|
68
78
|
};
|
|
69
79
|
exports.getBorrowingFee = getBorrowingFee;
|
|
80
|
+
/**
|
|
81
|
+
* @deprecated Use withinMaxGroupOiDynamic from @gains-network/sdk/markets/oi instead
|
|
82
|
+
* @dev This function uses static OI which doesn't reflect current market values
|
|
83
|
+
* @dev The v10 contracts use dynamic OI (beforeV10 + afterV10Token * currentPrice)
|
|
84
|
+
*/
|
|
70
85
|
const withinMaxGroupOi = (pairIndex, long, positionSizeCollateral, context) => {
|
|
71
86
|
const { groups, pairs } = context;
|
|
72
87
|
if (!groups || !pairs) {
|
|
@@ -85,18 +100,19 @@ const getPairGroupIndex = (pairIndex, context) => {
|
|
|
85
100
|
return pairGroups.length == 0 ? 0 : pairGroups[0].groupIndex;
|
|
86
101
|
};
|
|
87
102
|
const getPairPendingAccFees = (pairIndex, currentBlock, context) => {
|
|
88
|
-
const { pairs, openInterest: { long, short }, } = context;
|
|
103
|
+
const { pairs, openInterest: { long, short }, collateralPriceUsd, } = context;
|
|
89
104
|
const pair = pairs[pairIndex];
|
|
90
|
-
return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock, pair.oi.max, pair.feeExponent, pair.feePerBlockCap);
|
|
105
|
+
return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock, pair.oi.max, pair.feeExponent, pair.feePerBlockCap, collateralPriceUsd);
|
|
91
106
|
};
|
|
92
107
|
const getPairPendingAccFee = (pairIndex, currentBlock, long, context) => {
|
|
93
108
|
const { accFeeLong, accFeeShort } = getPairPendingAccFees(pairIndex, currentBlock, context);
|
|
94
109
|
return long ? accFeeLong : accFeeShort;
|
|
95
110
|
};
|
|
96
111
|
const getGroupPendingAccFees = (groupIndex, currentBlock, context) => {
|
|
97
|
-
const { groups } = context;
|
|
112
|
+
const { groups, collateralPriceUsd } = context;
|
|
98
113
|
const group = groups[groupIndex];
|
|
99
|
-
return getPendingAccFees(group.accFeeLong, group.accFeeShort, group.oi.long, group.oi.short, group.feePerBlock, currentBlock, group.accLastUpdatedBlock, group.oi.max, group.feeExponent
|
|
114
|
+
return getPendingAccFees(group.accFeeLong, group.accFeeShort, group.oi.long, group.oi.short, group.feePerBlock, currentBlock, group.accLastUpdatedBlock, group.oi.max, group.feeExponent, undefined, // no fee caps for groups
|
|
115
|
+
collateralPriceUsd);
|
|
100
116
|
};
|
|
101
117
|
const getGroupPendingAccFee = (groupIndex, currentBlock, long, context) => {
|
|
102
118
|
const { accFeeLong, accFeeShort } = getGroupPendingAccFees(groupIndex, currentBlock, context);
|
|
@@ -107,14 +123,16 @@ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long,
|
|
|
107
123
|
const beforeTradeOpen = group.block < initialFees.block;
|
|
108
124
|
let deltaGroup, deltaPair;
|
|
109
125
|
if (i == pairGroups.length - 1) {
|
|
110
|
-
const { currentBlock, groups, pairs } = context;
|
|
126
|
+
const { currentBlock, groups, pairs, collateralPriceUsd } = context;
|
|
111
127
|
const openInterest = pairs[pairIndex].oi;
|
|
112
128
|
deltaGroup = getGroupPendingAccFee(group.groupIndex, currentBlock, long, {
|
|
113
129
|
groups,
|
|
130
|
+
collateralPriceUsd,
|
|
114
131
|
});
|
|
115
132
|
deltaPair = getPairPendingAccFee(pairIndex, currentBlock, long, {
|
|
116
133
|
pairs,
|
|
117
134
|
openInterest,
|
|
135
|
+
collateralPriceUsd,
|
|
118
136
|
});
|
|
119
137
|
}
|
|
120
138
|
else {
|
|
@@ -137,8 +155,8 @@ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long,
|
|
|
137
155
|
}
|
|
138
156
|
return { deltaGroup, deltaPair, beforeTradeOpen };
|
|
139
157
|
};
|
|
140
|
-
const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock, currentBlock, accLastUpdatedBlock, maxOi, feeExponent, feeCaps // as percentage: eg minP: 0.1 = 10%, maxP: 0.5 = 50%
|
|
141
|
-
) => {
|
|
158
|
+
const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock, currentBlock, accLastUpdatedBlock, maxOi, feeExponent, feeCaps, // as percentage: eg minP: 0.1 = 10%, maxP: 0.5 = 50%
|
|
159
|
+
collateralPriceUsd) => {
|
|
142
160
|
const moreShorts = oiLong < oiShort;
|
|
143
161
|
const blockDistance = currentBlock > accLastUpdatedBlock ? currentBlock - accLastUpdatedBlock : 0;
|
|
144
162
|
// If block distance is zero nothing changes
|
|
@@ -150,19 +168,25 @@ const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock
|
|
|
150
168
|
deltaShort: 0,
|
|
151
169
|
};
|
|
152
170
|
}
|
|
153
|
-
|
|
154
|
-
|
|
171
|
+
// Convert OI to USD if collateral price is provided (dynamic collateral OI)
|
|
172
|
+
const oiLongUsd = collateralPriceUsd ? oiLong * collateralPriceUsd : oiLong;
|
|
173
|
+
const oiShortUsd = collateralPriceUsd
|
|
174
|
+
? oiShort * collateralPriceUsd
|
|
175
|
+
: oiShort;
|
|
176
|
+
const maxOiUsd = collateralPriceUsd ? maxOi * collateralPriceUsd : maxOi;
|
|
177
|
+
const netOi = Math.abs(oiLongUsd - oiShortUsd);
|
|
178
|
+
// Calculate minimum and maximum effective oi (using USD values if available)
|
|
155
179
|
const { minP, maxP } = getFeePerBlockCaps(feeCaps);
|
|
156
|
-
const minNetOi =
|
|
157
|
-
const maxNetOi =
|
|
180
|
+
const minNetOi = maxOiUsd * minP;
|
|
181
|
+
const maxNetOi = maxOiUsd * maxP;
|
|
158
182
|
// Calculate the minimum acc fee delta (applies to both sides)
|
|
159
183
|
const minDelta = minNetOi > 0
|
|
160
|
-
? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi,
|
|
184
|
+
? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi, maxOiUsd, feeExponent)
|
|
161
185
|
: 0;
|
|
162
186
|
// Calculate the actual acc fee (using capped oi of 100% or less)
|
|
163
187
|
const delta = netOi > minNetOi
|
|
164
188
|
? getPendingAccFeesDelta(blockDistance, feePerBlock, Math.min(netOi, maxNetOi), // if netOi > cap, use cap
|
|
165
|
-
|
|
189
|
+
maxOiUsd, feeExponent)
|
|
166
190
|
: minDelta;
|
|
167
191
|
const [deltaLong, deltaShort] = moreShorts
|
|
168
192
|
? [minDelta, delta]
|
|
@@ -217,3 +241,4 @@ exports.borrowingFeeUtils = {
|
|
|
217
241
|
};
|
|
218
242
|
exports.BorrowingFee = __importStar(require("./types"));
|
|
219
243
|
__exportStar(require("./converter"), exports);
|
|
244
|
+
__exportStar(require("./builder"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
|
|
2
|
+
import { GetPairBorrowingFeeV2Context } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Builds borrowing v2 sub-context for a specific pair
|
|
5
|
+
*/
|
|
6
|
+
export declare const buildBorrowingV2Context: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, currentTimestamp: number) => GetPairBorrowingFeeV2Context | undefined;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildBorrowingV2Context = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @dev Builds borrowing v2 sub-context for a specific pair
|
|
6
|
+
*/
|
|
7
|
+
const buildBorrowingV2Context = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
10
|
+
if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairBorrowingFeesV2)) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
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];
|
|
15
|
+
if (!params || !data) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
params,
|
|
20
|
+
data,
|
|
21
|
+
currentTimestamp,
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
exports.buildBorrowingV2Context = buildBorrowingV2Context;
|
|
@@ -38,14 +38,13 @@ export declare const convertTradeInitialAccFees: (contractTradeData: IFundingFee
|
|
|
38
38
|
export declare const convertTradeInitialAccFeesArray: (contractTradeDataArray: IFundingFees.TradeFeesDataStructOutput[]) => BorrowingFeeV2.TradeInitialAccFees[];
|
|
39
39
|
/**
|
|
40
40
|
* @dev Creates a context object from contract data arrays
|
|
41
|
-
* @param collateralIndices Array of collateral indices
|
|
42
41
|
* @param pairIndices Array of pair indices
|
|
43
42
|
* @param borrowingParams Array of borrowing fee params from contract
|
|
44
43
|
* @param borrowingData Array of pair borrowing fee data from contract
|
|
45
44
|
* @param currentTimestamp Optional current timestamp
|
|
46
|
-
* @returns Complete SDK context for borrowing v2 calculations
|
|
45
|
+
* @returns Complete SDK context for borrowing v2 calculations (collateral-scoped)
|
|
47
46
|
*/
|
|
48
|
-
export declare const createBorrowingV2Context: (
|
|
47
|
+
export declare const createBorrowingV2Context: (pairIndices: number[], borrowingParams: IFundingFees.BorrowingFeeParamsStructOutput[], borrowingData: IFundingFees.PairBorrowingFeeDataStructOutput[], currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
|
|
49
48
|
/**
|
|
50
49
|
* @dev Helper function to validate borrowing rate per second
|
|
51
50
|
* @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
|
|
@@ -64,3 +63,13 @@ export declare const borrowingRateToAPR: (borrowingRatePerSecondP: number) => nu
|
|
|
64
63
|
* @returns Borrowing rate per second (normalized float)
|
|
65
64
|
*/
|
|
66
65
|
export declare const aprToBorrowingRate: (aprPercentage: number) => number;
|
|
66
|
+
/**
|
|
67
|
+
* @dev Creates a collateral-scoped context from frontend data structure
|
|
68
|
+
* @param collateralBorrowingData Data structure from frontend (params and data arrays)
|
|
69
|
+
* @param currentTimestamp Optional current timestamp
|
|
70
|
+
* @returns Collateral-scoped borrowing fee v2 context
|
|
71
|
+
*/
|
|
72
|
+
export declare const createCollateralScopedBorrowingContext: (collateralBorrowingData: {
|
|
73
|
+
params: BorrowingFeeV2.BorrowingFeeParams[];
|
|
74
|
+
data: BorrowingFeeV2.PairBorrowingFeeData[];
|
|
75
|
+
}, currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.aprToBorrowingRate = exports.borrowingRateToAPR = exports.isValidBorrowingRate = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArray = exports.convertTradeInitialAccFees = exports.convertPairBorrowingFeeDataArray = exports.convertPairBorrowingFeeData = exports.convertBorrowingFeeParamsArray = exports.convertBorrowingFeeParams = void 0;
|
|
3
|
+
exports.createCollateralScopedBorrowingContext = exports.aprToBorrowingRate = exports.borrowingRateToAPR = exports.isValidBorrowingRate = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArray = exports.convertTradeInitialAccFees = exports.convertPairBorrowingFeeDataArray = exports.convertPairBorrowingFeeData = exports.convertBorrowingFeeParamsArray = exports.convertBorrowingFeeParams = void 0;
|
|
4
4
|
const index_1 = require("./index");
|
|
5
5
|
/**
|
|
6
6
|
* @dev Converts contract BorrowingFeeParams to SDK type
|
|
@@ -56,35 +56,24 @@ const convertTradeInitialAccFeesArray = (contractTradeDataArray) => contractTrad
|
|
|
56
56
|
exports.convertTradeInitialAccFeesArray = convertTradeInitialAccFeesArray;
|
|
57
57
|
/**
|
|
58
58
|
* @dev Creates a context object from contract data arrays
|
|
59
|
-
* @param collateralIndices Array of collateral indices
|
|
60
59
|
* @param pairIndices Array of pair indices
|
|
61
60
|
* @param borrowingParams Array of borrowing fee params from contract
|
|
62
61
|
* @param borrowingData Array of pair borrowing fee data from contract
|
|
63
62
|
* @param currentTimestamp Optional current timestamp
|
|
64
|
-
* @returns Complete SDK context for borrowing v2 calculations
|
|
63
|
+
* @returns Complete SDK context for borrowing v2 calculations (collateral-scoped)
|
|
65
64
|
*/
|
|
66
|
-
const createBorrowingV2Context = (
|
|
65
|
+
const createBorrowingV2Context = (pairIndices, borrowingParams, borrowingData, currentTimestamp) => {
|
|
67
66
|
const context = {
|
|
68
67
|
currentTimestamp,
|
|
69
68
|
borrowingParams: {},
|
|
70
69
|
borrowingData: {},
|
|
71
70
|
};
|
|
72
|
-
// Build
|
|
73
|
-
for (let i = 0; i <
|
|
74
|
-
const collateralIndex = collateralIndices[i];
|
|
71
|
+
// Build objects indexed by pairIndex
|
|
72
|
+
for (let i = 0; i < pairIndices.length; i++) {
|
|
75
73
|
const pairIndex = pairIndices[i];
|
|
76
|
-
// Initialize collateral index objects if they don't exist
|
|
77
|
-
if (!context.borrowingParams[collateralIndex]) {
|
|
78
|
-
context.borrowingParams[collateralIndex] = {};
|
|
79
|
-
}
|
|
80
|
-
if (!context.borrowingData[collateralIndex]) {
|
|
81
|
-
context.borrowingData[collateralIndex] = {};
|
|
82
|
-
}
|
|
83
74
|
// Store converted data
|
|
84
|
-
context.borrowingParams[
|
|
85
|
-
|
|
86
|
-
context.borrowingData[collateralIndex][pairIndex] =
|
|
87
|
-
(0, exports.convertPairBorrowingFeeData)(borrowingData[i]);
|
|
75
|
+
context.borrowingParams[pairIndex] = (0, exports.convertBorrowingFeeParams)(borrowingParams[i]);
|
|
76
|
+
context.borrowingData[pairIndex] = (0, exports.convertPairBorrowingFeeData)(borrowingData[i]);
|
|
88
77
|
}
|
|
89
78
|
return context;
|
|
90
79
|
};
|
|
@@ -119,3 +108,25 @@ const aprToBorrowingRate = (aprPercentage) => {
|
|
|
119
108
|
return aprPercentage / SECONDS_PER_YEAR;
|
|
120
109
|
};
|
|
121
110
|
exports.aprToBorrowingRate = aprToBorrowingRate;
|
|
111
|
+
/**
|
|
112
|
+
* @dev Creates a collateral-scoped context from frontend data structure
|
|
113
|
+
* @param collateralBorrowingData Data structure from frontend (params and data arrays)
|
|
114
|
+
* @param currentTimestamp Optional current timestamp
|
|
115
|
+
* @returns Collateral-scoped borrowing fee v2 context
|
|
116
|
+
*/
|
|
117
|
+
const createCollateralScopedBorrowingContext = (collateralBorrowingData, currentTimestamp) => {
|
|
118
|
+
const context = {
|
|
119
|
+
currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
|
|
120
|
+
borrowingParams: {},
|
|
121
|
+
borrowingData: {},
|
|
122
|
+
};
|
|
123
|
+
// Map arrays to objects indexed by array position (pairIndex)
|
|
124
|
+
collateralBorrowingData.params.forEach((param, index) => {
|
|
125
|
+
context.borrowingParams[index] = param;
|
|
126
|
+
});
|
|
127
|
+
collateralBorrowingData.data.forEach((data, index) => {
|
|
128
|
+
context.borrowingData[index] = data;
|
|
129
|
+
});
|
|
130
|
+
return context;
|
|
131
|
+
};
|
|
132
|
+
exports.createCollateralScopedBorrowingContext = createCollateralScopedBorrowingContext;
|
package/lib/{contracts/fetch/fees/borrowingFeesV2.d.ts → trade/fees/borrowingV2/fetcher.d.ts}
RENAMED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import { BorrowingFeeV2 } from "../../../trade/fees/borrowingV2";
|
|
1
|
+
import type { GNSMultiCollatDiamond } from "../../../contracts/types/generated";
|
|
2
|
+
import { BorrowingFeeV2 } from ".";
|
|
4
3
|
/**
|
|
5
4
|
* @dev Fetches borrowing fee parameters v2 for specific pairs
|
|
6
5
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = void 0;
|
|
13
|
-
const converter_1 = require("
|
|
13
|
+
const converter_1 = require("./converter");
|
|
14
14
|
/**
|
|
15
15
|
* @dev Fetches borrowing fee parameters v2 for specific pairs
|
|
16
16
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
@@ -147,20 +147,12 @@ const createBorrowingV2ContextFromArrays = (collateralIndices, pairIndices, para
|
|
|
147
147
|
borrowingParams: {},
|
|
148
148
|
borrowingData: {},
|
|
149
149
|
};
|
|
150
|
-
// Build
|
|
151
|
-
for (let i = 0; i <
|
|
152
|
-
const collateralIndex = collateralIndices[i];
|
|
150
|
+
// Build objects indexed by pairIndex (collateral-scoped)
|
|
151
|
+
for (let i = 0; i < pairIndices.length; i++) {
|
|
153
152
|
const pairIndex = pairIndices[i];
|
|
154
|
-
//
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
if (!context.borrowingData[collateralIndex]) {
|
|
159
|
-
context.borrowingData[collateralIndex] = {};
|
|
160
|
-
}
|
|
161
|
-
// Store data
|
|
162
|
-
context.borrowingParams[collateralIndex][pairIndex] = params[i];
|
|
163
|
-
context.borrowingData[collateralIndex][pairIndex] = data[i];
|
|
153
|
+
// Store data indexed by pairIndex
|
|
154
|
+
context.borrowingParams[pairIndex] = params[i];
|
|
155
|
+
context.borrowingData[pairIndex] = data[i];
|
|
164
156
|
}
|
|
165
157
|
return context;
|
|
166
158
|
};
|
|
@@ -22,23 +22,11 @@ export declare const BORROWING_V2_PRECISION: {
|
|
|
22
22
|
export declare const getPairPendingAccBorrowingFees: (params: BorrowingFeeV2.BorrowingFeeParams, data: BorrowingFeeV2.PairBorrowingFeeData, currentPairPrice: number, currentTimestamp?: number) => number;
|
|
23
23
|
/**
|
|
24
24
|
* @dev Calculates borrowing fees owed by a specific trade
|
|
25
|
-
* @param input Trade borrowing fee calculation input
|
|
26
|
-
* @param context
|
|
27
|
-
* @returns Borrowing fees in collateral tokens
|
|
28
|
-
*/
|
|
29
|
-
export declare const getTradeBorrowingFeesCollateral: (input: BorrowingFeeV2.TradeBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
30
|
-
/**
|
|
31
|
-
* @dev Convenience function to calculate borrowing fees for a trade using individual parameters
|
|
32
|
-
* @param positionSizeCollateral Position size in collateral tokens
|
|
33
|
-
* @param pairIndex Index of the trading pair
|
|
34
|
-
* @param collateralIndex Index of the collateral
|
|
35
|
-
* @param openPrice Price at which the trade was opened
|
|
36
|
-
* @param currentPairPrice Current price of the trading pair
|
|
37
|
-
* @param initialAccBorrowingFeeP Initial accumulated borrowing fee when trade was opened
|
|
38
|
-
* @param context Context containing borrowing parameters and data
|
|
25
|
+
* @param input Trade borrowing fee calculation input (without pairIndex)
|
|
26
|
+
* @param context Pair-specific borrowing context
|
|
39
27
|
* @returns Borrowing fees in collateral tokens
|
|
40
28
|
*/
|
|
41
|
-
export declare const
|
|
29
|
+
export declare const getTradeBorrowingFeesCollateral: (input: Omit<BorrowingFeeV2.TradeBorrowingFeeInput, "pairIndex">, context: BorrowingFeeV2.GetPairBorrowingFeeV2Context) => number;
|
|
42
30
|
/**
|
|
43
31
|
* @dev Utility function to get pending accumulated borrowing fees for a pair using context
|
|
44
32
|
* @param input Pair borrowing fee calculation input
|
|
@@ -51,9 +39,10 @@ export declare const getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingF
|
|
|
51
39
|
*/
|
|
52
40
|
export declare const borrowingFeeV2Utils: {
|
|
53
41
|
getPairPendingAccBorrowingFees: (params: BorrowingFeeV2.BorrowingFeeParams, data: BorrowingFeeV2.PairBorrowingFeeData, currentPairPrice: number, currentTimestamp?: number) => number;
|
|
54
|
-
getTradeBorrowingFeesCollateral: (input: BorrowingFeeV2.TradeBorrowingFeeInput, context: BorrowingFeeV2.
|
|
42
|
+
getTradeBorrowingFeesCollateral: (input: Omit<BorrowingFeeV2.TradeBorrowingFeeInput, "pairIndex">, context: BorrowingFeeV2.GetPairBorrowingFeeV2Context) => number;
|
|
55
43
|
getPairBorrowingFees: (input: BorrowingFeeV2.PairBorrowingFeeInput, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
56
|
-
getBorrowingFee: (positionSizeCollateral: number, pairIndex: number, collateralIndex: number, openPrice: number, currentPairPrice: number, initialAccBorrowingFeeP: number, context: BorrowingFeeV2.GetBorrowingFeeV2Context) => number;
|
|
57
44
|
};
|
|
58
45
|
export * as BorrowingFeeV2 from "./types";
|
|
46
|
+
export { GetPairBorrowingFeeV2Context } from "./types";
|
|
59
47
|
export * from "./converter";
|
|
48
|
+
export * from "./fetcher";
|
|
@@ -26,7 +26,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
26
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.BorrowingFeeV2 = exports.borrowingFeeV2Utils = exports.getPairBorrowingFees = exports.
|
|
29
|
+
exports.BorrowingFeeV2 = exports.borrowingFeeV2Utils = exports.getPairBorrowingFees = exports.getTradeBorrowingFeesCollateral = exports.getPairPendingAccBorrowingFees = exports.BORROWING_V2_PRECISION = exports.MAX_BORROWING_RATE_PER_SECOND = void 0;
|
|
30
30
|
/**
|
|
31
31
|
* @dev Maximum borrowing rate per second (1,000% APR)
|
|
32
32
|
*/
|
|
@@ -63,21 +63,18 @@ const getPairPendingAccBorrowingFees = (params, data, currentPairPrice, currentT
|
|
|
63
63
|
exports.getPairPendingAccBorrowingFees = getPairPendingAccBorrowingFees;
|
|
64
64
|
/**
|
|
65
65
|
* @dev Calculates borrowing fees owed by a specific trade
|
|
66
|
-
* @param input Trade borrowing fee calculation input
|
|
67
|
-
* @param context
|
|
66
|
+
* @param input Trade borrowing fee calculation input (without pairIndex)
|
|
67
|
+
* @param context Pair-specific borrowing context
|
|
68
68
|
* @returns Borrowing fees in collateral tokens
|
|
69
69
|
*/
|
|
70
70
|
const getTradeBorrowingFeesCollateral = (input, context) => {
|
|
71
|
-
|
|
72
|
-
const {
|
|
73
|
-
// Get borrowing parameters and data for the pair
|
|
74
|
-
const params = (_a = context.borrowingParams[collateralIndex]) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
75
|
-
const data = (_b = context.borrowingData[collateralIndex]) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
71
|
+
const { positionSizeCollateral, openPrice, currentPairPrice, initialAccBorrowingFeeP, currentTimestamp, } = input;
|
|
72
|
+
const { params, data } = context;
|
|
76
73
|
if (!params || !data) {
|
|
77
74
|
return 0;
|
|
78
75
|
}
|
|
79
76
|
// Calculate current accumulated borrowing fees
|
|
80
|
-
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);
|
|
81
78
|
// Calculate borrowing fees for this trade
|
|
82
79
|
// Formula: (positionSizeCollateral * (currentAccFee - initialAccFee)) / openPrice / 100
|
|
83
80
|
const feeDeltaP = currentAccBorrowingFeeP - initialAccBorrowingFeeP;
|
|
@@ -86,29 +83,6 @@ const getTradeBorrowingFeesCollateral = (input, context) => {
|
|
|
86
83
|
exports.BORROWING_V2_PRECISION.PERCENTAGE);
|
|
87
84
|
};
|
|
88
85
|
exports.getTradeBorrowingFeesCollateral = getTradeBorrowingFeesCollateral;
|
|
89
|
-
/**
|
|
90
|
-
* @dev Convenience function to calculate borrowing fees for a trade using individual parameters
|
|
91
|
-
* @param positionSizeCollateral Position size in collateral tokens
|
|
92
|
-
* @param pairIndex Index of the trading pair
|
|
93
|
-
* @param collateralIndex Index of the collateral
|
|
94
|
-
* @param openPrice Price at which the trade was opened
|
|
95
|
-
* @param currentPairPrice Current price of the trading pair
|
|
96
|
-
* @param initialAccBorrowingFeeP Initial accumulated borrowing fee when trade was opened
|
|
97
|
-
* @param context Context containing borrowing parameters and data
|
|
98
|
-
* @returns Borrowing fees in collateral tokens
|
|
99
|
-
*/
|
|
100
|
-
const getBorrowingFee = (positionSizeCollateral, pairIndex, collateralIndex, openPrice, currentPairPrice, initialAccBorrowingFeeP, context) => {
|
|
101
|
-
return (0, exports.getTradeBorrowingFeesCollateral)({
|
|
102
|
-
positionSizeCollateral,
|
|
103
|
-
openPrice,
|
|
104
|
-
collateralIndex,
|
|
105
|
-
pairIndex,
|
|
106
|
-
currentPairPrice,
|
|
107
|
-
initialAccBorrowingFeeP,
|
|
108
|
-
currentTimestamp: context.currentTimestamp,
|
|
109
|
-
}, context);
|
|
110
|
-
};
|
|
111
|
-
exports.getBorrowingFee = getBorrowingFee;
|
|
112
86
|
/**
|
|
113
87
|
* @dev Utility function to get pending accumulated borrowing fees for a pair using context
|
|
114
88
|
* @param input Pair borrowing fee calculation input
|
|
@@ -116,10 +90,9 @@ exports.getBorrowingFee = getBorrowingFee;
|
|
|
116
90
|
* @returns Updated accumulated borrowing fee (1e20 precision)
|
|
117
91
|
*/
|
|
118
92
|
const getPairBorrowingFees = (input, context) => {
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
const data = (_b = context.borrowingData[collateralIndex]) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
93
|
+
const { pairIndex, currentPairPrice, currentTimestamp } = input;
|
|
94
|
+
const params = context.borrowingParams[pairIndex];
|
|
95
|
+
const data = context.borrowingData[pairIndex];
|
|
123
96
|
if (!params || !data) {
|
|
124
97
|
return 0;
|
|
125
98
|
}
|
|
@@ -133,7 +106,7 @@ exports.borrowingFeeV2Utils = {
|
|
|
133
106
|
getPairPendingAccBorrowingFees: exports.getPairPendingAccBorrowingFees,
|
|
134
107
|
getTradeBorrowingFeesCollateral: exports.getTradeBorrowingFeesCollateral,
|
|
135
108
|
getPairBorrowingFees: exports.getPairBorrowingFees,
|
|
136
|
-
getBorrowingFee: exports.getBorrowingFee,
|
|
137
109
|
};
|
|
138
110
|
exports.BorrowingFeeV2 = __importStar(require("./types"));
|
|
139
111
|
__exportStar(require("./converter"), exports);
|
|
112
|
+
__exportStar(require("./fetcher"), exports);
|
|
@@ -39,6 +39,7 @@ export type TradeInitialAccFees = {
|
|
|
39
39
|
};
|
|
40
40
|
/**
|
|
41
41
|
* @dev Context required for borrowing v2 fee calculations
|
|
42
|
+
* @dev Context is already scoped to a specific collateral
|
|
42
43
|
*/
|
|
43
44
|
export type GetBorrowingFeeV2Context = {
|
|
44
45
|
/**
|
|
@@ -47,20 +48,36 @@ export type GetBorrowingFeeV2Context = {
|
|
|
47
48
|
currentTimestamp?: number;
|
|
48
49
|
/**
|
|
49
50
|
* @dev Borrowing fee parameters for pairs
|
|
50
|
-
* @dev Indexed by: params[
|
|
51
|
+
* @dev Indexed by: params[pairIndex]
|
|
51
52
|
*/
|
|
52
|
-
borrowingParams: Record<number,
|
|
53
|
+
borrowingParams: Record<number, BorrowingFeeParams>;
|
|
53
54
|
/**
|
|
54
55
|
* @dev Borrowing fee data for pairs
|
|
55
|
-
* @dev Indexed by: data[
|
|
56
|
+
* @dev Indexed by: data[pairIndex]
|
|
56
57
|
*/
|
|
57
|
-
borrowingData: Record<number,
|
|
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;
|
|
58
76
|
};
|
|
59
77
|
/**
|
|
60
78
|
* @dev Input for calculating pending borrowing fees for a pair
|
|
61
79
|
*/
|
|
62
80
|
export type PairBorrowingFeeInput = {
|
|
63
|
-
collateralIndex: number;
|
|
64
81
|
pairIndex: number;
|
|
65
82
|
currentPairPrice: number;
|
|
66
83
|
currentTimestamp?: number;
|
|
@@ -71,7 +88,6 @@ export type PairBorrowingFeeInput = {
|
|
|
71
88
|
export type TradeBorrowingFeeInput = {
|
|
72
89
|
positionSizeCollateral: number;
|
|
73
90
|
openPrice: number;
|
|
74
|
-
collateralIndex: number;
|
|
75
91
|
pairIndex: number;
|
|
76
92
|
currentPairPrice: number;
|
|
77
93
|
initialAccBorrowingFeeP: number;
|
|
@@ -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;
|