@gainsnetwork/sdk 0.0.0-v10.rc21 → 0.0.0-v10.rc22

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.
@@ -39,12 +39,13 @@ export declare const getTradeValue: (collateral: number, pnlPercent: number, tot
39
39
  /**
40
40
  * @dev Comprehensive PnL calculation including all fees
41
41
  * @param trade The trade to calculate PnL for
42
- * @param currentPrice Current market price
42
+ * @param marketPrice Current market price (without price impact)
43
+ * @param executionPrice Price after all impacts (spread, skew, volume)
43
44
  * @param tradeInfo Trade info with version and timestamps
44
45
  * @param context Context with all fee parameters
45
46
  * @returns Detailed PnL breakdown
46
47
  */
47
- export declare const getComprehensivePnl: (trade: Trade, currentPrice: number, tradeInfo: TradeInfo, context: GetComprehensivePnlContext) => ComprehensivePnlResult;
48
+ export declare const getComprehensivePnl: (trade: Trade, marketPrice: number, executionPrice: number, tradeInfo: TradeInfo, context: GetComprehensivePnlContext) => ComprehensivePnlResult;
48
49
  /**
49
50
  * @dev Legacy getPnl function for backward compatibility
50
51
  * @deprecated Use getComprehensivePnl for new implementations
@@ -74,23 +74,24 @@ exports.getTradeValue = getTradeValue;
74
74
  /**
75
75
  * @dev Comprehensive PnL calculation including all fees
76
76
  * @param trade The trade to calculate PnL for
77
- * @param currentPrice Current market price
77
+ * @param marketPrice Current market price (without price impact)
78
+ * @param executionPrice Price after all impacts (spread, skew, volume)
78
79
  * @param tradeInfo Trade info with version and timestamps
79
80
  * @param context Context with all fee parameters
80
81
  * @returns Detailed PnL breakdown
81
82
  */
82
- const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
83
+ const getComprehensivePnl = (trade, marketPrice, executionPrice, tradeInfo, context) => {
83
84
  var _a;
84
- // Calculate base PnL percentage
85
- let pnlPercent = (0, exports.getPnlPercent)(trade.openPrice, currentPrice, trade.long, trade.leverage);
85
+ // Calculate both raw PnL (market price) and impact-adjusted PnL (execution price)
86
+ let rawPnlPercent = (0, exports.getPnlPercent)(trade.openPrice, marketPrice, trade.long, trade.leverage);
87
+ let impactPnlPercent = (0, exports.getPnlPercent)(trade.openPrice, executionPrice, trade.long, trade.leverage);
86
88
  if (!context.tradeData) {
87
89
  throw new Error("Trade data is undefined");
88
90
  }
89
91
  // Calculate position size
90
92
  const positionSizeCollateral = trade.collateralAmount * trade.leverage;
91
93
  // Calculate holding fees - always use getTradePendingHoldingFeesCollateral
92
- // This mirrors the contract's getTradeValueCollateral which always calls this function
93
- const pendingHoldingFees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeData.tradeFeesData, currentPrice, {
94
+ const pendingHoldingFees = (0, trading_1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeData.tradeFeesData, executionPrice, {
94
95
  contractsVersion: context.core.contractsVersion,
95
96
  currentTimestamp: context.core.currentTimestamp,
96
97
  collateralPriceUsd: context.core.collateralPriceUsd,
@@ -112,39 +113,56 @@ const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
112
113
  // Total fees
113
114
  const totalHoldingFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee;
114
115
  const totalFees = totalHoldingFees + closingFee;
115
- // Check liquidation
116
+ // Check liquidation (using raw PnL for liquidation check)
116
117
  const liquidationThreshold = ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.liquidationParams)
117
118
  ? (0, liquidation_1.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage) *
118
119
  -100
119
120
  : -90; // Default 90% loss
120
- const isLiquidated = pnlPercent <= liquidationThreshold;
121
- // If liquidated, set PnL to -100%
121
+ const isLiquidated = rawPnlPercent <= liquidationThreshold;
122
+ // If liquidated, set both PnL percentages to -100%
122
123
  if (isLiquidated) {
123
- pnlPercent = -100;
124
+ rawPnlPercent = -100;
125
+ impactPnlPercent = -100;
124
126
  }
125
127
  // Get realized PnL components from TradeFeesData
126
128
  const { totalRealizedPnlCollateral } = (0, exports.getTradeRealizedPnlCollateral)(context.tradeData.tradeFeesData);
127
- // Calculate final trade value
128
- const tradeValue = (0, exports.getTradeValue)(trade.collateralAmount, pnlPercent, totalFees);
129
- // Calculate PnL in collateral
130
- const pnlCollateral = trade.collateralAmount * (pnlPercent / 100);
131
- // Calculate leveraged position size
132
- const leveragedPositionSize = trade.collateralAmount * trade.leverage;
133
- // Calculate unrealized PnL (before closing fee, after holding fees)
134
- const uPnlCollateral = pnlCollateral - totalHoldingFees + totalRealizedPnlCollateral;
129
+ // Calculate raw PnL in collateral (using market price)
130
+ const rawPnlCollateral = trade.collateralAmount * (rawPnlPercent / 100);
131
+ // Calculate impact-adjusted PnL in collateral (using execution price)
132
+ const impactPnlCollateral = trade.collateralAmount * (impactPnlPercent / 100);
133
+ // Calculate price impact
134
+ const priceImpactCollateral = impactPnlCollateral - rawPnlCollateral;
135
+ const priceImpactPercent = impactPnlPercent - rawPnlPercent;
136
+ // Calculate unrealized PnL (before closing fee, after holding fees, using market price)
137
+ // This is what the trader sees for open positions
138
+ const uPnlCollateral = rawPnlCollateral - totalHoldingFees + totalRealizedPnlCollateral;
135
139
  const uPnlPercent = (uPnlCollateral / trade.collateralAmount) * 100;
136
- // Realized PnL (after all fees including closing)
137
- const realizedPnlCollateral = pnlCollateral - totalFees + totalRealizedPnlCollateral;
140
+ // Calculate realized PnL (after all fees including closing, using execution price)
141
+ // This is what the trader would get if closing the position
142
+ const realizedPnlCollateral = impactPnlCollateral - totalFees + totalRealizedPnlCollateral;
138
143
  const realizedPnlPercent = (realizedPnlCollateral / trade.collateralAmount) * 100;
144
+ // Calculate trade value using execution price (what trader would receive)
145
+ const tradeValue = (0, exports.getTradeValue)(trade.collateralAmount, impactPnlPercent, totalFees);
139
146
  return {
140
- // Core PnL values
141
- pnlPercent,
142
- pnlCollateral,
147
+ // Raw PnL values (using market price, no price impact)
148
+ pnlPercent: rawPnlPercent,
149
+ pnlCollateral: rawPnlCollateral,
150
+ // Impact-adjusted PnL values (using execution price)
151
+ impactPnlPercent,
152
+ impactPnlCollateral,
153
+ // Price impact
154
+ priceImpact: {
155
+ percent: priceImpactPercent,
156
+ collateral: priceImpactCollateral,
157
+ },
158
+ // Trade value (what trader would receive if closing)
143
159
  tradeValue,
144
- // Unrealized PnL (after holding fees, before closing fee)
160
+ // Unrealized PnL (after holding fees, before closing fee, using market price)
161
+ // Use for open position display
145
162
  uPnlCollateral,
146
163
  uPnlPercent,
147
- // Realized PnL (after all fees)
164
+ // Realized PnL (after all fees, using execution price)
165
+ // Use for closing preview
148
166
  realizedPnlCollateral,
149
167
  realizedPnlPercent,
150
168
  // Fee breakdown
@@ -157,9 +175,7 @@ const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
157
175
  },
158
176
  // Status flags
159
177
  isLiquidated,
160
- isProfitable: pnlPercent > 0,
161
- // Additional info
162
- leveragedPositionSize,
178
+ isProfitable: rawPnlPercent > 0, // Based on raw PnL
163
179
  };
164
180
  };
165
181
  exports.getComprehensivePnl = getComprehensivePnl;
@@ -42,16 +42,20 @@ export type PriceImpactBreakdown = {
42
42
  export type ComprehensivePnlResult = {
43
43
  pnlPercent: number;
44
44
  pnlCollateral: number;
45
+ impactPnlPercent: number;
46
+ impactPnlCollateral: number;
47
+ priceImpact: {
48
+ percent: number;
49
+ collateral: number;
50
+ };
45
51
  tradeValue: number;
46
52
  uPnlCollateral: number;
47
53
  uPnlPercent: number;
48
54
  realizedPnlCollateral: number;
49
55
  realizedPnlPercent: number;
50
56
  fees: FeeBreakdown;
51
- priceImpact?: PriceImpactBreakdown;
52
57
  isLiquidated: boolean;
53
58
  isProfitable: boolean;
54
- leveragedPositionSize: number;
55
59
  };
56
60
  /**
57
61
  * @dev Context for comprehensive PnL calculations with nested sub-contexts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.0.0-v10.rc21",
3
+ "version": "0.0.0-v10.rc22",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [