@gainsnetwork/sdk 1.0.5-rc3 → 1.0.6-rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/backend/globalTrades/index.js +10 -10
- package/lib/backend/tradingVariables/converter.js +57 -57
- package/lib/backend/tradingVariables/index.js +6 -7
- package/lib/contracts/addresses.js +4 -1
- package/lib/contracts/index.d.ts +1 -1
- package/lib/contracts/index.js +3 -3
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +386 -260
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +2 -10
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +1621 -219
- package/lib/contracts/utils/borrowingFees.js +9 -20
- package/lib/contracts/utils/openTrades.js +11 -20
- package/lib/contracts/utils/pairs.js +12 -21
- package/lib/markets/forex.js +1 -1
- package/lib/markets/leverage/builder.js +2 -2
- package/lib/markets/price/index.d.ts +1 -0
- package/lib/markets/price/index.js +1 -0
- package/lib/markets/price/signedPrices.d.ts +20 -4
- package/lib/markets/price/signedPrices.js +65 -21
- package/lib/markets/price/types.d.ts +23 -0
- package/lib/trade/fees/borrowing/builder.js +2 -3
- package/lib/trade/fees/borrowing/converter.js +5 -1
- package/lib/trade/fees/borrowing/index.js +5 -5
- package/lib/trade/fees/borrowingV2/builder.js +3 -4
- package/lib/trade/fees/borrowingV2/converter.js +1 -1
- package/lib/trade/fees/borrowingV2/fetcher.js +26 -32
- package/lib/trade/fees/borrowingV2/index.js +3 -3
- package/lib/trade/fees/converter.js +22 -22
- package/lib/trade/fees/fundingFees/builder.js +6 -7
- package/lib/trade/fees/fundingFees/converter.js +1 -1
- package/lib/trade/fees/fundingFees/fetcher.js +16 -25
- package/lib/trade/fees/fundingFees/index.js +2 -3
- package/lib/trade/fees/tiers/index.js +1 -2
- package/lib/trade/fees/trading/index.js +5 -3
- package/lib/trade/liquidation/builder.js +6 -3
- package/lib/trade/liquidation/index.js +4 -6
- package/lib/trade/oiWindows.js +1 -2
- package/lib/trade/pnl/builder.js +1 -2
- package/lib/trade/pnl/converter.js +1 -1
- package/lib/trade/pnl/index.js +4 -7
- package/lib/trade/priceImpact/close/builder.js +1 -2
- package/lib/trade/priceImpact/close/index.js +4 -1
- package/lib/trade/priceImpact/cumulVol/builder.js +18 -10
- package/lib/trade/priceImpact/cumulVol/index.js +21 -16
- package/lib/trade/priceImpact/open/builder.js +1 -2
- package/lib/trade/priceImpact/open/index.js +4 -1
- package/lib/trade/priceImpact/skew/builder.js +2 -3
- package/lib/trade/priceImpact/skew/converter.js +1 -1
- package/lib/trade/priceImpact/skew/fetcher.js +24 -33
- package/package.json +2 -2
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.fetchBorrowingV2DataForPairs = exports.createBorrowingV2ContextFromArrays = exports.createBorrowingV2ContextFromContract = exports.fetchAllBorrowingV2Data = exports.fetchPairPendingAccBorrowingFeesV2 = exports.fetchTradeBorrowingFeesCollateralV2 = exports.fetchPairBorrowingFeeDataV2 = exports.fetchBorrowingFeeParamsV2 = void 0;
|
|
13
4
|
const converter_1 = require("./converter");
|
|
@@ -18,19 +9,19 @@ const converter_1 = require("./converter");
|
|
|
18
9
|
* @param pairIndices Array of pair indices
|
|
19
10
|
* @returns Promise resolving to array of borrowing fee parameters
|
|
20
11
|
*/
|
|
21
|
-
const fetchBorrowingFeeParamsV2 = (contract, collateralIndices, pairIndices) =>
|
|
12
|
+
const fetchBorrowingFeeParamsV2 = async (contract, collateralIndices, pairIndices) => {
|
|
22
13
|
if (collateralIndices.length !== pairIndices.length) {
|
|
23
14
|
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
24
15
|
}
|
|
25
16
|
try {
|
|
26
|
-
const contractParams =
|
|
17
|
+
const contractParams = await contract.getPairBorrowingFeeParams(collateralIndices, pairIndices);
|
|
27
18
|
return (0, converter_1.convertBorrowingFeeParamsArray)(contractParams);
|
|
28
19
|
}
|
|
29
20
|
catch (error) {
|
|
30
21
|
console.error("Error fetching borrowing fee params v2:", error);
|
|
31
22
|
throw error;
|
|
32
23
|
}
|
|
33
|
-
}
|
|
24
|
+
};
|
|
34
25
|
exports.fetchBorrowingFeeParamsV2 = fetchBorrowingFeeParamsV2;
|
|
35
26
|
/**
|
|
36
27
|
* @dev Fetches pair borrowing fee data v2 for specific pairs
|
|
@@ -39,19 +30,19 @@ exports.fetchBorrowingFeeParamsV2 = fetchBorrowingFeeParamsV2;
|
|
|
39
30
|
* @param pairIndices Array of pair indices
|
|
40
31
|
* @returns Promise resolving to array of pair borrowing fee data
|
|
41
32
|
*/
|
|
42
|
-
const fetchPairBorrowingFeeDataV2 = (contract, collateralIndices, pairIndices) =>
|
|
33
|
+
const fetchPairBorrowingFeeDataV2 = async (contract, collateralIndices, pairIndices) => {
|
|
43
34
|
if (collateralIndices.length !== pairIndices.length) {
|
|
44
35
|
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
45
36
|
}
|
|
46
37
|
try {
|
|
47
|
-
const contractData =
|
|
38
|
+
const contractData = await contract.getPairBorrowingFeeData(collateralIndices, pairIndices);
|
|
48
39
|
return (0, converter_1.convertPairBorrowingFeeDataArray)(contractData);
|
|
49
40
|
}
|
|
50
41
|
catch (error) {
|
|
51
42
|
console.error("Error fetching pair borrowing fee data v2:", error);
|
|
52
43
|
throw error;
|
|
53
44
|
}
|
|
54
|
-
}
|
|
45
|
+
};
|
|
55
46
|
exports.fetchPairBorrowingFeeDataV2 = fetchPairBorrowingFeeDataV2;
|
|
56
47
|
/**
|
|
57
48
|
* @dev Fetches borrowing fees in collateral tokens for a specific trade
|
|
@@ -61,9 +52,9 @@ exports.fetchPairBorrowingFeeDataV2 = fetchPairBorrowingFeeDataV2;
|
|
|
61
52
|
* @param currentPairPrice Current price of the trading pair (1e6 precision)
|
|
62
53
|
* @returns Promise resolving to borrowing fees in collateral tokens
|
|
63
54
|
*/
|
|
64
|
-
const fetchTradeBorrowingFeesCollateralV2 = (contract, trader, index, currentPairPrice) =>
|
|
55
|
+
const fetchTradeBorrowingFeesCollateralV2 = async (contract, trader, index, currentPairPrice) => {
|
|
65
56
|
try {
|
|
66
|
-
const feesCollateral =
|
|
57
|
+
const feesCollateral = await contract.getTradeBorrowingFeesCollateral(trader, index, currentPairPrice);
|
|
67
58
|
// Convert BigNumber to normalized float
|
|
68
59
|
// Note: Collateral precision varies by chain, but contract returns proper precision
|
|
69
60
|
return parseFloat(feesCollateral.toString());
|
|
@@ -72,7 +63,7 @@ const fetchTradeBorrowingFeesCollateralV2 = (contract, trader, index, currentPai
|
|
|
72
63
|
console.error("Error fetching trade borrowing fees collateral v2:", error);
|
|
73
64
|
throw error;
|
|
74
65
|
}
|
|
75
|
-
}
|
|
66
|
+
};
|
|
76
67
|
exports.fetchTradeBorrowingFeesCollateralV2 = fetchTradeBorrowingFeesCollateralV2;
|
|
77
68
|
/**
|
|
78
69
|
* @dev Fetches pending accumulated borrowing fees for a specific pair
|
|
@@ -82,9 +73,9 @@ exports.fetchTradeBorrowingFeesCollateralV2 = fetchTradeBorrowingFeesCollateralV
|
|
|
82
73
|
* @param currentPairPrice Current price of the trading pair (1e6 precision)
|
|
83
74
|
* @returns Promise resolving to pending accumulated borrowing fee
|
|
84
75
|
*/
|
|
85
|
-
const fetchPairPendingAccBorrowingFeesV2 = (contract, collateralIndex, pairIndex, currentPairPrice) =>
|
|
76
|
+
const fetchPairPendingAccBorrowingFeesV2 = async (contract, collateralIndex, pairIndex, currentPairPrice) => {
|
|
86
77
|
try {
|
|
87
|
-
const accBorrowingFeeP =
|
|
78
|
+
const accBorrowingFeeP = await contract.getPairPendingAccBorrowingFees(collateralIndex, pairIndex, currentPairPrice);
|
|
88
79
|
// Convert BigNumber to normalized float
|
|
89
80
|
return parseFloat(accBorrowingFeeP.toString()) / 1e20;
|
|
90
81
|
}
|
|
@@ -92,7 +83,7 @@ const fetchPairPendingAccBorrowingFeesV2 = (contract, collateralIndex, pairIndex
|
|
|
92
83
|
console.error("Error fetching pair pending acc borrowing fees v2:", error);
|
|
93
84
|
throw error;
|
|
94
85
|
}
|
|
95
|
-
}
|
|
86
|
+
};
|
|
96
87
|
exports.fetchPairPendingAccBorrowingFeesV2 = fetchPairPendingAccBorrowingFeesV2;
|
|
97
88
|
/**
|
|
98
89
|
* @dev Convenience function to fetch all borrowing v2 data for specific pairs
|
|
@@ -101,11 +92,11 @@ exports.fetchPairPendingAccBorrowingFeesV2 = fetchPairPendingAccBorrowingFeesV2;
|
|
|
101
92
|
* @param pairIndices Array of pair indices
|
|
102
93
|
* @returns Promise resolving to complete borrowing v2 data set
|
|
103
94
|
*/
|
|
104
|
-
const fetchAllBorrowingV2Data = (contract, collateralIndex, pairIndices) =>
|
|
95
|
+
const fetchAllBorrowingV2Data = async (contract, collateralIndex, pairIndices) => {
|
|
105
96
|
const collateralIndices = new Array(pairIndices.length).fill(collateralIndex);
|
|
106
97
|
try {
|
|
107
98
|
// Fetch both parameters and data in parallel
|
|
108
|
-
const [params, data] =
|
|
99
|
+
const [params, data] = await Promise.all([
|
|
109
100
|
(0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
|
|
110
101
|
(0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
|
|
111
102
|
]);
|
|
@@ -117,7 +108,7 @@ const fetchAllBorrowingV2Data = (contract, collateralIndex, pairIndices) => __aw
|
|
|
117
108
|
console.error("Error fetching all borrowing v2 data:", error);
|
|
118
109
|
throw error;
|
|
119
110
|
}
|
|
120
|
-
}
|
|
111
|
+
};
|
|
121
112
|
exports.fetchAllBorrowingV2Data = fetchAllBorrowingV2Data;
|
|
122
113
|
/**
|
|
123
114
|
* @dev Creates a complete borrowing v2 context from contract data
|
|
@@ -127,10 +118,13 @@ exports.fetchAllBorrowingV2Data = fetchAllBorrowingV2Data;
|
|
|
127
118
|
* @param currentTimestamp Optional current timestamp for calculations
|
|
128
119
|
* @returns Promise resolving to complete borrowing v2 context
|
|
129
120
|
*/
|
|
130
|
-
const createBorrowingV2ContextFromContract = (contract, collateralIndex, pairIndices, currentTimestamp) =>
|
|
131
|
-
const { context } =
|
|
132
|
-
return
|
|
133
|
-
|
|
121
|
+
const createBorrowingV2ContextFromContract = async (contract, collateralIndex, pairIndices, currentTimestamp) => {
|
|
122
|
+
const { context } = await (0, exports.fetchAllBorrowingV2Data)(contract, collateralIndex, pairIndices);
|
|
123
|
+
return {
|
|
124
|
+
...context,
|
|
125
|
+
currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
|
|
126
|
+
};
|
|
127
|
+
};
|
|
134
128
|
exports.createBorrowingV2ContextFromContract = createBorrowingV2ContextFromContract;
|
|
135
129
|
/**
|
|
136
130
|
* @dev Helper function to create context from already fetched arrays
|
|
@@ -143,7 +137,7 @@ exports.createBorrowingV2ContextFromContract = createBorrowingV2ContextFromContr
|
|
|
143
137
|
*/
|
|
144
138
|
const createBorrowingV2ContextFromArrays = (collateralIndices, pairIndices, params, data, currentTimestamp) => {
|
|
145
139
|
const context = {
|
|
146
|
-
currentTimestamp: currentTimestamp
|
|
140
|
+
currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
|
|
147
141
|
borrowingParams: {},
|
|
148
142
|
borrowingData: {},
|
|
149
143
|
};
|
|
@@ -164,13 +158,13 @@ exports.createBorrowingV2ContextFromArrays = createBorrowingV2ContextFromArrays;
|
|
|
164
158
|
* @param pairIndices Array of pair indices (must match collateralIndices length)
|
|
165
159
|
* @returns Promise resolving to complete borrowing v2 context
|
|
166
160
|
*/
|
|
167
|
-
const fetchBorrowingV2DataForPairs = (contract, collateralIndices, pairIndices) =>
|
|
161
|
+
const fetchBorrowingV2DataForPairs = async (contract, collateralIndices, pairIndices) => {
|
|
168
162
|
if (collateralIndices.length !== pairIndices.length) {
|
|
169
163
|
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
170
164
|
}
|
|
171
165
|
try {
|
|
172
166
|
// Fetch both parameters and data in parallel
|
|
173
|
-
const [params, data] =
|
|
167
|
+
const [params, data] = await Promise.all([
|
|
174
168
|
(0, exports.fetchBorrowingFeeParamsV2)(contract, collateralIndices, pairIndices),
|
|
175
169
|
(0, exports.fetchPairBorrowingFeeDataV2)(contract, collateralIndices, pairIndices),
|
|
176
170
|
]);
|
|
@@ -181,5 +175,5 @@ const fetchBorrowingV2DataForPairs = (contract, collateralIndices, pairIndices)
|
|
|
181
175
|
console.error("Error fetching borrowing v2 data for pairs:", error);
|
|
182
176
|
throw error;
|
|
183
177
|
}
|
|
184
|
-
}
|
|
178
|
+
};
|
|
185
179
|
exports.fetchBorrowingV2DataForPairs = fetchBorrowingV2DataForPairs;
|
|
@@ -48,7 +48,7 @@ exports.BORROWING_V2_PRECISION = {
|
|
|
48
48
|
* @returns Updated accumulated borrowing fee (1e20 precision)
|
|
49
49
|
*/
|
|
50
50
|
const getPairPendingAccBorrowingFees = (params, data, currentPairPrice, currentTimestamp) => {
|
|
51
|
-
const timestamp = currentTimestamp
|
|
51
|
+
const timestamp = currentTimestamp ?? Math.floor(Date.now() / 1000);
|
|
52
52
|
// Calculate time elapsed since last update
|
|
53
53
|
const timeElapsed = Math.max(0, timestamp - data.lastBorrowingUpdateTs);
|
|
54
54
|
// If no time elapsed, return current accumulated fee
|
|
@@ -74,7 +74,7 @@ const getTradeBorrowingFeesCollateral = (input, context) => {
|
|
|
74
74
|
return 0;
|
|
75
75
|
}
|
|
76
76
|
// Calculate current accumulated borrowing fees
|
|
77
|
-
const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp
|
|
77
|
+
const currentAccBorrowingFeeP = (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp ?? context.currentTimestamp);
|
|
78
78
|
// Calculate borrowing fees for this trade
|
|
79
79
|
// Formula: (positionSizeCollateral * (currentAccFee - initialAccFee)) / openPrice / 100
|
|
80
80
|
const feeDeltaP = currentAccBorrowingFeeP - initialAccBorrowingFeeP;
|
|
@@ -96,7 +96,7 @@ const getPairBorrowingFees = (input, context) => {
|
|
|
96
96
|
if (!params || !data) {
|
|
97
97
|
return 0;
|
|
98
98
|
}
|
|
99
|
-
return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp
|
|
99
|
+
return (0, exports.getPairPendingAccBorrowingFees)(params, data, currentPairPrice, currentTimestamp ?? context.currentTimestamp);
|
|
100
100
|
};
|
|
101
101
|
exports.getPairBorrowingFees = getPairBorrowingFees;
|
|
102
102
|
/**
|
|
@@ -16,14 +16,14 @@ const convertTradeFeesData = (data, collateralConfig) => {
|
|
|
16
16
|
const decimals = collateralConfig.decimals || 18;
|
|
17
17
|
return {
|
|
18
18
|
realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
|
|
19
|
-
|
|
20
|
-
realizedPnlCollateral: parseFloat(data.realizedPnlCollateral.toString()) /
|
|
19
|
+
10 ** decimals,
|
|
20
|
+
realizedPnlCollateral: parseFloat(data.realizedPnlCollateral.toString()) / 10 ** decimals,
|
|
21
21
|
manuallyRealizedNegativePnlCollateral: parseFloat(data.manuallyRealizedNegativePnlCollateral.toString()) /
|
|
22
|
-
|
|
22
|
+
10 ** decimals,
|
|
23
23
|
alreadyTransferredNegativePnlCollateral: parseFloat(data.alreadyTransferredNegativePnlCollateral.toString()) /
|
|
24
|
-
|
|
24
|
+
10 ** decimals,
|
|
25
25
|
virtualAvailableCollateralInDiamond: parseFloat(data.virtualAvailableCollateralInDiamond.toString()) /
|
|
26
|
-
|
|
26
|
+
10 ** decimals,
|
|
27
27
|
initialAccFundingFeeP: parseFloat(data.initialAccFundingFeeP.toString()) /
|
|
28
28
|
converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P,
|
|
29
29
|
initialAccBorrowingFeeP: parseFloat(data.initialAccBorrowingFeeP.toString()) /
|
|
@@ -51,16 +51,16 @@ const convertUiRealizedPnlData = (data, collateralConfig) => {
|
|
|
51
51
|
const decimals = collateralConfig.decimals || 18;
|
|
52
52
|
return {
|
|
53
53
|
realizedTradingFeesCollateral: parseFloat(data.realizedTradingFeesCollateral.toString()) /
|
|
54
|
-
|
|
54
|
+
10 ** decimals,
|
|
55
55
|
realizedOldBorrowingFeesCollateral: parseFloat(data.realizedOldBorrowingFeesCollateral.toString()) /
|
|
56
|
-
|
|
56
|
+
10 ** decimals,
|
|
57
57
|
realizedNewBorrowingFeesCollateral: parseFloat(data.realizedNewBorrowingFeesCollateral.toString()) /
|
|
58
|
-
|
|
58
|
+
10 ** decimals,
|
|
59
59
|
realizedFundingFeesCollateral: parseFloat(data.realizedFundingFeesCollateral.toString()) /
|
|
60
|
-
|
|
60
|
+
10 ** decimals,
|
|
61
61
|
realizedPnlPartialCloseCollateral: parseFloat(data.realizedPnlPartialCloseCollateral.toString()) /
|
|
62
|
-
|
|
63
|
-
pnlWithdrawnCollateral: parseFloat(data.pnlWithdrawnCollateral.toString()) /
|
|
62
|
+
10 ** decimals,
|
|
63
|
+
pnlWithdrawnCollateral: parseFloat(data.pnlWithdrawnCollateral.toString()) / 10 ** decimals,
|
|
64
64
|
};
|
|
65
65
|
};
|
|
66
66
|
exports.convertUiRealizedPnlData = convertUiRealizedPnlData;
|
|
@@ -83,11 +83,11 @@ exports.convertUiRealizedPnlDataArray = convertUiRealizedPnlDataArray;
|
|
|
83
83
|
const encodeTradeFeesData = (data, collateralConfig) => {
|
|
84
84
|
const decimals = collateralConfig.decimals || 18;
|
|
85
85
|
return {
|
|
86
|
-
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral *
|
|
87
|
-
realizedPnlCollateral: Math.round(data.realizedPnlCollateral *
|
|
88
|
-
manuallyRealizedNegativePnlCollateral: Math.round(data.manuallyRealizedNegativePnlCollateral *
|
|
89
|
-
alreadyTransferredNegativePnlCollateral: Math.round(data.alreadyTransferredNegativePnlCollateral *
|
|
90
|
-
virtualAvailableCollateralInDiamond: Math.round(data.virtualAvailableCollateralInDiamond *
|
|
86
|
+
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * 10 ** decimals),
|
|
87
|
+
realizedPnlCollateral: Math.round(data.realizedPnlCollateral * 10 ** decimals),
|
|
88
|
+
manuallyRealizedNegativePnlCollateral: Math.round(data.manuallyRealizedNegativePnlCollateral * 10 ** decimals),
|
|
89
|
+
alreadyTransferredNegativePnlCollateral: Math.round(data.alreadyTransferredNegativePnlCollateral * 10 ** decimals),
|
|
90
|
+
virtualAvailableCollateralInDiamond: Math.round(data.virtualAvailableCollateralInDiamond * 10 ** decimals),
|
|
91
91
|
__placeholder: 0,
|
|
92
92
|
initialAccFundingFeeP: Math.round(data.initialAccFundingFeeP * converter_1.FUNDING_FEES_PRECISION.ACC_FUNDING_FEE_P),
|
|
93
93
|
initialAccBorrowingFeeP: Math.round(data.initialAccBorrowingFeeP * borrowingV2_1.BORROWING_V2_PRECISION.ACC_FEE),
|
|
@@ -103,12 +103,12 @@ exports.encodeTradeFeesData = encodeTradeFeesData;
|
|
|
103
103
|
const encodeUiRealizedPnlData = (data, collateralConfig) => {
|
|
104
104
|
const decimals = collateralConfig.decimals || 18;
|
|
105
105
|
return {
|
|
106
|
-
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral *
|
|
107
|
-
realizedOldBorrowingFeesCollateral: Math.round(data.realizedOldBorrowingFeesCollateral *
|
|
108
|
-
realizedNewBorrowingFeesCollateral: Math.round(data.realizedNewBorrowingFeesCollateral *
|
|
109
|
-
realizedFundingFeesCollateral: Math.round(data.realizedFundingFeesCollateral *
|
|
110
|
-
realizedPnlPartialCloseCollateral: Math.round(data.realizedPnlPartialCloseCollateral *
|
|
111
|
-
pnlWithdrawnCollateral: Math.round(data.pnlWithdrawnCollateral *
|
|
106
|
+
realizedTradingFeesCollateral: Math.round(data.realizedTradingFeesCollateral * 10 ** decimals),
|
|
107
|
+
realizedOldBorrowingFeesCollateral: Math.round(data.realizedOldBorrowingFeesCollateral * 10 ** decimals),
|
|
108
|
+
realizedNewBorrowingFeesCollateral: Math.round(data.realizedNewBorrowingFeesCollateral * 10 ** decimals),
|
|
109
|
+
realizedFundingFeesCollateral: Math.round(data.realizedFundingFeesCollateral * 10 ** decimals),
|
|
110
|
+
realizedPnlPartialCloseCollateral: Math.round(data.realizedPnlPartialCloseCollateral * 10 ** decimals),
|
|
111
|
+
pnlWithdrawnCollateral: Math.round(data.pnlWithdrawnCollateral * 10 ** decimals),
|
|
112
112
|
};
|
|
113
113
|
};
|
|
114
114
|
exports.encodeUiRealizedPnlData = encodeUiRealizedPnlData;
|
|
@@ -9,14 +9,13 @@ const index_1 = require("../../../markets/oi/index");
|
|
|
9
9
|
* @dev Builds funding fees sub-context for a specific pair
|
|
10
10
|
*/
|
|
11
11
|
const buildFundingContext = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
|
|
12
|
-
var _a, _b, _c, _d, _e;
|
|
13
12
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
14
|
-
if (!
|
|
13
|
+
if (!collateral?.pairFundingFees) {
|
|
15
14
|
return undefined;
|
|
16
15
|
}
|
|
17
|
-
const params =
|
|
18
|
-
const data =
|
|
19
|
-
const pairOi =
|
|
16
|
+
const params = collateral.pairFundingFees.params?.[pairIndex];
|
|
17
|
+
const data = collateral.pairFundingFees.data?.[pairIndex];
|
|
18
|
+
const pairOi = collateral.pairOis?.[pairIndex];
|
|
20
19
|
const netExposureToken = (0, index_1.getPairV10OiTokenSkewCollateral)(pairIndex, {
|
|
21
20
|
pairOis: collateral.pairOis,
|
|
22
21
|
});
|
|
@@ -28,8 +27,8 @@ const buildFundingContext = (globalTradingVariables, collateralIndex, pairIndex,
|
|
|
28
27
|
data,
|
|
29
28
|
pairOi: pairOi
|
|
30
29
|
? {
|
|
31
|
-
oiLongToken:
|
|
32
|
-
oiShortToken:
|
|
30
|
+
oiLongToken: pairOi.token?.long || 0,
|
|
31
|
+
oiShortToken: pairOi.token?.short || 0,
|
|
33
32
|
}
|
|
34
33
|
: undefined,
|
|
35
34
|
currentTimestamp,
|
|
@@ -111,7 +111,7 @@ exports.convertTradeInitialAccFundingFees = convertTradeInitialAccFundingFees;
|
|
|
111
111
|
*/
|
|
112
112
|
const createFundingFeeContext = (collateralIndices, pairIndices, params, data, globalParams, currentTimestamp) => {
|
|
113
113
|
const context = {
|
|
114
|
-
currentTimestamp: currentTimestamp
|
|
114
|
+
currentTimestamp: currentTimestamp ?? Math.floor(Date.now() / 1000),
|
|
115
115
|
fundingParams: {},
|
|
116
116
|
fundingData: {},
|
|
117
117
|
globalParams: globalParams ? {} : undefined,
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.collateralToContractFormat = exports.priceToContractFormat = exports.fetchPairPendingAccFundingFeesBatch = exports.fetchTradeFeesDataBatch = exports.fetchTradeFeesData = exports.fetchTradeFundingFeesCollateral = exports.fetchPairPendingAccFundingFees = void 0;
|
|
13
4
|
/**
|
|
@@ -18,9 +9,9 @@ exports.collateralToContractFormat = exports.priceToContractFormat = exports.fet
|
|
|
18
9
|
* @param currentPairPrice Current pair price (1e10)
|
|
19
10
|
* @returns Promise resolving to accumulated funding fees and current rate
|
|
20
11
|
*/
|
|
21
|
-
const fetchPairPendingAccFundingFees = (contract, collateralIndex, pairIndex, currentPairPrice) =>
|
|
12
|
+
const fetchPairPendingAccFundingFees = async (contract, collateralIndex, pairIndex, currentPairPrice) => {
|
|
22
13
|
try {
|
|
23
|
-
const result =
|
|
14
|
+
const result = await contract.getPairPendingAccFundingFees(collateralIndex, pairIndex, currentPairPrice);
|
|
24
15
|
return {
|
|
25
16
|
accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
|
|
26
17
|
accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
|
|
@@ -31,7 +22,7 @@ const fetchPairPendingAccFundingFees = (contract, collateralIndex, pairIndex, cu
|
|
|
31
22
|
console.error("Error fetching pair pending acc funding fees:", error);
|
|
32
23
|
throw error;
|
|
33
24
|
}
|
|
34
|
-
}
|
|
25
|
+
};
|
|
35
26
|
exports.fetchPairPendingAccFundingFees = fetchPairPendingAccFundingFees;
|
|
36
27
|
/**
|
|
37
28
|
* @dev Fetches funding fees for a specific trade in collateral tokens
|
|
@@ -41,9 +32,9 @@ exports.fetchPairPendingAccFundingFees = fetchPairPendingAccFundingFees;
|
|
|
41
32
|
* @param currentPairPrice Current pair price (1e10)
|
|
42
33
|
* @returns Promise resolving to funding fee in collateral tokens
|
|
43
34
|
*/
|
|
44
|
-
const fetchTradeFundingFeesCollateral = (contract, trader, index, currentPairPrice) =>
|
|
35
|
+
const fetchTradeFundingFeesCollateral = async (contract, trader, index, currentPairPrice) => {
|
|
45
36
|
try {
|
|
46
|
-
const fundingFeeCollateral =
|
|
37
|
+
const fundingFeeCollateral = await contract.getTradeFundingFeesCollateral(trader, index, currentPairPrice);
|
|
47
38
|
// Convert from BigNumber to number (collateral precision already applied)
|
|
48
39
|
return Number(fundingFeeCollateral);
|
|
49
40
|
}
|
|
@@ -51,7 +42,7 @@ const fetchTradeFundingFeesCollateral = (contract, trader, index, currentPairPri
|
|
|
51
42
|
console.error("Error fetching trade funding fees:", error);
|
|
52
43
|
throw error;
|
|
53
44
|
}
|
|
54
|
-
}
|
|
45
|
+
};
|
|
55
46
|
exports.fetchTradeFundingFeesCollateral = fetchTradeFundingFeesCollateral;
|
|
56
47
|
/**
|
|
57
48
|
* @dev Fetches trade fees data for a specific trade
|
|
@@ -60,9 +51,9 @@ exports.fetchTradeFundingFeesCollateral = fetchTradeFundingFeesCollateral;
|
|
|
60
51
|
* @param index Trade index
|
|
61
52
|
* @returns Promise resolving to trade fees data
|
|
62
53
|
*/
|
|
63
|
-
const fetchTradeFeesData = (contract, trader, index) =>
|
|
54
|
+
const fetchTradeFeesData = async (contract, trader, index) => {
|
|
64
55
|
try {
|
|
65
|
-
const feesData =
|
|
56
|
+
const feesData = await contract.getTradeFeesData(trader, index);
|
|
66
57
|
return {
|
|
67
58
|
accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
68
59
|
accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
@@ -73,7 +64,7 @@ const fetchTradeFeesData = (contract, trader, index) => __awaiter(void 0, void 0
|
|
|
73
64
|
console.error("Error fetching trade fees data:", error);
|
|
74
65
|
throw error;
|
|
75
66
|
}
|
|
76
|
-
}
|
|
67
|
+
};
|
|
77
68
|
exports.fetchTradeFeesData = fetchTradeFeesData;
|
|
78
69
|
/**
|
|
79
70
|
* @dev Fetches trade fees data for multiple trades
|
|
@@ -82,12 +73,12 @@ exports.fetchTradeFeesData = fetchTradeFeesData;
|
|
|
82
73
|
* @param indices Array of trade indices
|
|
83
74
|
* @returns Promise resolving to array of trade fees data
|
|
84
75
|
*/
|
|
85
|
-
const fetchTradeFeesDataBatch = (contract, traders, indices) =>
|
|
76
|
+
const fetchTradeFeesDataBatch = async (contract, traders, indices) => {
|
|
86
77
|
if (traders.length !== indices.length) {
|
|
87
78
|
throw new Error("Traders and indices arrays must have the same length");
|
|
88
79
|
}
|
|
89
80
|
try {
|
|
90
|
-
const feesDatas =
|
|
81
|
+
const feesDatas = await contract.getTradeFeesDataArray(traders, indices);
|
|
91
82
|
return feesDatas.map(feesData => ({
|
|
92
83
|
accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
93
84
|
accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
@@ -98,7 +89,7 @@ const fetchTradeFeesDataBatch = (contract, traders, indices) => __awaiter(void 0
|
|
|
98
89
|
console.error("Error fetching trade fees data batch:", error);
|
|
99
90
|
throw error;
|
|
100
91
|
}
|
|
101
|
-
}
|
|
92
|
+
};
|
|
102
93
|
exports.fetchTradeFeesDataBatch = fetchTradeFeesDataBatch;
|
|
103
94
|
/**
|
|
104
95
|
* @dev Fetches pending accumulated funding fees for multiple pairs
|
|
@@ -108,7 +99,7 @@ exports.fetchTradeFeesDataBatch = fetchTradeFeesDataBatch;
|
|
|
108
99
|
* @param currentPairPrices Array of current pair prices (1e10)
|
|
109
100
|
* @returns Promise resolving to array of accumulated funding fees
|
|
110
101
|
*/
|
|
111
|
-
const fetchPairPendingAccFundingFeesBatch = (contract, collateralIndices, pairIndices, currentPairPrices) =>
|
|
102
|
+
const fetchPairPendingAccFundingFeesBatch = async (contract, collateralIndices, pairIndices, currentPairPrices) => {
|
|
112
103
|
if (collateralIndices.length !== pairIndices.length ||
|
|
113
104
|
pairIndices.length !== currentPairPrices.length) {
|
|
114
105
|
throw new Error("All input arrays must have the same length");
|
|
@@ -116,7 +107,7 @@ const fetchPairPendingAccFundingFeesBatch = (contract, collateralIndices, pairIn
|
|
|
116
107
|
try {
|
|
117
108
|
// Fetch all in parallel
|
|
118
109
|
const promises = collateralIndices.map((collateralIndex, i) => contract.getPairPendingAccFundingFees(collateralIndex, pairIndices[i], currentPairPrices[i]));
|
|
119
|
-
const results =
|
|
110
|
+
const results = await Promise.all(promises);
|
|
120
111
|
return results.map(result => ({
|
|
121
112
|
accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
|
|
122
113
|
accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
|
|
@@ -127,7 +118,7 @@ const fetchPairPendingAccFundingFeesBatch = (contract, collateralIndices, pairIn
|
|
|
127
118
|
console.error("Error fetching pair pending acc funding fees batch:", error);
|
|
128
119
|
throw error;
|
|
129
120
|
}
|
|
130
|
-
}
|
|
121
|
+
};
|
|
131
122
|
exports.fetchPairPendingAccFundingFeesBatch = fetchPairPendingAccFundingFeesBatch;
|
|
132
123
|
/**
|
|
133
124
|
* @dev Helper to convert price from number to contract format
|
|
@@ -145,6 +136,6 @@ exports.priceToContractFormat = priceToContractFormat;
|
|
|
145
136
|
* @returns Amount in contract format
|
|
146
137
|
*/
|
|
147
138
|
const collateralToContractFormat = (amount, decimals) => {
|
|
148
|
-
return BigInt(Math.round(amount *
|
|
139
|
+
return BigInt(Math.round(amount * 10 ** decimals));
|
|
149
140
|
};
|
|
150
141
|
exports.collateralToContractFormat = collateralToContractFormat;
|
|
@@ -259,10 +259,9 @@ exports.getTradeFundingFeesCollateral = getTradeFundingFeesCollateral;
|
|
|
259
259
|
* @returns Complete funding fee calculation result
|
|
260
260
|
*/
|
|
261
261
|
const getTradeFundingFees = (input, context) => {
|
|
262
|
-
var _a, _b;
|
|
263
262
|
// Get params and data from context
|
|
264
|
-
const params =
|
|
265
|
-
const data =
|
|
263
|
+
const params = context.fundingParams[input.collateralIndex]?.[input.pairIndex];
|
|
264
|
+
const data = context.fundingData[input.collateralIndex]?.[input.pairIndex];
|
|
266
265
|
if (!params || !data) {
|
|
267
266
|
throw new Error(`Missing funding fee data for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
268
267
|
}
|
|
@@ -24,9 +24,8 @@ exports.MAX_FEE_TIERS = 8;
|
|
|
24
24
|
const getCurrentDay = () => Math.floor(Date.now() / 1000 / 60 / 60 / 24);
|
|
25
25
|
exports.getCurrentDay = getCurrentDay;
|
|
26
26
|
const getFeeTiersCount = (feeTiers) => {
|
|
27
|
-
var _a;
|
|
28
27
|
for (let i = exports.MAX_FEE_TIERS; i > 0; --i) {
|
|
29
|
-
if (
|
|
28
|
+
if (feeTiers[i - 1]?.feeMultiplier > 0) {
|
|
30
29
|
return i;
|
|
31
30
|
}
|
|
32
31
|
}
|
|
@@ -35,11 +35,10 @@ const types_1 = require("../../../contracts/types");
|
|
|
35
35
|
* @returns Total fee in collateral tokens
|
|
36
36
|
*/
|
|
37
37
|
const getTotalTradeFeesCollateral = (collateralIndex, trader, pairIndex, positionSizeCollateral, isCounterTrade, context) => {
|
|
38
|
-
var _a;
|
|
39
38
|
const { fee, collateralPriceUsd } = context;
|
|
40
39
|
const { totalPositionSizeFeeP, minPositionSizeUsd } = fee;
|
|
41
40
|
// Get counter trade fee rate multiplier (default 1 = 1x)
|
|
42
|
-
const counterTradeFeeRateMultiplier = isCounterTrade &&
|
|
41
|
+
const counterTradeFeeRateMultiplier = isCounterTrade && context.counterTradeSettings?.[pairIndex]
|
|
43
42
|
? context.counterTradeSettings[pairIndex].feeRateMultiplier
|
|
44
43
|
: 1;
|
|
45
44
|
// Apply counter trade multiplier to position size
|
|
@@ -119,7 +118,10 @@ const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, c
|
|
|
119
118
|
if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
|
|
120
119
|
context.funding &&
|
|
121
120
|
tradeFeesData.initialAccFundingFeeP !== undefined) {
|
|
122
|
-
fundingFeeCollateral = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice,
|
|
121
|
+
fundingFeeCollateral = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, {
|
|
122
|
+
...context.funding,
|
|
123
|
+
currentTimestamp: context.currentTimestamp,
|
|
124
|
+
});
|
|
123
125
|
}
|
|
124
126
|
// Calculate borrowing fees v2 (v10+ only)
|
|
125
127
|
let borrowingFeeCollateral = 0;
|
|
@@ -17,7 +17,6 @@ const builder_4 = require("../fees/trading/builder");
|
|
|
17
17
|
* @returns Complete context ready for getLiquidationPrice
|
|
18
18
|
*/
|
|
19
19
|
const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, additionalParams) => {
|
|
20
|
-
var _a;
|
|
21
20
|
const { trade, tradeInfo } = tradeContainer;
|
|
22
21
|
const collateralIndex = trade.collateralIndex || 1;
|
|
23
22
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
@@ -29,7 +28,7 @@ const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, ad
|
|
|
29
28
|
core: {
|
|
30
29
|
currentBlock: additionalParams.currentBlock,
|
|
31
30
|
currentTimestamp: additionalParams.currentTimestamp,
|
|
32
|
-
collateralPriceUsd:
|
|
31
|
+
collateralPriceUsd: collateral.prices?.collateralPriceUsd || 1,
|
|
33
32
|
contractsVersion: tradeInfo.contractsVersion,
|
|
34
33
|
spreadP: additionalParams.spreadP,
|
|
35
34
|
},
|
|
@@ -37,7 +36,11 @@ const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, ad
|
|
|
37
36
|
borrowingV1: (0, builder_1.buildBorrowingV1Context)(globalTradingVariables, collateralIndex, additionalParams.currentBlock),
|
|
38
37
|
borrowingV2: (0, builder_2.buildBorrowingV2Context)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
39
38
|
funding: (0, builder_3.buildFundingContext)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
40
|
-
trading:
|
|
39
|
+
trading: {
|
|
40
|
+
...(0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier),
|
|
41
|
+
counterTradeSettings: globalTradingVariables.counterTradeSettings,
|
|
42
|
+
userPriceImpact: additionalParams.userPriceImpact,
|
|
43
|
+
},
|
|
41
44
|
// Trade-specific data
|
|
42
45
|
tradeData: {
|
|
43
46
|
tradeInfo,
|
|
@@ -27,7 +27,6 @@ const __1 = require("..");
|
|
|
27
27
|
* @returns Liquidation price
|
|
28
28
|
*/
|
|
29
29
|
const getLiquidationPrice = (trade, context) => {
|
|
30
|
-
var _a, _b;
|
|
31
30
|
// Extract parameters from structured context
|
|
32
31
|
const { currentPairPrice, additionalFeeCollateral = 0, partialCloseMultiplier = 1, beforeOpened = false, isCounterTrade = false, } = context.liquidationSpecific;
|
|
33
32
|
// 1. Calculate closing fees
|
|
@@ -72,9 +71,9 @@ const getLiquidationPrice = (trade, context) => {
|
|
|
72
71
|
trade.leverage;
|
|
73
72
|
// 6. Apply closing spread for v9.2+
|
|
74
73
|
if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
75
|
-
((
|
|
74
|
+
((context.tradeData.liquidationParams?.maxLiqSpreadP !== undefined &&
|
|
76
75
|
context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
|
|
77
|
-
(
|
|
76
|
+
(context.liquidationSpecific.userPriceImpact?.fixedSpreadP !==
|
|
78
77
|
undefined &&
|
|
79
78
|
context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
|
|
80
79
|
const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
|
|
@@ -99,7 +98,6 @@ exports.getLiquidationPrice = getLiquidationPrice;
|
|
|
99
98
|
* @returns New liquidation price after the update
|
|
100
99
|
*/
|
|
101
100
|
const getLiquidationPriceAfterPositionUpdate = (existingTrade, newCollateralAmount, newLeverage, isLeverageUpdate, positionSizeCollateralDelta, pnlToRealizeCollateral, context) => {
|
|
102
|
-
var _a, _b;
|
|
103
101
|
const { currentPairPrice, isCounterTrade = false } = context.liquidationSpecific;
|
|
104
102
|
// 1. Calculate closing fees on the new position size
|
|
105
103
|
const closingFeeCollateral = (0, __1.getTotalTradeFeesCollateral)(existingTrade.collateralIndex, "", // No fee tiers applied for liquidation calculation
|
|
@@ -167,9 +165,9 @@ const getLiquidationPriceAfterPositionUpdate = (existingTrade, newCollateralAmou
|
|
|
167
165
|
newLeverage;
|
|
168
166
|
// 9. Apply closing spread for v9.2+
|
|
169
167
|
if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
170
|
-
((
|
|
168
|
+
((context.tradeData.liquidationParams?.maxLiqSpreadP !== undefined &&
|
|
171
169
|
context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
|
|
172
|
-
(
|
|
170
|
+
(context.liquidationSpecific.userPriceImpact?.fixedSpreadP !==
|
|
173
171
|
undefined &&
|
|
174
172
|
context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
|
|
175
173
|
const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
|
package/lib/trade/oiWindows.js
CHANGED
|
@@ -7,13 +7,12 @@ const getCurrentOiWindowId = (oiWindowSettings) => {
|
|
|
7
7
|
};
|
|
8
8
|
exports.getCurrentOiWindowId = getCurrentOiWindowId;
|
|
9
9
|
const getActiveOi = (currentOiWindowId, windowsCount, oiWindows, buy) => {
|
|
10
|
-
var _a, _b;
|
|
11
10
|
if (oiWindows === undefined || windowsCount === 0)
|
|
12
11
|
return 0;
|
|
13
12
|
let activeOi = 0;
|
|
14
13
|
for (let id = currentOiWindowId - (windowsCount - 1); id <= currentOiWindowId; id++) {
|
|
15
14
|
activeOi +=
|
|
16
|
-
(buy ?
|
|
15
|
+
(buy ? oiWindows?.[id]?.oiLongUsd : oiWindows?.[id]?.oiShortUsd) || 0;
|
|
17
16
|
}
|
|
18
17
|
return activeOi;
|
|
19
18
|
};
|
package/lib/trade/pnl/builder.js
CHANGED
|
@@ -14,7 +14,6 @@ const builder_4 = require("../fees/trading/builder");
|
|
|
14
14
|
* @returns Complete context ready for getComprehensivePnl
|
|
15
15
|
*/
|
|
16
16
|
const buildComprehensivePnlContext = (globalTradingVariables, tradeContainer, additionalParams) => {
|
|
17
|
-
var _a;
|
|
18
17
|
const { trade, tradeInfo } = tradeContainer;
|
|
19
18
|
const collateralIndex = trade.collateralIndex || 1;
|
|
20
19
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
@@ -23,7 +22,7 @@ const buildComprehensivePnlContext = (globalTradingVariables, tradeContainer, ad
|
|
|
23
22
|
core: {
|
|
24
23
|
currentBlock: additionalParams.currentBlock,
|
|
25
24
|
currentTimestamp: additionalParams.currentTimestamp,
|
|
26
|
-
collateralPriceUsd:
|
|
25
|
+
collateralPriceUsd: collateral.prices?.collateralPriceUsd || 1,
|
|
27
26
|
contractsVersion: tradeInfo.contractsVersion,
|
|
28
27
|
},
|
|
29
28
|
// Build sub-contexts using dedicated builders
|
|
@@ -34,7 +34,7 @@ exports.encodePnlPercent = encodePnlPercent;
|
|
|
34
34
|
*/
|
|
35
35
|
const convertCollateralAmount = (amount, collateralDecimals) => {
|
|
36
36
|
const value = typeof amount === "bigint" ? Number(amount) : amount;
|
|
37
|
-
return value /
|
|
37
|
+
return value / 10 ** collateralDecimals;
|
|
38
38
|
};
|
|
39
39
|
exports.convertCollateralAmount = convertCollateralAmount;
|
|
40
40
|
/**
|