@gainsnetwork/sdk 0.2.67-rc6 → 0.2.67-rc8
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 +3 -0
- package/lib/backend/index.js +28 -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 +31 -0
- package/lib/backend/tradingVariables/converter.js +330 -0
- package/lib/backend/tradingVariables/index.d.ts +5 -0
- package/lib/backend/tradingVariables/index.js +95 -0
- package/lib/backend/tradingVariables/types.d.ts +109 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.js +2 -3
- 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/GNSMultiCollatDiamond.d.ts +2140 -286
- 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/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/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/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 +4502 -427
- 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/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.js +65 -142
- package/lib/contracts/utils/openLimitOrders.d.ts +8 -0
- package/lib/contracts/utils/openLimitOrders.js +88 -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/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/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +33 -0
- 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 +110 -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 +146 -0
- package/lib/trade/fees/fundingFees/index.js +346 -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/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- 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 +30 -0
- package/lib/trade/fees/trading/converter.js +43 -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/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 +26 -0
- package/lib/trade/liquidation/index.js +142 -0
- package/lib/trade/liquidation/types.d.ts +59 -0
- package/lib/trade/liquidation/types.js +2 -0
- package/lib/trade/openLimitOrder.d.ts +2 -0
- package/lib/trade/openLimitOrder.js +23 -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 +77 -0
- package/lib/trade/pnl/index.js +270 -0
- package/lib/trade/pnl/types.d.ts +114 -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/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/package.json +1 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Liquidation price context builder module
|
|
4
|
+
* @dev Provides builder functions for creating liquidation price contexts
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.buildLiquidationPriceContext = void 0;
|
|
8
|
+
const builder_1 = require("../fees/borrowing/builder");
|
|
9
|
+
const builder_2 = require("../fees/borrowingV2/builder");
|
|
10
|
+
const builder_3 = require("../fees/fundingFees/builder");
|
|
11
|
+
const builder_4 = require("../fees/trading/builder");
|
|
12
|
+
/**
|
|
13
|
+
* @dev Builds a complete context for liquidation price calculations
|
|
14
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
15
|
+
* @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
|
|
16
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
17
|
+
* @returns Complete context ready for getLiquidationPrice
|
|
18
|
+
*/
|
|
19
|
+
const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, additionalParams) => {
|
|
20
|
+
var _a;
|
|
21
|
+
const { trade, tradeInfo } = tradeContainer;
|
|
22
|
+
const collateralIndex = trade.collateralIndex || 1;
|
|
23
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
24
|
+
if (!tradeContainer.liquidationParams) {
|
|
25
|
+
throw new Error("Liquidation params are required for liquidation price calculation");
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
// Core shared context
|
|
29
|
+
core: {
|
|
30
|
+
currentBlock: additionalParams.currentBlock,
|
|
31
|
+
currentTimestamp: additionalParams.currentTimestamp,
|
|
32
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
33
|
+
contractsVersion: tradeInfo.contractsVersion,
|
|
34
|
+
spreadP: additionalParams.spreadP,
|
|
35
|
+
},
|
|
36
|
+
// Build sub-contexts using dedicated builders
|
|
37
|
+
borrowingV1: (0, builder_1.buildBorrowingV1Context)(globalTradingVariables, collateralIndex, additionalParams.currentBlock),
|
|
38
|
+
borrowingV2: (0, builder_2.buildBorrowingV2Context)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
39
|
+
funding: (0, builder_3.buildFundingContext)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
40
|
+
trading: (0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier),
|
|
41
|
+
// Trade-specific data
|
|
42
|
+
tradeData: {
|
|
43
|
+
tradeInfo,
|
|
44
|
+
tradeFeesData: tradeContainer.tradeFeesData,
|
|
45
|
+
liquidationParams: tradeContainer.liquidationParams,
|
|
46
|
+
initialAccFeesV1: tradeContainer.initialAccFees,
|
|
47
|
+
},
|
|
48
|
+
// Additional parameters for liquidation calculation
|
|
49
|
+
liquidationSpecific: {
|
|
50
|
+
currentPairPrice: additionalParams.currentPairPrice,
|
|
51
|
+
additionalFeeCollateral: additionalParams.additionalFeeCollateral || 0,
|
|
52
|
+
partialCloseMultiplier: additionalParams.partialCloseMultiplier || 1,
|
|
53
|
+
beforeOpened: additionalParams.beforeOpened || false,
|
|
54
|
+
isCounterTrade: trade.isCounterTrade || false,
|
|
55
|
+
userPriceImpact: additionalParams.userPriceImpact,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
exports.buildLiquidationPriceContext = buildLiquidationPriceContext;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Converters for liquidation data between contract and SDK formats
|
|
3
|
+
*/
|
|
4
|
+
import { IPairsStorage } from "../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
5
|
+
import { LiquidationParams } from "../types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Converts contract liquidation params to SDK format
|
|
8
|
+
* @param params Group liquidation params from contract
|
|
9
|
+
* @returns Normalized liquidation params
|
|
10
|
+
*/
|
|
11
|
+
export declare const convertLiquidationParams: (params: IPairsStorage.GroupLiquidationParamsStructOutput) => LiquidationParams;
|
|
12
|
+
/**
|
|
13
|
+
* @dev Converts array of liquidation params from contract
|
|
14
|
+
* @param paramsArray Array of group liquidation params
|
|
15
|
+
* @returns Array of normalized liquidation params
|
|
16
|
+
*/
|
|
17
|
+
export declare const convertLiquidationParamsArray: (paramsArray: IPairsStorage.GroupLiquidationParamsStructOutput[]) => LiquidationParams[];
|
|
18
|
+
/**
|
|
19
|
+
* @dev Converts liquidation params to contract format (for encoding)
|
|
20
|
+
* @param params SDK liquidation params
|
|
21
|
+
* @returns Contract-formatted liquidation params
|
|
22
|
+
*/
|
|
23
|
+
export declare const encodeLiquidationParams: (params: LiquidationParams) => IPairsStorage.GroupLiquidationParamsStruct;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Converters for liquidation data between contract and SDK formats
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.encodeLiquidationParams = exports.convertLiquidationParamsArray = exports.convertLiquidationParams = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Converts contract liquidation params to SDK format
|
|
9
|
+
* @param params Group liquidation params from contract
|
|
10
|
+
* @returns Normalized liquidation params
|
|
11
|
+
*/
|
|
12
|
+
const convertLiquidationParams = (params) => {
|
|
13
|
+
const ONCHAIN_LIQ_THRESHOLD = 0.9;
|
|
14
|
+
return {
|
|
15
|
+
maxLiqSpreadP: Number(params.maxLiqSpreadP) / 1e10 / 100,
|
|
16
|
+
startLiqThresholdP: Number(params.startLiqThresholdP) / 1e10 / 100 || ONCHAIN_LIQ_THRESHOLD,
|
|
17
|
+
endLiqThresholdP: Number(params.endLiqThresholdP) / 1e10 / 100 || ONCHAIN_LIQ_THRESHOLD,
|
|
18
|
+
startLeverage: Number(params.startLeverage) / 1e3,
|
|
19
|
+
endLeverage: Number(params.endLeverage) / 1e3, // 1e3 → float
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
exports.convertLiquidationParams = convertLiquidationParams;
|
|
23
|
+
/**
|
|
24
|
+
* @dev Converts array of liquidation params from contract
|
|
25
|
+
* @param paramsArray Array of group liquidation params
|
|
26
|
+
* @returns Array of normalized liquidation params
|
|
27
|
+
*/
|
|
28
|
+
const convertLiquidationParamsArray = (paramsArray) => {
|
|
29
|
+
return paramsArray.map(exports.convertLiquidationParams);
|
|
30
|
+
};
|
|
31
|
+
exports.convertLiquidationParamsArray = convertLiquidationParamsArray;
|
|
32
|
+
/**
|
|
33
|
+
* @dev Converts liquidation params to contract format (for encoding)
|
|
34
|
+
* @param params SDK liquidation params
|
|
35
|
+
* @returns Contract-formatted liquidation params
|
|
36
|
+
*/
|
|
37
|
+
const encodeLiquidationParams = (params) => {
|
|
38
|
+
return {
|
|
39
|
+
maxLiqSpreadP: Math.round(params.maxLiqSpreadP * 100 * 1e10),
|
|
40
|
+
startLiqThresholdP: Math.round(params.startLiqThresholdP * 100 * 1e10),
|
|
41
|
+
endLiqThresholdP: Math.round(params.endLiqThresholdP * 100 * 1e10),
|
|
42
|
+
startLeverage: Math.round(params.startLeverage * 1e3),
|
|
43
|
+
endLeverage: Math.round(params.endLeverage * 1e3), // float → 1e3
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
exports.encodeLiquidationParams = encodeLiquidationParams;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Main export file for liquidation module
|
|
3
|
+
*/
|
|
4
|
+
import { Trade, LiquidationParams } from "..";
|
|
5
|
+
import { GetLiquidationPriceContext } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Calculate liquidation price with structured context
|
|
8
|
+
* @param trade The trade to calculate liquidation price for
|
|
9
|
+
* @param context Structured context with all required data
|
|
10
|
+
* @returns Liquidation price
|
|
11
|
+
*/
|
|
12
|
+
export declare const getLiquidationPrice: (trade: Trade, context: GetLiquidationPriceContext) => number;
|
|
13
|
+
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|
|
14
|
+
/**
|
|
15
|
+
* @dev Simplified wrapper for getTradeLiquidationPrice
|
|
16
|
+
* @dev Mirrors the contract's simplified overload
|
|
17
|
+
* @param trade The trade to calculate liquidation price for
|
|
18
|
+
* @param additionalFeeCollateral Additional fees to consider
|
|
19
|
+
* @param currentPairPrice Current pair price
|
|
20
|
+
* @param context Context with all required data
|
|
21
|
+
* @returns Liquidation price
|
|
22
|
+
*/
|
|
23
|
+
export declare const getTradeLiquidationPriceSimple: (trade: Trade, additionalFeeCollateral: number, currentPairPrice: number, context: GetLiquidationPriceContext) => number;
|
|
24
|
+
export { convertLiquidationParams, convertLiquidationParamsArray, encodeLiquidationParams, } from "./converter";
|
|
25
|
+
export * from "./types";
|
|
26
|
+
export * from "./builder";
|
|
@@ -0,0 +1,142 @@
|
|
|
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 = 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, _c;
|
|
31
|
+
// Extract legacy parameters from structured context
|
|
32
|
+
const { currentPairPrice, additionalFeeCollateral, partialCloseMultiplier, beforeOpened, } = context.liquidationSpecific;
|
|
33
|
+
// 1. Calculate liquidation fees
|
|
34
|
+
const closingFee = (0, __1.getTotalTradeLiqFeesCollateral)(trade.collateralIndex, trade.user, trade.pairIndex, trade.collateralAmount, {
|
|
35
|
+
totalLiqCollateralFeeP: ((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.endLiqThresholdP) || 0.9,
|
|
36
|
+
globalTradeFeeParams: context.trading.globalTradeFeeParams,
|
|
37
|
+
traderFeeMultiplier: context.trading.traderFeeMultiplier,
|
|
38
|
+
});
|
|
39
|
+
// 2. Calculate holding fees and realized PnL
|
|
40
|
+
let holdingFeesTotal = 0;
|
|
41
|
+
let totalRealizedPnlCollateral = 0;
|
|
42
|
+
if (!beforeOpened && context.tradeData.tradeFeesData && currentPairPrice) {
|
|
43
|
+
// V10 data available - calculate full holding fees
|
|
44
|
+
const holdingFees = (0, __1.getTradePendingHoldingFeesCollateral)(trade, context.tradeData.tradeInfo, context.tradeData.tradeFeesData, currentPairPrice, {
|
|
45
|
+
contractsVersion: context.core.contractsVersion,
|
|
46
|
+
currentTimestamp: context.core.currentTimestamp,
|
|
47
|
+
collateralPriceUsd: context.core.collateralPriceUsd,
|
|
48
|
+
borrowingV1: context.borrowingV1,
|
|
49
|
+
borrowingV2: context.borrowingV2,
|
|
50
|
+
funding: context.funding,
|
|
51
|
+
initialAccFeesV1: context.tradeData.initialAccFeesV1,
|
|
52
|
+
});
|
|
53
|
+
holdingFeesTotal = holdingFees.totalFeeCollateral;
|
|
54
|
+
// Calculate total realized PnL (realized PnL minus realized trading fees)
|
|
55
|
+
totalRealizedPnlCollateral =
|
|
56
|
+
context.tradeData.tradeFeesData.realizedPnlCollateral -
|
|
57
|
+
context.tradeData.tradeFeesData.realizedTradingFeesCollateral;
|
|
58
|
+
}
|
|
59
|
+
else if (!beforeOpened &&
|
|
60
|
+
context.borrowingV1 &&
|
|
61
|
+
context.tradeData.initialAccFeesV1) {
|
|
62
|
+
// Markets using v1 borrowing fees model
|
|
63
|
+
holdingFeesTotal = (0, __1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, context.tradeData.initialAccFeesV1, context.borrowingV1);
|
|
64
|
+
}
|
|
65
|
+
// 3. Apply unified formula for all trades
|
|
66
|
+
const totalFeesCollateral = closingFee +
|
|
67
|
+
(holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
|
|
68
|
+
additionalFeeCollateral;
|
|
69
|
+
// 4. Calculate liquidation threshold
|
|
70
|
+
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage);
|
|
71
|
+
// 5. Calculate liquidation price distance
|
|
72
|
+
const collateralLiqNegativePnl = trade.collateralAmount * liqThresholdP;
|
|
73
|
+
let liqPriceDistance = (trade.openPrice * (collateralLiqNegativePnl - totalFeesCollateral)) /
|
|
74
|
+
trade.collateralAmount /
|
|
75
|
+
trade.leverage;
|
|
76
|
+
// 6. Apply closing spread for v9.2+
|
|
77
|
+
if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
78
|
+
((((_b = context.tradeData.liquidationParams) === null || _b === void 0 ? void 0 : _b.maxLiqSpreadP) !== undefined &&
|
|
79
|
+
context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
|
|
80
|
+
(((_c = context.liquidationSpecific.userPriceImpact) === null || _c === void 0 ? void 0 : _c.fixedSpreadP) !==
|
|
81
|
+
undefined &&
|
|
82
|
+
context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
|
|
83
|
+
const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
|
|
84
|
+
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
85
|
+
}
|
|
86
|
+
// 7. Calculate final liquidation price
|
|
87
|
+
return trade.long
|
|
88
|
+
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
89
|
+
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
90
|
+
};
|
|
91
|
+
exports.getLiquidationPrice = getLiquidationPrice;
|
|
92
|
+
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
93
|
+
if (liquidationParams === undefined ||
|
|
94
|
+
leverage === undefined ||
|
|
95
|
+
liquidationParams.maxLiqSpreadP === 0 ||
|
|
96
|
+
liquidationParams.startLiqThresholdP === 0 ||
|
|
97
|
+
liquidationParams.endLiqThresholdP === 0 ||
|
|
98
|
+
liquidationParams.startLeverage === 0 ||
|
|
99
|
+
liquidationParams.endLeverage === 0) {
|
|
100
|
+
return 0.9;
|
|
101
|
+
}
|
|
102
|
+
if (leverage < liquidationParams.startLeverage) {
|
|
103
|
+
return liquidationParams.startLiqThresholdP;
|
|
104
|
+
}
|
|
105
|
+
if (leverage > liquidationParams.endLeverage) {
|
|
106
|
+
return liquidationParams.endLiqThresholdP;
|
|
107
|
+
}
|
|
108
|
+
if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
|
|
109
|
+
return liquidationParams.endLiqThresholdP;
|
|
110
|
+
}
|
|
111
|
+
return (liquidationParams.startLiqThresholdP -
|
|
112
|
+
((leverage - liquidationParams.startLeverage) *
|
|
113
|
+
(liquidationParams.startLiqThresholdP -
|
|
114
|
+
liquidationParams.endLiqThresholdP)) /
|
|
115
|
+
(liquidationParams.endLeverage - liquidationParams.startLeverage));
|
|
116
|
+
};
|
|
117
|
+
exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
|
|
118
|
+
/**
|
|
119
|
+
* @dev Simplified wrapper for getTradeLiquidationPrice
|
|
120
|
+
* @dev Mirrors the contract's simplified overload
|
|
121
|
+
* @param trade The trade to calculate liquidation price for
|
|
122
|
+
* @param additionalFeeCollateral Additional fees to consider
|
|
123
|
+
* @param currentPairPrice Current pair price
|
|
124
|
+
* @param context Context with all required data
|
|
125
|
+
* @returns Liquidation price
|
|
126
|
+
*/
|
|
127
|
+
const getTradeLiquidationPriceSimple = (trade, additionalFeeCollateral, currentPairPrice, context) => {
|
|
128
|
+
// Build complete context with additional parameters
|
|
129
|
+
const fullContext = Object.assign(Object.assign({}, context), { liquidationSpecific: Object.assign(Object.assign({}, context.liquidationSpecific), { currentPairPrice,
|
|
130
|
+
additionalFeeCollateral, partialCloseMultiplier: 1, beforeOpened: false, isCounterTrade: trade.isCounterTrade || false }) });
|
|
131
|
+
return (0, exports.getLiquidationPrice)(trade, fullContext);
|
|
132
|
+
};
|
|
133
|
+
exports.getTradeLiquidationPriceSimple = getTradeLiquidationPriceSimple;
|
|
134
|
+
// Converters
|
|
135
|
+
var converter_1 = require("./converter");
|
|
136
|
+
Object.defineProperty(exports, "convertLiquidationParams", { enumerable: true, get: function () { return converter_1.convertLiquidationParams; } });
|
|
137
|
+
Object.defineProperty(exports, "convertLiquidationParamsArray", { enumerable: true, get: function () { return converter_1.convertLiquidationParamsArray; } });
|
|
138
|
+
Object.defineProperty(exports, "encodeLiquidationParams", { enumerable: true, get: function () { return converter_1.encodeLiquidationParams; } });
|
|
139
|
+
// Types
|
|
140
|
+
__exportStar(require("./types"), exports);
|
|
141
|
+
// Builder
|
|
142
|
+
__exportStar(require("./builder"), exports);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { GetBorrowingFeeContext, BorrowingFee } from "./../fees";
|
|
2
|
+
import { LiquidationParams, UserPriceImpact, TradeFeesData, TradeInfo } 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 Input parameters for getTradeLiquidationPrice
|
|
9
|
+
* @dev Mirrors the contract's LiqPriceInput struct
|
|
10
|
+
*/
|
|
11
|
+
export type LiqPriceInput = {
|
|
12
|
+
collateralIndex: number;
|
|
13
|
+
trader: string;
|
|
14
|
+
pairIndex: number;
|
|
15
|
+
index: number;
|
|
16
|
+
openPrice: number;
|
|
17
|
+
long: boolean;
|
|
18
|
+
collateral: number;
|
|
19
|
+
leverage: number;
|
|
20
|
+
additionalFeeCollateral: number;
|
|
21
|
+
liquidationParams: LiquidationParams;
|
|
22
|
+
currentPairPrice: number;
|
|
23
|
+
isCounterTrade: boolean;
|
|
24
|
+
partialCloseMultiplier: number;
|
|
25
|
+
beforeOpened: boolean;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* @dev Structured context for liquidation price calculations
|
|
29
|
+
* @dev Follows the same pattern as GetComprehensivePnlContext
|
|
30
|
+
*/
|
|
31
|
+
export type GetLiquidationPriceContext = {
|
|
32
|
+
core: {
|
|
33
|
+
currentBlock: number;
|
|
34
|
+
currentTimestamp: number;
|
|
35
|
+
collateralPriceUsd: number;
|
|
36
|
+
contractsVersion: ContractsVersion;
|
|
37
|
+
spreadP: number;
|
|
38
|
+
};
|
|
39
|
+
borrowingV1?: GetBorrowingFeeContext;
|
|
40
|
+
borrowingV2?: GetPairBorrowingFeeV2Context;
|
|
41
|
+
funding?: GetPairFundingFeeContext;
|
|
42
|
+
trading: TradingFeesSubContext & {
|
|
43
|
+
userPriceImpact?: UserPriceImpact;
|
|
44
|
+
};
|
|
45
|
+
tradeData: {
|
|
46
|
+
tradeInfo: TradeInfo;
|
|
47
|
+
tradeFeesData: TradeFeesData;
|
|
48
|
+
liquidationParams: LiquidationParams;
|
|
49
|
+
initialAccFeesV1?: BorrowingFee.InitialAccFees;
|
|
50
|
+
};
|
|
51
|
+
liquidationSpecific: {
|
|
52
|
+
currentPairPrice: number;
|
|
53
|
+
additionalFeeCollateral: number;
|
|
54
|
+
partialCloseMultiplier: number;
|
|
55
|
+
beforeOpened: boolean;
|
|
56
|
+
isCounterTrade: boolean;
|
|
57
|
+
userPriceImpact?: UserPriceImpact;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFulfillmentPrice = void 0;
|
|
4
|
+
const spread_1 = require("./spread");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
const getFulfillmentPrice = (order, pair, pairDepth, oiWindowsSettings, oiWindows) => {
|
|
7
|
+
if (!order || !pair) {
|
|
8
|
+
return 0;
|
|
9
|
+
}
|
|
10
|
+
// Get spread percentage
|
|
11
|
+
const spreadWithPriceImpactP = (0, spread_1.getSpreadWithPriceImpactP)(pair.spreadP, order.buy, order.positionSize, order.leverage, pairDepth, oiWindowsSettings, oiWindows);
|
|
12
|
+
if (spreadWithPriceImpactP === 0) {
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
const askingPrice = (order.buy && order.type === types_1.OpenLimitOrderType.REVERSAL) ||
|
|
16
|
+
(!order.buy && order.type === types_1.OpenLimitOrderType.MOMENTUM)
|
|
17
|
+
? order.maxPrice
|
|
18
|
+
: order.minPrice;
|
|
19
|
+
return order.buy
|
|
20
|
+
? askingPrice * (1 + spreadWithPriceImpactP)
|
|
21
|
+
: askingPrice * (1 - spreadWithPriceImpactP);
|
|
22
|
+
};
|
|
23
|
+
exports.getFulfillmentPrice = getFulfillmentPrice;
|
|
@@ -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
|
+
initialAccFeesV1: 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,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev PnL calculation module
|
|
3
|
+
* @dev Provides functions matching v10 contract implementations
|
|
4
|
+
*/
|
|
5
|
+
import { Trade, TradeInfo, LiquidationParams, Fee, GlobalTradeFeeParams } from "../types";
|
|
6
|
+
import { ComprehensivePnlResult, GetComprehensivePnlContext } from "./types";
|
|
7
|
+
import { BorrowingFee } from "../fees/borrowing";
|
|
8
|
+
import { ContractsVersion } from "../../contracts/types";
|
|
9
|
+
/**
|
|
10
|
+
* @dev Calculates PnL percentage for a position
|
|
11
|
+
* @dev Mirrors contract's getPnlPercent function
|
|
12
|
+
* @param openPrice Trade open price
|
|
13
|
+
* @param currentPrice Current market price
|
|
14
|
+
* @param long Whether position is long
|
|
15
|
+
* @param leverage Position leverage
|
|
16
|
+
* @returns PnL percentage (e.g., 10 = 10% profit, -50 = 50% loss)
|
|
17
|
+
*/
|
|
18
|
+
export declare const getPnlPercent: (openPrice: number, currentPrice: number, long: boolean, leverage: number) => number;
|
|
19
|
+
/**
|
|
20
|
+
* @dev Calculates trade value from collateral and PnL
|
|
21
|
+
* @dev Mirrors contract's getTradeValuePure function
|
|
22
|
+
* @param collateral Trade collateral amount
|
|
23
|
+
* @param pnlPercent PnL percentage
|
|
24
|
+
* @param totalFees Total fees to deduct
|
|
25
|
+
* @returns Trade value after PnL and fees
|
|
26
|
+
*/
|
|
27
|
+
export declare const getTradeValue: (collateral: number, pnlPercent: number, totalFees: number) => number;
|
|
28
|
+
/**
|
|
29
|
+
* @dev Comprehensive PnL calculation including all fees
|
|
30
|
+
* @param trade The trade to calculate PnL for
|
|
31
|
+
* @param currentPrice Current market price
|
|
32
|
+
* @param tradeInfo Trade info with version and timestamps
|
|
33
|
+
* @param context Context with all fee parameters
|
|
34
|
+
* @returns Detailed PnL breakdown
|
|
35
|
+
*/
|
|
36
|
+
export declare const getComprehensivePnl: (trade: Trade, currentPrice: number, tradeInfo: TradeInfo, context: GetComprehensivePnlContext) => ComprehensivePnlResult;
|
|
37
|
+
/**
|
|
38
|
+
* @dev Legacy getPnl function for backward compatibility
|
|
39
|
+
* @deprecated Use getComprehensivePnl for new implementations
|
|
40
|
+
*/
|
|
41
|
+
export type GetPnlContext = {
|
|
42
|
+
currentBlock: number;
|
|
43
|
+
groups: BorrowingFee.Group[];
|
|
44
|
+
pairs: BorrowingFee.Pair[];
|
|
45
|
+
collateralPriceUsd: number | undefined;
|
|
46
|
+
contractsVersion: ContractsVersion | undefined;
|
|
47
|
+
feeMultiplier: number | undefined;
|
|
48
|
+
fee: Fee;
|
|
49
|
+
globalTradeFeeParams: GlobalTradeFeeParams;
|
|
50
|
+
traderFeeMultiplier?: number;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* @dev Legacy PnL calculation function
|
|
54
|
+
* @deprecated Use getComprehensivePnl for more comprehensive calculations
|
|
55
|
+
* @param price Current price
|
|
56
|
+
* @param trade Trade object
|
|
57
|
+
* @param tradeInfo Trade info (not used in legacy implementation)
|
|
58
|
+
* @param initialAccFees Initial accumulated fees
|
|
59
|
+
* @param liquidationParams Liquidation parameters
|
|
60
|
+
* @param useFees Whether to include fees
|
|
61
|
+
* @param context Context with fee calculation parameters
|
|
62
|
+
* @returns [pnlCollateral, pnlPercentage] or undefined if no price
|
|
63
|
+
*/
|
|
64
|
+
export declare const getPnl: (price: number | undefined, trade: Trade, _tradeInfo: TradeInfo, initialAccFees: BorrowingFee.InitialAccFees, liquidationParams: LiquidationParams, useFees: boolean, context: GetPnlContext) => number[] | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* @dev Calculates the price needed to achieve a target PnL percentage
|
|
67
|
+
* @param targetPnlPercent The target PnL percentage (e.g., 50 for 50% profit, -25 for 25% loss)
|
|
68
|
+
* @param trade The trade to calculate for
|
|
69
|
+
* @param tradeInfo Trade info with timestamps
|
|
70
|
+
* @param context Context with fee calculation parameters
|
|
71
|
+
* @param netPnl Whether to include closing fees in the calculation
|
|
72
|
+
* @returns The price that would result in the target PnL percentage
|
|
73
|
+
*/
|
|
74
|
+
export declare const getPriceForTargetPnlPercentage: (targetPnlPercent: number, trade: Trade, tradeInfo: TradeInfo, context: GetComprehensivePnlContext, netPnl?: boolean) => number;
|
|
75
|
+
export * from "./types";
|
|
76
|
+
export * from "./converter";
|
|
77
|
+
export * from "./builder";
|