@gainsnetwork/sdk 0.0.0-v10.rc10 → 0.0.0-v10.rc11

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.
@@ -2,10 +2,21 @@
2
2
  * @dev PnL calculation module
3
3
  * @dev Provides functions matching v10 contract implementations
4
4
  */
5
- import { Trade, TradeInfo, LiquidationParams, Fee, GlobalTradeFeeParams } from "../types";
5
+ import { Trade, TradeInfo, LiquidationParams, Fee, GlobalTradeFeeParams, TradeFeesData } from "../types";
6
6
  import { ComprehensivePnlResult, GetComprehensivePnlContext } from "./types";
7
7
  import { BorrowingFee } from "../fees/borrowing";
8
8
  import { ContractsVersion } from "../../contracts/types";
9
+ /**
10
+ * @dev Gets trade realized PnL components from TradeFeesData
11
+ * @dev Mirrors contract's getTradeRealizedPnlCollateral function
12
+ * @param tradeFeesData Trade fees data containing realized components
13
+ * @returns Tuple of [realizedPnlCollateral, realizedTradingFeesCollateral, totalRealizedPnlCollateral]
14
+ */
15
+ export declare const getTradeRealizedPnlCollateral: (tradeFeesData: TradeFeesData) => {
16
+ realizedPnlCollateral: number;
17
+ realizedTradingFeesCollateral: number;
18
+ totalRealizedPnlCollateral: number;
19
+ };
9
20
  /**
10
21
  * @dev Calculates PnL percentage for a position
11
22
  * @dev Mirrors contract's getPnlPercent function
@@ -18,10 +18,27 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
18
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.getPriceForTargetPnlPercentage = exports.getPnl = exports.getComprehensivePnl = exports.getTradeValue = exports.getPnlPercent = void 0;
21
+ exports.getPriceForTargetPnlPercentage = exports.getPnl = exports.getComprehensivePnl = exports.getTradeValue = exports.getPnlPercent = exports.getTradeRealizedPnlCollateral = void 0;
22
22
  const borrowing_1 = require("../fees/borrowing");
23
23
  const trading_1 = require("../fees/trading");
24
24
  const liquidation_1 = require("../liquidation");
25
+ /**
26
+ * @dev Gets trade realized PnL components from TradeFeesData
27
+ * @dev Mirrors contract's getTradeRealizedPnlCollateral function
28
+ * @param tradeFeesData Trade fees data containing realized components
29
+ * @returns Tuple of [realizedPnlCollateral, realizedTradingFeesCollateral, totalRealizedPnlCollateral]
30
+ */
31
+ const getTradeRealizedPnlCollateral = (tradeFeesData) => {
32
+ const realizedPnlCollateral = tradeFeesData.realizedPnlCollateral;
33
+ const realizedTradingFeesCollateral = tradeFeesData.realizedTradingFeesCollateral;
34
+ const totalRealizedPnlCollateral = realizedPnlCollateral - realizedTradingFeesCollateral;
35
+ return {
36
+ realizedPnlCollateral,
37
+ realizedTradingFeesCollateral,
38
+ totalRealizedPnlCollateral,
39
+ };
40
+ };
41
+ exports.getTradeRealizedPnlCollateral = getTradeRealizedPnlCollateral;
25
42
  /**
26
43
  * @dev Calculates PnL percentage for a position
27
44
  * @dev Mirrors contract's getPnlPercent function
@@ -93,7 +110,8 @@ const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
93
110
  traderFeeMultiplier: context.trading.traderFeeMultiplier,
94
111
  });
95
112
  // Total fees
96
- const totalFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee + closingFee;
113
+ const totalHoldingFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee;
114
+ const totalFees = totalHoldingFees + closingFee;
97
115
  // Check liquidation
98
116
  const liquidationThreshold = ((_a = context.tradeData) === null || _a === void 0 ? void 0 : _a.liquidationParams)
99
117
  ? (0, liquidation_1.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage) *
@@ -104,20 +122,19 @@ const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
104
122
  if (isLiquidated) {
105
123
  pnlPercent = -100;
106
124
  }
125
+ // Get realized PnL components from TradeFeesData
126
+ const { totalRealizedPnlCollateral } = (0, exports.getTradeRealizedPnlCollateral)(context.tradeData.tradeFeesData);
107
127
  // Calculate final trade value
108
128
  const tradeValue = (0, exports.getTradeValue)(trade.collateralAmount, pnlPercent, totalFees);
109
129
  // Calculate PnL in collateral
110
130
  const pnlCollateral = trade.collateralAmount * (pnlPercent / 100);
111
131
  // Calculate leveraged position size
112
132
  const leveragedPositionSize = trade.collateralAmount * trade.leverage;
113
- // Calculate net PnL after fees
114
- const netPnlAfterFees = pnlCollateral - totalFees;
115
133
  // Calculate unrealized PnL (before closing fee, after holding fees)
116
- const totalHoldingFees = borrowingFeeV1 + borrowingFeeV2 + fundingFee;
117
- const uPnlCollateral = pnlCollateral - totalHoldingFees;
134
+ const uPnlCollateral = pnlCollateral - totalHoldingFees + totalRealizedPnlCollateral;
118
135
  const uPnlPercent = (uPnlCollateral / trade.collateralAmount) * 100;
119
136
  // Realized PnL (after all fees including closing)
120
- const realizedPnlCollateral = pnlCollateral - totalFees;
137
+ const realizedPnlCollateral = pnlCollateral - totalFees + totalRealizedPnlCollateral;
121
138
  const realizedPnlPercent = (realizedPnlCollateral / trade.collateralAmount) * 100;
122
139
  return {
123
140
  // Core PnL values
@@ -143,7 +160,6 @@ const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
143
160
  isProfitable: pnlPercent > 0,
144
161
  // Additional info
145
162
  leveragedPositionSize,
146
- netPnlAfterFees,
147
163
  };
148
164
  };
149
165
  exports.getComprehensivePnl = getComprehensivePnl;
@@ -52,7 +52,6 @@ export type ComprehensivePnlResult = {
52
52
  isLiquidated: boolean;
53
53
  isProfitable: boolean;
54
54
  leveragedPositionSize: number;
55
- netPnlAfterFees: number;
56
55
  };
57
56
  /**
58
57
  * @dev Context for comprehensive PnL calculations with nested sub-contexts
@@ -12,17 +12,20 @@ const types_1 = require("../../../contracts/types");
12
12
  var builder_1 = require("./builder");
13
13
  Object.defineProperty(exports, "buildTradeClosingPriceImpactContext", { enumerable: true, get: function () { return builder_1.buildTradeClosingPriceImpactContext; } });
14
14
  /**
15
- * @dev Calculates position size in tokens for partial close
16
- * @param originalPositionSizeToken Original position size in tokens
15
+ * @dev Calculates position size in tokens for the portion being closed
16
+ * @param positionSizeCollateral Position size in collateral units being closed
17
+ * @param originalPositionSizeToken Original total position size in tokens
17
18
  * @param originalCollateral Original collateral amount
18
- * @param closingCollateral Collateral amount being closed
19
- * @returns Proportional position size in tokens
19
+ * @param originalLeverage Original leverage
20
+ * @returns Position size in tokens for the closing portion
20
21
  */
