@gainsnetwork/sdk 0.2.12-rc7 → 0.2.12-rc9
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/contracts/types/index.d.ts +4 -0
- package/lib/contracts/types/index.js +6 -1
- package/lib/trade/fees/index.d.ts +1 -1
- package/lib/trade/fees/index.js +6 -3
- package/lib/trade/liquidation.d.ts +4 -1
- package/lib/trade/liquidation.js +7 -4
- package/lib/trade/pnl.d.ts +3 -0
- package/lib/trade/pnl.js +3 -2
- package/lib/trade/spread.d.ts +2 -0
- package/lib/trade/spread.js +3 -2
- package/lib/trade/types.d.ts +5 -0
- 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 = {}));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Fee, PairIndex } from "../types";
|
|
2
|
-
export declare const getClosingFee: (posDai: number, leverage: number, pairIndex: PairIndex, pairFee: Fee | undefined) => number;
|
|
2
|
+
export declare const getClosingFee: (posDai: number, leverage: number, pairIndex: PairIndex, pairFee: Fee | undefined, collateralPriceUsd?: number) => number;
|
|
3
3
|
export * from "./borrowing";
|
|
4
4
|
export * from "./tiers";
|
package/lib/trade/fees/index.js
CHANGED
|
@@ -15,15 +15,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.getClosingFee = void 0;
|
|
18
|
-
const getClosingFee = (posDai, leverage, pairIndex, pairFee) => {
|
|
18
|
+
const getClosingFee = (posDai, leverage, pairIndex, pairFee, collateralPriceUsd) => {
|
|
19
19
|
if (posDai === undefined ||
|
|
20
20
|
leverage === undefined ||
|
|
21
21
|
pairIndex === undefined ||
|
|
22
22
|
pairFee === undefined) {
|
|
23
23
|
return 0;
|
|
24
24
|
}
|
|
25
|
-
const { closeFeeP, triggerOrderFeeP } = pairFee;
|
|
26
|
-
return (closeFeeP + triggerOrderFeeP) *
|
|
25
|
+
const { closeFeeP, triggerOrderFeeP, minPositionSizeUsd } = pairFee;
|
|
26
|
+
return ((closeFeeP + triggerOrderFeeP) *
|
|
27
|
+
Math.max(collateralPriceUsd && collateralPriceUsd > 0
|
|
28
|
+
? minPositionSizeUsd / collateralPriceUsd
|
|
29
|
+
: 0, posDai * leverage));
|
|
27
30
|
};
|
|
28
31
|
exports.getClosingFee = getClosingFee;
|
|
29
32
|
__exportStar(require("./borrowing"), exports);
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
2
|
import { Fee, LiquidationParams, Trade } from "./types";
|
|
3
|
+
import { ContractsVersion } from "src/contracts/types";
|
|
3
4
|
export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
|
|
4
5
|
liquidationParams: LiquidationParams | undefined;
|
|
5
6
|
pairSpreadP: number | undefined;
|
|
7
|
+
collateralPriceUsd: number | undefined;
|
|
8
|
+
contractsVersion: ContractsVersion | undefined;
|
|
6
9
|
};
|
|
7
10
|
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;
|
|
11
|
+
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined, contractsVersion: ContractsVersion | undefined) => number;
|
package/lib/trade/liquidation.js
CHANGED
|
@@ -3,16 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
|
|
4
4
|
const fees_1 = require("./fees");
|
|
5
5
|
const spread_1 = require("./spread");
|
|
6
|
+
const types_1 = require("src/contracts/types");
|
|
6
7
|
const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
7
8
|
var _a;
|
|
8
|
-
const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee);
|
|
9
|
+
const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd);
|
|
9
10
|
const borrowingFee = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
10
|
-
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
|
|
11
|
+
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage, context.contractsVersion);
|
|
11
12
|
let liqPriceDistance = (trade.openPrice *
|
|
12
13
|
(trade.collateralAmount * liqThresholdP - (borrowingFee + closingFee))) /
|
|
13
14
|
trade.collateralAmount /
|
|
14
15
|
trade.leverage;
|
|
15
|
-
if ((
|
|
16
|
+
if ((context === null || context === void 0 ? void 0 : context.contractsVersion) === types_1.ContractsVersion.V9_2 &&
|
|
17
|
+
((_a = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
16
18
|
context.liquidationParams.maxLiqSpreadP > 0) {
|
|
17
19
|
const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams);
|
|
18
20
|
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
@@ -22,9 +24,10 @@ const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
|
22
24
|
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
23
25
|
};
|
|
24
26
|
exports.getLiquidationPrice = getLiquidationPrice;
|
|
25
|
-
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
27
|
+
const getLiqPnlThresholdP = (liquidationParams, leverage, contractsVersion) => {
|
|
26
28
|
if (liquidationParams === undefined ||
|
|
27
29
|
leverage === undefined ||
|
|
30
|
+
contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
|
|
28
31
|
liquidationParams.maxLiqSpreadP === 0 ||
|
|
29
32
|
liquidationParams.startLiqThresholdP === 0 ||
|
|
30
33
|
liquidationParams.endLiqThresholdP === 0 ||
|
package/lib/trade/pnl.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { GetBorrowingFeeContext } from "./fees";
|
|
2
2
|
import { Fee, LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
|
|
3
|
+
import { ContractsVersion } from "src/contracts/types";
|
|
3
4
|
export type GetPnlContext = GetBorrowingFeeContext & {
|
|
4
5
|
fee: Fee | undefined;
|
|
5
6
|
maxGainP: number | undefined;
|
|
7
|
+
collateralPriceUsd: number | undefined;
|
|
8
|
+
contractsVersion: ContractsVersion | undefined;
|
|
6
9
|
};
|
|
7
10
|
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
|
@@ -21,11 +21,12 @@ const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useF
|
|
|
21
21
|
let pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
22
22
|
// Can be liquidated
|
|
23
23
|
if (pnlPercentage <=
|
|
24
|
-
(0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) *
|
|
24
|
+
(0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage, context.contractsVersion) *
|
|
25
|
+
-100) {
|
|
25
26
|
pnlPercentage = -100;
|
|
26
27
|
}
|
|
27
28
|
else {
|
|
28
|
-
pnlCollat -= (0, fees_1.getClosingFee)(posCollat, trade.leverage, trade.pairIndex, fee);
|
|
29
|
+
pnlCollat -= (0, fees_1.getClosingFee)(posCollat, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd);
|
|
29
30
|
pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
30
31
|
}
|
|
31
32
|
pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
|
package/lib/trade/spread.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth } from "./types";
|
|
2
|
+
import { ContractsVersion } from "src/contracts/types";
|
|
2
3
|
export type SpreadContext = {
|
|
3
4
|
isOpen?: boolean;
|
|
4
5
|
isPnlPositive?: boolean;
|
|
@@ -7,6 +8,7 @@ export type SpreadContext = {
|
|
|
7
8
|
createdBlock?: number;
|
|
8
9
|
liquidationParams?: LiquidationParams | undefined;
|
|
9
10
|
currentBlock: number | undefined;
|
|
11
|
+
contractsVersion: ContractsVersion | undefined;
|
|
10
12
|
};
|
|
11
13
|
export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
|
|
12
14
|
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;
|
package/lib/trade/spread.js
CHANGED
|
@@ -3,8 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getSpreadP = exports.getSpreadWithPriceImpactP = exports.getProtectionCloseFactor = void 0;
|
|
4
4
|
const oiWindows_1 = require("./oiWindows");
|
|
5
5
|
const constants_1 = require("../constants");
|
|
6
|
+
const types_1 = require("src/contracts/types");
|
|
6
7
|
const getProtectionCloseFactor = (spreadCtx) => {
|
|
7
8
|
if (spreadCtx === undefined ||
|
|
9
|
+
spreadCtx.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
|
|
8
10
|
spreadCtx.isOpen === undefined ||
|
|
9
11
|
spreadCtx.isPnlPositive === undefined ||
|
|
10
12
|
spreadCtx.protectionCloseFactor === undefined ||
|
|
@@ -28,8 +30,7 @@ const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairD
|
|
|
28
30
|
}
|
|
29
31
|
// No spread or price impact when closing pre-v9.2 trades
|
|
30
32
|
if ((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
|
|
31
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.
|
|
32
|
-
spreadCtx.liquidationParams.maxLiqSpreadP === 0) {
|
|
33
|
+
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) {
|
|
33
34
|
return 0;
|
|
34
35
|
}
|
|
35
36
|
const onePercentDepth = buy
|
package/lib/trade/types.d.ts
CHANGED
|
@@ -167,6 +167,11 @@ export type TraderFeeTiers = {
|
|
|
167
167
|
lastDayUpdatedPoints: number;
|
|
168
168
|
expiredPoints: number[];
|
|
169
169
|
};
|
|
170
|
+
export type PairFactor = {
|
|
171
|
+
cumulativeFactor: number;
|
|
172
|
+
protectionCloseFactor: number;
|
|
173
|
+
protectionCloseFactorBlocks: number;
|
|
174
|
+
};
|
|
170
175
|
export declare enum PendingOrderType {
|
|
171
176
|
MARKET_OPEN = 0,
|
|
172
177
|
MARKET_CLOSE = 1,
|