@gainsnetwork/sdk 0.0.0-v10.rc15 → 0.0.0-v10.rc17
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/backend/tradingVariables/converter.js +1 -1
- package/lib/constants.d.ts +10 -0
- package/lib/constants.js +13 -2
- package/lib/contracts/addresses.json +20 -0
- package/lib/contracts/index.js +3 -1
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/pairs.js +10 -0
- package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
- package/lib/trade/effectiveLeverage/builder.js +30 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.d.ts +23 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.js +64 -0
- package/lib/trade/effectiveLeverage/index.d.ts +3 -0
- package/lib/trade/effectiveLeverage/index.js +22 -0
- package/lib/trade/effectiveLeverage/types.d.ts +33 -0
- package/lib/trade/effectiveLeverage/types.js +2 -0
- package/lib/trade/fees/borrowing/index.d.ts +0 -1
- package/lib/trade/fees/borrowing/index.js +0 -1
- package/lib/trade/index.d.ts +1 -0
- package/lib/trade/index.js +1 -0
- package/lib/trade/priceImpact/close/index.js +5 -1
- package/lib/trade/priceImpact/cumulVol/index.js +0 -13
- package/lib/trade/types.d.ts +11 -1
- package/lib/trade/types.js +10 -0
- package/package.json +1 -1
|
@@ -187,7 +187,7 @@ const convertTrade = (trade, collaterals) => {
|
|
|
187
187
|
tp: parseFloat(trade.tp) / 1e10,
|
|
188
188
|
isCounterTrade: trade.isCounterTrade,
|
|
189
189
|
positionSizeToken: trade.positionSizeToken
|
|
190
|
-
? parseFloat(trade.positionSizeToken) /
|
|
190
|
+
? parseFloat(trade.positionSizeToken) / 1e18
|
|
191
191
|
: undefined,
|
|
192
192
|
};
|
|
193
193
|
};
|
package/lib/constants.d.ts
CHANGED
|
@@ -386,6 +386,16 @@ export declare const pairs: {
|
|
|
386
386
|
"BVIV/USD": string;
|
|
387
387
|
"EVIV/USD": string;
|
|
388
388
|
"CRCL/USD": string;
|
|
389
|
+
"RESOLV/USD": string;
|
|
390
|
+
"SQD/USD": string;
|
|
391
|
+
"TAIKO/USD": string;
|
|
392
|
+
"HOME/USD": string;
|
|
393
|
+
"B/USD": string;
|
|
394
|
+
"HUMA/USD": string;
|
|
395
|
+
"SBET/USD": string;
|
|
396
|
+
"PLTR/USD": string;
|
|
397
|
+
"BIDU/USD": string;
|
|
398
|
+
"ROKU/USD": string;
|
|
389
399
|
};
|
|
390
400
|
export declare const syntheticPairs: Set<string>;
|
|
391
401
|
export declare const parentToSyntheticPairMap: Map<string, string>;
|
package/lib/constants.js
CHANGED
|
@@ -394,6 +394,16 @@ exports.pairs = {
|
|
|
394
394
|
"BVIV/USD": CRYPTO,
|
|
395
395
|
"EVIV/USD": CRYPTO,
|
|
396
396
|
"CRCL/USD": STOCKS,
|
|
397
|
+
"RESOLV/USD": CRYPTO,
|
|
398
|
+
"SQD/USD": CRYPTO,
|
|
399
|
+
"TAIKO/USD": CRYPTO,
|
|
400
|
+
"HOME/USD": CRYPTO,
|
|
401
|
+
"B/USD": CRYPTO,
|
|
402
|
+
"HUMA/USD": CRYPTO,
|
|
403
|
+
"SBET/USD": STOCKS,
|
|
404
|
+
"PLTR/USD": STOCKS,
|
|
405
|
+
"BIDU/USD": STOCKS,
|
|
406
|
+
"ROKU/USD": STOCKS,
|
|
397
407
|
};
|
|
398
408
|
exports.syntheticPairs = new Set([
|
|
399
409
|
"BTCDEGEN/USD",
|
|
@@ -442,11 +452,12 @@ exports.stockSplits = {
|
|
|
442
452
|
};
|
|
443
453
|
exports.delistedPairIxs = new Set([
|
|
444
454
|
4, 6, 15, 24, 25, 27, 28, 30, 31, 36, 48, 51, 52, 53, 54, 56, 59, 60, 61, 63,
|
|
445
|
-
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
|
|
446
|
-
|
|
455
|
+
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 95, 96, 97, 98, 99,
|
|
456
|
+
101, 106, 111, 113, 114, 116, 118, 120, 122, 123, 125, 127, 130, 147, 152,
|
|
447
457
|
160, 163, 170, 179, 182, 183, 187, 188, 189, 190, 208, 209, 225, 229, 230,
|
|
448
458
|
231, 238, 239, 241, 250, 253, 254, 258, 270, 275, 276, 278, 279, 282, 285,
|
|
449
459
|
290, 294, 296, 305, 330, 349, 352, 353, 354, 355, 357, 365, 366, 384, 385,
|
|
460
|
+
393, 394, 395, 396,
|
|
450
461
|
]);
|
|
451
462
|
exports.delistedGroupsIxs = new Set([]);
|
|
452
463
|
exports.DEFAULT_PROTECTION_CLOSE_FACTOR = 1;
|
|
@@ -22,6 +22,10 @@
|
|
|
22
22
|
"GNS": {
|
|
23
23
|
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
24
24
|
"gToken": "0x0000000000000000000000000000000000000000"
|
|
25
|
+
},
|
|
26
|
+
"BTCUSD": {
|
|
27
|
+
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
28
|
+
"gToken": "0x0000000000000000000000000000000000000000"
|
|
25
29
|
}
|
|
26
30
|
},
|
|
27
31
|
"42161": {
|
|
@@ -47,6 +51,10 @@
|
|
|
47
51
|
"GNS": {
|
|
48
52
|
"gTokenOpenPnlFeed": "0x4ca1638754Be8060E544Aca4A4F43702Be30E0D1",
|
|
49
53
|
"gToken": "0x4BeeF1113F968326905224D2Ca272f3032A9a9F4"
|
|
54
|
+
},
|
|
55
|
+
"BTCUSD": {
|
|
56
|
+
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
57
|
+
"gToken": "0x0000000000000000000000000000000000000000"
|
|
50
58
|
}
|
|
51
59
|
},
|
|
52
60
|
"421614": {
|
|
@@ -72,6 +80,10 @@
|
|
|
72
80
|
"GNS": {
|
|
73
81
|
"gTokenOpenPnlFeed": "0x68F8D4ec2EF23a15e61e1642E730b6f69fB9A5De",
|
|
74
82
|
"gToken": "0x5ed4bEA869300DB39bE2a92a8B42e53453742a43"
|
|
83
|
+
},
|
|
84
|
+
"BTCUSD": {
|
|
85
|
+
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
86
|
+
"gToken": "0x0000000000000000000000000000000000000000"
|
|
75
87
|
}
|
|
76
88
|
},
|
|
77
89
|
"8453": {
|
|
@@ -97,6 +109,10 @@
|
|
|
97
109
|
"GNS": {
|
|
98
110
|
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
99
111
|
"gToken": "0x0000000000000000000000000000000000000000"
|
|
112
|
+
},
|
|
113
|
+
"BTCUSD": {
|
|
114
|
+
"gTokenOpenPnlFeed": "0xA22F7b92d81Dd74bE5ca5758DF3FccF0A4CB19cc",
|
|
115
|
+
"gToken": "0xB7Cb7cB7C3cD96e251c9bF8800B9631134BbAdc6"
|
|
100
116
|
}
|
|
101
117
|
},
|
|
102
118
|
"33139": {
|
|
@@ -122,6 +138,10 @@
|
|
|
122
138
|
"GNS": {
|
|
123
139
|
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
124
140
|
"gToken": "0x0000000000000000000000000000000000000000"
|
|
141
|
+
},
|
|
142
|
+
"BTCUSD": {
|
|
143
|
+
"gTokenOpenPnlFeed": "0x0000000000000000000000000000000000000000",
|
|
144
|
+
"gToken": "0x0000000000000000000000000000000000000000"
|
|
125
145
|
}
|
|
126
146
|
}
|
|
127
147
|
}
|
package/lib/contracts/index.js
CHANGED
|
@@ -63,6 +63,7 @@ exports.COLLATERAL_TO_CHAIN_COLLATERAL_INDEX = {
|
|
|
63
63
|
},
|
|
64
64
|
[types_1.ChainId.BASE]: {
|
|
65
65
|
[types_1.CollateralTypes.USDC]: 1,
|
|
66
|
+
[types_1.CollateralTypes.BTCUSD]: 2,
|
|
66
67
|
},
|
|
67
68
|
[types_1.ChainId.APECHAIN]: {
|
|
68
69
|
[types_1.CollateralTypes.APE]: 1,
|
|
@@ -75,7 +76,8 @@ exports.COLLATERAL_TO_COLLATERAL_INDEX = {
|
|
|
75
76
|
[types_1.CollateralTypes.USDC]: 3,
|
|
76
77
|
[types_1.CollateralTypes.ARB]: 0,
|
|
77
78
|
[types_1.CollateralTypes.APE]: 0,
|
|
78
|
-
[types_1.CollateralTypes.GNS]: 0,
|
|
79
|
+
[types_1.CollateralTypes.GNS]: 0,
|
|
80
|
+
[types_1.CollateralTypes.BTCUSD]: 0,
|
|
79
81
|
};
|
|
80
82
|
__exportStar(require("./utils"), exports);
|
|
81
83
|
__exportStar(require("./addresses"), exports);
|
|
@@ -9,6 +9,7 @@ var CollateralTypes;
|
|
|
9
9
|
CollateralTypes["USDC"] = "USDC";
|
|
10
10
|
CollateralTypes["APE"] = "APE";
|
|
11
11
|
CollateralTypes["GNS"] = "GNS";
|
|
12
|
+
CollateralTypes["BTCUSD"] = "BTCUSD";
|
|
12
13
|
})(CollateralTypes = exports.CollateralTypes || (exports.CollateralTypes = {}));
|
|
13
14
|
var ContractsVersion;
|
|
14
15
|
(function (ContractsVersion) {
|
|
@@ -492,4 +492,14 @@ const PAIR_INDEX_TO_DESCRIPTION = {
|
|
|
492
492
|
[types_1.PairIndex.BVIVUSD]: "Bitcoin Volmex Implied Volatility 30 Day Index to US Dollar",
|
|
493
493
|
[types_1.PairIndex.EVIVUSD]: "Ethereum Volmex Implied Volatility 30 Day Index to US Dollar",
|
|
494
494
|
[types_1.PairIndex.CRCLUSD]: "Circle Internet Group to US Dollar",
|
|
495
|
+
[types_1.PairIndex.RESOLVUSD]: "Resolv to US Dollar",
|
|
496
|
+
[types_1.PairIndex.SQDUSD]: "Subsquid to US Dollar",
|
|
497
|
+
[types_1.PairIndex.TAIKOUSD]: "Taiko to US Dollar",
|
|
498
|
+
[types_1.PairIndex.HOMEUSD]: "Defi App to US Dollar",
|
|
499
|
+
[types_1.PairIndex.BUSD]: "BUILDon to US Dollar",
|
|
500
|
+
[types_1.PairIndex.HUMAUSD]: "Huma Finance to US Dollar",
|
|
501
|
+
[types_1.PairIndex.SBETUSD]: "Sharplink Gaming Inc to US Dollar",
|
|
502
|
+
[types_1.PairIndex.PLTRUSD]: "Palantir Technologies to US Dollar",
|
|
503
|
+
[types_1.PairIndex.BIDUUSD]: "Baidu to US Dollar",
|
|
504
|
+
[types_1.PairIndex.ROKUUSD]: "Roku to US Dollar",
|
|
495
505
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GlobalTradingVariablesType } from "../../backend/tradingVariables/types";
|
|
2
|
+
import { TradeInfo } from "..";
|
|
3
|
+
import { TradeEffectiveLeverageContext } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* @dev Builds a complete context for effective leverage calculations
|
|
6
|
+
* @dev Uses the closing price impact context builder as a sub-context
|
|
7
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
8
|
+
* @param collateralIndex The collateral index (1-based)
|
|
9
|
+
* @param pairIndex The pair index
|
|
10
|
+
* @param tradeInfo Trade information including createdBlock
|
|
11
|
+
* @param additionalParams Additional parameters for price impact calculations
|
|
12
|
+
* @returns Complete context ready for getTradeNewEffectiveLeverage
|
|
13
|
+
*/
|
|
14
|
+
export declare const buildTradeEffectiveLeverageContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, tradeInfo: TradeInfo, additionalParams: {
|
|
15
|
+
currentBlock: number;
|
|
16
|
+
contractsVersion?: number;
|
|
17
|
+
isPnlPositive?: boolean;
|
|
18
|
+
userPriceImpact?: {
|
|
19
|
+
cumulVolPriceImpactMultiplier: number;
|
|
20
|
+
fixedSpreadP: number;
|
|
21
|
+
};
|
|
22
|
+
protectionCloseFactorWhitelist?: boolean;
|
|
23
|
+
}) => TradeEffectiveLeverageContext | undefined;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildTradeEffectiveLeverageContext = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
/**
|
|
6
|
+
* @dev Builds a complete context for effective leverage calculations
|
|
7
|
+
* @dev Uses the closing price impact context builder as a sub-context
|
|
8
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
9
|
+
* @param collateralIndex The collateral index (1-based)
|
|
10
|
+
* @param pairIndex The pair index
|
|
11
|
+
* @param tradeInfo Trade information including createdBlock
|
|
12
|
+
* @param additionalParams Additional parameters for price impact calculations
|
|
13
|
+
* @returns Complete context ready for getTradeNewEffectiveLeverage
|
|
14
|
+
*/
|
|
15
|
+
const buildTradeEffectiveLeverageContext = (globalTradingVariables, collateralIndex, pairIndex, tradeInfo, additionalParams) => {
|
|
16
|
+
var _a;
|
|
17
|
+
// Build the closing price impact context which we'll use for PnL calculations
|
|
18
|
+
const closingPriceImpactContext = (0, __1.buildTradeClosingPriceImpactContext)(globalTradingVariables, collateralIndex, pairIndex, tradeInfo, additionalParams);
|
|
19
|
+
if (!closingPriceImpactContext) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
// Extract base spread from pairs data
|
|
23
|
+
const pairs = globalTradingVariables.pairs;
|
|
24
|
+
const baseSpreadP = ((_a = pairs === null || pairs === void 0 ? void 0 : pairs[pairIndex]) === null || _a === void 0 ? void 0 : _a.spreadP) || 0;
|
|
25
|
+
return {
|
|
26
|
+
closingPriceImpactContext,
|
|
27
|
+
baseSpreadP,
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
exports.buildTradeEffectiveLeverageContext = buildTradeEffectiveLeverageContext;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Trade effective leverage calculations
|
|
3
|
+
* @dev Mirrors contract's TradingCommonUtils.getTradeNewEffectiveLeverage
|
|
4
|
+
*/
|
|
5
|
+
import { TradeEffectiveLeverageInput, TradeEffectiveLeverageContext, TradeEffectiveLeverageResult } from "./types";
|
|
6
|
+
export type { TradeEffectiveLeverageInput, TradeEffectiveLeverageContext, TradeEffectiveLeverageResult, };
|
|
7
|
+
export { buildTradeEffectiveLeverageContext } from "./builder";
|
|
8
|
+
/**
|
|
9
|
+
* @dev Calculates the effective leverage of a trade accounting for unrealized PnL
|
|
10
|
+
* @dev Effective leverage increases when PnL is negative and decreases when positive
|
|
11
|
+
* @dev Mirrors contract's getTradeNewEffectiveLeverage function
|
|
12
|
+
* @param input Trade parameters including new position values
|
|
13
|
+
* @param context Combined context for calculations
|
|
14
|
+
* @returns Effective leverage and related values
|
|
15
|
+
*/
|
|
16
|
+
export declare const getTradeNewEffectiveLeverage: (input: TradeEffectiveLeverageInput, context: TradeEffectiveLeverageContext) => TradeEffectiveLeverageResult;
|
|
17
|
+
/**
|
|
18
|
+
* @dev Simplified version for existing positions (no opening fees)
|
|
19
|
+
* @param input Trade parameters
|
|
20
|
+
* @param context Combined context
|
|
21
|
+
* @returns Effective leverage and related values
|
|
22
|
+
*/
|
|
23
|
+
export declare const getTradeEffectiveLeverage: (input: Omit<TradeEffectiveLeverageInput, "openingFeesCollateral">, context: TradeEffectiveLeverageContext) => TradeEffectiveLeverageResult;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTradeEffectiveLeverage = exports.getTradeNewEffectiveLeverage = exports.buildTradeEffectiveLeverageContext = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
var builder_1 = require("./builder");
|
|
6
|
+
Object.defineProperty(exports, "buildTradeEffectiveLeverageContext", { enumerable: true, get: function () { return builder_1.buildTradeEffectiveLeverageContext; } });
|
|
7
|
+
/**
|
|
8
|
+
* @dev Calculates the effective leverage of a trade accounting for unrealized PnL
|
|
9
|
+
* @dev Effective leverage increases when PnL is negative and decreases when positive
|
|
10
|
+
* @dev Mirrors contract's getTradeNewEffectiveLeverage function
|
|
11
|
+
* @param input Trade parameters including new position values
|
|
12
|
+
* @param context Combined context for calculations
|
|
13
|
+
* @returns Effective leverage and related values
|
|
14
|
+
*/
|
|
15
|
+
const getTradeNewEffectiveLeverage = (input, context) => {
|
|
16
|
+
const { trade, newOpenPrice, newCollateralAmount, newLeverage, currentPairPrice, openingFeesCollateral, } = input;
|
|
17
|
+
const { closingPriceImpactContext } = context;
|
|
18
|
+
// Calculate new position size
|
|
19
|
+
const newPositionSize = newCollateralAmount * newLeverage;
|
|
20
|
+
// Calculate price impact for closing at current price
|
|
21
|
+
const closingPriceImpact = (0, __1.getTradeClosingPriceImpact)({
|
|
22
|
+
trade: Object.assign(Object.assign({}, trade), { openPrice: newOpenPrice, collateralAmount: newCollateralAmount, leverage: newLeverage }),
|
|
23
|
+
oraclePrice: currentPairPrice,
|
|
24
|
+
positionSizeCollateral: newPositionSize,
|
|
25
|
+
currentPairPrice: currentPairPrice,
|
|
26
|
+
useCumulativeVolPriceImpact: true,
|
|
27
|
+
collateralIndex: trade.collateralIndex,
|
|
28
|
+
pairIndex: trade.pairIndex,
|
|
29
|
+
pairSpreadP: context.baseSpreadP,
|
|
30
|
+
contractsVersion: closingPriceImpactContext.tradeInfo.contractsVersion,
|
|
31
|
+
}, closingPriceImpactContext);
|
|
32
|
+
// Calculate unrealized PnL
|
|
33
|
+
// For longs: (exitPrice - entryPrice) * positionSizeToken
|
|
34
|
+
// For shorts: (entryPrice - exitPrice) * positionSizeToken
|
|
35
|
+
const priceDiff = trade.long
|
|
36
|
+
? closingPriceImpact.priceAfterImpact - newOpenPrice
|
|
37
|
+
: newOpenPrice - closingPriceImpact.priceAfterImpact;
|
|
38
|
+
const unrealizedPnl = priceDiff * closingPriceImpact.positionSizeToken;
|
|
39
|
+
// Calculate effective collateral (collateral + PnL - fees)
|
|
40
|
+
// Note: fees are subtracted because they reduce the effective collateral
|
|
41
|
+
const effectiveCollateral = newCollateralAmount + unrealizedPnl - openingFeesCollateral;
|
|
42
|
+
// Calculate effective leverage
|
|
43
|
+
// If effective collateral is <= 0, leverage is effectively infinite
|
|
44
|
+
const effectiveLeverage = effectiveCollateral > 0
|
|
45
|
+
? newPositionSize / effectiveCollateral
|
|
46
|
+
: Number.MAX_SAFE_INTEGER;
|
|
47
|
+
return {
|
|
48
|
+
effectiveLeverage,
|
|
49
|
+
unrealizedPnl,
|
|
50
|
+
effectiveCollateral,
|
|
51
|
+
positionSize: newPositionSize,
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
exports.getTradeNewEffectiveLeverage = getTradeNewEffectiveLeverage;
|
|
55
|
+
/**
|
|
56
|
+
* @dev Simplified version for existing positions (no opening fees)
|
|
57
|
+
* @param input Trade parameters
|
|
58
|
+
* @param context Combined context
|
|
59
|
+
* @returns Effective leverage and related values
|
|
60
|
+
*/
|
|
61
|
+
const getTradeEffectiveLeverage = (input, context) => {
|
|
62
|
+
return (0, exports.getTradeNewEffectiveLeverage)(Object.assign(Object.assign({}, input), { openingFeesCollateral: 0 }), context);
|
|
63
|
+
};
|
|
64
|
+
exports.getTradeEffectiveLeverage = getTradeEffectiveLeverage;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.getTradeEffectiveLeverage = exports.getTradeNewEffectiveLeverage = void 0;
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
19
|
+
__exportStar(require("./builder"), exports);
|
|
20
|
+
var getTradeNewEffectiveLeverage_1 = require("./getTradeNewEffectiveLeverage");
|
|
21
|
+
Object.defineProperty(exports, "getTradeNewEffectiveLeverage", { enumerable: true, get: function () { return getTradeNewEffectiveLeverage_1.getTradeNewEffectiveLeverage; } });
|
|
22
|
+
Object.defineProperty(exports, "getTradeEffectiveLeverage", { enumerable: true, get: function () { return getTradeNewEffectiveLeverage_1.getTradeEffectiveLeverage; } });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Types for trade effective leverage calculations
|
|
3
|
+
*/
|
|
4
|
+
import { Trade, TradeClosingPriceImpactContext } from "..";
|
|
5
|
+
/**
|
|
6
|
+
* @dev Input parameters for effective leverage calculation
|
|
7
|
+
* @dev Mirrors contract's parameters for getTradeNewEffectiveLeverage
|
|
8
|
+
*/
|
|
9
|
+
export type TradeEffectiveLeverageInput = {
|
|
10
|
+
trade: Trade;
|
|
11
|
+
newOpenPrice: number;
|
|
12
|
+
newCollateralAmount: number;
|
|
13
|
+
newLeverage: number;
|
|
14
|
+
currentPairPrice: number;
|
|
15
|
+
openingFeesCollateral: number;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* @dev Context for effective leverage calculation
|
|
19
|
+
* Includes closing price impact context for PnL calculations
|
|
20
|
+
*/
|
|
21
|
+
export type TradeEffectiveLeverageContext = {
|
|
22
|
+
closingPriceImpactContext: TradeClosingPriceImpactContext;
|
|
23
|
+
baseSpreadP: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* @dev Result of effective leverage calculation
|
|
27
|
+
*/
|
|
28
|
+
export type TradeEffectiveLeverageResult = {
|
|
29
|
+
effectiveLeverage: number;
|
|
30
|
+
unrealizedPnl: number;
|
|
31
|
+
effectiveCollateral: number;
|
|
32
|
+
positionSize: number;
|
|
33
|
+
};
|
|
@@ -19,7 +19,6 @@ export type GetBorrowingFeeContext = {
|
|
|
19
19
|
*/
|
|
20
20
|
export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex | undefined, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
|
|
21
21
|
/**
|
|
22
|
-
* @deprecated Use withinMaxGroupOiDynamic from @gains-network/sdk/markets/oi instead
|
|
23
22
|
* @dev This function uses static OI which doesn't reflect current market values
|
|
24
23
|
* @dev The v10 contracts use dynamic OI (beforeV10 + afterV10Token * currentPrice)
|
|
25
24
|
*/
|
|
@@ -78,7 +78,6 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
78
78
|
};
|
|
79
79
|
exports.getBorrowingFee = getBorrowingFee;
|
|
80
80
|
/**
|
|
81
|
-
* @deprecated Use withinMaxGroupOiDynamic from @gains-network/sdk/markets/oi instead
|
|
82
81
|
* @dev This function uses static OI which doesn't reflect current market values
|
|
83
82
|
* @dev The v10 contracts use dynamic OI (beforeV10 + afterV10Token * currentPrice)
|
|
84
83
|
*/
|
package/lib/trade/index.d.ts
CHANGED
package/lib/trade/index.js
CHANGED
|
@@ -53,6 +53,11 @@ const getTradeClosingPriceImpact = (input, context) => {
|
|
|
53
53
|
const positionSizeToken = input.trade.positionSizeToken
|
|
54
54
|
? calculateClosingPositionSizeToken(input.positionSizeCollateral, input.trade.positionSizeToken, input.trade.collateralAmount, input.trade.leverage)
|
|
55
55
|
: 0;
|
|
56
|
+
console.log("positionSizeToken", positionSizeToken);
|
|
57
|
+
console.log("input.positionSizeCollateral", input.positionSizeCollateral);
|
|
58
|
+
console.log("input.trade.positionSizeToken", input.trade.positionSizeToken);
|
|
59
|
+
console.log("input.trade.collateralAmount", input.trade.collateralAmount);
|
|
60
|
+
console.log("input.trade.leverage", input.trade.leverage);
|
|
56
61
|
// Calculate fixed spread (reversed for closing)
|
|
57
62
|
const fixedSpreadP = (0, cumulVol_1.getFixedSpreadP)(input.pairSpreadP, input.trade.long, false // closing
|
|
58
63
|
);
|
|
@@ -74,7 +79,6 @@ const getTradeClosingPriceImpact = (input, context) => {
|
|
|
74
79
|
);
|
|
75
80
|
// Determine actual PnL from the calculated percentage
|
|
76
81
|
const isPnlPositive = pnlPercent > 0;
|
|
77
|
-
console.log("isPnlPositive", isPnlPositive);
|
|
78
82
|
// Second pass: Recalculate with actual PnL if positive
|
|
79
83
|
if (isPnlPositive) {
|
|
80
84
|
cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)(input.trade.user, input.pairIndex, input.trade.long, positionSizeUsd, true, // Positive PnL
|
|
@@ -43,13 +43,6 @@ const isProtectionCloseFactorActive = (context) => {
|
|
|
43
43
|
context.protectionCloseFactor === undefined) {
|
|
44
44
|
return undefined;
|
|
45
45
|
}
|
|
46
|
-
console.log("context.isPnlPositive", context.isPnlPositive);
|
|
47
|
-
console.log("context.isOpen", context.isOpen);
|
|
48
|
-
console.log("context.protectionCloseFactor", context.protectionCloseFactor);
|
|
49
|
-
console.log("context.currentBlock", context.currentBlock);
|
|
50
|
-
console.log("context.createdBlock", context.createdBlock);
|
|
51
|
-
console.log("context.protectionCloseFactorBlocks", context.protectionCloseFactorBlocks);
|
|
52
|
-
console.log("context.protectionCloseFactorWhitelist", context.protectionCloseFactorWhitelist);
|
|
53
46
|
return (context.isPnlPositive === true &&
|
|
54
47
|
context.isOpen === false &&
|
|
55
48
|
context.protectionCloseFactor > 0 &&
|
|
@@ -135,12 +128,6 @@ const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterest
|
|
|
135
128
|
onePercentDepth /
|
|
136
129
|
(0, exports.getLegacyFactor)(updatedContext)) *
|
|
137
130
|
(0, exports.getProtectionCloseFactor)(updatedContext);
|
|
138
|
-
console.log("signedActiveOi", signedActiveOi);
|
|
139
|
-
console.log("getCumulativeFactor", (0, exports.getCumulativeFactor)(updatedContext));
|
|
140
|
-
console.log("signedTradeOi", signedTradeOi);
|
|
141
|
-
console.log("onePercentDepth", onePercentDepth);
|
|
142
|
-
console.log("getLegacyFactor", (0, exports.getLegacyFactor)(updatedContext));
|
|
143
|
-
console.log("getProtectionCloseFactor", (0, exports.getProtectionCloseFactor)(updatedContext));
|
|
144
131
|
return finalPriceImpactP;
|
|
145
132
|
};
|
|
146
133
|
exports.getTradeCumulVolPriceImpactP = getTradeCumulVolPriceImpactP;
|
package/lib/trade/types.d.ts
CHANGED
|
@@ -629,5 +629,15 @@ export declare enum PairIndex {
|
|
|
629
629
|
LPTUSD = 383,
|
|
630
630
|
BVIVUSD = 384,
|
|
631
631
|
EVIVUSD = 385,
|
|
632
|
-
CRCLUSD = 386
|
|
632
|
+
CRCLUSD = 386,
|
|
633
|
+
RESOLVUSD = 387,
|
|
634
|
+
SQDUSD = 388,
|
|
635
|
+
TAIKOUSD = 389,
|
|
636
|
+
HOMEUSD = 390,
|
|
637
|
+
BUSD = 391,
|
|
638
|
+
HUMAUSD = 392,
|
|
639
|
+
SBETUSD = 393,
|
|
640
|
+
PLTRUSD = 394,
|
|
641
|
+
BIDUUSD = 395,
|
|
642
|
+
ROKUUSD = 396
|
|
633
643
|
}
|
package/lib/trade/types.js
CHANGED
|
@@ -416,4 +416,14 @@ var PairIndex;
|
|
|
416
416
|
PairIndex[PairIndex["BVIVUSD"] = 384] = "BVIVUSD";
|
|
417
417
|
PairIndex[PairIndex["EVIVUSD"] = 385] = "EVIVUSD";
|
|
418
418
|
PairIndex[PairIndex["CRCLUSD"] = 386] = "CRCLUSD";
|
|
419
|
+
PairIndex[PairIndex["RESOLVUSD"] = 387] = "RESOLVUSD";
|
|
420
|
+
PairIndex[PairIndex["SQDUSD"] = 388] = "SQDUSD";
|
|
421
|
+
PairIndex[PairIndex["TAIKOUSD"] = 389] = "TAIKOUSD";
|
|
422
|
+
PairIndex[PairIndex["HOMEUSD"] = 390] = "HOMEUSD";
|
|
423
|
+
PairIndex[PairIndex["BUSD"] = 391] = "BUSD";
|
|
424
|
+
PairIndex[PairIndex["HUMAUSD"] = 392] = "HUMAUSD";
|
|
425
|
+
PairIndex[PairIndex["SBETUSD"] = 393] = "SBETUSD";
|
|
426
|
+
PairIndex[PairIndex["PLTRUSD"] = 394] = "PLTRUSD";
|
|
427
|
+
PairIndex[PairIndex["BIDUUSD"] = 395] = "BIDUUSD";
|
|
428
|
+
PairIndex[PairIndex["ROKUUSD"] = 396] = "ROKUUSD";
|
|
419
429
|
})(PairIndex = exports.PairIndex || (exports.PairIndex = {}));
|