@gainsnetwork/sdk 1.3.0-rc7 → 1.4.2-rc1
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/tradingVariables/backend.types.d.ts +4 -11
- package/lib/backend/tradingVariables/converter.d.ts +3 -7
- package/lib/backend/tradingVariables/converter.js +7 -15
- package/lib/backend/tradingVariables/index.js +2 -5
- package/lib/backend/tradingVariables/types.d.ts +2 -4
- package/lib/constants.js +1 -1
- package/lib/contracts/addresses.d.ts +1 -1
- package/lib/contracts/addresses.js +5 -6
- package/lib/contracts/addresses.json +0 -29
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +40 -223
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +36 -312
- package/lib/contracts/utils/pairs.d.ts +2 -13
- package/lib/contracts/utils/pairs.js +11 -70
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/lib/trade/priceImpact/cumulVol/builder.js +2 -4
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +0 -63
- package/lib/trade/priceImpact/cumulVol/converter.js +1 -97
- package/lib/trade/priceImpact/cumulVol/index.d.ts +6 -7
- package/lib/trade/priceImpact/cumulVol/index.js +32 -135
- package/lib/trade/priceImpact/open/index.js +0 -3
- package/package.json +1 -1
|
@@ -4,8 +4,7 @@
|
|
|
4
4
|
* @dev All BigNumber values are normalized to floats with appropriate precision
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.
|
|
8
|
-
const depthBands_1 = require("../../../pricing/depthBands");
|
|
7
|
+
exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = void 0;
|
|
9
8
|
/**
|
|
10
9
|
* @dev Converts contract OI windows settings to SDK format
|
|
11
10
|
* @param contractData Contract OiWindowsSettings struct
|
|
@@ -58,98 +57,3 @@ const convertOiWindowsSettingsArray = (contractDataArray) => {
|
|
|
58
57
|
return contractDataArray.map(exports.convertOiWindowsSettings);
|
|
59
58
|
};
|
|
60
59
|
exports.convertOiWindowsSettingsArray = convertOiWindowsSettingsArray;
|
|
61
|
-
/**
|
|
62
|
-
* @dev Converts decoded depth bands from contract to SDK format
|
|
63
|
-
* @param totalDepthUsd Total depth in USD (already decoded from contract)
|
|
64
|
-
* @param bandsBps Array of 30 band percentages in basis points from contract
|
|
65
|
-
* @returns Normalized depth bands with bands in 0-1 range
|
|
66
|
-
*/
|
|
67
|
-
const convertDepthBands = (totalDepthUsd, bandsBps) => {
|
|
68
|
-
// Convert bands from basis points to 0-1 range
|
|
69
|
-
const bands = bandsBps.map(bps => bps / 10000);
|
|
70
|
-
return {
|
|
71
|
-
totalDepthUsd,
|
|
72
|
-
bands,
|
|
73
|
-
};
|
|
74
|
-
};
|
|
75
|
-
exports.convertDepthBands = convertDepthBands;
|
|
76
|
-
/**
|
|
77
|
-
* @dev Converts decoded pair depth bands from contract to SDK format
|
|
78
|
-
* @param aboveDepth Decoded above depth bands from getPairDepthBandsDecoded
|
|
79
|
-
* @param belowDepth Decoded below depth bands from getPairDepthBandsDecoded
|
|
80
|
-
* @returns Normalized pair depth bands with above/below
|
|
81
|
-
*/
|
|
82
|
-
const convertPairDepthBands = (aboveDepth, belowDepth) => {
|
|
83
|
-
// Convert above bands if configured
|
|
84
|
-
const above = aboveDepth && aboveDepth.totalDepthUsd > 0
|
|
85
|
-
? (0, exports.convertDepthBands)(aboveDepth.totalDepthUsd, aboveDepth.bands)
|
|
86
|
-
: undefined;
|
|
87
|
-
// Convert below bands if configured
|
|
88
|
-
const below = belowDepth && belowDepth.totalDepthUsd > 0
|
|
89
|
-
? (0, exports.convertDepthBands)(belowDepth.totalDepthUsd, belowDepth.bands)
|
|
90
|
-
: undefined;
|
|
91
|
-
return {
|
|
92
|
-
above,
|
|
93
|
-
below,
|
|
94
|
-
};
|
|
95
|
-
};
|
|
96
|
-
exports.convertPairDepthBands = convertPairDepthBands;
|
|
97
|
-
/**
|
|
98
|
-
* @dev Converts decoded depth bands mapping from contract to SDK format
|
|
99
|
-
* @param bandsBps Array of 30 band offset values in basis points from getDepthBandsMappingDecoded
|
|
100
|
-
* @returns Normalized depth bands mapping with offset values in 0-1 range
|
|
101
|
-
*/
|
|
102
|
-
const convertDepthBandsMapping = (bandsBps) => {
|
|
103
|
-
// Convert bands from basis points to 0-1 range
|
|
104
|
-
const bands = bandsBps.map(bps => bps / 10000);
|
|
105
|
-
return {
|
|
106
|
-
bands,
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
exports.convertDepthBandsMapping = convertDepthBandsMapping;
|
|
110
|
-
/**
|
|
111
|
-
* @dev Validates depth bands have correct number of bands
|
|
112
|
-
* @param depthBands Depth bands to validate
|
|
113
|
-
* @returns True if valid (30 bands)
|
|
114
|
-
*/
|
|
115
|
-
const validateDepthBands = (depthBands) => {
|
|
116
|
-
return depthBands.bands.length === 30;
|
|
117
|
-
};
|
|
118
|
-
exports.validateDepthBands = validateDepthBands;
|
|
119
|
-
/**
|
|
120
|
-
* @dev Validates depth bands mapping has correct number of bands
|
|
121
|
-
* @param mapping Depth bands mapping to validate
|
|
122
|
-
* @returns True if valid (30 bands)
|
|
123
|
-
*/
|
|
124
|
-
const validateDepthBandsMapping = (mapping) => {
|
|
125
|
-
return mapping.bands.length === 30;
|
|
126
|
-
};
|
|
127
|
-
exports.validateDepthBandsMapping = validateDepthBandsMapping;
|
|
128
|
-
/**
|
|
129
|
-
* @dev Alternative converter for decoded pair depth bands from contract
|
|
130
|
-
* @param contractData Decoded pair depth bands from getPairDepthBandsDecoded
|
|
131
|
-
* @returns Normalized pair depth bands
|
|
132
|
-
*/
|
|
133
|
-
const convertPairDepthBandsDecoded = (contractData) => {
|
|
134
|
-
return (0, exports.convertPairDepthBands)(contractData.above, contractData.below);
|
|
135
|
-
};
|
|
136
|
-
exports.convertPairDepthBandsDecoded = convertPairDepthBandsDecoded;
|
|
137
|
-
/**
|
|
138
|
-
* @dev Alternative converter for raw slot-based pair depth bands (if needed for legacy)
|
|
139
|
-
* @param aboveSlot1 First slot for above bands
|
|
140
|
-
* @param aboveSlot2 Second slot for above bands
|
|
141
|
-
* @param belowSlot1 First slot for below bands
|
|
142
|
-
* @param belowSlot2 Second slot for below bands
|
|
143
|
-
* @returns Normalized pair depth bands
|
|
144
|
-
*/
|
|
145
|
-
const convertPairDepthBandsFromSlots = (aboveSlot1, aboveSlot2, belowSlot1, belowSlot2) => {
|
|
146
|
-
// Use the decoding functions from pricing module if raw slots are provided
|
|
147
|
-
const above = aboveSlot1 !== BigInt(0) || aboveSlot2 !== BigInt(0)
|
|
148
|
-
? (0, depthBands_1.decodeDepthBands)(aboveSlot1, aboveSlot2)
|
|
149
|
-
: undefined;
|
|
150
|
-
const below = belowSlot1 !== BigInt(0) || belowSlot2 !== BigInt(0)
|
|
151
|
-
? (0, depthBands_1.decodeDepthBands)(belowSlot1, belowSlot2)
|
|
152
|
-
: undefined;
|
|
153
|
-
return (0, exports.convertPairDepthBands)(above, below);
|
|
154
|
-
};
|
|
155
|
-
exports.convertPairDepthBandsFromSlots = convertPairDepthBandsFromSlots;
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
* @dev Cumulative volume price impact calculations
|
|
3
3
|
* @dev Mirrors contract's getTradeCumulVolPriceImpactP functionality
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
6
|
-
import { LiquidationParams, OiWindows, OiWindowsSettings, PairFactor, UserPriceImpact } from "../../types";
|
|
5
|
+
import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth, PairFactor, UserPriceImpact } from "../../types";
|
|
7
6
|
import { ContractsVersion } from "../../../contracts/types";
|
|
8
7
|
export type CumulVolContext = {
|
|
9
8
|
isOpen?: boolean;
|
|
@@ -13,10 +12,9 @@ export type CumulVolContext = {
|
|
|
13
12
|
currentBlock?: number | undefined;
|
|
14
13
|
contractsVersion?: ContractsVersion | undefined;
|
|
15
14
|
protectionCloseFactorWhitelist?: boolean;
|
|
15
|
+
pairDepth?: PairDepth | undefined;
|
|
16
16
|
oiWindowsSettings?: OiWindowsSettings | undefined;
|
|
17
17
|
oiWindows?: OiWindows | undefined;
|
|
18
|
-
pairDepthBands?: PairDepthBands | undefined;
|
|
19
|
-
depthBandsMapping?: DepthBandsMapping | undefined;
|
|
20
18
|
userPriceImpact?: UserPriceImpact | undefined;
|
|
21
19
|
collateralPriceUsd?: number;
|
|
22
20
|
} & Partial<PairFactor>;
|
|
@@ -57,7 +55,7 @@ export declare const getLegacyFactor: (context: CumulVolContext | undefined) =>
|
|
|
57
55
|
* @param context Additional context with depths, OI data, and factors
|
|
58
56
|
* @returns Cumulative volume price impact percentage (not including spread)
|
|
59
57
|
*/
|
|
60
|
-
export declare const getTradeCumulVolPriceImpactP: (
|
|
58
|
+
export declare const getTradeCumulVolPriceImpactP: (trader: string, pairIndex: number, long: boolean, tradeOpenInterestUsd: number, isPnlPositive: boolean, open: boolean, lastPosIncreaseBlock: number, context: CumulVolContext) => number;
|
|
61
59
|
/**
|
|
62
60
|
* @dev Gets the fixed spread percentage with direction
|
|
63
61
|
* @dev Mirrors contract's getFixedSpreadP function
|
|
@@ -85,12 +83,13 @@ export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation
|
|
|
85
83
|
* @param buy True for long, false for short
|
|
86
84
|
* @param collateral Collateral amount
|
|
87
85
|
* @param leverage Position leverage
|
|
86
|
+
* @param pairDepth 1% depth values for the pair
|
|
88
87
|
* @param oiWindowsSettings OI windows configuration
|
|
89
88
|
* @param oiWindows Current OI windows data
|
|
90
89
|
* @param context Additional context for the calculation
|
|
91
90
|
* @returns Total spread + cumulative volume price impact percentage
|
|
92
91
|
*/
|
|
93
|
-
export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number,
|
|
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;
|
|
94
93
|
/**
|
|
95
94
|
* @dev Convenience function for calculating cumulative volume price impact
|
|
96
95
|
* @dev Uses collateral and leverage instead of USD position size
|
|
@@ -104,6 +103,6 @@ export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, bu
|
|
|
104
103
|
export declare const getCumulVolPriceImpact: (buy: boolean, collateral: number, leverage: number, open: boolean, context: CumulVolContext & {
|
|
105
104
|
collateralPriceUsd: number;
|
|
106
105
|
}) => number;
|
|
107
|
-
export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: 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;
|
|
108
107
|
export { convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, } from "./converter";
|
|
109
108
|
export { buildCumulVolContext } from "./builder";
|
|
@@ -73,108 +73,6 @@ const getLegacyFactor = (context) => {
|
|
|
73
73
|
return context?.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
|
|
74
74
|
};
|
|
75
75
|
exports.getLegacyFactor = getLegacyFactor;
|
|
76
|
-
/**
|
|
77
|
-
* @dev Mirrors contract's _calculateDepthBandsPriceImpact function
|
|
78
|
-
* @param tradeSizeUsd Trade size in USD (always positive here)
|
|
79
|
-
* @param depthBandParams Depth band parameters
|
|
80
|
-
* @returns Price impact percentage
|
|
81
|
-
*/
|
|
82
|
-
const _calculateDepthBandsPriceImpact = (tradeSizeUsd, depthBandParams) => {
|
|
83
|
-
console.log("tradeSizeUsd", tradeSizeUsd);
|
|
84
|
-
console.log("depthBandParams", depthBandParams);
|
|
85
|
-
const totalDepthUsd = depthBandParams.depthBands.totalDepthUsd;
|
|
86
|
-
if (totalDepthUsd === 0 || tradeSizeUsd === 0)
|
|
87
|
-
return 0;
|
|
88
|
-
let remainingSizeUsd = tradeSizeUsd;
|
|
89
|
-
let totalWeightedPriceImpactP = 0;
|
|
90
|
-
let prevBandDepthUsd = 0;
|
|
91
|
-
let topOfPrevBandOffsetPpm = 0;
|
|
92
|
-
for (let i = 0; i < 30 && remainingSizeUsd !== 0; i++) {
|
|
93
|
-
const bandLiquidityPercentageBps = depthBandParams.depthBands.bands[i]; // Already in 0-1 format
|
|
94
|
-
const topOfBandOffsetPpm = depthBandParams.depthBandsMapping.bands[i]; // Already in 0-1 format
|
|
95
|
-
const bandDepthUsd = bandLiquidityPercentageBps * totalDepthUsd;
|
|
96
|
-
console.log("bandDepthUsd", bandDepthUsd);
|
|
97
|
-
// Skip if band has same depth as previous (would cause division by zero)
|
|
98
|
-
if (bandDepthUsd <= prevBandDepthUsd) {
|
|
99
|
-
console.log("Skipping band");
|
|
100
|
-
prevBandDepthUsd = bandDepthUsd;
|
|
101
|
-
topOfPrevBandOffsetPpm = topOfBandOffsetPpm;
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
// Since bandDepthUsd represents liquidity from mid price to top of band, we need to subtract previous band depth
|
|
105
|
-
const bandAvailableDepthUsd = bandDepthUsd - prevBandDepthUsd;
|
|
106
|
-
console.log("bandAvailableDepthUsd", bandAvailableDepthUsd);
|
|
107
|
-
let depthConsumedUsd;
|
|
108
|
-
// At 100% band always consume all remaining size, even if more than band available depth
|
|
109
|
-
if (bandLiquidityPercentageBps === 1 ||
|
|
110
|
-
remainingSizeUsd <= bandAvailableDepthUsd) {
|
|
111
|
-
depthConsumedUsd = remainingSizeUsd;
|
|
112
|
-
remainingSizeUsd = 0;
|
|
113
|
-
console.log("Consumed all remaining size");
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
// Normal case: consume entire band and continue to next
|
|
117
|
-
depthConsumedUsd = bandAvailableDepthUsd;
|
|
118
|
-
remainingSizeUsd -= bandAvailableDepthUsd;
|
|
119
|
-
console.log("Consumed entire band");
|
|
120
|
-
}
|
|
121
|
-
// Calculate impact contribution from this band using trapezoidal rule
|
|
122
|
-
// Low = previous band's price offset, High = current band's price offset
|
|
123
|
-
const lowOffsetP = topOfPrevBandOffsetPpm;
|
|
124
|
-
const offsetRangeP = topOfBandOffsetPpm - topOfPrevBandOffsetPpm;
|
|
125
|
-
console.log("lowOffsetP", lowOffsetP);
|
|
126
|
-
console.log("offsetRangeP", offsetRangeP);
|
|
127
|
-
console.log("depthConsumedUsd", depthConsumedUsd);
|
|
128
|
-
console.log("bandAvailableDepthUsd", bandAvailableDepthUsd);
|
|
129
|
-
// Calculate average impact using trapezoidal rule: low + (range * fraction / 2)
|
|
130
|
-
const avgImpactP = lowOffsetP +
|
|
131
|
-
(offsetRangeP * depthConsumedUsd) / bandAvailableDepthUsd / 2;
|
|
132
|
-
console.log("avgImpactP", avgImpactP);
|
|
133
|
-
totalWeightedPriceImpactP += avgImpactP * depthConsumedUsd;
|
|
134
|
-
console.log("totalWeightedPriceImpactP", totalWeightedPriceImpactP);
|
|
135
|
-
// Update previous values for next iteration
|
|
136
|
-
topOfPrevBandOffsetPpm = topOfBandOffsetPpm;
|
|
137
|
-
prevBandDepthUsd = bandDepthUsd;
|
|
138
|
-
}
|
|
139
|
-
console.log("totalWeightedPriceImpactP", totalWeightedPriceImpactP);
|
|
140
|
-
console.log("tradeSizeUsd", tradeSizeUsd);
|
|
141
|
-
return totalWeightedPriceImpactP / tradeSizeUsd;
|
|
142
|
-
};
|
|
143
|
-
/**
|
|
144
|
-
* @dev Mirrors contract's _getDepthBandsPriceImpactP function
|
|
145
|
-
* @param cumulativeVolumeUsd Cumulative volume in USD (can be negative)
|
|
146
|
-
* @param tradeSizeUsd Trade size in USD (can be negative)
|
|
147
|
-
* @param depthBandParams Depth band parameters (contains both pair bands and global mapping)
|
|
148
|
-
* @param priceImpactFactor Price impact factor (protection close factor)
|
|
149
|
-
* @param cumulativeFactor Cumulative factor for volume impact
|
|
150
|
-
* @returns Price impact percentage (can be negative)
|
|
151
|
-
*/
|
|
152
|
-
const _getDepthBandsPriceImpactP = (cumulativeVolumeUsd, tradeSizeUsd, depthBandParams, priceImpactFactor, cumulativeFactor) => {
|
|
153
|
-
// Check for opposite signs
|
|
154
|
-
if ((cumulativeVolumeUsd > 0 && tradeSizeUsd < 0) ||
|
|
155
|
-
(cumulativeVolumeUsd < 0 && tradeSizeUsd > 0)) {
|
|
156
|
-
throw new Error("Wrong params: cumulative volume and trade size have opposite signs");
|
|
157
|
-
}
|
|
158
|
-
const effectiveCumulativeVolumeUsd = cumulativeVolumeUsd * cumulativeFactor;
|
|
159
|
-
const totalSizeLookupUsd = effectiveCumulativeVolumeUsd + tradeSizeUsd;
|
|
160
|
-
const isNegative = totalSizeLookupUsd < 0;
|
|
161
|
-
const effectiveCumulativeVolumeUsdUint = isNegative
|
|
162
|
-
? -effectiveCumulativeVolumeUsd
|
|
163
|
-
: effectiveCumulativeVolumeUsd;
|
|
164
|
-
const totalSizeLookupUsdUint = isNegative
|
|
165
|
-
? -totalSizeLookupUsd
|
|
166
|
-
: totalSizeLookupUsd;
|
|
167
|
-
console.log("effectiveCumulativeVolumeUsdUint", effectiveCumulativeVolumeUsdUint);
|
|
168
|
-
console.log("totalSizeLookupUsdUint", totalSizeLookupUsdUint);
|
|
169
|
-
const cumulativeVolPriceImpactP = _calculateDepthBandsPriceImpact(effectiveCumulativeVolumeUsdUint, depthBandParams);
|
|
170
|
-
const totalSizePriceImpactP = _calculateDepthBandsPriceImpact(totalSizeLookupUsdUint, depthBandParams);
|
|
171
|
-
console.log("cumulativeVolPriceImpactP", cumulativeVolPriceImpactP);
|
|
172
|
-
console.log("totalSizePriceImpactP", totalSizePriceImpactP);
|
|
173
|
-
const unscaledPriceImpactP = cumulativeVolPriceImpactP +
|
|
174
|
-
(totalSizePriceImpactP - cumulativeVolPriceImpactP) / 2;
|
|
175
|
-
const scaledPriceImpactP = unscaledPriceImpactP * priceImpactFactor;
|
|
176
|
-
return isNegative ? -scaledPriceImpactP : scaledPriceImpactP;
|
|
177
|
-
};
|
|
178
76
|
/**
|
|
179
77
|
* @dev Calculates cumulative volume price impact percentage
|
|
180
78
|
* @dev Mirrors contract's getTradeCumulVolPriceImpactP function
|
|
@@ -188,9 +86,7 @@ const _getDepthBandsPriceImpactP = (cumulativeVolumeUsd, tradeSizeUsd, depthBand
|
|
|
188
86
|
* @param context Additional context with depths, OI data, and factors
|
|
189
87
|
* @returns Cumulative volume price impact percentage (not including spread)
|
|
190
88
|
*/
|
|
191
|
-
const getTradeCumulVolPriceImpactP = (
|
|
192
|
-
_pairIndex, // Unused - kept for compatibility
|
|
193
|
-
long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context) => {
|
|
89
|
+
const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context) => {
|
|
194
90
|
// Update context with passed parameters
|
|
195
91
|
const updatedContext = {
|
|
196
92
|
...context,
|
|
@@ -208,43 +104,34 @@ long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context)
|
|
|
208
104
|
(!open &&
|
|
209
105
|
context?.exemptAfterProtectionCloseFactor === true &&
|
|
210
106
|
(0, exports.isProtectionCloseFactorActive)(updatedContext) !== true)) {
|
|
211
|
-
console.log("No price impact, protection close factor");
|
|
212
107
|
return 0;
|
|
213
108
|
}
|
|
109
|
+
// Calculate trade skew direction (matches Solidity logic)
|
|
214
110
|
const tradePositiveSkew = (long && open) || (!long && !open);
|
|
215
111
|
const tradeSkewMultiplier = tradePositiveSkew ? 1 : -1;
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
112
|
+
// Select depth based on trade direction
|
|
113
|
+
// For positive skew (long open or short close), use depth above
|
|
114
|
+
// For negative skew (short open or long close), use depth below
|
|
115
|
+
const onePercentDepth = tradePositiveSkew
|
|
116
|
+
? context.pairDepth?.onePercentDepthAboveUsd
|
|
117
|
+
: context.pairDepth?.onePercentDepthBelowUsd;
|
|
118
|
+
let activeOi = undefined;
|
|
119
|
+
if (context.oiWindowsSettings !== undefined) {
|
|
120
|
+
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long);
|
|
221
121
|
}
|
|
222
|
-
|
|
223
|
-
const depthBands = tradePositiveSkew
|
|
224
|
-
? context.pairDepthBands.above
|
|
225
|
-
: context.pairDepthBands.below;
|
|
226
|
-
// Return 0 if no depth bands configured (matching contract lines 588-590)
|
|
227
|
-
if (!depthBands || depthBands.totalDepthUsd === 0) {
|
|
228
|
-
console.log("No price impact, depth bands");
|
|
122
|
+
if (!onePercentDepth || activeOi === undefined) {
|
|
229
123
|
return 0;
|
|
230
124
|
}
|
|
231
|
-
//
|
|
232
|
-
let activeOi = 0;
|
|
233
|
-
if (context.oiWindowsSettings !== undefined) {
|
|
234
|
-
activeOi =
|
|
235
|
-
(0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long) || 0;
|
|
236
|
-
}
|
|
237
|
-
console.log("activeOi", activeOi);
|
|
125
|
+
// Apply trade skew multiplier to match Solidity's signed calculation
|
|
238
126
|
const signedActiveOi = activeOi * tradeSkewMultiplier;
|
|
239
127
|
const signedTradeOi = tradeOpenInterestUsd * tradeSkewMultiplier;
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
return priceImpactP;
|
|
128
|
+
// Calculate impact with proper signs (matching Solidity's _getTradePriceImpactP)
|
|
129
|
+
const finalPriceImpactP = ((signedActiveOi * (0, exports.getCumulativeFactor)(updatedContext) +
|
|
130
|
+
signedTradeOi / 2) /
|
|
131
|
+
onePercentDepth /
|
|
132
|
+
(0, exports.getLegacyFactor)(updatedContext)) *
|
|
133
|
+
(0, exports.getProtectionCloseFactor)(updatedContext);
|
|
134
|
+
return finalPriceImpactP;
|
|
248
135
|
};
|
|
249
136
|
exports.getTradeCumulVolPriceImpactP = getTradeCumulVolPriceImpactP;
|
|
250
137
|
/**
|
|
@@ -295,12 +182,13 @@ exports.getSpreadP = getSpreadP;
|
|
|
295
182
|
* @param buy True for long, false for short
|
|
296
183
|
* @param collateral Collateral amount
|
|
297
184
|
* @param leverage Position leverage
|
|
185
|
+
* @param pairDepth 1% depth values for the pair
|
|
298
186
|
* @param oiWindowsSettings OI windows configuration
|
|
299
187
|
* @param oiWindows Current OI windows data
|
|
300
188
|
* @param context Additional context for the calculation
|
|
301
189
|
* @returns Total spread + cumulative volume price impact percentage
|
|
302
190
|
*/
|
|
303
|
-
const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverage, context) => {
|
|
191
|
+
const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, context) => {
|
|
304
192
|
if (pairSpreadP === undefined) {
|
|
305
193
|
return 0;
|
|
306
194
|
}
|
|
@@ -309,7 +197,16 @@ const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverag
|
|
|
309
197
|
const positionSizeUsd = collateral * leverage * (context?.collateralPriceUsd || 1);
|
|
310
198
|
const cumulVolImpact = (0, exports.getTradeCumulVolPriceImpactP)("", // trader - not used in calculation
|
|
311
199
|
0, // pairIndex - not used in calculation
|
|
312
|
-
buy, positionSizeUsd, context?.isPnlPositive || false, context?.isOpen !== false, context?.createdBlock || 0,
|
|
200
|
+
buy, positionSizeUsd, context?.isPnlPositive || false, context?.isOpen !== false, context?.createdBlock || 0, {
|
|
201
|
+
...context,
|
|
202
|
+
pairDepth,
|
|
203
|
+
oiWindowsSettings,
|
|
204
|
+
oiWindows,
|
|
205
|
+
});
|
|
206
|
+
// If no depth or OI data, return just half spread
|
|
207
|
+
if (cumulVolImpact === 0 && (!pairDepth || !oiWindowsSettings)) {
|
|
208
|
+
return pairSpreadP / 2;
|
|
209
|
+
}
|
|
313
210
|
return baseSpread + cumulVolImpact;
|
|
314
211
|
};
|
|
315
212
|
exports.getSpreadWithCumulVolPriceImpactP = getSpreadWithCumulVolPriceImpactP;
|
|
@@ -23,17 +23,14 @@ const getTradeOpeningPriceImpact = (input, context) => {
|
|
|
23
23
|
// Calculate fixed spread
|
|
24
24
|
const spreadP = (0, cumulVol_1.getFixedSpreadP)(input.pairSpreadP, input.long, true // opening
|
|
25
25
|
);
|
|
26
|
-
console.log("spreadP", spreadP);
|
|
27
26
|
// Calculate position size in USD
|
|
28
27
|
const positionSizeUsd = positionSizeCollateral * context.collateralPriceUsd;
|
|
29
|
-
console.log("positionSizeUsd", positionSizeUsd);
|
|
30
28
|
// Calculate cumulative volume price impact
|
|
31
29
|
const cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)("", // trader - not needed for calculation
|
|
32
30
|
input.pairIndex, input.long, positionSizeUsd, false, // isPnlPositive - not relevant for opening
|
|
33
31
|
true, // open
|
|
34
32
|
0, // lastPosIncreaseBlock - not relevant for opening
|
|
35
33
|
context.cumulVolContext);
|
|
36
|
-
console.log("cumulVolPriceImpactP", cumulVolPriceImpactP);
|
|
37
34
|
// Calculate price after spread and cumulative volume impact (before skew)
|
|
38
35
|
const priceAfterSpreadAndCumulVolPriceImpact = (0, __1.getPriceAfterImpact)(input.openPrice, spreadP + cumulVolPriceImpactP);
|
|
39
36
|
// Calculate position size in tokens using the price after fixed spread and cumul vol impact
|