@gainsnetwork/sdk 1.3.0-rc1 → 1.3.0-rc3

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.
@@ -10,16 +10,15 @@ exports.buildCumulVolContext = void 0;
10
10
  * @returns Cumulative volume context ready for getTradeCumulVolPriceImpactP
11
11
  */
12
12
  const buildCumulVolContext = (globalTradingVariables, collateralIndex, pairIndex, additionalParams) => {
13
- var _a, _b, _c, _d, _e, _f, _g;
13
+ var _a, _b, _c, _d, _e, _f, _g, _h;
14
14
  const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
15
15
  if (!collateral) {
16
16
  return undefined;
17
17
  }
18
18
  // Get pair-specific data from global variables
19
- // TODO: Update to use pairDepthBands for v10.2
20
- const pairDepth = undefined; // globalTradingVariables.pairDepths?.[pairIndex];
21
- const pairFactor = (_a = globalTradingVariables.pairFactors) === null || _a === void 0 ? void 0 : _a[pairIndex];
22
- const oiWindows = (_b = globalTradingVariables.oiWindows) === null || _b === void 0 ? void 0 : _b[pairIndex];
19
+ const pairDepthBands = (_a = globalTradingVariables.pairDepthBands) === null || _a === void 0 ? void 0 : _a[pairIndex];
20
+ const pairFactor = (_b = globalTradingVariables.pairFactors) === null || _b === void 0 ? void 0 : _b[pairIndex];
21
+ const oiWindows = (_c = globalTradingVariables.oiWindows) === null || _c === void 0 ? void 0 : _c[pairIndex];
23
22
  // Get OI windows settings (same for all pairs)
24
23
  // OI windows settings from global trading variables are already in SDK format
25
24
  const oiWindowsSettings = globalTradingVariables.oiWindowsSettings;
@@ -27,18 +26,18 @@ const buildCumulVolContext = (globalTradingVariables, collateralIndex, pairIndex
27
26
  const userPriceImpact = additionalParams.userPriceImpact;
28
27
  const protectionCloseFactorWhitelist = additionalParams.protectionCloseFactorWhitelist;
29
28
  // Get liquidation params - check both pair and group level
30
- const liquidationParams = ((_d = (_c = globalTradingVariables.liquidationParams) === null || _c === void 0 ? void 0 : _c.pairs) === null || _d === void 0 ? void 0 : _d[pairIndex]) ||
31
- ((_f = (_e = globalTradingVariables.liquidationParams) === null || _e === void 0 ? void 0 : _e.groups) === null || _f === void 0 ? void 0 : _f[0]); // fallback to first group
29
+ const liquidationParams = ((_e = (_d = globalTradingVariables.liquidationParams) === null || _d === void 0 ? void 0 : _d.pairs) === null || _e === void 0 ? void 0 : _e[pairIndex]) ||
30
+ ((_g = (_f = globalTradingVariables.liquidationParams) === null || _f === void 0 ? void 0 : _f.groups) === null || _g === void 0 ? void 0 : _g[0]); // fallback to first group
32
31
  return Object.assign({
33
32
  // Trade state
34
33
  isOpen: additionalParams.isOpen, isPnlPositive: additionalParams.isPnlPositive, createdBlock: additionalParams.createdBlock,
35
34
  // Protection factors
36
35
  liquidationParams, currentBlock: additionalParams.currentBlock, contractsVersion: additionalParams.contractsVersion, protectionCloseFactorWhitelist,
37
36
  // Price impact data
38
- pairDepth,
37
+ pairDepthBands,
39
38
  oiWindowsSettings,
40
39
  oiWindows,
41
40
  // User/collateral specific
42
- userPriceImpact, collateralPriceUsd: ((_g = collateral.prices) === null || _g === void 0 ? void 0 : _g.collateralPriceUsd) || 1 }, pairFactor);
41
+ userPriceImpact, collateralPriceUsd: ((_h = collateral.prices) === null || _h === void 0 ? void 0 : _h.collateralPriceUsd) || 1 }, pairFactor);
43
42
  };
44
43
  exports.buildCumulVolContext = buildCumulVolContext;
@@ -3,7 +3,7 @@
3
3
  * @dev Mirrors contract's getTradeCumulVolPriceImpactP functionality
4
4
  */
5
5
  import { PairDepthBands, DepthBandsMapping } from "./types";
6
- import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth, PairFactor, UserPriceImpact } from "../../types";
6
+ import { LiquidationParams, OiWindows, OiWindowsSettings, PairFactor, UserPriceImpact } from "../../types";
7
7
  import { ContractsVersion } from "../../../contracts/types";
8
8
  export type CumulVolContext = {
9
9
  isOpen?: boolean;
@@ -13,7 +13,6 @@ export type CumulVolContext = {
13
13
  currentBlock?: number | undefined;
14
14
  contractsVersion?: ContractsVersion | undefined;
15
15
  protectionCloseFactorWhitelist?: boolean;
16
- pairDepth?: PairDepth | undefined;
17
16
  oiWindowsSettings?: OiWindowsSettings | undefined;
18
17
  oiWindows?: OiWindows | undefined;
19
18
  pairDepthBands?: PairDepthBands | undefined;
@@ -86,13 +85,12 @@ export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation
86
85
  * @param buy True for long, false for short
87
86
  * @param collateral Collateral amount
88
87
  * @param leverage Position leverage
89
- * @param pairDepth 1% depth values for the pair
90
88
  * @param oiWindowsSettings OI windows configuration
91
89
  * @param oiWindows Current OI windows data
92
90
  * @param context Additional context for the calculation
93
91
  * @returns Total spread + cumulative volume price impact percentage
94
92
  */
95
- export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
93
+ export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
96
94
  /**
97
95
  * @dev Convenience function for calculating cumulative volume price impact
98
96
  * @dev Uses collateral and leverage instead of USD position size
@@ -106,6 +104,6 @@ export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, bu
106
104
  export declare const getCumulVolPriceImpact: (buy: boolean, collateral: number, leverage: number, open: boolean, context: CumulVolContext & {
107
105
  collateralPriceUsd: number;
108
106
  }) => number;
109
- export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
107
+ export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
110
108
  export { convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, } from "./converter";
111
109
  export { buildCumulVolContext } from "./builder";
@@ -81,7 +81,7 @@ exports.getLegacyFactor = getLegacyFactor;
81
81
  * @returns Price impact percentage
82
82
  */
83
83
  const _calculateDepthBandsPriceImpact = (tradeSizeUsd, depthBandParams) => {
84
- const totalDepthUsd = depthBandParams.pairSlot1.totalDepthUsd;
84
+ const totalDepthUsd = depthBandParams.depthBands.totalDepthUsd;
85
85
  if (totalDepthUsd === 0 || tradeSizeUsd === 0)
86
86
  return 0;
87
87
  let remainingSizeUsd = tradeSizeUsd;
@@ -89,8 +89,8 @@ const _calculateDepthBandsPriceImpact = (tradeSizeUsd, depthBandParams) => {
89
89
  let prevBandDepthUsd = 0;
90
90
  let topOfPrevBandOffsetPpm = 0;
91
91
  for (let i = 0; i < 30 && remainingSizeUsd !== 0; i++) {
92
- const bandLiquidityPercentageBps = depthBandParams.pairSlot1.bands[i]; // Already in 0-1 format
93
- const topOfBandOffsetPpm = depthBandParams.mappingSlot1.bands[i]; // Already in 0-1 format
92
+ const bandLiquidityPercentageBps = depthBandParams.depthBands.bands[i]; // Already in 0-1 format
93
+ const topOfBandOffsetPpm = depthBandParams.depthBandsMapping.bands[i]; // Already in 0-1 format
94
94
  const bandDepthUsd = bandLiquidityPercentageBps * totalDepthUsd;
95
95
  // Skip if band has same depth as previous (would cause division by zero)
96
96
  if (bandDepthUsd <= prevBandDepthUsd) {
@@ -171,7 +171,6 @@ const _getDepthBandsPriceImpactP = (cumulativeVolumeUsd, tradeSizeUsd, depthBand
171
171
  * @returns Cumulative volume price impact percentage (not including spread)
172
172
  */
173
173
  const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context) => {
174
- var _a, _b;
175
174
  // Update context with passed parameters
176
175
  const updatedContext = Object.assign(Object.assign({}, context), { isOpen: open, isPnlPositive: isPnlPositive, createdBlock: context.createdBlock || lastPosIncreaseBlock });
177
176
  if (
@@ -188,50 +187,31 @@ const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterest
188
187
  }
189
188
  const tradePositiveSkew = (long && open) || (!long && !open);
190
189
  const tradeSkewMultiplier = tradePositiveSkew ? 1 : -1;
191
- if (context.pairDepthBands && context.depthBandsMapping) {
192
- // Select depth bands based on trade direction
193
- const depthBands = tradePositiveSkew
194
- ? context.pairDepthBands.above
195
- : context.pairDepthBands.below;
196
- if (depthBands && depthBands.totalDepthUsd > 0) {
197
- // Get active OI for cumulative volume calculation
198
- let activeOi = 0;
199
- if (context.oiWindowsSettings !== undefined) {
200
- activeOi =
201
- (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long) || 0;
202
- }
203
- const signedActiveOi = activeOi * tradeSkewMultiplier;
204
- const signedTradeOi = tradeOpenInterestUsd * tradeSkewMultiplier;
205
- // Calculate price impact using depth bands
206
- const priceImpactP = _getDepthBandsPriceImpactP(signedActiveOi, signedTradeOi, {
207
- pairSlot1: depthBands,
208
- mappingSlot1: context.depthBandsMapping,
209
- }, (0, exports.getProtectionCloseFactor)(updatedContext), (0, exports.getCumulativeFactor)(updatedContext));
210
- return priceImpactP;
211
- }
190
+ if (!context.pairDepthBands || !context.depthBandsMapping) {
212
191
  return 0;
213
192
  }
214
- // Fall back to legacy calculation for pre-v10.2 contracts
215
- const onePercentDepth = tradePositiveSkew
216
- ? (_a = context.pairDepth) === null || _a === void 0 ? void 0 : _a.onePercentDepthAboveUsd
217
- : (_b = context.pairDepth) === null || _b === void 0 ? void 0 : _b.onePercentDepthBelowUsd;
218
- let activeOi = undefined;
219
- if (context.oiWindowsSettings !== undefined) {
220
- activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long);
221
- }
222
- if (!onePercentDepth || activeOi === undefined) {
193
+ // Select depth bands based on trade direction
194
+ const depthBands = tradePositiveSkew
195
+ ? context.pairDepthBands.above
196
+ : context.pairDepthBands.below;
197
+ // Return 0 if no depth bands configured (matching contract lines 588-590)
198
+ if (!depthBands || depthBands.totalDepthUsd === 0) {
223
199
  return 0;
224
200
  }
225
- // Apply trade skew multiplier to match Solidity's signed calculation
201
+ // Get active OI for cumulative volume calculation
202
+ let activeOi = 0;
203
+ if (context.oiWindowsSettings !== undefined) {
204
+ activeOi =
205
+ (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long) || 0;
206
+ }
226
207
  const signedActiveOi = activeOi * tradeSkewMultiplier;
227
208
  const signedTradeOi = tradeOpenInterestUsd * tradeSkewMultiplier;
228
- // Calculate impact with proper signs (matching Solidity's _getTradePriceImpactP)
229
- const finalPriceImpactP = ((signedActiveOi * (0, exports.getCumulativeFactor)(updatedContext) +
230
- signedTradeOi / 2) /
231
- onePercentDepth /
232
- (0, exports.getLegacyFactor)(updatedContext)) *
233
- (0, exports.getProtectionCloseFactor)(updatedContext);
234
- return finalPriceImpactP;
209
+ // Calculate price impact using depth bands
210
+ const priceImpactP = _getDepthBandsPriceImpactP(signedActiveOi, signedTradeOi, {
211
+ depthBands: depthBands,
212
+ depthBandsMapping: context.depthBandsMapping,
213
+ }, (0, exports.getProtectionCloseFactor)(updatedContext), (0, exports.getCumulativeFactor)(updatedContext));
214
+ return priceImpactP;
235
215
  };
236
216
  exports.getTradeCumulVolPriceImpactP = getTradeCumulVolPriceImpactP;
237
217
  /**
@@ -283,13 +263,12 @@ exports.getSpreadP = getSpreadP;
283
263
  * @param buy True for long, false for short
284
264
  * @param collateral Collateral amount
285
265
  * @param leverage Position leverage
286
- * @param pairDepth 1% depth values for the pair
287
266
  * @param oiWindowsSettings OI windows configuration
288
267
  * @param oiWindows Current OI windows data
289
268
  * @param context Additional context for the calculation
290
269
  * @returns Total spread + cumulative volume price impact percentage
291
270
  */
292
- const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, context) => {
271
+ const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverage, oiWindowsSettings, oiWindows, context) => {
293
272
  if (pairSpreadP === undefined) {
294
273
  return 0;
295
274
  }
@@ -298,11 +277,10 @@ const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverag
298
277
  const positionSizeUsd = collateral * leverage * ((context === null || context === void 0 ? void 0 : context.collateralPriceUsd) || 1);
299
278
  const cumulVolImpact = (0, exports.getTradeCumulVolPriceImpactP)("", // trader - not used in calculation
300
279
  0, // pairIndex - not used in calculation
301
- buy, positionSizeUsd, (context === null || context === void 0 ? void 0 : context.isPnlPositive) || false, (context === null || context === void 0 ? void 0 : context.isOpen) !== false, (context === null || context === void 0 ? void 0 : context.createdBlock) || 0, Object.assign(Object.assign({}, context), { pairDepth,
302
- oiWindowsSettings,
280
+ buy, positionSizeUsd, (context === null || context === void 0 ? void 0 : context.isPnlPositive) || false, (context === null || context === void 0 ? void 0 : context.isOpen) !== false, (context === null || context === void 0 ? void 0 : context.createdBlock) || 0, Object.assign(Object.assign({}, context), { oiWindowsSettings,
303
281
  oiWindows }));
304
- // If no depth or OI data, return just half spread
305
- if (cumulVolImpact === 0 && (!pairDepth || !oiWindowsSettings)) {
282
+ // If no cumulative volume impact calculated, return half of base spread
283
+ if (cumulVolImpact === 0 && !oiWindowsSettings) {
306
284
  return pairSpreadP / 2;
307
285
  }
308
286
  return baseSpread + cumulVolImpact;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "1.3.0-rc1",
3
+ "version": "1.3.0-rc3",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [