@gainsnetwork/sdk 0.2.11-rc1 → 0.2.12-rc1

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.
@@ -14,7 +14,7 @@ const ethcall_1 = require("ethcall");
14
14
  const fetchOpenPairTrades = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
15
15
  const rawTrades = yield (0, exports.fetchOpenPairTradesRaw)(contracts, overrides);
16
16
  const collateralPrecisions = (yield contracts.gnsMultiCollatDiamond.getCollaterals()).map(({ precision }) => precision);
17
- return rawTrades.map(rawTrade => _prepareTradeContainer(rawTrade.trade, rawTrade.tradeInfo, rawTrade.initialAccFees, collateralPrecisions[parseInt(rawTrade.trade.collateralIndex.toString()) - 1]));
17
+ return rawTrades.map(rawTrade => _prepareTradeContainer(rawTrade.trade, rawTrade.tradeInfo, rawTrade.liquidationParams, rawTrade.initialAccFees, collateralPrecisions[parseInt(rawTrade.trade.collateralIndex.toString()) - 1]));
18
18
  });
19
19
  exports.fetchOpenPairTrades = fetchOpenPairTrades;
20
20
  // @todo rename
@@ -40,6 +40,7 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
40
40
  while (running) {
41
41
  const trades = yield multiCollatDiamondContract.getAllTrades(offset, offset + batchSize);
42
42
  const tradeInfos = yield multiCollatDiamondContract.getAllTradeInfos(offset, offset + batchSize);
43
+ const tradeLiquidationParams = yield multiCollatDiamondContract.getAllTradesLiquidationParams(offset, offset + batchSize);
43
44
  // Array is always of length `batchSize`
44
45
  // so we need to filter out the empty trades, indexes are reliable
45
46
  const openTrades = trades
@@ -48,6 +49,7 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
48
49
  .map((trade, ix) => ({
49
50
  trade,
50
51
  tradeInfo: tradeInfos[ix],
52
+ liquidationParams: tradeLiquidationParams[ix],
51
53
  initialAccFees: {
52
54
  accPairFee: 0,
53
55
  accGroupFee: 0,
@@ -83,7 +85,7 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
83
85
  }
84
86
  });
85
87
  exports.fetchOpenPairTradesRaw = fetchOpenPairTradesRaw;
86
- const _prepareTradeContainer = (trade, tradeInfo, tradeInitialAccFees, collateralPrecision) => ({
88
+ const _prepareTradeContainer = (trade, tradeInfo, tradeLiquidationParams, tradeInitialAccFees, collateralPrecision) => ({
87
89
  trade: {
88
90
  user: trade.user,
89
91
  index: parseInt(trade.index.toString()),
@@ -107,6 +109,13 @@ const _prepareTradeContainer = (trade, tradeInfo, tradeInitialAccFees, collatera
107
109
  lastOiUpdateTs: parseFloat(tradeInfo.lastOiUpdateTs),
108
110
  collateralPriceUsd: parseFloat(tradeInfo.collateralPriceUsd.toString()) / 1e8,
109
111
  },
112
+ liquidationParams: {
113
+ maxLiqSpreadP: parseFloat(tradeLiquidationParams.maxLiqSpreadP.toString()) / 1e10,
114
+ startLiqThresholdP: parseFloat(tradeLiquidationParams.startLiqThresholdP.toString()) / 1e10,
115
+ endLiqThresholdP: parseFloat(tradeLiquidationParams.endLiqThresholdP.toString()) / 1e10,
116
+ startLeverage: parseFloat(tradeLiquidationParams.startLeverage.toString()) / 1e3,
117
+ endLeverage: parseFloat(tradeLiquidationParams.endLeverage.toString()) / 1e3,
118
+ },
110
119
  initialAccFees: {
111
120
  accPairFee: parseFloat(tradeInitialAccFees.accPairFee.toString()) / 1e10,
112
121
  accGroupFee: parseFloat(tradeInitialAccFees.accGroupFee.toString()) / 1e10,
@@ -2,6 +2,7 @@ import { Pair, Fee, OpenInterest, PairDepth, PairIndex } from "../../trade/types
2
2
  import { Contracts } from "../../contracts/types";
3
3
  export declare const fetchPairs: (contracts: Contracts, pairIxs: PairIndex[]) => Promise<Pair[]>;
4
4
  export declare const fetchPairDepths: (contracts: Contracts, pairIxs: number[]) => Promise<PairDepth[]>;
5
+ export declare const fetchProtectionCloseFactors: (contracts: Contracts, pairIxs: number[]) => Promise<number[]>;
5
6
  export declare const fetchFees: (contracts: Contracts, feeIxs: PairIndex[]) => Promise<Fee[]>;
6
7
  export declare const fetchOpenInterest: (contracts: Contracts, collateralIndex: number, pairIxs: number[]) => Promise<OpenInterest[]>;
7
8
  export declare const getPairDescription: (pairIndex: PairIndex) => string;
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getPairDescription = exports.fetchOpenInterest = exports.fetchFees = exports.fetchPairDepths = exports.fetchPairs = void 0;
12
+ exports.getPairDescription = exports.fetchOpenInterest = exports.fetchFees = exports.fetchProtectionCloseFactors = exports.fetchPairDepths = exports.fetchPairs = void 0;
13
13
  /* eslint-disable @typescript-eslint/no-unsafe-call */
14
14
  /* eslint-disable @typescript-eslint/no-unsafe-return */
15
15
  /* eslint-disable @typescript-eslint/no-unsafe-member-access */
@@ -60,6 +60,23 @@ const fetchPairDepths = (contracts, pairIxs) => __awaiter(void 0, void 0, void 0
60
60
  }
61
61
  });
62
62
  exports.fetchPairDepths = fetchPairDepths;
63
+ const fetchProtectionCloseFactors = (contracts, pairIxs) => __awaiter(void 0, void 0, void 0, function* () {
64
+ if (!contracts) {
65
+ return [];
66
+ }
67
+ const { gnsMultiCollatDiamond: multiCollatContract } = contracts;
68
+ try {
69
+ const protectionCloseFactors = yield multiCollatContract.getProtectionCloseFactors(pairIxs);
70
+ return protectionCloseFactors.map(protectionCloseFactor => {
71
+ return protectionCloseFactor.toNumber();
72
+ });
73
+ }
74
+ catch (error) {
75
+ console.error(`Unexpected error while fetching pairs!`);
76
+ throw error;
77
+ }
78
+ });
79
+ exports.fetchProtectionCloseFactors = fetchProtectionCloseFactors;
63
80
  const fetchFees = (contracts, feeIxs) => __awaiter(void 0, void 0, void 0, function* () {
64
81
  if (!contracts) {
65
82
  return [];
@@ -356,8 +373,4 @@ const PAIR_INDEX_TO_DESCRIPTION = {
356
373
  [types_1.PairIndex.COREUSD]: "Core to US Dollar",
357
374
  [types_1.PairIndex.JASMYUSD]: "Jasmy Coin to US Dollar",
358
375
  [types_1.PairIndex.DARUSD]: "Mines of Dalarnia to US Dollar",
359
- [types_1.PairIndex.MEWUSD]: "cat in a dogs world to US Dollar",
360
- [types_1.PairIndex.DEGENUSD]: "Degen to US Dollar",
361
- [types_1.PairIndex.SLERFUSD]: "Slerf to US Dollar",
362
- [types_1.PairIndex.UXLINKUSD]: "UXLINK to US Dollar",
363
376
  };
@@ -1,2 +1,11 @@
1
1
  import { OiWindows, OiWindowsSettings, PairDepth } from "./types";
2
- export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined) => number;
2
+ export type SpreadContext = {
3
+ isOpen?: boolean;
4
+ isPnlPositive?: boolean;
5
+ protectionCloseFactor?: number;
6
+ protectionCloseFactorBlocks?: number;
7
+ createdBlock?: number;
8
+ currentBlock: number | undefined;
9
+ };
10
+ export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
11
+ 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;
@@ -1,22 +1,49 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSpreadWithPriceImpactP = void 0;
3
+ exports.getSpreadWithPriceImpactP = exports.getProtectionCloseFactor = void 0;
4
4
  const oiWindows_1 = require("./oiWindows");
5
- const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows) => {
5
+ const constants_1 = require("../constants");
6
+ const getProtectionCloseFactor = (spreadCtx) => {
7
+ if (spreadCtx === undefined ||
8
+ spreadCtx.isOpen === undefined ||
9
+ spreadCtx.isPnlPositive === undefined ||
10
+ spreadCtx.protectionCloseFactor === undefined ||
11
+ spreadCtx.protectionCloseFactorBlocks === undefined ||
12
+ spreadCtx.createdBlock === undefined ||
13
+ spreadCtx.currentBlock === undefined)
14
+ return constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR;
15
+ if (spreadCtx.isPnlPositive &&
16
+ !spreadCtx.isOpen &&
17
+ spreadCtx.protectionCloseFactor > 0 &&
18
+ spreadCtx.currentBlock <=
19
+ spreadCtx.createdBlock + spreadCtx.protectionCloseFactorBlocks) {
20
+ return spreadCtx.protectionCloseFactor;
21
+ }
22
+ return constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR;
23
+ };
24
+ exports.getProtectionCloseFactor = getProtectionCloseFactor;
25
+ const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, spreadCtx) => {
6
26
  if (pairSpreadP === undefined) {
7
27
  return 0;
8
28
  }
9
29
  const onePercentDepth = buy
10
- ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
11
- : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd;
30
+ ? // if `long`
31
+ (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
32
+ ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
33
+ : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
34
+ : // if `short`
35
+ (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
36
+ ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
37
+ : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
12
38
  let activeOi = undefined;
13
39
  if (oiWindowsSettings !== undefined && (oiWindowsSettings === null || oiWindowsSettings === void 0 ? void 0 : oiWindowsSettings.windowsCount) > 0) {
14
- activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(oiWindowsSettings), oiWindowsSettings.windowsCount, oiWindows, buy);
40
+ 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);
15
41
  }
16
42
  if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
17
- return pairSpreadP;
43
+ return pairSpreadP / 2;
18
44
  }
19
- return (pairSpreadP +
20
- (activeOi + (collateral * leverage) / 2) / onePercentDepth / 100);
45
+ return (pairSpreadP / 2 +
46
+ ((activeOi + (collateral * leverage) / 2) / onePercentDepth / 100 / 2) *
47
+ (0, exports.getProtectionCloseFactor)(spreadCtx));
21
48
  };
22
49
  exports.getSpreadWithPriceImpactP = getSpreadWithPriceImpactP;
@@ -1,4 +1,4 @@
1
- import { ITradingStorage, IBorrowingFees } from "../contracts/types/generated/GNSMultiCollatDiamond";
1
+ import { ITradingStorage, IBorrowingFees, IPairsStorage } from "../contracts/types/generated/GNSMultiCollatDiamond";
2
2
  import { BorrowingFee } from "./fees/borrowing";
3
3
  import { FeeTier, TraderInfo } from "./fees/tiers/types";
4
4
  export type PairIndexes = {
@@ -7,6 +7,7 @@ export type PairIndexes = {
7
7
  export type TradeContainer = {
8
8
  trade: Trade;
9
9
  tradeInfo: TradeInfo;
10
+ liquidationParams: LiquidationParams;
10
11
  initialAccFees: TradeInitialAccFees;
11
12
  receivedAt?: number;
12
13
  };
@@ -32,6 +33,13 @@ export type TradeInfo = {
32
33
  lastOiUpdateTs: number;
33
34
  collateralPriceUsd: number;
34
35
  };
36
+ export type LiquidationParams = {
37
+ maxLiqSpreadP: number;
38
+ startLiqThresholdP: number;
39
+ endLiqThresholdP: number;
40
+ startLeverage: number;
41
+ endLeverage: number;
42
+ };
35
43
  export type TradingGroup = {
36
44
  maxLeverage: number;
37
45
  minLeverage: number;
@@ -122,6 +130,7 @@ export declare enum PositionType {
122
130
  export type TradeContainerRaw = {
123
131
  trade: ITradingStorage.TradeStruct;
124
132
  tradeInfo: ITradingStorage.TradeInfoStruct;
133
+ liquidationParams: IPairsStorage.GroupLiquidationParamsStruct;
125
134
  initialAccFees: IBorrowingFees.BorrowingInitialAccFeesStruct;
126
135
  };
127
136
  export type OiWindowsSettings = {
@@ -440,9 +449,5 @@ export declare enum PairIndex {
440
449
  OMUSD = 247,
441
450
  COREUSD = 248,
442
451
  JASMYUSD = 249,
443
- DARUSD = 250,
444
- MEWUSD = 251,
445
- DEGENUSD = 252,
446
- SLERFUSD = 253,
447
- UXLINKUSD = 254
452
+ DARUSD = 250
448
453
  }
@@ -280,8 +280,4 @@ var PairIndex;
280
280
  PairIndex[PairIndex["COREUSD"] = 248] = "COREUSD";
281
281
  PairIndex[PairIndex["JASMYUSD"] = 249] = "JASMYUSD";
282
282
  PairIndex[PairIndex["DARUSD"] = 250] = "DARUSD";
283
- PairIndex[PairIndex["MEWUSD"] = 251] = "MEWUSD";
284
- PairIndex[PairIndex["DEGENUSD"] = 252] = "DEGENUSD";
285
- PairIndex[PairIndex["SLERFUSD"] = 253] = "SLERFUSD";
286
- PairIndex[PairIndex["UXLINKUSD"] = 254] = "UXLINKUSD";
287
283
  })(PairIndex = exports.PairIndex || (exports.PairIndex = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.2.11-rc1",
3
+ "version": "0.2.12-rc1",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [