@gainsnetwork/sdk 0.0.0-v10.rc1 → 0.0.0-v10.rc11
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 +18 -1
- package/lib/constants.js +19 -2
- 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/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/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/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/openTrades.d.ts +1 -0
- package/lib/contracts/utils/openTrades.js +94 -56
- package/lib/contracts/utils/pairs.js +17 -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/index.d.ts +3 -0
- package/lib/markets/index.js +3 -0
- package/lib/markets/oi/converter.d.ts +63 -0
- package/lib/markets/oi/converter.js +103 -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 +10 -0
- package/lib/markets/oi/index.js +37 -0
- package/lib/markets/oi/types.d.ts +82 -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/fees/borrowing/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +33 -0
- package/lib/trade/fees/borrowing/index.d.ts +24 -2
- package/lib/trade/fees/borrowing/index.js +51 -16
- 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 +75 -0
- package/lib/trade/fees/borrowingV2/converter.js +132 -0
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/fetcher.js +185 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +48 -0
- package/lib/trade/fees/borrowingV2/index.js +112 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +95 -0
- package/lib/trade/fees/borrowingV2/types.js +5 -0
- 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/trade/fees/fundingFees/converter.d.ts +102 -0
- package/lib/trade/fees/fundingFees/converter.js +196 -0
- package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
- package/lib/trade/fees/fundingFees/fetcher.js +150 -0
- package/lib/trade/fees/fundingFees/index.d.ts +124 -0
- package/lib/trade/fees/fundingFees/index.js +309 -0
- package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
- package/lib/trade/fees/fundingFees/pairContext.js +17 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +7 -2
- package/lib/trade/fees/index.js +66 -16
- 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 +18 -0
- package/lib/trade/fees/tiers/index.js +45 -1
- 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 +32 -0
- package/lib/trade/fees/trading/converter.js +47 -0
- 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 +62 -0
- package/lib/trade/fees/trading/index.js +155 -0
- package/lib/trade/fees/trading/types.d.ts +48 -0
- package/lib/trade/fees/trading/types.js +5 -0
- package/lib/trade/index.d.ts +3 -2
- package/lib/trade/index.js +3 -2
- 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 +137 -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 +231 -0
- package/lib/trade/priceImpact/index.d.ts +13 -0
- package/lib/trade/priceImpact/index.js +64 -0
- 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 +79 -0
- package/lib/trade/priceImpact/open/types.d.ts +42 -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 +46 -0
- package/lib/trade/priceImpact/skew/converter.js +81 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +60 -0
- package/lib/trade/priceImpact/skew/fetcher.js +169 -0
- package/lib/trade/priceImpact/skew/index.d.ts +60 -0
- package/lib/trade/priceImpact/skew/index.js +173 -0
- package/lib/trade/priceImpact/skew/types.d.ts +42 -0
- package/lib/trade/priceImpact/skew/types.js +5 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +44 -9
- package/lib/trade/types.js +17 -0
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +2 -2
- package/package.json +2 -1
- package/lib/trade/liquidation.d.ts +0 -12
- package/lib/trade/liquidation.js +0 -55
- package/lib/trade/pnl.d.ts +0 -10
- package/lib/trade/pnl.js +0 -33
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
|
@@ -1,14 +1,37 @@
|
|
|
1
|
-
import { ITradingStorage, IBorrowingFees, IPairsStorage } from "../contracts/types/generated/GNSMultiCollatDiamond";
|
|
1
|
+
import { ITradingStorage, IBorrowingFees, IPairsStorage, IFundingFees } from "../contracts/types/generated/GNSMultiCollatDiamond";
|
|
2
2
|
import { BorrowingFee } from "./fees/borrowing";
|
|
3
3
|
import { FeeTier, TraderEnrollment, TraderInfo } from "./fees/tiers/types";
|
|
4
4
|
export type PairIndexes = {
|
|
5
5
|
[key: string]: PairIndex;
|
|
6
6
|
};
|
|
7
|
+
export type TradeFeesData = {
|
|
8
|
+
realizedTradingFeesCollateral: number;
|
|
9
|
+
realizedPnlCollateral: number;
|
|
10
|
+
manuallyRealizedNegativePnlCollateral: number;
|
|
11
|
+
alreadyTransferredNegativePnlCollateral: number;
|
|
12
|
+
virtualAvailableCollateralInDiamond: number;
|
|
13
|
+
initialAccFundingFeeP: number;
|
|
14
|
+
initialAccBorrowingFeeP: number;
|
|
15
|
+
};
|
|
16
|
+
export type UiRealizedPnlData = {
|
|
17
|
+
realizedTradingFeesCollateral: number;
|
|
18
|
+
realizedOldBorrowingFeesCollateral: number;
|
|
19
|
+
realizedNewBorrowingFeesCollateral: number;
|
|
20
|
+
realizedFundingFeesCollateral: number;
|
|
21
|
+
realizedPnlPartialCloseCollateral: number;
|
|
22
|
+
pnlWithdrawnCollateral: number;
|
|
23
|
+
};
|
|
24
|
+
export type CounterTradeSettings = {
|
|
25
|
+
maxLeverage: number;
|
|
26
|
+
feeRateMultiplier: number;
|
|
27
|
+
};
|
|
7
28
|
export type TradeContainer = {
|
|
8
29
|
trade: Trade;
|
|
9
30
|
tradeInfo: TradeInfo;
|
|
10
31
|
liquidationParams: LiquidationParams;
|
|
11
32
|
initialAccFees: TradeInitialAccFees;
|
|
33
|
+
tradeFeesData?: TradeFeesData;
|
|
34
|
+
uiRealizedPnlData?: UiRealizedPnlData;
|
|
12
35
|
receivedAt?: number;
|
|
13
36
|
};
|
|
14
37
|
export type Trade = {
|
|
@@ -145,6 +168,8 @@ export type TradeContainerRaw = {
|
|
|
145
168
|
tradeInfo: ITradingStorage.TradeInfoStruct;
|
|
146
169
|
liquidationParams: IPairsStorage.GroupLiquidationParamsStruct;
|
|
147
170
|
initialAccFees: IBorrowingFees.BorrowingInitialAccFeesStruct;
|
|
171
|
+
tradeFeesData: IFundingFees.TradeFeesDataStruct;
|
|
172
|
+
uiRealizedPnlData?: IFundingFees.UiRealizedPnlDataStruct;
|
|
148
173
|
};
|
|
149
174
|
export type OiWindowsSettings = {
|
|
150
175
|
startTs: number;
|
|
@@ -159,13 +184,6 @@ export type OiWindow = PairOi;
|
|
|
159
184
|
export type OiWindows = {
|
|
160
185
|
[key: string]: OiWindow;
|
|
161
186
|
};
|
|
162
|
-
export type CollateralConfig = {
|
|
163
|
-
collateral: string;
|
|
164
|
-
isActive: boolean;
|
|
165
|
-
precision: number;
|
|
166
|
-
precisionDelta: number;
|
|
167
|
-
decimals?: number;
|
|
168
|
-
};
|
|
169
187
|
export type FeeTiers = {
|
|
170
188
|
tiers: FeeTier[];
|
|
171
189
|
multipliers: number[];
|
|
@@ -594,5 +612,22 @@ export declare enum PairIndex {
|
|
|
594
612
|
RFCUSD = 366,
|
|
595
613
|
WCTUSD = 367,
|
|
596
614
|
BIGTIMEUSD = 368,
|
|
597
|
-
BABYUSD = 369
|
|
615
|
+
BABYUSD = 369,
|
|
616
|
+
COOKIEUSD = 370,
|
|
617
|
+
KMNOUSD = 371,
|
|
618
|
+
INITUSD = 372,
|
|
619
|
+
SYRUPUSD = 373,
|
|
620
|
+
SIGNUSD = 374,
|
|
621
|
+
ZORAUSD = 375,
|
|
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
|
|
598
633
|
}
|
package/lib/trade/types.js
CHANGED
|
@@ -399,4 +399,21 @@ var PairIndex;
|
|
|
399
399
|
PairIndex[PairIndex["WCTUSD"] = 367] = "WCTUSD";
|
|
400
400
|
PairIndex[PairIndex["BIGTIMEUSD"] = 368] = "BIGTIMEUSD";
|
|
401
401
|
PairIndex[PairIndex["BABYUSD"] = 369] = "BABYUSD";
|
|
402
|
+
PairIndex[PairIndex["COOKIEUSD"] = 370] = "COOKIEUSD";
|
|
403
|
+
PairIndex[PairIndex["KMNOUSD"] = 371] = "KMNOUSD";
|
|
404
|
+
PairIndex[PairIndex["INITUSD"] = 372] = "INITUSD";
|
|
405
|
+
PairIndex[PairIndex["SYRUPUSD"] = 373] = "SYRUPUSD";
|
|
406
|
+
PairIndex[PairIndex["SIGNUSD"] = 374] = "SIGNUSD";
|
|
407
|
+
PairIndex[PairIndex["ZORAUSD"] = 375] = "ZORAUSD";
|
|
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";
|
|
402
419
|
})(PairIndex = exports.PairIndex || (exports.PairIndex = {}));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Trade-specific utility functions
|
|
3
|
+
* @dev Common calculations and conversions used across trading modules
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @dev Converts position size from collateral to tokens
|
|
7
|
+
* @param positionSizeCollateral Position size in collateral tokens
|
|
8
|
+
* @param currentPrice Current pair price
|
|
9
|
+
* @returns Position size in tokens
|
|
10
|
+
*/
|
|
11
|
+
export declare const calculatePositionSizeToken: (positionSizeCollateral: number, currentPrice: number) => number;
|
|
12
|
+
/**
|
|
13
|
+
* @dev Converts position size from tokens to collateral
|
|
14
|
+
* @param positionSizeToken Position size in tokens
|
|
15
|
+
* @param currentPrice Current pair price
|
|
16
|
+
* @returns Position size in collateral tokens
|
|
17
|
+
*/
|
|
18
|
+
export declare const calculatePositionSizeCollateral: (positionSizeToken: number, currentPrice: number) => number;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Trade-specific utility functions
|
|
4
|
+
* @dev Common calculations and conversions used across trading modules
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.calculatePositionSizeCollateral = exports.calculatePositionSizeToken = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* @dev Converts position size from collateral to tokens
|
|
10
|
+
* @param positionSizeCollateral Position size in collateral tokens
|
|
11
|
+
* @param currentPrice Current pair price
|
|
12
|
+
* @returns Position size in tokens
|
|
13
|
+
*/
|
|
14
|
+
const calculatePositionSizeToken = (positionSizeCollateral, currentPrice) => {
|
|
15
|
+
if (currentPrice === 0) {
|
|
16
|
+
throw new Error("Current price cannot be zero");
|
|
17
|
+
}
|
|
18
|
+
return positionSizeCollateral / currentPrice;
|
|
19
|
+
};
|
|
20
|
+
exports.calculatePositionSizeToken = calculatePositionSizeToken;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts position size from tokens to collateral
|
|
23
|
+
* @param positionSizeToken Position size in tokens
|
|
24
|
+
* @param currentPrice Current pair price
|
|
25
|
+
* @returns Position size in collateral tokens
|
|
26
|
+
*/
|
|
27
|
+
const calculatePositionSizeCollateral = (positionSizeToken, currentPrice) => {
|
|
28
|
+
return positionSizeToken * currentPrice;
|
|
29
|
+
};
|
|
30
|
+
exports.calculatePositionSizeCollateral = calculatePositionSizeCollateral;
|
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.rc11",
|
|
4
4
|
"description": "Gains Network SDK",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -98,6 +98,7 @@
|
|
|
98
98
|
},
|
|
99
99
|
"dependencies": {
|
|
100
100
|
"@ethersproject/providers": "^5.7.2",
|
|
101
|
+
"@gainsnetwork/contests": "^0.0.1",
|
|
101
102
|
"ethcall": "^4.8.13",
|
|
102
103
|
"ethers": "^5.7.2",
|
|
103
104
|
"luxon": "^3.2.0"
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
|
-
import { Fee, LiquidationParams, Trade, UserPriceImpact } from "./types";
|
|
3
|
-
import { ContractsVersion } from "../contracts/types";
|
|
4
|
-
export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
|
|
5
|
-
liquidationParams: LiquidationParams | undefined;
|
|
6
|
-
pairSpreadP: number | undefined;
|
|
7
|
-
collateralPriceUsd: number | undefined;
|
|
8
|
-
contractsVersion: ContractsVersion | undefined;
|
|
9
|
-
userPriceImpact?: UserPriceImpact | undefined;
|
|
10
|
-
};
|
|
11
|
-
export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
|
|
12
|
-
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|
package/lib/trade/liquidation.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
|
|
4
|
-
const fees_1 = require("./fees");
|
|
5
|
-
const spread_1 = require("./spread");
|
|
6
|
-
const types_1 = require("../contracts/types");
|
|
7
|
-
const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
8
|
-
var _a, _b;
|
|
9
|
-
const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd);
|
|
10
|
-
const borrowingFee = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
11
|
-
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
|
|
12
|
-
let liqPriceDistance = (trade.openPrice *
|
|
13
|
-
(trade.collateralAmount * liqThresholdP - (borrowingFee + closingFee))) /
|
|
14
|
-
trade.collateralAmount /
|
|
15
|
-
trade.leverage;
|
|
16
|
-
if ((context === null || context === void 0 ? void 0 : context.contractsVersion) !== undefined &&
|
|
17
|
-
context.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
18
|
-
((((_a = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
19
|
-
context.liquidationParams.maxLiqSpreadP > 0) ||
|
|
20
|
-
(((_b = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !== undefined &&
|
|
21
|
-
context.userPriceImpact.fixedSpreadP > 0))) {
|
|
22
|
-
const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams, context.userPriceImpact);
|
|
23
|
-
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
24
|
-
}
|
|
25
|
-
return trade.long
|
|
26
|
-
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
27
|
-
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
28
|
-
};
|
|
29
|
-
exports.getLiquidationPrice = getLiquidationPrice;
|
|
30
|
-
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
31
|
-
if (liquidationParams === undefined ||
|
|
32
|
-
leverage === undefined ||
|
|
33
|
-
liquidationParams.maxLiqSpreadP === 0 ||
|
|
34
|
-
liquidationParams.startLiqThresholdP === 0 ||
|
|
35
|
-
liquidationParams.endLiqThresholdP === 0 ||
|
|
36
|
-
liquidationParams.startLeverage === 0 ||
|
|
37
|
-
liquidationParams.endLeverage === 0) {
|
|
38
|
-
return 0.9;
|
|
39
|
-
}
|
|
40
|
-
if (leverage < liquidationParams.startLeverage) {
|
|
41
|
-
return liquidationParams.startLiqThresholdP;
|
|
42
|
-
}
|
|
43
|
-
if (leverage > liquidationParams.endLeverage) {
|
|
44
|
-
return liquidationParams.endLiqThresholdP;
|
|
45
|
-
}
|
|
46
|
-
if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
|
|
47
|
-
return liquidationParams.endLiqThresholdP;
|
|
48
|
-
}
|
|
49
|
-
return (liquidationParams.startLiqThresholdP -
|
|
50
|
-
((leverage - liquidationParams.startLeverage) *
|
|
51
|
-
(liquidationParams.startLiqThresholdP -
|
|
52
|
-
liquidationParams.endLiqThresholdP)) /
|
|
53
|
-
(liquidationParams.endLeverage - liquidationParams.startLeverage));
|
|
54
|
-
};
|
|
55
|
-
exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
|
package/lib/trade/pnl.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { GetBorrowingFeeContext } from "./fees";
|
|
2
|
-
import { Fee, LiquidationParams, Trade, TradeInfo, TradeInitialAccFees } from "./types";
|
|
3
|
-
import { ContractsVersion } from "../contracts/types";
|
|
4
|
-
export type GetPnlContext = GetBorrowingFeeContext & {
|
|
5
|
-
fee: Fee | undefined;
|
|
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,33 +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 liquidation_1 = require("./liquidation");
|
|
6
|
-
const getPnl = (price, trade, tradeInfo, initialAccFees, liquidationParams, useFees, context) => {
|
|
7
|
-
if (!price) {
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
const posCollat = trade.collateralAmount;
|
|
11
|
-
const { openPrice, leverage } = trade;
|
|
12
|
-
const { fee } = context;
|
|
13
|
-
let pnlCollat = trade.long
|
|
14
|
-
? ((price - openPrice) / openPrice) * leverage * posCollat
|
|
15
|
-
: ((openPrice - price) / openPrice) * leverage * posCollat;
|
|
16
|
-
if (useFees) {
|
|
17
|
-
pnlCollat -= (0, fees_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
18
|
-
}
|
|
19
|
-
let pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
20
|
-
// Can be liquidated
|
|
21
|
-
if (pnlPercentage <=
|
|
22
|
-
(0, liquidation_1.getLiqPnlThresholdP)(liquidationParams, leverage) * -100) {
|
|
23
|
-
pnlPercentage = -100;
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
pnlCollat -= (0, fees_1.getClosingFee)(posCollat, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd, context.feeMultiplier);
|
|
27
|
-
pnlPercentage = (pnlCollat / posCollat) * 100;
|
|
28
|
-
}
|
|
29
|
-
pnlPercentage = pnlPercentage < -100 ? -100 : pnlPercentage;
|
|
30
|
-
pnlCollat = (posCollat * pnlPercentage) / 100;
|
|
31
|
-
return [pnlCollat, pnlPercentage];
|
|
32
|
-
};
|
|
33
|
-
exports.getPnl = getPnl;
|