@gainsnetwork/sdk 1.0.0-rc1 → 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/lib/backend/tradingVariables/index.js +5 -5
- package/lib/constants.d.ts +11 -0
- package/lib/constants.js +17 -7
- package/lib/contracts/utils/pairs.js +11 -0
- package/lib/markets/holdingFees/index.js +4 -4
- package/lib/trade/fees/borrowing/builder.js +1 -0
- package/lib/trade/fees/borrowing/index.d.ts +4 -2
- package/lib/trade/fees/borrowing/index.js +25 -9
- package/lib/trade/fees/fundingFees/converter.js +1 -1
- package/lib/trade/fees/index.d.ts +1 -0
- package/lib/trade/fees/index.js +4 -2
- package/lib/trade/fees/trading/index.js +1 -1
- package/lib/trade/pnl/index.d.ts +2 -0
- package/lib/trade/pnl/index.js +3 -2
- package/lib/trade/priceImpact/open/index.js +1 -0
- package/lib/trade/priceImpact/open/types.d.ts +1 -0
- package/lib/trade/types.d.ts +12 -1
- package/lib/trade/types.js +11 -0
- package/package.json +1 -1
|
@@ -18,7 +18,7 @@ exports.transformGlobalTradingVariables = void 0;
|
|
|
18
18
|
const converter_1 = require("./converter");
|
|
19
19
|
const trade_1 = require("../../trade");
|
|
20
20
|
const transformGlobalTradingVariables = (rawData) => {
|
|
21
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
21
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
22
22
|
const globalTradingVariables = {
|
|
23
23
|
collaterals: (0, converter_1.convertCollaterals)(rawData.collaterals),
|
|
24
24
|
pairs: (0, converter_1.convertTradingPairs)(rawData.pairs),
|
|
@@ -47,11 +47,11 @@ const transformGlobalTradingVariables = (rawData) => {
|
|
|
47
47
|
: [],
|
|
48
48
|
feeTiers: (0, converter_1.convertFeeTiers)(rawData.feeTiers),
|
|
49
49
|
liquidationParams: {
|
|
50
|
-
groups: ((_c = rawData.liquidationParams) === null || _c === void 0 ? void 0 : _c.groups.map(liqParams => (0, trade_1.convertLiquidationParams)(liqParams))) || [],
|
|
51
|
-
pairs: ((
|
|
50
|
+
groups: ((_d = (_c = rawData.liquidationParams) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.map(liqParams => (0, trade_1.convertLiquidationParams)(liqParams))) || [],
|
|
51
|
+
pairs: ((_f = (_e = rawData.liquidationParams) === null || _e === void 0 ? void 0 : _e.pairs) === null || _f === void 0 ? void 0 : _f.map(liqParams => (0, trade_1.convertLiquidationParams)(liqParams))) || [],
|
|
52
52
|
},
|
|
53
53
|
counterTradeSettings: (0, trade_1.convertCounterTradeSettingsArray)(rawData.counterTradeSettings),
|
|
54
|
-
pairFactors: ((
|
|
54
|
+
pairFactors: ((_h = (_g = rawData.pairInfos) === null || _g === void 0 ? void 0 : _g.pairFactors) === null || _h === void 0 ? void 0 : _h.map(factor => (0, converter_1.convertPairFactor)(factor))) || [],
|
|
55
55
|
globalTradeFeeParams: rawData.globalTradeFeeParams
|
|
56
56
|
? (0, converter_1.convertGlobalTradeFeeParams)(rawData.globalTradeFeeParams)
|
|
57
57
|
: undefined,
|
|
@@ -60,7 +60,7 @@ const transformGlobalTradingVariables = (rawData) => {
|
|
|
60
60
|
const currentBlock = (rawData.currentBlock > -1 && rawData.currentBlock) || undefined;
|
|
61
61
|
const l1BlockNumber = (rawData.currentL1Block > -1 && rawData.currentL1Block) || undefined;
|
|
62
62
|
const pairIndexes = {};
|
|
63
|
-
for (let i = 0; i < ((
|
|
63
|
+
for (let i = 0; i < ((_j = rawData.pairs) === null || _j === void 0 ? void 0 : _j.length); i++) {
|
|
64
64
|
pairIndexes[rawData.pairs[i].from + "/" + rawData.pairs[i].to] = i;
|
|
65
65
|
}
|
|
66
66
|
if (globalTradingVariables.collaterals !== undefined) {
|
package/lib/constants.d.ts
CHANGED
|
@@ -396,6 +396,17 @@ export declare const pairs: {
|
|
|
396
396
|
"PLTR/USD": string;
|
|
397
397
|
"BIDU/USD": string;
|
|
398
398
|
"ROKU/USD": string;
|
|
399
|
+
"LMT/USD": string;
|
|
400
|
+
"RIOT/USD": string;
|
|
401
|
+
"MARA/USD": string;
|
|
402
|
+
"LOKA/USD": string;
|
|
403
|
+
"STO/USD": string;
|
|
404
|
+
"FUN/USD": string;
|
|
405
|
+
"KNC/USD": string;
|
|
406
|
+
"H/USD": string;
|
|
407
|
+
"ICNT/USD": string;
|
|
408
|
+
"NEWT/USD": string;
|
|
409
|
+
"PUMP/USD": string;
|
|
399
410
|
};
|
|
400
411
|
export declare const syntheticPairs: Set<string>;
|
|
401
412
|
export declare const parentToSyntheticPairMap: Map<string, string>;
|
package/lib/constants.js
CHANGED
|
@@ -404,6 +404,17 @@ exports.pairs = {
|
|
|
404
404
|
"PLTR/USD": STOCKS,
|
|
405
405
|
"BIDU/USD": STOCKS,
|
|
406
406
|
"ROKU/USD": STOCKS,
|
|
407
|
+
"LMT/USD": STOCKS,
|
|
408
|
+
"RIOT/USD": STOCKS,
|
|
409
|
+
"MARA/USD": STOCKS,
|
|
410
|
+
"LOKA/USD": CRYPTO,
|
|
411
|
+
"STO/USD": CRYPTO,
|
|
412
|
+
"FUN/USD": CRYPTO,
|
|
413
|
+
"KNC/USD": CRYPTO,
|
|
414
|
+
"H/USD": CRYPTO,
|
|
415
|
+
"ICNT/USD": CRYPTO,
|
|
416
|
+
"NEWT/USD": CRYPTO,
|
|
417
|
+
"PUMP/USD": CRYPTO,
|
|
407
418
|
};
|
|
408
419
|
exports.syntheticPairs = new Set([
|
|
409
420
|
"BTCDEGEN/USD",
|
|
@@ -451,13 +462,12 @@ exports.stockSplits = {
|
|
|
451
462
|
"TSLA_1/USD": { date: "8/25/2022", split: 3 },
|
|
452
463
|
};
|
|
453
464
|
exports.delistedPairIxs = new Set([
|
|
454
|
-
4, 6, 15, 24, 25, 27, 28, 30, 31, 36, 48, 51, 52, 53, 54, 56, 59, 60,
|
|
455
|
-
66, 67, 68, 69, 70, 71, 72, 73,
|
|
456
|
-
101, 106, 111, 113, 114, 116, 118, 120, 122, 123, 125, 127, 130, 147, 152,
|
|
457
|
-
160, 163, 170, 179, 182, 183,
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
393, 394, 395, 396,
|
|
465
|
+
4, 6, 12, 15, 20, 24, 25, 27, 28, 30, 31, 36, 48, 51, 52, 53, 54, 56, 59, 60,
|
|
466
|
+
61, 63, 66, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 95, 96, 97, 98,
|
|
467
|
+
99, 101, 106, 111, 113, 114, 116, 118, 120, 122, 123, 125, 127, 130, 147, 152,
|
|
468
|
+
160, 163, 170, 179, 182, 183, 188, 189, 190, 208, 209, 225, 229, 230, 231,
|
|
469
|
+
238, 239, 241, 250, 253, 254, 258, 270, 275, 276, 278, 279, 282, 285, 290,
|
|
470
|
+
294, 296, 305, 311, 330, 349, 352, 353, 354, 355, 357, 365, 366, 395, 396,
|
|
461
471
|
]);
|
|
462
472
|
exports.delistedGroupsIxs = new Set([]);
|
|
463
473
|
exports.DEFAULT_PROTECTION_CLOSE_FACTOR = 1;
|
|
@@ -502,4 +502,15 @@ const PAIR_INDEX_TO_DESCRIPTION = {
|
|
|
502
502
|
[types_1.PairIndex.PLTRUSD]: "Palantir Technologies to US Dollar",
|
|
503
503
|
[types_1.PairIndex.BIDUUSD]: "Baidu to US Dollar",
|
|
504
504
|
[types_1.PairIndex.ROKUUSD]: "Roku to US Dollar",
|
|
505
|
+
[types_1.PairIndex.LMTUSD]: "Lockheed Martin to US Dollar",
|
|
506
|
+
[types_1.PairIndex.RIOTUSD]: "Riot Platforms to US Dollar",
|
|
507
|
+
[types_1.PairIndex.MARAUSD]: "MARA Holdings to US Dollar",
|
|
508
|
+
[types_1.PairIndex.LOKAUSD]: "League of Kingdoms Arena to US Dollar",
|
|
509
|
+
[types_1.PairIndex.STOUSD]: "StakeStone to US Dollar",
|
|
510
|
+
[types_1.PairIndex.FUNUSD]: "FUNToken to US Dollar",
|
|
511
|
+
[types_1.PairIndex.KNCUSD]: "Kyber Network Crystal v2 to US Dollar",
|
|
512
|
+
[types_1.PairIndex.HUSD]: "Humanity Protocol to US Dollar",
|
|
513
|
+
[types_1.PairIndex.ICNTUSD]: "Impossible Cloud Network to US Dollar",
|
|
514
|
+
[types_1.PairIndex.NEWTUSD]: "Newton Protocol to US Dollar",
|
|
515
|
+
[types_1.PairIndex.PUMPUSD]: "Pump.fun to US Dollar",
|
|
505
516
|
};
|
|
@@ -50,8 +50,8 @@ const getPairHoldingFeeRates = (input) => {
|
|
|
50
50
|
// Get APR multipliers
|
|
51
51
|
const { longAprMultiplier, shortAprMultiplier } = (0, fundingFees_1.getLongShortAprMultiplier)(currentFundingRatePerSecondP, pairOiToken.oiLongToken, pairOiToken.oiShortToken, fundingParams.aprMultiplierEnabled);
|
|
52
52
|
// Calculate hourly rates
|
|
53
|
-
// Funding rate * seconds per hour *
|
|
54
|
-
const baseHourlyRate = (currentFundingRatePerSecondP * SECONDS_PER_HOUR
|
|
53
|
+
// Funding rate * seconds per hour * APR multiplier / 100
|
|
54
|
+
const baseHourlyRate = (currentFundingRatePerSecondP * SECONDS_PER_HOUR) /
|
|
55
55
|
PERCENTAGE_PRECISION;
|
|
56
56
|
// Long side pays when rate is positive, earns when negative
|
|
57
57
|
fundingFeeLongHourlyRate = baseHourlyRate * longAprMultiplier;
|
|
@@ -63,9 +63,9 @@ const getPairHoldingFeeRates = (input) => {
|
|
|
63
63
|
let currentBorrowingRatePerSecondP = 0;
|
|
64
64
|
if (borrowingParams && borrowingData) {
|
|
65
65
|
currentBorrowingRatePerSecondP = borrowingParams.borrowingRatePerSecondP;
|
|
66
|
-
// Borrowing rate * seconds per hour
|
|
66
|
+
// Borrowing rate * seconds per hour / 100
|
|
67
67
|
borrowingFeeHourlyRate =
|
|
68
|
-
(currentBorrowingRatePerSecondP * SECONDS_PER_HOUR
|
|
68
|
+
(currentBorrowingRatePerSecondP * SECONDS_PER_HOUR) /
|
|
69
69
|
PERCENTAGE_PRECISION;
|
|
70
70
|
}
|
|
71
71
|
// Total holding fees (funding can be negative/positive, borrowing always positive cost)
|
|
@@ -28,6 +28,7 @@ const buildBorrowingV1Context = (globalTradingVariables, collateralIndex, curren
|
|
|
28
28
|
pairs,
|
|
29
29
|
groups,
|
|
30
30
|
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
31
|
+
pairOis: collateral.pairOis,
|
|
31
32
|
};
|
|
32
33
|
};
|
|
33
34
|
exports.buildBorrowingV1Context = buildBorrowingV1Context;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { UnifiedPairOi } from "../../..";
|
|
1
2
|
import { OpenInterest, PairIndex } from "../../types";
|
|
2
3
|
import * as BorrowingFee from "./types";
|
|
3
4
|
export type GetBorrowingFeeContext = {
|
|
@@ -5,6 +6,7 @@ export type GetBorrowingFeeContext = {
|
|
|
5
6
|
groups: BorrowingFee.Group[];
|
|
6
7
|
pairs: BorrowingFee.Pair[];
|
|
7
8
|
collateralPriceUsd: number;
|
|
9
|
+
pairOis: UnifiedPairOi[];
|
|
8
10
|
};
|
|
9
11
|
/**
|
|
10
12
|
* @dev Calculates borrowing fees using v1 model (block-based with groups)
|
|
@@ -17,7 +19,7 @@ export type GetBorrowingFeeContext = {
|
|
|
17
19
|
* @param context Context with current block, fee data, and collateral price
|
|
18
20
|
* @returns Borrowing fee in collateral tokens
|
|
19
21
|
*/
|
|
20
|
-
export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex | undefined, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
|
|
22
|
+
export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex | undefined, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, currentPairPrice: number, context: GetBorrowingFeeContext) => number;
|
|
21
23
|
/**
|
|
22
24
|
* @dev This function uses static OI which doesn't reflect current market values
|
|
23
25
|
* @dev The v10 contracts use dynamic OI (beforeV10 + afterV10Token * currentPrice)
|
|
@@ -27,7 +29,7 @@ export declare const withinMaxGroupOi: (pairIndex: PairIndex, long: boolean, pos
|
|
|
27
29
|
pairs: BorrowingFee.Pair[];
|
|
28
30
|
}) => boolean;
|
|
29
31
|
export declare const borrowingFeeUtils: {
|
|
30
|
-
getPairGroupAccFeesDeltas: (i: number, pairGroups: BorrowingFee.PairGroup[], initialFees: BorrowingFee.InitialAccFees, pairIndex: PairIndex, long: boolean, context: GetBorrowingFeeContext) => {
|
|
32
|
+
getPairGroupAccFeesDeltas: (i: number, pairGroups: BorrowingFee.PairGroup[], initialFees: BorrowingFee.InitialAccFees, pairIndex: PairIndex, long: boolean, currentPairPrice: number, context: GetBorrowingFeeContext) => {
|
|
31
33
|
deltaGroup: number;
|
|
32
34
|
deltaPair: number;
|
|
33
35
|
beforeTradeOpen: boolean;
|
|
@@ -27,6 +27,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = exports.getBorrowingFee = void 0;
|
|
30
|
+
const __1 = require("../../..");
|
|
30
31
|
/**
|
|
31
32
|
* @dev Calculates borrowing fees using v1 model (block-based with groups)
|
|
32
33
|
* @dev Still actively used by markets that haven't migrated to v2
|
|
@@ -38,7 +39,7 @@ exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = ex
|
|
|
38
39
|
* @param context Context with current block, fee data, and collateral price
|
|
39
40
|
* @returns Borrowing fee in collateral tokens
|
|
40
41
|
*/
|
|
41
|
-
const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
42
|
+
const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, currentPairPrice, context) => {
|
|
42
43
|
if (pairIndex === undefined) {
|
|
43
44
|
throw new Error("pairIndex is required for borrowing fee calculations");
|
|
44
45
|
}
|
|
@@ -50,12 +51,19 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
50
51
|
const firstPairGroup = (pairGroups === null || pairGroups === void 0 ? void 0 : pairGroups.length) > 0 ? pairGroups[0] : undefined;
|
|
51
52
|
let fee = 0;
|
|
52
53
|
if (!firstPairGroup || firstPairGroup.block > initialAccFees.block) {
|
|
53
|
-
const openInterest =
|
|
54
|
+
const openInterest = (0, __1.getPairTotalOisDynamicCollateral)(pairIndex, {
|
|
55
|
+
pairOis: context.pairOis,
|
|
56
|
+
currentPairPrice,
|
|
57
|
+
});
|
|
54
58
|
fee =
|
|
55
59
|
(!firstPairGroup
|
|
56
60
|
? getPairPendingAccFee(pairIndex, context.currentBlock, long, {
|
|
57
61
|
pairs,
|
|
58
|
-
openInterest
|
|
62
|
+
openInterest: {
|
|
63
|
+
long: openInterest.long,
|
|
64
|
+
short: openInterest.short,
|
|
65
|
+
max: context.pairOis[pairIndex].maxCollateral,
|
|
66
|
+
},
|
|
59
67
|
collateralPriceUsd: context.collateralPriceUsd,
|
|
60
68
|
})
|
|
61
69
|
: long
|
|
@@ -63,11 +71,12 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
63
71
|
: firstPairGroup.pairAccFeeShort) - initialAccFees.accPairFee;
|
|
64
72
|
}
|
|
65
73
|
for (let i = pairGroups.length; i > 0; i--) {
|
|
66
|
-
const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long, {
|
|
74
|
+
const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long, currentPairPrice, {
|
|
67
75
|
currentBlock: context.currentBlock,
|
|
68
76
|
groups,
|
|
69
77
|
pairs,
|
|
70
78
|
collateralPriceUsd: context.collateralPriceUsd,
|
|
79
|
+
pairOis: context.pairOis,
|
|
71
80
|
});
|
|
72
81
|
fee += Math.max(deltaGroup, deltaPair);
|
|
73
82
|
if (beforeTradeOpen) {
|
|
@@ -101,7 +110,7 @@ const getPairGroupIndex = (pairIndex, context) => {
|
|
|
101
110
|
const getPairPendingAccFees = (pairIndex, currentBlock, context) => {
|
|
102
111
|
const { pairs, openInterest: { long, short }, collateralPriceUsd, } = context;
|
|
103
112
|
const pair = pairs[pairIndex];
|
|
104
|
-
return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock,
|
|
113
|
+
return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock, context.openInterest.max, pair.feeExponent, pair.feePerBlockCap, collateralPriceUsd);
|
|
105
114
|
};
|
|
106
115
|
const getPairPendingAccFee = (pairIndex, currentBlock, long, context) => {
|
|
107
116
|
const { accFeeLong, accFeeShort } = getPairPendingAccFees(pairIndex, currentBlock, context);
|
|
@@ -117,20 +126,27 @@ const getGroupPendingAccFee = (groupIndex, currentBlock, long, context) => {
|
|
|
117
126
|
const { accFeeLong, accFeeShort } = getGroupPendingAccFees(groupIndex, currentBlock, context);
|
|
118
127
|
return long ? accFeeLong : accFeeShort;
|
|
119
128
|
};
|
|
120
|
-
const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long, context) => {
|
|
129
|
+
const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long, currentPairPrice, context) => {
|
|
121
130
|
const group = pairGroups[i];
|
|
122
131
|
const beforeTradeOpen = group.block < initialFees.block;
|
|
123
132
|
let deltaGroup, deltaPair;
|
|
124
133
|
if (i == pairGroups.length - 1) {
|
|
125
134
|
const { currentBlock, groups, pairs, collateralPriceUsd } = context;
|
|
126
|
-
const openInterest =
|
|
135
|
+
const openInterest = (0, __1.getPairTotalOisDynamicCollateral)(pairIndex, {
|
|
136
|
+
pairOis: context.pairOis,
|
|
137
|
+
currentPairPrice,
|
|
138
|
+
});
|
|
127
139
|
deltaGroup = getGroupPendingAccFee(group.groupIndex, currentBlock, long, {
|
|
128
140
|
groups,
|
|
129
141
|
collateralPriceUsd,
|
|
130
142
|
});
|
|
131
143
|
deltaPair = getPairPendingAccFee(pairIndex, currentBlock, long, {
|
|
132
144
|
pairs,
|
|
133
|
-
openInterest
|
|
145
|
+
openInterest: {
|
|
146
|
+
long: openInterest.long,
|
|
147
|
+
short: openInterest.short,
|
|
148
|
+
max: context.pairOis[pairIndex].maxCollateral,
|
|
149
|
+
},
|
|
134
150
|
collateralPriceUsd,
|
|
135
151
|
});
|
|
136
152
|
}
|
|
@@ -180,7 +196,7 @@ collateralPriceUsd) => {
|
|
|
180
196
|
const maxNetOi = maxOiUsd * maxP;
|
|
181
197
|
// Calculate the minimum acc fee delta (applies to both sides)
|
|
182
198
|
const minDelta = minNetOi > 0
|
|
183
|
-
? getPendingAccFeesDelta(blockDistance, feePerBlock,
|
|
199
|
+
? getPendingAccFeesDelta(blockDistance, feePerBlock, minNetOi, maxOiUsd, feeExponent)
|
|
184
200
|
: 0;
|
|
185
201
|
// Calculate the actual acc fee (using capped oi of 100% or less)
|
|
186
202
|
const delta = netOi > minNetOi
|
|
@@ -74,7 +74,7 @@ exports.convertPairFundingFeeDataArray = convertPairFundingFeeDataArray;
|
|
|
74
74
|
*/
|
|
75
75
|
const convertPairGlobalParams = (contractParams) => {
|
|
76
76
|
return {
|
|
77
|
-
maxSkewCollateral: Number(contractParams.maxSkewCollateral),
|
|
77
|
+
maxSkewCollateral: Number(contractParams.maxSkewCollateral) / 1e10,
|
|
78
78
|
};
|
|
79
79
|
};
|
|
80
80
|
exports.convertPairGlobalParams = convertPairGlobalParams;
|
|
@@ -6,5 +6,6 @@ export { convertTradeFeesData, convertTradeFeesDataArray, convertUiRealizedPnlDa
|
|
|
6
6
|
export { BorrowingFeeV2, borrowingFeeV2Utils, getPairPendingAccBorrowingFees as getPairPendingAccBorrowingFeesV2, getTradeBorrowingFeesCollateral as getTradeBorrowingFeesCollateralV2, getPairBorrowingFees as getPairBorrowingFeesV2, MAX_BORROWING_RATE_PER_SECOND as MAX_BORROWING_RATE_PER_SECOND_V2, BORROWING_V2_PRECISION, } from "./borrowingV2";
|
|
7
7
|
export { convertBorrowingFeeParams as convertBorrowingFeeParamsV2, convertBorrowingFeeParamsArray as convertBorrowingFeeParamsArrayV2, convertPairBorrowingFeeData as convertPairBorrowingFeeDataV2, convertPairBorrowingFeeDataArray as convertPairBorrowingFeeDataArrayV2, convertTradeInitialAccFees as convertTradeInitialAccFeesV2, convertTradeInitialAccFeesArray as convertTradeInitialAccFeesArrayV2, createBorrowingV2Context, isValidBorrowingRate as isValidBorrowingRateV2, borrowingRateToAPR as borrowingRateToAPRV2, aprToBorrowingRate as aprToBorrowingRateV2, } from "./borrowingV2/converter";
|
|
8
8
|
export { fetchBorrowingFeeParamsV2, fetchPairBorrowingFeeDataV2, fetchTradeBorrowingFeesCollateralV2, fetchPairPendingAccBorrowingFeesV2, fetchAllBorrowingV2Data, createBorrowingV2ContextFromContract, createBorrowingV2ContextFromArrays, fetchBorrowingV2DataForPairs, } from "./borrowingV2/fetcher";
|
|
9
|
+
export { buildBorrowingV2Context } from "./borrowingV2/builder";
|
|
9
10
|
export { FundingFees, getCurrentFundingVelocityPerYear, getSecondsToReachZeroRate, getAvgFundingRatePerSecondP, getLongShortAprMultiplier, getPairPendingAccFundingFees, getTradeFundingFeesCollateral, getTradeFundingFeesCollateralSimple, getTradeFundingFees, } from "./fundingFees";
|
|
10
11
|
export { convertFundingFeeParams, convertFundingFeeParamsArray, convertPairFundingFeeData, convertPairFundingFeeDataArray, convertPairGlobalParams, convertPairGlobalParamsArray, convertTradeInitialAccFundingFees, createFundingFeeContext, isValidFundingRate, fundingRateToAPR, aprToFundingRate, calculateVelocityFromSkew, FUNDING_FEES_PRECISION, } from "./fundingFees/converter";
|
package/lib/trade/fees/index.js
CHANGED
|
@@ -14,8 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
18
|
-
exports.FUNDING_FEES_PRECISION = exports.calculateVelocityFromSkew = exports.aprToFundingRate = void 0;
|
|
17
|
+
exports.isValidFundingRate = exports.createFundingFeeContext = exports.convertTradeInitialAccFundingFees = exports.convertPairGlobalParamsArray = exports.convertPairGlobalParams = exports.convertPairFundingFeeDataArray = exports.convertPairFundingFeeData = exports.convertFundingFeeParamsArray = exports.convertFundingFeeParams = exports.getTradeFundingFees = exports.getTradeFundingFeesCollateralSimple = exports.getTradeFundingFeesCollateral = exports.getPairPendingAccFundingFees = exports.getLongShortAprMultiplier = exports.getAvgFundingRatePerSecondP = exports.getSecondsToReachZeroRate = exports.getCurrentFundingVelocityPerYear = exports.FundingFees = exports.buildBorrowingV2Context = exports.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = exports.aprToBorrowingRateV2 = exports.borrowingRateToAPRV2 = exports.isValidBorrowingRateV2 = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArrayV2 = exports.convertTradeInitialAccFeesV2 = exports.convertPairBorrowingFeeDataArrayV2 = exports.convertPairBorrowingFeeDataV2 = exports.convertBorrowingFeeParamsArrayV2 = exports.convertBorrowingFeeParamsV2 = exports.BORROWING_V2_PRECISION = exports.MAX_BORROWING_RATE_PER_SECOND_V2 = exports.getPairBorrowingFeesV2 = exports.getTradeBorrowingFeesCollateralV2 = exports.getPairPendingAccBorrowingFeesV2 = exports.borrowingFeeV2Utils = exports.BorrowingFeeV2 = exports.encodeUiRealizedPnlData = exports.encodeTradeFeesData = exports.convertUiRealizedPnlDataArray = exports.convertUiRealizedPnlData = exports.convertTradeFeesDataArray = exports.convertTradeFeesData = void 0;
|
|
18
|
+
exports.FUNDING_FEES_PRECISION = exports.calculateVelocityFromSkew = exports.aprToFundingRate = exports.fundingRateToAPR = void 0;
|
|
19
19
|
__exportStar(require("./borrowing"), exports);
|
|
20
20
|
__exportStar(require("./tiers"), exports);
|
|
21
21
|
__exportStar(require("./trading"), exports);
|
|
@@ -58,6 +58,8 @@ Object.defineProperty(exports, "fetchAllBorrowingV2Data", { enumerable: true, ge
|
|
|
58
58
|
Object.defineProperty(exports, "createBorrowingV2ContextFromContract", { enumerable: true, get: function () { return fetcher_1.createBorrowingV2ContextFromContract; } });
|
|
59
59
|
Object.defineProperty(exports, "createBorrowingV2ContextFromArrays", { enumerable: true, get: function () { return fetcher_1.createBorrowingV2ContextFromArrays; } });
|
|
60
60
|
Object.defineProperty(exports, "fetchBorrowingV2DataForPairs", { enumerable: true, get: function () { return fetcher_1.fetchBorrowingV2DataForPairs; } });
|
|
61
|
+
var builder_1 = require("./borrowingV2/builder");
|
|
62
|
+
Object.defineProperty(exports, "buildBorrowingV2Context", { enumerable: true, get: function () { return builder_1.buildBorrowingV2Context; } });
|
|
61
63
|
// Funding Fees exports
|
|
62
64
|
var fundingFees_1 = require("./fundingFees");
|
|
63
65
|
Object.defineProperty(exports, "FundingFees", { enumerable: true, get: function () { return fundingFees_1.FundingFees; } });
|
|
@@ -137,7 +137,7 @@ const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, c
|
|
|
137
137
|
// Calculate v1 borrowing fees (some markets use v1 indefinitely)
|
|
138
138
|
let borrowingFeeCollateral_old = 0;
|
|
139
139
|
if (context.borrowingV1 && context.initialAccFees) {
|
|
140
|
-
borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFees, context.borrowingV1);
|
|
140
|
+
borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFees, currentPairPrice, context.borrowingV1);
|
|
141
141
|
}
|
|
142
142
|
return {
|
|
143
143
|
fundingFeeCollateral,
|
package/lib/trade/pnl/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { Trade, TradeInfo, LiquidationParams, Fee, GlobalTradeFeeParams, TradeFe
|
|
|
6
6
|
import { ComprehensivePnlResult, GetComprehensivePnlContext } from "./types";
|
|
7
7
|
import { BorrowingFee } from "../fees/borrowing";
|
|
8
8
|
import { ContractsVersion } from "../../contracts/types";
|
|
9
|
+
import { UnifiedPairOi } from "src/markets";
|
|
9
10
|
/**
|
|
10
11
|
* @dev Gets trade realized PnL components from TradeFeesData
|
|
11
12
|
* @dev Mirrors contract's getTradeRealizedPnlCollateral function
|
|
@@ -60,6 +61,7 @@ export type GetPnlContext = {
|
|
|
60
61
|
fee: Fee;
|
|
61
62
|
globalTradeFeeParams: GlobalTradeFeeParams;
|
|
62
63
|
traderFeeMultiplier?: number;
|
|
64
|
+
pairOis: UnifiedPairOi[];
|
|
63
65
|
};
|
|
64
66
|
/**
|
|
65
67
|
* @dev Legacy PnL calculation function
|
package/lib/trade/pnl/index.js
CHANGED
|
@@ -91,7 +91,7 @@ const getComprehensivePnl = (trade, marketPrice, executionPrice, tradeInfo, cont
|
|
|
91
91
|
// Calculate position size
|
|
92
92
|
const positionSizeCollateral = trade.collateralAmount * trade.leverage;
|
|
93
93
|
// Calculate holding fees - always use getTradePendingHoldingFeesCollateral
|
|
94
|
-
const pendingHoldingFees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeData.tradeFeesData,
|
|
94
|
+
const pendingHoldingFees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeData.tradeFeesData, marketPrice, {
|
|
95
95
|
contractsVersion: context.core.contractsVersion,
|
|
96
96
|
currentTimestamp: context.core.currentTimestamp,
|
|
97
97
|
collateralPriceUsd: context.core.collateralPriceUsd,
|
|
@@ -206,11 +206,12 @@ const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, use
|
|
|
206
206
|
context.groups &&
|
|
207
207
|
context.currentBlock !== undefined &&
|
|
208
208
|
context.collateralPriceUsd !== undefined) {
|
|
209
|
-
pnlCollat -= (0, borrowing_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, {
|
|
209
|
+
pnlCollat -= (0, borrowing_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, price, {
|
|
210
210
|
currentBlock: context.currentBlock,
|
|
211
211
|
groups: context.groups,
|
|
212
212
|
pairs: context.pairs,
|
|
213
213
|
collateralPriceUsd: context.collateralPriceUsd,
|
|
214
|
+
pairOis: context.pairOis,
|
|
214
215
|
});
|
|
215
216
|
}
|
|
216
217
|
let pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
@@ -56,6 +56,7 @@ const getTradeOpeningPriceImpact = (input, context) => {
|
|
|
56
56
|
return {
|
|
57
57
|
priceAfterImpact,
|
|
58
58
|
percentProfitP,
|
|
59
|
+
fixedSpreadP: spreadP,
|
|
59
60
|
cumulVolPriceImpactP,
|
|
60
61
|
baseSkewPriceImpactP: skewPriceImpactObject.basePriceImpactP,
|
|
61
62
|
tradeSkewPriceImpactP: skewPriceImpactObject.tradePriceImpactP,
|
|
@@ -35,6 +35,7 @@ export type TradeOpeningPriceImpactContext = {
|
|
|
35
35
|
export type TradeOpeningPriceImpactResult = {
|
|
36
36
|
priceAfterImpact: number;
|
|
37
37
|
percentProfitP: number;
|
|
38
|
+
fixedSpreadP: number;
|
|
38
39
|
cumulVolPriceImpactP: number;
|
|
39
40
|
baseSkewPriceImpactP: number;
|
|
40
41
|
tradeSkewPriceImpactP: number;
|
package/lib/trade/types.d.ts
CHANGED
|
@@ -719,5 +719,16 @@ export declare enum PairIndex {
|
|
|
719
719
|
SBETUSD = 393,
|
|
720
720
|
PLTRUSD = 394,
|
|
721
721
|
BIDUUSD = 395,
|
|
722
|
-
ROKUUSD = 396
|
|
722
|
+
ROKUUSD = 396,
|
|
723
|
+
LMTUSD = 397,
|
|
724
|
+
RIOTUSD = 398,
|
|
725
|
+
MARAUSD = 399,
|
|
726
|
+
LOKAUSD = 400,
|
|
727
|
+
STOUSD = 401,
|
|
728
|
+
FUNUSD = 402,
|
|
729
|
+
KNCUSD = 403,
|
|
730
|
+
HUSD = 404,
|
|
731
|
+
ICNTUSD = 405,
|
|
732
|
+
NEWTUSD = 406,
|
|
733
|
+
PUMPUSD = 407
|
|
723
734
|
}
|
package/lib/trade/types.js
CHANGED
|
@@ -426,4 +426,15 @@ var PairIndex;
|
|
|
426
426
|
PairIndex[PairIndex["PLTRUSD"] = 394] = "PLTRUSD";
|
|
427
427
|
PairIndex[PairIndex["BIDUUSD"] = 395] = "BIDUUSD";
|
|
428
428
|
PairIndex[PairIndex["ROKUUSD"] = 396] = "ROKUUSD";
|
|
429
|
+
PairIndex[PairIndex["LMTUSD"] = 397] = "LMTUSD";
|
|
430
|
+
PairIndex[PairIndex["RIOTUSD"] = 398] = "RIOTUSD";
|
|
431
|
+
PairIndex[PairIndex["MARAUSD"] = 399] = "MARAUSD";
|
|
432
|
+
PairIndex[PairIndex["LOKAUSD"] = 400] = "LOKAUSD";
|
|
433
|
+
PairIndex[PairIndex["STOUSD"] = 401] = "STOUSD";
|
|
434
|
+
PairIndex[PairIndex["FUNUSD"] = 402] = "FUNUSD";
|
|
435
|
+
PairIndex[PairIndex["KNCUSD"] = 403] = "KNCUSD";
|
|
436
|
+
PairIndex[PairIndex["HUSD"] = 404] = "HUSD";
|
|
437
|
+
PairIndex[PairIndex["ICNTUSD"] = 405] = "ICNTUSD";
|
|
438
|
+
PairIndex[PairIndex["NEWTUSD"] = 406] = "NEWTUSD";
|
|
439
|
+
PairIndex[PairIndex["PUMPUSD"] = 407] = "PUMPUSD";
|
|
429
440
|
})(PairIndex = exports.PairIndex || (exports.PairIndex = {}));
|