@gainsnetwork/sdk 1.4.0-rc1 → 1.4.2-rc1
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.js +10 -10
- package/lib/backend/tradingVariables/converter.js +57 -57
- package/lib/backend/tradingVariables/index.js +6 -7
- package/lib/constants.js +2 -2
- package/lib/contracts/addresses.js +4 -1
- package/lib/contracts/index.d.ts +1 -1
- package/lib/contracts/index.js +3 -3
- 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 +386 -260
- 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.d.ts +2 -10
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +1621 -219
- 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/utils/borrowingFees.js +9 -20
- package/lib/contracts/utils/openTrades.js +11 -20
- package/lib/contracts/utils/pairs.js +12 -21
- package/lib/markets/forex.js +1 -1
- package/lib/markets/leverage/builder.js +2 -2
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/markets/price/index.d.ts +1 -0
- package/lib/markets/price/index.js +1 -0
- package/lib/markets/price/signedPrices.d.ts +36 -0
- package/lib/markets/price/signedPrices.js +181 -0
- package/lib/markets/price/types.d.ts +27 -0
- package/lib/pricing/depthBands/converter.d.ts +65 -0
- package/lib/pricing/depthBands/converter.js +155 -0
- package/lib/pricing/depthBands/decoder.d.ts +32 -0
- package/lib/pricing/depthBands/decoder.js +109 -0
- package/lib/pricing/depthBands/encoder.d.ts +19 -0
- package/lib/pricing/depthBands/encoder.js +105 -0
- package/lib/pricing/depthBands/index.d.ts +8 -0
- package/lib/pricing/depthBands/index.js +26 -0
- package/lib/pricing/depthBands/types.d.ts +49 -0
- package/lib/pricing/depthBands/types.js +10 -0
- package/lib/pricing/depthBands/validator.d.ts +22 -0
- package/lib/pricing/depthBands/validator.js +113 -0
- package/lib/pricing/depthBands.d.ts +39 -0
- package/lib/pricing/depthBands.js +94 -0
- package/lib/pricing/index.d.ts +4 -0
- package/lib/pricing/index.js +20 -0
- package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
- package/lib/trade/effectiveLeverage/builder.js +30 -0
- package/lib/trade/fees/borrowing/builder.js +2 -3
- package/lib/trade/fees/borrowing/converter.js +5 -1
- package/lib/trade/fees/borrowing/index.js +5 -5
- package/lib/trade/fees/borrowingV2/builder.js +3 -4
- package/lib/trade/fees/borrowingV2/converter.js +1 -1
- package/lib/trade/fees/borrowingV2/fetcher.js +26 -32
- package/lib/trade/fees/borrowingV2/index.js +3 -3
- package/lib/trade/fees/converter.js +22 -22
- package/lib/trade/fees/fundingFees/builder.js +6 -7
- package/lib/trade/fees/fundingFees/converter.js +1 -1
- package/lib/trade/fees/fundingFees/fetcher.js +16 -25
- package/lib/trade/fees/fundingFees/index.js +2 -3
- 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/tiers/index.js +1 -2
- 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.js +5 -3
- package/lib/trade/liquidation/builder.js +1 -2
- package/lib/trade/liquidation/index.js +4 -6
- package/lib/trade/oiWindows.js +1 -2
- package/lib/trade/pnl/builder.js +1 -2
- package/lib/trade/pnl/converter.js +1 -1
- package/lib/trade/pnl/index.js +4 -7
- package/lib/trade/priceImpact/close/builder.js +1 -2
- package/lib/trade/priceImpact/close/index.js +4 -1
- package/lib/trade/priceImpact/cumulVol/builder.js +18 -10
- package/lib/trade/priceImpact/cumulVol/index.js +21 -16
- package/lib/trade/priceImpact/cumulVol/types.d.ts +11 -0
- package/lib/trade/priceImpact/cumulVol/types.js +2 -0
- package/lib/trade/priceImpact/open/builder.js +1 -2
- package/lib/trade/priceImpact/open/index.js +4 -1
- package/lib/trade/priceImpact/skew/builder.js +2 -3
- package/lib/trade/priceImpact/skew/converter.js +1 -1
- package/lib/trade/priceImpact/skew/fetcher.js +24 -33
- package/package.json +2 -2
- 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
|
@@ -10,15 +10,14 @@ exports.buildCumulVolContext = void 0;
|
|
|
10
10
|
* @returns Cumulative volume context ready for getTradeCumulVolPriceImpactP
|
|
11
11
|
*/
|
|
12
12
|
const buildCumulVolContext = (globalTradingVariables, collateralIndex, pairIndex, additionalParams) => {
|
|
13
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
14
13
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
15
14
|
if (!collateral) {
|
|
16
15
|
return undefined;
|
|
17
16
|
}
|
|
18
17
|
// Get pair-specific data from global variables
|
|
19
|
-
const pairDepth =
|
|
20
|
-
const pairFactor =
|
|
21
|
-
const oiWindows =
|
|
18
|
+
const pairDepth = globalTradingVariables.pairDepths?.[pairIndex];
|
|
19
|
+
const pairFactor = globalTradingVariables.pairFactors?.[pairIndex];
|
|
20
|
+
const oiWindows = globalTradingVariables.oiWindows?.[pairIndex];
|
|
22
21
|
// Get OI windows settings (same for all pairs)
|
|
23
22
|
// OI windows settings from global trading variables are already in SDK format
|
|
24
23
|
const oiWindowsSettings = globalTradingVariables.oiWindowsSettings;
|
|
@@ -26,18 +25,27 @@ const buildCumulVolContext = (globalTradingVariables, collateralIndex, pairIndex
|
|
|
26
25
|
const userPriceImpact = additionalParams.userPriceImpact;
|
|
27
26
|
const protectionCloseFactorWhitelist = additionalParams.protectionCloseFactorWhitelist;
|
|
28
27
|
// Get liquidation params - check both pair and group level
|
|
29
|
-
const liquidationParams =
|
|
30
|
-
|
|
31
|
-
return
|
|
28
|
+
const liquidationParams = globalTradingVariables.liquidationParams?.pairs?.[pairIndex] ||
|
|
29
|
+
globalTradingVariables.liquidationParams?.groups?.[0]; // fallback to first group
|
|
30
|
+
return {
|
|
32
31
|
// Trade state
|
|
33
|
-
isOpen: additionalParams.isOpen,
|
|
32
|
+
isOpen: additionalParams.isOpen,
|
|
33
|
+
isPnlPositive: additionalParams.isPnlPositive,
|
|
34
|
+
createdBlock: additionalParams.createdBlock,
|
|
34
35
|
// Protection factors
|
|
35
|
-
liquidationParams,
|
|
36
|
+
liquidationParams,
|
|
37
|
+
currentBlock: additionalParams.currentBlock,
|
|
38
|
+
contractsVersion: additionalParams.contractsVersion,
|
|
39
|
+
protectionCloseFactorWhitelist,
|
|
36
40
|
// Price impact data
|
|
37
41
|
pairDepth,
|
|
38
42
|
oiWindowsSettings,
|
|
39
43
|
oiWindows,
|
|
40
44
|
// User/collateral specific
|
|
41
|
-
userPriceImpact,
|
|
45
|
+
userPriceImpact,
|
|
46
|
+
collateralPriceUsd: collateral.prices?.collateralPriceUsd || 1,
|
|
47
|
+
// Pair factors (spread across the context)
|
|
48
|
+
...pairFactor,
|
|
49
|
+
};
|
|
42
50
|
};
|
|
43
51
|
exports.buildCumulVolContext = buildCumulVolContext;
|
|
@@ -14,7 +14,6 @@ const types_1 = require("../../../contracts/types");
|
|
|
14
14
|
* @returns Protection close factor (1 = 100%)
|
|
15
15
|
*/
|
|
16
16
|
const getProtectionCloseFactor = (context) => {
|
|
17
|
-
var _a;
|
|
18
17
|
const protectionCloseFactor = context === undefined ||
|
|
19
18
|
context.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
|
|
20
19
|
context.isOpen === undefined ||
|
|
@@ -23,7 +22,7 @@ const getProtectionCloseFactor = (context) => {
|
|
|
23
22
|
(0, exports.isProtectionCloseFactorActive)(context) !== true
|
|
24
23
|
? constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR
|
|
25
24
|
: context.protectionCloseFactor;
|
|
26
|
-
const protectionCloseFactorMultiplier =
|
|
25
|
+
const protectionCloseFactorMultiplier = context?.userPriceImpact?.cumulVolPriceImpactMultiplier !== undefined &&
|
|
27
26
|
context.userPriceImpact.cumulVolPriceImpactMultiplier > 0
|
|
28
27
|
? context.userPriceImpact.cumulVolPriceImpactMultiplier
|
|
29
28
|
: 1;
|
|
@@ -71,7 +70,7 @@ exports.getCumulativeFactor = getCumulativeFactor;
|
|
|
71
70
|
* @returns 1 for pre-v9.2, 2 for v9.2+
|
|
72
71
|
*/
|
|
73
72
|
const getLegacyFactor = (context) => {
|
|
74
|
-
return
|
|
73
|
+
return context?.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
|
|
75
74
|
};
|
|
76
75
|
exports.getLegacyFactor = getLegacyFactor;
|
|
77
76
|
/**
|
|
@@ -88,18 +87,22 @@ exports.getLegacyFactor = getLegacyFactor;
|
|
|
88
87
|
* @returns Cumulative volume price impact percentage (not including spread)
|
|
89
88
|
*/
|
|
90
89
|
const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context) => {
|
|
91
|
-
var _a, _b;
|
|
92
90
|
// Update context with passed parameters
|
|
93
|
-
const updatedContext =
|
|
91
|
+
const updatedContext = {
|
|
92
|
+
...context,
|
|
93
|
+
isOpen: open,
|
|
94
|
+
isPnlPositive: isPnlPositive,
|
|
95
|
+
createdBlock: context.createdBlock || lastPosIncreaseBlock,
|
|
96
|
+
};
|
|
94
97
|
if (
|
|
95
98
|
// No price impact when closing pre-v9.2 trades
|
|
96
|
-
(!open &&
|
|
99
|
+
(!open && context?.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2) ||
|
|
97
100
|
// No price impact for opens when `pair.exemptOnOpen` is true
|
|
98
|
-
(open &&
|
|
101
|
+
(open && context?.exemptOnOpen === true) ||
|
|
99
102
|
// No price impact for closes after `protectionCloseFactor` has expired
|
|
100
103
|
// when `pair.exemptAfterProtectionCloseFactor` is true
|
|
101
104
|
(!open &&
|
|
102
|
-
|
|
105
|
+
context?.exemptAfterProtectionCloseFactor === true &&
|
|
103
106
|
(0, exports.isProtectionCloseFactorActive)(updatedContext) !== true)) {
|
|
104
107
|
return 0;
|
|
105
108
|
}
|
|
@@ -110,8 +113,8 @@ const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterest
|
|
|
110
113
|
// For positive skew (long open or short close), use depth above
|
|
111
114
|
// For negative skew (short open or long close), use depth below
|
|
112
115
|
const onePercentDepth = tradePositiveSkew
|
|
113
|
-
?
|
|
114
|
-
:
|
|
116
|
+
? context.pairDepth?.onePercentDepthAboveUsd
|
|
117
|
+
: context.pairDepth?.onePercentDepthBelowUsd;
|
|
115
118
|
let activeOi = undefined;
|
|
116
119
|
if (context.oiWindowsSettings !== undefined) {
|
|
117
120
|
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long);
|
|
@@ -159,8 +162,7 @@ exports.getFixedSpreadP = getFixedSpreadP;
|
|
|
159
162
|
* Currently it may double-count user fixed spread if pairSpreadP already includes it
|
|
160
163
|
*/
|
|
161
164
|
const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
|
|
162
|
-
|
|
163
|
-
const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
|
|
165
|
+
const fixedSpreadP = userPriceImpact?.fixedSpreadP ?? 0;
|
|
164
166
|
if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
|
|
165
167
|
return 0;
|
|
166
168
|
}
|
|
@@ -190,14 +192,17 @@ const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverag
|
|
|
190
192
|
if (pairSpreadP === undefined) {
|
|
191
193
|
return 0;
|
|
192
194
|
}
|
|
193
|
-
const baseSpread = (0, exports.getSpreadP)(pairSpreadP, undefined, undefined, context
|
|
195
|
+
const baseSpread = (0, exports.getSpreadP)(pairSpreadP, undefined, undefined, context?.userPriceImpact);
|
|
194
196
|
// Calculate position size in USD
|
|
195
|
-
const positionSizeUsd = collateral * leverage * (
|
|
197
|
+
const positionSizeUsd = collateral * leverage * (context?.collateralPriceUsd || 1);
|
|
196
198
|
const cumulVolImpact = (0, exports.getTradeCumulVolPriceImpactP)("", // trader - not used in calculation
|
|
197
199
|
0, // pairIndex - not used in calculation
|
|
198
|
-
buy, positionSizeUsd,
|
|
200
|
+
buy, positionSizeUsd, context?.isPnlPositive || false, context?.isOpen !== false, context?.createdBlock || 0, {
|
|
201
|
+
...context,
|
|
202
|
+
pairDepth,
|
|
199
203
|
oiWindowsSettings,
|
|
200
|
-
oiWindows
|
|
204
|
+
oiWindows,
|
|
205
|
+
});
|
|
201
206
|
// If no depth or OI data, return just half spread
|
|
202
207
|
if (cumulVolImpact === 0 && (!pairDepth || !oiWindowsSettings)) {
|
|
203
208
|
return pairSpreadP / 2;
|
|
@@ -13,7 +13,6 @@ const builder_2 = require("../skew/builder");
|
|
|
13
13
|
* @returns Complete context ready for getTradeOpeningPriceImpact
|
|
14
14
|
*/
|
|
15
15
|
const buildTradeOpeningPriceImpactContext = (globalTradingVariables, collateralIndex, pairIndex, additionalParams) => {
|
|
16
|
-
var _a;
|
|
17
16
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
18
17
|
if (!collateral) {
|
|
19
18
|
return undefined;
|
|
@@ -35,7 +34,7 @@ const buildTradeOpeningPriceImpactContext = (globalTradingVariables, collateralI
|
|
|
35
34
|
}
|
|
36
35
|
// Return structured context with proper subcontexts
|
|
37
36
|
return {
|
|
38
|
-
collateralPriceUsd:
|
|
37
|
+
collateralPriceUsd: collateral.prices?.collateralPriceUsd || 1,
|
|
39
38
|
cumulVolContext,
|
|
40
39
|
skewContext,
|
|
41
40
|
};
|
|
@@ -74,6 +74,9 @@ exports.getTradeOpeningPriceImpact = getTradeOpeningPriceImpact;
|
|
|
74
74
|
* @returns Price impact breakdown and final price
|
|
75
75
|
*/
|
|
76
76
|
const getTradeOpeningPriceImpactAtMarket = (input, context, currentMarketPrice) => {
|
|
77
|
-
return (0, exports.getTradeOpeningPriceImpact)(
|
|
77
|
+
return (0, exports.getTradeOpeningPriceImpact)({
|
|
78
|
+
...input,
|
|
79
|
+
openPrice: currentMarketPrice,
|
|
80
|
+
}, context);
|
|
78
81
|
};
|
|
79
82
|
exports.getTradeOpeningPriceImpactAtMarket = getTradeOpeningPriceImpactAtMarket;
|
|
@@ -11,9 +11,8 @@ exports.buildSkewPriceImpactContext = void 0;
|
|
|
11
11
|
* @returns Skew price impact context for the pair
|
|
12
12
|
*/
|
|
13
13
|
const buildSkewPriceImpactContext = (tradingVariables, pairIndex) => {
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const pairOi = (_c = tradingVariables.pairOis) === null || _c === void 0 ? void 0 : _c[pairIndex];
|
|
14
|
+
const skewDepth = tradingVariables.pairSkewDepths?.[pairIndex] ?? 0;
|
|
15
|
+
const pairOi = tradingVariables.pairOis?.[pairIndex];
|
|
17
16
|
if (!pairOi) {
|
|
18
17
|
throw new Error(`Pair OI data not found for pair index ${pairIndex}`);
|
|
19
18
|
}
|
|
@@ -34,7 +34,7 @@ exports.convertPairOiTokenArray = convertPairOiTokenArray;
|
|
|
34
34
|
* @returns Normalized pair OI collateral data
|
|
35
35
|
*/
|
|
36
36
|
const convertPairOiCollateral = (contractData, collateralDecimals) => {
|
|
37
|
-
const divisor =
|
|
37
|
+
const divisor = 10 ** collateralDecimals;
|
|
38
38
|
return {
|
|
39
39
|
oiLongCollateral: Number(contractData.oiLongCollateral) / divisor,
|
|
40
40
|
oiShortCollateral: Number(contractData.oiShortCollateral) / divisor,
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.calculateTradeSkewPriceImpact = exports.fetchCollateralDecimals = exports.fetchSkewPriceImpactContext = exports.fetchPairSkewDepths = exports.fetchPairSkewDepth = exports.fetchPairOisAfterV10Token = exports.fetchPairOiAfterV10Token = void 0;
|
|
13
4
|
const converter_1 = require("./converter");
|
|
@@ -18,16 +9,16 @@ const converter_1 = require("./converter");
|
|
|
18
9
|
* @param pairIndex Pair index
|
|
19
10
|
* @returns Promise resolving to pair OI in tokens
|
|
20
11
|
*/
|
|
21
|
-
const fetchPairOiAfterV10Token = (contract, collateralIndex, pairIndex) =>
|
|
12
|
+
const fetchPairOiAfterV10Token = async (contract, collateralIndex, pairIndex) => {
|
|
22
13
|
try {
|
|
23
|
-
const contractData =
|
|
14
|
+
const contractData = await contract.getPairOiAfterV10Token(collateralIndex, pairIndex);
|
|
24
15
|
return (0, converter_1.convertPairOiToken)(contractData);
|
|
25
16
|
}
|
|
26
17
|
catch (error) {
|
|
27
18
|
console.error("Error fetching pair OI token:", error);
|
|
28
19
|
throw error;
|
|
29
20
|
}
|
|
30
|
-
}
|
|
21
|
+
};
|
|
31
22
|
exports.fetchPairOiAfterV10Token = fetchPairOiAfterV10Token;
|
|
32
23
|
/**
|
|
33
24
|
* @dev Fetches pair open interest in tokens for multiple pairs
|
|
@@ -36,19 +27,19 @@ exports.fetchPairOiAfterV10Token = fetchPairOiAfterV10Token;
|
|
|
36
27
|
* @param pairIndices Array of pair indices
|
|
37
28
|
* @returns Promise resolving to array of pair OI in tokens
|
|
38
29
|
*/
|
|
39
|
-
const fetchPairOisAfterV10Token = (contract, collateralIndices, pairIndices) =>
|
|
30
|
+
const fetchPairOisAfterV10Token = async (contract, collateralIndices, pairIndices) => {
|
|
40
31
|
if (collateralIndices.length !== pairIndices.length) {
|
|
41
32
|
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
42
33
|
}
|
|
43
34
|
try {
|
|
44
|
-
const contractDataArray =
|
|
35
|
+
const contractDataArray = await contract.getPairOisAfterV10Token(collateralIndices, pairIndices);
|
|
45
36
|
return contractDataArray.map(converter_1.convertPairOiToken);
|
|
46
37
|
}
|
|
47
38
|
catch (error) {
|
|
48
39
|
console.error("Error fetching pair OIs token:", error);
|
|
49
40
|
throw error;
|
|
50
41
|
}
|
|
51
|
-
}
|
|
42
|
+
};
|
|
52
43
|
exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
|
|
53
44
|
/**
|
|
54
45
|
* @dev Fetches skew depth for a specific pair
|
|
@@ -57,9 +48,9 @@ exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
|
|
|
57
48
|
* @param pairIndex Pair index
|
|
58
49
|
* @returns Promise resolving to normalized skew depth
|
|
59
50
|
*/
|
|
60
|
-
const fetchPairSkewDepth = (contract, collateralIndex, pairIndex) =>
|
|
51
|
+
const fetchPairSkewDepth = async (contract, collateralIndex, pairIndex) => {
|
|
61
52
|
try {
|
|
62
|
-
const contractDepth =
|
|
53
|
+
const contractDepth = await contract.getPairSkewDepth(collateralIndex, pairIndex);
|
|
63
54
|
// Token depths are always 1e18 precision
|
|
64
55
|
return (0, converter_1.convertSkewDepth)(contractDepth.toString());
|
|
65
56
|
}
|
|
@@ -67,7 +58,7 @@ const fetchPairSkewDepth = (contract, collateralIndex, pairIndex) => __awaiter(v
|
|
|
67
58
|
console.error("Error fetching skew depth:", error);
|
|
68
59
|
throw error;
|
|
69
60
|
}
|
|
70
|
-
}
|
|
61
|
+
};
|
|
71
62
|
exports.fetchPairSkewDepth = fetchPairSkewDepth;
|
|
72
63
|
/**
|
|
73
64
|
* @dev Fetches skew depths for multiple pairs
|
|
@@ -76,12 +67,12 @@ exports.fetchPairSkewDepth = fetchPairSkewDepth;
|
|
|
76
67
|
* @param pairIndices Array of pair indices
|
|
77
68
|
* @returns Promise resolving to array of normalized skew depths
|
|
78
69
|
*/
|
|
79
|
-
const fetchPairSkewDepths = (contract, collateralIndices, pairIndices) =>
|
|
70
|
+
const fetchPairSkewDepths = async (contract, collateralIndices, pairIndices) => {
|
|
80
71
|
if (collateralIndices.length !== pairIndices.length) {
|
|
81
72
|
throw new Error("All input arrays must have the same length");
|
|
82
73
|
}
|
|
83
74
|
try {
|
|
84
|
-
const contractDepths =
|
|
75
|
+
const contractDepths = await contract.getPairSkewDepths(collateralIndices, pairIndices);
|
|
85
76
|
// Token depths are always 1e18 precision
|
|
86
77
|
return contractDepths.map(depth => (0, converter_1.convertSkewDepth)(depth.toString()));
|
|
87
78
|
}
|
|
@@ -89,7 +80,7 @@ const fetchPairSkewDepths = (contract, collateralIndices, pairIndices) => __awai
|
|
|
89
80
|
console.error("Error fetching skew depths:", error);
|
|
90
81
|
throw error;
|
|
91
82
|
}
|
|
92
|
-
}
|
|
83
|
+
};
|
|
93
84
|
exports.fetchPairSkewDepths = fetchPairSkewDepths;
|
|
94
85
|
/**
|
|
95
86
|
* @dev Fetches skew price impact context for a single pair
|
|
@@ -98,10 +89,10 @@ exports.fetchPairSkewDepths = fetchPairSkewDepths;
|
|
|
98
89
|
* @param pairIndex Pair index
|
|
99
90
|
* @returns Promise resolving to skew price impact context
|
|
100
91
|
*/
|
|
101
|
-
const fetchSkewPriceImpactContext = (contract, collateralIndex, pairIndex) =>
|
|
92
|
+
const fetchSkewPriceImpactContext = async (contract, collateralIndex, pairIndex) => {
|
|
102
93
|
try {
|
|
103
94
|
// Fetch OI data and skew depth in parallel
|
|
104
|
-
const [pairOiToken, skewDepth] =
|
|
95
|
+
const [pairOiToken, skewDepth] = await Promise.all([
|
|
105
96
|
(0, exports.fetchPairOiAfterV10Token)(contract, collateralIndex, pairIndex),
|
|
106
97
|
(0, exports.fetchPairSkewDepth)(contract, collateralIndex, pairIndex),
|
|
107
98
|
]);
|
|
@@ -114,7 +105,7 @@ const fetchSkewPriceImpactContext = (contract, collateralIndex, pairIndex) => __
|
|
|
114
105
|
console.error("Error fetching skew price impact context:", error);
|
|
115
106
|
throw error;
|
|
116
107
|
}
|
|
117
|
-
}
|
|
108
|
+
};
|
|
118
109
|
exports.fetchSkewPriceImpactContext = fetchSkewPriceImpactContext;
|
|
119
110
|
/**
|
|
120
111
|
* @dev Fetches collateral decimals for given collateral indices
|
|
@@ -122,16 +113,16 @@ exports.fetchSkewPriceImpactContext = fetchSkewPriceImpactContext;
|
|
|
122
113
|
* @param collateralIndices Array of collateral indices
|
|
123
114
|
* @returns Promise resolving to array of decimals
|
|
124
115
|
*/
|
|
125
|
-
const fetchCollateralDecimals = (contract, collateralIndices) =>
|
|
116
|
+
const fetchCollateralDecimals = async (contract, collateralIndices) => {
|
|
126
117
|
try {
|
|
127
118
|
// Get unique collateral indices to minimize calls
|
|
128
119
|
const uniqueIndices = [...new Set(collateralIndices)];
|
|
129
120
|
// Fetch collateral info for unique indices
|
|
130
|
-
const promises = uniqueIndices.map((index) =>
|
|
131
|
-
const collateral =
|
|
121
|
+
const promises = uniqueIndices.map(async (index) => {
|
|
122
|
+
const collateral = await contract.getCollateral(index);
|
|
132
123
|
return { index, decimals: Number(collateral.precision) };
|
|
133
|
-
})
|
|
134
|
-
const collateralData =
|
|
124
|
+
});
|
|
125
|
+
const collateralData = await Promise.all(promises);
|
|
135
126
|
// Create a map for quick lookup
|
|
136
127
|
const decimalsMap = new Map(collateralData.map(data => [data.index, data.decimals]));
|
|
137
128
|
// Return decimals in the same order as input
|
|
@@ -142,7 +133,7 @@ const fetchCollateralDecimals = (contract, collateralIndices) => __awaiter(void
|
|
|
142
133
|
console.error("Error fetching collateral decimals:", error);
|
|
143
134
|
throw error;
|
|
144
135
|
}
|
|
145
|
-
}
|
|
136
|
+
};
|
|
146
137
|
exports.fetchCollateralDecimals = fetchCollateralDecimals;
|
|
147
138
|
/**
|
|
148
139
|
* @dev Calculates skew price impact for a trade using contract call
|
|
@@ -154,9 +145,9 @@ exports.fetchCollateralDecimals = fetchCollateralDecimals;
|
|
|
154
145
|
* @param open Whether trade is opening
|
|
155
146
|
* @returns Promise resolving to price impact percentage (1e10)
|
|
156
147
|
*/
|
|
157
|
-
const calculateTradeSkewPriceImpact = (contract, collateralIndex, pairIndex, long, positionSizeToken, open) =>
|
|
148
|
+
const calculateTradeSkewPriceImpact = async (contract, collateralIndex, pairIndex, long, positionSizeToken, open) => {
|
|
158
149
|
try {
|
|
159
|
-
const priceImpactP =
|
|
150
|
+
const priceImpactP = await contract.getTradeSkewPriceImpactP(collateralIndex, pairIndex, long, BigInt(Math.round(positionSizeToken * 1e18)), // Convert to 1e18 precision
|
|
160
151
|
open);
|
|
161
152
|
// Convert from int256 1e10 to percentage
|
|
162
153
|
return Number(priceImpactP) / 1e10;
|
|
@@ -165,5 +156,5 @@ const calculateTradeSkewPriceImpact = (contract, collateralIndex, pairIndex, lon
|
|
|
165
156
|
console.error("Error calculating trade skew price impact:", error);
|
|
166
157
|
throw error;
|
|
167
158
|
}
|
|
168
|
-
}
|
|
159
|
+
};
|
|
169
160
|
exports.calculateTradeSkewPriceImpact = calculateTradeSkewPriceImpact;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gainsnetwork/sdk",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2-rc1",
|
|
4
4
|
"description": "Gains Network SDK",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"url": "git+git@github.com:GainsNetwork-org/sdk.git"
|
|
25
25
|
},
|
|
26
26
|
"engines": {
|
|
27
|
-
"node": ">=
|
|
27
|
+
"node": ">=14.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@typechain/ethers-v5": "^10.1.1",
|
|
@@ -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;
|