@gainsnetwork/sdk 0.0.0-v10.rc2 → 0.0.0-v10.rc21
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/globalTrades/index.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +3 -0
- package/lib/backend/index.js +28 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +318 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +34 -0
- package/lib/backend/tradingVariables/converter.js +338 -0
- package/lib/backend/tradingVariables/index.d.ts +5 -0
- package/lib/backend/tradingVariables/index.js +96 -0
- package/lib/backend/tradingVariables/types.d.ts +113 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.d.ts +20 -0
- package/lib/constants.js +24 -3
- package/lib/contracts/addresses.json +20 -0
- package/lib/contracts/index.js +3 -1
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- 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 +7 -20
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- 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/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.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/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.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/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -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/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +7 -16
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- 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/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -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/types/generated/factories/GToken__factory.d.ts +7 -0
- package/lib/contracts/types/generated/factories/GToken__factory.js +4 -0
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/index.d.ts +0 -3
- package/lib/contracts/utils/index.js +0 -3
- package/lib/contracts/utils/openTrades.js +14 -30
- package/lib/contracts/utils/pairs.js +20 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -0
- package/lib/markets/collateral/converter.d.ts +5 -0
- package/lib/markets/collateral/converter.js +11 -0
- package/lib/markets/collateral/index.d.ts +1 -0
- package/lib/markets/collateral/index.js +17 -0
- package/lib/markets/collateral/types.d.ts +7 -0
- package/lib/markets/collateral/types.js +2 -0
- package/lib/markets/holdingFees/index.d.ts +46 -0
- package/lib/markets/holdingFees/index.js +105 -0
- package/lib/markets/holdingFees/types.d.ts +23 -0
- package/lib/markets/holdingFees/types.js +5 -0
- package/lib/markets/index.d.ts +5 -0
- package/lib/markets/index.js +5 -0
- package/lib/markets/leverage/builder.d.ts +12 -0
- package/lib/markets/leverage/builder.js +25 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.d.ts +7 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.js +38 -0
- package/lib/markets/leverage/index.d.ts +3 -0
- package/lib/markets/leverage/index.js +19 -0
- package/lib/markets/leverage/types.d.ts +15 -0
- package/lib/markets/leverage/types.js +2 -0
- package/lib/markets/oi/converter.d.ts +62 -0
- package/lib/markets/oi/converter.js +102 -0
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/index.d.ts +49 -0
- package/lib/markets/oi/index.js +77 -0
- package/lib/markets/oi/types.d.ts +73 -0
- package/lib/markets/oi/types.js +6 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/markets/price/builder.d.ts +25 -0
- package/lib/markets/price/builder.js +69 -0
- package/lib/markets/price/index.d.ts +6 -0
- package/lib/markets/price/index.js +22 -0
- package/lib/markets/price/marketPrice.d.ts +12 -0
- package/lib/markets/price/marketPrice.js +34 -0
- package/lib/markets/price/types.d.ts +23 -0
- package/lib/markets/price/types.js +5 -0
- package/lib/trade/counterTrade/index.d.ts +2 -0
- package/lib/trade/counterTrade/index.js +18 -0
- package/lib/trade/counterTrade/types.d.ts +7 -0
- package/lib/trade/counterTrade/types.js +2 -0
- package/lib/trade/counterTrade/validateCounterTrade.d.ts +10 -0
- package/lib/trade/counterTrade/validateCounterTrade.js +29 -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/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +33 -0
- package/lib/trade/fees/borrowing/index.d.ts +15 -4
- package/lib/trade/fees/borrowing/index.js +42 -18
- package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
- package/lib/trade/fees/borrowingV2/builder.js +24 -0
- package/lib/trade/fees/borrowingV2/converter.d.ts +12 -3
- package/lib/trade/fees/borrowingV2/converter.js +29 -18
- package/lib/{contracts/fetch/fees/borrowingFeesV2.d.ts → trade/fees/borrowingV2/fetcher.d.ts} +2 -3
- package/lib/{contracts/fetch/fees/borrowingFeesV2.js → trade/fees/borrowingV2/fetcher.js} +6 -14
- package/lib/trade/fees/borrowingV2/index.d.ts +6 -17
- package/lib/trade/fees/borrowingV2/index.js +10 -37
- package/lib/trade/fees/borrowingV2/types.d.ts +22 -6
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +114 -0
- package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
- package/lib/trade/fees/fundingFees/builder.js +35 -0
- package/lib/{contracts/fetch/fees/fundingFees.d.ts → trade/fees/fundingFees/fetcher.d.ts} +2 -2
- package/lib/trade/fees/fundingFees/index.d.ts +28 -39
- package/lib/trade/fees/fundingFees/index.js +33 -46
- package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
- package/lib/trade/fees/fundingFees/pairContext.js +17 -0
- package/lib/trade/fees/holdingFees/index.d.ts +46 -0
- package/lib/trade/fees/holdingFees/index.js +105 -0
- package/lib/trade/fees/holdingFees/types.d.ts +23 -0
- package/lib/trade/fees/holdingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +4 -2
- package/lib/trade/fees/index.js +45 -36
- package/lib/trade/fees/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- package/lib/trade/fees/tiers/index.d.ts +1 -0
- package/lib/trade/fees/tiers/index.js +1 -0
- package/lib/trade/fees/trading/builder.d.ts +18 -0
- package/lib/trade/fees/trading/builder.js +20 -0
- package/lib/trade/fees/trading/converter.d.ts +2 -0
- package/lib/trade/fees/trading/converter.js +5 -1
- package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFees.js +66 -0
- package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
- package/lib/trade/fees/trading/index.d.ts +30 -2
- package/lib/trade/fees/trading/index.js +52 -1
- package/lib/trade/fees/trading/types.d.ts +9 -0
- package/lib/trade/index.d.ts +3 -1
- package/lib/trade/index.js +3 -1
- package/lib/trade/liquidation/builder.d.ts +25 -0
- package/lib/trade/liquidation/builder.js +59 -0
- package/lib/trade/liquidation/converter.d.ts +23 -0
- package/lib/trade/liquidation/converter.js +46 -0
- package/lib/trade/liquidation/index.d.ts +16 -0
- package/lib/trade/liquidation/index.js +123 -0
- package/lib/trade/liquidation/types.d.ts +42 -0
- package/lib/trade/liquidation/types.js +2 -0
- package/lib/trade/pnl/builder.d.ts +16 -0
- package/lib/trade/pnl/builder.js +44 -0
- package/lib/trade/pnl/converter.d.ts +47 -0
- package/lib/trade/pnl/converter.js +72 -0
- package/lib/trade/pnl/index.d.ts +88 -0
- package/lib/trade/pnl/index.js +286 -0
- package/lib/trade/pnl/types.d.ts +75 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/builder.d.ts +23 -0
- package/lib/trade/priceImpact/close/builder.js +45 -0
- package/lib/trade/priceImpact/close/index.d.ts +22 -0
- package/lib/trade/priceImpact/close/index.js +119 -0
- package/lib/trade/priceImpact/close/types.d.ts +44 -0
- package/lib/trade/priceImpact/close/types.js +5 -0
- package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
- package/lib/trade/priceImpact/cumulVol/builder.js +43 -0
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
- package/lib/trade/priceImpact/cumulVol/index.d.ts +108 -0
- package/lib/trade/priceImpact/cumulVol/index.js +235 -0
- package/lib/trade/priceImpact/index.d.ts +18 -5
- package/lib/trade/priceImpact/index.js +57 -10
- package/lib/trade/priceImpact/open/builder.d.ts +21 -0
- package/lib/trade/priceImpact/open/builder.js +43 -0
- package/lib/trade/priceImpact/open/index.d.ts +23 -0
- package/lib/trade/priceImpact/open/index.js +74 -0
- package/lib/trade/priceImpact/open/types.d.ts +41 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
- package/lib/trade/priceImpact/skew/builder.js +28 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +11 -42
- package/lib/trade/priceImpact/skew/converter.js +19 -109
- package/lib/{contracts/fetch/priceImpact/skew.d.ts → trade/priceImpact/skew/fetcher.d.ts} +9 -12
- package/lib/{contracts/fetch/priceImpact/skew.js → trade/priceImpact/skew/fetcher.js} +23 -22
- package/lib/trade/priceImpact/skew/index.d.ts +11 -15
- package/lib/trade/priceImpact/skew/index.js +22 -54
- package/lib/trade/priceImpact/skew/types.d.ts +2 -15
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +30 -8
- package/lib/trade/types.js +20 -0
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +2 -2
- package/package.json +2 -2
- package/lib/trade/liquidation.d.ts +0 -42
- package/lib/trade/liquidation.js +0 -140
- package/lib/trade/pnl.d.ts +0 -10
- package/lib/trade/pnl.js +0 -38
- /package/lib/{contracts/fetch/fees/fundingFees.js → trade/fees/fundingFees/fetcher.js} +0 -0
package/lib/trade/spread.js
CHANGED
|
@@ -1,108 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Pure spread calculations without price impact
|
|
4
|
+
* @dev For price impact calculations, see priceImpact module
|
|
5
|
+
*/
|
|
2
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSpreadP = exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
: spreadCtx.protectionCloseFactor;
|
|
17
|
-
const protectionCloseFactorMultiplier = ((_a = spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) === null || _a === void 0 ? void 0 : _a.cumulVolPriceImpactMultiplier) !== undefined &&
|
|
18
|
-
spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier > 0
|
|
19
|
-
? spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier
|
|
20
|
-
: 1;
|
|
21
|
-
return protectionCloseFactor * protectionCloseFactorMultiplier;
|
|
22
|
-
};
|
|
23
|
-
exports.getProtectionCloseFactor = getProtectionCloseFactor;
|
|
24
|
-
const isProtectionCloseFactorActive = (spreadCtx) => {
|
|
25
|
-
if (spreadCtx === undefined ||
|
|
26
|
-
spreadCtx.currentBlock === undefined ||
|
|
27
|
-
spreadCtx.createdBlock === undefined ||
|
|
28
|
-
spreadCtx.protectionCloseFactorBlocks === undefined ||
|
|
29
|
-
spreadCtx.protectionCloseFactor === undefined) {
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
32
|
-
return (spreadCtx.isPnlPositive === true &&
|
|
33
|
-
spreadCtx.isOpen === false &&
|
|
34
|
-
spreadCtx.protectionCloseFactor > 0 &&
|
|
35
|
-
spreadCtx.currentBlock <=
|
|
36
|
-
spreadCtx.createdBlock + spreadCtx.protectionCloseFactorBlocks &&
|
|
37
|
-
spreadCtx.protectionCloseFactorWhitelist !== true);
|
|
38
|
-
};
|
|
39
|
-
exports.isProtectionCloseFactorActive = isProtectionCloseFactorActive;
|
|
40
|
-
const getCumulativeFactor = (spreadCtx) => {
|
|
41
|
-
if (spreadCtx === undefined ||
|
|
42
|
-
spreadCtx.cumulativeFactor === undefined ||
|
|
43
|
-
spreadCtx.cumulativeFactor === 0) {
|
|
44
|
-
return constants_1.DEFAULT_CUMULATIVE_FACTOR;
|
|
45
|
-
}
|
|
46
|
-
return spreadCtx.cumulativeFactor;
|
|
47
|
-
};
|
|
48
|
-
exports.getCumulativeFactor = getCumulativeFactor;
|
|
49
|
-
const getLegacyFactor = (spreadCtx) => {
|
|
50
|
-
return (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
|
|
51
|
-
};
|
|
52
|
-
exports.getLegacyFactor = getLegacyFactor;
|
|
53
|
-
const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, spreadCtx) => {
|
|
54
|
-
if (pairSpreadP === undefined) {
|
|
55
|
-
return 0;
|
|
56
|
-
}
|
|
57
|
-
if (
|
|
58
|
-
// No spread or price impact when closing pre-v9.2 trades
|
|
59
|
-
((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
|
|
60
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) ||
|
|
61
|
-
// No spread or price impact for opens when `pair.exemptOnOpen` is true
|
|
62
|
-
((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === true && (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptOnOpen) === true) ||
|
|
63
|
-
// No spread or price impact for closes after `protectionCloseFactor` has expired
|
|
64
|
-
// when `pair.exemptAfterProtectionCloseFactor` is true
|
|
65
|
-
((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
|
|
66
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptAfterProtectionCloseFactor) === true &&
|
|
67
|
-
(0, exports.isProtectionCloseFactorActive)(spreadCtx) !== true)) {
|
|
68
|
-
return 0;
|
|
69
|
-
}
|
|
70
|
-
const onePercentDepth = buy
|
|
71
|
-
? // if `long`
|
|
72
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
|
|
73
|
-
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
|
|
74
|
-
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
75
|
-
: // if `short`
|
|
76
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
|
|
77
|
-
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
78
|
-
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
|
|
79
|
-
let activeOi = undefined;
|
|
80
|
-
if (oiWindowsSettings !== undefined) {
|
|
81
|
-
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(oiWindowsSettings), oiWindowsSettings.windowsCount, oiWindows, (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false ? buy : !buy);
|
|
82
|
-
}
|
|
83
|
-
if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
|
|
84
|
-
return pairSpreadP / 2;
|
|
85
|
-
}
|
|
86
|
-
return ((0, exports.getSpreadP)(pairSpreadP, undefined, undefined, spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) +
|
|
87
|
-
((activeOi * (0, exports.getCumulativeFactor)(spreadCtx) + (collateral * leverage) / 2) /
|
|
88
|
-
onePercentDepth /
|
|
89
|
-
100 /
|
|
90
|
-
(0, exports.getLegacyFactor)(spreadCtx)) *
|
|
91
|
-
(0, exports.getProtectionCloseFactor)(spreadCtx));
|
|
92
|
-
};
|
|
93
|
-
exports.getSpreadWithPriceImpactP = getSpreadWithPriceImpactP;
|
|
94
|
-
const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
|
|
95
|
-
var _a;
|
|
96
|
-
const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
|
|
97
|
-
if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
|
|
98
|
-
return 0;
|
|
99
|
-
}
|
|
100
|
-
const spreadP = pairSpreadP / 2 + fixedSpreadP;
|
|
101
|
-
return isLiquidation === true &&
|
|
102
|
-
liquidationParams !== undefined &&
|
|
103
|
-
liquidationParams.maxLiqSpreadP > 0 &&
|
|
104
|
-
spreadP > liquidationParams.maxLiqSpreadP
|
|
105
|
-
? liquidationParams.maxLiqSpreadP
|
|
106
|
-
: spreadP;
|
|
107
|
-
};
|
|
108
|
-
exports.getSpreadP = getSpreadP;
|
|
7
|
+
exports.getSpreadP = exports.getFixedSpreadP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = exports.getCumulVolPriceImpact = exports.getTradeCumulVolPriceImpactP = exports.getSpreadWithCumulVolPriceImpactP = exports.getSpreadWithPriceImpactP = void 0;
|
|
8
|
+
// Re-export from priceImpact/cumulVol for backward compatibility
|
|
9
|
+
var cumulVol_1 = require("./priceImpact/cumulVol");
|
|
10
|
+
Object.defineProperty(exports, "getSpreadWithPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithPriceImpactP; } });
|
|
11
|
+
Object.defineProperty(exports, "getSpreadWithCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithCumulVolPriceImpactP; } });
|
|
12
|
+
Object.defineProperty(exports, "getTradeCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getTradeCumulVolPriceImpactP; } });
|
|
13
|
+
Object.defineProperty(exports, "getCumulVolPriceImpact", { enumerable: true, get: function () { return cumulVol_1.getCumulVolPriceImpact; } });
|
|
14
|
+
Object.defineProperty(exports, "getProtectionCloseFactor", { enumerable: true, get: function () { return cumulVol_1.getProtectionCloseFactor; } });
|
|
15
|
+
Object.defineProperty(exports, "isProtectionCloseFactorActive", { enumerable: true, get: function () { return cumulVol_1.isProtectionCloseFactorActive; } });
|
|
16
|
+
Object.defineProperty(exports, "getCumulativeFactor", { enumerable: true, get: function () { return cumulVol_1.getCumulativeFactor; } });
|
|
17
|
+
Object.defineProperty(exports, "getLegacyFactor", { enumerable: true, get: function () { return cumulVol_1.getLegacyFactor; } });
|
|
18
|
+
Object.defineProperty(exports, "getFixedSpreadP", { enumerable: true, get: function () { return cumulVol_1.getFixedSpreadP; } });
|
|
19
|
+
Object.defineProperty(exports, "getSpreadP", { enumerable: true, get: function () { return cumulVol_1.getSpreadP; } });
|
package/lib/trade/types.d.ts
CHANGED
|
@@ -13,6 +13,14 @@ export type TradeFeesData = {
|
|
|
13
13
|
initialAccFundingFeeP: number;
|
|
14
14
|
initialAccBorrowingFeeP: number;
|
|
15
15
|
};
|
|
16
|
+
export type UiRealizedPnlData = {
|
|
17
|
+
realizedTradingFeesCollateral: number;
|
|
18
|
+
realizedOldBorrowingFeesCollateral: number;
|
|
19
|
+
realizedNewBorrowingFeesCollateral: number;
|
|
20
|
+
realizedFundingFeesCollateral: number;
|
|
21
|
+
realizedPnlPartialCloseCollateral: number;
|
|
22
|
+
pnlWithdrawnCollateral: number;
|
|
23
|
+
};
|
|
16
24
|
export type CounterTradeSettings = {
|
|
17
25
|
maxLeverage: number;
|
|
18
26
|
feeRateMultiplier: number;
|
|
@@ -23,6 +31,7 @@ export type TradeContainer = {
|
|
|
23
31
|
liquidationParams: LiquidationParams;
|
|
24
32
|
initialAccFees: TradeInitialAccFees;
|
|
25
33
|
tradeFeesData?: TradeFeesData;
|
|
34
|
+
uiRealizedPnlData?: UiRealizedPnlData;
|
|
26
35
|
receivedAt?: number;
|
|
27
36
|
};
|
|
28
37
|
export type Trade = {
|
|
@@ -175,13 +184,6 @@ export type OiWindow = PairOi;
|
|
|
175
184
|
export type OiWindows = {
|
|
176
185
|
[key: string]: OiWindow;
|
|
177
186
|
};
|
|
178
|
-
export type CollateralConfig = {
|
|
179
|
-
collateral: string;
|
|
180
|
-
isActive: boolean;
|
|
181
|
-
precision: number;
|
|
182
|
-
precisionDelta: number;
|
|
183
|
-
decimals?: number;
|
|
184
|
-
};
|
|
185
187
|
export type FeeTiers = {
|
|
186
188
|
tiers: FeeTier[];
|
|
187
189
|
multipliers: number[];
|
|
@@ -617,5 +619,25 @@ export declare enum PairIndex {
|
|
|
617
619
|
SYRUPUSD = 373,
|
|
618
620
|
SIGNUSD = 374,
|
|
619
621
|
ZORAUSD = 375,
|
|
620
|
-
COINUSD = 376
|
|
622
|
+
COINUSD = 376,
|
|
623
|
+
HOODUSD = 377,
|
|
624
|
+
MSTRUSD = 378,
|
|
625
|
+
NFLXUSD = 379,
|
|
626
|
+
LAUNCHCOINUSD = 380,
|
|
627
|
+
NXPCUSD = 381,
|
|
628
|
+
SOPHUSD = 382,
|
|
629
|
+
LPTUSD = 383,
|
|
630
|
+
BVIVUSD = 384,
|
|
631
|
+
EVIVUSD = 385,
|
|
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
|
|
621
643
|
}
|
package/lib/trade/types.js
CHANGED
|
@@ -406,4 +406,24 @@ var PairIndex;
|
|
|
406
406
|
PairIndex[PairIndex["SIGNUSD"] = 374] = "SIGNUSD";
|
|
407
407
|
PairIndex[PairIndex["ZORAUSD"] = 375] = "ZORAUSD";
|
|
408
408
|
PairIndex[PairIndex["COINUSD"] = 376] = "COINUSD";
|
|
409
|
+
PairIndex[PairIndex["HOODUSD"] = 377] = "HOODUSD";
|
|
410
|
+
PairIndex[PairIndex["MSTRUSD"] = 378] = "MSTRUSD";
|
|
411
|
+
PairIndex[PairIndex["NFLXUSD"] = 379] = "NFLXUSD";
|
|
412
|
+
PairIndex[PairIndex["LAUNCHCOINUSD"] = 380] = "LAUNCHCOINUSD";
|
|
413
|
+
PairIndex[PairIndex["NXPCUSD"] = 381] = "NXPCUSD";
|
|
414
|
+
PairIndex[PairIndex["SOPHUSD"] = 382] = "SOPHUSD";
|
|
415
|
+
PairIndex[PairIndex["LPTUSD"] = 383] = "LPTUSD";
|
|
416
|
+
PairIndex[PairIndex["BVIVUSD"] = 384] = "BVIVUSD";
|
|
417
|
+
PairIndex[PairIndex["EVIVUSD"] = 385] = "EVIVUSD";
|
|
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";
|
|
409
429
|
})(PairIndex = exports.PairIndex || (exports.PairIndex = {}));
|
package/lib/vault/index.d.ts
CHANGED
|
@@ -4,5 +4,7 @@ type GetPendingAccBlockWeightedMarketCap = {
|
|
|
4
4
|
accBlockWeightedMarketCapLastStored: number;
|
|
5
5
|
marketCapPrecision?: number;
|
|
6
6
|
};
|
|
7
|
-
export declare const getPendingAccBlockWeightedMarketCap: (
|
|
7
|
+
export declare const getPendingAccBlockWeightedMarketCap: (input: {
|
|
8
|
+
currentBlock: number;
|
|
9
|
+
}, context: GetPendingAccBlockWeightedMarketCap) => number;
|
|
8
10
|
export {};
|
package/lib/vault/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getPendingAccBlockWeightedMarketCap = void 0;
|
|
4
|
-
const getPendingAccBlockWeightedMarketCap = (
|
|
4
|
+
const getPendingAccBlockWeightedMarketCap = (input, context) => {
|
|
5
5
|
const { marketCap, accBlockWeightedMarketCap, accBlockWeightedMarketCapLastStored, marketCapPrecision, } = context;
|
|
6
6
|
return (accBlockWeightedMarketCap +
|
|
7
|
-
(currentBlock - accBlockWeightedMarketCapLastStored) /
|
|
7
|
+
(input.currentBlock - accBlockWeightedMarketCapLastStored) /
|
|
8
8
|
Math.max(marketCap * (marketCapPrecision || 1e18), 1));
|
|
9
9
|
};
|
|
10
10
|
exports.getPendingAccBlockWeightedMarketCap = getPendingAccBlockWeightedMarketCap;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gainsnetwork/sdk",
|
|
3
|
-
"version": "0.0.0-v10.
|
|
3
|
+
"version": "0.0.0-v10.rc21",
|
|
4
4
|
"description": "Gains Network SDK",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
},
|
|
99
99
|
"dependencies": {
|
|
100
100
|
"@ethersproject/providers": "^5.7.2",
|
|
101
|
-
"
|
|
101
|
+
"@gainsnetwork/contests": "^0.0.1",
|
|
102
102
|
"ethcall": "^4.8.13",
|
|
103
103
|
"ethers": "^5.7.2",
|
|
104
104
|
"luxon": "^3.2.0"
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
|
-
import { GetLiquidationFeesContext } from "./fees/trading";
|
|
3
|
-
import { BorrowingFeeV2 } from "./fees/borrowingV2";
|
|
4
|
-
import { Fee, LiquidationParams, Trade, TradeInfo, UserPriceImpact, TradeFeesData } from "./types";
|
|
5
|
-
import { ContractsVersion } from "../contracts/types";
|
|
6
|
-
export type GetLiquidationPriceContext = GetBorrowingFeeContext & BorrowingFeeV2.GetBorrowingFeeV2Context & GetLiquidationFeesContext & {
|
|
7
|
-
liquidationParams: LiquidationParams | undefined;
|
|
8
|
-
pairSpreadP: number | undefined;
|
|
9
|
-
collateralPriceUsd: number | undefined;
|
|
10
|
-
contractsVersion: ContractsVersion | undefined;
|
|
11
|
-
userPriceImpact?: UserPriceImpact | undefined;
|
|
12
|
-
currentPairPrice?: number;
|
|
13
|
-
isCounterTrade?: boolean;
|
|
14
|
-
tradeFeesData?: TradeFeesData;
|
|
15
|
-
partialCloseMultiplier?: number;
|
|
16
|
-
additionalFeeCollateral?: number;
|
|
17
|
-
beforeOpened?: boolean;
|
|
18
|
-
initialAccFees?: BorrowingFee.InitialAccFees;
|
|
19
|
-
fundingParams?: Record<number, Record<number, any>>;
|
|
20
|
-
fundingData?: Record<number, Record<number, any>>;
|
|
21
|
-
pairOiAfterV10?: Record<number, Record<number, any>>;
|
|
22
|
-
netExposureToken?: Record<number, Record<number, number>>;
|
|
23
|
-
netExposureUsd?: Record<number, Record<number, number>>;
|
|
24
|
-
};
|
|
25
|
-
export type TradeHoldingFees = {
|
|
26
|
-
fundingFeeCollateral: number;
|
|
27
|
-
borrowingFeeCollateral: number;
|
|
28
|
-
borrowingFeeCollateral_old: number;
|
|
29
|
-
totalFeeCollateral: number;
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* @dev Calculates total holding fees for a trade (funding + borrowing fees)
|
|
33
|
-
* @param trade The trade to calculate fees for
|
|
34
|
-
* @param tradeInfo Trade info containing contracts version
|
|
35
|
-
* @param tradeFeesData Trade fees data containing initial acc fees
|
|
36
|
-
* @param currentPairPrice Current pair price
|
|
37
|
-
* @param context Context with fee parameters
|
|
38
|
-
* @returns Object containing all holding fee components
|
|
39
|
-
*/
|
|
40
|
-
export declare const getTradePendingHoldingFeesCollateral: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: GetLiquidationPriceContext) => TradeHoldingFees;
|
|
41
|
-
export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
|
|
42
|
-
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|
package/lib/trade/liquidation.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLiqPnlThresholdP = exports.getLiquidationPrice = exports.getTradePendingHoldingFeesCollateral = void 0;
|
|
4
|
-
const fees_1 = require("./fees");
|
|
5
|
-
const trading_1 = require("./fees/trading");
|
|
6
|
-
const borrowingV2_1 = require("./fees/borrowingV2");
|
|
7
|
-
const spread_1 = require("./spread");
|
|
8
|
-
const types_1 = require("../contracts/types");
|
|
9
|
-
/**
|
|
10
|
-
* @dev Calculates total holding fees for a trade (funding + borrowing fees)
|
|
11
|
-
* @param trade The trade to calculate fees for
|
|
12
|
-
* @param tradeInfo Trade info containing contracts version
|
|
13
|
-
* @param tradeFeesData Trade fees data containing initial acc fees
|
|
14
|
-
* @param currentPairPrice Current pair price
|
|
15
|
-
* @param context Context with fee parameters
|
|
16
|
-
* @returns Object containing all holding fee components
|
|
17
|
-
*/
|
|
18
|
-
const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
|
|
19
|
-
var _a;
|
|
20
|
-
// Calculate funding fees (v10+ only)
|
|
21
|
-
const fundingFeeCollateral = ((_a = context.contractsVersion) !== null && _a !== void 0 ? _a : tradeInfo.contractsVersion) >=
|
|
22
|
-
types_1.ContractsVersion.V10
|
|
23
|
-
? (0, fees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, context)
|
|
24
|
-
: 0;
|
|
25
|
-
// Calculate borrowing fees v2
|
|
26
|
-
const borrowingFeeCollateral = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
|
|
27
|
-
positionSizeCollateral: trade.collateralAmount * trade.leverage,
|
|
28
|
-
openPrice: trade.openPrice,
|
|
29
|
-
collateralIndex: trade.collateralIndex,
|
|
30
|
-
pairIndex: trade.pairIndex,
|
|
31
|
-
currentPairPrice,
|
|
32
|
-
initialAccBorrowingFeeP: tradeFeesData.initialAccBorrowingFeeP,
|
|
33
|
-
currentTimestamp: context.currentTimestamp,
|
|
34
|
-
}, context);
|
|
35
|
-
// Calculate v1 borrowing fees (some markets use v1 indefinitely)
|
|
36
|
-
const borrowingFeeCollateral_old = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, context.initialAccFees || { accPairFee: 0, accGroupFee: 0, block: 0 }, // Use context initial fees or empty
|
|
37
|
-
context);
|
|
38
|
-
return {
|
|
39
|
-
fundingFeeCollateral,
|
|
40
|
-
borrowingFeeCollateral,
|
|
41
|
-
borrowingFeeCollateral_old,
|
|
42
|
-
totalFeeCollateral: fundingFeeCollateral +
|
|
43
|
-
borrowingFeeCollateral +
|
|
44
|
-
borrowingFeeCollateral_old,
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
exports.getTradePendingHoldingFeesCollateral = getTradePendingHoldingFeesCollateral;
|
|
48
|
-
const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
49
|
-
var _a, _b, _c, _d, _e, _f;
|
|
50
|
-
// Ensure initialAccFees is in context
|
|
51
|
-
if (!context.initialAccFees) {
|
|
52
|
-
context = Object.assign(Object.assign({}, context), { initialAccFees });
|
|
53
|
-
}
|
|
54
|
-
// 1. Calculate liquidation fees
|
|
55
|
-
const closingFee = (0, trading_1.getTotalTradeLiqFeesCollateral)(0, // collateralIndex not used in calculation
|
|
56
|
-
trade.user, trade.pairIndex, trade.collateralAmount, context);
|
|
57
|
-
// 2. Calculate holding fees and realized PnL
|
|
58
|
-
let holdingFeesTotal = 0;
|
|
59
|
-
let totalRealizedPnlCollateral = 0;
|
|
60
|
-
if (!context.beforeOpened &&
|
|
61
|
-
context.tradeFeesData &&
|
|
62
|
-
context.currentPairPrice) {
|
|
63
|
-
// V10 data available - calculate full holding fees
|
|
64
|
-
// Create a minimal tradeInfo from context
|
|
65
|
-
const tradeInfo = {
|
|
66
|
-
contractsVersion: (_a = context.contractsVersion) !== null && _a !== void 0 ? _a : types_1.ContractsVersion.V10,
|
|
67
|
-
createdBlock: 0,
|
|
68
|
-
tpLastUpdatedBlock: 0,
|
|
69
|
-
slLastUpdatedBlock: 0,
|
|
70
|
-
maxSlippageP: 0,
|
|
71
|
-
lastOiUpdateTs: 0,
|
|
72
|
-
collateralPriceUsd: (_b = context.collateralPriceUsd) !== null && _b !== void 0 ? _b : 0,
|
|
73
|
-
lastPosIncreaseBlock: 0,
|
|
74
|
-
};
|
|
75
|
-
const holdingFees = (0, exports.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeFeesData, context.currentPairPrice, context);
|
|
76
|
-
holdingFeesTotal = holdingFees.totalFeeCollateral;
|
|
77
|
-
// Calculate total realized PnL (realized PnL minus realized trading fees)
|
|
78
|
-
totalRealizedPnlCollateral =
|
|
79
|
-
context.tradeFeesData.realizedPnlCollateral -
|
|
80
|
-
context.tradeFeesData.realizedTradingFeesCollateral;
|
|
81
|
-
}
|
|
82
|
-
else if (!context.beforeOpened) {
|
|
83
|
-
// Markets using v1 borrowing fees model
|
|
84
|
-
holdingFeesTotal = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
85
|
-
}
|
|
86
|
-
// 3. Apply unified formula for all trades
|
|
87
|
-
const partialCloseMultiplier = (_c = context.partialCloseMultiplier) !== null && _c !== void 0 ? _c : 1;
|
|
88
|
-
const additionalFeeCollateral = (_d = context.additionalFeeCollateral) !== null && _d !== void 0 ? _d : 0;
|
|
89
|
-
const totalFeesCollateral = closingFee +
|
|
90
|
-
(holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
|
|
91
|
-
additionalFeeCollateral;
|
|
92
|
-
// 4. Calculate liquidation threshold
|
|
93
|
-
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
|
|
94
|
-
// 5. Calculate liquidation price distance
|
|
95
|
-
const collateralLiqNegativePnl = trade.collateralAmount * liqThresholdP;
|
|
96
|
-
let liqPriceDistance = (trade.openPrice * (collateralLiqNegativePnl - totalFeesCollateral)) /
|
|
97
|
-
trade.collateralAmount /
|
|
98
|
-
trade.leverage;
|
|
99
|
-
// 6. Apply closing spread for v9.2+
|
|
100
|
-
if ((context === null || context === void 0 ? void 0 : context.contractsVersion) !== undefined &&
|
|
101
|
-
context.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
102
|
-
((((_e = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _e === void 0 ? void 0 : _e.maxLiqSpreadP) !== undefined &&
|
|
103
|
-
context.liquidationParams.maxLiqSpreadP > 0) ||
|
|
104
|
-
(((_f = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _f === void 0 ? void 0 : _f.fixedSpreadP) !== undefined &&
|
|
105
|
-
context.userPriceImpact.fixedSpreadP > 0))) {
|
|
106
|
-
const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams, context.userPriceImpact);
|
|
107
|
-
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
108
|
-
}
|
|
109
|
-
// 7. Calculate final liquidation price
|
|
110
|
-
return trade.long
|
|
111
|
-
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
112
|
-
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
113
|
-
};
|
|
114
|
-
exports.getLiquidationPrice = getLiquidationPrice;
|
|
115
|
-
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
116
|
-
if (liquidationParams === undefined ||
|
|
117
|
-
leverage === undefined ||
|
|
118
|
-
liquidationParams.maxLiqSpreadP === 0 ||
|
|
119
|
-
liquidationParams.startLiqThresholdP === 0 ||
|
|
120
|
-
liquidationParams.endLiqThresholdP === 0 ||
|
|
121
|
-
liquidationParams.startLeverage === 0 ||
|
|
122
|
-
liquidationParams.endLeverage === 0) {
|
|
123
|
-
return 0.9;
|
|
124
|
-
}
|
|
125
|
-
if (leverage < liquidationParams.startLeverage) {
|
|
126
|
-
return liquidationParams.startLiqThresholdP;
|
|
127
|
-
}
|
|
128
|
-
if (leverage > liquidationParams.endLeverage) {
|
|
129
|
-
return liquidationParams.endLiqThresholdP;
|
|
130
|
-
}
|
|
131
|
-
if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
|
|
132
|
-
return liquidationParams.endLiqThresholdP;
|
|
133
|
-
}
|
|
134
|
-
return (liquidationParams.startLiqThresholdP -
|
|
135
|
-
((leverage - liquidationParams.startLeverage) *
|
|
136
|
-
(liquidationParams.startLiqThresholdP -
|
|
137
|
-
liquidationParams.endLiqThresholdP)) /
|
|
138
|
-
(liquidationParams.endLeverage - liquidationParams.startLeverage));
|
|
139
|
-
};
|
|
140
|
-
exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
|
package/lib/trade/pnl.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { GetBorrowingFeeContext } from "./fees";
|
|
2
|
-
import { GetTradeFeesContext } from "./fees/trading";
|
|
3
|
-
import { LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
|
|
4
|
-
import { ContractsVersion } from "../contracts/types";
|
|
5
|
-
export type GetPnlContext = GetBorrowingFeeContext & GetTradeFeesContext & {
|
|
6
|
-
collateralPriceUsd: number | undefined;
|
|
7
|
-
contractsVersion: ContractsVersion | undefined;
|
|
8
|
-
feeMultiplier: number | undefined;
|
|
9
|
-
};
|
|
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
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getPnl = void 0;
|
|
4
|
-
const fees_1 = require("./fees");
|
|
5
|
-
const trading_1 = require("./fees/trading");
|
|
6
|
-
const liquidation_1 = require("./liquidation");
|
|
7
|
-
const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
|
|
8
|
-
var _a;
|
|
9
|
-
if (!price) {
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
12
|
-
const posCollat = trade.collateralAmount;
|
|
13
|
-
const { openPrice, leverage } = trade;
|
|
14
|
-
let pnlCollat = trade.long
|
|
15
|
-
? ((price - openPrice) / openPrice) * leverage * posCollat
|
|
16
|
-
: ((openPrice - price) / openPrice) * leverage * posCollat;
|
|
17
|
-
if (useFees) {
|
|
18
|
-
pnlCollat -= (0, fees_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
19
|
-
}
|
|
20
|
-
let pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
21
|
-
// Can be liquidated
|
|
22
|
-
if (pnlPercentage <=
|
|
23
|
-
(0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
|
|
24
|
-
pnlPercentage = -100;
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
// Calculate closing fee using the same function as opening fees
|
|
28
|
-
const positionSizeCollateral = posCollat * trade.leverage;
|
|
29
|
-
const closingFee = (0, trading_1.getTotalTradeFeesCollateral)(0, // collateralIndex not used
|
|
30
|
-
trade.user, trade.pairIndex, positionSizeCollateral, (_a = trade.isCounterTrade) !== null && _a !== void 0 ? _a : false, context);
|
|
31
|
-
pnlCollat -= closingFee;
|
|
32
|
-
pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
33
|
-
}
|
|
34
|
-
pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
|
|
35
|
-
pnlCollat = (posCollat * pnlPercentage) / 100;
|
|
36
|
-
return [pnlCollat, pnlPercentage];
|
|
37
|
-
};
|
|
38
|
-
exports.getPnl = getPnl;
|
|
File without changes
|