@gainsnetwork/sdk 0.0.0-v10.rc1 → 0.0.0-v10.rc3
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.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +2 -0
- package/lib/backend/index.js +18 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +312 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +30 -0
- package/lib/backend/tradingVariables/converter.js +329 -0
- package/lib/backend/tradingVariables/index.d.ts +3 -0
- package/lib/backend/tradingVariables/index.js +78 -0
- package/lib/backend/tradingVariables/types.d.ts +109 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.d.ts +8 -1
- package/lib/constants.js +8 -1
- package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +75 -0
- package/lib/contracts/fetch/fees/borrowingFeesV2.js +193 -0
- package/lib/contracts/fetch/fees/fundingFees.d.ts +66 -0
- package/lib/contracts/fetch/fees/fundingFees.js +150 -0
- package/lib/contracts/fetch/priceImpact/skew.d.ts +63 -0
- package/lib/contracts/fetch/priceImpact/skew.js +168 -0
- 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/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/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/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/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 +7 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -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/index.d.ts +2 -0
- package/lib/markets/index.js +2 -0
- package/lib/markets/oi/converter.d.ts +63 -0
- package/lib/markets/oi/converter.js +103 -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 +10 -0
- package/lib/markets/oi/index.js +37 -0
- package/lib/markets/oi/types.d.ts +82 -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/trade/fees/borrowing/index.d.ts +22 -1
- package/lib/trade/fees/borrowing/index.js +39 -13
- package/lib/trade/fees/borrowingV2/converter.d.ts +66 -0
- package/lib/trade/fees/borrowingV2/converter.js +121 -0
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/fetcher.js +193 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +60 -0
- package/lib/trade/fees/borrowingV2/index.js +140 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +79 -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 +110 -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 +136 -0
- package/lib/trade/fees/fundingFees/index.js +326 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +7 -2
- package/lib/trade/fees/index.js +67 -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/converter.d.ts +30 -0
- package/lib/trade/fees/trading/converter.js +43 -0
- package/lib/trade/fees/trading/index.d.ts +54 -0
- package/lib/trade/fees/trading/index.js +147 -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 +3 -2
- package/lib/trade/index.js +3 -2
- package/lib/trade/liquidation/converter.d.ts +23 -0
- package/lib/trade/liquidation/converter.js +46 -0
- package/lib/trade/liquidation/index.d.ts +31 -0
- package/lib/trade/liquidation/index.js +187 -0
- package/lib/trade/liquidation/types.d.ts +44 -0
- package/lib/trade/liquidation/types.js +2 -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 +86 -0
- package/lib/trade/pnl/index.js +201 -0
- package/lib/trade/pnl/types.d.ts +86 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/index.d.ts +21 -0
- package/lib/trade/priceImpact/close/index.js +131 -0
- package/lib/trade/priceImpact/close/types.d.ts +43 -0
- package/lib/trade/priceImpact/close/types.js +5 -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 +107 -0
- package/lib/trade/priceImpact/cumulVol/index.js +228 -0
- package/lib/trade/priceImpact/index.d.ts +12 -0
- package/lib/trade/priceImpact/index.js +59 -0
- package/lib/trade/priceImpact/open/index.d.ts +22 -0
- package/lib/trade/priceImpact/open/index.js +76 -0
- package/lib/trade/priceImpact/open/types.d.ts +41 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +77 -0
- package/lib/trade/priceImpact/skew/converter.js +171 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +63 -0
- package/lib/trade/priceImpact/skew/fetcher.js +168 -0
- package/lib/trade/priceImpact/skew/index.d.ts +58 -0
- package/lib/trade/priceImpact/skew/index.js +179 -0
- package/lib/trade/priceImpact/skew/types.d.ts +55 -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 +34 -9
- package/lib/trade/types.js +7 -0
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/package.json +2 -1
|
@@ -0,0 +1,187 @@
|
|
|
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.getTradeLiquidationPriceSimple = exports.getLiqPnlThresholdP = exports.getLiquidationPrice = exports.getTradeLiquidationPrice = void 0;
|
|
21
|
+
const types_1 = require("../../contracts/types");
|
|
22
|
+
const __1 = require("..");
|
|
23
|
+
/**
|
|
24
|
+
* @dev Wrapper function that mirrors the contract's getTradeLiquidationPrice signature
|
|
25
|
+
* @dev This is the main entry point matching the contract interface
|
|
26
|
+
* @param input Liquidation price input parameters
|
|
27
|
+
* @param context Additional context for SDK calculations
|
|
28
|
+
* @returns Liquidation price
|
|
29
|
+
*/
|
|
30
|
+
const getTradeLiquidationPrice = (input, context) => {
|
|
31
|
+
// Create trade object from input
|
|
32
|
+
const trade = {
|
|
33
|
+
user: input.trader,
|
|
34
|
+
index: input.index,
|
|
35
|
+
pairIndex: input.pairIndex,
|
|
36
|
+
leverage: input.leverage,
|
|
37
|
+
long: input.long,
|
|
38
|
+
isOpen: true,
|
|
39
|
+
collateralIndex: input.collateralIndex,
|
|
40
|
+
tradeType: 0,
|
|
41
|
+
collateralAmount: input.collateral,
|
|
42
|
+
openPrice: input.openPrice,
|
|
43
|
+
sl: 0,
|
|
44
|
+
tp: 0,
|
|
45
|
+
isCounterTrade: input.isCounterTrade,
|
|
46
|
+
};
|
|
47
|
+
// Merge input params into context
|
|
48
|
+
const fullContext = Object.assign(Object.assign({}, context), { currentPairPrice: input.currentPairPrice, isCounterTrade: input.isCounterTrade, partialCloseMultiplier: input.partialCloseMultiplier, additionalFeeCollateral: input.additionalFeeCollateral, beforeOpened: input.beforeOpened, liquidationParams: input.liquidationParams });
|
|
49
|
+
// Call the existing implementation
|
|
50
|
+
return (0, exports.getLiquidationPrice)(trade, context.fee, context.initialAccFees || { accPairFee: 0, accGroupFee: 0, block: 0 }, fullContext);
|
|
51
|
+
};
|
|
52
|
+
exports.getTradeLiquidationPrice = getTradeLiquidationPrice;
|
|
53
|
+
const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
54
|
+
var _a, _b, _c, _d, _e, _f;
|
|
55
|
+
// Ensure initialAccFees is in context
|
|
56
|
+
if (!context.initialAccFees) {
|
|
57
|
+
context = Object.assign(Object.assign({}, context), { initialAccFees });
|
|
58
|
+
}
|
|
59
|
+
// 1. Calculate liquidation fees
|
|
60
|
+
const closingFee = (0, __1.getTotalTradeLiqFeesCollateral)(0, // collateralIndex not used in calculation
|
|
61
|
+
trade.user, trade.pairIndex, trade.collateralAmount, context);
|
|
62
|
+
// 2. Calculate holding fees and realized PnL
|
|
63
|
+
let holdingFeesTotal = 0;
|
|
64
|
+
let totalRealizedPnlCollateral = 0;
|
|
65
|
+
if (!context.beforeOpened &&
|
|
66
|
+
context.tradeFeesData &&
|
|
67
|
+
context.currentPairPrice) {
|
|
68
|
+
// V10 data available - calculate full holding fees
|
|
69
|
+
// Create a minimal tradeInfo from context
|
|
70
|
+
const tradeInfo = {
|
|
71
|
+
contractsVersion: (_a = context.contractsVersion) !== null && _a !== void 0 ? _a : types_1.ContractsVersion.V10,
|
|
72
|
+
createdBlock: 0,
|
|
73
|
+
tpLastUpdatedBlock: 0,
|
|
74
|
+
slLastUpdatedBlock: 0,
|
|
75
|
+
maxSlippageP: 0,
|
|
76
|
+
lastOiUpdateTs: 0,
|
|
77
|
+
collateralPriceUsd: (_b = context.collateralPriceUsd) !== null && _b !== void 0 ? _b : 0,
|
|
78
|
+
lastPosIncreaseBlock: 0,
|
|
79
|
+
};
|
|
80
|
+
const holdingFees = (0, __1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeFeesData, context.currentPairPrice, context);
|
|
81
|
+
holdingFeesTotal = holdingFees.totalFeeCollateral;
|
|
82
|
+
// Calculate total realized PnL (realized PnL minus realized trading fees)
|
|
83
|
+
totalRealizedPnlCollateral =
|
|
84
|
+
context.tradeFeesData.realizedPnlCollateral -
|
|
85
|
+
context.tradeFeesData.realizedTradingFeesCollateral;
|
|
86
|
+
}
|
|
87
|
+
else if (!context.beforeOpened) {
|
|
88
|
+
// Markets using v1 borrowing fees model
|
|
89
|
+
holdingFeesTotal = (0, __1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
90
|
+
}
|
|
91
|
+
// 3. Apply unified formula for all trades
|
|
92
|
+
const partialCloseMultiplier = (_c = context.partialCloseMultiplier) !== null && _c !== void 0 ? _c : 1;
|
|
93
|
+
const additionalFeeCollateral = (_d = context.additionalFeeCollateral) !== null && _d !== void 0 ? _d : 0;
|
|
94
|
+
const totalFeesCollateral = closingFee +
|
|
95
|
+
(holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
|
|
96
|
+
additionalFeeCollateral;
|
|
97
|
+
// 4. Calculate liquidation threshold
|
|
98
|
+
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
|
|
99
|
+
// 5. Calculate liquidation price distance
|
|
100
|
+
const collateralLiqNegativePnl = trade.collateralAmount * liqThresholdP;
|
|
101
|
+
let liqPriceDistance = (trade.openPrice * (collateralLiqNegativePnl - totalFeesCollateral)) /
|
|
102
|
+
trade.collateralAmount /
|
|
103
|
+
trade.leverage;
|
|
104
|
+
// 6. Apply closing spread for v9.2+
|
|
105
|
+
if ((context === null || context === void 0 ? void 0 : context.contractsVersion) !== undefined &&
|
|
106
|
+
context.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
107
|
+
((((_e = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _e === void 0 ? void 0 : _e.maxLiqSpreadP) !== undefined &&
|
|
108
|
+
context.liquidationParams.maxLiqSpreadP > 0) ||
|
|
109
|
+
(((_f = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _f === void 0 ? void 0 : _f.fixedSpreadP) !== undefined &&
|
|
110
|
+
context.userPriceImpact.fixedSpreadP > 0))) {
|
|
111
|
+
const closingSpreadP = (0, __1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams, context.userPriceImpact);
|
|
112
|
+
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
113
|
+
}
|
|
114
|
+
// 7. Calculate final liquidation price
|
|
115
|
+
return trade.long
|
|
116
|
+
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
117
|
+
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
118
|
+
};
|
|
119
|
+
exports.getLiquidationPrice = getLiquidationPrice;
|
|
120
|
+
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
121
|
+
if (liquidationParams === undefined ||
|
|
122
|
+
leverage === undefined ||
|
|
123
|
+
liquidationParams.maxLiqSpreadP === 0 ||
|
|
124
|
+
liquidationParams.startLiqThresholdP === 0 ||
|
|
125
|
+
liquidationParams.endLiqThresholdP === 0 ||
|
|
126
|
+
liquidationParams.startLeverage === 0 ||
|
|
127
|
+
liquidationParams.endLeverage === 0) {
|
|
128
|
+
return 0.9;
|
|
129
|
+
}
|
|
130
|
+
if (leverage < liquidationParams.startLeverage) {
|
|
131
|
+
return liquidationParams.startLiqThresholdP;
|
|
132
|
+
}
|
|
133
|
+
if (leverage > liquidationParams.endLeverage) {
|
|
134
|
+
return liquidationParams.endLiqThresholdP;
|
|
135
|
+
}
|
|
136
|
+
if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
|
|
137
|
+
return liquidationParams.endLiqThresholdP;
|
|
138
|
+
}
|
|
139
|
+
return (liquidationParams.startLiqThresholdP -
|
|
140
|
+
((leverage - liquidationParams.startLeverage) *
|
|
141
|
+
(liquidationParams.startLiqThresholdP -
|
|
142
|
+
liquidationParams.endLiqThresholdP)) /
|
|
143
|
+
(liquidationParams.endLeverage - liquidationParams.startLeverage));
|
|
144
|
+
};
|
|
145
|
+
exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
|
|
146
|
+
/**
|
|
147
|
+
* @dev Simplified wrapper for getTradeLiquidationPrice
|
|
148
|
+
* @dev Mirrors the contract's simplified overload
|
|
149
|
+
* @param trade The trade to calculate liquidation price for
|
|
150
|
+
* @param additionalFeeCollateral Additional fees to consider
|
|
151
|
+
* @param currentPairPrice Current pair price
|
|
152
|
+
* @param context Context with all required data
|
|
153
|
+
* @returns Liquidation price
|
|
154
|
+
*/
|
|
155
|
+
const getTradeLiquidationPriceSimple = (trade, additionalFeeCollateral, currentPairPrice, context) => {
|
|
156
|
+
const input = {
|
|
157
|
+
collateralIndex: trade.collateralIndex,
|
|
158
|
+
trader: trade.user,
|
|
159
|
+
pairIndex: trade.pairIndex,
|
|
160
|
+
index: trade.index,
|
|
161
|
+
openPrice: trade.openPrice,
|
|
162
|
+
long: trade.long,
|
|
163
|
+
collateral: trade.collateralAmount,
|
|
164
|
+
leverage: trade.leverage,
|
|
165
|
+
additionalFeeCollateral,
|
|
166
|
+
liquidationParams: context.liquidationParams || {
|
|
167
|
+
maxLiqSpreadP: 0,
|
|
168
|
+
startLiqThresholdP: 0.9,
|
|
169
|
+
endLiqThresholdP: 0.9,
|
|
170
|
+
startLeverage: 0,
|
|
171
|
+
endLeverage: 0,
|
|
172
|
+
},
|
|
173
|
+
currentPairPrice,
|
|
174
|
+
isCounterTrade: trade.isCounterTrade || false,
|
|
175
|
+
partialCloseMultiplier: 1,
|
|
176
|
+
beforeOpened: false,
|
|
177
|
+
};
|
|
178
|
+
return (0, exports.getTradeLiquidationPrice)(input, context);
|
|
179
|
+
};
|
|
180
|
+
exports.getTradeLiquidationPriceSimple = getTradeLiquidationPriceSimple;
|
|
181
|
+
// Converters
|
|
182
|
+
var converter_1 = require("./converter");
|
|
183
|
+
Object.defineProperty(exports, "convertLiquidationParams", { enumerable: true, get: function () { return converter_1.convertLiquidationParams; } });
|
|
184
|
+
Object.defineProperty(exports, "convertLiquidationParamsArray", { enumerable: true, get: function () { return converter_1.convertLiquidationParamsArray; } });
|
|
185
|
+
Object.defineProperty(exports, "encodeLiquidationParams", { enumerable: true, get: function () { return converter_1.encodeLiquidationParams; } });
|
|
186
|
+
// Types
|
|
187
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { GetBorrowingFeeContext, BorrowingFee } from "./../fees";
|
|
2
|
+
import { GetLiquidationFeesContext } from "./../fees/trading";
|
|
3
|
+
import { BorrowingFeeV2 } from "./../fees/borrowingV2";
|
|
4
|
+
import { LiquidationParams, UserPriceImpact, TradeFeesData } from "./../types";
|
|
5
|
+
import { ContractsVersion } from "../../contracts/types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Input parameters for getTradeLiquidationPrice
|
|
8
|
+
* @dev Mirrors the contract's LiqPriceInput struct
|
|
9
|
+
*/
|
|
10
|
+
export type LiqPriceInput = {
|
|
11
|
+
collateralIndex: number;
|
|
12
|
+
trader: string;
|
|
13
|
+
pairIndex: number;
|
|
14
|
+
index: number;
|
|
15
|
+
openPrice: number;
|
|
16
|
+
long: boolean;
|
|
17
|
+
collateral: number;
|
|
18
|
+
leverage: number;
|
|
19
|
+
additionalFeeCollateral: number;
|
|
20
|
+
liquidationParams: LiquidationParams;
|
|
21
|
+
currentPairPrice: number;
|
|
22
|
+
isCounterTrade: boolean;
|
|
23
|
+
partialCloseMultiplier: number;
|
|
24
|
+
beforeOpened: boolean;
|
|
25
|
+
};
|
|
26
|
+
export type GetLiquidationPriceContext = GetBorrowingFeeContext & BorrowingFeeV2.GetBorrowingFeeV2Context & GetLiquidationFeesContext & {
|
|
27
|
+
liquidationParams: LiquidationParams | undefined;
|
|
28
|
+
pairSpreadP: number | undefined;
|
|
29
|
+
collateralPriceUsd: number | undefined;
|
|
30
|
+
contractsVersion: ContractsVersion | undefined;
|
|
31
|
+
userPriceImpact?: UserPriceImpact | undefined;
|
|
32
|
+
currentPairPrice?: number;
|
|
33
|
+
isCounterTrade?: boolean;
|
|
34
|
+
tradeFeesData?: TradeFeesData;
|
|
35
|
+
partialCloseMultiplier?: number;
|
|
36
|
+
additionalFeeCollateral?: number;
|
|
37
|
+
beforeOpened?: boolean;
|
|
38
|
+
initialAccFees?: BorrowingFee.InitialAccFees;
|
|
39
|
+
fundingParams?: Record<number, Record<number, any>>;
|
|
40
|
+
fundingData?: Record<number, Record<number, any>>;
|
|
41
|
+
pairOiAfterV10?: Record<number, Record<number, any>>;
|
|
42
|
+
netExposureToken?: Record<number, Record<number, number>>;
|
|
43
|
+
netExposureUsd?: Record<number, Record<number, number>>;
|
|
44
|
+
};
|
|
@@ -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,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev PnL calculation module
|
|
3
|
+
* @dev Provides functions matching v10 contract implementations
|
|
4
|
+
*/
|
|
5
|
+
import { Trade, TradeInfo, LiquidationParams } from "../types";
|
|
6
|
+
import { ComprehensivePnlResult } from "./types";
|
|
7
|
+
import { GetBorrowingFeeContext, BorrowingFee } from "../fees";
|
|
8
|
+
import { GetTradeFeesContext } from "../fees/trading";
|
|
9
|
+
import { ContractsVersion } from "../../contracts/types";
|
|
10
|
+
/**
|
|
11
|
+
* @dev Calculates PnL percentage for a position
|
|
12
|
+
* @dev Mirrors contract's getPnlPercent function
|
|
13
|
+
* @param openPrice Trade open price
|
|
14
|
+
* @param currentPrice Current market price
|
|
15
|
+
* @param long Whether position is long
|
|
16
|
+
* @param leverage Position leverage
|
|
17
|
+
* @returns PnL percentage (e.g., 10 = 10% profit, -50 = 50% loss)
|
|
18
|
+
*/
|
|
19
|
+
export declare const getPnlPercent: (openPrice: number, currentPrice: number, long: boolean, leverage: number) => number;
|
|
20
|
+
/**
|
|
21
|
+
* @dev Calculates trade value from collateral and PnL
|
|
22
|
+
* @dev Mirrors contract's getTradeValuePure function
|
|
23
|
+
* @param collateral Trade collateral amount
|
|
24
|
+
* @param pnlPercent PnL percentage
|
|
25
|
+
* @param totalFees Total fees to deduct
|
|
26
|
+
* @returns Trade value after PnL and fees
|
|
27
|
+
*/
|
|
28
|
+
export declare const getTradeValue: (collateral: number, pnlPercent: number, totalFees: number) => number;
|
|
29
|
+
/**
|
|
30
|
+
* @dev Context for comprehensive PnL calculations
|
|
31
|
+
*/
|
|
32
|
+
export type GetComprehensivePnlContext = GetBorrowingFeeContext & GetTradeFeesContext & {
|
|
33
|
+
collateralPriceUsd: number;
|
|
34
|
+
contractsVersion: ContractsVersion;
|
|
35
|
+
currentTimestamp: number;
|
|
36
|
+
initialAccFees?: BorrowingFee.InitialAccFees;
|
|
37
|
+
borrowingProviderContext?: any;
|
|
38
|
+
fundingParams?: any;
|
|
39
|
+
fundingData?: any;
|
|
40
|
+
pairOiAfterV10?: any;
|
|
41
|
+
netExposureToken?: any;
|
|
42
|
+
netExposureUsd?: any;
|
|
43
|
+
tradeFeesData?: {
|
|
44
|
+
initialAccFundingFeeP: number;
|
|
45
|
+
initialAccBorrowingFeeP: number;
|
|
46
|
+
realizedPnlCollateral: number;
|
|
47
|
+
realizedTradingFeesCollateral: number;
|
|
48
|
+
manuallyRealizedNegativePnlCollateral?: number;
|
|
49
|
+
alreadyTransferredNegativePnlCollateral?: number;
|
|
50
|
+
virtualAvailableCollateralInDiamond?: number;
|
|
51
|
+
};
|
|
52
|
+
liquidationParams?: LiquidationParams;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* @dev Comprehensive PnL calculation including all fees
|
|
56
|
+
* @param trade The trade to calculate PnL for
|
|
57
|
+
* @param currentPrice Current market price
|
|
58
|
+
* @param tradeInfo Trade info with version and timestamps
|
|
59
|
+
* @param context Context with all fee parameters
|
|
60
|
+
* @returns Detailed PnL breakdown
|
|
61
|
+
*/
|
|
62
|
+
export declare const getComprehensivePnl: (trade: Trade, currentPrice: number, tradeInfo: TradeInfo, context: GetComprehensivePnlContext) => ComprehensivePnlResult;
|
|
63
|
+
/**
|
|
64
|
+
* @dev Legacy getPnl function for backward compatibility
|
|
65
|
+
* @deprecated Use getComprehensivePnl for new implementations
|
|
66
|
+
*/
|
|
67
|
+
export type GetPnlContext = GetBorrowingFeeContext & GetTradeFeesContext & {
|
|
68
|
+
collateralPriceUsd: number | undefined;
|
|
69
|
+
contractsVersion: ContractsVersion | undefined;
|
|
70
|
+
feeMultiplier: number | undefined;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* @dev Legacy PnL calculation function
|
|
74
|
+
* @deprecated Use getComprehensivePnl for more comprehensive calculations
|
|
75
|
+
* @param price Current price
|
|
76
|
+
* @param trade Trade object
|
|
77
|
+
* @param tradeInfo Trade info (not used in legacy implementation)
|
|
78
|
+
* @param initialAccFees Initial accumulated fees
|
|
79
|
+
* @param liquidationParams Liquidation parameters
|
|
80
|
+
* @param useFees Whether to include fees
|
|
81
|
+
* @param context Context with fee calculation parameters
|
|
82
|
+
* @returns [pnlCollateral, pnlPercentage] or undefined if no price
|
|
83
|
+
*/
|
|
84
|
+
export declare const getPnl: (price: number | undefined, trade: Trade, _tradeInfo: TradeInfo, initialAccFees: BorrowingFee.InitialAccFees, liquidationParams: LiquidationParams, useFees: boolean, context: GetPnlContext) => number[] | undefined;
|
|
85
|
+
export * from "./types";
|
|
86
|
+
export * from "./converter";
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev PnL calculation module
|
|
4
|
+
* @dev Provides functions matching v10 contract implementations
|
|
5
|
+
*/
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
18
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.getPnl = exports.getComprehensivePnl = exports.getTradeValue = exports.getPnlPercent = void 0;
|
|
22
|
+
const fees_1 = require("../fees");
|
|
23
|
+
const borrowingV2_1 = require("../fees/borrowingV2");
|
|
24
|
+
const fundingFees_1 = require("../fees/fundingFees");
|
|
25
|
+
const trading_1 = require("../fees/trading");
|
|
26
|
+
const liquidation_1 = require("../liquidation");
|
|
27
|
+
const types_1 = require("../../contracts/types");
|
|
28
|
+
/**
|
|
29
|
+
* @dev Calculates PnL percentage for a position
|
|
30
|
+
* @dev Mirrors contract's getPnlPercent function
|
|
31
|
+
* @param openPrice Trade open price
|
|
32
|
+
* @param currentPrice Current market price
|
|
33
|
+
* @param long Whether position is long
|
|
34
|
+
* @param leverage Position leverage
|
|
35
|
+
* @returns PnL percentage (e.g., 10 = 10% profit, -50 = 50% loss)
|
|
36
|
+
*/
|
|
37
|
+
const getPnlPercent = (openPrice, currentPrice, long, leverage) => {
|
|
38
|
+
if (openPrice === 0)
|
|
39
|
+
return -100;
|
|
40
|
+
const priceDiff = long ? currentPrice - openPrice : openPrice - currentPrice;
|
|
41
|
+
const pnlPercent = (priceDiff / openPrice) * 100 * leverage;
|
|
42
|
+
// Cap at -100% loss
|
|
43
|
+
return Math.max(pnlPercent, -100);
|
|
44
|
+
};
|
|
45
|
+
exports.getPnlPercent = getPnlPercent;
|
|
46
|
+
/**
|
|
47
|
+
* @dev Calculates trade value from collateral and PnL
|
|
48
|
+
* @dev Mirrors contract's getTradeValuePure function
|
|
49
|
+
* @param collateral Trade collateral amount
|
|
50
|
+
* @param pnlPercent PnL percentage
|
|
51
|
+
* @param totalFees Total fees to deduct
|
|
52
|
+
* @returns Trade value after PnL and fees
|
|
53
|
+
*/
|
|
54
|
+
const getTradeValue = (collateral, pnlPercent, totalFees) => {
|
|
55
|
+
const pnlCollateral = collateral * (pnlPercent / 100);
|
|
56
|
+
const value = collateral + pnlCollateral - totalFees;
|
|
57
|
+
return Math.max(0, value);
|
|
58
|
+
};
|
|
59
|
+
exports.getTradeValue = getTradeValue;
|
|
60
|
+
/**
|
|
61
|
+
* @dev Comprehensive PnL calculation including all fees
|
|
62
|
+
* @param trade The trade to calculate PnL for
|
|
63
|
+
* @param currentPrice Current market price
|
|
64
|
+
* @param tradeInfo Trade info with version and timestamps
|
|
65
|
+
* @param context Context with all fee parameters
|
|
66
|
+
* @returns Detailed PnL breakdown
|
|
67
|
+
*/
|
|
68
|
+
const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
|
|
69
|
+
// Calculate base PnL percentage
|
|
70
|
+
let pnlPercent = (0, exports.getPnlPercent)(trade.openPrice, currentPrice, trade.long, trade.leverage);
|
|
71
|
+
// Calculate position size
|
|
72
|
+
const positionSizeCollateral = trade.collateralAmount * trade.leverage;
|
|
73
|
+
// Initialize fees
|
|
74
|
+
let borrowingFeeV1 = 0;
|
|
75
|
+
let borrowingFeeV2 = 0;
|
|
76
|
+
let fundingFee = 0;
|
|
77
|
+
// Calculate holding fees based on version
|
|
78
|
+
if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
|
|
79
|
+
context.tradeFeesData) {
|
|
80
|
+
// V10: Use aggregated holding fees function
|
|
81
|
+
const holdingFees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeFeesData, // Cast to handle partial type
|
|
82
|
+
currentPrice, context);
|
|
83
|
+
fundingFee = holdingFees.fundingFeeCollateral;
|
|
84
|
+
borrowingFeeV2 = holdingFees.borrowingFeeCollateral;
|
|
85
|
+
borrowingFeeV1 = holdingFees.borrowingFeeCollateral_old;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
// Pre-v10: Calculate fees individually
|
|
89
|
+
// V1 borrowing fees (still used by some markets)
|
|
90
|
+
if (context.initialAccFees) {
|
|
91
|
+
borrowingFeeV1 = (0, fees_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFees, context);
|
|
92
|
+
}
|
|
93
|
+
// V2 borrowing fees
|
|
94
|
+
if (context.tradeFeesData && context.borrowingProviderContext) {
|
|
95
|
+
borrowingFeeV2 = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
|
|
96
|
+
positionSizeCollateral,
|
|
97
|
+
openPrice: trade.openPrice,
|
|
98
|
+
collateralIndex: trade.collateralIndex,
|
|
99
|
+
pairIndex: trade.pairIndex,
|
|
100
|
+
currentPairPrice: currentPrice,
|
|
101
|
+
initialAccBorrowingFeeP: context.tradeFeesData.initialAccBorrowingFeeP,
|
|
102
|
+
currentTimestamp: context.currentTimestamp,
|
|
103
|
+
}, context.borrowingProviderContext);
|
|
104
|
+
}
|
|
105
|
+
// Funding fees (v10+)
|
|
106
|
+
if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
|
|
107
|
+
context.tradeFeesData) {
|
|
108
|
+
fundingFee = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, context.tradeFeesData, // Cast to handle partial type
|
|
109
|
+
currentPrice, context);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Calculate closing fees
|
|
113
|
+
const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(trade.collateralIndex, trade.user, trade.pairIndex, positionSizeCollateral, trade.isCounterTrade || false, context);
|
|
114
|
+
// Total fees
|
|
115
|
+
const totalFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee + closingFee;
|
|
116
|
+
// Check liquidation
|
|
117
|
+
const liquidationThreshold = context.liquidationParams
|
|
118
|
+
? (0, liquidation_1.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage) * -100
|
|
119
|
+
: -90; // Default 90% loss
|
|
120
|
+
const isLiquidated = pnlPercent <= liquidationThreshold;
|
|
121
|
+
// If liquidated, set PnL to -100%
|
|
122
|
+
if (isLiquidated) {
|
|
123
|
+
pnlPercent = -100;
|
|
124
|
+
}
|
|
125
|
+
// Calculate final trade value
|
|
126
|
+
const tradeValue = (0, exports.getTradeValue)(trade.collateralAmount, pnlPercent, totalFees);
|
|
127
|
+
// Calculate PnL in collateral
|
|
128
|
+
const pnlCollateral = trade.collateralAmount * (pnlPercent / 100);
|
|
129
|
+
// Calculate leveraged position size
|
|
130
|
+
const leveragedPositionSize = trade.collateralAmount * trade.leverage;
|
|
131
|
+
// Calculate net PnL after fees
|
|
132
|
+
const netPnlAfterFees = pnlCollateral - totalFees;
|
|
133
|
+
return {
|
|
134
|
+
// Core PnL values
|
|
135
|
+
pnlPercent,
|
|
136
|
+
pnlCollateral,
|
|
137
|
+
tradeValue,
|
|
138
|
+
// Fee breakdown
|
|
139
|
+
fees: {
|
|
140
|
+
borrowingV1: borrowingFeeV1,
|
|
141
|
+
borrowingV2: borrowingFeeV2,
|
|
142
|
+
funding: fundingFee,
|
|
143
|
+
closing: closingFee,
|
|
144
|
+
total: totalFees,
|
|
145
|
+
},
|
|
146
|
+
// Status flags
|
|
147
|
+
isLiquidated,
|
|
148
|
+
isProfitable: pnlPercent > 0,
|
|
149
|
+
// Additional info
|
|
150
|
+
leveragedPositionSize,
|
|
151
|
+
netPnlAfterFees,
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
exports.getComprehensivePnl = getComprehensivePnl;
|
|
155
|
+
/**
|
|
156
|
+
* @dev Legacy PnL calculation function
|
|
157
|
+
* @deprecated Use getComprehensivePnl for more comprehensive calculations
|
|
158
|
+
* @param price Current price
|
|
159
|
+
* @param trade Trade object
|
|
160
|
+
* @param tradeInfo Trade info (not used in legacy implementation)
|
|
161
|
+
* @param initialAccFees Initial accumulated fees
|
|
162
|
+
* @param liquidationParams Liquidation parameters
|
|
163
|
+
* @param useFees Whether to include fees
|
|
164
|
+
* @param context Context with fee calculation parameters
|
|
165
|
+
* @returns [pnlCollateral, pnlPercentage] or undefined if no price
|
|
166
|
+
*/
|
|
167
|
+
const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
|
|
168
|
+
var _a;
|
|
169
|
+
if (!price) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const posCollat = trade.collateralAmount;
|
|
173
|
+
const { openPrice, leverage } = trade;
|
|
174
|
+
let pnlCollat = trade.long
|
|
175
|
+
? ((price - openPrice) / openPrice) * leverage * posCollat
|
|
176
|
+
: ((openPrice - price) / openPrice) * leverage * posCollat;
|
|
177
|
+
if (useFees) {
|
|
178
|
+
pnlCollat -= (0, fees_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
179
|
+
}
|
|
180
|
+
let pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
181
|
+
// Can be liquidated
|
|
182
|
+
if (pnlPercentage <=
|
|
183
|
+
(0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
|
|
184
|
+
pnlPercentage = -100;
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
// Calculate closing fee using the same function as opening fees
|
|
188
|
+
const positionSizeCollateral = posCollat * trade.leverage;
|
|
189
|
+
const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(0, // collateralIndex not used
|
|
190
|
+
trade.user, trade.pairIndex, positionSizeCollateral, (_a = trade.isCounterTrade) !== null && _a !== void 0 ? _a : false, context);
|
|
191
|
+
pnlCollat -= closingFee;
|
|
192
|
+
pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
193
|
+
}
|
|
194
|
+
pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
|
|
195
|
+
pnlCollat = (posCollat * pnlPercentage) / 100;
|
|
196
|
+
return [pnlCollat, pnlPercentage];
|
|
197
|
+
};
|
|
198
|
+
exports.getPnl = getPnl;
|
|
199
|
+
// Re-export types
|
|
200
|
+
__exportStar(require("./types"), exports);
|
|
201
|
+
__exportStar(require("./converter"), exports);
|