@gainsnetwork/sdk 0.2.73-rc2 → 1.0.0-rc10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/lib/backend/globalTrades/index.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +3 -0
- package/lib/backend/index.js +28 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +318 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +34 -0
- package/lib/backend/tradingVariables/converter.js +338 -0
- package/lib/backend/tradingVariables/index.d.ts +5 -0
- package/lib/backend/tradingVariables/index.js +96 -0
- package/lib/backend/tradingVariables/types.d.ts +113 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.d.ts +0 -5
- package/lib/constants.js +0 -5
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +2134 -293
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
- package/lib/contracts/types/generated/GToken.d.ts +78 -107
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +90 -53
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +4496 -430
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
- package/lib/contracts/types/generated/factories/GToken__factory.js +69 -142
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/openTrades.d.ts +1 -0
- package/lib/contracts/utils/openTrades.js +94 -56
- package/lib/contracts/utils/pairs.js +0 -5
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -0
- package/lib/markets/collateral/converter.d.ts +5 -0
- package/lib/markets/collateral/converter.js +11 -0
- package/lib/markets/collateral/index.d.ts +1 -0
- package/lib/markets/collateral/index.js +17 -0
- package/lib/markets/collateral/types.d.ts +7 -0
- package/lib/markets/collateral/types.js +2 -0
- package/lib/markets/holdingFees/index.d.ts +46 -0
- package/lib/markets/holdingFees/index.js +105 -0
- package/lib/markets/holdingFees/types.d.ts +23 -0
- package/lib/markets/holdingFees/types.js +5 -0
- package/lib/markets/index.d.ts +5 -0
- package/lib/markets/index.js +5 -0
- package/lib/markets/leverage/builder.d.ts +12 -0
- package/lib/markets/leverage/builder.js +25 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.d.ts +7 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.js +38 -0
- package/lib/markets/leverage/index.d.ts +3 -0
- package/lib/markets/leverage/index.js +19 -0
- package/lib/markets/leverage/types.d.ts +15 -0
- package/lib/markets/leverage/types.js +2 -0
- package/lib/markets/oi/converter.d.ts +62 -0
- package/lib/markets/oi/converter.js +102 -0
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/index.d.ts +49 -0
- package/lib/markets/oi/index.js +77 -0
- package/lib/markets/oi/types.d.ts +73 -0
- package/lib/markets/oi/types.js +6 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/markets/price/builder.d.ts +25 -0
- package/lib/markets/price/builder.js +69 -0
- package/lib/markets/price/index.d.ts +6 -0
- package/lib/markets/price/index.js +22 -0
- package/lib/markets/price/marketPrice.d.ts +13 -0
- package/lib/markets/price/marketPrice.js +35 -0
- package/lib/markets/price/types.d.ts +23 -0
- package/lib/markets/price/types.js +5 -0
- package/lib/trade/counterTrade/index.d.ts +2 -0
- package/lib/trade/counterTrade/index.js +18 -0
- package/lib/trade/counterTrade/types.d.ts +7 -0
- package/lib/trade/counterTrade/types.js +2 -0
- package/lib/trade/counterTrade/validateCounterTrade.d.ts +10 -0
- package/lib/trade/counterTrade/validateCounterTrade.js +29 -0
- package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
- package/lib/trade/effectiveLeverage/builder.js +30 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.d.ts +23 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.js +64 -0
- package/lib/trade/effectiveLeverage/index.d.ts +3 -0
- package/lib/trade/effectiveLeverage/index.js +22 -0
- package/lib/trade/effectiveLeverage/types.d.ts +33 -0
- package/lib/trade/effectiveLeverage/types.js +2 -0
- package/lib/trade/fees/borrowing/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +34 -0
- package/lib/trade/fees/borrowing/index.d.ts +26 -3
- package/lib/trade/fees/borrowing/index.js +72 -22
- package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
- package/lib/trade/fees/borrowingV2/builder.js +24 -0
- package/lib/trade/fees/borrowingV2/converter.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/converter.js +132 -0
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/fetcher.js +185 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +48 -0
- package/lib/trade/fees/borrowingV2/index.js +112 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +95 -0
- package/lib/trade/fees/borrowingV2/types.js +5 -0
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +114 -0
- package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
- package/lib/trade/fees/fundingFees/builder.js +35 -0
- package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
- package/lib/trade/fees/fundingFees/converter.js +196 -0
- package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
- package/lib/trade/fees/fundingFees/fetcher.js +150 -0
- package/lib/trade/fees/fundingFees/index.d.ts +124 -0
- package/lib/trade/fees/fundingFees/index.js +309 -0
- package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
- package/lib/trade/fees/fundingFees/pairContext.js +17 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- package/lib/trade/fees/holdingFees/index.d.ts +46 -0
- package/lib/trade/fees/holdingFees/index.js +105 -0
- package/lib/trade/fees/holdingFees/types.d.ts +23 -0
- package/lib/trade/fees/holdingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +9 -2
- package/lib/trade/fees/index.js +69 -16
- package/lib/trade/fees/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- package/lib/trade/fees/tiers/index.d.ts +18 -0
- package/lib/trade/fees/tiers/index.js +45 -1
- package/lib/trade/fees/trading/builder.d.ts +18 -0
- package/lib/trade/fees/trading/builder.js +20 -0
- package/lib/trade/fees/trading/converter.d.ts +32 -0
- package/lib/trade/fees/trading/converter.js +47 -0
- package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFees.js +66 -0
- package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
- package/lib/trade/fees/trading/index.d.ts +62 -0
- package/lib/trade/fees/trading/index.js +155 -0
- package/lib/trade/fees/trading/types.d.ts +48 -0
- package/lib/trade/fees/trading/types.js +5 -0
- package/lib/trade/index.d.ts +5 -2
- package/lib/trade/index.js +5 -2
- package/lib/trade/liquidation/builder.d.ts +25 -0
- package/lib/trade/liquidation/builder.js +59 -0
- package/lib/trade/liquidation/converter.d.ts +23 -0
- package/lib/trade/liquidation/converter.js +46 -0
- package/lib/trade/liquidation/index.d.ts +29 -0
- package/lib/trade/liquidation/index.js +218 -0
- package/lib/trade/liquidation/types.d.ts +43 -0
- package/lib/trade/liquidation/types.js +2 -0
- package/lib/trade/pnl/builder.d.ts +16 -0
- package/lib/trade/pnl/builder.js +44 -0
- package/lib/trade/pnl/converter.d.ts +47 -0
- package/lib/trade/pnl/converter.js +72 -0
- package/lib/trade/pnl/index.d.ts +91 -0
- package/lib/trade/pnl/index.js +303 -0
- package/lib/trade/pnl/types.d.ts +79 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/builder.d.ts +23 -0
- package/lib/trade/priceImpact/close/builder.js +45 -0
- package/lib/trade/priceImpact/close/index.d.ts +22 -0
- package/lib/trade/priceImpact/close/index.js +134 -0
- package/lib/trade/priceImpact/close/types.d.ts +47 -0
- package/lib/trade/priceImpact/close/types.js +5 -0
- package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
- package/lib/trade/priceImpact/cumulVol/builder.js +43 -0
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
- package/lib/trade/priceImpact/cumulVol/index.d.ts +108 -0
- package/lib/trade/priceImpact/cumulVol/index.js +235 -0
- package/lib/trade/priceImpact/index.d.ts +21 -0
- package/lib/trade/priceImpact/index.js +79 -0
- package/lib/trade/priceImpact/open/builder.d.ts +21 -0
- package/lib/trade/priceImpact/open/builder.js +43 -0
- package/lib/trade/priceImpact/open/index.d.ts +23 -0
- package/lib/trade/priceImpact/open/index.js +79 -0
- package/lib/trade/priceImpact/open/types.d.ts +45 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
- package/lib/trade/priceImpact/skew/builder.js +28 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +46 -0
- package/lib/trade/priceImpact/skew/converter.js +81 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +60 -0
- package/lib/trade/priceImpact/skew/fetcher.js +169 -0
- package/lib/trade/priceImpact/skew/index.d.ts +53 -0
- package/lib/trade/priceImpact/skew/index.js +148 -0
- package/lib/trade/priceImpact/skew/types.d.ts +44 -0
- package/lib/trade/priceImpact/skew/types.js +5 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +109 -14
- package/lib/trade/types.js +0 -5
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +2 -2
- package/package.json +2 -1
- package/lib/trade/liquidation.d.ts +0 -12
- package/lib/trade/liquidation.js +0 -55
- package/lib/trade/pnl.d.ts +0 -10
- package/lib/trade/pnl.js +0 -33
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Main export file for liquidation module
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.encodeLiquidationParams = exports.convertLiquidationParamsArray = exports.convertLiquidationParams = exports.getLiqPnlThresholdP = exports.getLiquidationPriceAfterPositionUpdate = exports.getLiquidationPrice = void 0;
|
|
21
|
+
const types_1 = require("../../contracts/types");
|
|
22
|
+
const __1 = require("..");
|
|
23
|
+
/**
|
|
24
|
+
* @dev Calculate liquidation price with structured context
|
|
25
|
+
* @param trade The trade to calculate liquidation price for
|
|
26
|
+
* @param context Structured context with all required data
|
|
27
|
+
* @returns Liquidation price
|
|
28
|
+
*/
|
|
29
|
+
const getLiquidationPrice = (trade, context) => {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
// Extract parameters from structured context
|
|
32
|
+
const { currentPairPrice, additionalFeeCollateral = 0, partialCloseMultiplier = 1, beforeOpened = false, isCounterTrade = false, } = context.liquidationSpecific;
|
|
33
|
+
// 1. Calculate closing fees
|
|
34
|
+
const closingFee = (0, __1.getTotalTradeFeesCollateral)(trade.collateralIndex, "", // No fee tiers applied for liquidation calculation
|
|
35
|
+
trade.pairIndex, trade.collateralAmount * trade.leverage, isCounterTrade, {
|
|
36
|
+
fee: context.trading.fee,
|
|
37
|
+
collateralPriceUsd: context.core.collateralPriceUsd,
|
|
38
|
+
globalTradeFeeParams: context.trading.globalTradeFeeParams,
|
|
39
|
+
traderFeeMultiplier: 1,
|
|
40
|
+
counterTradeSettings: context.trading.counterTradeSettings,
|
|
41
|
+
});
|
|
42
|
+
// 2. Calculate holding fees and realized PnL for opened trades
|
|
43
|
+
let holdingFeesTotal = 0;
|
|
44
|
+
let totalRealizedPnlCollateral = 0;
|
|
45
|
+
if (!beforeOpened) {
|
|
46
|
+
// Calculate holding fees
|
|
47
|
+
const holdingFees = (0, __1.getTradePendingHoldingFeesCollateral)(trade, context.tradeData.tradeInfo, context.tradeData.tradeFeesData, currentPairPrice, {
|
|
48
|
+
contractsVersion: context.core.contractsVersion,
|
|
49
|
+
currentTimestamp: context.core.currentTimestamp,
|
|
50
|
+
collateralPriceUsd: context.core.collateralPriceUsd,
|
|
51
|
+
borrowingV1: context.borrowingV1,
|
|
52
|
+
borrowingV2: context.borrowingV2,
|
|
53
|
+
funding: context.funding,
|
|
54
|
+
initialAccFees: context.tradeData.initialAccFees,
|
|
55
|
+
});
|
|
56
|
+
holdingFeesTotal = holdingFees.totalFeeCollateral;
|
|
57
|
+
// Calculate total realized PnL (realized PnL minus realized trading fees)
|
|
58
|
+
totalRealizedPnlCollateral =
|
|
59
|
+
context.tradeData.tradeFeesData.realizedPnlCollateral -
|
|
60
|
+
context.tradeData.tradeFeesData.realizedTradingFeesCollateral;
|
|
61
|
+
}
|
|
62
|
+
// 3. Apply unified formula for all trades
|
|
63
|
+
const totalFeesCollateral = closingFee +
|
|
64
|
+
(holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
|
|
65
|
+
additionalFeeCollateral;
|
|
66
|
+
// 4. Calculate liquidation threshold
|
|
67
|
+
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage);
|
|
68
|
+
// 5. Calculate liquidation price distance
|
|
69
|
+
const collateralLiqNegativePnl = trade.collateralAmount * liqThresholdP;
|
|
70
|
+
let liqPriceDistance = (trade.openPrice * (collateralLiqNegativePnl - totalFeesCollateral)) /
|
|
71
|
+
trade.collateralAmount /
|
|
72
|
+
trade.leverage;
|
|
73
|
+
// 6. Apply closing spread for v9.2+
|
|
74
|
+
if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
75
|
+
((((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
76
|
+
context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
|
|
77
|
+
(((_b = context.liquidationSpecific.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !==
|
|
78
|
+
undefined &&
|
|
79
|
+
context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
|
|
80
|
+
const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
|
|
81
|
+
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
82
|
+
}
|
|
83
|
+
// 7. Calculate final liquidation price
|
|
84
|
+
return trade.long
|
|
85
|
+
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
86
|
+
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
87
|
+
};
|
|
88
|
+
exports.getLiquidationPrice = getLiquidationPrice;
|
|
89
|
+
/**
|
|
90
|
+
* @dev Calculate liquidation price after a position size update
|
|
91
|
+
* @dev Mirrors the contract's IncreasePositionSizeUtils.sol and DecreasePositionSizeUtils.sol logic
|
|
92
|
+
* @param existingTrade The current trade before the update
|
|
93
|
+
* @param newCollateralAmount New collateral amount after the update
|
|
94
|
+
* @param newLeverage New leverage after the update
|
|
95
|
+
* @param isLeverageUpdate Whether this is a leverage update vs regular position change
|
|
96
|
+
* @param positionSizeCollateralDelta The absolute change in position size (in collateral terms)
|
|
97
|
+
* @param pnlToRealizeCollateral PnL to be realized (only relevant for leverage decrease)
|
|
98
|
+
* @param context Structured context with all required data (including additionalFeesCollateral for increases)
|
|
99
|
+
* @returns New liquidation price after the update
|
|
100
|
+
*/
|
|
101
|
+
const getLiquidationPriceAfterPositionUpdate = (existingTrade, newCollateralAmount, newLeverage, isLeverageUpdate, positionSizeCollateralDelta, pnlToRealizeCollateral, context) => {
|
|
102
|
+
var _a, _b;
|
|
103
|
+
const { currentPairPrice, isCounterTrade = false } = context.liquidationSpecific;
|
|
104
|
+
// 1. Calculate closing fees on the new position size
|
|
105
|
+
const closingFeeCollateral = (0, __1.getTotalTradeFeesCollateral)(existingTrade.collateralIndex, "", // No fee tiers applied for liquidation calculation
|
|
106
|
+
existingTrade.pairIndex, newCollateralAmount * newLeverage, isCounterTrade, {
|
|
107
|
+
fee: context.trading.fee,
|
|
108
|
+
collateralPriceUsd: context.core.collateralPriceUsd,
|
|
109
|
+
globalTradeFeeParams: context.trading.globalTradeFeeParams,
|
|
110
|
+
traderFeeMultiplier: 1,
|
|
111
|
+
counterTradeSettings: context.trading.counterTradeSettings,
|
|
112
|
+
});
|
|
113
|
+
// 2. Calculate holding fees on the EXISTING trade (full position)
|
|
114
|
+
const holdingFees = (0, __1.getTradePendingHoldingFeesCollateral)(existingTrade, context.tradeData.tradeInfo, context.tradeData.tradeFeesData, currentPairPrice, {
|
|
115
|
+
contractsVersion: context.core.contractsVersion,
|
|
116
|
+
currentTimestamp: context.core.currentTimestamp,
|
|
117
|
+
collateralPriceUsd: context.core.collateralPriceUsd,
|
|
118
|
+
borrowingV1: context.borrowingV1,
|
|
119
|
+
borrowingV2: context.borrowingV2,
|
|
120
|
+
funding: context.funding,
|
|
121
|
+
initialAccFees: context.tradeData.initialAccFees,
|
|
122
|
+
});
|
|
123
|
+
// 3. Calculate total realized PnL
|
|
124
|
+
const totalRealizedPnlCollateral = context.tradeData.tradeFeesData.realizedPnlCollateral -
|
|
125
|
+
context.tradeData.tradeFeesData.realizedTradingFeesCollateral;
|
|
126
|
+
// 4. Determine if this is an increase or decrease
|
|
127
|
+
const existingPositionSizeCollateral = existingTrade.collateralAmount * existingTrade.leverage;
|
|
128
|
+
const newPositionSizeCollateral = newCollateralAmount * newLeverage;
|
|
129
|
+
const isIncrease = newPositionSizeCollateral > existingPositionSizeCollateral;
|
|
130
|
+
// 5. Calculate additional fee and partial close multiplier based on update type
|
|
131
|
+
let additionalFeeCollateral;
|
|
132
|
+
let partialCloseMultiplier;
|
|
133
|
+
if (isIncrease) {
|
|
134
|
+
// For position increases: use additional fees from context (e.g., opening fees)
|
|
135
|
+
additionalFeeCollateral =
|
|
136
|
+
context.liquidationSpecific.additionalFeeCollateral || 0;
|
|
137
|
+
partialCloseMultiplier = 1; // Set to 1
|
|
138
|
+
}
|
|
139
|
+
else if (isLeverageUpdate) {
|
|
140
|
+
// For leverage decreases: additional fee includes closing fee minus PnL to realize
|
|
141
|
+
additionalFeeCollateral = closingFeeCollateral - pnlToRealizeCollateral;
|
|
142
|
+
partialCloseMultiplier = 1; // Full multiplier for leverage updates
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
// For regular position decreases: no additional fee, scaled multiplier
|
|
146
|
+
additionalFeeCollateral = 0;
|
|
147
|
+
partialCloseMultiplier =
|
|
148
|
+
(existingPositionSizeCollateral - positionSizeCollateralDelta) /
|
|
149
|
+
existingPositionSizeCollateral;
|
|
150
|
+
}
|
|
151
|
+
// 6. Calculate total fees
|
|
152
|
+
const totalFeesCollateral = closingFeeCollateral +
|
|
153
|
+
(holdingFees.totalFeeCollateral - totalRealizedPnlCollateral) *
|
|
154
|
+
partialCloseMultiplier +
|
|
155
|
+
additionalFeeCollateral;
|
|
156
|
+
// 7. Calculate liquidation threshold
|
|
157
|
+
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.tradeData.liquidationParams, newLeverage);
|
|
158
|
+
// 8. Calculate liquidation price distance
|
|
159
|
+
const collateralLiqNegativePnl = newCollateralAmount * liqThresholdP;
|
|
160
|
+
// For increases, we need to use the new weighted average open price
|
|
161
|
+
// For decreases, we use the existing open price
|
|
162
|
+
const openPriceToUse = isIncrease
|
|
163
|
+
? context.liquidationSpecific.newOpenPrice || existingTrade.openPrice
|
|
164
|
+
: existingTrade.openPrice;
|
|
165
|
+
let liqPriceDistance = (openPriceToUse * (collateralLiqNegativePnl - totalFeesCollateral)) /
|
|
166
|
+
newCollateralAmount /
|
|
167
|
+
newLeverage;
|
|
168
|
+
// 9. Apply closing spread for v9.2+
|
|
169
|
+
if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
170
|
+
((((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
171
|
+
context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
|
|
172
|
+
(((_b = context.liquidationSpecific.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !==
|
|
173
|
+
undefined &&
|
|
174
|
+
context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
|
|
175
|
+
const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
|
|
176
|
+
liqPriceDistance -= openPriceToUse * closingSpreadP;
|
|
177
|
+
}
|
|
178
|
+
// 10. Calculate final liquidation price
|
|
179
|
+
return existingTrade.long
|
|
180
|
+
? Math.max(openPriceToUse - liqPriceDistance, 0)
|
|
181
|
+
: Math.max(openPriceToUse + liqPriceDistance, 0);
|
|
182
|
+
};
|
|
183
|
+
exports.getLiquidationPriceAfterPositionUpdate = getLiquidationPriceAfterPositionUpdate;
|
|
184
|
+
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
185
|
+
if (liquidationParams === undefined ||
|
|
186
|
+
leverage === undefined ||
|
|
187
|
+
liquidationParams.maxLiqSpreadP === 0 ||
|
|
188
|
+
liquidationParams.startLiqThresholdP === 0 ||
|
|
189
|
+
liquidationParams.endLiqThresholdP === 0 ||
|
|
190
|
+
liquidationParams.startLeverage === 0 ||
|
|
191
|
+
liquidationParams.endLeverage === 0) {
|
|
192
|
+
return 0.9;
|
|
193
|
+
}
|
|
194
|
+
if (leverage < liquidationParams.startLeverage) {
|
|
195
|
+
return liquidationParams.startLiqThresholdP;
|
|
196
|
+
}
|
|
197
|
+
if (leverage > liquidationParams.endLeverage) {
|
|
198
|
+
return liquidationParams.endLiqThresholdP;
|
|
199
|
+
}
|
|
200
|
+
if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
|
|
201
|
+
return liquidationParams.endLiqThresholdP;
|
|
202
|
+
}
|
|
203
|
+
return (liquidationParams.startLiqThresholdP -
|
|
204
|
+
((leverage - liquidationParams.startLeverage) *
|
|
205
|
+
(liquidationParams.startLiqThresholdP -
|
|
206
|
+
liquidationParams.endLiqThresholdP)) /
|
|
207
|
+
(liquidationParams.endLeverage - liquidationParams.startLeverage));
|
|
208
|
+
};
|
|
209
|
+
exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
|
|
210
|
+
// Converters
|
|
211
|
+
var converter_1 = require("./converter");
|
|
212
|
+
Object.defineProperty(exports, "convertLiquidationParams", { enumerable: true, get: function () { return converter_1.convertLiquidationParams; } });
|
|
213
|
+
Object.defineProperty(exports, "convertLiquidationParamsArray", { enumerable: true, get: function () { return converter_1.convertLiquidationParamsArray; } });
|
|
214
|
+
Object.defineProperty(exports, "encodeLiquidationParams", { enumerable: true, get: function () { return converter_1.encodeLiquidationParams; } });
|
|
215
|
+
// Types
|
|
216
|
+
__exportStar(require("./types"), exports);
|
|
217
|
+
// Builder
|
|
218
|
+
__exportStar(require("./builder"), exports);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { GetBorrowingFeeContext, BorrowingFee } from "./../fees";
|
|
2
|
+
import { LiquidationParams, UserPriceImpact, TradeFeesData, TradeInfo, CounterTradeSettings } from "./../types";
|
|
3
|
+
import { ContractsVersion } from "../../contracts/types";
|
|
4
|
+
import { GetPairBorrowingFeeV2Context } from "../fees/borrowingV2";
|
|
5
|
+
import { GetPairFundingFeeContext } from "../fees/fundingFees";
|
|
6
|
+
import { TradingFeesSubContext } from "../fees/trading/builder";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Structured context for liquidation price calculations
|
|
9
|
+
* @dev Follows the same pattern as GetComprehensivePnlContext
|
|
10
|
+
*/
|
|
11
|
+
export type GetLiquidationPriceContext = {
|
|
12
|
+
core: {
|
|
13
|
+
currentBlock: number;
|
|
14
|
+
currentTimestamp: number;
|
|
15
|
+
collateralPriceUsd: number;
|
|
16
|
+
contractsVersion: ContractsVersion;
|
|
17
|
+
spreadP: number;
|
|
18
|
+
};
|
|
19
|
+
borrowingV1?: GetBorrowingFeeContext;
|
|
20
|
+
borrowingV2?: GetPairBorrowingFeeV2Context;
|
|
21
|
+
funding?: GetPairFundingFeeContext;
|
|
22
|
+
trading: TradingFeesSubContext & {
|
|
23
|
+
userPriceImpact?: UserPriceImpact;
|
|
24
|
+
counterTradeSettings?: {
|
|
25
|
+
[pairIndex: number]: CounterTradeSettings;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
tradeData: {
|
|
29
|
+
tradeInfo: TradeInfo;
|
|
30
|
+
tradeFeesData: TradeFeesData;
|
|
31
|
+
liquidationParams: LiquidationParams;
|
|
32
|
+
initialAccFees?: BorrowingFee.InitialAccFees;
|
|
33
|
+
};
|
|
34
|
+
liquidationSpecific: {
|
|
35
|
+
currentPairPrice: number;
|
|
36
|
+
additionalFeeCollateral: number;
|
|
37
|
+
partialCloseMultiplier: number;
|
|
38
|
+
beforeOpened: boolean;
|
|
39
|
+
isCounterTrade: boolean;
|
|
40
|
+
userPriceImpact?: UserPriceImpact;
|
|
41
|
+
newOpenPrice?: number;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TradeContainer } from "../types";
|
|
2
|
+
import { GlobalTradingVariablesType } from "src/backend/tradingVariables/types";
|
|
3
|
+
import { GetComprehensivePnlContext } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* @dev Builds a complete context for comprehensive PnL calculations
|
|
6
|
+
* @dev Uses sub-context builders to create properly scoped contexts
|
|
7
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
8
|
+
* @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
|
|
9
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
10
|
+
* @returns Complete context ready for getComprehensivePnl
|
|
11
|
+
*/
|
|
12
|
+
export declare const buildComprehensivePnlContext: (globalTradingVariables: GlobalTradingVariablesType, tradeContainer: TradeContainer, additionalParams: {
|
|
13
|
+
currentBlock: number;
|
|
14
|
+
currentTimestamp: number;
|
|
15
|
+
traderFeeMultiplier?: number;
|
|
16
|
+
}) => GetComprehensivePnlContext;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildComprehensivePnlContext = void 0;
|
|
4
|
+
const builder_1 = require("../fees/borrowingV2/builder");
|
|
5
|
+
const builder_2 = require("../fees/fundingFees/builder");
|
|
6
|
+
const builder_3 = require("../fees/borrowing/builder");
|
|
7
|
+
const builder_4 = require("../fees/trading/builder");
|
|
8
|
+
/**
|
|
9
|
+
* @dev Builds a complete context for comprehensive PnL calculations
|
|
10
|
+
* @dev Uses sub-context builders to create properly scoped contexts
|
|
11
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
12
|
+
* @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
|
|
13
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
14
|
+
* @returns Complete context ready for getComprehensivePnl
|
|
15
|
+
*/
|
|
16
|
+
const buildComprehensivePnlContext = (globalTradingVariables, tradeContainer, additionalParams) => {
|
|
17
|
+
var _a;
|
|
18
|
+
const { trade, tradeInfo } = tradeContainer;
|
|
19
|
+
const collateralIndex = trade.collateralIndex || 1;
|
|
20
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
21
|
+
return {
|
|
22
|
+
// Core shared context
|
|
23
|
+
core: {
|
|
24
|
+
currentBlock: additionalParams.currentBlock,
|
|
25
|
+
currentTimestamp: additionalParams.currentTimestamp,
|
|
26
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
27
|
+
contractsVersion: tradeInfo.contractsVersion,
|
|
28
|
+
},
|
|
29
|
+
// Build sub-contexts using dedicated builders
|
|
30
|
+
borrowingV1: (0, builder_3.buildBorrowingV1Context)(globalTradingVariables, collateralIndex, additionalParams.currentBlock),
|
|
31
|
+
borrowingV2: (0, builder_1.buildBorrowingV2Context)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
32
|
+
funding: (0, builder_2.buildFundingContext)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
33
|
+
trading: (0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier),
|
|
34
|
+
// Trade-specific data
|
|
35
|
+
tradeData: tradeContainer.tradeFeesData && tradeContainer.liquidationParams
|
|
36
|
+
? {
|
|
37
|
+
tradeFeesData: tradeContainer.tradeFeesData,
|
|
38
|
+
liquidationParams: tradeContainer.liquidationParams,
|
|
39
|
+
initialAccFees: tradeContainer.initialAccFees,
|
|
40
|
+
}
|
|
41
|
+
: undefined,
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
exports.buildComprehensivePnlContext = buildComprehensivePnlContext;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Converters for PnL data between contract and SDK formats
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @dev Convert PnL percentage from contract precision to SDK format
|
|
6
|
+
* @param pnlPercentContract PnL percentage with 1e10 precision
|
|
7
|
+
* @returns PnL percentage as regular number (e.g., 10 = 10%)
|
|
8
|
+
*/
|
|
9
|
+
export declare const convertPnlPercent: (pnlPercentContract: bigint | number) => number;
|
|
10
|
+
/**
|
|
11
|
+
* @dev Convert PnL percentage from SDK format to contract precision
|
|
12
|
+
* @param pnlPercent PnL percentage as regular number
|
|
13
|
+
* @returns PnL percentage with 1e10 precision
|
|
14
|
+
*/
|
|
15
|
+
export declare const encodePnlPercent: (pnlPercent: number) => bigint;
|
|
16
|
+
/**
|
|
17
|
+
* @dev Convert collateral amount considering precision
|
|
18
|
+
* @param amount Amount in contract format
|
|
19
|
+
* @param collateralDecimals Collateral token decimals (6 or 18)
|
|
20
|
+
* @returns Amount as SDK float
|
|
21
|
+
*/
|
|
22
|
+
export declare const convertCollateralAmount: (amount: bigint | number, collateralDecimals: number) => number;
|
|
23
|
+
/**
|
|
24
|
+
* @dev Convert price from contract format to SDK format
|
|
25
|
+
* @param price Price with 1e10 precision
|
|
26
|
+
* @returns Price as SDK float
|
|
27
|
+
*/
|
|
28
|
+
export declare const convertPrice: (price: bigint | number) => number;
|
|
29
|
+
/**
|
|
30
|
+
* @dev Convert leverage from contract format to SDK format
|
|
31
|
+
* @param leverage Leverage with 1e3 precision
|
|
32
|
+
* @returns Leverage as SDK float (e.g., 10 = 10x)
|
|
33
|
+
*/
|
|
34
|
+
export declare const convertLeverage: (leverage: bigint | number) => number;
|
|
35
|
+
/**
|
|
36
|
+
* @dev Batch convert PnL results from contract format
|
|
37
|
+
* @param results Array of PnL results from contract
|
|
38
|
+
* @param collateralDecimals Collateral token decimals
|
|
39
|
+
* @returns Array of converted PnL results
|
|
40
|
+
*/
|
|
41
|
+
export declare const convertPnlResults: (results: Array<{
|
|
42
|
+
pnlCollateral: bigint;
|
|
43
|
+
pnlPercent: bigint;
|
|
44
|
+
}>, collateralDecimals: number) => Array<{
|
|
45
|
+
pnlCollateral: number;
|
|
46
|
+
pnlPercent: number;
|
|
47
|
+
}>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Converters for PnL data between contract and SDK formats
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.convertPnlResults = exports.convertLeverage = exports.convertPrice = exports.convertCollateralAmount = exports.encodePnlPercent = exports.convertPnlPercent = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Convert PnL percentage from contract precision to SDK format
|
|
9
|
+
* @param pnlPercentContract PnL percentage with 1e10 precision
|
|
10
|
+
* @returns PnL percentage as regular number (e.g., 10 = 10%)
|
|
11
|
+
*/
|
|
12
|
+
const convertPnlPercent = (pnlPercentContract) => {
|
|
13
|
+
const value = typeof pnlPercentContract === "bigint"
|
|
14
|
+
? Number(pnlPercentContract)
|
|
15
|
+
: pnlPercentContract;
|
|
16
|
+
// Contract uses 1e10 precision for percentages
|
|
17
|
+
return value / 1e10;
|
|
18
|
+
};
|
|
19
|
+
exports.convertPnlPercent = convertPnlPercent;
|
|
20
|
+
/**
|
|
21
|
+
* @dev Convert PnL percentage from SDK format to contract precision
|
|
22
|
+
* @param pnlPercent PnL percentage as regular number
|
|
23
|
+
* @returns PnL percentage with 1e10 precision
|
|
24
|
+
*/
|
|
25
|
+
const encodePnlPercent = (pnlPercent) => {
|
|
26
|
+
return BigInt(Math.round(pnlPercent * 1e10));
|
|
27
|
+
};
|
|
28
|
+
exports.encodePnlPercent = encodePnlPercent;
|
|
29
|
+
/**
|
|
30
|
+
* @dev Convert collateral amount considering precision
|
|
31
|
+
* @param amount Amount in contract format
|
|
32
|
+
* @param collateralDecimals Collateral token decimals (6 or 18)
|
|
33
|
+
* @returns Amount as SDK float
|
|
34
|
+
*/
|
|
35
|
+
const convertCollateralAmount = (amount, collateralDecimals) => {
|
|
36
|
+
const value = typeof amount === "bigint" ? Number(amount) : amount;
|
|
37
|
+
return value / Math.pow(10, collateralDecimals);
|
|
38
|
+
};
|
|
39
|
+
exports.convertCollateralAmount = convertCollateralAmount;
|
|
40
|
+
/**
|
|
41
|
+
* @dev Convert price from contract format to SDK format
|
|
42
|
+
* @param price Price with 1e10 precision
|
|
43
|
+
* @returns Price as SDK float
|
|
44
|
+
*/
|
|
45
|
+
const convertPrice = (price) => {
|
|
46
|
+
const value = typeof price === "bigint" ? Number(price) : price;
|
|
47
|
+
return value / 1e10;
|
|
48
|
+
};
|
|
49
|
+
exports.convertPrice = convertPrice;
|
|
50
|
+
/**
|
|
51
|
+
* @dev Convert leverage from contract format to SDK format
|
|
52
|
+
* @param leverage Leverage with 1e3 precision
|
|
53
|
+
* @returns Leverage as SDK float (e.g., 10 = 10x)
|
|
54
|
+
*/
|
|
55
|
+
const convertLeverage = (leverage) => {
|
|
56
|
+
const value = typeof leverage === "bigint" ? Number(leverage) : leverage;
|
|
57
|
+
return value / 1e3;
|
|
58
|
+
};
|
|
59
|
+
exports.convertLeverage = convertLeverage;
|
|
60
|
+
/**
|
|
61
|
+
* @dev Batch convert PnL results from contract format
|
|
62
|
+
* @param results Array of PnL results from contract
|
|
63
|
+
* @param collateralDecimals Collateral token decimals
|
|
64
|
+
* @returns Array of converted PnL results
|
|
65
|
+
*/
|
|
66
|
+
const convertPnlResults = (results, collateralDecimals) => {
|
|
67
|
+
return results.map(result => ({
|
|
68
|
+
pnlCollateral: (0, exports.convertCollateralAmount)(result.pnlCollateral, collateralDecimals),
|
|
69
|
+
pnlPercent: (0, exports.convertPnlPercent)(result.pnlPercent),
|
|
70
|
+
}));
|
|
71
|
+
};
|
|
72
|
+
exports.convertPnlResults = convertPnlResults;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev PnL calculation module
|
|
3
|
+
* @dev Provides functions matching v10 contract implementations
|
|
4
|
+
*/
|
|
5
|
+
import { Trade, TradeInfo, LiquidationParams, Fee, GlobalTradeFeeParams, TradeFeesData } from "../types";
|
|
6
|
+
import { ComprehensivePnlResult, GetComprehensivePnlContext } from "./types";
|
|
7
|
+
import { BorrowingFee } from "../fees/borrowing";
|
|
8
|
+
import { ContractsVersion } from "../../contracts/types";
|
|
9
|
+
import { UnifiedPairOi } from "src/markets";
|
|
10
|
+
/**
|
|
11
|
+
* @dev Gets trade realized PnL components from TradeFeesData
|
|
12
|
+
* @dev Mirrors contract's getTradeRealizedPnlCollateral function
|
|
13
|
+
* @param tradeFeesData Trade fees data containing realized components
|
|
14
|
+
* @returns Tuple of [realizedPnlCollateral, realizedTradingFeesCollateral, totalRealizedPnlCollateral]
|
|
15
|
+
*/
|
|
16
|
+
export declare const getTradeRealizedPnlCollateral: (tradeFeesData: TradeFeesData) => {
|
|
17
|
+
realizedPnlCollateral: number;
|
|
18
|
+
realizedTradingFeesCollateral: number;
|
|
19
|
+
totalRealizedPnlCollateral: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* @dev Calculates PnL percentage for a position
|
|
23
|
+
* @dev Mirrors contract's getPnlPercent function
|
|
24
|
+
* @param openPrice Trade open price
|
|
25
|
+
* @param currentPrice Current market price
|
|
26
|
+
* @param long Whether position is long
|
|
27
|
+
* @param leverage Position leverage
|
|
28
|
+
* @returns PnL percentage (e.g., 10 = 10% profit, -50 = 50% loss)
|
|
29
|
+
*/
|
|
30
|
+
export declare const getPnlPercent: (openPrice: number, currentPrice: number, long: boolean, leverage: number) => number;
|
|
31
|
+
/**
|
|
32
|
+
* @dev Calculates trade value from collateral and PnL
|
|
33
|
+
* @dev Mirrors contract's getTradeValuePure function
|
|
34
|
+
* @param collateral Trade collateral amount
|
|
35
|
+
* @param pnlPercent PnL percentage
|
|
36
|
+
* @param totalFees Total fees to deduct
|
|
37
|
+
* @returns Trade value after PnL and fees
|
|
38
|
+
*/
|
|
39
|
+
export declare const getTradeValue: (collateral: number, pnlPercent: number, totalFees: number) => number;
|
|
40
|
+
/**
|
|
41
|
+
* @dev Comprehensive PnL calculation including all fees
|
|
42
|
+
* @param trade The trade to calculate PnL for
|
|
43
|
+
* @param marketPrice Current market price (without price impact)
|
|
44
|
+
* @param executionPrice Price after all impacts (spread, skew, volume)
|
|
45
|
+
* @param tradeInfo Trade info with version and timestamps
|
|
46
|
+
* @param context Context with all fee parameters
|
|
47
|
+
* @returns Detailed PnL breakdown
|
|
48
|
+
*/
|
|
49
|
+
export declare const getComprehensivePnl: (trade: Trade, marketPrice: number, executionPrice: number, tradeInfo: TradeInfo, context: GetComprehensivePnlContext) => ComprehensivePnlResult;
|
|
50
|
+
/**
|
|
51
|
+
* @dev Legacy getPnl function for backward compatibility
|
|
52
|
+
* @deprecated Use getComprehensivePnl for new implementations
|
|
53
|
+
*/
|
|
54
|
+
export type GetPnlContext = {
|
|
55
|
+
currentBlock: number;
|
|
56
|
+
groups: BorrowingFee.Group[];
|
|
57
|
+
pairs: BorrowingFee.Pair[];
|
|
58
|
+
collateralPriceUsd: number | undefined;
|
|
59
|
+
contractsVersion: ContractsVersion | undefined;
|
|
60
|
+
feeMultiplier: number | undefined;
|
|
61
|
+
fee: Fee;
|
|
62
|
+
globalTradeFeeParams: GlobalTradeFeeParams;
|
|
63
|
+
traderFeeMultiplier?: number;
|
|
64
|
+
pairOis: UnifiedPairOi[];
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* @dev Legacy PnL calculation function
|
|
68
|
+
* @deprecated Use getComprehensivePnl for more comprehensive calculations
|
|
69
|
+
* @param price Current price
|
|
70
|
+
* @param trade Trade object
|
|
71
|
+
* @param tradeInfo Trade info (not used in legacy implementation)
|
|
72
|
+
* @param initialAccFees Initial accumulated fees
|
|
73
|
+
* @param liquidationParams Liquidation parameters
|
|
74
|
+
* @param useFees Whether to include fees
|
|
75
|
+
* @param context Context with fee calculation parameters
|
|
76
|
+
* @returns [pnlCollateral, pnlPercentage] or undefined if no price
|
|
77
|
+
*/
|
|
78
|
+
export declare const getPnl: (price: number | undefined, trade: Trade, _tradeInfo: TradeInfo, initialAccFees: BorrowingFee.InitialAccFees, liquidationParams: LiquidationParams, useFees: boolean, context: GetPnlContext) => number[] | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* @dev Calculates the price needed to achieve a target PnL percentage
|
|
81
|
+
* @param targetPnlPercent The target PnL percentage (e.g., 50 for 50% profit, -25 for 25% loss)
|
|
82
|
+
* @param trade The trade to calculate for
|
|
83
|
+
* @param tradeInfo Trade info with timestamps
|
|
84
|
+
* @param context Context with fee calculation parameters
|
|
85
|
+
* @param netPnl Whether to include closing fees in the calculation
|
|
86
|
+
* @returns The price that would result in the target PnL percentage
|
|
87
|
+
*/
|
|
88
|
+
export declare const getPriceForTargetPnlPercentage: (targetPnlPercent: number, trade: Trade, tradeInfo: TradeInfo, context: GetComprehensivePnlContext, netPnl?: boolean) => number;
|
|
89
|
+
export * from "./types";
|
|
90
|
+
export * from "./converter";
|
|
91
|
+
export * from "./builder";
|