@gainsnetwork/sdk 1.3.0-rc3 → 1.3.0-rc6
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 +58 -57
- package/lib/backend/tradingVariables/index.js +6 -7
- 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/utils/borrowingFees.js +9 -20
- package/lib/contracts/utils/openTrades.js +11 -20
- package/lib/contracts/utils/pairs.js +21 -30
- package/lib/markets/forex.js +1 -1
- package/lib/markets/leverage/builder.js +2 -2
- package/lib/markets/price/index.d.ts +1 -0
- package/lib/markets/price/index.js +1 -0
- package/lib/markets/price/types.d.ts +27 -0
- package/lib/pricing/depthBands.d.ts +1 -1
- package/lib/pricing/depthBands.js +7 -5
- 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/tiers/index.js +1 -2
- 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 +20 -10
- package/lib/trade/priceImpact/cumulVol/index.d.ts +3 -3
- package/lib/trade/priceImpact/cumulVol/index.js +47 -20
- package/lib/trade/priceImpact/open/builder.js +1 -2
- package/lib/trade/priceImpact/open/index.js +7 -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
|
@@ -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
|
/**
|
|
@@ -81,6 +80,8 @@ exports.getLegacyFactor = getLegacyFactor;
|
|
|
81
80
|
* @returns Price impact percentage
|
|
82
81
|
*/
|
|
83
82
|
const _calculateDepthBandsPriceImpact = (tradeSizeUsd, depthBandParams) => {
|
|
83
|
+
console.log("tradeSizeUsd", tradeSizeUsd);
|
|
84
|
+
console.log("depthBandParams", depthBandParams);
|
|
84
85
|
const totalDepthUsd = depthBandParams.depthBands.totalDepthUsd;
|
|
85
86
|
if (totalDepthUsd === 0 || tradeSizeUsd === 0)
|
|
86
87
|
return 0;
|
|
@@ -92,38 +93,51 @@ const _calculateDepthBandsPriceImpact = (tradeSizeUsd, depthBandParams) => {
|
|
|
92
93
|
const bandLiquidityPercentageBps = depthBandParams.depthBands.bands[i]; // Already in 0-1 format
|
|
93
94
|
const topOfBandOffsetPpm = depthBandParams.depthBandsMapping.bands[i]; // Already in 0-1 format
|
|
94
95
|
const bandDepthUsd = bandLiquidityPercentageBps * totalDepthUsd;
|
|
96
|
+
console.log("bandDepthUsd", bandDepthUsd);
|
|
95
97
|
// Skip if band has same depth as previous (would cause division by zero)
|
|
96
98
|
if (bandDepthUsd <= prevBandDepthUsd) {
|
|
99
|
+
console.log("Skipping band");
|
|
97
100
|
prevBandDepthUsd = bandDepthUsd;
|
|
98
101
|
topOfPrevBandOffsetPpm = topOfBandOffsetPpm;
|
|
99
102
|
continue;
|
|
100
103
|
}
|
|
101
104
|
// Since bandDepthUsd represents liquidity from mid price to top of band, we need to subtract previous band depth
|
|
102
105
|
const bandAvailableDepthUsd = bandDepthUsd - prevBandDepthUsd;
|
|
106
|
+
console.log("bandAvailableDepthUsd", bandAvailableDepthUsd);
|
|
103
107
|
let depthConsumedUsd;
|
|
104
108
|
// At 100% band always consume all remaining size, even if more than band available depth
|
|
105
109
|
if (bandLiquidityPercentageBps === 1 ||
|
|
106
110
|
remainingSizeUsd <= bandAvailableDepthUsd) {
|
|
107
111
|
depthConsumedUsd = remainingSizeUsd;
|
|
108
112
|
remainingSizeUsd = 0;
|
|
113
|
+
console.log("Consumed all remaining size");
|
|
109
114
|
}
|
|
110
115
|
else {
|
|
111
116
|
// Normal case: consume entire band and continue to next
|
|
112
117
|
depthConsumedUsd = bandAvailableDepthUsd;
|
|
113
118
|
remainingSizeUsd -= bandAvailableDepthUsd;
|
|
119
|
+
console.log("Consumed entire band");
|
|
114
120
|
}
|
|
115
121
|
// Calculate impact contribution from this band using trapezoidal rule
|
|
116
122
|
// Low = previous band's price offset, High = current band's price offset
|
|
117
123
|
const lowOffsetP = topOfPrevBandOffsetPpm;
|
|
118
124
|
const offsetRangeP = topOfBandOffsetPpm - topOfPrevBandOffsetPpm;
|
|
125
|
+
console.log("lowOffsetP", lowOffsetP);
|
|
126
|
+
console.log("offsetRangeP", offsetRangeP);
|
|
127
|
+
console.log("depthConsumedUsd", depthConsumedUsd);
|
|
128
|
+
console.log("bandAvailableDepthUsd", bandAvailableDepthUsd);
|
|
119
129
|
// Calculate average impact using trapezoidal rule: low + (range * fraction / 2)
|
|
120
130
|
const avgImpactP = lowOffsetP +
|
|
121
131
|
(offsetRangeP * depthConsumedUsd) / bandAvailableDepthUsd / 2;
|
|
132
|
+
console.log("avgImpactP", avgImpactP);
|
|
122
133
|
totalWeightedPriceImpactP += avgImpactP * depthConsumedUsd;
|
|
134
|
+
console.log("totalWeightedPriceImpactP", totalWeightedPriceImpactP);
|
|
123
135
|
// Update previous values for next iteration
|
|
124
136
|
topOfPrevBandOffsetPpm = topOfBandOffsetPpm;
|
|
125
137
|
prevBandDepthUsd = bandDepthUsd;
|
|
126
138
|
}
|
|
139
|
+
console.log("totalWeightedPriceImpactP", totalWeightedPriceImpactP);
|
|
140
|
+
console.log("tradeSizeUsd", tradeSizeUsd);
|
|
127
141
|
return totalWeightedPriceImpactP / tradeSizeUsd;
|
|
128
142
|
};
|
|
129
143
|
/**
|
|
@@ -136,7 +150,7 @@ const _calculateDepthBandsPriceImpact = (tradeSizeUsd, depthBandParams) => {
|
|
|
136
150
|
* @returns Price impact percentage (can be negative)
|
|
137
151
|
*/
|
|
138
152
|
const _getDepthBandsPriceImpactP = (cumulativeVolumeUsd, tradeSizeUsd, depthBandParams, priceImpactFactor, cumulativeFactor) => {
|
|
139
|
-
// Check for opposite signs
|
|
153
|
+
// Check for opposite signs
|
|
140
154
|
if ((cumulativeVolumeUsd > 0 && tradeSizeUsd < 0) ||
|
|
141
155
|
(cumulativeVolumeUsd < 0 && tradeSizeUsd > 0)) {
|
|
142
156
|
throw new Error("Wrong params: cumulative volume and trade size have opposite signs");
|
|
@@ -150,8 +164,12 @@ const _getDepthBandsPriceImpactP = (cumulativeVolumeUsd, tradeSizeUsd, depthBand
|
|
|
150
164
|
const totalSizeLookupUsdUint = isNegative
|
|
151
165
|
? -totalSizeLookupUsd
|
|
152
166
|
: totalSizeLookupUsd;
|
|
167
|
+
console.log("effectiveCumulativeVolumeUsdUint", effectiveCumulativeVolumeUsdUint);
|
|
168
|
+
console.log("totalSizeLookupUsdUint", totalSizeLookupUsdUint);
|
|
153
169
|
const cumulativeVolPriceImpactP = _calculateDepthBandsPriceImpact(effectiveCumulativeVolumeUsdUint, depthBandParams);
|
|
154
170
|
const totalSizePriceImpactP = _calculateDepthBandsPriceImpact(totalSizeLookupUsdUint, depthBandParams);
|
|
171
|
+
console.log("cumulativeVolPriceImpactP", cumulativeVolPriceImpactP);
|
|
172
|
+
console.log("totalSizePriceImpactP", totalSizePriceImpactP);
|
|
155
173
|
const unscaledPriceImpactP = cumulativeVolPriceImpactP +
|
|
156
174
|
(totalSizePriceImpactP - cumulativeVolPriceImpactP) / 2;
|
|
157
175
|
const scaledPriceImpactP = unscaledPriceImpactP * priceImpactFactor;
|
|
@@ -170,24 +188,35 @@ const _getDepthBandsPriceImpactP = (cumulativeVolumeUsd, tradeSizeUsd, depthBand
|
|
|
170
188
|
* @param context Additional context with depths, OI data, and factors
|
|
171
189
|
* @returns Cumulative volume price impact percentage (not including spread)
|
|
172
190
|
*/
|
|
173
|
-
const getTradeCumulVolPriceImpactP = (
|
|
191
|
+
const getTradeCumulVolPriceImpactP = (_trader, // Unused - kept for compatibility
|
|
192
|
+
_pairIndex, // Unused - kept for compatibility
|
|
193
|
+
long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context) => {
|
|
174
194
|
// Update context with passed parameters
|
|
175
|
-
const updatedContext =
|
|
195
|
+
const updatedContext = {
|
|
196
|
+
...context,
|
|
197
|
+
isOpen: open,
|
|
198
|
+
isPnlPositive: isPnlPositive,
|
|
199
|
+
createdBlock: context.createdBlock || lastPosIncreaseBlock,
|
|
200
|
+
};
|
|
176
201
|
if (
|
|
177
202
|
// No price impact when closing pre-v9.2 trades
|
|
178
|
-
(!open &&
|
|
203
|
+
(!open && context?.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2) ||
|
|
179
204
|
// No price impact for opens when `pair.exemptOnOpen` is true
|
|
180
|
-
(open &&
|
|
205
|
+
(open && context?.exemptOnOpen === true) ||
|
|
181
206
|
// No price impact for closes after `protectionCloseFactor` has expired
|
|
182
207
|
// when `pair.exemptAfterProtectionCloseFactor` is true
|
|
183
208
|
(!open &&
|
|
184
|
-
|
|
209
|
+
context?.exemptAfterProtectionCloseFactor === true &&
|
|
185
210
|
(0, exports.isProtectionCloseFactorActive)(updatedContext) !== true)) {
|
|
211
|
+
console.log("No price impact, protection close factor");
|
|
186
212
|
return 0;
|
|
187
213
|
}
|
|
188
214
|
const tradePositiveSkew = (long && open) || (!long && !open);
|
|
189
215
|
const tradeSkewMultiplier = tradePositiveSkew ? 1 : -1;
|
|
216
|
+
console.log("bands", context.pairDepthBands);
|
|
217
|
+
console.log("mapping", context.depthBandsMapping);
|
|
190
218
|
if (!context.pairDepthBands || !context.depthBandsMapping) {
|
|
219
|
+
console.log("No price impact, bands");
|
|
191
220
|
return 0;
|
|
192
221
|
}
|
|
193
222
|
// Select depth bands based on trade direction
|
|
@@ -196,6 +225,7 @@ const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterest
|
|
|
196
225
|
: context.pairDepthBands.below;
|
|
197
226
|
// Return 0 if no depth bands configured (matching contract lines 588-590)
|
|
198
227
|
if (!depthBands || depthBands.totalDepthUsd === 0) {
|
|
228
|
+
console.log("No price impact, depth bands");
|
|
199
229
|
return 0;
|
|
200
230
|
}
|
|
201
231
|
// Get active OI for cumulative volume calculation
|
|
@@ -204,8 +234,11 @@ const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterest
|
|
|
204
234
|
activeOi =
|
|
205
235
|
(0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long) || 0;
|
|
206
236
|
}
|
|
237
|
+
console.log("activeOi", activeOi);
|
|
207
238
|
const signedActiveOi = activeOi * tradeSkewMultiplier;
|
|
208
239
|
const signedTradeOi = tradeOpenInterestUsd * tradeSkewMultiplier;
|
|
240
|
+
console.log("signedActiveOi", signedActiveOi);
|
|
241
|
+
console.log("signedTradeOi", signedTradeOi);
|
|
209
242
|
// Calculate price impact using depth bands
|
|
210
243
|
const priceImpactP = _getDepthBandsPriceImpactP(signedActiveOi, signedTradeOi, {
|
|
211
244
|
depthBands: depthBands,
|
|
@@ -242,8 +275,7 @@ exports.getFixedSpreadP = getFixedSpreadP;
|
|
|
242
275
|
* Currently it may double-count user fixed spread if pairSpreadP already includes it
|
|
243
276
|
*/
|
|
244
277
|
const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
|
|
245
|
-
|
|
246
|
-
const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
|
|
278
|
+
const fixedSpreadP = userPriceImpact?.fixedSpreadP ?? 0;
|
|
247
279
|
if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
|
|
248
280
|
return 0;
|
|
249
281
|
}
|
|
@@ -268,21 +300,16 @@ exports.getSpreadP = getSpreadP;
|
|
|
268
300
|
* @param context Additional context for the calculation
|
|
269
301
|
* @returns Total spread + cumulative volume price impact percentage
|
|
270
302
|
*/
|
|
271
|
-
const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverage,
|
|
303
|
+
const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverage, context) => {
|
|
272
304
|
if (pairSpreadP === undefined) {
|
|
273
305
|
return 0;
|
|
274
306
|
}
|
|
275
|
-
const baseSpread = (0, exports.getSpreadP)(pairSpreadP, undefined, undefined, context
|
|
307
|
+
const baseSpread = (0, exports.getSpreadP)(pairSpreadP, undefined, undefined, context?.userPriceImpact);
|
|
276
308
|
// Calculate position size in USD
|
|
277
|
-
const positionSizeUsd = collateral * leverage * (
|
|
309
|
+
const positionSizeUsd = collateral * leverage * (context?.collateralPriceUsd || 1);
|
|
278
310
|
const cumulVolImpact = (0, exports.getTradeCumulVolPriceImpactP)("", // trader - not used in calculation
|
|
279
311
|
0, // pairIndex - not used in calculation
|
|
280
|
-
buy, positionSizeUsd,
|
|
281
|
-
oiWindows }));
|
|
282
|
-
// If no cumulative volume impact calculated, return half of base spread
|
|
283
|
-
if (cumulVolImpact === 0 && !oiWindowsSettings) {
|
|
284
|
-
return pairSpreadP / 2;
|
|
285
|
-
}
|
|
312
|
+
buy, positionSizeUsd, context?.isPnlPositive || false, context?.isOpen !== false, context?.createdBlock || 0, context);
|
|
286
313
|
return baseSpread + cumulVolImpact;
|
|
287
314
|
};
|
|
288
315
|
exports.getSpreadWithCumulVolPriceImpactP = getSpreadWithCumulVolPriceImpactP;
|
|
@@ -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
|
};
|
|
@@ -23,14 +23,17 @@ const getTradeOpeningPriceImpact = (input, context) => {
|
|
|
23
23
|
// Calculate fixed spread
|
|
24
24
|
const spreadP = (0, cumulVol_1.getFixedSpreadP)(input.pairSpreadP, input.long, true // opening
|
|
25
25
|
);
|
|
26
|
+
console.log("spreadP", spreadP);
|
|
26
27
|
// Calculate position size in USD
|
|
27
28
|
const positionSizeUsd = positionSizeCollateral * context.collateralPriceUsd;
|
|
29
|
+
console.log("positionSizeUsd", positionSizeUsd);
|
|
28
30
|
// Calculate cumulative volume price impact
|
|
29
31
|
const cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)("", // trader - not needed for calculation
|
|
30
32
|
input.pairIndex, input.long, positionSizeUsd, false, // isPnlPositive - not relevant for opening
|
|
31
33
|
true, // open
|
|
32
34
|
0, // lastPosIncreaseBlock - not relevant for opening
|
|
33
35
|
context.cumulVolContext);
|
|
36
|
+
console.log("cumulVolPriceImpactP", cumulVolPriceImpactP);
|
|
34
37
|
// Calculate price after spread and cumulative volume impact (before skew)
|
|
35
38
|
const priceAfterSpreadAndCumulVolPriceImpact = (0, __1.getPriceAfterImpact)(input.openPrice, spreadP + cumulVolPriceImpactP);
|
|
36
39
|
// Calculate position size in tokens using the price after fixed spread and cumul vol impact
|
|
@@ -74,6 +77,9 @@ exports.getTradeOpeningPriceImpact = getTradeOpeningPriceImpact;
|
|
|
74
77
|
* @returns Price impact breakdown and final price
|
|
75
78
|
*/
|
|
76
79
|
const getTradeOpeningPriceImpactAtMarket = (input, context, currentMarketPrice) => {
|
|
77
|
-
return (0, exports.getTradeOpeningPriceImpact)(
|
|
80
|
+
return (0, exports.getTradeOpeningPriceImpact)({
|
|
81
|
+
...input,
|
|
82
|
+
openPrice: currentMarketPrice,
|
|
83
|
+
}, context);
|
|
78
84
|
};
|
|
79
85
|
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.3.0-
|
|
3
|
+
"version": "1.3.0-rc6",
|
|
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",
|