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

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.
@@ -180,6 +180,7 @@ export interface GlobalTradingVariablesBackend {
180
180
  groups: LiquidationParamsBackend[];
181
181
  pairs: LiquidationParamsBackend[];
182
182
  };
183
+ counterTradeSettings: CounterTradeSettingsBackend[];
183
184
  globalTradeFeeParams: GlobalTradeFeeParamsBackend;
184
185
  negPnlCumulVolMultiplier: string;
185
186
  congestionLevels: {
@@ -310,3 +311,7 @@ export type GlobalTradeFeeParamsBackend = {
310
311
  gnsOtcFeeP: string;
311
312
  gTokenFeeP: string;
312
313
  };
314
+ export type CounterTradeSettingsBackend = {
315
+ maxLeverage: string;
316
+ feeRateMultiplier: string;
317
+ };
@@ -50,6 +50,7 @@ const transformGlobalTradingVariables = (rawData) => {
50
50
  groups: ((_c = rawData.liquidationParams) === null || _c === void 0 ? void 0 : _c.groups.map(liqParams => (0, trade_1.convertLiquidationParams)(liqParams))) || [],
51
51
  pairs: ((_d = rawData.liquidationParams) === null || _d === void 0 ? void 0 : _d.pairs.map(liqParams => (0, trade_1.convertLiquidationParams)(liqParams))) || [],
52
52
  },
53
+ counterTradeSettings: (0, trade_1.convertCounterTradeSettingsArray)(rawData.counterTradeSettings),
53
54
  pairFactors: ((_f = (_e = rawData.pairInfos) === null || _e === void 0 ? void 0 : _e.pairFactors) === null || _f === void 0 ? void 0 : _f.map(factor => (0, converter_1.convertPairFactor)(factor))) || [],
54
55
  globalTradeFeeParams: rawData.globalTradeFeeParams
55
56
  ? (0, converter_1.convertGlobalTradeFeeParams)(rawData.globalTradeFeeParams)
@@ -1,5 +1,5 @@
1
1
  import { CollateralConfig } from "src/markets/collateral";
2
- import { BorrowingFee, BorrowingFeeV2, Fee, FeeTiers, FundingFees, GlobalTradeFeeParams, LeaderboardTrader, LiquidationParams, OiWindows, OiWindowsSettings, Pair, PairDepth, PairFactor, PairIndexes, TradingGroup } from "../../trade";
2
+ import { BorrowingFee, BorrowingFeeV2, CounterTradeSettings, Fee, FeeTiers, FundingFees, GlobalTradeFeeParams, LeaderboardTrader, LiquidationParams, OiWindows, OiWindowsSettings, Pair, PairDepth, PairFactor, PairIndexes, TradingGroup } from "../../trade";
3
3
  import { UnifiedPairOi } from "src/markets";
4
4
  export type TransformedGlobalTradingVariables = {
5
5
  globalTradingVariables: GlobalTradingVariablesType;
@@ -71,6 +71,7 @@ export type GlobalTradingVariablesType = {
71
71
  groups: LiquidationParams[];
72
72
  pairs: LiquidationParams[];
73
73
  };
74
+ counterTradeSettings: CounterTradeSettings[];
74
75
  pairFactors: PairFactor[];
75
76
  globalTradeFeeParams?: GlobalTradeFeeParams;
76
77
  congestionLevels: {
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * @dev Converters for trading fee data between contract and SDK formats
3
3
  */
4
+ import { CounterTradeSettingsBackend } from "src/backend";
4
5
  import { CounterTradeSettings } from "../../types";
5
6
  import { GlobalTradeFeeParams } from "./types";
6
7
  /**
@@ -10,6 +11,7 @@ import { GlobalTradeFeeParams } from "./types";
10
11
  * @returns Normalized counter trade settings
11
12
  */
12
13
  export declare const convertCounterTradeSettings: (feeRateMultiplier: number, maxLeverage: number) => CounterTradeSettings;
14
+ export declare const convertCounterTradeSettingsArray: (settings: CounterTradeSettingsBackend[]) => CounterTradeSettings[];
13
15
  /**
14
16
  * @dev Converts array of counter trade fee rate multipliers from contract
15
17
  * @param multipliers Array of fee rate multipliers (1e3 precision)
@@ -3,7 +3,7 @@
3
3
  * @dev Converters for trading fee data between contract and SDK formats
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.convertGlobalTradeFeeParams = exports.convertCounterTradeFeeRateMultipliers = exports.convertCounterTradeSettings = void 0;
6
+ exports.convertGlobalTradeFeeParams = exports.convertCounterTradeFeeRateMultipliers = exports.convertCounterTradeSettingsArray = exports.convertCounterTradeSettings = void 0;
7
7
  /**
8
8
  * @dev Converts contract counter trade settings to SDK format
9
9
  * @param feeRateMultiplier Fee rate multiplier from contract (1e3 precision)
@@ -17,6 +17,10 @@ const convertCounterTradeSettings = (feeRateMultiplier, maxLeverage) => {
17
17
  };
18
18
  };
19
19
  exports.convertCounterTradeSettings = convertCounterTradeSettings;
20
+ const convertCounterTradeSettingsArray = (settings) => {
21
+ return settings.map(setting => (0, exports.convertCounterTradeSettings)(Number(setting.feeRateMultiplier), Number(setting.maxLeverage)));
22
+ };
23
+ exports.convertCounterTradeSettingsArray = convertCounterTradeSettingsArray;
20
24
  /**
21
25
  * @dev Converts array of counter trade fee rate multipliers from contract
22
26
  * @param multipliers Array of fee rate multipliers (1e3 precision)
@@ -45,7 +45,7 @@ export type GetStructuredHoldingFeesContext = {
45
45
  borrowingV1?: GetBorrowingFeeContext;
46
46
  borrowingV2?: GetPairBorrowingFeeV2Context;
47
47
  funding?: GetPairFundingFeeContext;
48
- initialAccFeesV1?: BorrowingFee.InitialAccFees;
48
+ initialAccFees?: BorrowingFee.InitialAccFees;
49
49
  };
50
50
  /**
51
51
  * @dev Calculates total holding fees for a trade (funding + borrowing fees)
@@ -136,8 +136,8 @@ const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, c
136
136
  }
137
137
  // Calculate v1 borrowing fees (some markets use v1 indefinitely)
138
138
  let borrowingFeeCollateral_old = 0;
139
- if (context.borrowingV1 && context.initialAccFeesV1) {
140
- borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFeesV1, context.borrowingV1);
139
+ if (context.borrowingV1 && context.initialAccFees) {
140
+ borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFees, context.borrowingV1);
141
141
  }
142
142
  return {
143
143
  fundingFeeCollateral,
@@ -37,13 +37,13 @@ const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, ad
37
37
  borrowingV1: (0, builder_1.buildBorrowingV1Context)(globalTradingVariables, collateralIndex, additionalParams.currentBlock),
38
38
  borrowingV2: (0, builder_2.buildBorrowingV2Context)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
39
39
  funding: (0, builder_3.buildFundingContext)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
40
- trading: (0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier),
40
+ trading: Object.assign(Object.assign({}, (0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier)), { counterTradeSettings: globalTradingVariables.counterTradeSettings, userPriceImpact: additionalParams.userPriceImpact }),
41
41
  // Trade-specific data
42
42
  tradeData: {
43
43
  tradeInfo,
44
44
  tradeFeesData: tradeContainer.tradeFeesData,
45
45
  liquidationParams: tradeContainer.liquidationParams,
46
- initialAccFeesV1: tradeContainer.initialAccFees,
46
+ initialAccFees: tradeContainer.initialAccFees,
47
47
  },
48
48
  // Additional parameters for liquidation calculation
49
49
  liquidationSpecific: {
@@ -11,16 +11,6 @@ import { GetLiquidationPriceContext } from "./types";
11
11
  */
12
12
  export declare const getLiquidationPrice: (trade: Trade, context: GetLiquidationPriceContext) => number;
13
13
  export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
14
- /**
15
- * @dev Simplified wrapper for getTradeLiquidationPrice
16
- * @dev Mirrors the contract's simplified overload
17
- * @param trade The trade to calculate liquidation price for
18
- * @param additionalFeeCollateral Additional fees to consider
19
- * @param currentPairPrice Current pair price
20
- * @param context Context with all required data
21
- * @returns Liquidation price
22
- */
23
- export declare const getTradeLiquidationPriceSimple: (trade: Trade, additionalFeeCollateral: number, currentPairPrice: number, context: GetLiquidationPriceContext) => number;
24
14
  export { convertLiquidationParams, convertLiquidationParamsArray, encodeLiquidationParams, } from "./converter";
25
15
  export * from "./types";
26
16
  export * from "./builder";
@@ -17,7 +17,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
17
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.encodeLiquidationParams = exports.convertLiquidationParamsArray = exports.convertLiquidationParams = exports.getTradeLiquidationPriceSimple = exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
20
+ exports.encodeLiquidationParams = exports.convertLiquidationParamsArray = exports.convertLiquidationParams = exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
21
21
  const types_1 = require("../../contracts/types");
22
22
  const __1 = require("..");
23
23
  /**
@@ -27,20 +27,23 @@ const __1 = require("..");
27
27
  * @returns Liquidation price
28
28
  */
29
29
  const getLiquidationPrice = (trade, context) => {
30
- var _a, _b, _c;
31
- // Extract legacy parameters from structured context
32
- const { currentPairPrice, additionalFeeCollateral, partialCloseMultiplier, beforeOpened, } = context.liquidationSpecific;
33
- // 1. Calculate liquidation fees
34
- const closingFee = (0, __1.getTotalTradeLiqFeesCollateral)(trade.collateralIndex, trade.user, trade.pairIndex, trade.collateralAmount, {
35
- totalLiqCollateralFeeP: ((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.endLiqThresholdP) || 0.9,
30
+ var _a, _b;
31
+ // Extract parameters from structured context
32
+ const { currentPairPrice, additionalFeeCollateral = 0, partialCloseMultiplier = 1, beforeOpened = false, isCounterTrade = false, } = context.liquidationSpecific;
33
+ // 1. Calculate closing fees
34
+ const closingFee = (0, __1.getTotalTradeFeesCollateral)(trade.collateralIndex, "", // No fee tiers applied for liquidation calculation
35
+ trade.pairIndex, trade.collateralAmount * trade.leverage, isCounterTrade, {
36
+ fee: context.trading.fee,
37
+ collateralPriceUsd: context.core.collateralPriceUsd,
36
38
  globalTradeFeeParams: context.trading.globalTradeFeeParams,
37
- traderFeeMultiplier: context.trading.traderFeeMultiplier,
39
+ traderFeeMultiplier: 1,
40
+ counterTradeSettings: context.trading.counterTradeSettings,
38
41
  });
39
- // 2. Calculate holding fees and realized PnL
42
+ // 2. Calculate holding fees and realized PnL for opened trades
40
43
  let holdingFeesTotal = 0;
41
44
  let totalRealizedPnlCollateral = 0;
42
- if (!beforeOpened && context.tradeData.tradeFeesData && currentPairPrice) {
43
- // V10 data available - calculate full holding fees
45
+ if (!beforeOpened) {
46
+ // Calculate holding fees
44
47
  const holdingFees = (0, __1.getTradePendingHoldingFeesCollateral)(trade, context.tradeData.tradeInfo, context.tradeData.tradeFeesData, currentPairPrice, {
45
48
  contractsVersion: context.core.contractsVersion,
46
49
  currentTimestamp: context.core.currentTimestamp,
@@ -48,7 +51,7 @@ const getLiquidationPrice = (trade, context) => {
48
51
  borrowingV1: context.borrowingV1,
49
52
  borrowingV2: context.borrowingV2,
50
53
  funding: context.funding,
51
- initialAccFeesV1: context.tradeData.initialAccFeesV1,
54
+ initialAccFees: context.tradeData.initialAccFees,
52
55
  });
53
56
  holdingFeesTotal = holdingFees.totalFeeCollateral;
54
57
  // Calculate total realized PnL (realized PnL minus realized trading fees)
@@ -56,12 +59,6 @@ const getLiquidationPrice = (trade, context) => {
56
59
  context.tradeData.tradeFeesData.realizedPnlCollateral -
57
60
  context.tradeData.tradeFeesData.realizedTradingFeesCollateral;
58
61
  }
59
- else if (!beforeOpened &&
60
- context.borrowingV1 &&
61
- context.tradeData.initialAccFeesV1) {
62
- // Markets using v1 borrowing fees model
63
- holdingFeesTotal = (0, __1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, context.tradeData.initialAccFeesV1, context.borrowingV1);
64
- }
65
62
  // 3. Apply unified formula for all trades
66
63
  const totalFeesCollateral = closingFee +
67
64
  (holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
@@ -75,9 +72,9 @@ const getLiquidationPrice = (trade, context) => {
75
72
  trade.leverage;
76
73
  // 6. Apply closing spread for v9.2+
77
74
  if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
78
- ((((_b = context.tradeData.liquidationParams) === null || _b === void 0 ? void 0 : _b.maxLiqSpreadP) !== undefined &&
75
+ ((((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
79
76
  context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
80
- (((_c = context.liquidationSpecific.userPriceImpact) === null || _c === void 0 ? void 0 : _c.fixedSpreadP) !==
77
+ (((_b = context.liquidationSpecific.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !==
81
78
  undefined &&
82
79
  context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
83
80
  const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
@@ -115,22 +112,6 @@ const getLiqPnlThresholdP = (liquidationParams, leverage) => {
115
112
  (liquidationParams.endLeverage - liquidationParams.startLeverage));
116
113
  };
117
114
  exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
118
- /**
119
- * @dev Simplified wrapper for getTradeLiquidationPrice
120
- * @dev Mirrors the contract's simplified overload
121
- * @param trade The trade to calculate liquidation price for
122
- * @param additionalFeeCollateral Additional fees to consider
123
- * @param currentPairPrice Current pair price
124
- * @param context Context with all required data
125
- * @returns Liquidation price
126
- */
127
- const getTradeLiquidationPriceSimple = (trade, additionalFeeCollateral, currentPairPrice, context) => {
128
- // Build complete context with additional parameters
129
- const fullContext = Object.assign(Object.assign({}, context), { liquidationSpecific: Object.assign(Object.assign({}, context.liquidationSpecific), { currentPairPrice,
130
- additionalFeeCollateral, partialCloseMultiplier: 1, beforeOpened: false, isCounterTrade: trade.isCounterTrade || false }) });
131
- return (0, exports.getLiquidationPrice)(trade, fullContext);
132
- };
133
- exports.getTradeLiquidationPriceSimple = getTradeLiquidationPriceSimple;
134
115
  // Converters
135
116
  var converter_1 = require("./converter");
136
117
  Object.defineProperty(exports, "convertLiquidationParams", { enumerable: true, get: function () { return converter_1.convertLiquidationParams; } });
@@ -1,29 +1,9 @@
1
1
  import { GetBorrowingFeeContext, BorrowingFee } from "./../fees";
2
- import { LiquidationParams, UserPriceImpact, TradeFeesData, TradeInfo } from "./../types";
2
+ import { LiquidationParams, UserPriceImpact, TradeFeesData, TradeInfo, CounterTradeSettings } from "./../types";
3
3
  import { ContractsVersion } from "../../contracts/types";
4
4
  import { GetPairBorrowingFeeV2Context } from "../fees/borrowingV2";
5
5
  import { GetPairFundingFeeContext } from "../fees/fundingFees";
6
6
  import { TradingFeesSubContext } from "../fees/trading/builder";
7
- /**
8
- * @dev Input parameters for getTradeLiquidationPrice
9
- * @dev Mirrors the contract's LiqPriceInput struct
10
- */
11
- export type LiqPriceInput = {
12
- collateralIndex: number;
13
- trader: string;
14
- pairIndex: number;
15
- index: number;
16
- openPrice: number;
17
- long: boolean;
18
- collateral: number;
19
- leverage: number;
20
- additionalFeeCollateral: number;
21
- liquidationParams: LiquidationParams;
22
- currentPairPrice: number;
23
- isCounterTrade: boolean;
24
- partialCloseMultiplier: number;
25
- beforeOpened: boolean;
26
- };
27
7
  /**
28
8
  * @dev Structured context for liquidation price calculations
29
9
  * @dev Follows the same pattern as GetComprehensivePnlContext
@@ -41,12 +21,15 @@ export type GetLiquidationPriceContext = {
41
21
  funding?: GetPairFundingFeeContext;
42
22
  trading: TradingFeesSubContext & {
43
23
  userPriceImpact?: UserPriceImpact;
24
+ counterTradeSettings?: {
25
+ [pairIndex: number]: CounterTradeSettings;
26
+ };
44
27
  };
45
28
  tradeData: {
46
29
  tradeInfo: TradeInfo;
47
30
  tradeFeesData: TradeFeesData;
48
31
  liquidationParams: LiquidationParams;
49
- initialAccFeesV1?: BorrowingFee.InitialAccFees;
32
+ initialAccFees?: BorrowingFee.InitialAccFees;
50
33
  };
51
34
  liquidationSpecific: {
52
35
  currentPairPrice: number;
@@ -36,7 +36,7 @@ const buildComprehensivePnlContext = (globalTradingVariables, tradeContainer, ad
36
36
  ? {
37
37
  tradeFeesData: tradeContainer.tradeFeesData,
38
38
  liquidationParams: tradeContainer.liquidationParams,
39
- initialAccFeesV1: tradeContainer.initialAccFees,
39
+ initialAccFees: tradeContainer.initialAccFees,
40
40
  }
41
41
  : undefined,
42
42
  };
@@ -80,7 +80,7 @@ const getComprehensivePnl = (trade, currentPrice, tradeInfo, context) => {
80
80
  borrowingV1: context.borrowingV1,
81
81
  borrowingV2: context.borrowingV2,
82
82
  funding: context.funding,
83
- initialAccFeesV1: context.tradeData.initialAccFeesV1,
83
+ initialAccFees: context.tradeData.initialAccFees,
84
84
  });
85
85
  const borrowingFeeV1 = pendingHoldingFees.borrowingFeeCollateral_old;
86
86
  const borrowingFeeV2 = pendingHoldingFees.borrowingFeeCollateral;
@@ -236,7 +236,7 @@ const getPriceForTargetPnlPercentage = (targetPnlPercent, trade, tradeInfo, cont
236
236
  borrowingV1: context.borrowingV1,
237
237
  borrowingV2: context.borrowingV2,
238
238
  funding: context.funding,
239
- initialAccFeesV1: (_b = context.tradeData) === null || _b === void 0 ? void 0 : _b.initialAccFeesV1,
239
+ initialAccFees: (_b = context.tradeData) === null || _b === void 0 ? void 0 : _b.initialAccFees,
240
240
  });
241
241
  const totalHoldingFees = fees.fundingFeeCollateral +
242
242
  fees.borrowingFeeCollateral +
@@ -5,21 +5,7 @@ import { ContractsVersion } from "src/contracts/types";
5
5
  import { BorrowingFee, GetBorrowingFeeContext, TradingFeesSubContext } from "../fees";
6
6
  import { GetPairBorrowingFeeV2Context } from "../fees/borrowingV2";
7
7
  import { GetPairFundingFeeContext } from "../fees/fundingFees";
8
- import { Trade, TradeInfo, TradeFeesData, LiquidationParams } from "../types";
9
- /**
10
- * @dev Input for trade value calculation with all fees
11
- * @dev Mirrors contract's TradeValueInput struct
12
- */
13
- export type TradeValueInput = {
14
- trade: Trade;
15
- currentPrice: number;
16
- collateralPriceUsd: number;
17
- fees: {
18
- openingFeeCollateral: number;
19
- closingFeeCollateral: number;
20
- holdingFeesCollateral: number;
21
- };
22
- };
8
+ import { TradeFeesData, LiquidationParams } from "../types";
23
9
  /**
24
10
  * @dev Result of trade value calculation
25
11
  */
@@ -68,30 +54,6 @@ export type ComprehensivePnlResult = {
68
54
  leveragedPositionSize: number;
69
55
  netPnlAfterFees: number;
70
56
  };
71
- /**
72
- * @dev Simple PnL result for backward compatibility
73
- */
74
- export type SimplePnlResult = [pnlCollateral: number, pnlPercent: number];
75
- /**
76
- * @dev Input for PnL calculation with price impact
77
- */
78
- export type PnlWithPriceImpactInput = {
79
- trade: Trade;
80
- tradeInfo: TradeInfo;
81
- oraclePrice: number;
82
- currentPairPrice: number;
83
- usePriceImpact: boolean;
84
- includeOpeningFees?: boolean;
85
- };
86
- /**
87
- * @dev Context for v10 PnL calculations
88
- */
89
- export type V10PnlContext = {
90
- tradeFeesData: TradeFeesData;
91
- priceImpactContext?: any;
92
- skewContext?: any;
93
- cumulVolContext?: any;
94
- };
95
57
  /**
96
58
  * @dev Context for comprehensive PnL calculations with nested sub-contexts
97
59
  */
@@ -109,6 +71,6 @@ export type GetComprehensivePnlContext = {
109
71
  tradeData?: {
110
72
  tradeFeesData: TradeFeesData;
111
73
  liquidationParams: LiquidationParams;
112
- initialAccFeesV1?: BorrowingFee.InitialAccFees;
74
+ initialAccFees?: BorrowingFee.InitialAccFees;
113
75
  };
114
76
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.0.0-v10.rc4",
3
+ "version": "0.0.0-v10.rc5",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [