@gainsnetwork/sdk 0.2.12-rc1 → 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/constants.d.ts +4 -0
- package/lib/constants.js +5 -1
- package/lib/contracts/utils/pairs.js +4 -0
- 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 +22 -3
- package/lib/trade/types.d.ts +5 -1
- package/lib/trade/types.js +4 -0
- package/package.json +1 -1
package/lib/constants.d.ts
CHANGED
|
@@ -250,6 +250,10 @@ export declare const pairs: {
|
|
|
250
250
|
"CORE/USD": string;
|
|
251
251
|
"JASMY/USD": string;
|
|
252
252
|
"DAR/USD": string;
|
|
253
|
+
"MEW/USD": string;
|
|
254
|
+
"DEGEN/USD": string;
|
|
255
|
+
"SLERF/USD": string;
|
|
256
|
+
"UXLINK/USD": string;
|
|
253
257
|
};
|
|
254
258
|
export declare const getAssetClassFromGroupIndex: (groupIndex: number) => string | undefined;
|
|
255
259
|
export declare const tickerChanges: {
|
package/lib/constants.js
CHANGED
|
@@ -258,6 +258,10 @@ exports.pairs = {
|
|
|
258
258
|
"CORE/USD": CRYPTO,
|
|
259
259
|
"JASMY/USD": CRYPTO,
|
|
260
260
|
"DAR/USD": CRYPTO,
|
|
261
|
+
"MEW/USD": CRYPTO,
|
|
262
|
+
"DEGEN/USD": CRYPTO,
|
|
263
|
+
"SLERF/USD": CRYPTO,
|
|
264
|
+
"UXLINK/USD": CRYPTO,
|
|
261
265
|
};
|
|
262
266
|
const getAssetClassFromGroupIndex = (groupIndex) => {
|
|
263
267
|
switch (groupIndex) {
|
|
@@ -292,7 +296,7 @@ exports.delistedPairIxs = new Set([
|
|
|
292
296
|
6, 31, 36, 42, 45, 48, 51, 54, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
|
|
293
297
|
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
|
|
294
298
|
89, 97, 99, 101, 106, 108, 52, 131, 147, 160, 179, 182, 183, 190, 229, 163,
|
|
295
|
-
155, 15, 170, 239,
|
|
299
|
+
155, 15, 170, 239, 251, 252, 253, 254,
|
|
296
300
|
]);
|
|
297
301
|
exports.delistedGroupsIxs = new Set([6, 7]);
|
|
298
302
|
exports.DEFAULT_PROTECTION_CLOSE_FACTOR = 1;
|
|
@@ -373,4 +373,8 @@ const PAIR_INDEX_TO_DESCRIPTION = {
|
|
|
373
373
|
[types_1.PairIndex.COREUSD]: "Core to US Dollar",
|
|
374
374
|
[types_1.PairIndex.JASMYUSD]: "Jasmy Coin to US Dollar",
|
|
375
375
|
[types_1.PairIndex.DARUSD]: "Mines of Dalarnia to US Dollar",
|
|
376
|
+
[types_1.PairIndex.MEWUSD]: "cat in a dogs world to US Dollar",
|
|
377
|
+
[types_1.PairIndex.DEGENUSD]: "Degen to US Dollar",
|
|
378
|
+
[types_1.PairIndex.SLERFUSD]: "Slerf to US Dollar",
|
|
379
|
+
[types_1.PairIndex.UXLINKUSD]: "UXLINK to US Dollar",
|
|
376
380
|
};
|
|
@@ -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,13 +26,19 @@ 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
|
|
32
38
|
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
|
|
33
39
|
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
34
40
|
: // if `short`
|
|
35
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
|
|
41
|
+
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
|
|
36
42
|
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
37
43
|
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
|
|
38
44
|
let activeOi = undefined;
|
|
@@ -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;
|
package/lib/trade/types.d.ts
CHANGED
package/lib/trade/types.js
CHANGED
|
@@ -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 = {}));
|