@gainsnetwork/sdk 0.2.12-rc1 → 0.2.12-rc10

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.
@@ -16,4 +16,8 @@ export declare enum CollateralTypes {
16
16
  ARB = "ARB",
17
17
  USDC = "USDC"
18
18
  }
19
+ export declare enum ContractsVersion {
20
+ BEFORE_V9_2 = 0,
21
+ V9_2 = 1
22
+ }
19
23
  export type ContractAddressList = Record<string, Partial<Record<CollateralTypes | "global", Partial<ContractAddresses>>>>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CollateralTypes = void 0;
3
+ exports.ContractsVersion = exports.CollateralTypes = void 0;
4
4
  var CollateralTypes;
5
5
  (function (CollateralTypes) {
6
6
  CollateralTypes["DAI"] = "DAI";
@@ -8,3 +8,8 @@ var CollateralTypes;
8
8
  CollateralTypes["ARB"] = "ARB";
9
9
  CollateralTypes["USDC"] = "USDC";
10
10
  })(CollateralTypes = exports.CollateralTypes || (exports.CollateralTypes = {}));
11
+ var ContractsVersion;
12
+ (function (ContractsVersion) {
13
+ ContractsVersion[ContractsVersion["BEFORE_V9_2"] = 0] = "BEFORE_V9_2";
14
+ ContractsVersion[ContractsVersion["V9_2"] = 1] = "V9_2";
15
+ })(ContractsVersion = exports.ContractsVersion || (exports.ContractsVersion = {}));
@@ -108,11 +108,13 @@ const _prepareTradeContainer = (trade, tradeInfo, tradeLiquidationParams, tradeI
108
108
  maxSlippageP: parseFloat(tradeInfo.maxSlippageP.toString()) / 1e3,
109
109
  lastOiUpdateTs: parseFloat(tradeInfo.lastOiUpdateTs),
110
110
  collateralPriceUsd: parseFloat(tradeInfo.collateralPriceUsd.toString()) / 1e8,
111
+ contractsVersion: parseInt(tradeInfo.contractsVersion.toString()),
112
+ lastPosIncreaseBlock: parseInt(tradeInfo.lastPosIncreaseBlock.toString()),
111
113
  },
112
114
  liquidationParams: {
113
- maxLiqSpreadP: parseFloat(tradeLiquidationParams.maxLiqSpreadP.toString()) / 1e10,
114
- startLiqThresholdP: parseFloat(tradeLiquidationParams.startLiqThresholdP.toString()) / 1e10,
115
- endLiqThresholdP: parseFloat(tradeLiquidationParams.endLiqThresholdP.toString()) / 1e10,
115
+ maxLiqSpreadP: parseFloat(tradeLiquidationParams.maxLiqSpreadP.toString()) / 1e12,
116
+ startLiqThresholdP: parseFloat(tradeLiquidationParams.startLiqThresholdP.toString()) / 1e12,
117
+ endLiqThresholdP: parseFloat(tradeLiquidationParams.endLiqThresholdP.toString()) / 1e12,
116
118
  startLeverage: parseFloat(tradeLiquidationParams.startLeverage.toString()) / 1e3,
117
119
  endLeverage: parseFloat(tradeLiquidationParams.endLeverage.toString()) / 1e3,
118
120
  },
@@ -2,7 +2,6 @@ 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[]>;
6
5
  export declare const fetchFees: (contracts: Contracts, feeIxs: PairIndex[]) => Promise<Fee[]>;
7
6
  export declare const fetchOpenInterest: (contracts: Contracts, collateralIndex: number, pairIxs: number[]) => Promise<OpenInterest[]>;
8
7
  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.fetchProtectionCloseFactors = exports.fetchPairDepths = exports.fetchPairs = void 0;
