@gainsnetwork/sdk 0.2.12-rc2 → 0.2.12-rc3
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/trade/liquidation.d.ts +7 -2
- package/lib/trade/liquidation.js +34 -3
- package/lib/trade/pnl.d.ts +2 -2
- package/lib/trade/pnl.js +4 -2
- package/lib/trade/spread.d.ts +3 -1
- package/lib/trade/spread.js +21 -2
- package/package.json +1 -1
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
|
-
import { Fee, Trade } from "./types";
|
|
3
|
-
export
|
|
2
|
+
import { Fee, LiquidationParams, Trade } from "./types";
|
|
3
|
+
export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
|
|
4
|
+
liquidationParams: LiquidationParams | undefined;
|
|
5
|
+
pairSpreadP: number | undefined;
|
|
6
|
+
};
|
|
7
|
+
export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
|
|
8
|
+
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|
package/lib/trade/liquidation.js
CHANGED
|
@@ -1,16 +1,47 @@
|
|
|
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");
|
|
5
6
|
const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
7
|
+
var _a;
|
|
6
8
|
const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee);
|
|
7
9
|
const borrowingFee = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
+
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
|
|
11
|
+
let liqPriceDistance = (trade.openPrice *
|
|
12
|
+
(trade.collateralAmount * liqThresholdP - (borrowingFee + closingFee))) /
|
|
10
13
|
trade.collateralAmount /
|
|
11
14
|
trade.leverage;
|
|
15
|
+
if (((_a = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
16
|
+
context.liquidationParams.maxLiqSpreadP > 0) {
|
|
17
|
+
const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams);
|
|
18
|
+
liqPriceDistance += trade.openPrice * closingSpreadP;
|
|
19
|
+
}
|
|
12
20
|
return trade.long
|
|
13
21
|
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
14
22
|
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
15
23
|
};
|
|
16
24
|
exports.getLiquidationPrice = getLiquidationPrice;
|
|
25
|
+
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
26
|
+
if (liquidationParams === undefined ||
|
|
27
|
+
leverage === undefined ||
|
|
28
|
+
liquidationParams.maxLiqSpreadP === 0 ||
|
|
29
|
+
liquidationParams.startLiqThresholdP === 0 ||
|
|
30
|
+
liquidationParams.endLiqThresholdP === 0 ||
|
|
31
|
+
liquidationParams.startLeverage === 0 ||
|
|
32
|
+
liquidationParams.endLeverage === 0) {
|
|
33
|
+
return 0.9;
|
|
34
|
+
}
|
|
35
|
+
if (leverage < liquidationParams.startLeverage) {
|
|
36
|
+
return liquidationParams.startLiqThresholdP;
|
|
37
|
+
}
|
|
38
|
+
if (leverage > liquidationParams.endLeverage) {
|
|
39
|
+
return liquidationParams.endLiqThresholdP;
|
|
40
|
+
}
|
|
41
|
+
return (liquidationParams.startLiqThresholdP -
|
|
42
|
+
((leverage - liquidationParams.startLeverage) *
|
|
43
|
+
(liquidationParams.startLiqThresholdP -
|
|
44
|
+
liquidationParams.endLiqThresholdP)) /
|
|
45
|
+
(liquidationParams.endLeverage - liquidationParams.startLeverage));
|
|
46
|
+
};
|
|
47
|
+
exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
|
package/lib/trade/pnl.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { GetBorrowingFeeContext } from "./fees";
|
|
2
|
-
import { Fee, Trade, TradeInfo, TradeInitialAccFees } from "./types";
|
|
2
|
+
import { Fee, LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
|
|
3
3
|
export type GetPnlContext = GetBorrowingFeeContext & {
|
|
4
4
|
fee: Fee | undefined;
|
|
5
5
|
maxGainP: number | undefined;
|
|
6
6
|
};
|
|
7
|
-
export declare const getPnl: (price: number | undefined, trade: Trade, tradeInfo: TradeInfo, initialAccFees: TradeInitialAccFees, useFees: boolean, context: GetPnlContext) => number[] | undefined;
|
|
7
|
+
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,11 +1,13 @@
|
|
|
1
|
-
import { OiWindows, OiWindowsSettings, PairDepth } from "./types";
|
|
1
|
+
import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth } from "./types";
|
|
2
2
|
export type SpreadContext = {
|
|
3
3
|
isOpen?: boolean;
|
|
4
4
|
isPnlPositive?: boolean;
|
|
5
5
|
protectionCloseFactor?: number;
|
|
6
6
|
protectionCloseFactorBlocks?: number;
|
|
7
7
|
createdBlock?: number;
|
|
8
|
+
liquidationParams?: LiquidationParams | undefined;
|
|
8
9
|
currentBlock: number | undefined;
|
|
9
10
|
};
|
|
10
11
|
export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
|
|
11
12
|
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;
|
|
13
|
+
export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation?: boolean | undefined, liquidationParams?: LiquidationParams | undefined) => number;
|
package/lib/trade/spread.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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
6
|
const getProtectionCloseFactor = (spreadCtx) => {
|
|
@@ -26,6 +26,12 @@ const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairD
|
|
|
26
26
|
if (pairSpreadP === undefined) {
|
|
27
27
|
return 0;
|
|
28
28
|
}
|
|
29
|
+
// No spread or price impact when closing pre-v9.2 trades
|
|
30
|
+
if ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
|
|
31
|
+
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.liquidationParams) !== undefined &&
|
|
32
|
+
spreadCtx.liquidationParams.maxLiqSpreadP === 0) {
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
29
35
|
const onePercentDepth = buy
|
|
30
36
|
? // if `long`
|
|
31
37
|
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
|
|
@@ -42,8 +48,21 @@ const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairD
|
|
|
42
48
|
if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
|
|
43
49
|
return pairSpreadP / 2;
|
|
44
50
|
}
|
|
45
|
-
return (pairSpreadP
|
|
51
|
+
return ((0, exports.getSpreadP)(pairSpreadP) +
|
|
46
52
|
((activeOi + (collateral * leverage) / 2) / onePercentDepth / 100 / 2) *
|
|
47
53
|
(0, exports.getProtectionCloseFactor)(spreadCtx));
|
|
48
54
|
};
|
|
49
55
|
exports.getSpreadWithPriceImpactP = getSpreadWithPriceImpactP;
|
|
56
|
+
const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams) => {
|
|
57
|
+
if (pairSpreadP === undefined || pairSpreadP === 0) {
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
const spreadP = pairSpreadP / 2;
|
|
61
|
+
return isLiquidation === true &&
|
|
62
|
+
liquidationParams !== undefined &&
|
|
63
|
+
liquidationParams.maxLiqSpreadP > 0 &&
|
|
64
|
+
spreadP > liquidationParams.maxLiqSpreadP
|
|
65
|
+
? liquidationParams.maxLiqSpreadP
|
|
66
|
+
: spreadP;
|
|
67
|
+
};
|
|
68
|
+
exports.getSpreadP = getSpreadP;
|