@gainsnetwork/sdk 0.2.12-rc3 → 0.2.12
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 +0 -1
- package/lib/constants.js +2 -3
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +412 -892
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +94 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +852 -2130
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/utils/openLimitOrders.d.ts +8 -0
- package/lib/contracts/utils/openLimitOrders.js +88 -0
- package/lib/contracts/utils/openTrades.js +2 -11
- package/lib/contracts/utils/pairs.d.ts +0 -1
- package/lib/contracts/utils/pairs.js +1 -18
- package/lib/markets/forex.js +2 -2
- package/lib/trade/liquidation.d.ts +2 -7
- package/lib/trade/liquidation.js +3 -34
- package/lib/trade/openLimitOrder.d.ts +2 -0
- package/lib/trade/openLimitOrder.js +23 -0
- package/lib/trade/pnl.d.ts +2 -2
- package/lib/trade/pnl.js +2 -4
- package/lib/trade/spread.d.ts +2 -13
- package/lib/trade/spread.js +8 -54
- package/lib/trade/types.d.ts +1 -10
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { LimitOrder } from "../../trade/types";
|
|
2
|
+
import { BlockTag, Contracts } from "../types";
|
|
3
|
+
export type FetchOpenLimitOrdersOverrides = {
|
|
4
|
+
blockTag?: BlockTag;
|
|
5
|
+
useMulticall?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare const fetchOpenLimitOrders: (contracts: Contracts, overrides?: FetchOpenLimitOrdersOverrides) => Promise<LimitOrder[]>;
|
|
8
|
+
export declare const fetchOpenLimitOrdersRaw: (contracts: Contracts, overrides?: FetchOpenLimitOrdersOverrides) => Promise<any[]>;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
5
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
6
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
7
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
8
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
9
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
10
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
11
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.fetchOpenLimitOrdersRaw = exports.fetchOpenLimitOrders = void 0;
|
|
16
|
+
const ethcall_1 = require("ethcall");
|
|
17
|
+
const fetchOpenLimitOrders = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
const openLimitOrdersRaw = yield (0, exports.fetchOpenLimitOrdersRaw)(contracts, overrides);
|
|
19
|
+
return openLimitOrdersRaw.map((order) => ({
|
|
20
|
+
block: parseInt(order.block.toString()),
|
|
21
|
+
buy: order.buy,
|
|
22
|
+
index: parseInt(order.index.toString()),
|
|
23
|
+
leverage: parseInt(order.leverage.toString()),
|
|
24
|
+
maxPrice: parseFloat(order.maxPrice.toString()) / 1e10,
|
|
25
|
+
minPrice: parseFloat(order.minPrice.toString()) / 1e10,
|
|
26
|
+
pairIndex: parseInt(order.pairIndex.toString()),
|
|
27
|
+
positionSize: parseFloat(order.positionSize.toString()) / 1e18,
|
|
28
|
+
sl: parseFloat(order.sl.toString()) / 1e10,
|
|
29
|
+
spreadReductionP: parseInt(order.spreadReductionP.toString()) / 100,
|
|
30
|
+
tp: parseFloat(order.tp.toString()) / 1e10,
|
|
31
|
+
trader: order.trader,
|
|
32
|
+
type: parseInt(order.type.toString()),
|
|
33
|
+
maxSlippageP: parseFloat(order.maxSlippageP.toString()) / 1e10,
|
|
34
|
+
}));
|
|
35
|
+
});
|
|
36
|
+
exports.fetchOpenLimitOrders = fetchOpenLimitOrders;
|
|
37
|
+
const fetchOpenLimitOrdersRaw = (contracts, overrides = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
38
|
+
if (!contracts) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
console.time("fetchOpenLimitOrdersRaw");
|
|
42
|
+
const { useMulticall = false, blockTag = "latest" } = overrides;
|
|
43
|
+
const { gfarmTradingStorageV5: storageContract, gnsNftRewards: nftRewards, gnsTradingCallbacks: callbacks, } = contracts;
|
|
44
|
+
const openLimitOrders = yield storageContract.getOpenLimitOrders({
|
|
45
|
+
blockTag,
|
|
46
|
+
});
|
|
47
|
+
let openLimitOrderTypes = [];
|
|
48
|
+
let openLimitOrderTradeData = [];
|
|
49
|
+
if (useMulticall) {
|
|
50
|
+
const multicallProvider = new ethcall_1.Provider();
|
|
51
|
+
yield multicallProvider.init(storageContract.provider);
|
|
52
|
+
const nftRewardsContractMulticall = new ethcall_1.Contract(nftRewards.address, [
|
|
53
|
+
...nftRewards.interface.fragments,
|
|
54
|
+
]);
|
|
55
|
+
const callbacksContractMulticall = new ethcall_1.Contract(callbacks.address, [
|
|
56
|
+
...callbacks.interface.fragments,
|
|
57
|
+
]);
|
|
58
|
+
openLimitOrderTypes = yield multicallProvider.all(openLimitOrders.map(order => nftRewardsContractMulticall.openLimitOrderTypes(order.trader, order.pairIndex, order.index)), blockTag);
|
|
59
|
+
openLimitOrderTradeData = yield multicallProvider.all(openLimitOrders.map(order => callbacksContractMulticall.tradeData(order.trader, order.pairIndex, order.index, 1)), blockTag);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
openLimitOrderTypes = yield Promise.all(openLimitOrders.map(order => nftRewards.openLimitOrderTypes(order.trader, order.pairIndex, order.index, { blockTag })));
|
|
63
|
+
openLimitOrderTradeData = yield Promise.all(openLimitOrders.map(order => callbacks.tradeData(order.trader, order.pairIndex, order.index, 1, {
|
|
64
|
+
blockTag,
|
|
65
|
+
})));
|
|
66
|
+
}
|
|
67
|
+
return openLimitOrderTypes.map((openLimitOrderType, index) => {
|
|
68
|
+
const openLimitOrder = openLimitOrders[index];
|
|
69
|
+
const tradeData = openLimitOrderTradeData[index];
|
|
70
|
+
return {
|
|
71
|
+
trader: openLimitOrder.trader,
|
|
72
|
+
pairIndex: openLimitOrder.pairIndex,
|
|
73
|
+
index: openLimitOrder.index,
|
|
74
|
+
positionSize: openLimitOrder.positionSize,
|
|
75
|
+
spreadReductionP: openLimitOrder.spreadReductionP,
|
|
76
|
+
buy: openLimitOrder.buy,
|
|
77
|
+
leverage: openLimitOrder.leverage,
|
|
78
|
+
tp: openLimitOrder.tp,
|
|
79
|
+
sl: openLimitOrder.sl,
|
|
80
|
+
minPrice: openLimitOrder.minPrice,
|
|
81
|
+
maxPrice: openLimitOrder.maxPrice,
|
|
82
|
+
block: openLimitOrder.block,
|
|
83
|
+
type: openLimitOrderType,
|
|
84
|
+
maxSlippageP: tradeData.maxSlippageP,
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
exports.fetchOpenLimitOrdersRaw = fetchOpenLimitOrdersRaw;
|
|
@@ -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.
|
|
17
|
+
return rawTrades.map(rawTrade => _prepareTradeContainer(rawTrade.trade, rawTrade.tradeInfo, rawTrade.initialAccFees, collateralPrecisions[parseInt(rawTrade.trade.collateralIndex.toString()) - 1]));
|
|
18
18
|
});
|
|
19
19
|
exports.fetchOpenPairTrades = fetchOpenPairTrades;
|
|
20
20
|
// @todo rename
|
|
@@ -40,7 +40,6 @@ 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);
|
|
44
43
|
// Array is always of length `batchSize`
|
|
45
44
|
// so we need to filter out the empty trades, indexes are reliable
|
|
46
45
|
const openTrades = trades
|
|
@@ -49,7 +48,6 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
|
|
|
49
48
|
.map((trade, ix) => ({
|
|
50
49
|
trade,
|
|
51
50
|
tradeInfo: tradeInfos[ix],
|
|
52
|
-
liquidationParams: tradeLiquidationParams[ix],
|
|
53
51
|
initialAccFees: {
|
|
54
52
|
accPairFee: 0,
|
|
55
53
|
accGroupFee: 0,
|
|
@@ -85,7 +83,7 @@ const fetchOpenPairTradesRaw = (contracts, overrides = {}) => __awaiter(void 0,
|
|
|
85
83
|
}
|
|
86
84
|
});
|
|
87
85
|
exports.fetchOpenPairTradesRaw = fetchOpenPairTradesRaw;
|
|
88
|
-
const _prepareTradeContainer = (trade, tradeInfo,
|
|
86
|
+
const _prepareTradeContainer = (trade, tradeInfo, tradeInitialAccFees, collateralPrecision) => ({
|
|
89
87
|
trade: {
|
|
90
88
|
user: trade.user,
|
|
91
89
|
index: parseInt(trade.index.toString()),
|
|
@@ -109,13 +107,6 @@ const _prepareTradeContainer = (trade, tradeInfo, tradeLiquidationParams, tradeI
|
|
|
109
107
|
lastOiUpdateTs: parseFloat(tradeInfo.lastOiUpdateTs),
|
|
110
108
|
collateralPriceUsd: parseFloat(tradeInfo.collateralPriceUsd.toString()) / 1e8,
|
|
111
109
|
},
|
|
112
|
-
liquidationParams: {
|
|
113
|
-
maxLiqSpreadP: parseFloat(tradeLiquidationParams.maxLiqSpreadP.toString()) / 1e10,
|
|
114
|
-
startLiqThresholdP: parseFloat(tradeLiquidationParams.startLiqThresholdP.toString()) / 1e10,
|
|
115
|
-
endLiqThresholdP: parseFloat(tradeLiquidationParams.endLiqThresholdP.toString()) / 1e10,
|
|
116
|
-
startLeverage: parseFloat(tradeLiquidationParams.startLeverage.toString()) / 1e3,
|
|
117
|
-
endLeverage: parseFloat(tradeLiquidationParams.endLeverage.toString()) / 1e3,
|
|
118
|
-
},
|
|
119
110
|
initialAccFees: {
|
|
120
111
|
accPairFee: parseFloat(tradeInitialAccFees.accPairFee.toString()) / 1e10,
|
|
121
112
|
accGroupFee: parseFloat(tradeInitialAccFees.accGroupFee.toString()) / 1e10,
|
|
@@ -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.
|
|
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 [];
|
package/lib/markets/forex.js
CHANGED
|
@@ -36,8 +36,8 @@ const isForexLowLiquidity = (timestampToCheck, pair) => {
|
|
|
36
36
|
const groupIndex = pair === null || pair === void 0 ? void 0 : pair.groupIndex;
|
|
37
37
|
if (groupIndex && extendedLowLiqGroupIds.includes(+groupIndex)) {
|
|
38
38
|
return ((isInDST &&
|
|
39
|
-
((hour ==
|
|
40
|
-
(!isInDST && ((hour ==
|
|
39
|
+
((hour == 14 && minute >= 45) || (hour >= 16 && hour < 21))) ||
|
|
40
|
+
(!isInDST && ((hour == 15 && minute >= 45) || (hour >= 17 && hour < 22))));
|
|
41
41
|
}
|
|
42
42
|
return ((isInDST && ((hour == 15 && minute >= 45) || (hour >= 16 && hour < 19))) ||
|
|
43
43
|
(!isInDST && ((hour == 16 && minute >= 45) || (hour >= 17 && hour < 20))));
|
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
|
-
import { Fee,
|
|
3
|
-
export
|
|
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;
|
|
2
|
+
import { Fee, Trade } from "./types";
|
|
3
|
+
export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
|
package/lib/trade/liquidation.js
CHANGED
|
@@ -1,47 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getLiquidationPrice = void 0;
|
|
4
4
|
const fees_1 = require("./fees");
|
|
5
|
-
const spread_1 = require("./spread");
|
|
6
5
|
const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
7
|
-
var _a;
|
|
8
6
|
const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee);
|
|
9
7
|
const borrowingFee = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
(trade.collateralAmount * liqThresholdP - (borrowingFee + closingFee))) /
|
|
8
|
+
const liqPriceDistance = (trade.openPrice *
|
|
9
|
+
(trade.collateralAmount * 0.9 - (borrowingFee + closingFee))) /
|
|
13
10
|
trade.collateralAmount /
|
|
14
11
|
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
|
-
}
|
|
20
12
|
return trade.long
|
|
21
13
|
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
22
14
|
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
23
15
|
};
|
|
24
16
|
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;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFulfillmentPrice = void 0;
|
|
4
|
+
const spread_1 = require("./spread");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
const getFulfillmentPrice = (order, pair, pairDepth, oiWindowsSettings, oiWindows) => {
|
|
7
|
+
if (!order || !pair) {
|
|
8
|
+
return 0;
|
|
9
|
+
}
|
|
10
|
+
// Get spread percentage
|
|
11
|
+
const spreadWithPriceImpactP = (0, spread_1.getSpreadWithPriceImpactP)(pair.spreadP, order.buy, order.positionSize, order.leverage, pairDepth, oiWindowsSettings, oiWindows);
|
|
12
|
+
if (spreadWithPriceImpactP === 0) {
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
const askingPrice = (order.buy && order.type === types_1.OpenLimitOrderType.REVERSAL) ||
|
|
16
|
+
(!order.buy && order.type === types_1.OpenLimitOrderType.MOMENTUM)
|
|
17
|
+
? order.maxPrice
|
|
18
|
+
: order.minPrice;
|
|
19
|
+
return order.buy
|
|
20
|
+
? askingPrice * (1 + spreadWithPriceImpactP)
|
|
21
|
+
: askingPrice * (1 - spreadWithPriceImpactP);
|
|
22
|
+
};
|
|
23
|
+
exports.getFulfillmentPrice = getFulfillmentPrice;
|
package/lib/trade/pnl.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { GetBorrowingFeeContext } from "./fees";
|
|
2
|
-
import { Fee,
|
|
2
|
+
import { Fee, 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,
|
|
7
|
+
export declare const getPnl: (price: number | undefined, trade: Trade, tradeInfo: TradeInfo, initialAccFees: TradeInitialAccFees, useFees: boolean, context: GetPnlContext) => number[] | undefined;
|
package/lib/trade/pnl.js
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getPnl = void 0;
|
|
4
4
|
const fees_1 = require("./fees");
|
|
5
|
-
const
|
|
6
|
-
const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
|
|
5
|
+
const getPnl = (price, trade, tradeInfo, initialAccFees, useFees, context) => {
|
|
7
6
|
if (!price) {
|
|
8
7
|
return;
|
|
9
8
|
}
|
|
@@ -20,8 +19,7 @@ const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useF
|
|
|
20
19
|
}
|
|
21
20
|
let pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
22
21
|
// Can be liquidated
|
|
23
|
-
if (pnlPercentage <=
|
|
24
|
-
(0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
|
|
22
|
+
if (pnlPercentage <= -90) {
|
|
25
23
|
pnlPercentage = -100;
|
|
26
24
|
}
|
|
27
25
|
else {
|
package/lib/trade/spread.d.ts
CHANGED
|
@@ -1,13 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
3
|
-
isOpen?: boolean;
|
|
4
|
-
isPnlPositive?: boolean;
|
|
5
|
-
protectionCloseFactor?: number;
|
|
6
|
-
protectionCloseFactorBlocks?: number;
|
|
7
|
-
createdBlock?: number;
|
|
8
|
-
liquidationParams?: LiquidationParams | undefined;
|
|
9
|
-
currentBlock: number | undefined;
|
|
10
|
-
};
|
|
11
|
-
export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
|
|
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;
|
|
1
|
+
import { OiWindows, OiWindowsSettings, PairDepth } from "./types";
|
|
2
|
+
export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined) => number;
|
package/lib/trade/spread.js
CHANGED
|
@@ -1,68 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getSpreadWithPriceImpactP = void 0;
|
|
4
4
|
const oiWindows_1 = require("./oiWindows");
|
|
5
|
-
const
|
|
6
|
-
const getProtectionCloseFactor = (spreadCtx) => {
|
|
7
|
-
if (spreadCtx === undefined ||
|
|
8
|
-
spreadCtx.isOpen === undefined ||
|
|
9
|
-
spreadCtx.isPnlPositive === undefined ||
|
|
10
|
-
spreadCtx.protectionCloseFactor === undefined ||
|
|
11
|
-
spreadCtx.protectionCloseFactorBlocks === undefined ||
|
|
12
|
-
spreadCtx.createdBlock === undefined ||
|
|
13
|
-
spreadCtx.currentBlock === undefined)
|
|
14
|
-
return constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR;
|
|
15
|
-
if (spreadCtx.isPnlPositive &&
|
|
16
|
-
!spreadCtx.isOpen &&
|
|
17
|
-
spreadCtx.protectionCloseFactor > 0 &&
|
|
18
|
-
spreadCtx.currentBlock <=
|
|
19
|
-
spreadCtx.createdBlock + spreadCtx.protectionCloseFactorBlocks) {
|
|
20
|
-
return spreadCtx.protectionCloseFactor;
|
|
21
|
-
}
|
|
22
|
-
return constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR;
|
|
23
|
-
};
|
|
24
|
-
exports.getProtectionCloseFactor = getProtectionCloseFactor;
|
|
25
|
-
const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, spreadCtx) => {
|
|
5
|
+
const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows) => {
|
|
26
6
|
if (pairSpreadP === undefined) {
|
|
27
7
|
return 0;
|
|
28
8
|
}
|
|
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
|
-
}
|
|
35
9
|
const onePercentDepth = buy
|
|
36
|
-
?
|
|
37
|
-
|
|
38
|
-
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
|
|
39
|
-
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
40
|
-
: // if `short`
|
|
41
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
|
|
42
|
-
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
43
|
-
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
|
|
10
|
+
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
|
|
11
|
+
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd;
|
|
44
12
|
let activeOi = undefined;
|
|
45
13
|
if (oiWindowsSettings !== undefined && (oiWindowsSettings === null || oiWindowsSettings === void 0 ? void 0 : oiWindowsSettings.windowsCount) > 0) {
|
|
46
|
-
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(oiWindowsSettings), oiWindowsSettings.windowsCount, oiWindows,
|
|
14
|
+
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(oiWindowsSettings), oiWindowsSettings.windowsCount, oiWindows, buy);
|
|
47
15
|
}
|
|
48
16
|
if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
|
|
49
|
-
return pairSpreadP
|
|
17
|
+
return pairSpreadP;
|
|
50
18
|
}
|
|
51
|
-
return (
|
|
52
|
-
(
|
|
53
|
-
(0, exports.getProtectionCloseFactor)(spreadCtx));
|
|
19
|
+
return (pairSpreadP +
|
|
20
|
+
(activeOi + (collateral * leverage) / 2) / onePercentDepth / 100);
|
|
54
21
|
};
|
|
55
22
|
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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ITradingStorage, IBorrowingFees
|
|
1
|
+
import { ITradingStorage, IBorrowingFees } 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,7 +7,6 @@ export type PairIndexes = {
|
|
|
7
7
|
export type TradeContainer = {
|
|
8
8
|
trade: Trade;
|
|
9
9
|
tradeInfo: TradeInfo;
|
|
10
|
-
liquidationParams: LiquidationParams;
|
|
11
10
|
initialAccFees: TradeInitialAccFees;
|
|
12
11
|
receivedAt?: number;
|
|
13
12
|
};
|
|
@@ -33,13 +32,6 @@ export type TradeInfo = {
|
|
|
33
32
|
lastOiUpdateTs: number;
|
|
34
33
|
collateralPriceUsd: number;
|
|
35
34
|
};
|
|
36
|
-
export type LiquidationParams = {
|
|
37
|
-
maxLiqSpreadP: number;
|
|
38
|
-
startLiqThresholdP: number;
|
|
39
|
-
endLiqThresholdP: number;
|
|
40
|
-
startLeverage: number;
|
|
41
|
-
endLeverage: number;
|
|
42
|
-
};
|
|
43
35
|
export type TradingGroup = {
|
|
44
36
|
maxLeverage: number;
|
|
45
37
|
minLeverage: number;
|
|
@@ -130,7 +122,6 @@ export declare enum PositionType {
|
|
|
130
122
|
export type TradeContainerRaw = {
|
|
131
123
|
trade: ITradingStorage.TradeStruct;
|
|
132
124
|
tradeInfo: ITradingStorage.TradeInfoStruct;
|
|
133
|
-
liquidationParams: IPairsStorage.GroupLiquidationParamsStruct;
|
|
134
125
|
initialAccFees: IBorrowingFees.BorrowingInitialAccFeesStruct;
|
|
135
126
|
};
|
|
136
127
|
export type OiWindowsSettings = {
|