@gainsnetwork/sdk 0.0.0-v10.rc7 → 0.0.0-v10.rc9
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/tradingVariables/backend.types.d.ts +1 -0
- package/lib/backend/tradingVariables/converter.d.ts +3 -0
- package/lib/backend/tradingVariables/converter.js +10 -2
- package/lib/backend/tradingVariables/types.d.ts +3 -0
- package/lib/markets/index.d.ts +1 -0
- package/lib/markets/index.js +1 -0
- package/lib/markets/price/builder.d.ts +25 -0
- package/lib/markets/price/builder.js +69 -0
- package/lib/markets/price/index.d.ts +6 -0
- package/lib/markets/price/index.js +22 -0
- package/lib/markets/price/marketPrice.d.ts +12 -0
- package/lib/markets/price/marketPrice.js +34 -0
- package/lib/markets/price/types.d.ts +23 -0
- package/lib/markets/price/types.js +5 -0
- package/lib/trade/fees/fundingFees/index.d.ts +19 -17
- package/lib/trade/fees/fundingFees/index.js +11 -19
- package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
- package/lib/trade/priceImpact/cumulVol/builder.js +43 -0
- package/lib/trade/priceImpact/cumulVol/index.d.ts +1 -0
- package/lib/trade/priceImpact/cumulVol/index.js +7 -4
- package/lib/trade/priceImpact/index.d.ts +5 -4
- package/lib/trade/priceImpact/index.js +10 -7
- package/lib/trade/priceImpact/open/builder.d.ts +21 -0
- package/lib/trade/priceImpact/open/builder.js +43 -0
- package/lib/trade/priceImpact/open/index.d.ts +1 -0
- package/lib/trade/priceImpact/open/index.js +5 -2
- package/lib/trade/priceImpact/open/types.d.ts +3 -2
- package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
- package/lib/trade/priceImpact/skew/builder.js +28 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +11 -42
- package/lib/trade/priceImpact/skew/converter.js +19 -109
- package/lib/trade/priceImpact/skew/fetcher.d.ts +8 -11
- package/lib/trade/priceImpact/skew/fetcher.js +23 -22
- package/lib/trade/priceImpact/skew/index.d.ts +7 -5
- package/lib/trade/priceImpact/skew/index.js +12 -19
- package/lib/trade/priceImpact/skew/types.d.ts +2 -15
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +2 -2
- package/package.json +1 -1
|
@@ -4,9 +4,12 @@
|
|
|
4
4
|
* @dev Mirrors contract's TradingCommonUtils.getTradeOpeningPriceImpact
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = void 0;
|
|
7
|
+
exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = exports.buildTradeOpeningPriceImpactContext = void 0;
|
|
8
8
|
const cumulVol_1 = require("../cumulVol");
|
|
9
9
|
const skew_1 = require("../skew");
|
|
10
|
+
// Export builder
|
|
11
|
+
var builder_1 = require("./builder");
|
|
12
|
+
Object.defineProperty(exports, "buildTradeOpeningPriceImpactContext", { enumerable: true, get: function () { return builder_1.buildTradeOpeningPriceImpactContext; } });
|
|
10
13
|
/**
|
|
11
14
|
* @dev Calculates all price impacts for trade opening
|
|
12
15
|
* @dev Mirrors contract's getTradeOpeningPriceImpact function
|
|
@@ -26,7 +29,7 @@ const getTradeOpeningPriceImpact = (input, context) => {
|
|
|
26
29
|
input.pairIndex, input.long, positionSizeUsd, false, // isPnlPositive - not relevant for opening
|
|
27
30
|
true, // open
|
|
28
31
|
0, // lastPosIncreaseBlock - not relevant for opening
|
|
29
|
-
context);
|
|
32
|
+
context.cumulVolContext);
|
|
30
33
|
// Calculate skew price impact (v10+ only)
|
|
31
34
|
const skewPriceImpactP = (0, skew_1.getTradeSkewPriceImpactWithChecks)({
|
|
32
35
|
collateralIndex: input.collateralIndex,
|
|
@@ -23,9 +23,10 @@ export type TradeOpeningPriceImpactInput = {
|
|
|
23
23
|
* @dev Context for trade opening price impact calculation
|
|
24
24
|
* Combines contexts from spread, cumul vol, and skew
|
|
25
25
|
*/
|
|
26
|
-
export type TradeOpeningPriceImpactContext =
|
|
27
|
-
skewContext: SkewPriceImpactContext;
|
|
26
|
+
export type TradeOpeningPriceImpactContext = {
|
|
28
27
|
collateralPriceUsd: number;
|
|
28
|
+
cumulVolContext: CumulVolContext;
|
|
29
|
+
skewContext: SkewPriceImpactContext;
|
|
29
30
|
};
|
|
30
31
|
/**
|
|
31
32
|
* @dev Result of trade opening price impact calculation
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Builders for skew price impact contexts
|
|
3
|
+
*/
|
|
4
|
+
import { TradingVariablesCollateral } from "../../../backend/tradingVariables/types";
|
|
5
|
+
import { SkewPriceImpactContext } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Builds skew price impact context from trading variables for a specific pair
|
|
8
|
+
* @param tradingVariables Trading variables containing collateral data
|
|
9
|
+
* @param pairIndex Index of the pair to build context for
|
|
10
|
+
* @returns Skew price impact context for the pair
|
|
11
|
+
*/
|
|
12
|
+
export declare const buildSkewPriceImpactContext: (tradingVariables: TradingVariablesCollateral, pairIndex: number) => SkewPriceImpactContext;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Builders for skew price impact contexts
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.buildSkewPriceImpactContext = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Builds skew price impact context from trading variables for a specific pair
|
|
9
|
+
* @param tradingVariables Trading variables containing collateral data
|
|
10
|
+
* @param pairIndex Index of the pair to build context for
|
|
11
|
+
* @returns Skew price impact context for the pair
|
|
12
|
+
*/
|
|
13
|
+
const buildSkewPriceImpactContext = (tradingVariables, pairIndex) => {
|
|
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];
|
|
17
|
+
if (!pairOi) {
|
|
18
|
+
throw new Error(`Pair OI data not found for pair index ${pairIndex}`);
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
skewDepth,
|
|
22
|
+
pairOiToken: {
|
|
23
|
+
oiLongToken: pairOi.token.long,
|
|
24
|
+
oiShortToken: pairOi.token.short,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
exports.buildSkewPriceImpactContext = buildSkewPriceImpactContext;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @dev All BigNumber values are normalized to floats with appropriate precision
|
|
4
4
|
*/
|
|
5
5
|
import { IPriceImpact } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
6
|
-
import { PairOiToken, PairOiCollateral
|
|
6
|
+
import { PairOiToken, PairOiCollateral } from "./types";
|
|
7
7
|
/**
|
|
8
8
|
* @dev Converts contract pair OI token data to SDK format
|
|
9
9
|
* @param contractData Contract pair OI token struct
|
|
@@ -31,47 +31,16 @@ export declare const convertPairOiCollateral: (contractData: IPriceImpact.PairOi
|
|
|
31
31
|
*/
|
|
32
32
|
export declare const convertPairOiCollateralArray: (contractDataArray: IPriceImpact.PairOiCollateralStruct[], collateralDecimals: number[]) => PairOiCollateral[];
|
|
33
33
|
/**
|
|
34
|
-
* @dev
|
|
35
|
-
* @param depth Skew depth from contract (in
|
|
36
|
-
* @
|
|
37
|
-
* @returns Normalized skew depth
|
|
34
|
+
* @dev Converts skew depth from contract format to SDK format
|
|
35
|
+
* @param depth Skew depth from contract (in token units with 1e18 precision)
|
|
36
|
+
* @returns Normalized skew depth in tokens
|
|
38
37
|
*/
|
|
39
|
-
export declare const
|
|
38
|
+
export declare const convertSkewDepth: (depth: string) => number;
|
|
40
39
|
/**
|
|
41
|
-
* @dev
|
|
42
|
-
* @param
|
|
43
|
-
* @
|
|
44
|
-
* @param depth Normalized depth value
|
|
45
|
-
* @returns Skew depth object
|
|
40
|
+
* @dev Converts array of skew depths from contract format to SDK format
|
|
41
|
+
* @param depths Array of skew depths from contract (in token units with 1e18 precision)
|
|
42
|
+
* @returns Object mapping pair index to normalized depth
|
|
46
43
|
*/
|
|
47
|
-
export declare const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
* @param collateralIndices Array of collateral indices
|
|
51
|
-
* @param pairIndices Array of pair indices
|
|
52
|
-
* @param skewDepths Array of normalized skew depths
|
|
53
|
-
* @param pairOiTokens Array of pair OI token data
|
|
54
|
-
* @returns Complete skew price impact context
|
|
55
|
-
*/
|
|
56
|
-
export declare const createSkewPriceImpactContext: (collateralIndices: number[], pairIndices: number[], skewDepths: number[], pairOiTokens: PairOiToken[]) => SkewPriceImpactContext;
|
|
57
|
-
/**
|
|
58
|
-
* @dev Validates skew depth is within reasonable bounds
|
|
59
|
-
* @param depth Normalized skew depth
|
|
60
|
-
* @param minDepth Minimum allowed depth (default: 0)
|
|
61
|
-
* @param maxDepth Maximum allowed depth (default: 1e12)
|
|
62
|
-
* @returns Whether depth is valid
|
|
63
|
-
*/
|
|
64
|
-
export declare const isValidSkewDepth: (depth: number, minDepth?: number, maxDepth?: number) => boolean;
|
|
65
|
-
/**
|
|
66
|
-
* @dev Converts contract skew depths array to normalized values
|
|
67
|
-
* @param contractDepths Array of depths from contract
|
|
68
|
-
* @param collateralDecimals Array of decimals for each collateral
|
|
69
|
-
* @returns Array of normalized depths
|
|
70
|
-
*/
|
|
71
|
-
export declare const convertSkewDepthsArray: (contractDepths: Array<bigint | number | string>, collateralDecimals: number[]) => number[];
|
|
72
|
-
/**
|
|
73
|
-
* @dev Merges multiple contexts into one
|
|
74
|
-
* @param contexts Array of contexts to merge
|
|
75
|
-
* @returns Merged context
|
|
76
|
-
*/
|
|
77
|
-
export declare const mergeSkewPriceImpactContexts: (contexts: SkewPriceImpactContext[]) => SkewPriceImpactContext;
|
|
44
|
+
export declare const convertPairSkewDepths: (depths: string[]) => {
|
|
45
|
+
[pairIndex: number]: number;
|
|
46
|
+
};
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @dev All BigNumber values are normalized to floats with appropriate precision
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.
|
|
7
|
+
exports.convertPairSkewDepths = exports.convertSkewDepth = exports.convertPairOiCollateralArray = exports.convertPairOiCollateral = exports.convertPairOiTokenArray = exports.convertPairOiToken = void 0;
|
|
8
8
|
/**
|
|
9
9
|
* @dev Converts contract pair OI token data to SDK format
|
|
10
10
|
* @param contractData Contract pair OI token struct
|
|
@@ -55,117 +55,27 @@ const convertPairOiCollateralArray = (contractDataArray, collateralDecimals) =>
|
|
|
55
55
|
};
|
|
56
56
|
exports.convertPairOiCollateralArray = convertPairOiCollateralArray;
|
|
57
57
|
/**
|
|
58
|
-
* @dev
|
|
59
|
-
* @param depth Skew depth from contract (in
|
|
60
|
-
* @
|
|
61
|
-
* @returns Normalized skew depth
|
|
58
|
+
* @dev Converts skew depth from contract format to SDK format
|
|
59
|
+
* @param depth Skew depth from contract (in token units with 1e18 precision)
|
|
60
|
+
* @returns Normalized skew depth in tokens
|
|
62
61
|
*/
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
return Number(depth) /
|
|
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]));
|
|
62
|
+
const convertSkewDepth = (depth) => {
|
|
63
|
+
// Token depths are always stored with 1e18 precision in the contract
|
|
64
|
+
return Number(depth) / 1e18;
|
|
141
65
|
};
|
|
142
|
-
exports.
|
|
66
|
+
exports.convertSkewDepth = convertSkewDepth;
|
|
143
67
|
/**
|
|
144
|
-
* @dev
|
|
145
|
-
* @param
|
|
146
|
-
* @returns
|
|
68
|
+
* @dev Converts array of skew depths from contract format to SDK format
|
|
69
|
+
* @param depths Array of skew depths from contract (in token units with 1e18 precision)
|
|
70
|
+
* @returns Object mapping pair index to normalized depth
|
|
147
71
|
*/
|
|
148
|
-
const
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
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]);
|
|
72
|
+
const convertPairSkewDepths = (depths) => {
|
|
73
|
+
const result = {};
|
|
74
|
+
depths.forEach((depth, index) => {
|
|
75
|
+
if (depth && depth !== "0") {
|
|
76
|
+
result[index] = (0, exports.convertSkewDepth)(depth);
|
|
167
77
|
}
|
|
168
|
-
}
|
|
169
|
-
return
|
|
78
|
+
});
|
|
79
|
+
return result;
|
|
170
80
|
};
|
|
171
|
-
exports.
|
|
81
|
+
exports.convertPairSkewDepths = convertPairSkewDepths;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { GNSMultiCollatDiamond } from "../../../contracts/types/generated";
|
|
2
|
-
import type { PairOiToken, SkewPriceImpactContext } from "
|
|
2
|
+
import type { PairOiToken, SkewPriceImpactContext } from "./types";
|
|
3
3
|
/**
|
|
4
4
|
* @dev Fetches pair open interest in tokens for a specific pair
|
|
5
5
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
@@ -21,28 +21,25 @@ export declare const fetchPairOisAfterV10Token: (contract: GNSMultiCollatDiamond
|
|
|
21
21
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
22
22
|
* @param collateralIndex Collateral index
|
|
23
23
|
* @param pairIndex Pair index
|
|
24
|
-
* @param collateralDecimals Number of decimals for the collateral
|
|
25
24
|
* @returns Promise resolving to normalized skew depth
|
|
26
25
|
*/
|
|
27
|
-
export declare const fetchPairSkewDepth: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number
|
|
26
|
+
export declare const fetchPairSkewDepth: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number) => Promise<number>;
|
|
28
27
|
/**
|
|
29
28
|
* @dev Fetches skew depths for multiple pairs
|
|
30
29
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
31
30
|
* @param collateralIndices Array of collateral indices
|
|
32
31
|
* @param pairIndices Array of pair indices
|
|
33
|
-
* @param collateralDecimals Array of collateral decimals for each pair
|
|
34
32
|
* @returns Promise resolving to array of normalized skew depths
|
|
35
33
|
*/
|
|
36
|
-
export declare const fetchPairSkewDepths: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]
|
|
34
|
+
export declare const fetchPairSkewDepths: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<number[]>;
|
|
37
35
|
/**
|
|
38
|
-
* @dev Fetches
|
|
36
|
+
* @dev Fetches skew price impact context for a single pair
|
|
39
37
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
40
|
-
* @param
|
|
41
|
-
* @param
|
|
42
|
-
* @
|
|
43
|
-
* @returns Promise resolving to complete skew price impact context
|
|
38
|
+
* @param collateralIndex Collateral index
|
|
39
|
+
* @param pairIndex Pair index
|
|
40
|
+
* @returns Promise resolving to skew price impact context
|
|
44
41
|
*/
|
|
45
|
-
export declare const fetchSkewPriceImpactContext: (contract: GNSMultiCollatDiamond,
|
|
42
|
+
export declare const fetchSkewPriceImpactContext: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number) => Promise<SkewPriceImpactContext>;
|
|
46
43
|
/**
|
|
47
44
|
* @dev Fetches collateral decimals for given collateral indices
|
|
48
45
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.calculateTradeSkewPriceImpact = exports.fetchCollateralDecimals = exports.fetchSkewPriceImpactContext = exports.fetchPairSkewDepths = exports.fetchPairSkewDepth = exports.fetchPairOisAfterV10Token = exports.fetchPairOiAfterV10Token = void 0;
|
|
13
|
-
const
|
|
13
|
+
const converter_1 = require("./converter");
|
|
14
14
|
/**
|
|
15
15
|
* @dev Fetches pair open interest in tokens for a specific pair
|
|
16
16
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
@@ -21,7 +21,7 @@ const __1 = require("..");
|
|
|
21
21
|
const fetchPairOiAfterV10Token = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
22
|
try {
|
|
23
23
|
const contractData = yield contract.getPairOiAfterV10Token(collateralIndex, pairIndex);
|
|
24
|
-
return (0,
|
|
24
|
+
return (0, converter_1.convertPairOiToken)(contractData);
|
|
25
25
|
}
|
|
26
26
|
catch (error) {
|
|
27
27
|
console.error("Error fetching pair OI token:", error);
|
|
@@ -42,7 +42,7 @@ const fetchPairOisAfterV10Token = (contract, collateralIndices, pairIndices) =>
|
|
|
42
42
|
}
|
|
43
43
|
try {
|
|
44
44
|
const contractDataArray = yield contract.getPairOisAfterV10Token(collateralIndices, pairIndices);
|
|
45
|
-
return contractDataArray.map(
|
|
45
|
+
return contractDataArray.map(converter_1.convertPairOiToken);
|
|
46
46
|
}
|
|
47
47
|
catch (error) {
|
|
48
48
|
console.error("Error fetching pair OIs token:", error);
|
|
@@ -55,13 +55,13 @@ exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
|
|
|
55
55
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
56
56
|
* @param collateralIndex Collateral index
|
|
57
57
|
* @param pairIndex Pair index
|
|
58
|
-
* @param collateralDecimals Number of decimals for the collateral
|
|
59
58
|
* @returns Promise resolving to normalized skew depth
|
|
60
59
|
*/
|
|
61
|
-
const fetchPairSkewDepth = (contract, collateralIndex, pairIndex
|
|
60
|
+
const fetchPairSkewDepth = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
61
|
try {
|
|
63
62
|
const contractDepth = yield contract.getPairSkewDepth(collateralIndex, pairIndex);
|
|
64
|
-
|
|
63
|
+
// Token depths are always 1e18 precision
|
|
64
|
+
return (0, converter_1.convertSkewDepth)(contractDepth.toString());
|
|
65
65
|
}
|
|
66
66
|
catch (error) {
|
|
67
67
|
console.error("Error fetching skew depth:", error);
|
|
@@ -74,17 +74,16 @@ exports.fetchPairSkewDepth = fetchPairSkewDepth;
|
|
|
74
74
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
75
75
|
* @param collateralIndices Array of collateral indices
|
|
76
76
|
* @param pairIndices Array of pair indices
|
|
77
|
-
* @param collateralDecimals Array of collateral decimals for each pair
|
|
78
77
|
* @returns Promise resolving to array of normalized skew depths
|
|
79
78
|
*/
|
|
80
|
-
const fetchPairSkewDepths = (contract, collateralIndices, pairIndices
|
|
81
|
-
if (collateralIndices.length !== pairIndices.length
|
|
82
|
-
pairIndices.length !== collateralDecimals.length) {
|
|
79
|
+
const fetchPairSkewDepths = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
80
|
+
if (collateralIndices.length !== pairIndices.length) {
|
|
83
81
|
throw new Error("All input arrays must have the same length");
|
|
84
82
|
}
|
|
85
83
|
try {
|
|
86
84
|
const contractDepths = yield contract.getPairSkewDepths(collateralIndices, pairIndices);
|
|
87
|
-
|
|
85
|
+
// Token depths are always 1e18 precision
|
|
86
|
+
return contractDepths.map(depth => (0, converter_1.convertSkewDepth)(depth.toString()));
|
|
88
87
|
}
|
|
89
88
|
catch (error) {
|
|
90
89
|
console.error("Error fetching skew depths:", error);
|
|
@@ -93,21 +92,23 @@ const fetchPairSkewDepths = (contract, collateralIndices, pairIndices, collatera
|
|
|
93
92
|
});
|
|
94
93
|
exports.fetchPairSkewDepths = fetchPairSkewDepths;
|
|
95
94
|
/**
|
|
96
|
-
* @dev Fetches
|
|
95
|
+
* @dev Fetches skew price impact context for a single pair
|
|
97
96
|
* @param contract GNSMultiCollatDiamond contract instance
|
|
98
|
-
* @param
|
|
99
|
-
* @param
|
|
100
|
-
* @
|
|
101
|
-
* @returns Promise resolving to complete skew price impact context
|
|
97
|
+
* @param collateralIndex Collateral index
|
|
98
|
+
* @param pairIndex Pair index
|
|
99
|
+
* @returns Promise resolving to skew price impact context
|
|
102
100
|
*/
|
|
103
|
-
const fetchSkewPriceImpactContext = (contract,
|
|
101
|
+
const fetchSkewPriceImpactContext = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
102
|
try {
|
|
105
|
-
// Fetch OI data and skew
|
|
106
|
-
const [
|
|
107
|
-
(0, exports.
|
|
108
|
-
(0, exports.
|
|
103
|
+
// Fetch OI data and skew depth in parallel
|
|
104
|
+
const [pairOiToken, skewDepth] = yield Promise.all([
|
|
105
|
+
(0, exports.fetchPairOiAfterV10Token)(contract, collateralIndex, pairIndex),
|
|
106
|
+
(0, exports.fetchPairSkewDepth)(contract, collateralIndex, pairIndex),
|
|
109
107
|
]);
|
|
110
|
-
return
|
|
108
|
+
return {
|
|
109
|
+
skewDepth,
|
|
110
|
+
pairOiToken,
|
|
111
|
+
};
|
|
111
112
|
}
|
|
112
113
|
catch (error) {
|
|
113
114
|
console.error("Error fetching skew price impact context:", error);
|
|
@@ -10,10 +10,10 @@ import { PairOiToken, SkewPriceImpactInput, SkewPriceImpactResult, SkewPriceImpa
|
|
|
10
10
|
*/
|
|
11
11
|
export declare const getNetSkewToken: (pairOi: PairOiToken) => number;
|
|
12
12
|
/**
|
|
13
|
-
* @dev Calculates net skew in collateral
|
|
13
|
+
* @dev Calculates net skew in collateral
|
|
14
14
|
* @param netSkewToken Net skew in tokens
|
|
15
15
|
* @param currentPrice Current pair price
|
|
16
|
-
* @returns Net skew in collateral
|
|
16
|
+
* @returns Net skew in collateral
|
|
17
17
|
*/
|
|
18
18
|
export declare const getNetSkewCollateral: (netSkewToken: number, currentPrice: number) => number;
|
|
19
19
|
/**
|
|
@@ -27,18 +27,18 @@ export declare const getTradeSkewDirection: (long: boolean, open: boolean) => bo
|
|
|
27
27
|
* @dev Core skew price impact calculation
|
|
28
28
|
* @param existingSkewToken Current net skew in tokens (signed)
|
|
29
29
|
* @param tradeSizeToken Trade size in tokens (always positive)
|
|
30
|
-
* @param skewDepth Skew depth in
|
|
30
|
+
* @param skewDepth Skew depth in tokens
|
|
31
31
|
* @param tradeIncreasesSkew Whether trade increases skew in its direction
|
|
32
32
|
* @returns Price impact percentage (can be positive or negative)
|
|
33
33
|
*/
|
|
34
34
|
export declare const calculateSkewPriceImpactP: (existingSkewToken: number, tradeSizeToken: number, skewDepth: number, tradeIncreasesSkew: boolean) => number;
|
|
35
35
|
/**
|
|
36
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
37
|
* @param input Trade parameters
|
|
38
|
+
* @param context Skew price impact context with depths and OI data
|
|
39
39
|
* @returns Skew price impact result
|
|
40
40
|
*/
|
|
41
|
-
export declare const getTradeSkewPriceImpact: (
|
|
41
|
+
export declare const getTradeSkewPriceImpact: (input: SkewPriceImpactInput, context: SkewPriceImpactContext) => SkewPriceImpactResult;
|
|
42
42
|
/**
|
|
43
43
|
* @dev Calculate skew price impact for a trade with all parameters
|
|
44
44
|
* @param params Trade parameters including price and version checks
|
|
@@ -55,4 +55,6 @@ export declare const getTradeSkewPriceImpactWithChecks: (params: TradeSkewParams
|
|
|
55
55
|
*/
|
|
56
56
|
export declare const calculatePartialSizeToken: (originalSizeCollateral: number, deltaCollateral: number, originalSizeToken: number) => number;
|
|
57
57
|
export * as SkewPriceImpact from "./types";
|
|
58
|
+
export * from "./converter";
|
|
59
|
+
export * from "./builder";
|
|
58
60
|
export * from "./fetcher";
|
|
@@ -45,10 +45,10 @@ const getNetSkewToken = (pairOi) => {
|
|
|
45
45
|
};
|
|
46
46
|
exports.getNetSkewToken = getNetSkewToken;
|
|
47
47
|
/**
|
|
48
|
-
* @dev Calculates net skew in collateral
|
|
48
|
+
* @dev Calculates net skew in collateral
|
|
49
49
|
* @param netSkewToken Net skew in tokens
|
|
50
50
|
* @param currentPrice Current pair price
|
|
51
|
-
* @returns Net skew in collateral
|
|
51
|
+
* @returns Net skew in collateral
|
|
52
52
|
*/
|
|
53
53
|
const getNetSkewCollateral = (netSkewToken, currentPrice) => {
|
|
54
54
|
return netSkewToken * currentPrice;
|
|
@@ -70,7 +70,7 @@ exports.getTradeSkewDirection = getTradeSkewDirection;
|
|
|
70
70
|
* @dev Core skew price impact calculation
|
|
71
71
|
* @param existingSkewToken Current net skew in tokens (signed)
|
|
72
72
|
* @param tradeSizeToken Trade size in tokens (always positive)
|
|
73
|
-
* @param skewDepth Skew depth in
|
|
73
|
+
* @param skewDepth Skew depth in tokens
|
|
74
74
|
* @param tradeIncreasesSkew Whether trade increases skew in its direction
|
|
75
75
|
* @returns Price impact percentage (can be positive or negative)
|
|
76
76
|
*/
|
|
@@ -84,29 +84,20 @@ const calculateSkewPriceImpactP = (existingSkewToken, tradeSizeToken, skewDepth,
|
|
|
84
84
|
const signedTradeSize = tradeSizeToken * tradeSkewMultiplier;
|
|
85
85
|
// Formula: (existingSkew + tradeSize/2) / skewDepth
|
|
86
86
|
const numerator = signedExistingSkew + signedTradeSize / 2;
|
|
87
|
-
const priceImpactP =
|
|
87
|
+
const priceImpactP = numerator / skewDepth;
|
|
88
88
|
// Apply divider to match cumulative volume impact scale
|
|
89
89
|
return priceImpactP / PRICE_IMPACT_DIVIDER;
|
|
90
90
|
};
|
|
91
91
|
exports.calculateSkewPriceImpactP = calculateSkewPriceImpactP;
|
|
92
92
|
/**
|
|
93
93
|
* @dev Main function to calculate skew price impact for a trade
|
|
94
|
-
* @param context Skew price impact context with depths and OI data
|
|
95
94
|
* @param input Trade parameters
|
|
95
|
+
* @param context Skew price impact context with depths and OI data
|
|
96
96
|
* @returns Skew price impact result
|
|
97
97
|
*/
|
|
98
|
-
const getTradeSkewPriceImpact = (
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const skewDepth = (_a = context.skewDepths[input.collateralIndex]) === null || _a === void 0 ? void 0 : _a[input.pairIndex];
|
|
102
|
-
if (skewDepth === undefined) {
|
|
103
|
-
throw new Error(`Missing skew depth for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
104
|
-
}
|
|
105
|
-
// Get pair OI data
|
|
106
|
-
const pairOi = (_b = context.pairOiTokens[input.collateralIndex]) === null || _b === void 0 ? void 0 : _b[input.pairIndex];
|
|
107
|
-
if (!pairOi) {
|
|
108
|
-
throw new Error(`Missing pair OI data for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
109
|
-
}
|
|
98
|
+
const getTradeSkewPriceImpact = (input, context) => {
|
|
99
|
+
// Get skew depth and pair OI from simplified context
|
|
100
|
+
const { skewDepth, pairOiToken: pairOi } = context;
|
|
110
101
|
// Calculate net skew
|
|
111
102
|
const netSkewToken = (0, exports.getNetSkewToken)(pairOi);
|
|
112
103
|
// Determine trade direction impact
|
|
@@ -150,13 +141,13 @@ const getTradeSkewPriceImpactWithChecks = (params, context) => {
|
|
|
150
141
|
// Calculate position size in tokens
|
|
151
142
|
const positionSizeToken = (0, utils_1.calculatePositionSizeToken)(params.positionSizeCollateral, params.currentPrice);
|
|
152
143
|
// Get skew price impact
|
|
153
|
-
const result = (0, exports.getTradeSkewPriceImpact)(
|
|
144
|
+
const result = (0, exports.getTradeSkewPriceImpact)({
|
|
154
145
|
collateralIndex: params.collateralIndex,
|
|
155
146
|
pairIndex: params.pairIndex,
|
|
156
147
|
long: params.long,
|
|
157
148
|
open: params.open,
|
|
158
149
|
positionSizeToken,
|
|
159
|
-
});
|
|
150
|
+
}, context);
|
|
160
151
|
return result.priceImpactP;
|
|
161
152
|
};
|
|
162
153
|
exports.getTradeSkewPriceImpactWithChecks = getTradeSkewPriceImpactWithChecks;
|
|
@@ -177,4 +168,6 @@ const calculatePartialSizeToken = (originalSizeCollateral, deltaCollateral, orig
|
|
|
177
168
|
exports.calculatePartialSizeToken = calculatePartialSizeToken;
|
|
178
169
|
// Export namespace for types
|
|
179
170
|
exports.SkewPriceImpact = __importStar(require("./types"));
|
|
171
|
+
__exportStar(require("./converter"), exports);
|
|
172
|
+
__exportStar(require("./builder"), exports);
|
|
180
173
|
__exportStar(require("./fetcher"), exports);
|
|
@@ -9,11 +9,6 @@ export type PairOiCollateral = {
|
|
|
9
9
|
oiLongCollateral: number;
|
|
10
10
|
oiShortCollateral: number;
|
|
11
11
|
};
|
|
12
|
-
export type SkewDepth = {
|
|
13
|
-
collateralIndex: number;
|
|
14
|
-
pairIndex: number;
|
|
15
|
-
depth: number;
|
|
16
|
-
};
|
|
17
12
|
export type SkewPriceImpactInput = {
|
|
18
13
|
collateralIndex: number;
|
|
19
14
|
pairIndex: number;
|
|
@@ -28,16 +23,8 @@ export type SkewPriceImpactResult = {
|
|
|
28
23
|
tradeDirection: "increase" | "decrease" | "neutral";
|
|
29
24
|
};
|
|
30
25
|
export type SkewPriceImpactContext = {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
[pairIndex: number]: number;
|
|
34
|
-
};
|
|
35
|
-
};
|
|
36
|
-
pairOiTokens: {
|
|
37
|
-
[collateralIndex: number]: {
|
|
38
|
-
[pairIndex: number]: PairOiToken;
|
|
39
|
-
};
|
|
40
|
-
};
|
|
26
|
+
skewDepth: number;
|
|
27
|
+
pairOiToken: PairOiToken;
|
|
41
28
|
};
|
|
42
29
|
export type TradeSkewParams = {
|
|
43
30
|
collateralIndex: number;
|
package/lib/vault/index.d.ts
CHANGED
|
@@ -4,5 +4,7 @@ type GetPendingAccBlockWeightedMarketCap = {
|
|
|
4
4
|
accBlockWeightedMarketCapLastStored: number;
|
|
5
5
|
marketCapPrecision?: number;
|
|
6
6
|
};
|
|
7
|
-
export declare const getPendingAccBlockWeightedMarketCap: (
|
|
7
|
+
export declare const getPendingAccBlockWeightedMarketCap: (input: {
|
|
8
|
+
currentBlock: number;
|
|
9
|
+
}, context: GetPendingAccBlockWeightedMarketCap) => number;
|
|
8
10
|
export {};
|
package/lib/vault/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getPendingAccBlockWeightedMarketCap = void 0;
|
|
4
|
-
const getPendingAccBlockWeightedMarketCap = (
|
|
4
|
+
const getPendingAccBlockWeightedMarketCap = (input, context) => {
|
|
5
5
|
const { marketCap, accBlockWeightedMarketCap, accBlockWeightedMarketCapLastStored, marketCapPrecision, } = context;
|
|
6
6
|
return (accBlockWeightedMarketCap +
|
|
7
|
-
(currentBlock - accBlockWeightedMarketCapLastStored) /
|
|
7
|
+
(input.currentBlock - accBlockWeightedMarketCapLastStored) /
|
|
8
8
|
Math.max(marketCap * (marketCapPrecision || 1e18), 1));
|
|
9
9
|
};
|
|
10
10
|
exports.getPendingAccBlockWeightedMarketCap = getPendingAccBlockWeightedMarketCap;
|