@gainsnetwork/sdk 0.0.0-v10.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 (81) hide show
  1. package/README.md +11 -0
  2. package/lib/constants.d.ts +403 -0
  3. package/lib/constants.js +436 -0
  4. package/lib/contracts/addresses.d.ts +3 -0
  5. package/lib/contracts/addresses.js +31 -0
  6. package/lib/contracts/addresses.json +127 -0
  7. package/lib/contracts/index.d.ts +14 -0
  8. package/lib/contracts/index.js +83 -0
  9. package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +8716 -0
  10. package/lib/contracts/types/generated/GNSMultiCollatDiamond.js +2 -0
  11. package/lib/contracts/types/generated/GToken.d.ts +1917 -0
  12. package/lib/contracts/types/generated/GToken.js +2 -0
  13. package/lib/contracts/types/generated/GTokenOpenPnlFeed.d.ts +557 -0
  14. package/lib/contracts/types/generated/GTokenOpenPnlFeed.js +2 -0
  15. package/lib/contracts/types/generated/common.d.ts +22 -0
  16. package/lib/contracts/types/generated/common.js +2 -0
  17. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +284 -0
  18. package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +19330 -0
  19. package/lib/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.d.ts +59 -0
  20. package/lib/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.js +765 -0
  21. package/lib/contracts/types/generated/factories/GToken__factory.d.ts +128 -0
  22. package/lib/contracts/types/generated/factories/GToken__factory.js +2871 -0
  23. package/lib/contracts/types/generated/factories/index.d.ts +3 -0
  24. package/lib/contracts/types/generated/factories/index.js +12 -0
  25. package/lib/contracts/types/generated/index.d.ts +7 -0
  26. package/lib/contracts/types/generated/index.js +33 -0
  27. package/lib/contracts/types/index.d.ts +32 -0
  28. package/lib/contracts/types/index.js +25 -0
  29. package/lib/contracts/utils/borrowingFees.d.ts +9 -0
  30. package/lib/contracts/utils/borrowingFees.js +54 -0
  31. package/lib/contracts/utils/index.d.ts +3 -0
  32. package/lib/contracts/utils/index.js +19 -0
  33. package/lib/contracts/utils/openTrades.d.ts +10 -0
  34. package/lib/contracts/utils/openTrades.js +126 -0
  35. package/lib/contracts/utils/pairs.d.ts +7 -0
  36. package/lib/contracts/utils/pairs.js +478 -0
  37. package/lib/index.d.ts +7 -0
  38. package/lib/index.js +24 -0
  39. package/lib/markets/commodities.d.ts +1 -0
  40. package/lib/markets/commodities.js +33 -0
  41. package/lib/markets/crypto.d.ts +1 -0
  42. package/lib/markets/crypto.js +6 -0
  43. package/lib/markets/forex.d.ts +3 -0
  44. package/lib/markets/forex.js +45 -0
  45. package/lib/markets/index.d.ts +5 -0
  46. package/lib/markets/index.js +21 -0
  47. package/lib/markets/indices.d.ts +1 -0
  48. package/lib/markets/indices.js +6 -0
  49. package/lib/markets/stocks.d.ts +3 -0
  50. package/lib/markets/stocks.js +58 -0
  51. package/lib/trade/fees/borrowing/converter.d.ts +17 -0
  52. package/lib/trade/fees/borrowing/converter.js +42 -0
  53. package/lib/trade/fees/borrowing/index.d.ts +58 -0
  54. package/lib/trade/fees/borrowing/index.js +209 -0
  55. package/lib/trade/fees/borrowing/types.d.ts +36 -0
  56. package/lib/trade/fees/borrowing/types.js +2 -0
  57. package/lib/trade/fees/index.d.ts +4 -0
  58. package/lib/trade/fees/index.js +34 -0
  59. package/lib/trade/fees/tiers/index.d.ts +12 -0
  60. package/lib/trade/fees/tiers/index.js +54 -0
  61. package/lib/trade/fees/tiers/types.d.ts +15 -0
  62. package/lib/trade/fees/tiers/types.js +8 -0
  63. package/lib/trade/index.d.ts +7 -0
  64. package/lib/trade/index.js +23 -0
  65. package/lib/trade/liquidation.d.ts +12 -0
  66. package/lib/trade/liquidation.js +55 -0
  67. package/lib/trade/oiWindows.d.ts +3 -0
  68. package/lib/trade/oiWindows.js +20 -0
  69. package/lib/trade/pnl.d.ts +10 -0
  70. package/lib/trade/pnl.js +33 -0
  71. package/lib/trade/spread.d.ts +18 -0
  72. package/lib/trade/spread.js +108 -0
  73. package/lib/trade/types.d.ts +598 -0
  74. package/lib/trade/types.js +402 -0
  75. package/lib/utils/index.d.ts +1 -0
  76. package/lib/utils/index.js +17 -0
  77. package/lib/utils/packing.d.ts +2 -0
  78. package/lib/utils/packing.js +39 -0
  79. package/lib/vault/index.d.ts +8 -0
  80. package/lib/vault/index.js +10 -0
  81. package/package.json +105 -0
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
4
+ const fees_1 = require("./fees");
5
+ const spread_1 = require("./spread");
6
+ const types_1 = require("../contracts/types");
7
+ const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
8
+ var _a, _b;
9
+ const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd);
10
+ const borrowingFee = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
11
+ const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
12
+ let liqPriceDistance = (trade.openPrice *
13
+ (trade.collateralAmount * liqThresholdP - (borrowingFee + closingFee))) /
14
+ trade.collateralAmount /
15
+ trade.leverage;
16
+ if ((context === null || context === void 0 ? void 0 : context.contractsVersion) !== undefined &&
17
+ context.contractsVersion >= types_1.ContractsVersion.V9_2 &&
18
+ ((((_a = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
19
+ context.liquidationParams.maxLiqSpreadP > 0) ||
20
+ (((_b = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !== undefined &&
21
+ context.userPriceImpact.fixedSpreadP > 0))) {
22
+ const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams, context.userPriceImpact);
23
+ liqPriceDistance -= trade.openPrice * closingSpreadP;
24
+ }
25
+ return trade.long
26
+ ? Math.max(trade.openPrice - liqPriceDistance, 0)
27
+ : Math.max(trade.openPrice + liqPriceDistance, 0);
28
+ };
29
+ exports.getLiquidationPrice = getLiquidationPrice;
30
+ const getLiqPnlThresholdP = (liquidationParams, leverage) => {
31
+ if (liquidationParams === undefined ||
32
+ leverage === undefined ||
33
+ liquidationParams.maxLiqSpreadP === 0 ||
34
+ liquidationParams.startLiqThresholdP === 0 ||
35
+ liquidationParams.endLiqThresholdP === 0 ||
36
+ liquidationParams.startLeverage === 0 ||
37
+ liquidationParams.endLeverage === 0) {
38
+ return 0.9;
39
+ }
40
+ if (leverage < liquidationParams.startLeverage) {
41
+ return liquidationParams.startLiqThresholdP;
42
+ }
43
+ if (leverage > liquidationParams.endLeverage) {
44
+ return liquidationParams.endLiqThresholdP;
45
+ }
46
+ if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
47
+ return liquidationParams.endLiqThresholdP;
48
+ }
49
+ return (liquidationParams.startLiqThresholdP -
50
+ ((leverage - liquidationParams.startLeverage) *
51
+ (liquidationParams.startLiqThresholdP -
52
+ liquidationParams.endLiqThresholdP)) /
53
+ (liquidationParams.endLeverage - liquidationParams.startLeverage));
54
+ };
55
+ exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
@@ -0,0 +1,3 @@
1
+ import { OiWindows, OiWindowsSettings } from "../trade/types";
2
+ export declare const getCurrentOiWindowId: (oiWindowSettings: OiWindowsSettings) => number;
3
+ export declare const getActiveOi: (currentOiWindowId: number, windowsCount: number, oiWindows: OiWindows | undefined, buy: boolean) => number;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getActiveOi = exports.getCurrentOiWindowId = void 0;
4
+ const getCurrentOiWindowId = (oiWindowSettings) => {
5
+ return Math.floor((Math.floor(Date.now() / 1000) - oiWindowSettings.startTs) /
6
+ oiWindowSettings.windowsDuration);
7
+ };
8
+ exports.getCurrentOiWindowId = getCurrentOiWindowId;
9
+ const getActiveOi = (currentOiWindowId, windowsCount, oiWindows, buy) => {
10
+ var _a, _b;
11
+ if (oiWindows === undefined || windowsCount === 0)
12
+ return 0;
13
+ let activeOi = 0;
14
+ for (let id = currentOiWindowId - (windowsCount - 1); id <= currentOiWindowId; id++) {
15
+ activeOi +=
16
+ (buy ? (_a = oiWindows === null || oiWindows === void 0 ? void 0 : oiWindows[id]) === null || _a === void 0 ? void 0 : _a.oiLongUsd : (_b = oiWindows === null || oiWindows === void 0 ? void 0 : oiWindows[id]) === null || _b === void 0 ? void 0 : _b.oiShortUsd) || 0;
17
+ }
18
+ return activeOi;
19
+ };
20
+ exports.getActiveOi = getActiveOi;
@@ -0,0 +1,10 @@
1
+ import { GetBorrowingFeeContext } from "./fees";
2
+ import { Fee, LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
3
+ import { ContractsVersion } from "../contracts/types";
4
+ export type GetPnlContext = GetBorrowingFeeContext & {
5
+ fee: Fee | undefined;
6
+ collateralPriceUsd: number | undefined;
7
+ contractsVersion: ContractsVersion | undefined;
8
+ feeMultiplier: number | undefined;
9
+ };
10
+ export declare const getPnl: (price: number | undefined, trade: Trade, tradeInfo: TradeInfo, initialAccFees: TradeInitialAccFees, liquidationParams: LiquidationParams, useFees: boolean, context: GetPnlContext) => number[] | undefined;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPnl = void 0;
4
+ const fees_1 = require("./fees");
5
+ const liquidation_1 = require("./liquidation");
6
+ const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
7
+ if (!price) {
8
+ return;
9
+ }
10
+ const posCollat = trade.collateralAmount;
11
+ const { openPrice, leverage } = trade;
12
+ const { fee } = context;
13
+ let pnlCollat = trade.long
14
+ ? ((price - openPrice) / openPrice) * leverage * posCollat
15
+ : ((openPrice - price) / openPrice) * leverage * posCollat;
16
+ if (useFees) {
17
+ pnlCollat -= (0, fees_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
18
+ }
19
+ let pnlPercentage = (pnlCollat / posCollat) * 100;
20
+ // Can be liquidated
21
+ if (pnlPercentage <=
22
+ (0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
23
+ pnlPercentage = -100;
24
+ }
25
+ else {
26
+ pnlCollat -= (0, fees_1.getClosingFee)(posCollat, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd, context.feeMultiplier);
27
+ pnlPercentage = (pnlCollat / posCollat) * 100;
28
+ }
29
+ pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
30
+ pnlCollat = (posCollat * pnlPercentage) / 100;
31
+ return [pnlCollat, pnlPercentage];
32
+ };
33
+ exports.getPnl = getPnl;
@@ -0,0 +1,18 @@
1
+ import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth, PairFactor, UserPriceImpact } from "./types";
2
+ import { ContractsVersion } from "../contracts/types";
3
+ export type SpreadContext = {
4
+ isOpen?: boolean;
5
+ isPnlPositive?: boolean;
6
+ createdBlock?: number;
7
+ liquidationParams?: LiquidationParams | undefined;
8
+ currentBlock?: number | undefined;
9
+ contractsVersion?: ContractsVersion | undefined;
10
+ protectionCloseFactorWhitelist?: boolean;
11
+ userPriceImpact?: UserPriceImpact | undefined;
12
+ } & Partial<PairFactor>;
13
+ export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
14
+ export declare const isProtectionCloseFactorActive: (spreadCtx: SpreadContext | undefined) => boolean | undefined;
15
+ export declare const getCumulativeFactor: (spreadCtx: SpreadContext | undefined) => number;
16
+ export declare const getLegacyFactor: (spreadCtx: SpreadContext | undefined) => number;
17
+ 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;
18
+ export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation?: boolean | undefined, liquidationParams?: LiquidationParams | undefined, userPriceImpact?: UserPriceImpact | undefined) => number;
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSpreadP = exports.getSpreadWithPriceImpactP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = void 0;
4
+ const oiWindows_1 = require("./oiWindows");
5
+ const constants_1 = require("../constants");
6
+ const types_1 = require("../contracts/types");
7
+ const getProtectionCloseFactor = (spreadCtx) => {
8
+ var _a;
9
+ const protectionCloseFactor = spreadCtx === undefined ||
10
+ spreadCtx.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
11
+ spreadCtx.isOpen === undefined ||
12
+ spreadCtx.isPnlPositive === undefined ||
13
+ spreadCtx.protectionCloseFactor === undefined ||
14
+ (0, exports.isProtectionCloseFactorActive)(spreadCtx) !== true
15
+ ? constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR
16
+ : spreadCtx.protectionCloseFactor;
17
+ const protectionCloseFactorMultiplier = ((_a = spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) === null || _a === void 0 ? void 0 : _a.cumulVolPriceImpactMultiplier) !== undefined &&
18
+ spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier > 0
19
+ ? spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier
20
+ : 1;
21
+ return protectionCloseFactor * protectionCloseFactorMultiplier;
22
+ };
23
+ exports.getProtectionCloseFactor = getProtectionCloseFactor;
24
+ const isProtectionCloseFactorActive = (spreadCtx) => {
25
+ if (spreadCtx === undefined ||
26
+ spreadCtx.currentBlock === undefined ||
27
+ spreadCtx.createdBlock === undefined ||
28
+ spreadCtx.protectionCloseFactorBlocks === undefined ||
29
+ spreadCtx.protectionCloseFactor === undefined) {
30
+ return undefined;
31
+ }
32
+ return (spreadCtx.isPnlPositive === true &&
33
+ spreadCtx.isOpen === false &&
34
+ spreadCtx.protectionCloseFactor > 0 &&
35
+ spreadCtx.currentBlock <=
36
+ spreadCtx.createdBlock + spreadCtx.protectionCloseFactorBlocks &&
37
+ spreadCtx.protectionCloseFactorWhitelist !== true);
38
+ };
39
+ exports.isProtectionCloseFactorActive = isProtectionCloseFactorActive;
40
+ const getCumulativeFactor = (spreadCtx) => {
41
+ if (spreadCtx === undefined ||
42
+ spreadCtx.cumulativeFactor === undefined ||
43
+ spreadCtx.cumulativeFactor === 0) {
44
+ return constants_1.DEFAULT_CUMULATIVE_FACTOR;
45
+ }
46
+ return spreadCtx.cumulativeFactor;
47
+ };
48
+ exports.getCumulativeFactor = getCumulativeFactor;
49
+ const getLegacyFactor = (spreadCtx) => {
50
+ return (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
51
+ };
52
+ exports.getLegacyFactor = getLegacyFactor;
53
+ const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, spreadCtx) => {
54
+ if (pairSpreadP === undefined) {
55
+ return 0;
56
+ }
57
+ if (
58
+ // No spread or price impact when closing pre-v9.2 trades
59
+ ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
60
+ (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) ||
61
+ // No spread or price impact for opens when `pair.exemptOnOpen` is true
62
+ ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === true && (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptOnOpen) === true) ||
63
+ // No spread or price impact for closes after `protectionCloseFactor` has expired
64
+ // when `pair.exemptAfterProtectionCloseFactor` is true
65
+ ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
66
+ (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptAfterProtectionCloseFactor) === true &&
67
+ (0, exports.isProtectionCloseFactorActive)(spreadCtx) !== true)) {
68
+ return 0;
69
+ }
70
+ const onePercentDepth = buy
71
+ ? // if `long`
72
+ (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
73
+ ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
74
+ : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
75
+ : // if `short`
76
+ (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
77
+ ? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
78
+ : pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
79
+ let activeOi = undefined;
80
+ if (oiWindowsSettings !== undefined) {
81
+ 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);
82
+ }
83
+ if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
84
+ return pairSpreadP / 2;
85
+ }
86
+ return ((0, exports.getSpreadP)(pairSpreadP, undefined, undefined, spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) +
87
+ ((activeOi * (0, exports.getCumulativeFactor)(spreadCtx) + (collateral * leverage) / 2) /
88
+ onePercentDepth /
89
+ 100 /
90
+ (0, exports.getLegacyFactor)(spreadCtx)) *
91
+ (0, exports.getProtectionCloseFactor)(spreadCtx));
92
+ };
93
+ exports.getSpreadWithPriceImpactP = getSpreadWithPriceImpactP;
94
+ const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
95
+ var _a;
96
+ const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
97
+ if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
98
+ return 0;
99
+ }
100
+ const spreadP = pairSpreadP / 2 + fixedSpreadP;
101
+ return isLiquidation === true &&
102
+ liquidationParams !== undefined &&
103
+ liquidationParams.maxLiqSpreadP > 0 &&
104
+ spreadP > liquidationParams.maxLiqSpreadP
105
+ ? liquidationParams.maxLiqSpreadP
106
+ : spreadP;
107
+ };
108
+ exports.getSpreadP = getSpreadP;