@gainsnetwork/sdk 1.0.6-rc4 → 1.1.0-rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/backend/globalTrades/index.js +10 -10
- package/lib/backend/tradingVariables/backend.types.d.ts +11 -4
- package/lib/backend/tradingVariables/converter.d.ts +7 -3
- package/lib/backend/tradingVariables/converter.js +70 -63
- package/lib/backend/tradingVariables/index.js +11 -7
- package/lib/backend/tradingVariables/types.d.ts +4 -2
- package/lib/contracts/addresses.js +1 -4
- package/lib/contracts/index.d.ts +1 -1
- package/lib/contracts/index.js +3 -3
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +627 -305
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +1949 -257
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +0 -7
- package/lib/contracts/types/generated/factories/GToken__factory.js +0 -4
- package/lib/contracts/utils/borrowingFees.js +20 -9
- package/lib/contracts/utils/openTrades.js +20 -11
- package/lib/contracts/utils/pairs.d.ts +13 -2
- package/lib/contracts/utils/pairs.js +89 -21
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/markets/forex.js +1 -1
- package/lib/markets/leverage/builder.js +2 -2
- package/lib/markets/price/index.d.ts +0 -1
- package/lib/markets/price/index.js +0 -1
- package/lib/markets/price/types.d.ts +0 -27
- package/lib/pricing/depthBands/converter.d.ts +65 -0
- package/lib/pricing/depthBands/converter.js +155 -0
- package/lib/pricing/depthBands/decoder.d.ts +32 -0
- package/lib/pricing/depthBands/decoder.js +109 -0
- package/lib/pricing/depthBands/encoder.d.ts +19 -0
- package/lib/pricing/depthBands/encoder.js +105 -0
- package/lib/pricing/depthBands/index.d.ts +8 -0
- package/lib/pricing/depthBands/index.js +26 -0
- package/lib/pricing/depthBands/types.d.ts +49 -0
- package/lib/pricing/depthBands/types.js +10 -0
- package/lib/pricing/depthBands/validator.d.ts +22 -0
- package/lib/pricing/depthBands/validator.js +113 -0
- package/lib/pricing/depthBands.d.ts +39 -0
- package/lib/pricing/depthBands.js +92 -0
- package/lib/pricing/index.d.ts +4 -0
- package/lib/pricing/index.js +20 -0
- package/lib/trade/fees/borrowing/builder.js +3 -2
- package/lib/trade/fees/borrowing/converter.js +1 -5
- package/lib/trade/fees/borrowing/index.js +5 -5
- package/lib/trade/fees/borrowingV2/builder.js +4 -3
- package/lib/trade/fees/borrowingV2/converter.js +1 -1
- package/lib/trade/fees/borrowingV2/fetcher.js +32 -26
- package/lib/trade/fees/borrowingV2/index.js +3 -3
- package/lib/trade/fees/converter.js +22 -22
- package/lib/trade/fees/fundingFees/builder.js +7 -6
- package/lib/trade/fees/fundingFees/converter.js +1 -1
- package/lib/trade/fees/fundingFees/fetcher.js +25 -16
- package/lib/trade/fees/fundingFees/index.js +3 -2
- package/lib/trade/fees/tiers/index.js +2 -1
- package/lib/trade/fees/trading/index.js +3 -5
- package/lib/trade/liquidation/builder.js +3 -6
- package/lib/trade/liquidation/index.js +6 -4
- package/lib/trade/oiWindows.js +2 -1
- package/lib/trade/pnl/builder.js +2 -1
- package/lib/trade/pnl/converter.js +1 -1
- package/lib/trade/pnl/index.js +7 -4
- package/lib/trade/priceImpact/close/builder.js +2 -1
- package/lib/trade/priceImpact/close/index.js +1 -4
- package/lib/trade/priceImpact/cumulVol/builder.js +11 -18
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +63 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +97 -1
- package/lib/trade/priceImpact/cumulVol/index.d.ts +3 -0
- package/lib/trade/priceImpact/cumulVol/index.js +123 -25
- package/lib/trade/priceImpact/cumulVol/types.d.ts +11 -0
- package/lib/trade/priceImpact/cumulVol/types.js +2 -0
- package/lib/trade/priceImpact/open/builder.js +2 -1
- package/lib/trade/priceImpact/open/index.js +1 -4
- package/lib/trade/priceImpact/skew/builder.js +3 -2
- package/lib/trade/priceImpact/skew/converter.js +1 -1
- package/lib/trade/priceImpact/skew/fetcher.js +33 -24
- package/package.json +2 -2
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.collateralToContractFormat = exports.priceToContractFormat = exports.fetchPairPendingAccFundingFeesBatch = exports.fetchTradeFeesDataBatch = exports.fetchTradeFeesData = exports.fetchTradeFundingFeesCollateral = exports.fetchPairPendingAccFundingFees = void 0;
|
|
4
13
|
/**
|
|
@@ -9,9 +18,9 @@ exports.collateralToContractFormat = exports.priceToContractFormat = exports.fet
|
|
|
9
18
|
* @param currentPairPrice Current pair price (1e10)
|
|
10
19
|
* @returns Promise resolving to accumulated funding fees and current rate
|
|
11
20
|
*/
|
|
12
|
-
const fetchPairPendingAccFundingFees =
|
|
21
|
+
const fetchPairPendingAccFundingFees = (contract, collateralIndex, pairIndex, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
22
|
try {
|
|
14
|
-
const result =
|
|
23
|
+
const result = yield contract.getPairPendingAccFundingFees(collateralIndex, pairIndex, currentPairPrice);
|
|
15
24
|
return {
|
|
16
25
|
accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
|
|
17
26
|
accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
|
|
@@ -22,7 +31,7 @@ const fetchPairPendingAccFundingFees = async (contract, collateralIndex, pairInd
|
|
|
22
31
|
console.error("Error fetching pair pending acc funding fees:", error);
|
|
23
32
|
throw error;
|
|
24
33
|
}
|
|
25
|
-
};
|
|
34
|
+
});
|
|
26
35
|
exports.fetchPairPendingAccFundingFees = fetchPairPendingAccFundingFees;
|
|
27
36
|
/**
|
|
28
37
|
* @dev Fetches funding fees for a specific trade in collateral tokens
|
|
@@ -32,9 +41,9 @@ exports.fetchPairPendingAccFundingFees = fetchPairPendingAccFundingFees;
|
|
|
32
41
|
* @param currentPairPrice Current pair price (1e10)
|
|
33
42
|
* @returns Promise resolving to funding fee in collateral tokens
|
|
34
43
|
*/
|
|
35
|
-
const fetchTradeFundingFeesCollateral =
|
|
44
|
+
const fetchTradeFundingFeesCollateral = (contract, trader, index, currentPairPrice) => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
45
|
try {
|
|
37
|
-
const fundingFeeCollateral =
|
|
46
|
+
const fundingFeeCollateral = yield contract.getTradeFundingFeesCollateral(trader, index, currentPairPrice);
|
|
38
47
|
// Convert from BigNumber to number (collateral precision already applied)
|
|
39
48
|
return Number(fundingFeeCollateral);
|
|
40
49
|
}
|
|
@@ -42,7 +51,7 @@ const fetchTradeFundingFeesCollateral = async (contract, trader, index, currentP
|
|
|
42
51
|
console.error("Error fetching trade funding fees:", error);
|
|
43
52
|
throw error;
|
|
44
53
|
}
|
|
45
|
-
};
|
|
54
|
+
});
|
|
46
55
|
exports.fetchTradeFundingFeesCollateral = fetchTradeFundingFeesCollateral;
|
|
47
56
|
/**
|
|
48
57
|
* @dev Fetches trade fees data for a specific trade
|
|
@@ -51,9 +60,9 @@ exports.fetchTradeFundingFeesCollateral = fetchTradeFundingFeesCollateral;
|
|
|
51
60
|
* @param index Trade index
|
|
52
61
|
* @returns Promise resolving to trade fees data
|
|
53
62
|
*/
|
|
54
|
-
const fetchTradeFeesData =
|
|
63
|
+
const fetchTradeFeesData = (contract, trader, index) => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
64
|
try {
|
|
56
|
-
const feesData =
|
|
65
|
+
const feesData = yield contract.getTradeFeesData(trader, index);
|
|
57
66
|
return {
|
|
58
67
|
accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
59
68
|
accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
@@ -64,7 +73,7 @@ const fetchTradeFeesData = async (contract, trader, index) => {
|
|
|
64
73
|
console.error("Error fetching trade fees data:", error);
|
|
65
74
|
throw error;
|
|
66
75
|
}
|
|
67
|
-
};
|
|
76
|
+
});
|
|
68
77
|
exports.fetchTradeFeesData = fetchTradeFeesData;
|
|
69
78
|
/**
|
|
70
79
|
* @dev Fetches trade fees data for multiple trades
|
|
@@ -73,12 +82,12 @@ exports.fetchTradeFeesData = fetchTradeFeesData;
|
|
|
73
82
|
* @param indices Array of trade indices
|
|
74
83
|
* @returns Promise resolving to array of trade fees data
|
|
75
84
|
*/
|
|
76
|
-
const fetchTradeFeesDataBatch =
|
|
85
|
+
const fetchTradeFeesDataBatch = (contract, traders, indices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
86
|
if (traders.length !== indices.length) {
|
|
78
87
|
throw new Error("Traders and indices arrays must have the same length");
|
|
79
88
|
}
|
|
80
89
|
try {
|
|
81
|
-
const feesDatas =
|
|
90
|
+
const feesDatas = yield contract.getTradeFeesDataArray(traders, indices);
|
|
82
91
|
return feesDatas.map(feesData => ({
|
|
83
92
|
accPerOiLong: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
84
93
|
accPerOiShort: Number(feesData.initialAccFundingFeeP) / 1e20,
|
|
@@ -89,7 +98,7 @@ const fetchTradeFeesDataBatch = async (contract, traders, indices) => {
|
|
|
89
98
|
console.error("Error fetching trade fees data batch:", error);
|
|
90
99
|
throw error;
|
|
91
100
|
}
|
|
92
|
-
};
|
|
101
|
+
});
|
|
93
102
|
exports.fetchTradeFeesDataBatch = fetchTradeFeesDataBatch;
|
|
94
103
|
/**
|
|
95
104
|
* @dev Fetches pending accumulated funding fees for multiple pairs
|
|
@@ -99,7 +108,7 @@ exports.fetchTradeFeesDataBatch = fetchTradeFeesDataBatch;
|
|
|
99
108
|
* @param currentPairPrices Array of current pair prices (1e10)
|
|
100
109
|
* @returns Promise resolving to array of accumulated funding fees
|
|
101
110
|
*/
|
|
102
|
-
const fetchPairPendingAccFundingFeesBatch =
|
|
111
|
+
const fetchPairPendingAccFundingFeesBatch = (contract, collateralIndices, pairIndices, currentPairPrices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
103
112
|
if (collateralIndices.length !== pairIndices.length ||
|
|
104
113
|
pairIndices.length !== currentPairPrices.length) {
|
|
105
114
|
throw new Error("All input arrays must have the same length");
|
|
@@ -107,7 +116,7 @@ const fetchPairPendingAccFundingFeesBatch = async (contract, collateralIndices,
|
|
|
107
116
|
try {
|
|
108
117
|
// Fetch all in parallel
|
|
109
118
|
const promises = collateralIndices.map((collateralIndex, i) => contract.getPairPendingAccFundingFees(collateralIndex, pairIndices[i], currentPairPrices[i]));
|
|
110
|
-
const results =
|
|
119
|
+
const results = yield Promise.all(promises);
|
|
111
120
|
return results.map(result => ({
|
|
112
121
|
accFundingFeeLongP: Number(result.accFundingFeeLongP) / 1e20,
|
|
113
122
|
accFundingFeeShortP: Number(result.accFundingFeeShortP) / 1e20,
|
|
@@ -118,7 +127,7 @@ const fetchPairPendingAccFundingFeesBatch = async (contract, collateralIndices,
|
|
|
118
127
|
console.error("Error fetching pair pending acc funding fees batch:", error);
|
|
119
128
|
throw error;
|
|
120
129
|
}
|
|
121
|
-
};
|
|
130
|
+
});
|
|
122
131
|
exports.fetchPairPendingAccFundingFeesBatch = fetchPairPendingAccFundingFeesBatch;
|
|
123
132
|
/**
|
|
124
133
|
* @dev Helper to convert price from number to contract format
|
|
@@ -136,6 +145,6 @@ exports.priceToContractFormat = priceToContractFormat;
|
|
|
136
145
|
* @returns Amount in contract format
|
|
137
146
|
*/
|
|
138
147
|
const collateralToContractFormat = (amount, decimals) => {
|
|
139
|
-
return BigInt(Math.round(amount * 10
|
|
148
|
+
return BigInt(Math.round(amount * Math.pow(10, decimals)));
|
|
140
149
|
};
|
|
141
150
|
exports.collateralToContractFormat = collateralToContractFormat;
|
|
@@ -259,9 +259,10 @@ exports.getTradeFundingFeesCollateral = getTradeFundingFeesCollateral;
|
|
|
259
259
|
* @returns Complete funding fee calculation result
|
|
260
260
|
*/
|
|
261
261
|
const getTradeFundingFees = (input, context) => {
|
|
262
|
+
var _a, _b;
|
|
262
263
|
// Get params and data from context
|
|
263
|
-
const params = context.fundingParams[input.collateralIndex]
|
|
264
|
-
const data = context.fundingData[input.collateralIndex]
|
|
264
|
+
const params = (_a = context.fundingParams[input.collateralIndex]) === null || _a === void 0 ? void 0 : _a[input.pairIndex];
|
|
265
|
+
const data = (_b = context.fundingData[input.collateralIndex]) === null || _b === void 0 ? void 0 : _b[input.pairIndex];
|
|
265
266
|
if (!params || !data) {
|
|
266
267
|
throw new Error(`Missing funding fee data for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
267
268
|
}
|
|
@@ -24,8 +24,9 @@ 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;
|
|
27
28
|
for (let i = exports.MAX_FEE_TIERS; i > 0; --i) {
|
|
28
|
-
if (feeTiers[i - 1]
|
|
29
|
+
if (((_a = feeTiers[i - 1]) === null || _a === void 0 ? void 0 : _a.feeMultiplier) > 0) {
|
|
29
30
|
return i;
|
|
30
31
|
}
|
|
31
32
|
}
|
|
@@ -35,10 +35,11 @@ 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;
|
|
38
39
|
const { fee, collateralPriceUsd } = context;
|
|
39
40
|
const { totalPositionSizeFeeP, minPositionSizeUsd } = fee;
|
|
40
41
|
// Get counter trade fee rate multiplier (default 1 = 1x)
|
|
41
|
-
const counterTradeFeeRateMultiplier = isCounterTrade && context.counterTradeSettings
|
|
42
|
+
const counterTradeFeeRateMultiplier = isCounterTrade && ((_a = context.counterTradeSettings) === null || _a === void 0 ? void 0 : _a[pairIndex])
|
|
42
43
|
? context.counterTradeSettings[pairIndex].feeRateMultiplier
|
|
43
44
|
: 1;
|
|
44
45
|
// Apply counter trade multiplier to position size
|
|
@@ -118,10 +119,7 @@ const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, c
|
|
|
118
119
|
if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
|
|
119
120
|
context.funding &&
|
|
120
121
|
tradeFeesData.initialAccFundingFeeP !== undefined) {
|
|
121
|
-
fundingFeeCollateral = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, {
|
|
122
|
-
...context.funding,
|
|
123
|
-
currentTimestamp: context.currentTimestamp,
|
|
124
|
-
});
|
|
122
|
+
fundingFeeCollateral = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, Object.assign(Object.assign({}, context.funding), { currentTimestamp: context.currentTimestamp }));
|
|
125
123
|
}
|
|
126
124
|
// Calculate borrowing fees v2 (v10+ only)
|
|
127
125
|
let borrowingFeeCollateral = 0;
|
|
@@ -17,6 +17,7 @@ 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;
|
|
20
21
|
const { trade, tradeInfo } = tradeContainer;
|
|
21
22
|
const collateralIndex = trade.collateralIndex || 1;
|
|
22
23
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
@@ -28,7 +29,7 @@ const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, ad
|
|
|
28
29
|
core: {
|
|
29
30
|
currentBlock: additionalParams.currentBlock,
|
|
30
31
|
currentTimestamp: additionalParams.currentTimestamp,
|
|
31
|
-
collateralPriceUsd: collateral.prices
|
|
32
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
32
33
|
contractsVersion: tradeInfo.contractsVersion,
|
|
33
34
|
spreadP: additionalParams.spreadP,
|
|
34
35
|
},
|
|
@@ -36,11 +37,7 @@ const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, ad
|
|
|
36
37
|
borrowingV1: (0, builder_1.buildBorrowingV1Context)(globalTradingVariables, collateralIndex, additionalParams.currentBlock),
|
|
37
38
|
borrowingV2: (0, builder_2.buildBorrowingV2Context)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
38
39
|
funding: (0, builder_3.buildFundingContext)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
39
|
-
trading: {
|
|
40
|
-
...(0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier),
|
|
41
|
-
counterTradeSettings: globalTradingVariables.counterTradeSettings,
|
|
42
|
-
userPriceImpact: additionalParams.userPriceImpact,
|
|
43
|
-
},
|
|
40
|
+
trading: Object.assign(Object.assign({}, (0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier)), { counterTradeSettings: globalTradingVariables.counterTradeSettings, userPriceImpact: additionalParams.userPriceImpact }),
|
|
44
41
|
// Trade-specific data
|
|
45
42
|
tradeData: {
|
|
46
43
|
tradeInfo,
|
|
@@ -27,6 +27,7 @@ const __1 = require("..");
|
|
|
27
27
|
* @returns Liquidation price
|
|
28
28
|
*/
|
|
29
29
|
const getLiquidationPrice = (trade, context) => {
|
|
30
|
+
var _a, _b;
|
|
30
31
|
// Extract parameters from structured context
|
|
31
32
|
const { currentPairPrice, additionalFeeCollateral = 0, partialCloseMultiplier = 1, beforeOpened = false, isCounterTrade = false, } = context.liquidationSpecific;
|
|
32
33
|
// 1. Calculate closing fees
|
|
@@ -71,9 +72,9 @@ const getLiquidationPrice = (trade, context) => {
|
|
|
71
72
|
trade.leverage;
|
|
72
73
|
// 6. Apply closing spread for v9.2+
|
|
73
74
|
if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
74
|
-
((context.tradeData.liquidationParams
|
|
75
|
+
((((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
75
76
|
context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
|
|
76
|
-
(context.liquidationSpecific.userPriceImpact
|
|
77
|
+
(((_b = context.liquidationSpecific.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !==
|
|
77
78
|
undefined &&
|
|
78
79
|
context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
|
|
79
80
|
const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
|
|
@@ -98,6 +99,7 @@ exports.getLiquidationPrice = getLiquidationPrice;
|
|
|
98
99
|
* @returns New liquidation price after the update
|
|
99
100
|
*/
|
|
100
101
|
const getLiquidationPriceAfterPositionUpdate = (existingTrade, newCollateralAmount, newLeverage, isLeverageUpdate, positionSizeCollateralDelta, pnlToRealizeCollateral, context) => {
|
|
102
|
+
var _a, _b;
|
|
101
103
|
const { currentPairPrice, isCounterTrade = false } = context.liquidationSpecific;
|
|
102
104
|
// 1. Calculate closing fees on the new position size
|
|
103
105
|
const closingFeeCollateral = (0, __1.getTotalTradeFeesCollateral)(existingTrade.collateralIndex, "", // No fee tiers applied for liquidation calculation
|
|
@@ -165,9 +167,9 @@ const getLiquidationPriceAfterPositionUpdate = (existingTrade, newCollateralAmou
|
|
|
165
167
|
newLeverage;
|
|
166
168
|
// 9. Apply closing spread for v9.2+
|
|
167
169
|
if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
168
|
-
((context.tradeData.liquidationParams
|
|
170
|
+
((((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
169
171
|
context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
|
|
170
|
-
(context.liquidationSpecific.userPriceImpact
|
|
172
|
+
(((_b = context.liquidationSpecific.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !==
|
|
171
173
|
undefined &&
|
|
172
174
|
context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
|
|
173
175
|
const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
|
package/lib/trade/oiWindows.js
CHANGED
|
@@ -7,12 +7,13 @@ const getCurrentOiWindowId = (oiWindowSettings) => {
|
|
|
7
7
|
};
|
|
8
8
|
exports.getCurrentOiWindowId = getCurrentOiWindowId;
|
|
9
9
|
const getActiveOi = (currentOiWindowId, windowsCount, oiWindows, buy) => {
|
|
10
|
+
var _a, _b;
|
|
10
11
|
if (oiWindows === undefined || windowsCount === 0)
|
|
11
12
|
return 0;
|
|
12
13
|
let activeOi = 0;
|
|
13
14
|
for (let id = currentOiWindowId - (windowsCount - 1); id <= currentOiWindowId; id++) {
|
|
14
15
|
activeOi +=
|
|
15
|
-
(buy ? oiWindows
|
|
16
|
+
(buy ? (_a = oiWindows === null || oiWindows === void 0 ? void 0 : oiWindows[id]) === null || _a === void 0 ? void 0 : _a.oiLongUsd : (_b = oiWindows === null || oiWindows === void 0 ? void 0 : oiWindows[id]) === null || _b === void 0 ? void 0 : _b.oiShortUsd) || 0;
|
|
16
17
|
}
|
|
17
18
|
return activeOi;
|
|
18
19
|
};
|
package/lib/trade/pnl/builder.js
CHANGED
|
@@ -14,6 +14,7 @@ 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;
|
|
17
18
|
const { trade, tradeInfo } = tradeContainer;
|
|
18
19
|
const collateralIndex = trade.collateralIndex || 1;
|
|
19
20
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
@@ -22,7 +23,7 @@ const buildComprehensivePnlContext = (globalTradingVariables, tradeContainer, ad
|
|
|
22
23
|
core: {
|
|
23
24
|
currentBlock: additionalParams.currentBlock,
|
|
24
25
|
currentTimestamp: additionalParams.currentTimestamp,
|
|
25
|
-
collateralPriceUsd: collateral.prices
|
|
26
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
26
27
|
contractsVersion: tradeInfo.contractsVersion,
|
|
27
28
|
},
|
|
28
29
|
// 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 / 10
|
|
37
|
+
return value / Math.pow(10, collateralDecimals);
|
|
38
38
|
};
|
|
39
39
|
exports.convertCollateralAmount = convertCollateralAmount;
|
|
40
40
|
/**
|
package/lib/trade/pnl/index.js
CHANGED
|
@@ -81,6 +81,7 @@ exports.getTradeValue = getTradeValue;
|
|
|
81
81
|
* @returns Detailed PnL breakdown
|
|
82
82
|
*/
|
|
83
83
|
const getComprehensivePnl = (trade, marketPrice, executionPrice, tradeInfo, context) => {
|
|
84
|
+
var _a;
|
|
84
85
|
// Calculate both raw PnL (market price) and impact-adjusted PnL (execution price)
|
|
85
86
|
let rawPnlPercent = (0, exports.getPnlPercent)(trade.openPrice, marketPrice, trade.long, trade.leverage);
|
|
86
87
|
let impactPnlPercent = (0, exports.getPnlPercent)(trade.openPrice, executionPrice, trade.long, trade.leverage);
|
|
@@ -113,7 +114,7 @@ const getComprehensivePnl = (trade, marketPrice, executionPrice, tradeInfo, cont
|
|
|
113
114
|
const totalHoldingFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee;
|
|
114
115
|
const totalFees = totalHoldingFees + closingFee;
|
|
115
116
|
// Check liquidation (using raw PnL for liquidation check)
|
|
116
|
-
const liquidationThreshold = context.tradeData
|
|
117
|
+
const liquidationThreshold = ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.liquidationParams)
|
|
117
118
|
? (0, liquidation_1.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage) *
|
|
118
119
|
-100
|
|
119
120
|
: -90; // Default 90% loss
|
|
@@ -190,6 +191,7 @@ exports.getComprehensivePnl = getComprehensivePnl;
|
|
|
190
191
|
* @returns [pnlCollateral, pnlPercentage] or undefined if no price
|
|
191
192
|
*/
|
|
192
193
|
const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
|
|
194
|
+
var _a;
|
|
193
195
|
if (!price) {
|
|
194
196
|
return;
|
|
195
197
|
}
|
|
@@ -221,7 +223,7 @@ const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, use
|
|
|
221
223
|
// Calculate closing fee using the same function as opening fees
|
|
222
224
|
const positionSizeCollateral = posCollat * trade.leverage;
|
|
223
225
|
const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(0, // collateralIndex not used
|
|
224
|
-
trade.user, trade.pairIndex, positionSizeCollateral, trade.isCounterTrade
|
|
226
|
+
trade.user, trade.pairIndex, positionSizeCollateral, (_a = trade.isCounterTrade) !== null && _a !== void 0 ? _a : false, {
|
|
225
227
|
fee: context.fee,
|
|
226
228
|
globalTradeFeeParams: context.globalTradeFeeParams,
|
|
227
229
|
collateralPriceUsd: context.collateralPriceUsd || 1,
|
|
@@ -245,11 +247,12 @@ exports.getPnl = getPnl;
|
|
|
245
247
|
* @returns The price that would result in the target PnL percentage
|
|
246
248
|
*/
|
|
247
249
|
const getPriceForTargetPnlPercentage = (targetPnlPercent, trade, tradeInfo, context, netPnl = false) => {
|
|
250
|
+
var _a, _b;
|
|
248
251
|
const { leverage, openPrice, long, collateralAmount } = trade;
|
|
249
252
|
const positionSizeCollateral = collateralAmount * leverage;
|
|
250
253
|
// Calculate holding fees - always use getTradePendingHoldingFeesCollateral
|
|
251
254
|
// This mirrors the contract's getTradeValueCollateral which always calls this function
|
|
252
|
-
const fees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeData
|
|
255
|
+
const fees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.tradeFeesData) || {
|
|
253
256
|
realizedTradingFeesCollateral: 0,
|
|
254
257
|
realizedPnlCollateral: 0,
|
|
255
258
|
manuallyRealizedNegativePnlCollateral: 0,
|
|
@@ -265,7 +268,7 @@ const getPriceForTargetPnlPercentage = (targetPnlPercent, trade, tradeInfo, cont
|
|
|
265
268
|
borrowingV1: context.borrowingV1,
|
|
266
269
|
borrowingV2: context.borrowingV2,
|
|
267
270
|
funding: context.funding,
|
|
268
|
-
initialAccFees: context.tradeData
|
|
271
|
+
initialAccFees: (_b = context.tradeData) === null || _b === void 0 ? void 0 : _b.initialAccFees,
|
|
269
272
|
});
|
|
270
273
|
const totalHoldingFees = fees.fundingFeeCollateral +
|
|
271
274
|
fees.borrowingFeeCollateral +
|
|
@@ -14,6 +14,7 @@ const builder_2 = require("../skew/builder");
|
|
|
14
14
|
* @returns Complete context ready for getTradeClosingPriceImpact
|
|
15
15
|
*/
|
|
16
16
|
const buildTradeClosingPriceImpactContext = (globalTradingVariables, collateralIndex, pairIndex, tradeInfo, additionalParams) => {
|
|
17
|
+
var _a;
|
|
17
18
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
18
19
|
if (!collateral) {
|
|
19
20
|
return undefined;
|
|
@@ -35,7 +36,7 @@ const buildTradeClosingPriceImpactContext = (globalTradingVariables, collateralI
|
|
|
35
36
|
}
|
|
36
37
|
// Return structured context with proper subcontexts
|
|
37
38
|
return {
|
|
38
|
-
collateralPriceUsd: collateral.prices
|
|
39
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
39
40
|
cumulVolContext,
|
|
40
41
|
skewContext,
|
|
41
42
|
tradeInfo,
|
|
@@ -129,9 +129,6 @@ exports.getTradeClosingPriceImpact = getTradeClosingPriceImpact;
|
|
|
129
129
|
* @returns Price impact breakdown and trade value
|
|
130
130
|
*/
|
|
131
131
|
const getTradeClosingPriceImpactAtOracle = (input, context) => {
|
|
132
|
-
return (0, exports.getTradeClosingPriceImpact)({
|
|
133
|
-
...input,
|
|
134
|
-
currentPairPrice: input.oraclePrice,
|
|
135
|
-
}, context);
|
|
132
|
+
return (0, exports.getTradeClosingPriceImpact)(Object.assign(Object.assign({}, input), { currentPairPrice: input.oraclePrice }), context);
|
|
136
133
|
};
|
|
137
134
|
exports.getTradeClosingPriceImpactAtOracle = getTradeClosingPriceImpactAtOracle;
|
|
@@ -10,14 +10,16 @@ exports.buildCumulVolContext = void 0;
|
|
|
10
10
|
* @returns Cumulative volume context ready for getTradeCumulVolPriceImpactP
|
|
11
11
|
*/
|
|
12
12
|
const buildCumulVolContext = (globalTradingVariables, collateralIndex, pairIndex, additionalParams) => {
|
|
13
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
13
14
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
14
15
|
if (!collateral) {
|
|
15
16
|
return undefined;
|
|
16
17
|
}
|
|
17
18
|
// Get pair-specific data from global variables
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
const
|
|
19
|
+
// TODO: Update to use pairDepthBands for v10.2
|
|
20
|
+
const pairDepth = undefined; // globalTradingVariables.pairDepths?.[pairIndex];
|
|
21
|
+
const pairFactor = (_a = globalTradingVariables.pairFactors) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
22
|
+
const oiWindows = (_b = globalTradingVariables.oiWindows) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
21
23
|
// Get OI windows settings (same for all pairs)
|
|
22
24
|
// OI windows settings from global trading variables are already in SDK format
|
|
23
25
|
const oiWindowsSettings = globalTradingVariables.oiWindowsSettings;
|
|
@@ -25,27 +27,18 @@ const buildCumulVolContext = (globalTradingVariables, collateralIndex, pairIndex
|
|
|
25
27
|
const userPriceImpact = additionalParams.userPriceImpact;
|
|
26
28
|
const protectionCloseFactorWhitelist = additionalParams.protectionCloseFactorWhitelist;
|
|
27
29
|
// Get liquidation params - check both pair and group level
|
|
28
|
-
const liquidationParams = globalTradingVariables.liquidationParams
|
|
29
|
-
globalTradingVariables.liquidationParams
|
|
30
|
-
return {
|
|
30
|
+
const liquidationParams = ((_d = (_c = globalTradingVariables.liquidationParams) === null || _c === void 0 ? void 0 : _c.pairs) === null || _d === void 0 ? void 0 : _d[pairIndex]) ||
|
|
31
|
+
((_f = (_e = globalTradingVariables.liquidationParams) === null || _e === void 0 ? void 0 : _e.groups) === null || _f === void 0 ? void 0 : _f[0]); // fallback to first group
|
|
32
|
+
return Object.assign({
|
|
31
33
|
// Trade state
|
|
32
|
-
isOpen: additionalParams.isOpen,
|
|
33
|
-
isPnlPositive: additionalParams.isPnlPositive,
|
|
34
|
-
createdBlock: additionalParams.createdBlock,
|
|
34
|
+
isOpen: additionalParams.isOpen, isPnlPositive: additionalParams.isPnlPositive, createdBlock: additionalParams.createdBlock,
|
|
35
35
|
// Protection factors
|
|
36
|
-
liquidationParams,
|
|
37
|
-
currentBlock: additionalParams.currentBlock,
|
|
38
|
-
contractsVersion: additionalParams.contractsVersion,
|
|
39
|
-
protectionCloseFactorWhitelist,
|
|
36
|
+
liquidationParams, currentBlock: additionalParams.currentBlock, contractsVersion: additionalParams.contractsVersion, protectionCloseFactorWhitelist,
|
|
40
37
|
// Price impact data
|
|
41
38
|
pairDepth,
|
|
42
39
|
oiWindowsSettings,
|
|
43
40
|
oiWindows,
|
|
44
41
|
// User/collateral specific
|
|
45
|
-
userPriceImpact,
|
|
46
|
-
collateralPriceUsd: collateral.prices?.collateralPriceUsd || 1,
|
|
47
|
-
// Pair factors (spread across the context)
|
|
48
|
-
...pairFactor,
|
|
49
|
-
};
|
|
42
|
+
userPriceImpact, collateralPriceUsd: ((_g = collateral.prices) === null || _g === void 0 ? void 0 : _g.collateralPriceUsd) || 1 }, pairFactor);
|
|
50
43
|
};
|
|
51
44
|
exports.buildCumulVolContext = buildCumulVolContext;
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { IPriceImpact } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
6
6
|
import { OiWindowsSettings, OiWindow, OiWindows } from "../../types";
|
|
7
|
+
import { DepthBands, PairDepthBands, DepthBandsMapping } from "./types";
|
|
7
8
|
/**
|
|
8
9
|
* @dev Converts contract OI windows settings to SDK format
|
|
9
10
|
* @param contractData Contract OiWindowsSettings struct
|
|
@@ -29,3 +30,65 @@ export declare const convertOiWindows: (windowIds: string[], contractWindows: IP
|
|
|
29
30
|
* @returns Array of normalized OI windows settings
|
|
30
31
|
*/
|
|
31
32
|
export declare const convertOiWindowsSettingsArray: (contractDataArray: IPriceImpact.OiWindowsSettingsStructOutput[]) => OiWindowsSettings[];
|
|
33
|
+
/**
|
|
34
|
+
* @dev Converts decoded depth bands from contract to SDK format
|
|
35
|
+
* @param totalDepthUsd Total depth in USD (already decoded from contract)
|
|
36
|
+
* @param bandsBps Array of 30 band percentages in basis points from contract
|
|
37
|
+
* @returns Normalized depth bands with bands in 0-1 range
|
|
38
|
+
*/
|
|
39
|
+
export declare const convertDepthBands: (totalDepthUsd: number, bandsBps: number[]) => DepthBands;
|
|
40
|
+
/**
|
|
41
|
+
* @dev Converts decoded pair depth bands from contract to SDK format
|
|
42
|
+
* @param aboveDepth Decoded above depth bands from getPairDepthBandsDecoded
|
|
43
|
+
* @param belowDepth Decoded below depth bands from getPairDepthBandsDecoded
|
|
44
|
+
* @returns Normalized pair depth bands with above/below
|
|
45
|
+
*/
|
|
46
|
+
export declare const convertPairDepthBands: (aboveDepth: {
|
|
47
|
+
totalDepthUsd: number;
|
|
48
|
+
bands: number[];
|
|
49
|
+
} | undefined, belowDepth: {
|
|
50
|
+
totalDepthUsd: number;
|
|
51
|
+
bands: number[];
|
|
52
|
+
} | undefined) => PairDepthBands;
|
|
53
|
+
/**
|
|
54
|
+
* @dev Converts decoded depth bands mapping from contract to SDK format
|
|
55
|
+
* @param bandsBps Array of 30 band offset values in basis points from getDepthBandsMappingDecoded
|
|
56
|
+
* @returns Normalized depth bands mapping with offset values in 0-1 range
|
|
57
|
+
*/
|
|
58
|
+
export declare const convertDepthBandsMapping: (bandsBps: number[]) => DepthBandsMapping;
|
|
59
|
+
/**
|
|
60
|
+
* @dev Validates depth bands have correct number of bands
|
|
61
|
+
* @param depthBands Depth bands to validate
|
|
62
|
+
* @returns True if valid (30 bands)
|
|
63
|
+
*/
|
|
64
|
+
export declare const validateDepthBands: (depthBands: DepthBands) => boolean;
|
|
65
|
+
/**
|
|
66
|
+
* @dev Validates depth bands mapping has correct number of bands
|
|
67
|
+
* @param mapping Depth bands mapping to validate
|
|
68
|
+
* @returns True if valid (30 bands)
|
|
69
|
+
*/
|
|
70
|
+
export declare const validateDepthBandsMapping: (mapping: DepthBandsMapping) => boolean;
|
|
71
|
+
/**
|
|
72
|
+
* @dev Alternative converter for decoded pair depth bands from contract
|
|
73
|
+
* @param contractData Decoded pair depth bands from getPairDepthBandsDecoded
|
|
74
|
+
* @returns Normalized pair depth bands
|
|
75
|
+
*/
|
|
76
|
+
export declare const convertPairDepthBandsDecoded: (contractData: {
|
|
77
|
+
above: {
|
|
78
|
+
totalDepthUsd: number;
|
|
79
|
+
bands: number[];
|
|
80
|
+
};
|
|
81
|
+
below: {
|
|
82
|
+
totalDepthUsd: number;
|
|
83
|
+
bands: number[];
|
|
84
|
+
};
|
|
85
|
+
}) => PairDepthBands;
|
|
86
|
+
/**
|
|
87
|
+
* @dev Alternative converter for raw slot-based pair depth bands (if needed for legacy)
|
|
88
|
+
* @param aboveSlot1 First slot for above bands
|
|
89
|
+
* @param aboveSlot2 Second slot for above bands
|
|
90
|
+
* @param belowSlot1 First slot for below bands
|
|
91
|
+
* @param belowSlot2 Second slot for below bands
|
|
92
|
+
* @returns Normalized pair depth bands
|
|
93
|
+
*/
|
|
94
|
+
export declare const convertPairDepthBandsFromSlots: (aboveSlot1: bigint, aboveSlot2: bigint, belowSlot1: bigint, belowSlot2: bigint) => PairDepthBands;
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* @dev All BigNumber values are normalized to floats with appropriate precision
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = void 0;
|
|
7
|
+
exports.convertPairDepthBandsFromSlots = exports.convertPairDepthBandsDecoded = exports.validateDepthBandsMapping = exports.validateDepthBands = exports.convertDepthBandsMapping = exports.convertPairDepthBands = exports.convertDepthBands = exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = void 0;
|
|
8
|
+
const depthBands_1 = require("../../../pricing/depthBands");
|
|
8
9
|
/**
|
|
9
10
|
* @dev Converts contract OI windows settings to SDK format
|
|
10
11
|
* @param contractData Contract OiWindowsSettings struct
|
|
@@ -57,3 +58,98 @@ const convertOiWindowsSettingsArray = (contractDataArray) => {
|
|
|
57
58
|
return contractDataArray.map(exports.convertOiWindowsSettings);
|
|
58
59
|
};
|
|
59
60
|
exports.convertOiWindowsSettingsArray = convertOiWindowsSettingsArray;
|
|
61
|
+
/**
|
|
62
|
+
* @dev Converts decoded depth bands from contract to SDK format
|
|
63
|
+
* @param totalDepthUsd Total depth in USD (already decoded from contract)
|
|
64
|
+
* @param bandsBps Array of 30 band percentages in basis points from contract
|
|
65
|
+
* @returns Normalized depth bands with bands in 0-1 range
|
|
66
|
+
*/
|
|
67
|
+
const convertDepthBands = (totalDepthUsd, bandsBps) => {
|
|
68
|
+
// Convert bands from basis points to 0-1 range
|
|
69
|
+
const bands = bandsBps.map(bps => bps / 10000);
|
|
70
|
+
return {
|
|
71
|
+
totalDepthUsd,
|
|
72
|
+
bands,
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
exports.convertDepthBands = convertDepthBands;
|
|
76
|
+
/**
|
|
77
|
+
* @dev Converts decoded pair depth bands from contract to SDK format
|
|
78
|
+
* @param aboveDepth Decoded above depth bands from getPairDepthBandsDecoded
|
|
79
|
+
* @param belowDepth Decoded below depth bands from getPairDepthBandsDecoded
|
|
80
|
+
* @returns Normalized pair depth bands with above/below
|
|
81
|
+
*/
|
|
82
|
+
const convertPairDepthBands = (aboveDepth, belowDepth) => {
|
|
83
|
+
// Convert above bands if configured
|
|
84
|
+
const above = aboveDepth && aboveDepth.totalDepthUsd > 0
|
|
85
|
+
? (0, exports.convertDepthBands)(aboveDepth.totalDepthUsd, aboveDepth.bands)
|
|
86
|
+
: undefined;
|
|
87
|
+
// Convert below bands if configured
|
|
88
|
+
const below = belowDepth && belowDepth.totalDepthUsd > 0
|
|
89
|
+
? (0, exports.convertDepthBands)(belowDepth.totalDepthUsd, belowDepth.bands)
|
|
90
|
+
: undefined;
|
|
91
|
+
return {
|
|
92
|
+
above,
|
|
93
|
+
below,
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
exports.convertPairDepthBands = convertPairDepthBands;
|
|
97
|
+
/**
|
|
98
|
+
* @dev Converts decoded depth bands mapping from contract to SDK format
|
|
99
|
+
* @param bandsBps Array of 30 band offset values in basis points from getDepthBandsMappingDecoded
|
|
100
|
+
* @returns Normalized depth bands mapping with offset values in 0-1 range
|
|
101
|
+
*/
|
|
102
|
+
const convertDepthBandsMapping = (bandsBps) => {
|
|
103
|
+
// Convert bands from basis points to 0-1 range
|
|
104
|
+
const bands = bandsBps.map(bps => bps / 10000);
|
|
105
|
+
return {
|
|
106
|
+
bands,
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
exports.convertDepthBandsMapping = convertDepthBandsMapping;
|
|
110
|
+
/**
|
|
111
|
+
* @dev Validates depth bands have correct number of bands
|
|
112
|
+
* @param depthBands Depth bands to validate
|
|
113
|
+
* @returns True if valid (30 bands)
|
|
114
|
+
*/
|
|
115
|
+
const validateDepthBands = (depthBands) => {
|
|
116
|
+
return depthBands.bands.length === 30;
|
|
117
|
+
};
|
|
118
|
+
exports.validateDepthBands = validateDepthBands;
|
|
119
|
+
/**
|
|
120
|
+
* @dev Validates depth bands mapping has correct number of bands
|
|
121
|
+
* @param mapping Depth bands mapping to validate
|
|
122
|
+
* @returns True if valid (30 bands)
|
|
123
|
+
*/
|
|
124
|
+
const validateDepthBandsMapping = (mapping) => {
|
|
125
|
+
return mapping.bands.length === 30;
|
|
126
|
+
};
|
|
127
|
+
exports.validateDepthBandsMapping = validateDepthBandsMapping;
|
|
128
|
+
/**
|
|
129
|
+
* @dev Alternative converter for decoded pair depth bands from contract
|
|
130
|
+
* @param contractData Decoded pair depth bands from getPairDepthBandsDecoded
|
|
131
|
+
* @returns Normalized pair depth bands
|
|
132
|
+
*/
|
|
133
|
+
const convertPairDepthBandsDecoded = (contractData) => {
|
|
134
|
+
return (0, exports.convertPairDepthBands)(contractData.above, contractData.below);
|
|
135
|
+
};
|
|
136
|
+
exports.convertPairDepthBandsDecoded = convertPairDepthBandsDecoded;
|
|
137
|
+
/**
|
|
138
|
+
* @dev Alternative converter for raw slot-based pair depth bands (if needed for legacy)
|
|
139
|
+
* @param aboveSlot1 First slot for above bands
|
|
140
|
+
* @param aboveSlot2 Second slot for above bands
|
|
141
|
+
* @param belowSlot1 First slot for below bands
|
|
142
|
+
* @param belowSlot2 Second slot for below bands
|
|
143
|
+
* @returns Normalized pair depth bands
|
|
144
|
+
*/
|
|
145
|
+
const convertPairDepthBandsFromSlots = (aboveSlot1, aboveSlot2, belowSlot1, belowSlot2) => {
|
|
146
|
+
// Use the decoding functions from pricing module if raw slots are provided
|
|
147
|
+
const above = aboveSlot1 !== BigInt(0) || aboveSlot2 !== BigInt(0)
|
|
148
|
+
? (0, depthBands_1.decodeDepthBands)(aboveSlot1, aboveSlot2)
|
|
149
|
+
: undefined;
|
|
150
|
+
const below = belowSlot1 !== BigInt(0) || belowSlot2 !== BigInt(0)
|
|
151
|
+
? (0, depthBands_1.decodeDepthBands)(belowSlot1, belowSlot2)
|
|
152
|
+
: undefined;
|
|
153
|
+
return (0, exports.convertPairDepthBands)(above, below);
|
|
154
|
+
};
|
|
155
|
+
exports.convertPairDepthBandsFromSlots = convertPairDepthBandsFromSlots;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* @dev Cumulative volume price impact calculations
|
|
3
3
|
* @dev Mirrors contract's getTradeCumulVolPriceImpactP functionality
|
|
4
4
|
*/
|
|
5
|
+
import { PairDepthBands, DepthBandsMapping } from "./types";
|
|
5
6
|
import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth, PairFactor, UserPriceImpact } from "../../types";
|
|
6
7
|
import { ContractsVersion } from "../../../contracts/types";
|
|
7
8
|
export type CumulVolContext = {
|
|
@@ -15,6 +16,8 @@ export type CumulVolContext = {
|
|
|
15
16
|
pairDepth?: PairDepth | undefined;
|
|
16
17
|
oiWindowsSettings?: OiWindowsSettings | undefined;
|
|
17
18
|
oiWindows?: OiWindows | undefined;
|
|
19
|
+
pairDepthBands?: PairDepthBands | undefined;
|
|
20
|
+
depthBandsMapping?: DepthBandsMapping | undefined;
|
|
18
21
|
userPriceImpact?: UserPriceImpact | undefined;
|
|
19
22
|
collateralPriceUsd?: number;
|
|
20
23
|
} & Partial<PairFactor>;
|