@gainsnetwork/sdk 1.0.6-rc4 → 1.1.0-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/backend.types.d.ts +11 -4
- package/lib/backend/tradingVariables/converter.d.ts +7 -3
- package/lib/backend/tradingVariables/converter.js +70 -63
- package/lib/backend/tradingVariables/index.js +11 -7
- package/lib/backend/tradingVariables/types.d.ts +4 -2
- package/lib/contracts/addresses.js +1 -4
- package/lib/contracts/index.d.ts +1 -1
- package/lib/contracts/index.js +3 -3
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +627 -305
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +1949 -257
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +0 -7
- package/lib/contracts/types/generated/factories/GToken__factory.js +0 -4
- package/lib/contracts/utils/borrowingFees.js +20 -9
- package/lib/contracts/utils/openTrades.js +20 -11
- package/lib/contracts/utils/pairs.d.ts +13 -2
- package/lib/contracts/utils/pairs.js +89 -21
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/markets/forex.js +1 -1
- package/lib/markets/leverage/builder.js +2 -2
- package/lib/markets/price/index.d.ts +0 -1
- package/lib/markets/price/index.js +0 -1
- package/lib/markets/price/types.d.ts +0 -27
- 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 +92 -0
- package/lib/pricing/index.d.ts +4 -0
- package/lib/pricing/index.js +20 -0
- package/lib/trade/fees/borrowing/builder.js +3 -2
- package/lib/trade/fees/borrowing/converter.js +1 -5
- package/lib/trade/fees/borrowing/index.js +5 -5
- package/lib/trade/fees/borrowingV2/builder.js +4 -3
- package/lib/trade/fees/borrowingV2/converter.js +1 -1
- package/lib/trade/fees/borrowingV2/fetcher.js +32 -26
- package/lib/trade/fees/borrowingV2/index.js +3 -3
- package/lib/trade/fees/converter.js +22 -22
- package/lib/trade/fees/fundingFees/builder.js +7 -6
- package/lib/trade/fees/fundingFees/converter.js +1 -1
- package/lib/trade/fees/fundingFees/fetcher.js +25 -16
- package/lib/trade/fees/fundingFees/index.js +3 -2
- package/lib/trade/fees/tiers/index.js +2 -1
- package/lib/trade/fees/trading/index.js +3 -5
- package/lib/trade/liquidation/builder.js +3 -6
- package/lib/trade/liquidation/index.js +6 -4
- package/lib/trade/oiWindows.js +2 -1
- package/lib/trade/pnl/builder.js +2 -1
- package/lib/trade/pnl/converter.js +1 -1
- package/lib/trade/pnl/index.js +7 -4
- package/lib/trade/priceImpact/close/builder.js +2 -1
- package/lib/trade/priceImpact/close/index.js +1 -4
- package/lib/trade/priceImpact/cumulVol/builder.js +11 -18
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +63 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +97 -1
- package/lib/trade/priceImpact/cumulVol/index.d.ts +3 -0
- package/lib/trade/priceImpact/cumulVol/index.js +123 -25
- 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 +2 -1
- package/lib/trade/priceImpact/open/index.js +1 -4
- package/lib/trade/priceImpact/skew/builder.js +3 -2
- package/lib/trade/priceImpact/skew/converter.js +1 -1
- package/lib/trade/priceImpact/skew/fetcher.js +33 -24
- package/package.json +2 -2
|
@@ -14,6 +14,7 @@ const types_1 = require("../../../contracts/types");
|
|
|
14
14
|
* @returns Protection close factor (1 = 100%)
|
|
15
15
|
*/
|
|
16
16
|
const getProtectionCloseFactor = (context) => {
|
|
17
|
+
var _a;
|
|
17
18
|
const protectionCloseFactor = context === undefined ||
|
|
18
19
|
context.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
|
|
19
20
|
context.isOpen === undefined ||
|
|
@@ -22,7 +23,7 @@ const getProtectionCloseFactor = (context) => {
|
|
|
22
23
|
(0, exports.isProtectionCloseFactorActive)(context) !== true
|
|
23
24
|
? constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR
|
|
24
25
|
: context.protectionCloseFactor;
|
|
25
|
-
const protectionCloseFactorMultiplier = context
|
|
26
|
+
const protectionCloseFactorMultiplier = ((_a = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _a === void 0 ? void 0 : _a.cumulVolPriceImpactMultiplier) !== undefined &&
|
|
26
27
|
context.userPriceImpact.cumulVolPriceImpactMultiplier > 0
|
|
27
28
|
? context.userPriceImpact.cumulVolPriceImpactMultiplier
|
|
28
29
|
: 1;
|
|
@@ -70,9 +71,92 @@ exports.getCumulativeFactor = getCumulativeFactor;
|
|
|
70
71
|
* @returns 1 for pre-v9.2, 2 for v9.2+
|
|
71
72
|
*/
|
|
72
73
|
const getLegacyFactor = (context) => {
|
|
73
|
-
return context
|
|
74
|
+
return (context === null || context === void 0 ? void 0 : context.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
|
|
74
75
|
};
|
|
75
76
|
exports.getLegacyFactor = getLegacyFactor;
|
|
77
|
+
/**
|
|
78
|
+
* @dev Mirrors contract's _calculateDepthBandsPriceImpact function
|
|
79
|
+
* @param tradeSizeUsd Trade size in USD (always positive here)
|
|
80
|
+
* @param depthBandParams Depth band parameters
|
|
81
|
+
* @returns Price impact percentage
|
|
82
|
+
*/
|
|
83
|
+
const _calculateDepthBandsPriceImpact = (tradeSizeUsd, depthBandParams) => {
|
|
84
|
+
const totalDepthUsd = depthBandParams.pairSlot1.totalDepthUsd;
|
|
85
|
+
if (totalDepthUsd === 0 || tradeSizeUsd === 0)
|
|
86
|
+
return 0;
|
|
87
|
+
let remainingSizeUsd = tradeSizeUsd;
|
|
88
|
+
let totalWeightedPriceImpactP = 0;
|
|
89
|
+
let prevBandDepthUsd = 0;
|
|
90
|
+
let topOfPrevBandOffsetPpm = 0;
|
|
91
|
+
for (let i = 0; i < 30 && remainingSizeUsd !== 0; i++) {
|
|
92
|
+
const bandLiquidityPercentageBps = depthBandParams.pairSlot1.bands[i]; // Already in 0-1 format
|
|
93
|
+
const topOfBandOffsetPpm = depthBandParams.mappingSlot1.bands[i]; // Already in 0-1 format
|
|
94
|
+
const bandDepthUsd = bandLiquidityPercentageBps * totalDepthUsd;
|
|
95
|
+
// Skip if band has same depth as previous (would cause division by zero)
|
|
96
|
+
if (bandDepthUsd <= prevBandDepthUsd) {
|
|
97
|
+
prevBandDepthUsd = bandDepthUsd;
|
|
98
|
+
topOfPrevBandOffsetPpm = topOfBandOffsetPpm;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
// Since bandDepthUsd represents liquidity from mid price to top of band, we need to subtract previous band depth
|
|
102
|
+
const bandAvailableDepthUsd = bandDepthUsd - prevBandDepthUsd;
|
|
103
|
+
let depthConsumedUsd;
|
|
104
|
+
// At 100% band always consume all remaining size, even if more than band available depth
|
|
105
|
+
if (bandLiquidityPercentageBps === 1 ||
|
|
106
|
+
remainingSizeUsd <= bandAvailableDepthUsd) {
|
|
107
|
+
depthConsumedUsd = remainingSizeUsd;
|
|
108
|
+
remainingSizeUsd = 0;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
// Normal case: consume entire band and continue to next
|
|
112
|
+
depthConsumedUsd = bandAvailableDepthUsd;
|
|
113
|
+
remainingSizeUsd -= bandAvailableDepthUsd;
|
|
114
|
+
}
|
|
115
|
+
// Calculate impact contribution from this band using trapezoidal rule
|
|
116
|
+
// Low = previous band's price offset, High = current band's price offset
|
|
117
|
+
const lowOffsetP = topOfPrevBandOffsetPpm;
|
|
118
|
+
const offsetRangeP = topOfBandOffsetPpm - topOfPrevBandOffsetPpm;
|
|
119
|
+
// Calculate average impact using trapezoidal rule: low + (range * fraction / 2)
|
|
120
|
+
const avgImpactP = lowOffsetP +
|
|
121
|
+
(offsetRangeP * depthConsumedUsd) / bandAvailableDepthUsd / 2;
|
|
122
|
+
totalWeightedPriceImpactP += avgImpactP * depthConsumedUsd;
|
|
123
|
+
// Update previous values for next iteration
|
|
124
|
+
topOfPrevBandOffsetPpm = topOfBandOffsetPpm;
|
|
125
|
+
prevBandDepthUsd = bandDepthUsd;
|
|
126
|
+
}
|
|
127
|
+
return totalWeightedPriceImpactP / tradeSizeUsd;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* @dev Mirrors contract's _getDepthBandsPriceImpactP function
|
|
131
|
+
* @param cumulativeVolumeUsd Cumulative volume in USD (can be negative)
|
|
132
|
+
* @param tradeSizeUsd Trade size in USD (can be negative)
|
|
133
|
+
* @param depthBandParams Depth band parameters (contains both pair bands and global mapping)
|
|
134
|
+
* @param priceImpactFactor Price impact factor (protection close factor)
|
|
135
|
+
* @param cumulativeFactor Cumulative factor for volume impact
|
|
136
|
+
* @returns Price impact percentage (can be negative)
|
|
137
|
+
*/
|
|
138
|
+
const _getDepthBandsPriceImpactP = (cumulativeVolumeUsd, tradeSizeUsd, depthBandParams, priceImpactFactor, cumulativeFactor) => {
|
|
139
|
+
// Check for opposite signs (would revert in contract)
|
|
140
|
+
if ((cumulativeVolumeUsd > 0 && tradeSizeUsd < 0) ||
|
|
141
|
+
(cumulativeVolumeUsd < 0 && tradeSizeUsd > 0)) {
|
|
142
|
+
throw new Error("Wrong params: cumulative volume and trade size have opposite signs");
|
|
143
|
+
}
|
|
144
|
+
const effectiveCumulativeVolumeUsd = cumulativeVolumeUsd * cumulativeFactor;
|
|
145
|
+
const totalSizeLookupUsd = effectiveCumulativeVolumeUsd + tradeSizeUsd;
|
|
146
|
+
const isNegative = totalSizeLookupUsd < 0;
|
|
147
|
+
const effectiveCumulativeVolumeUsdUint = isNegative
|
|
148
|
+
? -effectiveCumulativeVolumeUsd
|
|
149
|
+
: effectiveCumulativeVolumeUsd;
|
|
150
|
+
const totalSizeLookupUsdUint = isNegative
|
|
151
|
+
? -totalSizeLookupUsd
|
|
152
|
+
: totalSizeLookupUsd;
|
|
153
|
+
const cumulativeVolPriceImpactP = _calculateDepthBandsPriceImpact(effectiveCumulativeVolumeUsdUint, depthBandParams);
|
|
154
|
+
const totalSizePriceImpactP = _calculateDepthBandsPriceImpact(totalSizeLookupUsdUint, depthBandParams);
|
|
155
|
+
const unscaledPriceImpactP = cumulativeVolPriceImpactP +
|
|
156
|
+
(totalSizePriceImpactP - cumulativeVolPriceImpactP) / 2;
|
|
157
|
+
const scaledPriceImpactP = unscaledPriceImpactP * priceImpactFactor;
|
|
158
|
+
return isNegative ? -scaledPriceImpactP : scaledPriceImpactP;
|
|
159
|
+
};
|
|
76
160
|
/**
|
|
77
161
|
* @dev Calculates cumulative volume price impact percentage
|
|
78
162
|
* @dev Mirrors contract's getTradeCumulVolPriceImpactP function
|
|
@@ -87,34 +171,50 @@ exports.getLegacyFactor = getLegacyFactor;
|
|
|
87
171
|
* @returns Cumulative volume price impact percentage (not including spread)
|
|
88
172
|
*/
|
|
89
173
|
const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context) => {
|
|
174
|
+
var _a, _b;
|
|
90
175
|
// Update context with passed parameters
|
|
91
|
-
const updatedContext = {
|
|
92
|
-
...context,
|
|
93
|
-
isOpen: open,
|
|
94
|
-
isPnlPositive: isPnlPositive,
|
|
95
|
-
createdBlock: context.createdBlock || lastPosIncreaseBlock,
|
|
96
|
-
};
|
|
176
|
+
const updatedContext = Object.assign(Object.assign({}, context), { isOpen: open, isPnlPositive: isPnlPositive, createdBlock: context.createdBlock || lastPosIncreaseBlock });
|
|
97
177
|
if (
|
|
98
178
|
// No price impact when closing pre-v9.2 trades
|
|
99
|
-
(!open && context
|
|
179
|
+
(!open && (context === null || context === void 0 ? void 0 : context.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) ||
|
|
100
180
|
// No price impact for opens when `pair.exemptOnOpen` is true
|
|
101
|
-
(open && context
|
|
181
|
+
(open && (context === null || context === void 0 ? void 0 : context.exemptOnOpen) === true) ||
|
|
102
182
|
// No price impact for closes after `protectionCloseFactor` has expired
|
|
103
183
|
// when `pair.exemptAfterProtectionCloseFactor` is true
|
|
104
184
|
(!open &&
|
|
105
|
-
context
|
|
185
|
+
(context === null || context === void 0 ? void 0 : context.exemptAfterProtectionCloseFactor) === true &&
|
|
106
186
|
(0, exports.isProtectionCloseFactorActive)(updatedContext) !== true)) {
|
|
107
187
|
return 0;
|
|
108
188
|
}
|
|
109
|
-
// Calculate trade skew direction (matches Solidity logic)
|
|
110
189
|
const tradePositiveSkew = (long && open) || (!long && !open);
|
|
111
190
|
const tradeSkewMultiplier = tradePositiveSkew ? 1 : -1;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
191
|
+
if (context.pairDepthBands && context.depthBandsMapping) {
|
|
192
|
+
// Select depth bands based on trade direction
|
|
193
|
+
const depthBands = tradePositiveSkew
|
|
194
|
+
? context.pairDepthBands.above
|
|
195
|
+
: context.pairDepthBands.below;
|
|
196
|
+
if (depthBands && depthBands.totalDepthUsd > 0) {
|
|
197
|
+
// Get active OI for cumulative volume calculation
|
|
198
|
+
let activeOi = 0;
|
|
199
|
+
if (context.oiWindowsSettings !== undefined) {
|
|
200
|
+
activeOi =
|
|
201
|
+
(0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long) || 0;
|
|
202
|
+
}
|
|
203
|
+
const signedActiveOi = activeOi * tradeSkewMultiplier;
|
|
204
|
+
const signedTradeOi = tradeOpenInterestUsd * tradeSkewMultiplier;
|
|
205
|
+
// Calculate price impact using depth bands
|
|
206
|
+
const priceImpactP = _getDepthBandsPriceImpactP(signedActiveOi, signedTradeOi, {
|
|
207
|
+
pairSlot1: depthBands,
|
|
208
|
+
mappingSlot1: context.depthBandsMapping,
|
|
209
|
+
}, (0, exports.getProtectionCloseFactor)(updatedContext), (0, exports.getCumulativeFactor)(updatedContext));
|
|
210
|
+
return priceImpactP;
|
|
211
|
+
}
|
|
212
|
+
return 0;
|
|
213
|
+
}
|
|
214
|
+
// Fall back to legacy calculation for pre-v10.2 contracts
|
|
115
215
|
const onePercentDepth = tradePositiveSkew
|
|
116
|
-
? context.pairDepth
|
|
117
|
-
: context.pairDepth
|
|
216
|
+
? (_a = context.pairDepth) === null || _a === void 0 ? void 0 : _a.onePercentDepthAboveUsd
|
|
217
|
+
: (_b = context.pairDepth) === null || _b === void 0 ? void 0 : _b.onePercentDepthBelowUsd;
|
|
118
218
|
let activeOi = undefined;
|
|
119
219
|
if (context.oiWindowsSettings !== undefined) {
|
|
120
220
|
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long);
|
|
@@ -162,7 +262,8 @@ exports.getFixedSpreadP = getFixedSpreadP;
|
|
|
162
262
|
* Currently it may double-count user fixed spread if pairSpreadP already includes it
|
|
163
263
|
*/
|
|
164
264
|
const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
|
|
165
|
-
|
|
265
|
+
var _a;
|
|
266
|
+
const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
|
|
166
267
|
if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
|
|
167
268
|
return 0;
|
|
168
269
|
}
|
|
@@ -192,17 +293,14 @@ const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverag
|
|
|
192
293
|
if (pairSpreadP === undefined) {
|
|
193
294
|
return 0;
|
|
194
295
|
}
|
|
195
|
-
const baseSpread = (0, exports.getSpreadP)(pairSpreadP, undefined, undefined, context
|
|
296
|
+
const baseSpread = (0, exports.getSpreadP)(pairSpreadP, undefined, undefined, context === null || context === void 0 ? void 0 : context.userPriceImpact);
|
|
196
297
|
// Calculate position size in USD
|
|
197
|
-
const positionSizeUsd = collateral * leverage * (context
|
|
298
|
+
const positionSizeUsd = collateral * leverage * ((context === null || context === void 0 ? void 0 : context.collateralPriceUsd) || 1);
|
|
198
299
|
const cumulVolImpact = (0, exports.getTradeCumulVolPriceImpactP)("", // trader - not used in calculation
|
|
199
300
|
0, // pairIndex - not used in calculation
|
|
200
|
-
buy, positionSizeUsd, context
|
|
201
|
-
...context,
|
|
202
|
-
pairDepth,
|
|
301
|
+
buy, positionSizeUsd, (context === null || context === void 0 ? void 0 : context.isPnlPositive) || false, (context === null || context === void 0 ? void 0 : context.isOpen) !== false, (context === null || context === void 0 ? void 0 : context.createdBlock) || 0, Object.assign(Object.assign({}, context), { pairDepth,
|
|
203
302
|
oiWindowsSettings,
|
|
204
|
-
oiWindows
|
|
205
|
-
});
|
|
303
|
+
oiWindows }));
|
|
206
304
|
// If no depth or OI data, return just half spread
|
|
207
305
|
if (cumulVolImpact === 0 && (!pairDepth || !oiWindowsSettings)) {
|
|
208
306
|
return pairSpreadP / 2;
|
|
@@ -13,6 +13,7 @@ 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;
|
|
16
17
|
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
17
18
|
if (!collateral) {
|
|
18
19
|
return undefined;
|
|
@@ -34,7 +35,7 @@ const buildTradeOpeningPriceImpactContext = (globalTradingVariables, collateralI
|
|
|
34
35
|
}
|
|
35
36
|
// Return structured context with proper subcontexts
|
|
36
37
|
return {
|
|
37
|
-
collateralPriceUsd: collateral.prices
|
|
38
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
38
39
|
cumulVolContext,
|
|
39
40
|
skewContext,
|
|
40
41
|
};
|
|
@@ -74,9 +74,6 @@ 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)({
|
|
78
|
-
...input,
|
|
79
|
-
openPrice: currentMarketPrice,
|
|
80
|
-
}, context);
|
|
77
|
+
return (0, exports.getTradeOpeningPriceImpact)(Object.assign(Object.assign({}, input), { openPrice: currentMarketPrice }), context);
|
|
81
78
|
};
|
|
82
79
|
exports.getTradeOpeningPriceImpactAtMarket = getTradeOpeningPriceImpactAtMarket;
|
|
@@ -11,8 +11,9 @@ 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
|
|
14
|
+
var _a, _b, _c;
|
|
15
|
+
const skewDepth = (_b = (_a = tradingVariables.pairSkewDepths) === null || _a === void 0 ? void 0 : _a[pairIndex]) !== null && _b !== void 0 ? _b : 0;
|
|
16
|
+
const pairOi = (_c = tradingVariables.pairOis) === null || _c === void 0 ? void 0 : _c[pairIndex];
|
|
16
17
|
if (!pairOi) {
|
|
17
18
|
throw new Error(`Pair OI data not found for pair index ${pairIndex}`);
|
|
18
19
|
}
|
|
@@ -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 = 10
|
|
37
|
+
const divisor = Math.pow(10, collateralDecimals);
|
|
38
38
|
return {
|
|
39
39
|
oiLongCollateral: Number(contractData.oiLongCollateral) / divisor,
|
|
40
40
|
oiShortCollateral: Number(contractData.oiShortCollateral) / divisor,
|
|
@@ -1,4 +1,13 @@
|
|
|
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
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.calculateTradeSkewPriceImpact = exports.fetchCollateralDecimals = exports.fetchSkewPriceImpactContext = exports.fetchPairSkewDepths = exports.fetchPairSkewDepth = exports.fetchPairOisAfterV10Token = exports.fetchPairOiAfterV10Token = void 0;
|
|
4
13
|
const converter_1 = require("./converter");
|
|
@@ -9,16 +18,16 @@ const converter_1 = require("./converter");
|
|
|
9
18
|
* @param pairIndex Pair index
|
|
10
19
|
* @returns Promise resolving to pair OI in tokens
|
|
11
20
|
*/
|
|
12
|
-
const fetchPairOiAfterV10Token =
|
|
21
|
+
const fetchPairOiAfterV10Token = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
22
|
try {
|
|
14
|
-
const contractData =
|
|
23
|
+
const contractData = yield contract.getPairOiAfterV10Token(collateralIndex, pairIndex);
|
|
15
24
|
return (0, converter_1.convertPairOiToken)(contractData);
|
|
16
25
|
}
|
|
17
26
|
catch (error) {
|
|
18
27
|
console.error("Error fetching pair OI token:", error);
|
|
19
28
|
throw error;
|
|
20
29
|
}
|
|
21
|
-
};
|
|
30
|
+
});
|
|
22
31
|
exports.fetchPairOiAfterV10Token = fetchPairOiAfterV10Token;
|
|
23
32
|
/**
|
|
24
33
|
* @dev Fetches pair open interest in tokens for multiple pairs
|
|
@@ -27,19 +36,19 @@ exports.fetchPairOiAfterV10Token = fetchPairOiAfterV10Token;
|
|
|
27
36
|
* @param pairIndices Array of pair indices
|
|
28
37
|
* @returns Promise resolving to array of pair OI in tokens
|
|
29
38
|
*/
|
|
30
|
-
const fetchPairOisAfterV10Token =
|
|
39
|
+
const fetchPairOisAfterV10Token = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
40
|
if (collateralIndices.length !== pairIndices.length) {
|
|
32
41
|
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
33
42
|
}
|
|
34
43
|
try {
|
|
35
|
-
const contractDataArray =
|
|
44
|
+
const contractDataArray = yield contract.getPairOisAfterV10Token(collateralIndices, pairIndices);
|
|
36
45
|
return contractDataArray.map(converter_1.convertPairOiToken);
|
|
37
46
|
}
|
|
38
47
|
catch (error) {
|
|
39
48
|
console.error("Error fetching pair OIs token:", error);
|
|
40
49
|
throw error;
|
|
41
50
|
}
|
|
42
|
-
};
|
|
51
|
+
});
|
|
43
52
|
exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
|
|
44
53
|
/**
|
|
45
54
|
* @dev Fetches skew depth for a specific pair
|
|
@@ -48,9 +57,9 @@ exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
|
|
|
48
57
|
* @param pairIndex Pair index
|
|
49
58
|
* @returns Promise resolving to normalized skew depth
|
|
50
59
|
*/
|
|
51
|
-
const fetchPairSkewDepth =
|
|
60
|
+
const fetchPairSkewDepth = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
52
61
|
try {
|
|
53
|
-
const contractDepth =
|
|
62
|
+
const contractDepth = yield contract.getPairSkewDepth(collateralIndex, pairIndex);
|
|
54
63
|
// Token depths are always 1e18 precision
|
|
55
64
|
return (0, converter_1.convertSkewDepth)(contractDepth.toString());
|
|
56
65
|
}
|
|
@@ -58,7 +67,7 @@ const fetchPairSkewDepth = async (contract, collateralIndex, pairIndex) => {
|
|
|
58
67
|
console.error("Error fetching skew depth:", error);
|
|
59
68
|
throw error;
|
|
60
69
|
}
|
|
61
|
-
};
|
|
70
|
+
});
|
|
62
71
|
exports.fetchPairSkewDepth = fetchPairSkewDepth;
|
|
63
72
|
/**
|
|
64
73
|
* @dev Fetches skew depths for multiple pairs
|
|
@@ -67,12 +76,12 @@ exports.fetchPairSkewDepth = fetchPairSkewDepth;
|
|
|
67
76
|
* @param pairIndices Array of pair indices
|
|
68
77
|
* @returns Promise resolving to array of normalized skew depths
|
|
69
78
|
*/
|
|
70
|
-
const fetchPairSkewDepths =
|
|
79
|
+
const fetchPairSkewDepths = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
71
80
|
if (collateralIndices.length !== pairIndices.length) {
|
|
72
81
|
throw new Error("All input arrays must have the same length");
|
|
73
82
|
}
|
|
74
83
|
try {
|
|
75
|
-
const contractDepths =
|
|
84
|
+
const contractDepths = yield contract.getPairSkewDepths(collateralIndices, pairIndices);
|
|
76
85
|
// Token depths are always 1e18 precision
|
|
77
86
|
return contractDepths.map(depth => (0, converter_1.convertSkewDepth)(depth.toString()));
|
|
78
87
|
}
|
|
@@ -80,7 +89,7 @@ const fetchPairSkewDepths = async (contract, collateralIndices, pairIndices) =>
|
|
|
80
89
|
console.error("Error fetching skew depths:", error);
|
|
81
90
|
throw error;
|
|
82
91
|
}
|
|
83
|
-
};
|
|
92
|
+
});
|
|
84
93
|
exports.fetchPairSkewDepths = fetchPairSkewDepths;
|
|
85
94
|
/**
|
|
86
95
|
* @dev Fetches skew price impact context for a single pair
|
|
@@ -89,10 +98,10 @@ exports.fetchPairSkewDepths = fetchPairSkewDepths;
|
|
|
89
98
|
* @param pairIndex Pair index
|
|
90
99
|
* @returns Promise resolving to skew price impact context
|
|
91
100
|
*/
|
|
92
|
-
const fetchSkewPriceImpactContext =
|
|
101
|
+
const fetchSkewPriceImpactContext = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
93
102
|
try {
|
|
94
103
|
// Fetch OI data and skew depth in parallel
|
|
95
|
-
const [pairOiToken, skewDepth] =
|
|
104
|
+
const [pairOiToken, skewDepth] = yield Promise.all([
|
|
96
105
|
(0, exports.fetchPairOiAfterV10Token)(contract, collateralIndex, pairIndex),
|
|
97
106
|
(0, exports.fetchPairSkewDepth)(contract, collateralIndex, pairIndex),
|
|
98
107
|
]);
|
|
@@ -105,7 +114,7 @@ const fetchSkewPriceImpactContext = async (contract, collateralIndex, pairIndex)
|
|
|
105
114
|
console.error("Error fetching skew price impact context:", error);
|
|
106
115
|
throw error;
|
|
107
116
|
}
|
|
108
|
-
};
|
|
117
|
+
});
|
|
109
118
|
exports.fetchSkewPriceImpactContext = fetchSkewPriceImpactContext;
|
|
110
119
|
/**
|
|
111
120
|
* @dev Fetches collateral decimals for given collateral indices
|
|
@@ -113,16 +122,16 @@ exports.fetchSkewPriceImpactContext = fetchSkewPriceImpactContext;
|
|
|
113
122
|
* @param collateralIndices Array of collateral indices
|
|
114
123
|
* @returns Promise resolving to array of decimals
|
|
115
124
|
*/
|
|
116
|
-
const fetchCollateralDecimals =
|
|
125
|
+
const fetchCollateralDecimals = (contract, collateralIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
117
126
|
try {
|
|
118
127
|
// Get unique collateral indices to minimize calls
|
|
119
128
|
const uniqueIndices = [...new Set(collateralIndices)];
|
|
120
129
|
// Fetch collateral info for unique indices
|
|
121
|
-
const promises = uniqueIndices.map(
|
|
122
|
-
const collateral =
|
|
130
|
+
const promises = uniqueIndices.map((index) => __awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
const collateral = yield contract.getCollateral(index);
|
|
123
132
|
return { index, decimals: Number(collateral.precision) };
|
|
124
|
-
});
|
|
125
|
-
const collateralData =
|
|
133
|
+
}));
|
|
134
|
+
const collateralData = yield Promise.all(promises);
|
|
126
135
|
// Create a map for quick lookup
|
|
127
136
|
const decimalsMap = new Map(collateralData.map(data => [data.index, data.decimals]));
|
|
128
137
|
// Return decimals in the same order as input
|
|
@@ -133,7 +142,7 @@ const fetchCollateralDecimals = async (contract, collateralIndices) => {
|
|
|
133
142
|
console.error("Error fetching collateral decimals:", error);
|
|
134
143
|
throw error;
|
|
135
144
|
}
|
|
136
|
-
};
|
|
145
|
+
});
|
|
137
146
|
exports.fetchCollateralDecimals = fetchCollateralDecimals;
|
|
138
147
|
/**
|
|
139
148
|
* @dev Calculates skew price impact for a trade using contract call
|
|
@@ -145,9 +154,9 @@ exports.fetchCollateralDecimals = fetchCollateralDecimals;
|
|
|
145
154
|
* @param open Whether trade is opening
|
|
146
155
|
* @returns Promise resolving to price impact percentage (1e10)
|
|
147
156
|
*/
|
|
148
|
-
const calculateTradeSkewPriceImpact =
|
|
157
|
+
const calculateTradeSkewPriceImpact = (contract, collateralIndex, pairIndex, long, positionSizeToken, open) => __awaiter(void 0, void 0, void 0, function* () {
|
|
149
158
|
try {
|
|
150
|
-
const priceImpactP =
|
|
159
|
+
const priceImpactP = yield contract.getTradeSkewPriceImpactP(collateralIndex, pairIndex, long, BigInt(Math.round(positionSizeToken * 1e18)), // Convert to 1e18 precision
|
|
151
160
|
open);
|
|
152
161
|
// Convert from int256 1e10 to percentage
|
|
153
162
|
return Number(priceImpactP) / 1e10;
|
|
@@ -156,5 +165,5 @@ const calculateTradeSkewPriceImpact = async (contract, collateralIndex, pairInde
|
|
|
156
165
|
console.error("Error calculating trade skew price impact:", error);
|
|
157
166
|
throw error;
|
|
158
167
|
}
|
|
159
|
-
};
|
|
168
|
+
});
|
|
160
169
|
exports.calculateTradeSkewPriceImpact = calculateTradeSkewPriceImpact;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gainsnetwork/sdk",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0-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": ">=12.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@typechain/ethers-v5": "^10.1.1",
|