12
+ exports.getPairDescription = exports.fetchOpenInterest = exports.fetchFees = 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,23 +60,6 @@ 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;
80
63
  const fetchFees = (contracts, feeIxs) => __awaiter(void 0, void 0, void 0, function* () {
81
64
  if (!contracts) {
82
65
  return [];
@@ -373,4 +356,8 @@ const PAIR_INDEX_TO_DESCRIPTION = {
373
356
  [types_1.PairIndex.COREUSD]: "Core to US Dollar",
374
357
  [types_1.PairIndex.JASMYUSD]: "Jasmy Coin to US Dollar",
375
358
  [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",
376
363
  };
@@ -1,4 +1,4 @@
1
1
  import { Fee, PairIndex } from "../types";
2
- export declare const getClosingFee: (posDai: number, leverage: number, pairIndex: PairIndex, pairFee: Fee | undefined) => number;
2
+ export declare const getClosingFee: (posDai: number, leverage: number, pairIndex: PairIndex, pairFee: Fee | undefined, collateralPriceUsd?: number) => number;
3
3
  export * from "./borrowing";
4
4
  export * from "./tiers";
@@ -15,15 +15,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.getClosingFee = void 0;
18
- const getClosingFee = (posDai, leverage, pairIndex, pairFee) => {
18
+ const getClosingFee = (posDai, leverage, pairIndex, pairFee, collateralPriceUsd) => {
19
19
  if (posDai === undefined ||
20
20
  leverage === undefined ||
21
21
  pairIndex === undefined ||
22
22
  pairFee === undefined) {
23
23
  return 0;
24
24
  }
25
- const { closeFeeP, triggerOrderFeeP } = pairFee;
26
- return (closeFeeP + triggerOrderFeeP) * posDai * leverage;
25
+ const { closeFeeP, triggerOrderFeeP, minPositionSizeUsd } = pairFee;
26
+ return ((closeFeeP + triggerOrderFeeP) *
27
+ Math.max(collateralPriceUsd && collateralPriceUsd > 0
28
+ ? minPositionSizeUsd / collateralPriceUsd
29
+ : 0, posDai * leverage));
27
30
  };
28
31
  exports.getClosingFee = getClosingFee;
29
32
  __exportStar(require("./borrowing"), exports);
@@ -1,3 +1,11 @@
1
1
  import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
2
- import { Fee, Trade } from "./types";
3
- export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
2
+ import { Fee, LiquidationParams, Trade } from "./types";
3
+ import { ContractsVersion } from "../contracts/types";
4
+ export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
5
+ liquidationParams: LiquidationParams | undefined;
6
+ pairSpreadP: number | undefined;
7
+ collateralPriceUsd: number | undefined;
8
+ contractsVersion: ContractsVersion | undefined;
9
+ };
10
+ export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
11
+ export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined, contractsVersion: ContractsVersion | undefined) => number;
@@ -1,16 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getLiquidationPrice = void 0;
3
+ exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
4
4
  const fees_1 = require("./fees");
5
+ const spread_1 = require("./spread");
6
+ const types_1 = require("../contracts/types");
5
7
  const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
6
- const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee);
8
+ var _a;
9
+ const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd);
7
10
  const borrowingFee = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
8
- const liqPriceDistance = (trade.openPrice *
9
- (trade.collateralAmount * 0.9 - (borrowingFee + closingFee))) /
11
+ const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage, context.contractsVersion);
12
+ let liqPriceDistance = (trade.openPrice *
13
+ (trade.collateralAmount * liqThresholdP - (borrowingFee + closingFee))) /
10
14
  trade.collateralAmount /
11
15
  trade.leverage;
16
+ if ((context === null || context === void 0 ? void 0 : context.contractsVersion) === types_1.ContractsVersion.V9_2 &&
17
+ ((_a = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
18
+ context.liquidationParams.maxLiqSpreadP > 0) {
19
+ const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams);
20
+ liqPriceDistance -= trade.openPrice * closingSpreadP;
21
+ }
12
22
  return trade.long
13
23
  ? Math.max(trade.openPrice - liqPriceDistance, 0)
14
24
  : Math.max(trade.openPrice + liqPriceDistance, 0);
15
25
  };
16
26
  exports.getLiquidationPrice = getLiquidationPrice;
