@gainsnetwork/sdk 0.2.20-rc2 → 0.2.21
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.
- package/lib/constants.d.ts +2 -0
- package/lib/constants.js +3 -1
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +1131 -477
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +28 -81
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +3174 -1417
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/index.d.ts +4 -0
- package/lib/contracts/types/index.js +6 -1
- package/lib/contracts/utils/openTrades.js +13 -2
- package/lib/trade/fees/borrowing/index.d.ts +0 -1
- package/lib/trade/fees/borrowing/index.js +9 -7
- package/lib/trade/liquidation.d.ts +6 -1
- package/lib/trade/liquidation.js +37 -3
- package/lib/trade/pnl.d.ts +4 -2
- package/lib/trade/pnl.js +4 -2
- package/lib/trade/spread.d.ts +18 -2
- package/lib/trade/spread.js +69 -8
- package/lib/trade/types.d.ts +17 -1
- package/package.json +1 -1
|
@@ -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 = {}));
|
|
@@ -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()),
|
|
@@ -106,6 +108,15 @@ const _prepareTradeContainer = (trade, tradeInfo, tradeInitialAccFees, collatera
|
|
|
106
108
|
maxSlippageP: parseFloat(tradeInfo.maxSlippageP.toString()) / 1e3,
|
|
107
109
|
lastOiUpdateTs: parseFloat(tradeInfo.lastOiUpdateTs),
|
|
108
110
|
collateralPriceUsd: parseFloat(tradeInfo.collateralPriceUsd.toString()) / 1e8,
|
|
111
|
+
contractsVersion: parseInt(tradeInfo.contractsVersion.toString()),
|
|
112
|
+
lastPosIncreaseBlock: parseInt(tradeInfo.lastPosIncreaseBlock.toString()),
|
|
113
|
+
},
|
|
114
|
+
liquidationParams: {
|
|
115
|
+
maxLiqSpreadP: parseFloat(tradeLiquidationParams.maxLiqSpreadP.toString()) / 1e12,
|
|
116
|
+
startLiqThresholdP: parseFloat(tradeLiquidationParams.startLiqThresholdP.toString()) / 1e12,
|
|
117
|
+
endLiqThresholdP: parseFloat(tradeLiquidationParams.endLiqThresholdP.toString()) / 1e12,
|
|
118
|
+
startLeverage: parseFloat(tradeLiquidationParams.startLeverage.toString()) / 1e3,
|
|
119
|
+
endLeverage: parseFloat(tradeLiquidationParams.endLeverage.toString()) / 1e3,
|
|
109
120
|
},
|
|
110
121
|
initialAccFees: {
|
|
111
122
|
accPairFee: parseFloat(tradeInitialAccFees.accPairFee.toString()) / 1e10,
|
|
@@ -4,7 +4,6 @@ export type GetBorrowingFeeContext = {
|
|
|
4
4
|
currentBlock: number;
|
|
5
5
|
groups: BorrowingFee.Group[];
|
|
6
6
|
pairs: BorrowingFee.Pair[];
|
|
7
|
-
openInterest: OpenInterest;
|
|
8
7
|
};
|
|
9
8
|
export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
|
|
10
9
|
export declare const withinMaxGroupOi: (pairIndex: PairIndex, long: boolean, positionSizeCollateral: number, context: {
|
|
@@ -28,10 +28,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = exports.getBorrowingFee = void 0;
|
|
30
30
|
const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
31
|
-
if (!context.groups ||
|
|
32
|
-
!context.pairs ||
|
|
33
|
-
!context.openInterest ||
|
|
34
|
-
!context.pairs[pairIndex]) {
|
|
31
|
+
if (!context.groups || !context.pairs || !context.pairs[pairIndex]) {
|
|
35
32
|
return 0;
|
|
36
33
|
}
|
|
37
34
|
const { pairs } = context;
|
|
@@ -39,9 +36,13 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
39
36
|
const firstPairGroup = (pairGroups === null || pairGroups === void 0 ? void 0 : pairGroups.length) > 0 ? pairGroups[0] : undefined;
|
|
40
37
|
let fee = 0;
|
|
41
38
|
if (!firstPairGroup || firstPairGroup.block > initialAccFees.block) {
|
|
39
|
+
const openInterest = pairs[pairIndex].oi;
|
|
42
40
|
fee =
|
|
43
41
|
(!firstPairGroup
|
|
44
|
-
? getPairPendingAccFee(pairIndex, context.currentBlock, long,
|
|
42
|
+
? getPairPendingAccFee(pairIndex, context.currentBlock, long, {
|
|
43
|
+
pairs,
|
|
44
|
+
openInterest,
|
|
45
|
+
})
|
|
45
46
|
: long
|
|
46
47
|
? firstPairGroup.pairAccFeeLong
|
|
47
48
|
: firstPairGroup.pairAccFeeShort) - initialAccFees.accPairFee;
|
|
@@ -96,13 +97,14 @@ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long,
|
|
|
96
97
|
const beforeTradeOpen = group.block < initialFees.block;
|
|
97
98
|
let deltaGroup, deltaPair;
|
|
98
99
|
if (i == pairGroups.length - 1) {
|
|
99
|
-
const { currentBlock, groups, pairs
|
|
100
|
+
const { currentBlock, groups, pairs } = context;
|
|
101
|
+
const openInterest = pairs[pairIndex].oi;
|
|
100
102
|
deltaGroup = getGroupPendingAccFee(group.groupIndex, currentBlock, long, {
|
|
101
103
|
groups,
|
|
102
104
|
});
|
|
103
105
|
deltaPair = getPairPendingAccFee(pairIndex, currentBlock, long, {
|
|
104
106
|
pairs,
|
|
105
|
-
openInterest
|
|
107
|
+
openInterest,
|
|
106
108
|
});
|
|
107
109
|
}
|
|
108
110
|
else {
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
|
-
import { Fee, Trade } from "./types";
|
|
2
|
+
import { Fee, LiquidationParams, Trade } from "./types";
|
|
3
|
+
import { ContractsVersion } from "../contracts/types";
|
|
3
4
|
export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
|
|
5
|
+
liquidationParams: LiquidationParams | undefined;
|
|
6
|
+
pairSpreadP: number | undefined;
|
|
4
7
|
collateralPriceUsd: number | undefined;
|
|
8
|
+
contractsVersion: ContractsVersion | undefined;
|
|
5
9
|
};
|
|
6
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) => number;
|
package/lib/trade/liquidation.js
CHANGED
|
@@ -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) => {
|
|
8
|
+
var _a;
|
|
6
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
|
|
9
|
-
|
|
11
|
+
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
|
|
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) !== 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
|
+
const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams);
|
|
21
|
+
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
22
|
+
}
|
|
12
23
|
return trade.long
|
|
13
24
|
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
14
25
|
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
15
26
|
};
|
|
16
27
|
exports.getLiquidationPrice = getLiquidationPrice;
|
|
28
|
+
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
29
|
+
if (liquidationParams === undefined ||
|
|
30
|
+
leverage === undefined ||
|
|
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;
|
package/lib/trade/pnl.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
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 "../contracts/types";
|
|
3
4
|
export type GetPnlContext = GetBorrowingFeeContext & {
|
|
4
5
|
fee: Fee | undefined;
|
|
5
6
|
maxGainP: number | undefined;
|
|
6
7
|
collateralPriceUsd: number | undefined;
|
|
8
|
+
contractsVersion: ContractsVersion | undefined;
|
|
7
9
|
feeMultiplier: number | undefined;
|
|
8
10
|
};
|
|
9
|
-
export declare const getPnl: (price: number | undefined, trade: Trade, tradeInfo: TradeInfo, initialAccFees: TradeInitialAccFees, useFees: boolean, context: GetPnlContext) => number[] | undefined;
|
|
11
|
+
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
|
|
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,7 +20,8 @@ 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 <=
|
|
23
|
+
if (pnlPercentage <=
|
|
24
|
+
(0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
|
|
23
25
|
pnlPercentage = -100;
|
|
24
26
|
}
|
|
25
27
|
else {
|
package/lib/trade/spread.d.ts
CHANGED
|
@@ -1,2 +1,18 @@
|
|
|
1
|
-
import { OiWindows, OiWindowsSettings, PairDepth } from "./types";
|
|
2
|
-
|
|
1
|
+
import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth } from "./types";
|
|
2
|
+
import { ContractsVersion } from "../contracts/types";
|
|
3
|
+
export type SpreadContext = {
|
|
4
|
+
isOpen?: boolean;
|
|
5
|
+
isPnlPositive?: boolean;
|
|
6
|
+
protectionCloseFactor?: number;
|
|
7
|
+
protectionCloseFactorBlocks?: number;
|
|
8
|
+
cumulativeFactor?: number;
|
|
9
|
+
createdBlock?: number;
|
|
10
|
+
liquidationParams?: LiquidationParams | undefined;
|
|
11
|
+
currentBlock?: number | undefined;
|
|
12
|
+
contractsVersion?: ContractsVersion | undefined;
|
|
13
|
+
};
|
|
14
|
+
export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
|
|
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) => number;
|
package/lib/trade/spread.js
CHANGED
|
@@ -1,22 +1,83 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSpreadWithPriceImpactP = void 0;
|
|
3
|
+
exports.getSpreadP = exports.getSpreadWithPriceImpactP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.getProtectionCloseFactor = void 0;
|
|
4
4
|
const oiWindows_1 = require("./oiWindows");
|
|
5
|
-
const
|
|
5
|
+
const constants_1 = require("../constants");
|
|
6
|
+
const types_1 = require("../contracts/types");
|
|
7
|
+
const getProtectionCloseFactor = (spreadCtx) => {
|
|
8
|
+
if (spreadCtx === undefined ||
|
|
9
|
+
spreadCtx.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
|
|
10
|
+
spreadCtx.isOpen === undefined ||
|
|
11
|
+
spreadCtx.isPnlPositive === undefined ||
|
|
12
|
+
spreadCtx.protectionCloseFactor === undefined ||
|
|
13
|
+
spreadCtx.protectionCloseFactorBlocks === undefined ||
|
|
14
|
+
spreadCtx.createdBlock === undefined ||
|
|
15
|
+
spreadCtx.currentBlock === undefined)
|
|
16
|
+
return constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR;
|
|
17
|
+
if (spreadCtx.isPnlPositive &&
|
|
18
|
+
!spreadCtx.isOpen &&
|
|
19
|
+
spreadCtx.protectionCloseFactor > 0 &&
|
|
20
|
+
spreadCtx.currentBlock <=
|
|
21
|
+
spreadCtx.createdBlock + spreadCtx.protectionCloseFactorBlocks) {
|
|
22
|
+
return spreadCtx.protectionCloseFactor;
|
|
23
|
+
}
|
|
24
|
+
return constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR;
|
|
25
|
+
};
|
|
26
|
+
exports.getProtectionCloseFactor = getProtectionCloseFactor;
|
|
27
|
+
const getCumulativeFactor = (spreadCtx) => {
|
|
28
|
+
if (spreadCtx === undefined || spreadCtx.cumulativeFactor === undefined) {
|
|
29
|
+
return constants_1.DEFAULT_CUMULATIVE_FACTOR;
|
|
30
|
+
}
|
|
31
|
+
return spreadCtx.cumulativeFactor;
|
|
32
|
+
};
|
|
33
|
+
exports.getCumulativeFactor = getCumulativeFactor;
|
|
34
|
+
const getLegacyFactor = (spreadCtx) => {
|
|
35
|
+
return (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
|
|
36
|
+
};
|
|
37
|
+
exports.getLegacyFactor = getLegacyFactor;
|
|
38
|
+
const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, spreadCtx) => {
|
|
6
39
|
if (pairSpreadP === undefined) {
|
|
7
40
|
return 0;
|
|
8
41
|
}
|
|
42
|
+
// No spread or price impact when closing pre-v9.2 trades
|
|
43
|
+
if ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
|
|
44
|
+
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) {
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
9
47
|
const onePercentDepth = buy
|
|
10
|
-
?
|
|
11
|
-
|
|
48
|
+
? // if `long`
|
|
49
|
+
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
|
|
50
|
+
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
|
|
51
|
+
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
52
|
+
: // if `short`
|
|
53
|
+
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
|
|
54
|
+
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
55
|
+
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
|
|
12
56
|
let activeOi = undefined;
|
|
13
57
|
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);
|
|
58
|
+
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
59
|
}
|
|
16
60
|
if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
|
|
17
|
-
return pairSpreadP;
|
|
61
|
+
return pairSpreadP / 2;
|
|
18
62
|
}
|
|
19
|
-
return (pairSpreadP +
|
|
20
|
-
(activeOi + (collateral * leverage) / 2) /
|
|
63
|
+
return ((0, exports.getSpreadP)(pairSpreadP) +
|
|
64
|
+
((activeOi * (0, exports.getCumulativeFactor)(spreadCtx) + (collateral * leverage) / 2) /
|
|
65
|
+
onePercentDepth /
|
|
66
|
+
100 /
|
|
67
|
+
(0, exports.getLegacyFactor)(spreadCtx)) *
|
|
68
|
+
(0, exports.getProtectionCloseFactor)(spreadCtx));
|
|
21
69
|
};
|
|
22
70
|
exports.getSpreadWithPriceImpactP = getSpreadWithPriceImpactP;
|
|
71
|
+
const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams) => {
|
|
72
|
+
if (pairSpreadP === undefined || pairSpreadP === 0) {
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
const spreadP = pairSpreadP / 2;
|
|
76
|
+
return isLiquidation === true &&
|
|
77
|
+
liquidationParams !== undefined &&
|
|
78
|
+
liquidationParams.maxLiqSpreadP > 0 &&
|
|
79
|
+
spreadP > liquidationParams.maxLiqSpreadP
|
|
80
|
+
? liquidationParams.maxLiqSpreadP
|
|
81
|
+
: spreadP;
|
|
82
|
+
};
|
|
83
|
+
exports.getSpreadP = getSpreadP;
|
package/lib/trade/types.d.ts
CHANGED
|
@@ -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
|
};
|
|
@@ -31,6 +32,15 @@ export type TradeInfo = {
|
|
|
31
32
|
maxSlippageP: number;
|
|
32
33
|
lastOiUpdateTs: number;
|
|
33
34
|
collateralPriceUsd: number;
|
|
35
|
+
contractsVersion: number;
|
|
36
|
+
lastPosIncreaseBlock: number;
|
|
37
|
+
};
|
|
38
|
+
export type LiquidationParams = {
|
|
39
|
+
maxLiqSpreadP: number;
|
|
40
|
+
startLiqThresholdP: number;
|
|
41
|
+
endLiqThresholdP: number;
|
|
42
|
+
startLeverage: number;
|
|
43
|
+
endLeverage: number;
|
|
34
44
|
};
|
|
35
45
|
export type TradingGroup = {
|
|
36
46
|
maxLeverage: number;
|
|
@@ -122,6 +132,7 @@ export declare enum PositionType {
|
|
|
122
132
|
export type TradeContainerRaw = {
|
|
123
133
|
trade: ITradingStorage.TradeStruct;
|
|
124
134
|
tradeInfo: ITradingStorage.TradeInfoStruct;
|
|
135
|
+
liquidationParams: IPairsStorage.GroupLiquidationParamsStruct;
|
|
125
136
|
initialAccFees: IBorrowingFees.BorrowingInitialAccFeesStruct;
|
|
126
137
|
};
|
|
127
138
|
export type OiWindowsSettings = {
|
|
@@ -156,6 +167,11 @@ export type TraderFeeTiers = {
|
|
|
156
167
|
lastDayUpdatedPoints: number;
|
|
157
168
|
expiredPoints: number[];
|
|
158
169
|
};
|
|
170
|
+
export type PairFactor = {
|
|
171
|
+
cumulativeFactor: number;
|
|
172
|
+
protectionCloseFactor: number;
|
|
173
|
+
protectionCloseFactorBlocks: number;
|
|
174
|
+
};
|
|
159
175
|
export declare enum PendingOrderType {
|
|
160
176
|
MARKET_OPEN = 0,
|
|
161
177
|
MARKET_CLOSE = 1,
|