@gainsnetwork/sdk 0.0.0-v10.rc1 → 0.0.0-v10.rc3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/backend/globalTrades/index.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +2 -0
- package/lib/backend/index.js +18 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +312 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +30 -0
- package/lib/backend/tradingVariables/converter.js +329 -0
- package/lib/backend/tradingVariables/index.d.ts +3 -0
- package/lib/backend/tradingVariables/index.js +78 -0
- package/lib/backend/tradingVariables/types.d.ts +109 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.d.ts +8 -1
- package/lib/constants.js +8 -1
- package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +75 -0
- package/lib/contracts/fetch/fees/borrowingFeesV2.js +193 -0
- package/lib/contracts/fetch/fees/fundingFees.d.ts +66 -0
- package/lib/contracts/fetch/fees/fundingFees.js +150 -0
- package/lib/contracts/fetch/priceImpact/skew.d.ts +63 -0
- package/lib/contracts/fetch/priceImpact/skew.js +168 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/openTrades.d.ts +1 -0
- package/lib/contracts/utils/openTrades.js +94 -56
- package/lib/contracts/utils/pairs.js +7 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/markets/collateral/converter.d.ts +5 -0
- package/lib/markets/collateral/converter.js +11 -0
- package/lib/markets/collateral/index.d.ts +1 -0
- package/lib/markets/collateral/index.js +17 -0
- package/lib/markets/collateral/types.d.ts +7 -0
- package/lib/markets/collateral/types.js +2 -0
- package/lib/markets/index.d.ts +2 -0
- package/lib/markets/index.js +2 -0
- package/lib/markets/oi/converter.d.ts +63 -0
- package/lib/markets/oi/converter.js +103 -0
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/index.d.ts +10 -0
- package/lib/markets/oi/index.js +37 -0
- package/lib/markets/oi/types.d.ts +82 -0
- package/lib/markets/oi/types.js +6 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/trade/fees/borrowing/index.d.ts +22 -1
- package/lib/trade/fees/borrowing/index.js +39 -13
- package/lib/trade/fees/borrowingV2/converter.d.ts +66 -0
- package/lib/trade/fees/borrowingV2/converter.js +121 -0
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/fetcher.js +193 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +60 -0
- package/lib/trade/fees/borrowingV2/index.js +140 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +79 -0
- package/lib/trade/fees/borrowingV2/types.js +5 -0
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +110 -0
- package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
- package/lib/trade/fees/fundingFees/converter.js +196 -0
- package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
- package/lib/trade/fees/fundingFees/fetcher.js +150 -0
- package/lib/trade/fees/fundingFees/index.d.ts +136 -0
- package/lib/trade/fees/fundingFees/index.js +326 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +7 -2
- package/lib/trade/fees/index.js +67 -16
- package/lib/trade/fees/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- package/lib/trade/fees/tiers/index.d.ts +18 -0
- package/lib/trade/fees/tiers/index.js +45 -1
- package/lib/trade/fees/trading/converter.d.ts +30 -0
- package/lib/trade/fees/trading/converter.js +43 -0
- package/lib/trade/fees/trading/index.d.ts +54 -0
- package/lib/trade/fees/trading/index.js +147 -0
- package/lib/trade/fees/trading/types.d.ts +48 -0
- package/lib/trade/fees/trading/types.js +5 -0
- package/lib/trade/index.d.ts +3 -2
- package/lib/trade/index.js +3 -2
- package/lib/trade/liquidation/converter.d.ts +23 -0
- package/lib/trade/liquidation/converter.js +46 -0
- package/lib/trade/liquidation/index.d.ts +31 -0
- package/lib/trade/liquidation/index.js +187 -0
- package/lib/trade/liquidation/types.d.ts +44 -0
- package/lib/trade/liquidation/types.js +2 -0
- package/lib/trade/pnl/converter.d.ts +47 -0
- package/lib/trade/pnl/converter.js +72 -0
- package/lib/trade/pnl/index.d.ts +86 -0
- package/lib/trade/pnl/index.js +201 -0
- package/lib/trade/pnl/types.d.ts +86 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/index.d.ts +21 -0
- package/lib/trade/priceImpact/close/index.js +131 -0
- package/lib/trade/priceImpact/close/types.d.ts +43 -0
- package/lib/trade/priceImpact/close/types.js +5 -0
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
- package/lib/trade/priceImpact/cumulVol/index.d.ts +107 -0
- package/lib/trade/priceImpact/cumulVol/index.js +228 -0
- package/lib/trade/priceImpact/index.d.ts +12 -0
- package/lib/trade/priceImpact/index.js +59 -0
- package/lib/trade/priceImpact/open/index.d.ts +22 -0
- package/lib/trade/priceImpact/open/index.js +76 -0
- package/lib/trade/priceImpact/open/types.d.ts +41 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +77 -0
- package/lib/trade/priceImpact/skew/converter.js +171 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +63 -0
- package/lib/trade/priceImpact/skew/fetcher.js +168 -0
- package/lib/trade/priceImpact/skew/index.d.ts +58 -0
- package/lib/trade/priceImpact/skew/index.js +179 -0
- package/lib/trade/priceImpact/skew/types.d.ts +55 -0
- package/lib/trade/priceImpact/skew/types.js +5 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +34 -9
- package/lib/trade/types.js +7 -0
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/package.json +2 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Converters for skew price impact data between contract and SDK formats
|
|
4
|
+
* @dev All BigNumber values are normalized to floats with appropriate precision
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.mergeSkewPriceImpactContexts = exports.convertSkewDepthsArray = exports.isValidSkewDepth = exports.createSkewPriceImpactContext = exports.createSkewDepth = exports.normalizeSkewDepth = exports.convertPairOiCollateralArray = exports.convertPairOiCollateral = exports.convertPairOiTokenArray = exports.convertPairOiToken = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* @dev Converts contract pair OI token data to SDK format
|
|
10
|
+
* @param contractData Contract pair OI token struct
|
|
11
|
+
* @returns Normalized pair OI token data
|
|
12
|
+
*/
|
|
13
|
+
const convertPairOiToken = (contractData) => {
|
|
14
|
+
// Token amounts are stored as 1e18 in contract
|
|
15
|
+
return {
|
|
16
|
+
oiLongToken: Number(contractData.oiLongToken) / 1e18,
|
|
17
|
+
oiShortToken: Number(contractData.oiShortToken) / 1e18,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.convertPairOiToken = convertPairOiToken;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts array of contract pair OI token data to SDK format
|
|
23
|
+
* @param contractDataArray Array of contract pair OI token data
|
|
24
|
+
* @returns Array of normalized pair OI token data
|
|
25
|
+
*/
|
|
26
|
+
const convertPairOiTokenArray = (contractDataArray) => {
|
|
27
|
+
return contractDataArray.map(exports.convertPairOiToken);
|
|
28
|
+
};
|
|
29
|
+
exports.convertPairOiTokenArray = convertPairOiTokenArray;
|
|
30
|
+
/**
|
|
31
|
+
* @dev Converts contract pair OI collateral data to SDK format
|
|
32
|
+
* @param contractData Contract pair OI collateral struct
|
|
33
|
+
* @param collateralDecimals Number of decimals for the collateral (e.g., 18 for DAI, 6 for USDC)
|
|
34
|
+
* @returns Normalized pair OI collateral data
|
|
35
|
+
*/
|
|
36
|
+
const convertPairOiCollateral = (contractData, collateralDecimals) => {
|
|
37
|
+
const divisor = Math.pow(10, collateralDecimals);
|
|
38
|
+
return {
|
|
39
|
+
oiLongCollateral: Number(contractData.oiLongCollateral) / divisor,
|
|
40
|
+
oiShortCollateral: Number(contractData.oiShortCollateral) / divisor,
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
exports.convertPairOiCollateral = convertPairOiCollateral;
|
|
44
|
+
/**
|
|
45
|
+
* @dev Converts array of contract pair OI collateral data to SDK format
|
|
46
|
+
* @param contractDataArray Array of contract pair OI collateral data
|
|
47
|
+
* @param collateralDecimals Array of collateral decimals for each entry
|
|
48
|
+
* @returns Array of normalized pair OI collateral data
|
|
49
|
+
*/
|
|
50
|
+
const convertPairOiCollateralArray = (contractDataArray, collateralDecimals) => {
|
|
51
|
+
if (contractDataArray.length !== collateralDecimals.length) {
|
|
52
|
+
throw new Error("Contract data array and collateral decimals array must have the same length");
|
|
53
|
+
}
|
|
54
|
+
return contractDataArray.map((data, index) => (0, exports.convertPairOiCollateral)(data, collateralDecimals[index]));
|
|
55
|
+
};
|
|
56
|
+
exports.convertPairOiCollateralArray = convertPairOiCollateralArray;
|
|
57
|
+
/**
|
|
58
|
+
* @dev Normalizes skew depth from contract format
|
|
59
|
+
* @param depth Skew depth from contract (in collateral wei)
|
|
60
|
+
* @param collateralDecimals Number of decimals for the collateral
|
|
61
|
+
* @returns Normalized skew depth
|
|
62
|
+
*/
|
|
63
|
+
const normalizeSkewDepth = (depth, collateralDecimals) => {
|
|
64
|
+
const divisor = Math.pow(10, collateralDecimals);
|
|
65
|
+
return Number(depth) / divisor;
|
|
66
|
+
};
|
|
67
|
+
exports.normalizeSkewDepth = normalizeSkewDepth;
|
|
68
|
+
/**
|
|
69
|
+
* @dev Creates a skew depth object
|
|
70
|
+
* @param collateralIndex Collateral index
|
|
71
|
+
* @param pairIndex Pair index
|
|
72
|
+
* @param depth Normalized depth value
|
|
73
|
+
* @returns Skew depth object
|
|
74
|
+
*/
|
|
75
|
+
const createSkewDepth = (collateralIndex, pairIndex, depth) => {
|
|
76
|
+
return {
|
|
77
|
+
collateralIndex,
|
|
78
|
+
pairIndex,
|
|
79
|
+
depth,
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
exports.createSkewDepth = createSkewDepth;
|
|
83
|
+
/**
|
|
84
|
+
* @dev Creates skew price impact context from arrays of data
|
|
85
|
+
* @param collateralIndices Array of collateral indices
|
|
86
|
+
* @param pairIndices Array of pair indices
|
|
87
|
+
* @param skewDepths Array of normalized skew depths
|
|
88
|
+
* @param pairOiTokens Array of pair OI token data
|
|
89
|
+
* @returns Complete skew price impact context
|
|
90
|
+
*/
|
|
91
|
+
const createSkewPriceImpactContext = (collateralIndices, pairIndices, skewDepths, pairOiTokens) => {
|
|
92
|
+
if (collateralIndices.length !== pairIndices.length ||
|
|
93
|
+
pairIndices.length !== skewDepths.length ||
|
|
94
|
+
skewDepths.length !== pairOiTokens.length) {
|
|
95
|
+
throw new Error("All input arrays must have the same length");
|
|
96
|
+
}
|
|
97
|
+
const context = {
|
|
98
|
+
skewDepths: {},
|
|
99
|
+
pairOiTokens: {},
|
|
100
|
+
};
|
|
101
|
+
// Build nested objects indexed by collateralIndex and pairIndex
|
|
102
|
+
for (let i = 0; i < collateralIndices.length; i++) {
|
|
103
|
+
const collateralIndex = collateralIndices[i];
|
|
104
|
+
const pairIndex = pairIndices[i];
|
|
105
|
+
// Initialize collateral index objects if they don't exist
|
|
106
|
+
if (!context.skewDepths[collateralIndex]) {
|
|
107
|
+
context.skewDepths[collateralIndex] = {};
|
|
108
|
+
}
|
|
109
|
+
if (!context.pairOiTokens[collateralIndex]) {
|
|
110
|
+
context.pairOiTokens[collateralIndex] = {};
|
|
111
|
+
}
|
|
112
|
+
// Store data
|
|
113
|
+
context.skewDepths[collateralIndex][pairIndex] = skewDepths[i];
|
|
114
|
+
context.pairOiTokens[collateralIndex][pairIndex] = pairOiTokens[i];
|
|
115
|
+
}
|
|
116
|
+
return context;
|
|
117
|
+
};
|
|
118
|
+
exports.createSkewPriceImpactContext = createSkewPriceImpactContext;
|
|
119
|
+
/**
|
|
120
|
+
* @dev Validates skew depth is within reasonable bounds
|
|
121
|
+
* @param depth Normalized skew depth
|
|
122
|
+
* @param minDepth Minimum allowed depth (default: 0)
|
|
123
|
+
* @param maxDepth Maximum allowed depth (default: 1e12)
|
|
124
|
+
* @returns Whether depth is valid
|
|
125
|
+
*/
|
|
126
|
+
const isValidSkewDepth = (depth, minDepth = 0, maxDepth = 1e12) => {
|
|
127
|
+
return depth >= minDepth && depth <= maxDepth;
|
|
128
|
+
};
|
|
129
|
+
exports.isValidSkewDepth = isValidSkewDepth;
|
|
130
|
+
/**
|
|
131
|
+
* @dev Converts contract skew depths array to normalized values
|
|
132
|
+
* @param contractDepths Array of depths from contract
|
|
133
|
+
* @param collateralDecimals Array of decimals for each collateral
|
|
134
|
+
* @returns Array of normalized depths
|
|
135
|
+
*/
|
|
136
|
+
const convertSkewDepthsArray = (contractDepths, collateralDecimals) => {
|
|
137
|
+
if (contractDepths.length !== collateralDecimals.length) {
|
|
138
|
+
throw new Error("Contract depths array and collateral decimals array must have the same length");
|
|
139
|
+
}
|
|
140
|
+
return contractDepths.map((depth, index) => (0, exports.normalizeSkewDepth)(depth, collateralDecimals[index]));
|
|
141
|
+
};
|
|
142
|
+
exports.convertSkewDepthsArray = convertSkewDepthsArray;
|
|
143
|
+
/**
|
|
144
|
+
* @dev Merges multiple contexts into one
|
|
145
|
+
* @param contexts Array of contexts to merge
|
|
146
|
+
* @returns Merged context
|
|
147
|
+
*/
|
|
148
|
+
const mergeSkewPriceImpactContexts = (contexts) => {
|
|
149
|
+
const merged = {
|
|
150
|
+
skewDepths: {},
|
|
151
|
+
pairOiTokens: {},
|
|
152
|
+
};
|
|
153
|
+
for (const context of contexts) {
|
|
154
|
+
// Merge skew depths
|
|
155
|
+
for (const collateralIndex in context.skewDepths) {
|
|
156
|
+
if (!merged.skewDepths[collateralIndex]) {
|
|
157
|
+
merged.skewDepths[collateralIndex] = {};
|
|
158
|
+
}
|
|
159
|
+
Object.assign(merged.skewDepths[collateralIndex], context.skewDepths[collateralIndex]);
|
|
160
|
+
}
|
|
161
|
+
// Merge pair OI tokens
|
|
162
|
+
for (const collateralIndex in context.pairOiTokens) {
|
|
163
|
+
if (!merged.pairOiTokens[collateralIndex]) {
|
|
164
|
+
merged.pairOiTokens[collateralIndex] = {};
|
|
165
|
+
}
|
|
166
|
+
Object.assign(merged.pairOiTokens[collateralIndex], context.pairOiTokens[collateralIndex]);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return merged;
|
|
170
|
+
};
|
|
171
|
+
exports.mergeSkewPriceImpactContexts = mergeSkewPriceImpactContexts;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { GNSMultiCollatDiamond } from "../../../contracts/types/generated";
|
|
2
|
+
import type { PairOiToken, SkewPriceImpactContext } from "..";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Fetches pair open interest in tokens for a specific pair
|
|
5
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
6
|
+
* @param collateralIndex Collateral index
|
|
7
|
+
* @param pairIndex Pair index
|
|
8
|
+
* @returns Promise resolving to pair OI in tokens
|
|
9
|
+
*/
|
|
10
|
+
export declare const fetchPairOiAfterV10Token: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number) => Promise<PairOiToken>;
|
|
11
|
+
/**
|
|
12
|
+
* @dev Fetches pair open interest in tokens for multiple pairs
|
|
13
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
14
|
+
* @param collateralIndices Array of collateral indices
|
|
15
|
+
* @param pairIndices Array of pair indices
|
|
16
|
+
* @returns Promise resolving to array of pair OI in tokens
|
|
17
|
+
*/
|
|
18
|
+
export declare const fetchPairOisAfterV10Token: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<PairOiToken[]>;
|
|
19
|
+
/**
|
|
20
|
+
* @dev Fetches skew depth for a specific pair
|
|
21
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
22
|
+
* @param collateralIndex Collateral index
|
|
23
|
+
* @param pairIndex Pair index
|
|
24
|
+
* @param collateralDecimals Number of decimals for the collateral
|
|
25
|
+
* @returns Promise resolving to normalized skew depth
|
|
26
|
+
*/
|
|
27
|
+
export declare const fetchPairSkewDepth: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, collateralDecimals: number) => Promise<number>;
|
|
28
|
+
/**
|
|
29
|
+
* @dev Fetches skew depths for multiple pairs
|
|
30
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
31
|
+
* @param collateralIndices Array of collateral indices
|
|
32
|
+
* @param pairIndices Array of pair indices
|
|
33
|
+
* @param collateralDecimals Array of collateral decimals for each pair
|
|
34
|
+
* @returns Promise resolving to array of normalized skew depths
|
|
35
|
+
*/
|
|
36
|
+
export declare const fetchPairSkewDepths: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[], collateralDecimals: number[]) => Promise<number[]>;
|
|
37
|
+
/**
|
|
38
|
+
* @dev Fetches complete skew price impact context for multiple pairs
|
|
39
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
40
|
+
* @param collateralIndices Array of collateral indices
|
|
41
|
+
* @param pairIndices Array of pair indices
|
|
42
|
+
* @param collateralDecimals Array of collateral decimals for each pair
|
|
43
|
+
* @returns Promise resolving to complete skew price impact context
|
|
44
|
+
*/
|
|
45
|
+
export declare const fetchSkewPriceImpactContext: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[], collateralDecimals: number[]) => Promise<SkewPriceImpactContext>;
|
|
46
|
+
/**
|
|
47
|
+
* @dev Fetches collateral decimals for given collateral indices
|
|
48
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
49
|
+
* @param collateralIndices Array of collateral indices
|
|
50
|
+
* @returns Promise resolving to array of decimals
|
|
51
|
+
*/
|
|
52
|
+
export declare const fetchCollateralDecimals: (contract: GNSMultiCollatDiamond, collateralIndices: number[]) => Promise<number[]>;
|
|
53
|
+
/**
|
|
54
|
+
* @dev Calculates skew price impact for a trade using contract call
|
|
55
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
56
|
+
* @param collateralIndex Collateral index
|
|
57
|
+
* @param pairIndex Pair index
|
|
58
|
+
* @param long Whether trade is long
|
|
59
|
+
* @param positionSizeToken Position size in tokens
|
|
60
|
+
* @param open Whether trade is opening
|
|
61
|
+
* @returns Promise resolving to price impact percentage (1e10)
|
|
62
|
+
*/
|
|
63
|
+
export declare const calculateTradeSkewPriceImpact: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, long: boolean, positionSizeToken: number, open: boolean) => Promise<number>;
|
|
@@ -0,0 +1,168 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.calculateTradeSkewPriceImpact = exports.fetchCollateralDecimals = exports.fetchSkewPriceImpactContext = exports.fetchPairSkewDepths = exports.fetchPairSkewDepth = exports.fetchPairOisAfterV10Token = exports.fetchPairOiAfterV10Token = void 0;
|
|
13
|
+
const __1 = require("..");
|
|
14
|
+
/**
|
|
15
|
+
* @dev Fetches pair open interest in tokens for a specific pair
|
|
16
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
17
|
+
* @param collateralIndex Collateral index
|
|
18
|
+
* @param pairIndex Pair index
|
|
19
|
+
* @returns Promise resolving to pair OI in tokens
|
|
20
|
+
*/
|
|
21
|
+
const fetchPairOiAfterV10Token = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
try {
|
|
23
|
+
const contractData = yield contract.getPairOiAfterV10Token(collateralIndex, pairIndex);
|
|
24
|
+
return (0, __1.convertPairOiToken)(contractData);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
console.error("Error fetching pair OI token:", error);
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
exports.fetchPairOiAfterV10Token = fetchPairOiAfterV10Token;
|
|
32
|
+
/**
|
|
33
|
+
* @dev Fetches pair open interest in tokens for multiple pairs
|
|
34
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
35
|
+
* @param collateralIndices Array of collateral indices
|
|
36
|
+
* @param pairIndices Array of pair indices
|
|
37
|
+
* @returns Promise resolving to array of pair OI in tokens
|
|
38
|
+
*/
|
|
39
|
+
const fetchPairOisAfterV10Token = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
+
if (collateralIndices.length !== pairIndices.length) {
|
|
41
|
+
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
const contractDataArray = yield contract.getPairOisAfterV10Token(collateralIndices, pairIndices);
|
|
45
|
+
return contractDataArray.map(__1.convertPairOiToken);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.error("Error fetching pair OIs token:", error);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
|
|
53
|
+
/**
|
|
54
|
+
* @dev Fetches skew depth for a specific pair
|
|
55
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
56
|
+
* @param collateralIndex Collateral index
|
|
57
|
+
* @param pairIndex Pair index
|
|
58
|
+
* @param collateralDecimals Number of decimals for the collateral
|
|
59
|
+
* @returns Promise resolving to normalized skew depth
|
|
60
|
+
*/
|
|
61
|
+
const fetchPairSkewDepth = (contract, collateralIndex, pairIndex, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
|
+
try {
|
|
63
|
+
const contractDepth = yield contract.getPairSkewDepth(collateralIndex, pairIndex);
|
|
64
|
+
return (0, __1.normalizeSkewDepth)(contractDepth.toBigInt(), collateralDecimals);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
console.error("Error fetching skew depth:", error);
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
exports.fetchPairSkewDepth = fetchPairSkewDepth;
|
|
72
|
+
/**
|
|
73
|
+
* @dev Fetches skew depths for multiple pairs
|
|
74
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
75
|
+
* @param collateralIndices Array of collateral indices
|
|
76
|
+
* @param pairIndices Array of pair indices
|
|
77
|
+
* @param collateralDecimals Array of collateral decimals for each pair
|
|
78
|
+
* @returns Promise resolving to array of normalized skew depths
|
|
79
|
+
*/
|
|
80
|
+
const fetchPairSkewDepths = (contract, collateralIndices, pairIndices, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
+
if (collateralIndices.length !== pairIndices.length ||
|
|
82
|
+
pairIndices.length !== collateralDecimals.length) {
|
|
83
|
+
throw new Error("All input arrays must have the same length");
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const contractDepths = yield contract.getPairSkewDepths(collateralIndices, pairIndices);
|
|
87
|
+
return contractDepths.map((depth, i) => (0, __1.normalizeSkewDepth)(depth.toBigInt(), collateralDecimals[i]));
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
console.error("Error fetching skew depths:", error);
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
exports.fetchPairSkewDepths = fetchPairSkewDepths;
|
|
95
|
+
/**
|
|
96
|
+
* @dev Fetches complete skew price impact context for multiple pairs
|
|
97
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
98
|
+
* @param collateralIndices Array of collateral indices
|
|
99
|
+
* @param pairIndices Array of pair indices
|
|
100
|
+
* @param collateralDecimals Array of collateral decimals for each pair
|
|
101
|
+
* @returns Promise resolving to complete skew price impact context
|
|
102
|
+
*/
|
|
103
|
+
const fetchSkewPriceImpactContext = (contract, collateralIndices, pairIndices, collateralDecimals) => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
|
+
try {
|
|
105
|
+
// Fetch OI data and skew depths in parallel
|
|
106
|
+
const [pairOiTokens, skewDepths] = yield Promise.all([
|
|
107
|
+
(0, exports.fetchPairOisAfterV10Token)(contract, collateralIndices, pairIndices),
|
|
108
|
+
(0, exports.fetchPairSkewDepths)(contract, collateralIndices, pairIndices, collateralDecimals),
|
|
109
|
+
]);
|
|
110
|
+
return (0, __1.createSkewPriceImpactContext)(collateralIndices, pairIndices, skewDepths, pairOiTokens);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
console.error("Error fetching skew price impact context:", error);
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
exports.fetchSkewPriceImpactContext = fetchSkewPriceImpactContext;
|
|
118
|
+
/**
|
|
119
|
+
* @dev Fetches collateral decimals for given collateral indices
|
|
120
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
121
|
+
* @param collateralIndices Array of collateral indices
|
|
122
|
+
* @returns Promise resolving to array of decimals
|
|
123
|
+
*/
|
|
124
|
+
const fetchCollateralDecimals = (contract, collateralIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
125
|
+
try {
|
|
126
|
+
// Get unique collateral indices to minimize calls
|
|
127
|
+
const uniqueIndices = [...new Set(collateralIndices)];
|
|
128
|
+
// Fetch collateral info for unique indices
|
|
129
|
+
const promises = uniqueIndices.map((index) => __awaiter(void 0, void 0, void 0, function* () {
|
|
130
|
+
const collateral = yield contract.getCollateral(index);
|
|
131
|
+
return { index, decimals: Number(collateral.precision) };
|
|
132
|
+
}));
|
|
133
|
+
const collateralData = yield Promise.all(promises);
|
|
134
|
+
// Create a map for quick lookup
|
|
135
|
+
const decimalsMap = new Map(collateralData.map(data => [data.index, data.decimals]));
|
|
136
|
+
// Return decimals in the same order as input
|
|
137
|
+
return collateralIndices.map(index => decimalsMap.get(index) || 18 // Default to 18 if not found
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
console.error("Error fetching collateral decimals:", error);
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
exports.fetchCollateralDecimals = fetchCollateralDecimals;
|
|
146
|
+
/**
|
|
147
|
+
* @dev Calculates skew price impact for a trade using contract call
|
|
148
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
149
|
+
* @param collateralIndex Collateral index
|
|
150
|
+
* @param pairIndex Pair index
|
|
151
|
+
* @param long Whether trade is long
|
|
152
|
+
* @param positionSizeToken Position size in tokens
|
|
153
|
+
* @param open Whether trade is opening
|
|
154
|
+
* @returns Promise resolving to price impact percentage (1e10)
|
|
155
|
+
*/
|
|
156
|
+
const calculateTradeSkewPriceImpact = (contract, collateralIndex, pairIndex, long, positionSizeToken, open) => __awaiter(void 0, void 0, void 0, function* () {
|
|
157
|
+
try {
|
|
158
|
+
const priceImpactP = yield contract.getTradeSkewPriceImpactP(collateralIndex, pairIndex, long, BigInt(Math.round(positionSizeToken * 1e18)), // Convert to 1e18 precision
|
|
159
|
+
open);
|
|
160
|
+
// Convert from int256 1e10 to percentage
|
|
161
|
+
return Number(priceImpactP) / 1e10;
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
console.error("Error calculating trade skew price impact:", error);
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
exports.calculateTradeSkewPriceImpact = calculateTradeSkewPriceImpact;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Skew price impact calculations for v10+ trades
|
|
3
|
+
* @dev Based on formula: (existingSkew + tradeSize/2) / skewDepth
|
|
4
|
+
*/
|
|
5
|
+
import { PairOiToken, SkewPriceImpactInput, SkewPriceImpactResult, SkewPriceImpactContext, TradeSkewParams } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Calculates net skew in tokens (long - short)
|
|
8
|
+
* @param pairOi Pair OI data with long and short token amounts
|
|
9
|
+
* @returns Net skew in tokens (positive = long heavy, negative = short heavy)
|
|
10
|
+
*/
|
|
11
|
+
export declare const getNetSkewToken: (pairOi: PairOiToken) => number;
|
|
12
|
+
/**
|
|
13
|
+
* @dev Calculates net skew in collateral tokens
|
|
14
|
+
* @param netSkewToken Net skew in tokens
|
|
15
|
+
* @param currentPrice Current pair price
|
|
16
|
+
* @returns Net skew in collateral tokens
|
|
17
|
+
*/
|
|
18
|
+
export declare const getNetSkewCollateral: (netSkewToken: number, currentPrice: number) => number;
|
|
19
|
+
/**
|
|
20
|
+
* @dev Determines trade direction impact on skew
|
|
21
|
+
* @param long Is long position
|
|
22
|
+
* @param open Is opening (true) or closing (false)
|
|
23
|
+
* @returns Whether trade increases or decreases skew
|
|
24
|
+
*/
|
|
25
|
+
export declare const getTradeSkewDirection: (long: boolean, open: boolean) => boolean;
|
|
26
|
+
/**
|
|
27
|
+
* @dev Core skew price impact calculation
|
|
28
|
+
* @param existingSkewToken Current net skew in tokens (signed)
|
|
29
|
+
* @param tradeSizeToken Trade size in tokens (always positive)
|
|
30
|
+
* @param skewDepth Skew depth in collateral tokens
|
|
31
|
+
* @param tradeIncreasesSkew Whether trade increases skew in its direction
|
|
32
|
+
* @returns Price impact percentage (can be positive or negative)
|
|
33
|
+
*/
|
|
34
|
+
export declare const calculateSkewPriceImpactP: (existingSkewToken: number, tradeSizeToken: number, skewDepth: number, tradeIncreasesSkew: boolean) => number;
|
|
35
|
+
/**
|
|
36
|
+
* @dev Main function to calculate skew price impact for a trade
|
|
37
|
+
* @param context Skew price impact context with depths and OI data
|
|
38
|
+
* @param input Trade parameters
|
|
39
|
+
* @returns Skew price impact result
|
|
40
|
+
*/
|
|
41
|
+
export declare const getTradeSkewPriceImpact: (context: SkewPriceImpactContext, input: SkewPriceImpactInput) => SkewPriceImpactResult;
|
|
42
|
+
/**
|
|
43
|
+
* @dev Calculate skew price impact for a trade with all parameters
|
|
44
|
+
* @param params Trade parameters including price and version checks
|
|
45
|
+
* @param context Skew price impact context
|
|
46
|
+
* @returns Price impact percentage or 0 if not applicable
|
|
47
|
+
*/
|
|
48
|
+
export declare const getTradeSkewPriceImpactWithChecks: (params: TradeSkewParams, context: SkewPriceImpactContext) => number;
|
|
49
|
+
/**
|
|
50
|
+
* @dev Calculate position sizes for partial operations
|
|
51
|
+
* @param originalSizeCollateral Original position size in collateral
|
|
52
|
+
* @param deltaCollateral Position size delta in collateral
|
|
53
|
+
* @param originalSizeToken Original position size in tokens
|
|
54
|
+
* @returns Delta in tokens proportional to collateral delta
|
|
55
|
+
*/
|
|
56
|
+
export declare const calculatePartialSizeToken: (originalSizeCollateral: number, deltaCollateral: number, originalSizeToken: number) => number;
|
|
57
|
+
export * as SkewPriceImpact from "./types";
|
|
58
|
+
export * from "./fetcher";
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Skew price impact calculations for v10+ trades
|
|
4
|
+
* @dev Based on formula: (existingSkew + tradeSize/2) / skewDepth
|
|
5
|
+
*/
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
+
}) : function(o, v) {
|
|
20
|
+
o["default"] = v;
|
|
21
|
+
});
|
|
22
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
23
|
+
if (mod && mod.__esModule) return mod;
|
|
24
|
+
var result = {};
|
|
25
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
26
|
+
__setModuleDefault(result, mod);
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
30
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
31
|
+
};
|
|
32
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
|
+
exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpactWithChecks = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = void 0;
|
|
34
|
+
const utils_1 = require("../../utils");
|
|
35
|
+
// Constants
|
|
36
|
+
const PRICE_IMPACT_DIVIDER = 2; // Half price impact to match cumulative volume impact scale
|
|
37
|
+
/**
|
|
38
|
+
* @dev Calculates net skew in tokens (long - short)
|
|
39
|
+
* @param pairOi Pair OI data with long and short token amounts
|
|
40
|
+
* @returns Net skew in tokens (positive = long heavy, negative = short heavy)
|
|
41
|
+
*/
|
|
42
|
+
const getNetSkewToken = (pairOi) => {
|
|
43
|
+
return pairOi.oiLongToken - pairOi.oiShortToken;
|
|
44
|
+
};
|
|
45
|
+
exports.getNetSkewToken = getNetSkewToken;
|
|
46
|
+
/**
|
|
47
|
+
* @dev Calculates net skew in collateral tokens
|
|
48
|
+
* @param netSkewToken Net skew in tokens
|
|
49
|
+
* @param currentPrice Current pair price
|
|
50
|
+
* @returns Net skew in collateral tokens
|
|
51
|
+
*/
|
|
52
|
+
const getNetSkewCollateral = (netSkewToken, currentPrice) => {
|
|
53
|
+
return netSkewToken * currentPrice;
|
|
54
|
+
};
|
|
55
|
+
exports.getNetSkewCollateral = getNetSkewCollateral;
|
|
56
|
+
/**
|
|
57
|
+
* @dev Determines trade direction impact on skew
|
|
58
|
+
* @param long Is long position
|
|
59
|
+
* @param open Is opening (true) or closing (false)
|
|
60
|
+
* @returns Whether trade increases or decreases skew
|
|
61
|
+
*/
|
|
62
|
+
const getTradeSkewDirection = (long, open) => {
|
|
63
|
+
// Opening long or closing short increases positive skew
|
|
64
|
+
// Opening short or closing long increases negative skew
|
|
65
|
+
return (long && open) || (!long && !open);
|
|
66
|
+
};
|
|
67
|
+
exports.getTradeSkewDirection = getTradeSkewDirection;
|
|
68
|
+
/**
|
|
69
|
+
* @dev Core skew price impact calculation
|
|
70
|
+
* @param existingSkewToken Current net skew in tokens (signed)
|
|
71
|
+
* @param tradeSizeToken Trade size in tokens (always positive)
|
|
72
|
+
* @param skewDepth Skew depth in collateral tokens
|
|
73
|
+
* @param tradeIncreasesSkew Whether trade increases skew in its direction
|
|
74
|
+
* @returns Price impact percentage (can be positive or negative)
|
|
75
|
+
*/
|
|
76
|
+
const calculateSkewPriceImpactP = (existingSkewToken, tradeSizeToken, skewDepth, tradeIncreasesSkew) => {
|
|
77
|
+
if (skewDepth === 0) {
|
|
78
|
+
return 0; // No impact if depth is 0
|
|
79
|
+
}
|
|
80
|
+
// Convert signed values based on trade direction
|
|
81
|
+
const tradeSkewMultiplier = tradeIncreasesSkew ? 1 : -1;
|
|
82
|
+
const signedExistingSkew = existingSkewToken;
|
|
83
|
+
const signedTradeSize = tradeSizeToken * tradeSkewMultiplier;
|
|
84
|
+
// Formula: (existingSkew + tradeSize/2) / skewDepth
|
|
85
|
+
const numerator = signedExistingSkew + signedTradeSize / 2;
|
|
86
|
+
const priceImpactP = (numerator / skewDepth) * 100; // Convert to percentage
|
|
87
|
+
// Apply divider to match cumulative volume impact scale
|
|
88
|
+
return priceImpactP / PRICE_IMPACT_DIVIDER;
|
|
89
|
+
};
|
|
90
|
+
exports.calculateSkewPriceImpactP = calculateSkewPriceImpactP;
|
|
91
|
+
/**
|
|
92
|
+
* @dev Main function to calculate skew price impact for a trade
|
|
93
|
+
* @param context Skew price impact context with depths and OI data
|
|
94
|
+
* @param input Trade parameters
|
|
95
|
+
* @returns Skew price impact result
|
|
96
|
+
*/
|
|
97
|
+
const getTradeSkewPriceImpact = (context, input) => {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
// Get skew depth for the pair
|
|
100
|
+
const skewDepth = (_a = context.skewDepths[input.collateralIndex]) === null || _a === void 0 ? void 0 : _a[input.pairIndex];
|
|
101
|
+
if (skewDepth === undefined) {
|
|
102
|
+
throw new Error(`Missing skew depth for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
103
|
+
}
|
|
104
|
+
// Get pair OI data
|
|
105
|
+
const pairOi = (_b = context.pairOiTokens[input.collateralIndex]) === null || _b === void 0 ? void 0 : _b[input.pairIndex];
|
|
106
|
+
if (!pairOi) {
|
|
107
|
+
throw new Error(`Missing pair OI data for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
108
|
+
}
|
|
109
|
+
// Calculate net skew
|
|
110
|
+
const netSkewToken = (0, exports.getNetSkewToken)(pairOi);
|
|
111
|
+
// Determine trade direction impact
|
|
112
|
+
const tradeIncreasesSkew = (0, exports.getTradeSkewDirection)(input.long, input.open);
|
|
113
|
+
// Calculate price impact
|
|
114
|
+
const priceImpactP = (0, exports.calculateSkewPriceImpactP)(netSkewToken, input.positionSizeToken, skewDepth, tradeIncreasesSkew);
|
|
115
|
+
// Determine trade direction relative to skew
|
|
116
|
+
let tradeDirection;
|
|
117
|
+
if (priceImpactP > 0) {
|
|
118
|
+
tradeDirection = "increase";
|
|
119
|
+
}
|
|
120
|
+
else if (priceImpactP < 0) {
|
|
121
|
+
tradeDirection = "decrease";
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
tradeDirection = "neutral";
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
priceImpactP,
|
|
128
|
+
netSkewToken,
|
|
129
|
+
netSkewCollateral: 0,
|
|
130
|
+
tradeDirection,
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
exports.getTradeSkewPriceImpact = getTradeSkewPriceImpact;
|
|
134
|
+
/**
|
|
135
|
+
* @dev Calculate skew price impact for a trade with all parameters
|
|
136
|
+
* @param params Trade parameters including price and version checks
|
|
137
|
+
* @param context Skew price impact context
|
|
138
|
+
* @returns Price impact percentage or 0 if not applicable
|
|
139
|
+
*/
|
|
140
|
+
const getTradeSkewPriceImpactWithChecks = (params, context) => {
|
|
141
|
+
// v10+ trades only
|
|
142
|
+
if (params.contractsVersion < 10) {
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
// Counter trades don't pay skew impact
|
|
146
|
+
if (params.isCounterTrade) {
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
// Calculate position size in tokens
|
|
150
|
+
const positionSizeToken = (0, utils_1.calculatePositionSizeToken)(params.positionSizeCollateral, params.currentPrice);
|
|
151
|
+
// Get skew price impact
|
|
152
|
+
const result = (0, exports.getTradeSkewPriceImpact)(context, {
|
|
153
|
+
collateralIndex: params.collateralIndex,
|
|
154
|
+
pairIndex: params.pairIndex,
|
|
155
|
+
long: params.long,
|
|
156
|
+
open: params.open,
|
|
157
|
+
positionSizeToken,
|
|
158
|
+
});
|
|
159
|
+
return result.priceImpactP;
|
|
160
|
+
};
|
|
161
|
+
exports.getTradeSkewPriceImpactWithChecks = getTradeSkewPriceImpactWithChecks;
|
|
162
|
+
/**
|
|
163
|
+
* @dev Calculate position sizes for partial operations
|
|
164
|
+
* @param originalSizeCollateral Original position size in collateral
|
|
165
|
+
* @param deltaCollateral Position size delta in collateral
|
|
166
|
+
* @param originalSizeToken Original position size in tokens
|
|
167
|
+
* @returns Delta in tokens proportional to collateral delta
|
|
168
|
+
*/
|
|
169
|
+
const calculatePartialSizeToken = (originalSizeCollateral, deltaCollateral, originalSizeToken) => {
|
|
170
|
+
if (originalSizeCollateral === 0) {
|
|
171
|
+
return 0;
|
|
172
|
+
}
|
|
173
|
+
// For partial close/add, token delta is proportional to collateral delta
|
|
174
|
+
return (deltaCollateral * originalSizeToken) / originalSizeCollateral;
|
|
175
|
+
};
|
|
176
|
+
exports.calculatePartialSizeToken = calculatePartialSizeToken;
|
|
177
|
+
// Export namespace for types
|
|
178
|
+
exports.SkewPriceImpact = __importStar(require("./types"));
|
|
179
|
+
__exportStar(require("./fetcher"), exports);
|