27
+ const getLiqPnlThresholdP = (liquidationParams, leverage, contractsVersion) => {
28
+ if (liquidationParams === undefined ||
29
+ leverage === undefined ||
30
+ contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
31
+ liquidationParams.maxLiqSpreadP === 0 ||
32
+ liquidationParams.startLiqThresholdP === 0 ||
33
+ liquidationParams.endLiqThresholdP === 0 ||
34
+ liquidationParams.startLeverage === 0 ||
35
+ liquidationParams.endLeverage === 0) {
36
+ return 0.9;
37
+ }
38
+ if (leverage < liquidationParams.startLeverage) {
39
+ return liquidationParams.startLiqThresholdP;
40
+ }
41
+ if (leverage > liquidationParams.endLeverage) {
42
+ return liquidationParams.endLiqThresholdP;
43
+ }
44
+ return (liquidationParams.startLiqThresholdP -
45
+ ((leverage - liquidationParams.startLeverage) *
46
+ (liquidationParams.startLiqThresholdP -
47
+ liquidationParams.endLiqThresholdP)) /
48
+ (liquidationParams.endLeverage - liquidationParams.startLeverage));
49
+ };
50
+ exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
@@ -1,7 +1,10 @@
1
1
  import { GetBorrowingFeeContext } from "./fees";
