@gainsnetwork/sdk 0.0.0-v10.rc14 → 0.0.0-v10.rc15
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.
|
@@ -8,6 +8,8 @@ exports.getTradeClosingPriceImpactAtOracle = exports.getTradeClosingPriceImpact
|
|
|
8
8
|
const cumulVol_1 = require("../cumulVol");
|
|
9
9
|
const skew_1 = require("../skew");
|
|
10
10
|
const types_1 = require("../../../contracts/types");
|
|
11
|
+
const pnl_1 = require("../../pnl");
|
|
12
|
+
const __1 = require("../");
|
|
11
13
|
// Export builder
|
|
12
14
|
var builder_1 = require("./builder");
|
|
13
15
|
Object.defineProperty(exports, "buildTradeClosingPriceImpactContext", { enumerable: true, get: function () { return builder_1.buildTradeClosingPriceImpactContext; } });
|
|
@@ -63,29 +65,16 @@ const getTradeClosingPriceImpact = (input, context) => {
|
|
|
63
65
|
false, // closing
|
|
64
66
|
context.tradeInfo.lastPosIncreaseBlock || context.tradeInfo.createdBlock, context.cumulVolContext);
|
|
65
67
|
// Calculate price with conservative impact
|
|
66
|
-
const priceWithImpact = input.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
tradeValueCollateralNoFactor = positionSizeToken * priceWithImpact;
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
// Short calculation: profit from price decrease
|
|
80
|
-
const pnlFactor = (2 * input.trade.openPrice - priceWithImpact) / input.trade.openPrice;
|
|
81
|
-
tradeValueCollateralNoFactor = input.positionSizeCollateral * pnlFactor;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
tradeValueCollateralNoFactor = input.positionSizeCollateral;
|
|
86
|
-
}
|
|
87
|
-
// Determine actual PnL
|
|
88
|
-
const isPnlPositive = tradeValueCollateralNoFactor > input.trade.collateralAmount;
|
|
68
|
+
const priceWithImpact = (0, __1.getPriceAfterImpact)(input.currentPairPrice, fixedSpreadP + cumulVolPriceImpactP);
|
|
69
|
+
// Calculate PnL percentage using the proper function
|
|
70
|
+
const pnlPercent = (0, pnl_1.getPnlPercent)(input.trade.openPrice, priceWithImpact, input.trade.long, input.trade.leverage);
|
|
71
|
+
// Calculate trade value using getTradeValue function
|
|
72
|
+
// Note: We don't include fees here as this is the raw trade value
|
|
73
|
+
tradeValueCollateralNoFactor = (0, pnl_1.getTradeValue)(input.trade.collateralAmount, pnlPercent, 0 // No fees for raw trade value calculation
|
|
74
|
+
);
|
|
75
|
+
// Determine actual PnL from the calculated percentage
|
|
76
|
+
const isPnlPositive = pnlPercent > 0;
|
|
77
|
+
console.log("isPnlPositive", isPnlPositive);
|
|
89
78
|
// Second pass: Recalculate with actual PnL if positive
|
|
90
79
|
if (isPnlPositive) {
|
|
91
80
|
cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)(input.trade.user, input.pairIndex, input.trade.long, positionSizeUsd, true, // Positive PnL
|
|
@@ -110,10 +99,8 @@ const getTradeClosingPriceImpact = (input, context) => {
|
|
|
110
99
|
// Total price impact (all components)
|
|
111
100
|
const totalPriceImpactP = fixedSpreadP + cumulVolPriceImpactP + skewPriceImpactP;
|
|
112
101
|
// Calculate final price after all impacts
|
|
113
|
-
//
|
|
114
|
-
const priceAfterImpact = input.
|
|
115
|
-
? input.currentPairPrice * (1 - totalPriceImpactP / 100)
|
|
116
|
-
: input.currentPairPrice / (1 - totalPriceImpactP / 100);
|
|
102
|
+
// The direction is already handled by getFixedSpreadP (reverses for closing)
|
|
103
|
+
const priceAfterImpact = (0, __1.getPriceAfterImpact)(input.currentPairPrice, totalPriceImpactP);
|
|
117
104
|
return {
|
|
118
105
|
positionSizeToken,
|
|
119
106
|
fixedSpreadP,
|
|
@@ -43,6 +43,13 @@ const isProtectionCloseFactorActive = (context) => {
|
|
|
43
43
|
context.protectionCloseFactor === undefined) {
|
|
44
44
|
return undefined;
|
|
45
45
|
}
|
|
46
|
+
console.log("context.isPnlPositive", context.isPnlPositive);
|
|
47
|
+
console.log("context.isOpen", context.isOpen);
|
|
48
|
+
console.log("context.protectionCloseFactor", context.protectionCloseFactor);
|
|
49
|
+
console.log("context.currentBlock", context.currentBlock);
|
|
50
|
+
console.log("context.createdBlock", context.createdBlock);
|
|
51
|
+
console.log("context.protectionCloseFactorBlocks", context.protectionCloseFactorBlocks);
|
|
52
|
+
console.log("context.protectionCloseFactorWhitelist", context.protectionCloseFactorWhitelist);
|
|
46
53
|
return (context.isPnlPositive === true &&
|
|
47
54
|
context.isOpen === false &&
|
|
48
55
|
context.protectionCloseFactor > 0 &&
|
|
@@ -88,7 +95,7 @@ exports.getLegacyFactor = getLegacyFactor;
|
|
|
88
95
|
* @returns Cumulative volume price impact percentage (not including spread)
|
|
89
96
|
*/
|
|
90
97
|
const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context) => {
|
|
91
|
-
var _a, _b
|
|
98
|
+
var _a, _b;
|
|
92
99
|
// Update context with passed parameters
|
|
93
100
|
const updatedContext = Object.assign(Object.assign({}, context), { isOpen: open, isPnlPositive: isPnlPositive, createdBlock: context.createdBlock || lastPosIncreaseBlock });
|
|
94
101
|
if (
|
|
@@ -103,15 +110,15 @@ const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterest
|
|
|
103
110
|
(0, exports.isProtectionCloseFactorActive)(updatedContext) !== true)) {
|
|
104
111
|
return 0;
|
|
105
112
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
// Calculate trade skew direction (matches Solidity logic)
|
|
114
|
+
const tradePositiveSkew = (long && open) || (!long && !open);
|
|
115
|
+
const tradeSkewMultiplier = tradePositiveSkew ? 1 : -1;
|
|
116
|
+
// Select depth based on trade direction
|
|
117
|
+
// For positive skew (long open or short close), use depth above
|
|
118
|
+
// For negative skew (short open or long close), use depth below
|
|
119
|
+
const onePercentDepth = tradePositiveSkew
|
|
120
|
+
? (_a = context.pairDepth) === null || _a === void 0 ? void 0 : _a.onePercentDepthAboveUsd
|
|
121
|
+
: (_b = context.pairDepth) === null || _b === void 0 ? void 0 : _b.onePercentDepthBelowUsd;
|
|
115
122
|
let activeOi = undefined;
|
|
116
123
|
if (context.oiWindowsSettings !== undefined) {
|
|
117
124
|
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long);
|
|
@@ -119,11 +126,21 @@ const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterest
|
|
|
119
126
|
if (!onePercentDepth || activeOi === undefined) {
|
|
120
127
|
return 0;
|
|
121
128
|
}
|
|
122
|
-
|
|
123
|
-
|
|
129
|
+
// Apply trade skew multiplier to match Solidity's signed calculation
|
|
130
|
+
const signedActiveOi = activeOi * tradeSkewMultiplier;
|
|
131
|
+
const signedTradeOi = tradeOpenInterestUsd * tradeSkewMultiplier;
|
|
132
|
+
// Calculate impact with proper signs (matching Solidity's _getTradePriceImpactP)
|
|
133
|
+
const finalPriceImpactP = ((signedActiveOi * (0, exports.getCumulativeFactor)(updatedContext) +
|
|
134
|
+
signedTradeOi / 2) /
|
|
124
135
|
onePercentDepth /
|
|
125
136
|
(0, exports.getLegacyFactor)(updatedContext)) *
|
|
126
137
|
(0, exports.getProtectionCloseFactor)(updatedContext);
|
|
138
|
+
console.log("signedActiveOi", signedActiveOi);
|
|
139
|
+
console.log("getCumulativeFactor", (0, exports.getCumulativeFactor)(updatedContext));
|
|
140
|
+
console.log("signedTradeOi", signedTradeOi);
|
|
141
|
+
console.log("onePercentDepth", onePercentDepth);
|
|
142
|
+
console.log("getLegacyFactor", (0, exports.getLegacyFactor)(updatedContext));
|
|
143
|
+
console.log("getProtectionCloseFactor", (0, exports.getProtectionCloseFactor)(updatedContext));
|
|
127
144
|
return finalPriceImpactP;
|
|
128
145
|
};
|
|
129
146
|
exports.getTradeCumulVolPriceImpactP = getTradeCumulVolPriceImpactP;
|
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
* @dev Main price impact module
|
|
3
3
|
* @dev Exports cumulative volume, skew, and combined opening/closing price impact functionality
|
|
4
4
|
*/
|
|
5
|
+
/**
|
|
6
|
+
* @dev Calculates price after impact using the same formula as the Solidity contract
|
|
7
|
+
* @dev Mirrors contract's getPriceAfterImpact function
|
|
8
|
+
* @param oraclePrice Base oracle price (no decimals requirement)
|
|
9
|
+
* @param totalPriceImpactP Total price impact percentage (can be positive or negative)
|
|
10
|
+
* @returns Price after impact has been applied
|
|
11
|
+
*/
|
|
12
|
+
export declare const getPriceAfterImpact: (oraclePrice: number, totalPriceImpactP: number) => number;
|
|
5
13
|
export { getTradeOpeningPriceImpact, getTradeOpeningPriceImpactAtMarket, buildTradeOpeningPriceImpactContext, TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult, } from "./open";
|
|
6
14
|
export { getTradeClosingPriceImpact, getTradeClosingPriceImpactAtOracle, buildTradeClosingPriceImpactContext, TradeClosingPriceImpactInput, TradeClosingPriceImpactContext, TradeClosingPriceImpactResult, } from "./close";
|
|
7
15
|
export { getTradeCumulVolPriceImpactP, getCumulVolPriceImpact, // Convenience function
|
|
@@ -4,7 +4,23 @@
|
|
|
4
4
|
* @dev Exports cumulative volume, skew, and combined opening/closing price impact functionality
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.buildSkewPriceImpactContext = exports.convertPairSkewDepths = exports.convertSkewDepth = 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.buildCumulVolContext = 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.buildTradeClosingPriceImpactContext = exports.getTradeClosingPriceImpactAtOracle = exports.getTradeClosingPriceImpact = exports.buildTradeOpeningPriceImpactContext = exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = void 0;
|
|
7
|
+
exports.buildSkewPriceImpactContext = exports.convertPairSkewDepths = exports.convertSkewDepth = 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.buildCumulVolContext = 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.buildTradeClosingPriceImpactContext = exports.getTradeClosingPriceImpactAtOracle = exports.getTradeClosingPriceImpact = exports.buildTradeOpeningPriceImpactContext = exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = exports.getPriceAfterImpact = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* @dev Calculates price after impact using the same formula as the Solidity contract
|
|
10
|
+
* @dev Mirrors contract's getPriceAfterImpact function
|
|
11
|
+
* @param oraclePrice Base oracle price (no decimals requirement)
|
|
12
|
+
* @param totalPriceImpactP Total price impact percentage (can be positive or negative)
|
|
13
|
+
* @returns Price after impact has been applied
|
|
14
|
+
*/
|
|
15
|
+
const getPriceAfterImpact = (oraclePrice, totalPriceImpactP) => {
|
|
16
|
+
// Match Solidity: price = oraclePrice + (oraclePrice * totalPriceImpactP / 100)
|
|
17
|
+
const priceAfterImpact = oraclePrice * (1 + totalPriceImpactP / 100);
|
|
18
|
+
if (priceAfterImpact <= 0) {
|
|
19
|
+
throw new Error("Price after impact must be positive");
|
|
20
|
+
}
|
|
21
|
+
return priceAfterImpact;
|
|
22
|
+
};
|
|
23
|
+
exports.getPriceAfterImpact = getPriceAfterImpact;
|
|
8
24
|
// Export trade opening price impact functionality
|
|
9
25
|
var open_1 = require("./open");
|
|
10
26
|
// Core functions
|
|
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = exports.buildTradeOpeningPriceImpactContext = void 0;
|
|
8
8
|
const cumulVol_1 = require("../cumulVol");
|
|
9
9
|
const skew_1 = require("../skew");
|
|
10
|
+
const __1 = require("../");
|
|
10
11
|
// Export builder
|
|
11
12
|
var builder_1 = require("./builder");
|
|
12
13
|
Object.defineProperty(exports, "buildTradeOpeningPriceImpactContext", { enumerable: true, get: function () { return builder_1.buildTradeOpeningPriceImpactContext; } });
|
|
@@ -44,18 +45,12 @@ const getTradeOpeningPriceImpact = (input, context) => {
|
|
|
44
45
|
// Total price impact (signed - can be positive or negative)
|
|
45
46
|
// Spread is always positive, impacts can be negative
|
|
46
47
|
const totalPriceImpactP = spreadP + cumulVolPriceImpactP + skewPriceImpactP;
|
|
47
|
-
// Calculate final price after impact
|
|
48
|
-
|
|
49
|
-
// For shorts: price decreases with positive impact
|
|
50
|
-
const priceImpactFactor = 1 + totalPriceImpactP / 100;
|
|
51
|
-
const priceAfterImpact = input.long
|
|
52
|
-
? input.openPrice * priceImpactFactor
|
|
53
|
-
: input.openPrice / priceImpactFactor;
|
|
48
|
+
// Calculate final price after impact using the same formula as Solidity
|
|
49
|
+
const priceAfterImpact = (0, __1.getPriceAfterImpact)(input.openPrice, totalPriceImpactP);
|
|
54
50
|
// Calculate percent profit from impact
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
: totalPriceImpactP; // Short gains when price goes up
|
|
51
|
+
// For longs: negative impact = profit (price goes down, good for buyer)
|
|
52
|
+
// For shorts: positive impact = profit (price goes up, good for seller)
|
|
53
|
+
const percentProfitP = -totalPriceImpactP;
|
|
59
54
|
return {
|
|
60
55
|
priceAfterImpact,
|
|
61
56
|
priceImpactP: Math.abs(totalPriceImpactP),
|