@gainsnetwork/sdk 0.0.0-v10.rc2 → 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/contracts/fetch/fees/borrowingFeesV2.d.ts +0 -1
- 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 +7 -20
- 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/GNSMultiCollatDiamond__factory.js +7 -16
- 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 +4 -0
- package/lib/contracts/utils/index.d.ts +0 -3
- package/lib/contracts/utils/index.js +0 -3
- package/lib/contracts/utils/openTrades.js +14 -30
- 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 +13 -2
- package/lib/trade/fees/borrowing/index.js +30 -14
- 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 +1 -0
- package/lib/trade/fees/borrowingV2/index.js +1 -0
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +110 -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 +1 -0
- package/lib/trade/fees/fundingFees/index.js +4 -0
- package/lib/trade/fees/index.d.ts +2 -1
- package/lib/trade/fees/index.js +44 -35
- 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 +1 -0
- package/lib/trade/fees/tiers/index.js +1 -0
- package/lib/trade/fees/trading/index.d.ts +22 -2
- package/lib/trade/fees/trading/index.js +44 -1
- package/lib/trade/fees/trading/types.d.ts +9 -0
- package/lib/trade/index.d.ts +1 -1
- package/lib/trade/index.js +1 -1
- 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/liquidation.d.ts +2 -32
- package/lib/trade/liquidation.js +8 -93
- 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/pnl.d.ts +3 -3
- package/lib/trade/pnl.js +2 -7
- 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 +6 -2
- package/lib/trade/priceImpact/index.js +30 -3
- 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/fetcher.d.ts +63 -0
- package/lib/trade/priceImpact/skew/fetcher.js +168 -0
- package/lib/trade/priceImpact/skew/index.d.ts +1 -0
- package/lib/trade/priceImpact/skew/index.js +4 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +9 -7
- package/package.json +2 -2
|
@@ -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);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Types for PnL calculations
|
|
3
|
+
*/
|
|
4
|
+
import { Trade, TradeInfo, TradeFeesData } from "../types";
|
|
5
|
+
/**
|
|
6
|
+
* @dev Input for trade value calculation with all fees
|
|
7
|
+
* @dev Mirrors contract's TradeValueInput struct
|
|
8
|
+
*/
|
|
9
|
+
export type TradeValueInput = {
|
|
10
|
+
trade: Trade;
|
|
11
|
+
currentPrice: number;
|
|
12
|
+
collateralPriceUsd: number;
|
|
13
|
+
fees: {
|
|
14
|
+
openingFeeCollateral: number;
|
|
15
|
+
closingFeeCollateral: number;
|
|
16
|
+
holdingFeesCollateral: number;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* @dev Result of trade value calculation
|
|
21
|
+
*/
|
|
22
|
+
export type TradeValueResult = {
|
|
23
|
+
tradeValue: number;
|
|
24
|
+
pnlPercent: number;
|
|
25
|
+
pnlCollateral: number;
|
|
26
|
+
totalFees: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* @dev Detailed fee breakdown
|
|
30
|
+
*/
|
|
31
|
+
export type FeeBreakdown = {
|
|
32
|
+
borrowingV1: number;
|
|
33
|
+
borrowingV2: number;
|
|
34
|
+
funding: number;
|
|
35
|
+
closing: number;
|
|
36
|
+
opening?: number;
|
|
37
|
+
total: number;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* @dev Price impact breakdown for v10
|
|
41
|
+
*/
|
|
42
|
+
export type PriceImpactBreakdown = {
|
|
43
|
+
fixedSpread: number;
|
|
44
|
+
skewImpact: number;
|
|
45
|
+
cumulVolImpact: number;
|
|
46
|
+
total: number;
|
|
47
|
+
priceAfterImpact: number;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* @dev Comprehensive PnL result with all details
|
|
51
|
+
*/
|
|
52
|
+
export type ComprehensivePnlResult = {
|
|
53
|
+
pnlPercent: number;
|
|
54
|
+
pnlCollateral: number;
|
|
55
|
+
tradeValue: number;
|
|
56
|
+
fees: FeeBreakdown;
|
|
57
|
+
priceImpact?: PriceImpactBreakdown;
|
|
58
|
+
isLiquidated: boolean;
|
|
59
|
+
isProfitable: boolean;
|
|
60
|
+
leveragedPositionSize: number;
|
|
61
|
+
netPnlAfterFees: number;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* @dev Simple PnL result for backward compatibility
|
|
65
|
+
*/
|
|
66
|
+
export type SimplePnlResult = [pnlCollateral: number, pnlPercent: number];
|
|
67
|
+
/**
|
|
68
|
+
* @dev Input for PnL calculation with price impact
|
|
69
|
+
*/
|
|
70
|
+
export type PnlWithPriceImpactInput = {
|
|
71
|
+
trade: Trade;
|
|
72
|
+
tradeInfo: TradeInfo;
|
|
73
|
+
oraclePrice: number;
|
|
74
|
+
currentPairPrice: number;
|
|
75
|
+
usePriceImpact: boolean;
|
|
76
|
+
includeOpeningFees?: boolean;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* @dev Context for v10 PnL calculations
|
|
80
|
+
*/
|
|
81
|
+
export type V10PnlContext = {
|
|
82
|
+
tradeFeesData: TradeFeesData;
|
|
83
|
+
priceImpactContext?: any;
|
|
84
|
+
skewContext?: any;
|
|
85
|
+
cumulVolContext?: any;
|
|
86
|
+
};
|
package/lib/trade/pnl.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { GetBorrowingFeeContext } from "./fees";
|
|
2
|
-
import {
|
|
3
|
-
import { LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
|
|
2
|
+
import { Fee, LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
|
|
4
3
|
import { ContractsVersion } from "../contracts/types";
|
|
5
|
-
export type GetPnlContext = GetBorrowingFeeContext &
|
|
4
|
+
export type GetPnlContext = GetBorrowingFeeContext & {
|
|
5
|
+
fee: Fee | undefined;
|
|
6
6
|
collateralPriceUsd: number | undefined;
|
|
7
7
|
contractsVersion: ContractsVersion | undefined;
|
|
8
8
|
feeMultiplier: number | undefined;
|
package/lib/trade/pnl.js
CHANGED
|
@@ -2,15 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getPnl = void 0;
|
|
4
4
|
const fees_1 = require("./fees");
|
|
5
|
-
const trading_1 = require("./fees/trading");
|
|
6
5
|
const liquidation_1 = require("./liquidation");
|
|
7
6
|
const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
|
|
8
|
-
var _a;
|
|
9
7
|
if (!price) {
|
|
10
8
|
return;
|
|
11
9
|
}
|
|
12
10
|
const posCollat = trade.collateralAmount;
|
|
13
11
|
const { openPrice, leverage } = trade;
|
|
12
|
+
const { fee } = context;
|
|
14
13
|
let pnlCollat = trade.long
|
|
15
14
|
? ((price - openPrice) / openPrice) * leverage * posCollat
|
|
16
15
|
: ((openPrice - price) / openPrice) * leverage * posCollat;
|
|
@@ -24,11 +23,7 @@ const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useF
|
|
|
24
23
|
pnlPercentage = -100;
|
|
25
24
|
}
|
|
26
25
|
else {
|
|
27
|
-
|
|
28
|
-
const positionSizeCollateral = posCollat * trade.leverage;
|
|
29
|
-
const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(0, // collateralIndex not used
|
|
30
|
-
trade.user, trade.pairIndex, positionSizeCollateral, (_a = trade.isCounterTrade) !== null && _a !== void 0 ? _a : false, context);
|
|
31
|
-
pnlCollat -= closingFee;
|
|
26
|
+
pnlCollat -= (0, fees_1.getClosingFee)(posCollat, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd, context.feeMultiplier);
|
|
32
27
|
pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
33
28
|
}
|
|
34
29
|
pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Trade closing price impact calculations
|
|
3
|
+
* @dev Mirrors contract's TradingCommonUtils.getTradeClosingPriceImpact
|
|
4
|
+
*/
|
|
5
|
+
import { TradeClosingPriceImpactInput, TradeClosingPriceImpactContext, TradeClosingPriceImpactResult } from "./types";
|
|
6
|
+
export type { TradeClosingPriceImpactInput, TradeClosingPriceImpactContext, TradeClosingPriceImpactResult, };
|
|
7
|
+
/**
|
|
8
|
+
* @dev Calculates all price impacts for trade closing
|
|
9
|
+
* @dev Mirrors contract's getTradeClosingPriceImpact function
|
|
10
|
+
* @param input Trade parameters
|
|
11
|
+
* @param context Combined context for calculations
|
|
12
|
+
* @returns Price impact breakdown and trade value
|
|
13
|
+
*/
|
|
14
|
+
export declare const getTradeClosingPriceImpact: (input: TradeClosingPriceImpactInput, context: TradeClosingPriceImpactContext) => TradeClosingPriceImpactResult;
|
|
15
|
+
/**
|
|
16
|
+
* @dev Simplified version using oracle price as current price
|
|
17
|
+
* @param input Trade parameters (without currentPairPrice)
|
|
18
|
+
* @param context Combined context
|
|
19
|
+
* @returns Price impact breakdown and trade value
|
|
20
|
+
*/
|
|
21
|
+
export declare const getTradeClosingPriceImpactAtOracle: (input: Omit<TradeClosingPriceImpactInput, "currentPairPrice">, context: TradeClosingPriceImpactContext) => TradeClosingPriceImpactResult;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Trade closing price impact calculations
|
|
4
|
+
* @dev Mirrors contract's TradingCommonUtils.getTradeClosingPriceImpact
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getTradeClosingPriceImpactAtOracle = exports.getTradeClosingPriceImpact = void 0;
|
|
8
|
+
const cumulVol_1 = require("../cumulVol");
|
|
9
|
+
const skew_1 = require("../skew");
|
|
10
|
+
const types_1 = require("../../../contracts/types");
|
|
11
|
+
/**
|
|
12
|
+
* @dev Calculates position size in tokens for partial close
|
|
13
|
+
* @param originalPositionSizeToken Original position size in tokens
|
|
14
|
+
* @param originalCollateral Original collateral amount
|
|
15
|
+
* @param closingCollateral Collateral amount being closed
|
|
16
|
+
* @returns Proportional position size in tokens
|
|
17
|
+
*/
|
|
18
|
+
const calculateClosingPositionSizeToken = (originalPositionSizeToken, originalCollateral, closingCollateral) => {
|
|
19
|
+
if (originalCollateral === 0)
|
|
20
|
+
return 0;
|
|
21
|
+
// Proportional calculation: (closingCollateral / originalCollateral) * originalPositionSizeToken
|
|
22
|
+
return (closingCollateral * originalPositionSizeToken) / originalCollateral;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* @dev Calculates all price impacts for trade closing
|
|
26
|
+
* @dev Mirrors contract's getTradeClosingPriceImpact function
|
|
27
|
+
* @param input Trade parameters
|
|
28
|
+
* @param context Combined context for calculations
|
|
29
|
+
* @returns Price impact breakdown and trade value
|
|
30
|
+
*/
|
|
31
|
+
const getTradeClosingPriceImpact = (input, context) => {
|
|
32
|
+
// For trades before V9.2, return oracle price without any impact
|
|
33
|
+
if (input.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2) {
|
|
34
|
+
return {
|
|
35
|
+
positionSizeToken: 0,
|
|
36
|
+
fixedSpreadP: 0,
|
|
37
|
+
cumulVolPriceImpactP: 0,
|
|
38
|
+
skewPriceImpactP: 0,
|
|
39
|
+
totalPriceImpactP: 0,
|
|
40
|
+
priceAfterImpact: input.oraclePrice,
|
|
41
|
+
tradeValueCollateralNoFactor: 0,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
// Calculate position size in tokens (proportional to collateral being closed)
|
|
45
|
+
const positionSizeToken = input.trade.positionSizeToken
|
|
46
|
+
? calculateClosingPositionSizeToken(input.trade.positionSizeToken, input.trade.collateralAmount, input.positionSizeCollateral)
|
|
47
|
+
: 0;
|
|
48
|
+
// Calculate fixed spread (reversed for closing)
|
|
49
|
+
const fixedSpreadP = (0, cumulVol_1.getFixedSpreadP)(input.pairSpreadP, input.trade.long, false // closing
|
|
50
|
+
);
|
|
51
|
+
let cumulVolPriceImpactP = 0;
|
|
52
|
+
let tradeValueCollateralNoFactor = 0;
|
|
53
|
+
if (input.useCumulativeVolPriceImpact) {
|
|
54
|
+
// First pass: Calculate with negative PnL assumption
|
|
55
|
+
const positionSizeUsd = input.positionSizeCollateral * context.collateralPriceUsd;
|
|
56
|
+
cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)(input.trade.user, input.pairIndex, input.trade.long, positionSizeUsd, false, // Assume negative PnL initially
|
|
57
|
+
false, // closing
|
|
58
|
+
context.tradeInfo.lastPosIncreaseBlock || context.tradeInfo.createdBlock, Object.assign(Object.assign({}, context), { isOpen: false, isPnlPositive: false, contractsVersion: input.contractsVersion, createdBlock: context.tradeInfo.createdBlock }));
|
|
59
|
+
// Calculate price with conservative impact
|
|
60
|
+
const priceWithImpact = input.trade.long
|
|
61
|
+
? input.currentPairPrice *
|
|
62
|
+
(1 - (fixedSpreadP + cumulVolPriceImpactP) / 100)
|
|
63
|
+
: input.currentPairPrice /
|
|
64
|
+
(1 - (fixedSpreadP + cumulVolPriceImpactP) / 100);
|
|
65
|
+
// Calculate trade value in collateral
|
|
66
|
+
// For long: value = positionSizeToken * priceWithImpact
|
|
67
|
+
// For short: value = positionSizeToken / priceWithImpact * openPrice^2 / currentPrice
|
|
68
|
+
if (positionSizeToken > 0) {
|
|
69
|
+
if (input.trade.long) {
|
|
70
|
+
tradeValueCollateralNoFactor = positionSizeToken * priceWithImpact;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// Short calculation: profit from price decrease
|
|
74
|
+
const pnlFactor = (2 * input.trade.openPrice - priceWithImpact) / input.trade.openPrice;
|
|
75
|
+
tradeValueCollateralNoFactor = input.positionSizeCollateral * pnlFactor;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
tradeValueCollateralNoFactor = input.positionSizeCollateral;
|
|
80
|
+
}
|
|
81
|
+
// Determine actual PnL
|
|
82
|
+
const isPnlPositive = tradeValueCollateralNoFactor > input.trade.collateralAmount;
|
|
83
|
+
// Second pass: Recalculate with actual PnL if positive
|
|
84
|
+
if (isPnlPositive) {
|
|
85
|
+
cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)(input.trade.user, input.pairIndex, input.trade.long, positionSizeUsd, true, // Positive PnL
|
|
86
|
+
false, // closing
|
|
87
|
+
context.tradeInfo.lastPosIncreaseBlock ||
|
|
88
|
+
context.tradeInfo.createdBlock, Object.assign(Object.assign({}, context), { isOpen: false, isPnlPositive: true, contractsVersion: input.contractsVersion, createdBlock: context.tradeInfo.createdBlock }));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Calculate skew price impact (v10+ only)
|
|
92
|
+
const skewPriceImpactP = input.contractsVersion === types_1.ContractsVersion.V10
|
|
93
|
+
? (0, skew_1.getTradeSkewPriceImpactWithChecks)({
|
|
94
|
+
collateralIndex: input.collateralIndex,
|
|
95
|
+
pairIndex: input.pairIndex,
|
|
96
|
+
long: input.trade.long,
|
|
97
|
+
open: false,
|
|
98
|
+
positionSizeCollateral: input.positionSizeCollateral,
|
|
99
|
+
currentPrice: input.currentPairPrice,
|
|
100
|
+
contractsVersion: input.contractsVersion,
|
|
101
|
+
isCounterTrade: input.trade.isCounterTrade,
|
|
102
|
+
}, context.skewContext)
|
|
103
|
+
: 0;
|
|
104
|
+
// Total price impact (all components)
|
|
105
|
+
const totalPriceImpactP = fixedSpreadP + cumulVolPriceImpactP + skewPriceImpactP;
|
|
106
|
+
// Calculate final price after all impacts
|
|
107
|
+
// For closing: longs get worse price when impact is positive, shorts get better
|
|
108
|
+
const priceAfterImpact = input.trade.long
|
|
109
|
+
? input.currentPairPrice * (1 - totalPriceImpactP / 100)
|
|
110
|
+
: input.currentPairPrice / (1 - totalPriceImpactP / 100);
|
|
111
|
+
return {
|
|
112
|
+
positionSizeToken,
|
|
113
|
+
fixedSpreadP,
|
|
114
|
+
cumulVolPriceImpactP,
|
|
115
|
+
skewPriceImpactP,
|
|
116
|
+
totalPriceImpactP,
|
|
117
|
+
priceAfterImpact,
|
|
118
|
+
tradeValueCollateralNoFactor,
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
exports.getTradeClosingPriceImpact = getTradeClosingPriceImpact;
|
|
122
|
+
/**
|
|
123
|
+
* @dev Simplified version using oracle price as current price
|
|
124
|
+
* @param input Trade parameters (without currentPairPrice)
|
|
125
|
+
* @param context Combined context
|
|
126
|
+
* @returns Price impact breakdown and trade value
|
|
127
|
+
*/
|
|
128
|
+
const getTradeClosingPriceImpactAtOracle = (input, context) => {
|
|
129
|
+
return (0, exports.getTradeClosingPriceImpact)(Object.assign(Object.assign({}, input), { currentPairPrice: input.oraclePrice }), context);
|
|
130
|
+
};
|
|
131
|
+
exports.getTradeClosingPriceImpactAtOracle = getTradeClosingPriceImpactAtOracle;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Types for trade closing price impact calculations
|
|
3
|
+
*/
|
|
4
|
+
import { CumulVolContext } from "../cumulVol";
|
|
5
|
+
import { SkewPriceImpactContext } from "../skew/types";
|
|
6
|
+
import { Trade, TradeInfo, PairIndex } from "../../types";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Input parameters for trade closing price impact
|
|
9
|
+
* @dev Mirrors contract's TradePriceImpactInput struct
|
|
10
|
+
*/
|
|
11
|
+
export type TradeClosingPriceImpactInput = {
|
|
12
|
+
trade: Trade;
|
|
13
|
+
oraclePrice: number;
|
|
14
|
+
positionSizeCollateral: number;
|
|
15
|
+
currentPairPrice: number;
|
|
16
|
+
useCumulativeVolPriceImpact: boolean;
|
|
17
|
+
collateralIndex: number;
|
|
18
|
+
pairIndex: PairIndex;
|
|
19
|
+
pairSpreadP: number;
|
|
20
|
+
contractsVersion: number;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* @dev Context for trade closing price impact calculation
|
|
24
|
+
* Combines contexts from spread, cumul vol, and skew
|
|
25
|
+
*/
|
|
26
|
+
export type TradeClosingPriceImpactContext = CumulVolContext & {
|
|
27
|
+
skewContext: SkewPriceImpactContext;
|
|
28
|
+
collateralPriceUsd: number;
|
|
29
|
+
tradeInfo: TradeInfo;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* @dev Result of trade closing price impact calculation
|
|
33
|
+
* @dev Mirrors contract's TradePriceImpact struct with additional return value
|
|
34
|
+
*/
|
|
35
|
+
export type TradeClosingPriceImpactResult = {
|
|
36
|
+
positionSizeToken: number;
|
|
37
|
+
fixedSpreadP: number;
|
|
38
|
+
cumulVolPriceImpactP: number;
|
|
39
|
+
skewPriceImpactP: number;
|
|
40
|
+
totalPriceImpactP: number;
|
|
41
|
+
priceAfterImpact: number;
|
|
42
|
+
tradeValueCollateralNoFactor: number;
|
|
43
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Converters for cumulative volume price impact data between contract and SDK formats
|
|
3
|
+
* @dev All BigNumber values are normalized to floats with appropriate precision
|
|
4
|
+
*/
|
|
5
|
+
import { IPriceImpact } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
6
|
+
import { OiWindowsSettings, OiWindow, OiWindows } from "../../types";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Converts contract OI windows settings to SDK format
|
|
9
|
+
* @param contractData Contract OiWindowsSettings struct
|
|
10
|
+
* @returns Normalized OI windows settings
|
|
11
|
+
*/
|
|
12
|
+
export declare const convertOiWindowsSettings: (contractData: IPriceImpact.OiWindowsSettingsStructOutput) => OiWindowsSettings;
|
|
13
|
+
/**
|
|
14
|
+
* @dev Converts contract PairOi data to SDK OiWindow format
|
|
15
|
+
* @param contractData Contract PairOi struct with USD values
|
|
16
|
+
* @returns Normalized OI window data
|
|
17
|
+
*/
|
|
18
|
+
export declare const convertOiWindow: (contractData: IPriceImpact.PairOiStructOutput) => OiWindow;
|
|
19
|
+
/**
|
|
20
|
+
* @dev Converts array of OI windows from contract format
|
|
21
|
+
* @param windowIds Array of window IDs (as strings for mapping)
|
|
22
|
+
* @param contractWindows Array of PairOi data from contract
|
|
23
|
+
* @returns Normalized OI windows mapping
|
|
24
|
+
*/
|
|
25
|
+
export declare const convertOiWindows: (windowIds: string[], contractWindows: IPriceImpact.PairOiStructOutput[]) => OiWindows;
|
|
26
|
+
/**
|
|
27
|
+
* @dev Batch converter for multiple OI windows settings
|
|
28
|
+
* @param contractDataArray Array of contract OiWindowsSettings
|
|
29
|
+
* @returns Array of normalized OI windows settings
|
|
30
|
+
*/
|
|
31
|
+
export declare const convertOiWindowsSettingsArray: (contractDataArray: IPriceImpact.OiWindowsSettingsStructOutput[]) => OiWindowsSettings[];
|