2
- import { Fee, Trade, TradeInfo, TradeInitialAccFees } from "./types";
2
+ import { Fee, LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
3
+ import { ContractsVersion } from "src/contracts/types";
3
4
  export type GetPnlContext = GetBorrowingFeeContext & {
4
5
  fee: Fee | undefined;
5
6
  maxGainP: number | undefined;
7
+ collateralPriceUsd: number | undefined;
8
+ contractsVersion: ContractsVersion | undefined;
6
9
  };
7
- export declare const getPnl: (price: number | undefined, trade: Trade, tradeInfo: TradeInfo, initialAccFees: TradeInitialAccFees, useFees: boolean, context: GetPnlContext) => number[] | undefined;
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 CHANGED
@@ -2,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getPnl = void 0;
4
4
  const fees_1 = require("./fees");
5
- const getPnl = (price, trade, tradeInfo, initialAccFees, useFees, context) => {
5
+ const liquidation_1 = require("./liquidation");
6
+ const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
6
7
  if (!price) {
7
8
  return;
8
9
  }
@@ -19,11 +20,13 @@ const getPnl = (price, trade, tradeInfo, initialAccFees, useFees, context) => {
19
20
  }
20
21
  let pnlPercentage = (pnlCollat / posCollat) * 100;
21
22
  // Can be liquidated
22
- if (pnlPercentage <= -90) {
23
+ if (pnlPercentage <=
24
+ (0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage, context.contractsVersion) *
25
+ -100) {
23
26
  pnlPercentage = -100;
24
27
  }
25
28
  else {
26
- pnlCollat -= (0, fees_1.getClosingFee)(posCollat, trade.leverage, trade.pairIndex, fee);
29
+ pnlCollat -= (0, fees_1.getClosingFee)(posCollat, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd);
27
30
  pnlPercentage = (pnlCollat / posCollat) * 100;
28
31
  }
29
32
  pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
@@ -1,11 +1,15 @@
1
- import { OiWindows, OiWindowsSettings, PairDepth } from "./types";
1
+ import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth } from "./types";
2
+ import { ContractsVersion } from "src/contracts/types";
2
3
  export type SpreadContext = {
3
4
  isOpen?: boolean;
4
5
  isPnlPositive?: boolean;
5
6
  protectionCloseFactor?: number;
6
7
  protectionCloseFactorBlocks?: number;
7
8
  createdBlock?: number;
9
+ liquidationParams?: LiquidationParams | undefined;
8
10
  currentBlock: number | undefined;
11
+ contractsVersion: ContractsVersion | undefined;
9
12
  };
10
13
  export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
11
14
  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;
15
+ export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation?: boolean | undefined, liquidationParams?: LiquidationParams | undefined) => number;
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSpreadWithPriceImpactP = exports.getProtectionCloseFactor = void 0;
3
+ exports.getSpreadP = exports.getSpreadWithPriceImpactP = exports.getProtectionCloseFactor = void 0;
4
4
  const oiWindows_1 = require("./oiWindows");
5
5
  const constants_1 = require("../constants");
6
+ const types_1 = require("src/contracts/types");
6
7
  const getProtectionCloseFactor = (spreadCtx) => {
7
8
  if (spreadCtx === undefined ||
9
+ spreadCtx.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
8
10
  spreadCtx.isOpen === undefined ||
9
11
  spreadCtx.isPnlPositive === undefined ||
10
12
  spreadCtx.protectionCloseFactor === undefined ||
@@ -26,13 +28,18 @@ const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairD
26
28
  if (pairSpreadP === undefined) {
27
29
  return 0;
28
30
  }
31
+ // No spread or price impact when closing pre-v9.2 trades
32
+ if ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
33
+ (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) {
34
+ return 0;
35
+ }
29
36
  const onePercentDepth = buy
30
37
  ? // if `long`
31
38
  (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
32
39
  ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
33
40
  : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
34
41
  : // if `short`
35
- (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
42
+ (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
36
43
  ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
37
44
  : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
38
45
  let activeOi = undefined;
@@ -42,8 +49,21 @@ const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairD
42
49
  if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
43
50
  return pairSpreadP / 2;
44
51
  }
45
- return (pairSpreadP / 2 +
52
+ return ((0, exports.getSpreadP)(pairSpreadP) +
46
53
  ((activeOi + (collateral * leverage) / 2) / onePercentDepth / 100 / 2) *
47
54
  (0, exports.getProtectionCloseFactor)(spreadCtx));
48
55
  };
49
56
  exports.getSpreadWithPriceImpactP = getSpreadWithPriceImpactP;
57
+ const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams) => {
58
+ if (pairSpreadP === undefined || pairSpreadP === 0) {
59
+ return 0;
60
+ }
61
+ const spreadP = pairSpreadP / 2;
62
+ return isLiquidation === true &&
63
+ liquidationParams !== undefined &&
64
+ liquidationParams.maxLiqSpreadP > 0 &&
65
+ spreadP > liquidationParams.maxLiqSpreadP
66
+ ? liquidationParams.maxLiqSpreadP
67
+ : spreadP;
68
+ };
69
+ exports.getSpreadP = getSpreadP;
@@ -32,6 +32,8 @@ export type TradeInfo = {
32
32
  maxSlippageP: number;
33
33
  lastOiUpdateTs: number;
34
34
  collateralPriceUsd: number;
35
+ contractsVersion: number;
36
+ lastPosIncreaseBlock: number;
35
37
  };
36
38
  export type LiquidationParams = {
37
39
  maxLiqSpreadP: number;
@@ -165,6 +167,11 @@ export type TraderFeeTiers = {
165
167
  lastDayUpdatedPoints: number;
166
168
  expiredPoints: number[];
167
169
  };
170
+ export type PairFactor = {
171
+ cumulativeFactor: number;
172
+ protectionCloseFactor: number;
173
+ protectionCloseFactorBlocks: number;
174
+ };
168
175
  export declare enum PendingOrderType {
169
176
  MARKET_OPEN = 0,
170
177
  MARKET_CLOSE = 1,
@@ -449,5 +456,9 @@ export declare enum PairIndex {
449
456
  OMUSD = 247,
450
457
  COREUSD = 248,
451
458
  JASMYUSD = 249,
452
- DARUSD = 250
459
+ DARUSD = 250,
460
+ MEWUSD = 251,
461
+ DEGENUSD = 252,
462
+ SLERFUSD = 253,
463
+ UXLINKUSD = 254
453
464
  }
@@ -280,4 +280,8 @@ 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";
283
287
  })(PairIndex = exports.PairIndex || (exports.PairIndex = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "0.2.12-rc1",
3
+ "version": "0.2.12-rc10",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [