@gainsnetwork/sdk 1.3.0-rc6 → 1.4.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 +4 -11
- package/lib/backend/tradingVariables/converter.d.ts +3 -7
- package/lib/backend/tradingVariables/converter.js +63 -71
- package/lib/backend/tradingVariables/index.js +8 -10
- package/lib/backend/tradingVariables/types.d.ts +2 -4
- package/lib/constants.d.ts +4 -0
- package/lib/constants.js +6 -1
- package/lib/contracts/addresses.d.ts +1 -1
- package/lib/contracts/addresses.js +4 -8
- package/lib/contracts/addresses.json +0 -29
- package/lib/contracts/index.d.ts +1 -1
- package/lib/contracts/index.js +3 -3
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +299 -608
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +313 -1996
- package/lib/contracts/utils/borrowingFees.js +20 -9
- package/lib/contracts/utils/openTrades.js +20 -11
- package/lib/contracts/utils/pairs.d.ts +2 -13
- package/lib/contracts/utils/pairs.js +34 -80
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- 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/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 +2 -1
- package/lib/trade/liquidation/index.js +6 -4
- package/lib/trade/liquidation.d.ts +12 -0
- package/lib/trade/liquidation.js +55 -0
- 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/pnl.d.ts +10 -0
- package/lib/trade/pnl.js +33 -0
- 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 -21
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +0 -63
- package/lib/trade/priceImpact/cumulVol/converter.js +1 -97
- package/lib/trade/priceImpact/cumulVol/index.d.ts +6 -7
- package/lib/trade/priceImpact/cumulVol/index.js +41 -149
- package/lib/trade/priceImpact/open/builder.js +2 -1
- package/lib/trade/priceImpact/open/index.js +1 -7
- 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/lib/trade/types.d.ts +5 -1
- package/lib/trade/types.js +4 -0
- package/package.json +2 -2
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +0 -1911
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +0 -1067
- package/lib/contracts/types/generated/GNSBorrowingFees.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +0 -979
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +0 -1058
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +0 -2
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +0 -533
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +0 -2
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +0 -613
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +0 -2
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +0 -911
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +0 -2
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +0 -660
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +0 -2
- package/lib/contracts/types/generated/GNSTrading.d.ts +0 -758
- package/lib/contracts/types/generated/GNSTrading.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +0 -875
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +0 -806
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +0 -821
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +0 -2
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +0 -1387
- package/lib/contracts/types/generated/GNSTradingStorage.js +0 -2
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +0 -1838
- package/lib/contracts/types/generated/GTokenV6_3_2.js +0 -2
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +0 -83
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +0 -2691
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +0 -88
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +0 -1654
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +0 -113
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +0 -1742
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +0 -124
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +0 -1784
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +0 -100
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +0 -1116
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +0 -100
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +0 -1003
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +0 -98
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +0 -1485
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +0 -117
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +0 -1265
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +0 -82
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +0 -1273
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +0 -82
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +0 -1326
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +0 -113
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +0 -1428
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +0 -96
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +0 -2241
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +0 -95
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +0 -1071
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +0 -110
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +0 -2682
- package/lib/markets/oi/fetcher.d.ts +0 -58
- package/lib/markets/oi/fetcher.js +0 -181
- package/lib/markets/oi/validation.d.ts +0 -80
- package/lib/markets/oi/validation.js +0 -172
- package/lib/markets/price/signedPrices.d.ts +0 -36
- package/lib/markets/price/signedPrices.js +0 -181
- package/lib/pricing/depthBands/converter.d.ts +0 -65
- package/lib/pricing/depthBands/converter.js +0 -155
- package/lib/pricing/depthBands/decoder.d.ts +0 -32
- package/lib/pricing/depthBands/decoder.js +0 -109
- package/lib/pricing/depthBands/encoder.d.ts +0 -19
- package/lib/pricing/depthBands/encoder.js +0 -105
- package/lib/pricing/depthBands/index.d.ts +0 -8
- package/lib/pricing/depthBands/index.js +0 -26
- package/lib/pricing/depthBands/types.d.ts +0 -49
- package/lib/pricing/depthBands/types.js +0 -10
- package/lib/pricing/depthBands/validator.d.ts +0 -22
- package/lib/pricing/depthBands/validator.js +0 -113
- package/lib/pricing/depthBands.d.ts +0 -39
- package/lib/pricing/depthBands.js +0 -94
- package/lib/pricing/index.d.ts +0 -4
- package/lib/pricing/index.js +0 -20
- package/lib/trade/effectiveLeverage/builder.d.ts +0 -23
- package/lib/trade/effectiveLeverage/builder.js +0 -30
- package/lib/trade/fees/holdingFees/index.d.ts +0 -46
- package/lib/trade/fees/holdingFees/index.js +0 -105
- package/lib/trade/fees/holdingFees/types.d.ts +0 -23
- package/lib/trade/fees/holdingFees/types.js +0 -5
- package/lib/trade/fees/trading/holdingFees.d.ts +0 -28
- package/lib/trade/fees/trading/holdingFees.js +0 -66
- package/lib/trade/fees/trading/holdingFeesStructured.d.ts +0 -28
- package/lib/trade/fees/trading/holdingFeesStructured.js +0 -66
- package/lib/trade/priceImpact/cumulVol/types.d.ts +0 -11
- package/lib/trade/priceImpact/cumulVol/types.js +0 -2
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);
|
|
@@ -114,7 +115,7 @@ const getComprehensivePnl = (trade, marketPrice, executionPrice, tradeInfo, cont
|
|
|
114
115
|
const totalHoldingFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee;
|
|
115
116
|
const totalFees = totalHoldingFees + closingFee;
|
|
116
117
|
// Check liquidation (using raw PnL for liquidation check)
|
|
117
|
-
const liquidationThreshold = context.tradeData
|
|
118
|
+
const liquidationThreshold = ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.liquidationParams)
|
|
118
119
|
? (0, liquidation_1.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage) *
|
|
119
120
|
-100
|
|
120
121
|
: -90; // Default 90% loss
|
|
@@ -191,6 +192,7 @@ exports.getComprehensivePnl = getComprehensivePnl;
|
|
|
191
192
|
* @returns [pnlCollateral, pnlPercentage] or undefined if no price
|
|
192
193
|
*/
|
|
193
194
|
const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
|
|
195
|
+
var _a;
|
|
194
196
|
if (!price) {
|
|
195
197
|
return;
|
|
196
198
|
}
|
|
@@ -222,7 +224,7 @@ const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, use
|
|
|
222
224
|
// Calculate closing fee using the same function as opening fees
|
|
223
225
|
const positionSizeCollateral = posCollat * trade.leverage;
|
|
224
226
|
const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(0, // collateralIndex not used
|
|
225
|
-
trade.user, trade.pairIndex, positionSizeCollateral, trade.isCounterTrade
|
|
227
|
+
trade.user, trade.pairIndex, positionSizeCollateral, (_a = trade.isCounterTrade) !== null && _a !== void 0 ? _a : false, {
|
|
226
228
|
fee: context.fee,
|
|
227
229
|
globalTradeFeeParams: context.globalTradeFeeParams,
|
|
228
230
|
collateralPriceUsd: context.collateralPriceUsd || 1,
|
|
@@ -246,11 +248,12 @@ exports.getPnl = getPnl;
|
|
|
246
248
|
* @returns The price that would result in the target PnL percentage
|
|
247
249
|
*/
|
|
248
250
|
const getPriceForTargetPnlPercentage = (targetPnlPercent, trade, tradeInfo, context, netPnl = false) => {
|
|
251
|
+
var _a, _b;
|
|
249
252
|
const { leverage, openPrice, long, collateralAmount } = trade;
|
|
250
253
|
const positionSizeCollateral = collateralAmount * leverage;
|
|
251
254
|
// Calculate holding fees - always use getTradePendingHoldingFeesCollateral
|
|
252
255
|
// This mirrors the contract's getTradeValueCollateral which always calls this function
|
|
253
|
-
const fees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeData
|
|
256
|
+
const fees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.tradeFeesData) || {
|
|
254
257
|
realizedTradingFeesCollateral: 0,
|
|
255
258
|
realizedPnlCollateral: 0,
|
|
256
259
|
manuallyRealizedNegativePnlCollateral: 0,
|
|
@@ -266,7 +269,7 @@ const getPriceForTargetPnlPercentage = (targetPnlPercent, trade, tradeInfo, cont
|
|
|
266
269
|
borrowingV1: context.borrowingV1,
|
|
267
270
|
borrowingV2: context.borrowingV2,
|
|
268
271
|
funding: context.funding,
|
|
269
|
-
initialAccFees: context.tradeData
|
|
272
|
+
initialAccFees: (_b = context.tradeData) === null || _b === void 0 ? void 0 : _b.initialAccFees,
|
|
270
273
|
});
|
|
271
274
|
const totalHoldingFees = fees.fundingFeeCollateral +
|
|
272
275
|
fees.borrowingFeeCollateral +
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { GetBorrowingFeeContext } from "./fees";
|
|
2
|
+
import { Fee, LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
|
|
3
|
+
import { ContractsVersion } from "../contracts/types";
|
|
4
|
+
export type GetPnlContext = GetBorrowingFeeContext & {
|
|
5
|
+
fee: Fee | undefined;
|
|
6
|
+
collateralPriceUsd: number | undefined;
|
|
7
|
+
contractsVersion: ContractsVersion | undefined;
|
|
8
|
+
feeMultiplier: number | undefined;
|
|
9
|
+
};
|
|
10
|
+
export declare const getPnl: (price: number | undefined, trade: Trade, tradeInfo: TradeInfo, initialAccFees: TradeInitialAccFees, liquidationParams: LiquidationParams, useFees: boolean, context: GetPnlContext) => number[] | undefined;
|
package/lib/trade/pnl.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPnl = void 0;
|
|
4
|
+
const fees_1 = require("./fees");
|
|
5
|
+
const liquidation_1 = require("./liquidation");
|
|
6
|
+
const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
|
|
7
|
+
if (!price) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const posCollat = trade.collateralAmount;
|
|
11
|
+
const { openPrice, leverage } = trade;
|
|
12
|
+
const { fee } = context;
|
|
13
|
+
let pnlCollat = trade.long
|
|
14
|
+
? ((price - openPrice) / openPrice) * leverage * posCollat
|
|
15
|
+
: ((openPrice - price) / openPrice) * leverage * posCollat;
|
|
16
|
+
if (useFees) {
|
|
17
|
+
pnlCollat -= (0, fees_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
18
|
+
}
|
|
19
|
+
let pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
20
|
+
// Can be liquidated
|
|
21
|
+
if (pnlPercentage <=
|
|
22
|
+
(0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
|
|
23
|
+
pnlPercentage = -100;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
pnlCollat -= (0, fees_1.getClosingFee)(posCollat, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd, context.feeMultiplier);
|
|
27
|
+
pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
28
|
+
}
|
|
29
|
+
pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
|
|
30
|
+
pnlCollat = (posCollat * pnlPercentage) / 100;
|
|
31
|
+
return [pnlCollat, pnlPercentage];
|
|
32
|
+
};
|
|
33
|
+
exports.getPnl = getPnl;
|
|
@@ -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,15 +10,15 @@ 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, _h;
|
|
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
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const oiWindows = globalTradingVariables.oiWindows?.[pairIndex];
|
|
19
|
+
const pairDepth = (_a = globalTradingVariables.pairDepths) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
20
|
+
const pairFactor = (_b = globalTradingVariables.pairFactors) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
21
|
+
const oiWindows = (_c = globalTradingVariables.oiWindows) === null || _c === void 0 ? void 0 : _c[pairIndex];
|
|
22
22
|
// Get OI windows settings (same for all pairs)
|
|
23
23
|
// OI windows settings from global trading variables are already in SDK format
|
|
24
24
|
const oiWindowsSettings = globalTradingVariables.oiWindowsSettings;
|
|
@@ -26,28 +26,18 @@ const buildCumulVolContext = (globalTradingVariables, collateralIndex, pairIndex
|
|
|
26
26
|
const userPriceImpact = additionalParams.userPriceImpact;
|
|
27
27
|
const protectionCloseFactorWhitelist = additionalParams.protectionCloseFactorWhitelist;
|
|
28
28
|
// Get liquidation params - check both pair and group level
|
|
29
|
-
const liquidationParams = globalTradingVariables.liquidationParams
|
|
30
|
-
globalTradingVariables.liquidationParams
|
|
31
|
-
return {
|
|
29
|
+
const liquidationParams = ((_e = (_d = globalTradingVariables.liquidationParams) === null || _d === void 0 ? void 0 : _d.pairs) === null || _e === void 0 ? void 0 : _e[pairIndex]) ||
|
|
30
|
+
((_g = (_f = globalTradingVariables.liquidationParams) === null || _f === void 0 ? void 0 : _f.groups) === null || _g === void 0 ? void 0 : _g[0]); // fallback to first group
|
|
31
|
+
return Object.assign({
|
|
32
32
|
// Trade state
|
|
33
|
-
isOpen: additionalParams.isOpen,
|
|
34
|
-
isPnlPositive: additionalParams.isPnlPositive,
|
|
35
|
-
createdBlock: additionalParams.createdBlock,
|
|
33
|
+
isOpen: additionalParams.isOpen, isPnlPositive: additionalParams.isPnlPositive, createdBlock: additionalParams.createdBlock,
|
|
36
34
|
// Protection factors
|
|
37
|
-
liquidationParams,
|
|
38
|
-
currentBlock: additionalParams.currentBlock,
|
|
39
|
-
contractsVersion: additionalParams.contractsVersion,
|
|
40
|
-
protectionCloseFactorWhitelist,
|
|
35
|
+
liquidationParams, currentBlock: additionalParams.currentBlock, contractsVersion: additionalParams.contractsVersion, protectionCloseFactorWhitelist,
|
|
41
36
|
// Price impact data
|
|
42
|
-
|
|
43
|
-
depthBandsMapping,
|
|
37
|
+
pairDepth,
|
|
44
38
|
oiWindowsSettings,
|
|
45
39
|
oiWindows,
|
|
46
40
|
// User/collateral specific
|
|
47
|
-
userPriceImpact,
|
|
48
|
-
collateralPriceUsd: collateral.prices?.collateralPriceUsd || 1,
|
|
49
|
-
// Pair factors (spread across the context)
|
|
50
|
-
...pairFactor,
|
|
51
|
-
};
|
|
41
|
+
userPriceImpact, collateralPriceUsd: ((_h = collateral.prices) === null || _h === void 0 ? void 0 : _h.collateralPriceUsd) || 1 }, pairFactor);
|
|
52
42
|
};
|
|
53
43
|
exports.buildCumulVolContext = buildCumulVolContext;
|
|
@@ -4,7 +4,6 @@
|
|
|
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";
|
|
8
7
|
/**
|
|
9
8
|
* @dev Converts contract OI windows settings to SDK format
|
|
10
9
|
* @param contractData Contract OiWindowsSettings struct
|
|
@@ -30,65 +29,3 @@ export declare const convertOiWindows: (windowIds: string[], contractWindows: IP
|
|
|
30
29
|
* @returns Array of normalized OI windows settings
|
|
31
30
|
*/
|
|
32
31
|
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,8 +4,7 @@
|
|
|
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.
|
|
8
|
-
const depthBands_1 = require("../../../pricing/depthBands");
|
|
7
|
+
exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = void 0;
|
|
9
8
|
/**
|
|
10
9
|
* @dev Converts contract OI windows settings to SDK format
|
|
11
10
|
* @param contractData Contract OiWindowsSettings struct
|
|
@@ -58,98 +57,3 @@ const convertOiWindowsSettingsArray = (contractDataArray) => {
|
|
|
58
57
|
return contractDataArray.map(exports.convertOiWindowsSettings);
|
|
59
58
|
};
|
|
60
59
|
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,8 +2,7 @@
|
|
|
2
2
|
* @dev Cumulative volume price impact calculations
|
|
3
3
|
* @dev Mirrors contract's getTradeCumulVolPriceImpactP functionality
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
6
|
-
import { LiquidationParams, OiWindows, OiWindowsSettings, PairFactor, UserPriceImpact } from "../../types";
|
|
5
|
+
import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth, PairFactor, UserPriceImpact } from "../../types";
|
|
7
6
|
import { ContractsVersion } from "../../../contracts/types";
|
|
8
7
|
export type CumulVolContext = {
|
|
9
8
|
isOpen?: boolean;
|
|
@@ -13,10 +12,9 @@ export type CumulVolContext = {
|
|
|
13
12
|
currentBlock?: number | undefined;
|
|
14
13
|
contractsVersion?: ContractsVersion | undefined;
|
|
15
14
|
protectionCloseFactorWhitelist?: boolean;
|
|
15
|
+
pairDepth?: PairDepth | undefined;
|
|
16
16
|
oiWindowsSettings?: OiWindowsSettings | undefined;
|
|
17
17
|
oiWindows?: OiWindows | undefined;
|
|
18
|
-
pairDepthBands?: PairDepthBands | undefined;
|
|
19
|
-
depthBandsMapping?: DepthBandsMapping | undefined;
|
|
20
18
|
userPriceImpact?: UserPriceImpact | undefined;
|
|
21
19
|
collateralPriceUsd?: number;
|
|
22
20
|
} & Partial<PairFactor>;
|
|
@@ -57,7 +55,7 @@ export declare const getLegacyFactor: (context: CumulVolContext | undefined) =>
|
|
|
57
55
|
* @param context Additional context with depths, OI data, and factors
|
|
58
56
|
* @returns Cumulative volume price impact percentage (not including spread)
|
|
59
57
|
*/
|
|
60
|
-
export declare const getTradeCumulVolPriceImpactP: (
|
|
58
|
+
export declare const getTradeCumulVolPriceImpactP: (trader: string, pairIndex: number, long: boolean, tradeOpenInterestUsd: number, isPnlPositive: boolean, open: boolean, lastPosIncreaseBlock: number, context: CumulVolContext) => number;
|
|
61
59
|
/**
|
|
62
60
|
* @dev Gets the fixed spread percentage with direction
|
|
63
61
|
* @dev Mirrors contract's getFixedSpreadP function
|
|
@@ -85,12 +83,13 @@ export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation
|
|
|
85
83
|
* @param buy True for long, false for short
|
|
86
84
|
* @param collateral Collateral amount
|
|
87
85
|
* @param leverage Position leverage
|
|
86
|
+
* @param pairDepth 1% depth values for the pair
|
|
88
87
|
* @param oiWindowsSettings OI windows configuration
|
|
89
88
|
* @param oiWindows Current OI windows data
|
|
90
89
|
* @param context Additional context for the calculation
|
|
91
90
|
* @returns Total spread + cumulative volume price impact percentage
|
|
92
91
|
*/
|
|
93
|
-
export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number,
|
|
92
|
+
export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
|
|
94
93
|
/**
|
|
95
94
|
* @dev Convenience function for calculating cumulative volume price impact
|
|
96
95
|
* @dev Uses collateral and leverage instead of USD position size
|
|
@@ -104,6 +103,6 @@ export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, bu
|
|
|
104
103
|
export declare const getCumulVolPriceImpact: (buy: boolean, collateral: number, leverage: number, open: boolean, context: CumulVolContext & {
|
|
105
104
|
collateralPriceUsd: number;
|
|
106
105
|
}) => number;
|
|
107
|
-
export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number,
|
|
106
|
+
export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
|
|
108
107
|
export { convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, } from "./converter";
|
|
109
108
|
export { buildCumulVolContext } from "./builder";
|