@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 Converters for cumulative volume price impact data between contract and SDK formats
|
|
4
|
+
* @dev All BigNumber values are normalized to floats with appropriate precision
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* @dev Converts contract OI windows settings to SDK format
|
|
10
|
+
* @param contractData Contract OiWindowsSettings struct
|
|
11
|
+
* @returns Normalized OI windows settings
|
|
12
|
+
*/
|
|
13
|
+
const convertOiWindowsSettings = (contractData) => {
|
|
14
|
+
return {
|
|
15
|
+
startTs: Number(contractData.startTs),
|
|
16
|
+
windowsDuration: Number(contractData.windowsDuration),
|
|
17
|
+
windowsCount: Number(contractData.windowsCount),
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.convertOiWindowsSettings = convertOiWindowsSettings;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts contract PairOi data to SDK OiWindow format
|
|
23
|
+
* @param contractData Contract PairOi struct with USD values
|
|
24
|
+
* @returns Normalized OI window data
|
|
25
|
+
*/
|
|
26
|
+
const convertOiWindow = (contractData) => {
|
|
27
|
+
// USD values are stored as 1e18 in contract
|
|
28
|
+
return {
|
|
29
|
+
oiLongUsd: Number(contractData.oiLongUsd) / 1e18,
|
|
30
|
+
oiShortUsd: Number(contractData.oiShortUsd) / 1e18,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
exports.convertOiWindow = convertOiWindow;
|
|
34
|
+
/**
|
|
35
|
+
* @dev Converts array of OI windows from contract format
|
|
36
|
+
* @param windowIds Array of window IDs (as strings for mapping)
|
|
37
|
+
* @param contractWindows Array of PairOi data from contract
|
|
38
|
+
* @returns Normalized OI windows mapping
|
|
39
|
+
*/
|
|
40
|
+
const convertOiWindows = (windowIds, contractWindows) => {
|
|
41
|
+
if (windowIds.length !== contractWindows.length) {
|
|
42
|
+
throw new Error("Window IDs and data arrays must have the same length");
|
|
43
|
+
}
|
|
44
|
+
const windows = {};
|
|
45
|
+
windowIds.forEach((id, index) => {
|
|
46
|
+
windows[id] = (0, exports.convertOiWindow)(contractWindows[index]);
|
|
47
|
+
});
|
|
48
|
+
return windows;
|
|
49
|
+
};
|
|
50
|
+
exports.convertOiWindows = convertOiWindows;
|
|
51
|
+
/**
|
|
52
|
+
* @dev Batch converter for multiple OI windows settings
|
|
53
|
+
* @param contractDataArray Array of contract OiWindowsSettings
|
|
54
|
+
* @returns Array of normalized OI windows settings
|
|
55
|
+
*/
|
|
56
|
+
const convertOiWindowsSettingsArray = (contractDataArray) => {
|
|
57
|
+
return contractDataArray.map(exports.convertOiWindowsSettings);
|
|
58
|
+
};
|
|
59
|
+
exports.convertOiWindowsSettingsArray = convertOiWindowsSettingsArray;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Cumulative volume price impact calculations
|
|
3
|
+
* @dev Mirrors contract's getTradeCumulVolPriceImpactP functionality
|
|
4
|
+
*/
|
|
5
|
+
import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth, PairFactor, UserPriceImpact } from "../../types";
|
|
6
|
+
import { ContractsVersion } from "../../../contracts/types";
|
|
7
|
+
export type CumulVolContext = {
|
|
8
|
+
isOpen?: boolean;
|
|
9
|
+
isPnlPositive?: boolean;
|
|
10
|
+
createdBlock?: number;
|
|
11
|
+
liquidationParams?: LiquidationParams | undefined;
|
|
12
|
+
currentBlock?: number | undefined;
|
|
13
|
+
contractsVersion?: ContractsVersion | undefined;
|
|
14
|
+
protectionCloseFactorWhitelist?: boolean;
|
|
15
|
+
pairDepth?: PairDepth | undefined;
|
|
16
|
+
oiWindowsSettings?: OiWindowsSettings | undefined;
|
|
17
|
+
oiWindows?: OiWindows | undefined;
|
|
18
|
+
userPriceImpact?: UserPriceImpact | undefined;
|
|
19
|
+
collateralPriceUsd?: number;
|
|
20
|
+
} & Partial<PairFactor>;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Gets the protection close factor with user multiplier
|
|
23
|
+
* @param context Cumulative volume context
|
|
24
|
+
* @returns Protection close factor (1 = 100%)
|
|
25
|
+
*/
|
|
26
|
+
export declare const getProtectionCloseFactor: (context: CumulVolContext | undefined) => number;
|
|
27
|
+
/**
|
|
28
|
+
* @dev Checks if protection close factor is active
|
|
29
|
+
* @param context Cumulative volume context
|
|
30
|
+
* @returns True if protection close factor should be applied
|
|
31
|
+
*/
|
|
32
|
+
export declare const isProtectionCloseFactorActive: (context: CumulVolContext | undefined) => boolean | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Gets the cumulative factor for price impact calculation
|
|
35
|
+
* @param context Cumulative volume context
|
|
36
|
+
* @returns Cumulative factor (default 1)
|
|
37
|
+
*/
|
|
38
|
+
export declare const getCumulativeFactor: (context: CumulVolContext | undefined) => number;
|
|
39
|
+
/**
|
|
40
|
+
* @dev Gets the legacy factor for v9.2 compatibility
|
|
41
|
+
* @param context Cumulative volume context
|
|
42
|
+
* @returns 1 for pre-v9.2, 2 for v9.2+
|
|
43
|
+
*/
|
|
44
|
+
export declare const getLegacyFactor: (context: CumulVolContext | undefined) => number;
|
|
45
|
+
/**
|
|
46
|
+
* @dev Calculates cumulative volume price impact percentage
|
|
47
|
+
* @dev Mirrors contract's getTradeCumulVolPriceImpactP function
|
|
48
|
+
* @param trader Trader address
|
|
49
|
+
* @param pairIndex Trading pair index
|
|
50
|
+
* @param long True for long, false for short
|
|
51
|
+
* @param tradeOpenInterestUsd Position size in USD
|
|
52
|
+
* @param isPnlPositive Whether PnL is positive (only relevant when closing)
|
|
53
|
+
* @param open True for opening, false for closing
|
|
54
|
+
* @param lastPosIncreaseBlock Last block when position was increased (only relevant when closing)
|
|
55
|
+
* @param context Additional context with depths, OI data, and factors
|
|
56
|
+
* @returns Cumulative volume price impact percentage (not including spread)
|
|
57
|
+
*/
|
|
58
|
+
export declare const getTradeCumulVolPriceImpactP: (trader: string, pairIndex: number, long: boolean, tradeOpenInterestUsd: number, isPnlPositive: boolean, open: boolean, lastPosIncreaseBlock: number, context: CumulVolContext) => number;
|
|
59
|
+
/**
|
|
60
|
+
* @dev Gets the fixed spread percentage with direction
|
|
61
|
+
* @dev Mirrors contract's getFixedSpreadP function
|
|
62
|
+
* @param spreadP Total spread percentage (includes base + user spread)
|
|
63
|
+
* @param long True for long position
|
|
64
|
+
* @param open True for opening, false for closing
|
|
65
|
+
* @returns Signed spread percentage (positive or negative based on direction)
|
|
66
|
+
*/
|
|
67
|
+
export declare const getFixedSpreadP: (spreadP: number, long: boolean, open: boolean) => number;
|
|
68
|
+
/**
|
|
69
|
+
* @dev Gets the base spread percentage
|
|
70
|
+
* @param pairSpreadP Pair spread percentage
|
|
71
|
+
* @param isLiquidation True if liquidation
|
|
72
|
+
* @param liquidationParams Liquidation parameters
|
|
73
|
+
* @param userPriceImpact User-specific price impact settings
|
|
74
|
+
* @returns Base spread percentage
|
|
75
|
+
* @todo Review if this function still makes sense or should use getFixedSpreadP pattern
|
|
76
|
+
* Currently it may double-count user fixed spread if pairSpreadP already includes it
|
|
77
|
+
*/
|
|
78
|
+
export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation?: boolean | undefined, liquidationParams?: LiquidationParams | undefined, userPriceImpact?: UserPriceImpact | undefined) => number;
|
|
79
|
+
/**
|
|
80
|
+
* @dev Gets spread with cumulative volume price impact
|
|
81
|
+
* @dev This combines base spread + cumulative volume impact
|
|
82
|
+
* @param pairSpreadP Base pair spread percentage
|
|
83
|
+
* @param buy True for long, false for short
|
|
84
|
+
* @param collateral Collateral amount
|
|
85
|
+
* @param leverage Position leverage
|
|
86
|
+
* @param pairDepth 1% depth values for the pair
|
|
87
|
+
* @param oiWindowsSettings OI windows configuration
|
|
88
|
+
* @param oiWindows Current OI windows data
|
|
89
|
+
* @param context Additional context for the calculation
|
|
90
|
+
* @returns Total spread + cumulative volume price impact percentage
|
|
91
|
+
*/
|
|
92
|
+
export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
|
|
93
|
+
/**
|
|
94
|
+
* @dev Convenience function for calculating cumulative volume price impact
|
|
95
|
+
* @dev Uses collateral and leverage instead of USD position size
|
|
96
|
+
* @param buy True for long, false for short
|
|
97
|
+
* @param collateral Collateral amount
|
|
98
|
+
* @param leverage Position leverage
|
|
99
|
+
* @param open True for opening, false for closing
|
|
100
|
+
* @param context Full context including depths, OI data, and collateral price
|
|
101
|
+
* @returns Cumulative volume price impact percentage
|
|
102
|
+
*/
|
|
103
|
+
export declare const getCumulVolPriceImpact: (buy: boolean, collateral: number, leverage: number, open: boolean, context: CumulVolContext & {
|
|
104
|
+
collateralPriceUsd: number;
|
|
105
|
+
}) => number;
|
|
106
|
+
export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
|
|
107
|
+
export { convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, } from "./converter";
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Cumulative volume price impact calculations
|
|
4
|
+
* @dev Mirrors contract's getTradeCumulVolPriceImpactP functionality
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = exports.getSpreadWithPriceImpactP = exports.getCumulVolPriceImpact = exports.getSpreadWithCumulVolPriceImpactP = exports.getSpreadP = exports.getFixedSpreadP = exports.getTradeCumulVolPriceImpactP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = void 0;
|
|
8
|
+
const oiWindows_1 = require("../../oiWindows");
|
|
9
|
+
const constants_1 = require("../../../constants");
|
|
10
|
+
const types_1 = require("../../../contracts/types");
|
|
11
|
+
/**
|
|
12
|
+
* @dev Gets the protection close factor with user multiplier
|
|
13
|
+
* @param context Cumulative volume context
|
|
14
|
+
* @returns Protection close factor (1 = 100%)
|
|
15
|
+
*/
|
|
16
|
+
const getProtectionCloseFactor = (context) => {
|
|
17
|
+
var _a;
|
|
18
|
+
const protectionCloseFactor = context === undefined ||
|
|
19
|
+
context.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
|
|
20
|
+
context.isOpen === undefined ||
|
|
21
|
+
context.isPnlPositive === undefined ||
|
|
22
|
+
context.protectionCloseFactor === undefined ||
|
|
23
|
+
(0, exports.isProtectionCloseFactorActive)(context) !== true
|
|
24
|
+
? constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR
|
|
25
|
+
: context.protectionCloseFactor;
|
|
26
|
+
const protectionCloseFactorMultiplier = ((_a = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _a === void 0 ? void 0 : _a.cumulVolPriceImpactMultiplier) !== undefined &&
|
|
27
|
+
context.userPriceImpact.cumulVolPriceImpactMultiplier > 0
|
|
28
|
+
? context.userPriceImpact.cumulVolPriceImpactMultiplier
|
|
29
|
+
: 1;
|
|
30
|
+
return protectionCloseFactor * protectionCloseFactorMultiplier;
|
|
31
|
+
};
|
|
32
|
+
exports.getProtectionCloseFactor = getProtectionCloseFactor;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Checks if protection close factor is active
|
|
35
|
+
* @param context Cumulative volume context
|
|
36
|
+
* @returns True if protection close factor should be applied
|
|
37
|
+
*/
|
|
38
|
+
const isProtectionCloseFactorActive = (context) => {
|
|
39
|
+
if (context === undefined ||
|
|
40
|
+
context.currentBlock === undefined ||
|
|
41
|
+
context.createdBlock === undefined ||
|
|
42
|
+
context.protectionCloseFactorBlocks === undefined ||
|
|
43
|
+
context.protectionCloseFactor === undefined) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
return (context.isPnlPositive === true &&
|
|
47
|
+
context.isOpen === false &&
|
|
48
|
+
context.protectionCloseFactor > 0 &&
|
|
49
|
+
context.currentBlock <=
|
|
50
|
+
context.createdBlock + context.protectionCloseFactorBlocks &&
|
|
51
|
+
context.protectionCloseFactorWhitelist !== true);
|
|
52
|
+
};
|
|
53
|
+
exports.isProtectionCloseFactorActive = isProtectionCloseFactorActive;
|
|
54
|
+
/**
|
|
55
|
+
* @dev Gets the cumulative factor for price impact calculation
|
|
56
|
+
* @param context Cumulative volume context
|
|
57
|
+
* @returns Cumulative factor (default 1)
|
|
58
|
+
*/
|
|
59
|
+
const getCumulativeFactor = (context) => {
|
|
60
|
+
if (context === undefined ||
|
|
61
|
+
context.cumulativeFactor === undefined ||
|
|
62
|
+
context.cumulativeFactor === 0) {
|
|
63
|
+
return constants_1.DEFAULT_CUMULATIVE_FACTOR;
|
|
64
|
+
}
|
|
65
|
+
return context.cumulativeFactor;
|
|
66
|
+
};
|
|
67
|
+
exports.getCumulativeFactor = getCumulativeFactor;
|
|
68
|
+
/**
|
|
69
|
+
* @dev Gets the legacy factor for v9.2 compatibility
|
|
70
|
+
* @param context Cumulative volume context
|
|
71
|
+
* @returns 1 for pre-v9.2, 2 for v9.2+
|
|
72
|
+
*/
|
|
73
|
+
const getLegacyFactor = (context) => {
|
|
74
|
+
return (context === null || context === void 0 ? void 0 : context.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
|
|
75
|
+
};
|
|
76
|
+
exports.getLegacyFactor = getLegacyFactor;
|
|
77
|
+
/**
|
|
78
|
+
* @dev Calculates cumulative volume price impact percentage
|
|
79
|
+
* @dev Mirrors contract's getTradeCumulVolPriceImpactP function
|
|
80
|
+
* @param trader Trader address
|
|
81
|
+
* @param pairIndex Trading pair index
|
|
82
|
+
* @param long True for long, false for short
|
|
83
|
+
* @param tradeOpenInterestUsd Position size in USD
|
|
84
|
+
* @param isPnlPositive Whether PnL is positive (only relevant when closing)
|
|
85
|
+
* @param open True for opening, false for closing
|
|
86
|
+
* @param lastPosIncreaseBlock Last block when position was increased (only relevant when closing)
|
|
87
|
+
* @param context Additional context with depths, OI data, and factors
|
|
88
|
+
* @returns Cumulative volume price impact percentage (not including spread)
|
|
89
|
+
*/
|
|
90
|
+
const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context) => {
|
|
91
|
+
var _a, _b, _c, _d;
|
|
92
|
+
// Update context with passed parameters
|
|
93
|
+
const updatedContext = Object.assign(Object.assign({}, context), { isOpen: open, isPnlPositive: isPnlPositive, createdBlock: context.createdBlock || lastPosIncreaseBlock });
|
|
94
|
+
if (
|
|
95
|
+
// No price impact when closing pre-v9.2 trades
|
|
96
|
+
(!open && (context === null || context === void 0 ? void 0 : context.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) ||
|
|
97
|
+
// No price impact for opens when `pair.exemptOnOpen` is true
|
|
98
|
+
(open && (context === null || context === void 0 ? void 0 : context.exemptOnOpen) === true) ||
|
|
99
|
+
// No price impact for closes after `protectionCloseFactor` has expired
|
|
100
|
+
// when `pair.exemptAfterProtectionCloseFactor` is true
|
|
101
|
+
(!open &&
|
|
102
|
+
(context === null || context === void 0 ? void 0 : context.exemptAfterProtectionCloseFactor) === true &&
|
|
103
|
+
(0, exports.isProtectionCloseFactorActive)(updatedContext) !== true)) {
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
const onePercentDepth = long
|
|
107
|
+
? // if `long`
|
|
108
|
+
open
|
|
109
|
+
? (_a = context.pairDepth) === null || _a === void 0 ? void 0 : _a.onePercentDepthAboveUsd
|
|
110
|
+
: (_b = context.pairDepth) === null || _b === void 0 ? void 0 : _b.onePercentDepthBelowUsd
|
|
111
|
+
: // if `short`
|
|
112
|
+
open
|
|
113
|
+
? (_c = context.pairDepth) === null || _c === void 0 ? void 0 : _c.onePercentDepthBelowUsd
|
|
114
|
+
: (_d = context.pairDepth) === null || _d === void 0 ? void 0 : _d.onePercentDepthAboveUsd;
|
|
115
|
+
let activeOi = undefined;
|
|
116
|
+
if (context.oiWindowsSettings !== undefined) {
|
|
117
|
+
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long);
|
|
118
|
+
}
|
|
119
|
+
if (!onePercentDepth || activeOi === undefined) {
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
return (((activeOi * (0, exports.getCumulativeFactor)(updatedContext) +
|
|
123
|
+
tradeOpenInterestUsd / 2) /
|
|
124
|
+
onePercentDepth /
|
|
125
|
+
100 /
|
|
126
|
+
(0, exports.getLegacyFactor)(updatedContext)) *
|
|
127
|
+
(0, exports.getProtectionCloseFactor)(updatedContext));
|
|
128
|
+
};
|
|
129
|
+
exports.getTradeCumulVolPriceImpactP = getTradeCumulVolPriceImpactP;
|
|
130
|
+
/**
|
|
131
|
+
* @dev Gets the fixed spread percentage with direction
|
|
132
|
+
* @dev Mirrors contract's getFixedSpreadP function
|
|
133
|
+
* @param spreadP Total spread percentage (includes base + user spread)
|
|
134
|
+
* @param long True for long position
|
|
135
|
+
* @param open True for opening, false for closing
|
|
136
|
+
* @returns Signed spread percentage (positive or negative based on direction)
|
|
137
|
+
*/
|
|
138
|
+
const getFixedSpreadP = (spreadP, long, open) => {
|
|
139
|
+
// Reverse spread direction on close
|
|
140
|
+
const effectiveLong = open ? long : !long;
|
|
141
|
+
// Calculate half spread
|
|
142
|
+
const fixedSpreadP = spreadP / 2;
|
|
143
|
+
// Apply direction
|
|
144
|
+
return effectiveLong ? fixedSpreadP : -fixedSpreadP;
|
|
145
|
+
};
|
|
146
|
+
exports.getFixedSpreadP = getFixedSpreadP;
|
|
147
|
+
/**
|
|
148
|
+
* @dev Gets the base spread percentage
|
|
149
|
+
* @param pairSpreadP Pair spread percentage
|
|
150
|
+
* @param isLiquidation True if liquidation
|
|
151
|
+
* @param liquidationParams Liquidation parameters
|
|
152
|
+
* @param userPriceImpact User-specific price impact settings
|
|
153
|
+
* @returns Base spread percentage
|
|
154
|
+
* @todo Review if this function still makes sense or should use getFixedSpreadP pattern
|
|
155
|
+
* Currently it may double-count user fixed spread if pairSpreadP already includes it
|
|
156
|
+
*/
|
|
157
|
+
const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
|
|
158
|
+
var _a;
|
|
159
|
+
const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
|
|
160
|
+
if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
const spreadP = pairSpreadP / 2 + fixedSpreadP;
|
|
164
|
+
return isLiquidation === true &&
|
|
165
|
+
liquidationParams !== undefined &&
|
|
166
|
+
liquidationParams.maxLiqSpreadP > 0 &&
|
|
167
|
+
spreadP > liquidationParams.maxLiqSpreadP
|
|
168
|
+
? liquidationParams.maxLiqSpreadP
|
|
169
|
+
: spreadP;
|
|
170
|
+
};
|
|
171
|
+
exports.getSpreadP = getSpreadP;
|
|
172
|
+
/**
|
|
173
|
+
* @dev Gets spread with cumulative volume price impact
|
|
174
|
+
* @dev This combines base spread + cumulative volume impact
|
|
175
|
+
* @param pairSpreadP Base pair spread percentage
|
|
176
|
+
* @param buy True for long, false for short
|
|
177
|
+
* @param collateral Collateral amount
|
|
178
|
+
* @param leverage Position leverage
|
|
179
|
+
* @param pairDepth 1% depth values for the pair
|
|
180
|
+
* @param oiWindowsSettings OI windows configuration
|
|
181
|
+
* @param oiWindows Current OI windows data
|
|
182
|
+
* @param context Additional context for the calculation
|
|
183
|
+
* @returns Total spread + cumulative volume price impact percentage
|
|
184
|
+
*/
|
|
185
|
+
const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, context) => {
|
|
186
|
+
if (pairSpreadP === undefined) {
|
|
187
|
+
return 0;
|
|
188
|
+
}
|
|
189
|
+
const baseSpread = (0, exports.getSpreadP)(pairSpreadP, undefined, undefined, context === null || context === void 0 ? void 0 : context.userPriceImpact);
|
|
190
|
+
// Calculate position size in USD
|
|
191
|
+
const positionSizeUsd = collateral * leverage * ((context === null || context === void 0 ? void 0 : context.collateralPriceUsd) || 1);
|
|
192
|
+
const cumulVolImpact = (0, exports.getTradeCumulVolPriceImpactP)("", // trader - not used in calculation
|
|
193
|
+
0, // pairIndex - not used in calculation
|
|
194
|
+
buy, positionSizeUsd, (context === null || context === void 0 ? void 0 : context.isPnlPositive) || false, (context === null || context === void 0 ? void 0 : context.isOpen) !== false, (context === null || context === void 0 ? void 0 : context.createdBlock) || 0, Object.assign(Object.assign({}, context), { pairDepth,
|
|
195
|
+
oiWindowsSettings,
|
|
196
|
+
oiWindows }));
|
|
197
|
+
// If no depth or OI data, return just half spread
|
|
198
|
+
if (cumulVolImpact === 0 && (!pairDepth || !oiWindowsSettings)) {
|
|
199
|
+
return pairSpreadP / 2;
|
|
200
|
+
}
|
|
201
|
+
return baseSpread + cumulVolImpact;
|
|
202
|
+
};
|
|
203
|
+
exports.getSpreadWithCumulVolPriceImpactP = getSpreadWithCumulVolPriceImpactP;
|
|
204
|
+
/**
|
|
205
|
+
* @dev Convenience function for calculating cumulative volume price impact
|
|
206
|
+
* @dev Uses collateral and leverage instead of USD position size
|
|
207
|
+
* @param buy True for long, false for short
|
|
208
|
+
* @param collateral Collateral amount
|
|
209
|
+
* @param leverage Position leverage
|
|
210
|
+
* @param open True for opening, false for closing
|
|
211
|
+
* @param context Full context including depths, OI data, and collateral price
|
|
212
|
+
* @returns Cumulative volume price impact percentage
|
|
213
|
+
*/
|
|
214
|
+
const getCumulVolPriceImpact = (buy, collateral, leverage, open, context) => {
|
|
215
|
+
const positionSizeUsd = collateral * leverage * context.collateralPriceUsd;
|
|
216
|
+
return (0, exports.getTradeCumulVolPriceImpactP)("", // trader - not used in calculation
|
|
217
|
+
0, // pairIndex - not used in calculation
|
|
218
|
+
buy, positionSizeUsd, context.isPnlPositive || false, open, context.createdBlock || 0, context);
|
|
219
|
+
};
|
|
220
|
+
exports.getCumulVolPriceImpact = getCumulVolPriceImpact;
|
|
221
|
+
// Legacy export for backward compatibility
|
|
222
|
+
exports.getSpreadWithPriceImpactP = exports.getSpreadWithCumulVolPriceImpactP;
|
|
223
|
+
// Export converters
|
|
224
|
+
var converter_1 = require("./converter");
|
|
225
|
+
Object.defineProperty(exports, "convertOiWindowsSettings", { enumerable: true, get: function () { return converter_1.convertOiWindowsSettings; } });
|
|
226
|
+
Object.defineProperty(exports, "convertOiWindow", { enumerable: true, get: function () { return converter_1.convertOiWindow; } });
|
|
227
|
+
Object.defineProperty(exports, "convertOiWindows", { enumerable: true, get: function () { return converter_1.convertOiWindows; } });
|
|
228
|
+
Object.defineProperty(exports, "convertOiWindowsSettingsArray", { enumerable: true, get: function () { return converter_1.convertOiWindowsSettingsArray; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Main price impact module
|
|
3
|
+
* @dev Exports cumulative volume, skew, and combined opening/closing price impact functionality
|
|
4
|
+
*/
|
|
5
|
+
export { getTradeOpeningPriceImpact, getTradeOpeningPriceImpactAtMarket, TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult, } from "./open";
|
|
6
|
+
export { getTradeClosingPriceImpact, getTradeClosingPriceImpactAtOracle, TradeClosingPriceImpactInput, TradeClosingPriceImpactContext, TradeClosingPriceImpactResult, } from "./close";
|
|
7
|
+
export { getTradeCumulVolPriceImpactP, getCumulVolPriceImpact, // Convenience function
|
|
8
|
+
getSpreadWithCumulVolPriceImpactP, getSpreadWithPriceImpactP, // Legacy alias
|
|
9
|
+
getProtectionCloseFactor, isProtectionCloseFactorActive, getCumulativeFactor, getLegacyFactor, getFixedSpreadP, getSpreadP, convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, CumulVolContext, } from "./cumulVol";
|
|
10
|
+
export { getNetSkewToken, getNetSkewCollateral, getTradeSkewDirection, calculateSkewPriceImpactP, getTradeSkewPriceImpact, getTradeSkewPriceImpactWithChecks, calculatePartialSizeToken, SkewPriceImpact, } from "./skew";
|
|
11
|
+
export { convertPairOiToken, convertPairOiTokenArray, convertPairOiCollateral, convertPairOiCollateralArray, normalizeSkewDepth, createSkewDepth, createSkewPriceImpactContext, isValidSkewDepth, convertSkewDepthsArray, mergeSkewPriceImpactContexts, } from "./skew/converter";
|
|
12
|
+
export type { PairOiToken, PairOiCollateral, SkewDepth, SkewPriceImpactInput, SkewPriceImpactResult, SkewPriceImpactContext, TradeSkewParams, PositionSizeResult, } from "./skew/types";
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Main price impact module
|
|
4
|
+
* @dev Exports cumulative volume, skew, and combined opening/closing price impact functionality
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.mergeSkewPriceImpactContexts = exports.convertSkewDepthsArray = exports.isValidSkewDepth = exports.createSkewPriceImpactContext = exports.createSkewDepth = exports.normalizeSkewDepth = exports.convertPairOiCollateralArray = exports.convertPairOiCollateral = exports.convertPairOiTokenArray = exports.convertPairOiToken = exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpactWithChecks = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = exports.getSpreadP = exports.getFixedSpreadP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = exports.getSpreadWithPriceImpactP = exports.getSpreadWithCumulVolPriceImpactP = exports.getCumulVolPriceImpact = exports.getTradeCumulVolPriceImpactP = exports.getTradeClosingPriceImpactAtOracle = exports.getTradeClosingPriceImpact = exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = void 0;
|
|
8
|
+
// Export trade opening price impact functionality
|
|
9
|
+
var open_1 = require("./open");
|
|
10
|
+
// Core functions
|
|
11
|
+
Object.defineProperty(exports, "getTradeOpeningPriceImpact", { enumerable: true, get: function () { return open_1.getTradeOpeningPriceImpact; } });
|
|
12
|
+
Object.defineProperty(exports, "getTradeOpeningPriceImpactAtMarket", { enumerable: true, get: function () { return open_1.getTradeOpeningPriceImpactAtMarket; } });
|
|
13
|
+
// Export trade closing price impact functionality
|
|
14
|
+
var close_1 = require("./close");
|
|
15
|
+
// Core functions
|
|
16
|
+
Object.defineProperty(exports, "getTradeClosingPriceImpact", { enumerable: true, get: function () { return close_1.getTradeClosingPriceImpact; } });
|
|
17
|
+
Object.defineProperty(exports, "getTradeClosingPriceImpactAtOracle", { enumerable: true, get: function () { return close_1.getTradeClosingPriceImpactAtOracle; } });
|
|
18
|
+
// Export cumulative volume price impact functionality
|
|
19
|
+
var cumulVol_1 = require("./cumulVol");
|
|
20
|
+
// Core functions
|
|
21
|
+
Object.defineProperty(exports, "getTradeCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getTradeCumulVolPriceImpactP; } });
|
|
22
|
+
Object.defineProperty(exports, "getCumulVolPriceImpact", { enumerable: true, get: function () { return cumulVol_1.getCumulVolPriceImpact; } });
|
|
23
|
+
Object.defineProperty(exports, "getSpreadWithCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithCumulVolPriceImpactP; } });
|
|
24
|
+
Object.defineProperty(exports, "getSpreadWithPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithPriceImpactP; } });
|
|
25
|
+
Object.defineProperty(exports, "getProtectionCloseFactor", { enumerable: true, get: function () { return cumulVol_1.getProtectionCloseFactor; } });
|
|
26
|
+
Object.defineProperty(exports, "isProtectionCloseFactorActive", { enumerable: true, get: function () { return cumulVol_1.isProtectionCloseFactorActive; } });
|
|
27
|
+
Object.defineProperty(exports, "getCumulativeFactor", { enumerable: true, get: function () { return cumulVol_1.getCumulativeFactor; } });
|
|
28
|
+
Object.defineProperty(exports, "getLegacyFactor", { enumerable: true, get: function () { return cumulVol_1.getLegacyFactor; } });
|
|
29
|
+
Object.defineProperty(exports, "getFixedSpreadP", { enumerable: true, get: function () { return cumulVol_1.getFixedSpreadP; } });
|
|
30
|
+
Object.defineProperty(exports, "getSpreadP", { enumerable: true, get: function () { return cumulVol_1.getSpreadP; } });
|
|
31
|
+
// Converters
|
|
32
|
+
Object.defineProperty(exports, "convertOiWindowsSettings", { enumerable: true, get: function () { return cumulVol_1.convertOiWindowsSettings; } });
|
|
33
|
+
Object.defineProperty(exports, "convertOiWindow", { enumerable: true, get: function () { return cumulVol_1.convertOiWindow; } });
|
|
34
|
+
Object.defineProperty(exports, "convertOiWindows", { enumerable: true, get: function () { return cumulVol_1.convertOiWindows; } });
|
|
35
|
+
Object.defineProperty(exports, "convertOiWindowsSettingsArray", { enumerable: true, get: function () { return cumulVol_1.convertOiWindowsSettingsArray; } });
|
|
36
|
+
// Export skew price impact functionality
|
|
37
|
+
var skew_1 = require("./skew");
|
|
38
|
+
// Core functions
|
|
39
|
+
Object.defineProperty(exports, "getNetSkewToken", { enumerable: true, get: function () { return skew_1.getNetSkewToken; } });
|
|
40
|
+
Object.defineProperty(exports, "getNetSkewCollateral", { enumerable: true, get: function () { return skew_1.getNetSkewCollateral; } });
|
|
41
|
+
Object.defineProperty(exports, "getTradeSkewDirection", { enumerable: true, get: function () { return skew_1.getTradeSkewDirection; } });
|
|
42
|
+
Object.defineProperty(exports, "calculateSkewPriceImpactP", { enumerable: true, get: function () { return skew_1.calculateSkewPriceImpactP; } });
|
|
43
|
+
Object.defineProperty(exports, "getTradeSkewPriceImpact", { enumerable: true, get: function () { return skew_1.getTradeSkewPriceImpact; } });
|
|
44
|
+
Object.defineProperty(exports, "getTradeSkewPriceImpactWithChecks", { enumerable: true, get: function () { return skew_1.getTradeSkewPriceImpactWithChecks; } });
|
|
45
|
+
Object.defineProperty(exports, "calculatePartialSizeToken", { enumerable: true, get: function () { return skew_1.calculatePartialSizeToken; } });
|
|
46
|
+
// Types namespace
|
|
47
|
+
Object.defineProperty(exports, "SkewPriceImpact", { enumerable: true, get: function () { return skew_1.SkewPriceImpact; } });
|
|
48
|
+
// Export converters
|
|
49
|
+
var converter_1 = require("./skew/converter");
|
|
50
|
+
Object.defineProperty(exports, "convertPairOiToken", { enumerable: true, get: function () { return converter_1.convertPairOiToken; } });
|
|
51
|
+
Object.defineProperty(exports, "convertPairOiTokenArray", { enumerable: true, get: function () { return converter_1.convertPairOiTokenArray; } });
|
|
52
|
+
Object.defineProperty(exports, "convertPairOiCollateral", { enumerable: true, get: function () { return converter_1.convertPairOiCollateral; } });
|
|
53
|
+
Object.defineProperty(exports, "convertPairOiCollateralArray", { enumerable: true, get: function () { return converter_1.convertPairOiCollateralArray; } });
|
|
54
|
+
Object.defineProperty(exports, "normalizeSkewDepth", { enumerable: true, get: function () { return converter_1.normalizeSkewDepth; } });
|
|
55
|
+
Object.defineProperty(exports, "createSkewDepth", { enumerable: true, get: function () { return converter_1.createSkewDepth; } });
|
|
56
|
+
Object.defineProperty(exports, "createSkewPriceImpactContext", { enumerable: true, get: function () { return converter_1.createSkewPriceImpactContext; } });
|
|
57
|
+
Object.defineProperty(exports, "isValidSkewDepth", { enumerable: true, get: function () { return converter_1.isValidSkewDepth; } });
|
|
58
|
+
Object.defineProperty(exports, "convertSkewDepthsArray", { enumerable: true, get: function () { return converter_1.convertSkewDepthsArray; } });
|
|
59
|
+
Object.defineProperty(exports, "mergeSkewPriceImpactContexts", { enumerable: true, get: function () { return converter_1.mergeSkewPriceImpactContexts; } });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Trade opening price impact calculations
|
|
3
|
+
* @dev Mirrors contract's TradingCommonUtils.getTradeOpeningPriceImpact
|
|
4
|
+
*/
|
|
5
|
+
import { TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult } from "./types";
|
|
6
|
+
export type { TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult, };
|
|
7
|
+
/**
|
|
8
|
+
* @dev Calculates all price impacts for trade opening
|
|
9
|
+
* @dev Mirrors contract's getTradeOpeningPriceImpact function
|
|
10
|
+
* @param input Trade parameters
|
|
11
|
+
* @param context Combined context for calculations
|
|
12
|
+
* @returns Price impact breakdown and final price
|
|
13
|
+
*/
|
|
14
|
+
export declare const getTradeOpeningPriceImpact: (input: TradeOpeningPriceImpactInput, context: TradeOpeningPriceImpactContext) => TradeOpeningPriceImpactResult;
|
|
15
|
+
/**
|
|
16
|
+
* @dev Simplified version using current market price
|
|
17
|
+
* @param input Trade parameters
|
|
18
|
+
* @param context Combined context
|
|
19
|
+
* @param currentMarketPrice Current market price to use as open price
|
|
20
|
+
* @returns Price impact breakdown and final price
|
|
21
|
+
*/
|
|
22
|
+
export declare const getTradeOpeningPriceImpactAtMarket: (input: Omit<TradeOpeningPriceImpactInput, "openPrice">, context: TradeOpeningPriceImpactContext, currentMarketPrice: number) => TradeOpeningPriceImpactResult;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Trade opening price impact calculations
|
|
4
|
+
* @dev Mirrors contract's TradingCommonUtils.getTradeOpeningPriceImpact
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = void 0;
|
|
8
|
+
const cumulVol_1 = require("../cumulVol");
|
|
9
|
+
const skew_1 = require("../skew");
|
|
10
|
+
/**
|
|
11
|
+
* @dev Calculates all price impacts for trade opening
|
|
12
|
+
* @dev Mirrors contract's getTradeOpeningPriceImpact function
|
|
13
|
+
* @param input Trade parameters
|
|
14
|
+
* @param context Combined context for calculations
|
|
15
|
+
* @returns Price impact breakdown and final price
|
|
16
|
+
*/
|
|
17
|
+
const getTradeOpeningPriceImpact = (input, context) => {
|
|
18
|
+
const positionSizeCollateral = input.collateralAmount * input.leverage;
|
|
19
|
+
// Calculate fixed spread
|
|
20
|
+
const spreadP = (0, cumulVol_1.getFixedSpreadP)(input.pairSpreadP, input.long, true // opening
|
|
21
|
+
);
|
|
22
|
+
// Calculate position size in USD
|
|
23
|
+
const positionSizeUsd = positionSizeCollateral * context.collateralPriceUsd;
|
|
24
|
+
// Calculate cumulative volume price impact
|
|
25
|
+
const cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)("", // trader - not needed for calculation
|
|
26
|
+
input.pairIndex, input.long, positionSizeUsd, false, // isPnlPositive - not relevant for opening
|
|
27
|
+
true, // open
|
|
28
|
+
0, // lastPosIncreaseBlock - not relevant for opening
|
|
29
|
+
context);
|
|
30
|
+
// Calculate skew price impact (v10+ only)
|
|
31
|
+
const skewPriceImpactP = (0, skew_1.getTradeSkewPriceImpactWithChecks)({
|
|
32
|
+
collateralIndex: input.collateralIndex,
|
|
33
|
+
pairIndex: input.pairIndex,
|
|
34
|
+
long: input.long,
|
|
35
|
+
open: true,
|
|
36
|
+
positionSizeCollateral,
|
|
37
|
+
currentPrice: input.openPrice,
|
|
38
|
+
contractsVersion: input.contractsVersion,
|
|
39
|
+
isCounterTrade: input.isCounterTrade,
|
|
40
|
+
}, context.skewContext);
|
|
41
|
+
// Total price impact (signed - can be positive or negative)
|
|
42
|
+
// Spread is always positive, impacts can be negative
|
|
43
|
+
const totalPriceImpactP = spreadP + cumulVolPriceImpactP + skewPriceImpactP;
|
|
44
|
+
// Calculate final price after impact
|
|
45
|
+
// For longs: price increases with positive impact
|
|
46
|
+
// For shorts: price decreases with positive impact
|
|
47
|
+
const priceImpactFactor = 1 + totalPriceImpactP / 100;
|
|
48
|
+
const priceAfterImpact = input.long
|
|
49
|
+
? input.openPrice * priceImpactFactor
|
|
50
|
+
: input.openPrice / priceImpactFactor;
|
|
51
|
+
// Calculate percent profit from impact
|
|
52
|
+
// Positive when trader benefits, negative when trader loses
|
|
53
|
+
const percentProfitP = input.long
|
|
54
|
+
? -totalPriceImpactP // Long loses when price goes up
|
|
55
|
+
: totalPriceImpactP; // Short gains when price goes up
|
|
56
|
+
return {
|
|
57
|
+
priceAfterImpact,
|
|
58
|
+
priceImpactP: Math.abs(totalPriceImpactP),
|
|
59
|
+
percentProfitP,
|
|
60
|
+
cumulVolPriceImpactP,
|
|
61
|
+
skewPriceImpactP,
|
|
62
|
+
totalPriceImpactP,
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
exports.getTradeOpeningPriceImpact = getTradeOpeningPriceImpact;
|
|
66
|
+
/**
|
|
67
|
+
* @dev Simplified version using current market price
|
|
68
|
+
* @param input Trade parameters
|
|
69
|
+
* @param context Combined context
|
|
70
|
+
* @param currentMarketPrice Current market price to use as open price
|
|
71
|
+
* @returns Price impact breakdown and final price
|
|
72
|
+
*/
|
|
73
|
+
const getTradeOpeningPriceImpactAtMarket = (input, context, currentMarketPrice) => {
|
|
74
|
+
return (0, exports.getTradeOpeningPriceImpact)(Object.assign(Object.assign({}, input), { openPrice: currentMarketPrice }), context);
|
|
75
|
+
};
|
|
76
|
+
exports.getTradeOpeningPriceImpactAtMarket = getTradeOpeningPriceImpactAtMarket;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Types for trade opening price impact calculations
|
|
3
|
+
*/
|
|
4
|
+
import { CumulVolContext } from "../cumulVol";
|
|
5
|
+
import { SkewPriceImpactContext } from "../skew/types";
|
|
6
|
+
import { Fee, PairIndex } from "../../types";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Input parameters for trade opening price impact
|
|
9
|
+
*/
|
|
10
|
+
export type TradeOpeningPriceImpactInput = {
|
|
11
|
+
collateralIndex: number;
|
|
12
|
+
pairIndex: PairIndex;
|
|
13
|
+
long: boolean;
|
|
14
|
+
collateralAmount: number;
|
|
15
|
+
leverage: number;
|
|
16
|
+
openPrice: number;
|
|
17
|
+
pairSpreadP: number;
|
|
18
|
+
fee: Fee;
|
|
19
|
+
contractsVersion: number;
|
|
20
|
+
isCounterTrade?: boolean;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* @dev Context for trade opening price impact calculation
|
|
24
|
+
* Combines contexts from spread, cumul vol, and skew
|
|
25
|
+
*/
|
|
26
|
+
export type TradeOpeningPriceImpactContext = CumulVolContext & {
|
|
27
|
+
skewContext: SkewPriceImpactContext;
|
|
28
|
+
collateralPriceUsd: number;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* @dev Result of trade opening price impact calculation
|
|
32
|
+
* Mirrors contract's PriceImpact struct
|
|
33
|
+
*/
|
|
34
|
+
export type TradeOpeningPriceImpactResult = {
|
|
35
|
+
priceAfterImpact: number;
|
|
36
|
+
priceImpactP: number;
|
|
37
|
+
percentProfitP: number;
|
|
38
|
+
cumulVolPriceImpactP: number;
|
|
39
|
+
skewPriceImpactP: number;
|
|
40
|
+
totalPriceImpactP: number;
|
|
41
|
+
};
|