21
- const calculateClosingPositionSizeToken = (originalPositionSizeToken, originalCollateral, closingCollateral) => {
22
- if (originalCollateral === 0)
22
+ const calculateClosingPositionSizeToken = (positionSizeCollateral, originalPositionSizeToken, originalCollateral, originalLeverage) => {
23
+ const totalPositionSizeCollateral = originalCollateral * originalLeverage;
24
+ if (totalPositionSizeCollateral === 0)
23
25
  return 0;
24
- // Proportional calculation: (closingCollateral / originalCollateral) * originalPositionSizeToken
25
- return (closingCollateral * originalPositionSizeToken) / originalCollateral;
26
+ // Match contract logic: (positionSizeCollateral * originalPositionSizeToken) / totalPositionSizeCollateral
27
+ return ((positionSizeCollateral * originalPositionSizeToken) /
28
+ totalPositionSizeCollateral);
26
29
  };
27
30
  /**
28
31
  * @dev Calculates all price impacts for trade closing
@@ -46,7 +49,7 @@ const getTradeClosingPriceImpact = (input, context) => {
46
49
  }
47
50
  // Calculate position size in tokens (proportional to collateral being closed)
48
51
  const positionSizeToken = input.trade.positionSizeToken
49
- ? calculateClosingPositionSizeToken(input.trade.positionSizeToken, input.trade.collateralAmount, input.positionSizeCollateral)
52
+ ? calculateClosingPositionSizeToken(input.positionSizeCollateral, input.trade.positionSizeToken, input.trade.collateralAmount, input.trade.leverage)
50
53
  : 0;
51
54
  // Calculate fixed spread (reversed for closing)
52
55
  const fixedSpreadP = (0, cumulVol_1.getFixedSpreadP)(input.pairSpreadP, input.trade.long, false // closing
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.0.0-v10.rc10",
3
+ "version": "0.0.0-v10.rc11",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [