@gainsnetwork/sdk 0.2.8-rc2 → 0.2.9-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.
Files changed (40) hide show
  1. package/lib/constants.d.ts +1 -0
  2. package/lib/constants.js +3 -2
  3. package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
  4. package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
  5. package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
  6. package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
  7. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +892 -412
  8. package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
  9. package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
  10. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
  11. package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
  12. package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
  13. package/lib/contracts/types/generated/GNSTrading.js +2 -0
  14. package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
  15. package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
  16. package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
  17. package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
  18. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
  19. package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
  20. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
  21. package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
  22. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +0 -94
  23. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +2190 -912
  24. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
  25. package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
  26. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
  27. package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
  28. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
  29. package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
  30. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
  31. package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
  32. package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
  33. package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
  34. package/lib/contracts/utils/openTrades.js +11 -2
  35. package/lib/contracts/utils/pairs.d.ts +1 -0
  36. package/lib/contracts/utils/pairs.js +18 -1
  37. package/lib/trade/spread.d.ts +8 -1
  38. package/lib/trade/spread.js +23 -6
  39. package/lib/trade/types.d.ts +10 -1
  40. package/package.json +1 -1
@@ -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 [];
@@ -1,2 +1,9 @@
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 | undefined;
4
+ protectionCloseFactor: number | undefined;
5
+ createdBlock: number | undefined;
6
+ currentBlock: number | undefined;
7
+ };
8
+ export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
9
+ 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,8 +1,20 @@
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
+ return spreadCtx === undefined ||
8
+ spreadCtx.protectionCloseFactor === undefined ||
9
+ spreadCtx.createdBlock === undefined ||
10
+ spreadCtx.currentBlock === undefined ||
11
+ spreadCtx.createdBlock + constants_1.PROTECTION_CLOSE_FACTOR_BLOCKS <
12
+ spreadCtx.currentBlock
13
+ ? 1
14
+ : spreadCtx.protectionCloseFactor;
15
+ };
16
+ exports.getProtectionCloseFactor = getProtectionCloseFactor;
17
+ const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, spreadCtx) => {
6
18
  if (pairSpreadP === undefined) {
7
19
  return 0;
8
20
  }
@@ -11,12 +23,17 @@ const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairD
11
23
  : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd;
12
24
  let activeOi = undefined;
13
25
  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);
26
+ activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(oiWindowsSettings), oiWindowsSettings.windowsCount, oiWindows, spreadCtx === undefined ||
27
+ spreadCtx.isOpen === undefined ||
28
+ spreadCtx.isOpen
29
+ ? buy
30
+ : !buy);
15
31
  }
16
32
  if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
17
- return pairSpreadP;
33
+ return pairSpreadP / 2;
18
34
  }
19
- return (pairSpreadP +
20
- (activeOi + (collateral * leverage) / 2) / onePercentDepth / 100);
35
+ return (pairSpreadP / 2 +
36
+ ((activeOi + (collateral * leverage) / 2) / onePercentDepth / 100 / 2) *
37
+ (0, exports.getProtectionCloseFactor)(spreadCtx));
21
38
  };
22
39
  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 = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.2.8-rc2",
3
+ "version": "0.2.9-rc1",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [