@gainsnetwork/sdk 0.0.0-v10.rc2 → 0.0.0-v10.rc4

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.
Files changed (181) hide show
  1. package/lib/backend/globalTrades/index.d.ts +11 -0
  2. package/lib/backend/globalTrades/index.js +69 -0
  3. package/lib/backend/index.d.ts +3 -0
  4. package/lib/backend/index.js +28 -0
  5. package/lib/backend/tradingVariables/backend.types.d.ts +312 -0
  6. package/lib/backend/tradingVariables/backend.types.js +2 -0
  7. package/lib/backend/tradingVariables/converter.d.ts +31 -0
  8. package/lib/backend/tradingVariables/converter.js +330 -0
  9. package/lib/backend/tradingVariables/index.d.ts +5 -0
  10. package/lib/backend/tradingVariables/index.js +95 -0
  11. package/lib/backend/tradingVariables/types.d.ts +109 -0
  12. package/lib/backend/tradingVariables/types.js +14 -0
  13. package/lib/constants.d.ts +10 -0
  14. package/lib/constants.js +11 -1
  15. package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
  16. package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
  17. package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
  18. package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
  19. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
  20. package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
  21. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
  22. package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
  23. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +7 -20
  24. package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
  25. package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
  26. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
  27. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
  28. package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
  29. package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
  30. package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
  31. package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
  32. package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
  33. package/lib/contracts/types/generated/GNSTrading.js +2 -0
  34. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
  35. package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
  36. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
  37. package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
  38. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
  39. package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
  40. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
  41. package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
  42. package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
  43. package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
  44. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
  45. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
  46. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
  47. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
  48. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
  49. package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
  50. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
  51. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
  52. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +7 -16
  53. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  54. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  55. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
  56. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
  57. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
  58. package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
  59. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
  60. package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
  61. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
  62. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
  63. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
  64. package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
  65. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
  66. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
  67. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
  68. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
  69. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
  70. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
  71. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
  72. package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
  73. package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
  74. package/lib/contracts/types/generated/factories/GToken__factory.js +4 -0
  75. package/lib/contracts/utils/index.d.ts +0 -3
  76. package/lib/contracts/utils/index.js +0 -3
  77. package/lib/contracts/utils/openTrades.js +14 -30
  78. package/lib/contracts/utils/pairs.js +10 -0
  79. package/lib/index.d.ts +2 -0
  80. package/lib/index.js +5 -0
  81. package/lib/markets/collateral/converter.d.ts +5 -0
  82. package/lib/markets/collateral/converter.js +11 -0
  83. package/lib/markets/collateral/index.d.ts +1 -0
  84. package/lib/markets/collateral/index.js +17 -0
  85. package/lib/markets/collateral/types.d.ts +7 -0
  86. package/lib/markets/collateral/types.js +2 -0
  87. package/lib/markets/index.d.ts +2 -0
  88. package/lib/markets/index.js +2 -0
  89. package/lib/markets/oi/converter.d.ts +63 -0
  90. package/lib/markets/oi/converter.js +103 -0
  91. package/lib/markets/oi/fetcher.d.ts +58 -0
  92. package/lib/markets/oi/fetcher.js +181 -0
  93. package/lib/markets/oi/index.d.ts +10 -0
  94. package/lib/markets/oi/index.js +37 -0
  95. package/lib/markets/oi/types.d.ts +82 -0
  96. package/lib/markets/oi/types.js +6 -0
  97. package/lib/markets/oi/validation.d.ts +80 -0
  98. package/lib/markets/oi/validation.js +172 -0
  99. package/lib/trade/fees/borrowing/builder.d.ts +14 -0
  100. package/lib/trade/fees/borrowing/builder.js +33 -0
  101. package/lib/trade/fees/borrowing/index.d.ts +16 -4
  102. package/lib/trade/fees/borrowing/index.js +43 -18
  103. package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
  104. package/lib/trade/fees/borrowingV2/builder.js +24 -0
  105. package/lib/trade/fees/borrowingV2/converter.d.ts +12 -3
  106. package/lib/trade/fees/borrowingV2/converter.js +29 -18
  107. package/lib/{contracts/fetch/fees/borrowingFeesV2.d.ts → trade/fees/borrowingV2/fetcher.d.ts} +2 -3
  108. package/lib/{contracts/fetch/fees/borrowingFeesV2.js → trade/fees/borrowingV2/fetcher.js} +6 -14
  109. package/lib/trade/fees/borrowingV2/index.d.ts +6 -17
  110. package/lib/trade/fees/borrowingV2/index.js +10 -37
  111. package/lib/trade/fees/borrowingV2/types.d.ts +22 -6
  112. package/lib/trade/fees/converter.d.ts +48 -0
  113. package/lib/trade/fees/converter.js +110 -0
  114. package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
  115. package/lib/trade/fees/fundingFees/builder.js +35 -0
  116. package/lib/{contracts/fetch/fees/fundingFees.d.ts → trade/fees/fundingFees/fetcher.d.ts} +2 -2
  117. package/lib/trade/fees/fundingFees/index.d.ts +13 -2
  118. package/lib/trade/fees/fundingFees/index.js +27 -3
  119. package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
  120. package/lib/trade/fees/fundingFees/pairContext.js +17 -0
  121. package/lib/trade/fees/index.d.ts +3 -2
  122. package/lib/trade/fees/index.js +44 -36
  123. package/lib/trade/fees/tiers/converter.d.ts +54 -0
  124. package/lib/trade/fees/tiers/converter.js +81 -0
  125. package/lib/trade/fees/tiers/index.d.ts +1 -0
  126. package/lib/trade/fees/tiers/index.js +1 -0
  127. package/lib/trade/fees/trading/builder.d.ts +18 -0
  128. package/lib/trade/fees/trading/builder.js +20 -0
  129. package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
  130. package/lib/trade/fees/trading/holdingFees.js +66 -0
  131. package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
  132. package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
  133. package/lib/trade/fees/trading/index.d.ts +30 -2
  134. package/lib/trade/fees/trading/index.js +52 -1
  135. package/lib/trade/fees/trading/types.d.ts +9 -0
  136. package/lib/trade/index.d.ts +1 -1
  137. package/lib/trade/index.js +1 -1
  138. package/lib/trade/liquidation/builder.d.ts +25 -0
  139. package/lib/trade/liquidation/builder.js +59 -0
  140. package/lib/trade/liquidation/converter.d.ts +23 -0
  141. package/lib/trade/liquidation/converter.js +46 -0
  142. package/lib/trade/liquidation/index.d.ts +26 -0
  143. package/lib/trade/liquidation/index.js +142 -0
  144. package/lib/trade/liquidation/types.d.ts +59 -0
  145. package/lib/trade/liquidation/types.js +2 -0
  146. package/lib/trade/pnl/builder.d.ts +16 -0
  147. package/lib/trade/pnl/builder.js +44 -0
  148. package/lib/trade/pnl/converter.d.ts +47 -0
  149. package/lib/trade/pnl/converter.js +72 -0
  150. package/lib/trade/pnl/index.d.ts +77 -0
  151. package/lib/trade/pnl/index.js +270 -0
  152. package/lib/trade/pnl/types.d.ts +114 -0
  153. package/lib/trade/pnl/types.js +5 -0
  154. package/lib/trade/priceImpact/close/index.d.ts +21 -0
  155. package/lib/trade/priceImpact/close/index.js +131 -0
  156. package/lib/trade/priceImpact/close/types.d.ts +43 -0
  157. package/lib/trade/priceImpact/close/types.js +5 -0
  158. package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
  159. package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
  160. package/lib/trade/priceImpact/cumulVol/index.d.ts +107 -0
  161. package/lib/trade/priceImpact/cumulVol/index.js +228 -0
  162. package/lib/trade/priceImpact/index.d.ts +6 -2
  163. package/lib/trade/priceImpact/index.js +30 -3
  164. package/lib/trade/priceImpact/open/index.d.ts +22 -0
  165. package/lib/trade/priceImpact/open/index.js +76 -0
  166. package/lib/trade/priceImpact/open/types.d.ts +41 -0
  167. package/lib/trade/priceImpact/open/types.js +5 -0
  168. package/lib/{contracts/fetch/priceImpact/skew.d.ts → trade/priceImpact/skew/fetcher.d.ts} +2 -2
  169. package/lib/{contracts/fetch/priceImpact/skew.js → trade/priceImpact/skew/fetcher.js} +6 -6
  170. package/lib/trade/priceImpact/skew/index.d.ts +1 -0
  171. package/lib/trade/priceImpact/skew/index.js +4 -0
  172. package/lib/trade/spread.d.ts +5 -18
  173. package/lib/trade/spread.js +17 -106
  174. package/lib/trade/types.d.ts +20 -8
  175. package/lib/trade/types.js +10 -0
  176. package/package.json +2 -2
  177. package/lib/trade/liquidation.d.ts +0 -42
  178. package/lib/trade/liquidation.js +0 -140
  179. package/lib/trade/pnl.d.ts +0 -10
  180. package/lib/trade/pnl.js +0 -38
  181. /package/lib/{contracts/fetch/fees/fundingFees.js → trade/fees/fundingFees/fetcher.js} +0 -0
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.calculateTradeSkewPriceImpact = exports.fetchCollateralDecimals = exports.fetchSkewPriceImpactContext = exports.fetchPairSkewDepths = exports.fetchPairSkewDepth = exports.fetchPairOisAfterV10Token = exports.fetchPairOiAfterV10Token = void 0;
13
- const priceImpact_1 = require("../../../trade/priceImpact");
13
+ const __1 = require("..");
14
14
  /**
15
15
  * @dev Fetches pair open interest in tokens for a specific pair
16
16
  * @param contract GNSMultiCollatDiamond contract instance
@@ -21,7 +21,7 @@ const priceImpact_1 = require("../../../trade/priceImpact");
21
21
  const fetchPairOiAfterV10Token = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
22
22
  try {
23
23
  const contractData = yield contract.getPairOiAfterV10Token(collateralIndex, pairIndex);
24
- return (0, priceImpact_1.convertPairOiToken)(contractData);
24
+ return (0, __1.convertPairOiToken)(contractData);
25
25
  }
26
26
  catch (error) {
27
27
  console.error("Error fetching pair OI token:", error);
@@ -42,7 +42,7 @@ const fetchPairOisAfterV10Token = (contract, collateralIndices, pairIndices) =>
42
42
  }
43
43
  try {
44
44
  const contractDataArray = yield contract.getPairOisAfterV10Token(collateralIndices, pairIndices);
45
- return contractDataArray.map(priceImpact_1.convertPairOiToken);
45
+ return contractDataArray.map(__1.convertPairOiToken);
46
46
  }
47
47
  catch (error) {
48
48
  console.error("Error fetching pair OIs token:", error);
@@ -61,7 +61,7 @@ exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
61
61
  const fetchPairSkewDepth = (contract, collateralIndex, pairIndex, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
62
62
  try {
63
63
  const contractDepth = yield contract.getPairSkewDepth(collateralIndex, pairIndex);
64
- return (0, priceImpact_1.normalizeSkewDepth)(contractDepth.toBigInt(), collateralDecimals);
64
+ return (0, __1.normalizeSkewDepth)(contractDepth.toBigInt(), collateralDecimals);
65
65
  }
66
66
  catch (error) {
67
67
  console.error("Error fetching skew depth:", error);
@@ -84,7 +84,7 @@ const fetchPairSkewDepths = (contract, collateralIndices, pairIndices, collatera
84
84
  }
85
85
  try {
86
86
  const contractDepths = yield contract.getPairSkewDepths(collateralIndices, pairIndices);
87
- return contractDepths.map((depth, i) => (0, priceImpact_1.normalizeSkewDepth)(depth.toBigInt(), collateralDecimals[i]));
87
+ return contractDepths.map((depth, i) => (0, __1.normalizeSkewDepth)(depth.toBigInt(), collateralDecimals[i]));
88
88
  }
89
89
  catch (error) {
90
90
  console.error("Error fetching skew depths:", error);
@@ -107,7 +107,7 @@ const fetchSkewPriceImpactContext = (contract, collateralIndices, pairIndices, c
107
107
  (0, exports.fetchPairOisAfterV10Token)(contract, collateralIndices, pairIndices),
108
108
  (0, exports.fetchPairSkewDepths)(contract, collateralIndices, pairIndices, collateralDecimals),
109
109
  ]);
110
- return (0, priceImpact_1.createSkewPriceImpactContext)(collateralIndices, pairIndices, skewDepths, pairOiTokens);
110
+ return (0, __1.createSkewPriceImpactContext)(collateralIndices, pairIndices, skewDepths, pairOiTokens);
111
111
  }
112
112
  catch (error) {
113
113
  console.error("Error fetching skew price impact context:", error);
@@ -55,3 +55,4 @@ export declare const getTradeSkewPriceImpactWithChecks: (params: TradeSkewParams
55
55
  */
56
56
  export declare const calculatePartialSizeToken: (originalSizeCollateral: number, deltaCollateral: number, originalSizeToken: number) => number;
57
57
  export * as SkewPriceImpact from "./types";
58
+ export * from "./fetcher";
@@ -26,6 +26,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  __setModuleDefault(result, mod);
27
27
  return result;
28
28
  };
29
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
30
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
31
+ };
29
32
  Object.defineProperty(exports, "__esModule", { value: true });
30
33
  exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpactWithChecks = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = void 0;
31
34
  const utils_1 = require("../../utils");
@@ -173,3 +176,4 @@ const calculatePartialSizeToken = (originalSizeCollateral, deltaCollateral, orig
173
176
  exports.calculatePartialSizeToken = calculatePartialSizeToken;
174
177
  // Export namespace for types
175
178
  exports.SkewPriceImpact = __importStar(require("./types"));
179
+ __exportStar(require("./fetcher"), exports);
@@ -1,18 +1,5 @@
1
- import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth, PairFactor, UserPriceImpact } from "./types";
2
- import { ContractsVersion } from "../contracts/types";
3
- export type SpreadContext = {
4
- isOpen?: boolean;
5
- isPnlPositive?: boolean;
6
- createdBlock?: number;
7
- liquidationParams?: LiquidationParams | undefined;
8
- currentBlock?: number | undefined;
9
- contractsVersion?: ContractsVersion | undefined;
10
- protectionCloseFactorWhitelist?: boolean;
11
- userPriceImpact?: UserPriceImpact | undefined;
12
- } & Partial<PairFactor>;
13
- export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
14
- export declare const isProtectionCloseFactorActive: (spreadCtx: SpreadContext | undefined) => boolean | undefined;
15
- export declare const getCumulativeFactor: (spreadCtx: SpreadContext | undefined) => number;
16
- export declare const getLegacyFactor: (spreadCtx: SpreadContext | undefined) => number;
17
- export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, spreadCtx?: SpreadContext | undefined) => number;
18
- export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation?: boolean | undefined, liquidationParams?: LiquidationParams | undefined, userPriceImpact?: UserPriceImpact | undefined) => number;
1
+ /**
2
+ * @dev Pure spread calculations without price impact
3
+ * @dev For price impact calculations, see priceImpact module
4
+ */
5
+ export { getSpreadWithPriceImpactP, getSpreadWithCumulVolPriceImpactP, getTradeCumulVolPriceImpactP, getCumulVolPriceImpact, getProtectionCloseFactor, isProtectionCloseFactorActive, getCumulativeFactor, getLegacyFactor, getFixedSpreadP, getSpreadP, CumulVolContext as SpreadContext, } from "./priceImpact/cumulVol";
@@ -1,108 +1,19 @@
1
1
  "use strict";
2
+ /**
3
+ * @dev Pure spread calculations without price impact
4
+ * @dev For price impact calculations, see priceImpact module
5
+ */
2
6
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSpreadP = exports.getSpreadWithPriceImpactP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = void 0;
4
- const oiWindows_1 = require("./oiWindows");
5
- const constants_1 = require("../constants");
6
- const types_1 = require("../contracts/types");
7
- const getProtectionCloseFactor = (spreadCtx) => {
8
- var _a;
9
- const protectionCloseFactor = spreadCtx === undefined ||
10
- spreadCtx.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
11
- spreadCtx.isOpen === undefined ||
12
- spreadCtx.isPnlPositive === undefined ||
13
- spreadCtx.protectionCloseFactor === undefined ||
14
- (0, exports.isProtectionCloseFactorActive)(spreadCtx) !== true
15
- ? constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR
16
- : spreadCtx.protectionCloseFactor;
17
- const protectionCloseFactorMultiplier = ((_a = spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) === null || _a === void 0 ? void 0 : _a.cumulVolPriceImpactMultiplier) !== undefined &&
18
- spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier > 0
19
- ? spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier
20
- : 1;
21
- return protectionCloseFactor * protectionCloseFactorMultiplier;
22
- };
23
- exports.getProtectionCloseFactor = getProtectionCloseFactor;
24
- const isProtectionCloseFactorActive = (spreadCtx) => {
25
- if (spreadCtx === undefined ||
26
- spreadCtx.currentBlock === undefined ||
27
- spreadCtx.createdBlock === undefined ||
28
- spreadCtx.protectionCloseFactorBlocks === undefined ||
29
- spreadCtx.protectionCloseFactor === undefined) {
30
- return undefined;
31
- }
32
- return (spreadCtx.isPnlPositive === true &&
33
- spreadCtx.isOpen === false &&
34
- spreadCtx.protectionCloseFactor > 0 &&
35
- spreadCtx.currentBlock <=
36
- spreadCtx.createdBlock + spreadCtx.protectionCloseFactorBlocks &&
37
- spreadCtx.protectionCloseFactorWhitelist !== true);
38
- };
39
- exports.isProtectionCloseFactorActive = isProtectionCloseFactorActive;
40
- const getCumulativeFactor = (spreadCtx) => {
41
- if (spreadCtx === undefined ||
42
- spreadCtx.cumulativeFactor === undefined ||
43
- spreadCtx.cumulativeFactor === 0) {
44
- return constants_1.DEFAULT_CUMULATIVE_FACTOR;
45
- }
46
- return spreadCtx.cumulativeFactor;
47
- };
48
- exports.getCumulativeFactor = getCumulativeFactor;
49
- const getLegacyFactor = (spreadCtx) => {
50
- return (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
51
- };
52
- exports.getLegacyFactor = getLegacyFactor;
53
- const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, spreadCtx) => {
54
- if (pairSpreadP === undefined) {
55
- return 0;
56
- }
57
- if (
58
- // No spread or price impact when closing pre-v9.2 trades
59
- ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
60
- (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) ||
61
- // No spread or price impact for opens when `pair.exemptOnOpen` is true
62
- ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === true && (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptOnOpen) === true) ||
63
- // No spread or price impact for closes after `protectionCloseFactor` has expired
64
- // when `pair.exemptAfterProtectionCloseFactor` is true
65
- ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
66
- (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptAfterProtectionCloseFactor) === true &&
67
- (0, exports.isProtectionCloseFactorActive)(spreadCtx) !== true)) {
68
- return 0;
69
- }
70
- const onePercentDepth = buy
71
- ? // if `long`
72
- (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
73
- ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
74
- : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
75
- : // if `short`
76
- (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
77
- ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
78
- : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
79
- let activeOi = undefined;
80
- if (oiWindowsSettings !== undefined) {
81
- activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(oiWindowsSettings), oiWindowsSettings.windowsCount, oiWindows, (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false ? buy : !buy);
82
- }
83
- if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
84
- return pairSpreadP / 2;
85
- }
86
- return ((0, exports.getSpreadP)(pairSpreadP, undefined, undefined, spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) +
87
- ((activeOi * (0, exports.getCumulativeFactor)(spreadCtx) + (collateral * leverage) / 2) /
88
- onePercentDepth /
89
- 100 /
90
- (0, exports.getLegacyFactor)(spreadCtx)) *
91
- (0, exports.getProtectionCloseFactor)(spreadCtx));
92
- };
93
- exports.getSpreadWithPriceImpactP = getSpreadWithPriceImpactP;
94
- const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
95
- var _a;
96
- const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
97
- if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
98
- return 0;
99
- }
100
- const spreadP = pairSpreadP / 2 + fixedSpreadP;
101
- return isLiquidation === true &&
102
- liquidationParams !== undefined &&
103
- liquidationParams.maxLiqSpreadP > 0 &&
104
- spreadP > liquidationParams.maxLiqSpreadP
105
- ? liquidationParams.maxLiqSpreadP
106
- : spreadP;
107
- };
108
- exports.getSpreadP = getSpreadP;
7
+ exports.getSpreadP = exports.getFixedSpreadP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = exports.getCumulVolPriceImpact = exports.getTradeCumulVolPriceImpactP = exports.getSpreadWithCumulVolPriceImpactP = exports.getSpreadWithPriceImpactP = void 0;
8
+ // Re-export from priceImpact/cumulVol for backward compatibility
9
+ var cumulVol_1 = require("./priceImpact/cumulVol");
10
+ Object.defineProperty(exports, "getSpreadWithPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithPriceImpactP; } });
11
+ Object.defineProperty(exports, "getSpreadWithCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithCumulVolPriceImpactP; } });
12
+ Object.defineProperty(exports, "getTradeCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getTradeCumulVolPriceImpactP; } });
13
+ Object.defineProperty(exports, "getCumulVolPriceImpact", { enumerable: true, get: function () { return cumulVol_1.getCumulVolPriceImpact; } });
14
+ Object.defineProperty(exports, "getProtectionCloseFactor", { enumerable: true, get: function () { return cumulVol_1.getProtectionCloseFactor; } });
15
+ Object.defineProperty(exports, "isProtectionCloseFactorActive", { enumerable: true, get: function () { return cumulVol_1.isProtectionCloseFactorActive; } });
16
+ Object.defineProperty(exports, "getCumulativeFactor", { enumerable: true, get: function () { return cumulVol_1.getCumulativeFactor; } });
17
+ Object.defineProperty(exports, "getLegacyFactor", { enumerable: true, get: function () { return cumulVol_1.getLegacyFactor; } });
18
+ Object.defineProperty(exports, "getFixedSpreadP", { enumerable: true, get: function () { return cumulVol_1.getFixedSpreadP; } });
19
+ Object.defineProperty(exports, "getSpreadP", { enumerable: true, get: function () { return cumulVol_1.getSpreadP; } });
@@ -13,6 +13,14 @@ export type TradeFeesData = {
13
13
  initialAccFundingFeeP: number;
14
14
  initialAccBorrowingFeeP: number;
15
15
  };
16
+ export type UiRealizedPnlData = {
17
+ realizedTradingFeesCollateral: number;
18
+ realizedOldBorrowingFeesCollateral: number;
19
+ realizedNewBorrowingFeesCollateral: number;
20
+ realizedFundingFeesCollateral: number;
21
+ realizedPnlPartialCloseCollateral: number;
22
+ pnlWithdrawnCollateral: number;
23
+ };
16
24
  export type CounterTradeSettings = {
17
25
  maxLeverage: number;
18
26
  feeRateMultiplier: number;
@@ -23,6 +31,7 @@ export type TradeContainer = {
23
31
  liquidationParams: LiquidationParams;
24
32
  initialAccFees: TradeInitialAccFees;
25
33
  tradeFeesData?: TradeFeesData;
34
+ uiRealizedPnlData?: UiRealizedPnlData;
26
35
  receivedAt?: number;
27
36
  };
28
37
  export type Trade = {
@@ -175,13 +184,6 @@ export type OiWindow = PairOi;
175
184
  export type OiWindows = {
176
185
  [key: string]: OiWindow;
177
186
  };
178
- export type CollateralConfig = {
179
- collateral: string;
180
- isActive: boolean;
181
- precision: number;
182
- precisionDelta: number;
183
- decimals?: number;
184
- };
185
187
  export type FeeTiers = {
186
188
  tiers: FeeTier[];
187
189
  multipliers: number[];
@@ -617,5 +619,15 @@ export declare enum PairIndex {
617
619
  SYRUPUSD = 373,
618
620
  SIGNUSD = 374,
619
621
  ZORAUSD = 375,
620
- COINUSD = 376
622
+ COINUSD = 376,
623
+ HOODUSD = 377,
624
+ MSTRUSD = 378,
625
+ NFLXUSD = 379,
626
+ LAUNCHCOINUSD = 380,
627
+ NXPCUSD = 381,
628
+ SOPHUSD = 382,
629
+ LPTUSD = 383,
630
+ BVIVUSD = 384,
631
+ EVIVUSD = 385,
632
+ CRCLUSD = 386
621
633
  }
@@ -406,4 +406,14 @@ var PairIndex;
406
406
  PairIndex[PairIndex["SIGNUSD"] = 374] = "SIGNUSD";
407
407
  PairIndex[PairIndex["ZORAUSD"] = 375] = "ZORAUSD";
408
408
  PairIndex[PairIndex["COINUSD"] = 376] = "COINUSD";
409
+ PairIndex[PairIndex["HOODUSD"] = 377] = "HOODUSD";
410
+ PairIndex[PairIndex["MSTRUSD"] = 378] = "MSTRUSD";
411
+ PairIndex[PairIndex["NFLXUSD"] = 379] = "NFLXUSD";
412
+ PairIndex[PairIndex["LAUNCHCOINUSD"] = 380] = "LAUNCHCOINUSD";
413
+ PairIndex[PairIndex["NXPCUSD"] = 381] = "NXPCUSD";
414
+ PairIndex[PairIndex["SOPHUSD"] = 382] = "SOPHUSD";
415
+ PairIndex[PairIndex["LPTUSD"] = 383] = "LPTUSD";
416
+ PairIndex[PairIndex["BVIVUSD"] = 384] = "BVIVUSD";
417
+ PairIndex[PairIndex["EVIVUSD"] = 385] = "EVIVUSD";
418
+ PairIndex[PairIndex["CRCLUSD"] = 386] = "CRCLUSD";
409
419
  })(PairIndex = exports.PairIndex || (exports.PairIndex = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.0.0-v10.rc2",
3
+ "version": "0.0.0-v10.rc4",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -98,7 +98,7 @@
98
98
  },
99
99
  "dependencies": {
100
100
  "@ethersproject/providers": "^5.7.2",
101
- "build": "^0.1.4",
101
+ "@gainsnetwork/contests": "^0.0.1",
102
102
  "ethcall": "^4.8.13",
103
103
  "ethers": "^5.7.2",
104
104
  "luxon": "^3.2.0"
@@ -1,42 +0,0 @@
1
- import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
2
- import { GetLiquidationFeesContext } from "./fees/trading";
3
- import { BorrowingFeeV2 } from "./fees/borrowingV2";
4
- import { Fee, LiquidationParams, Trade, TradeInfo, UserPriceImpact, TradeFeesData } from "./types";
5
- import { ContractsVersion } from "../contracts/types";
6
- export type GetLiquidationPriceContext = GetBorrowingFeeContext & BorrowingFeeV2.GetBorrowingFeeV2Context & GetLiquidationFeesContext & {
7
- liquidationParams: LiquidationParams | undefined;
8
- pairSpreadP: number | undefined;
9
- collateralPriceUsd: number | undefined;
10
- contractsVersion: ContractsVersion | undefined;
11
- userPriceImpact?: UserPriceImpact | undefined;
12
- currentPairPrice?: number;
13
- isCounterTrade?: boolean;
14
- tradeFeesData?: TradeFeesData;
15
- partialCloseMultiplier?: number;
16
- additionalFeeCollateral?: number;
17
- beforeOpened?: boolean;
18
- initialAccFees?: BorrowingFee.InitialAccFees;
19
- fundingParams?: Record<number, Record<number, any>>;
20
- fundingData?: Record<number, Record<number, any>>;
21
- pairOiAfterV10?: Record<number, Record<number, any>>;
22
- netExposureToken?: Record<number, Record<number, number>>;
23
- netExposureUsd?: Record<number, Record<number, number>>;
24
- };
25
- export type TradeHoldingFees = {
26
- fundingFeeCollateral: number;
27
- borrowingFeeCollateral: number;
28
- borrowingFeeCollateral_old: number;
29
- totalFeeCollateral: number;
30
- };
31
- /**
32
- * @dev Calculates total holding fees for a trade (funding + borrowing fees)
33
- * @param trade The trade to calculate fees for
34
- * @param tradeInfo Trade info containing contracts version
35
- * @param tradeFeesData Trade fees data containing initial acc fees
36
- * @param currentPairPrice Current pair price
37
- * @param context Context with fee parameters
38
- * @returns Object containing all holding fee components
39
- */
40
- export declare const getTradePendingHoldingFeesCollateral: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: GetLiquidationPriceContext) => TradeHoldingFees;
41
- export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
42
- export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
@@ -1,140 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getLiqPnlThresholdP = exports.getLiquidationPrice = exports.getTradePendingHoldingFeesCollateral = void 0;
4
- const fees_1 = require("./fees");
5
- const trading_1 = require("./fees/trading");
6
- const borrowingV2_1 = require("./fees/borrowingV2");
7
- const spread_1 = require("./spread");
8
- const types_1 = require("../contracts/types");
9
- /**
10
- * @dev Calculates total holding fees for a trade (funding + borrowing fees)
11
- * @param trade The trade to calculate fees for
12
- * @param tradeInfo Trade info containing contracts version
13
- * @param tradeFeesData Trade fees data containing initial acc fees
14
- * @param currentPairPrice Current pair price
15
- * @param context Context with fee parameters
16
- * @returns Object containing all holding fee components
17
- */
18
- const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
19
- var _a;
20
- // Calculate funding fees (v10+ only)
21
- const fundingFeeCollateral = ((_a = context.contractsVersion) !== null && _a !== void 0 ? _a : tradeInfo.contractsVersion) >=
22
- types_1.ContractsVersion.V10
23
- ? (0, fees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, context)
24
- : 0;
25
- // Calculate borrowing fees v2
26
- const borrowingFeeCollateral = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
27
- positionSizeCollateral: trade.collateralAmount * trade.leverage,
28
- openPrice: trade.openPrice,
29
- collateralIndex: trade.collateralIndex,
30
- pairIndex: trade.pairIndex,
31
- currentPairPrice,
32
- initialAccBorrowingFeeP: tradeFeesData.initialAccBorrowingFeeP,
33
- currentTimestamp: context.currentTimestamp,
34
- }, context);
35
- // Calculate v1 borrowing fees (some markets use v1 indefinitely)
36
- const borrowingFeeCollateral_old = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, context.initialAccFees || { accPairFee: 0, accGroupFee: 0, block: 0 }, // Use context initial fees or empty
37
- context);
38
- return {
39
- fundingFeeCollateral,
40
- borrowingFeeCollateral,
41
- borrowingFeeCollateral_old,
42
- totalFeeCollateral: fundingFeeCollateral +
43
- borrowingFeeCollateral +
44
- borrowingFeeCollateral_old,
45
- };
46
- };
47
- exports.getTradePendingHoldingFeesCollateral = getTradePendingHoldingFeesCollateral;
48
- const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
49
- var _a, _b, _c, _d, _e, _f;
50
- // Ensure initialAccFees is in context
51
- if (!context.initialAccFees) {
52
- context = Object.assign(Object.assign({}, context), { initialAccFees });
53
- }
54
- // 1. Calculate liquidation fees
55
- const closingFee = (0, trading_1.getTotalTradeLiqFeesCollateral)(0, // collateralIndex not used in calculation
56
- trade.user, trade.pairIndex, trade.collateralAmount, context);
57
- // 2. Calculate holding fees and realized PnL
58
- let holdingFeesTotal = 0;
59
- let totalRealizedPnlCollateral = 0;
60
- if (!context.beforeOpened &&
61
- context.tradeFeesData &&
62
- context.currentPairPrice) {
63
- // V10 data available - calculate full holding fees
64
- // Create a minimal tradeInfo from context
65
- const tradeInfo = {
66
- contractsVersion: (_a = context.contractsVersion) !== null && _a !== void 0 ? _a : types_1.ContractsVersion.V10,
67
- createdBlock: 0,
68
- tpLastUpdatedBlock: 0,
69
- slLastUpdatedBlock: 0,
70
- maxSlippageP: 0,
71
- lastOiUpdateTs: 0,
72
- collateralPriceUsd: (_b = context.collateralPriceUsd) !== null && _b !== void 0 ? _b : 0,
73
- lastPosIncreaseBlock: 0,
74
- };
75
- const holdingFees = (0, exports.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeFeesData, context.currentPairPrice, context);
76
- holdingFeesTotal = holdingFees.totalFeeCollateral;
77
- // Calculate total realized PnL (realized PnL minus realized trading fees)
78
- totalRealizedPnlCollateral =
79
- context.tradeFeesData.realizedPnlCollateral -
80
- context.tradeFeesData.realizedTradingFeesCollateral;
81
- }
82
- else if (!context.beforeOpened) {
83
- // Markets using v1 borrowing fees model
84
- holdingFeesTotal = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
85
- }
86
- // 3. Apply unified formula for all trades
87
- const partialCloseMultiplier = (_c = context.partialCloseMultiplier) !== null && _c !== void 0 ? _c : 1;
88
- const additionalFeeCollateral = (_d = context.additionalFeeCollateral) !== null && _d !== void 0 ? _d : 0;
89
- const totalFeesCollateral = closingFee +
90
- (holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
91
- additionalFeeCollateral;
92
- // 4. Calculate liquidation threshold
93
- const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
94
- // 5. Calculate liquidation price distance
95
- const collateralLiqNegativePnl = trade.collateralAmount * liqThresholdP;
96
- let liqPriceDistance = (trade.openPrice * (collateralLiqNegativePnl - totalFeesCollateral)) /
97
- trade.collateralAmount /
98
- trade.leverage;
99
- // 6. Apply closing spread for v9.2+
100
- if ((context === null || context === void 0 ? void 0 : context.contractsVersion) !== undefined &&
101
- context.contractsVersion >= types_1.ContractsVersion.V9_2 &&
102
- ((((_e = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _e === void 0 ? void 0 : _e.maxLiqSpreadP) !== undefined &&
103
- context.liquidationParams.maxLiqSpreadP > 0) ||
104
- (((_f = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _f === void 0 ? void 0 : _f.fixedSpreadP) !== undefined &&
105
- context.userPriceImpact.fixedSpreadP > 0))) {
106
- const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams, context.userPriceImpact);
107
- liqPriceDistance -= trade.openPrice * closingSpreadP;
108
- }
109
- // 7. Calculate final liquidation price
110
- return trade.long
111
- ? Math.max(trade.openPrice - liqPriceDistance, 0)
112
- : Math.max(trade.openPrice + liqPriceDistance, 0);
113
- };
114
- exports.getLiquidationPrice = getLiquidationPrice;
115
- const getLiqPnlThresholdP = (liquidationParams, leverage) => {
116
- if (liquidationParams === undefined ||
117
- leverage === undefined ||
118
- liquidationParams.maxLiqSpreadP === 0 ||
119
- liquidationParams.startLiqThresholdP === 0 ||
120
- liquidationParams.endLiqThresholdP === 0 ||
121
- liquidationParams.startLeverage === 0 ||
122
- liquidationParams.endLeverage === 0) {
123
- return 0.9;
124
- }
125
- if (leverage < liquidationParams.startLeverage) {
126
- return liquidationParams.startLiqThresholdP;
127
- }
128
- if (leverage > liquidationParams.endLeverage) {
129
- return liquidationParams.endLiqThresholdP;
130
- }
131
- if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
132
- return liquidationParams.endLiqThresholdP;
133
- }
134
- return (liquidationParams.startLiqThresholdP -
135
- ((leverage - liquidationParams.startLeverage) *
136
- (liquidationParams.startLiqThresholdP -
137
- liquidationParams.endLiqThresholdP)) /
138
- (liquidationParams.endLeverage - liquidationParams.startLeverage));
139
- };
140
- exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
@@ -1,10 +0,0 @@
1
- import { GetBorrowingFeeContext } from "./fees";
2
- import { GetTradeFeesContext } from "./fees/trading";
3
- import { LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
4
- import { ContractsVersion } from "../contracts/types";
5
- export type GetPnlContext = GetBorrowingFeeContext & GetTradeFeesContext & {
6
- collateralPriceUsd: number | undefined;
7
- contractsVersion: ContractsVersion | undefined;
8
- feeMultiplier: number | undefined;
9
- };
10
- export declare const getPnl: (price: number | undefined, trade: Trade, tradeInfo: TradeInfo, initialAccFees: TradeInitialAccFees, liquidationParams: LiquidationParams, useFees: boolean, context: GetPnlContext) => number[] | undefined;
package/lib/trade/pnl.js DELETED
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPnl = void 0;
4
- const fees_1 = require("./fees");
5
- const trading_1 = require("./fees/trading");
6
- const liquidation_1 = require("./liquidation");
7
- const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
8
- var _a;
9
- if (!price) {
10
- return;
11
- }
12
- const posCollat = trade.collateralAmount;
13
- const { openPrice, leverage } = trade;
14
- let pnlCollat = trade.long
15
- ? ((price - openPrice) / openPrice) * leverage * posCollat
16
- : ((openPrice - price) / openPrice) * leverage * posCollat;
17
- if (useFees) {
18
- pnlCollat -= (0, fees_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
19
- }
20
- let pnlPercentage = (pnlCollat / posCollat) * 100;
21
- // Can be liquidated
22
- if (pnlPercentage <=
23
- (0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
24
- pnlPercentage = -100;
25
- }
26
- else {
27
- // Calculate closing fee using the same function as opening fees
28
- const positionSizeCollateral = posCollat * trade.leverage;
29
- const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(0, // collateralIndex not used
30
- trade.user, trade.pairIndex, positionSizeCollateral, (_a = trade.isCounterTrade) !== null && _a !== void 0 ? _a : false, context);
31
- pnlCollat -= closingFee;
32
- pnlPercentage = (pnlCollat / posCollat) * 100;
33
- }
34
- pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
35
- pnlCollat = (posCollat * pnlPercentage) / 100;
36
- return [pnlCollat, pnlPercentage];
37
- };
38
- exports.getPnl = getPnl;