@gainsnetwork/sdk 0.0.0-v10.rc7 → 0.0.0-v10.rc8
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/index.d.ts +3 -2
- package/lib/trade/priceImpact/index.js +6 -7
- 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 +11 -18
- 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
|
@@ -6,6 +6,9 @@ export declare const convertFees: (fees: FeeBackend[]) => Fee[];
|
|
|
6
6
|
export declare const convertCollaterals: (collaterals: CollateralBackend[]) => TradingVariablesCollateral[];
|
|
7
7
|
export declare const convertOpenInterests: (interests: OpenInterestBackend[]) => OpenInterest[];
|
|
8
8
|
export declare const convertPairDepths: (pairDepths: PairDepthBackend[]) => PairDepth[];
|
|
9
|
+
export declare const convertPairSkewDepths: (pairSkewDepths: string[] | undefined) => {
|
|
10
|
+
[pairIndex: number]: number;
|
|
11
|
+
};
|
|
9
12
|
export declare const convertPairBorrowingFees: (pairParams: PairParamsBorrowingFeesBackend) => BorrowingFee.Pair[];
|
|
10
13
|
export declare const convertGroupBorrowingFees: (pairParams: PairParamsBorrowingFeesBackend) => BorrowingFee.Group[];
|
|
11
14
|
export declare const convertTradingGroups: (groups: TradingGroupBackend[]) => TradingGroup[];
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertLiquidationParams = exports.convertPairName = exports.convertFeePerBlockCap = exports.convertMaxLeverages = exports.convertGlobalTradeFeeParams = exports.convertTraderFeeTiers = exports.convertFeeTiers = exports.convertCollateralConfig = exports.convertOiWindowsSettings = exports.convertOiWindows = exports.convertPairOi = exports.convertContestLeaderboardEntry = exports.generateStockPairToActiveStockSplit = exports.convertTradeInitialAccFees = exports.convertTradeInfo = exports.convertTrade = exports.convertPairFactor = exports.convertTradeContainer = exports.convertTradesAndLimitOrders = exports.convertTradingPairs = exports.convertTradingGroups = exports.convertGroupBorrowingFees = exports.convertPairBorrowingFees = exports.convertPairDepths = exports.convertOpenInterests = exports.convertCollaterals = exports.convertFees = void 0;
|
|
3
|
+
exports.convertLiquidationParams = exports.convertPairName = exports.convertFeePerBlockCap = exports.convertMaxLeverages = exports.convertGlobalTradeFeeParams = exports.convertTraderFeeTiers = exports.convertFeeTiers = exports.convertCollateralConfig = exports.convertOiWindowsSettings = exports.convertOiWindows = exports.convertPairOi = exports.convertContestLeaderboardEntry = exports.generateStockPairToActiveStockSplit = exports.convertTradeInitialAccFees = exports.convertTradeInfo = exports.convertTrade = exports.convertPairFactor = exports.convertTradeContainer = exports.convertTradesAndLimitOrders = exports.convertTradingPairs = exports.convertTradingGroups = exports.convertGroupBorrowingFees = exports.convertPairBorrowingFees = exports.convertPairSkewDepths = exports.convertPairDepths = exports.convertOpenInterests = exports.convertCollaterals = exports.convertFees = void 0;
|
|
4
4
|
const __1 = require("../../");
|
|
5
5
|
Object.defineProperty(exports, "convertLiquidationParams", { enumerable: true, get: function () { return __1.convertLiquidationParams; } });
|
|
6
|
+
const converter_1 = require("../../trade/priceImpact/skew/converter");
|
|
6
7
|
const convertFees = (fees) => fees === null || fees === void 0 ? void 0 : fees.map(fee => convertFee(fee));
|
|
7
8
|
exports.convertFees = convertFees;
|
|
8
9
|
const convertCollateral = (collateral) => {
|
|
@@ -30,7 +31,8 @@ const convertCollateral = (collateral) => {
|
|
|
30
31
|
params: (0, __1.convertFundingFeeParamsArray)((_h = collateral.fundingFees) === null || _h === void 0 ? void 0 : _h.pairParams),
|
|
31
32
|
data: (0, __1.convertPairFundingFeeDataArray)((_j = collateral.fundingFees) === null || _j === void 0 ? void 0 : _j.pairData),
|
|
32
33
|
},
|
|
33
|
-
pairOis: (0, __1.convertPairOiArray)(collateral.pairOis, collateral.collateralConfig.
|
|
34
|
+
pairOis: (0, __1.convertPairOiArray)(collateral.pairOis, parseInt(collateral.collateralConfig.precision)),
|
|
35
|
+
pairSkewDepths: (0, exports.convertPairSkewDepths)(collateral.pairSkewDepths),
|
|
34
36
|
});
|
|
35
37
|
};
|
|
36
38
|
const convertCollaterals = (collaterals) => collaterals === null || collaterals === void 0 ? void 0 : collaterals.map(collateral => convertCollateral(collateral));
|
|
@@ -54,6 +56,12 @@ const convertPairDepth = (pairDepth) => ({
|
|
|
54
56
|
onePercentDepthAboveUsd: parseInt(pairDepth.onePercentDepthAboveUsd),
|
|
55
57
|
onePercentDepthBelowUsd: parseInt(pairDepth.onePercentDepthBelowUsd),
|
|
56
58
|
});
|
|
59
|
+
const convertPairSkewDepths = (pairSkewDepths) => {
|
|
60
|
+
if (!pairSkewDepths)
|
|
61
|
+
return {};
|
|
62
|
+
return (0, converter_1.convertPairSkewDepths)(pairSkewDepths);
|
|
63
|
+
};
|
|
64
|
+
exports.convertPairSkewDepths = convertPairSkewDepths;
|
|
57
65
|
const convertPairBorrowingFees = (pairParams) => pairParams === null || pairParams === void 0 ? void 0 : pairParams.pairs.map(pairParam => convertPairBorrowingFee(pairParam));
|
|
58
66
|
exports.convertPairBorrowingFees = convertPairBorrowingFees;
|
|
59
67
|
const convertPairGroupBorrowingFee = (pairParam) => ({
|
package/lib/markets/index.d.ts
CHANGED
package/lib/markets/index.js
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Builder functions for creating market price contexts
|
|
3
|
+
*/
|
|
4
|
+
import { MarketPriceContext } from "./types";
|
|
5
|
+
import { TradingVariablesCollateral } from "../../backend/tradingVariables/types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Builds a market price context for a specific pair from trading variables
|
|
8
|
+
* @param tradingVariables Trading variables for a specific collateral
|
|
9
|
+
* @param pairIndex Pair index to create context for
|
|
10
|
+
* @returns Market price context for the pair
|
|
11
|
+
*/
|
|
12
|
+
export declare const buildMarketPriceContext: (tradingVariables: TradingVariablesCollateral, pairIndex: number) => MarketPriceContext;
|
|
13
|
+
/**
|
|
14
|
+
* @dev Creates an empty market price context
|
|
15
|
+
* @returns Empty market price context
|
|
16
|
+
*/
|
|
17
|
+
export declare const createEmptyMarketPriceContext: () => MarketPriceContext;
|
|
18
|
+
/**
|
|
19
|
+
* @dev Creates a market price context with custom values
|
|
20
|
+
* @param skewDepth Skew depth in tokens
|
|
21
|
+
* @param oiLongToken Long OI in tokens
|
|
22
|
+
* @param oiShortToken Short OI in tokens
|
|
23
|
+
* @returns Market price context
|
|
24
|
+
*/
|
|
25
|
+
export declare const createMarketPriceContext: (skewDepth: number, oiLongToken: number, oiShortToken: number) => MarketPriceContext;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Builder functions for creating market price contexts
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createMarketPriceContext = exports.createEmptyMarketPriceContext = exports.buildMarketPriceContext = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Builds a market price context for a specific pair from trading variables
|
|
9
|
+
* @param tradingVariables Trading variables for a specific collateral
|
|
10
|
+
* @param pairIndex Pair index to create context for
|
|
11
|
+
* @returns Market price context for the pair
|
|
12
|
+
*/
|
|
13
|
+
const buildMarketPriceContext = (tradingVariables, pairIndex) => {
|
|
14
|
+
// Get skew depth for the pair, default to 0 if not set
|
|
15
|
+
const skewDepth = tradingVariables.pairSkewDepths[pairIndex] || 0;
|
|
16
|
+
// Get pair OI data
|
|
17
|
+
const pairOi = tradingVariables.pairOis[pairIndex];
|
|
18
|
+
if (!pairOi) {
|
|
19
|
+
// Return default context if no OI data
|
|
20
|
+
return {
|
|
21
|
+
skewDepth,
|
|
22
|
+
pairOiToken: {
|
|
23
|
+
oiLongToken: 0,
|
|
24
|
+
oiShortToken: 0,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// Extract token OI from unified pair OI
|
|
29
|
+
const pairOiToken = {
|
|
30
|
+
oiLongToken: pairOi.token.long,
|
|
31
|
+
oiShortToken: pairOi.token.short,
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
skewDepth,
|
|
35
|
+
pairOiToken,
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
exports.buildMarketPriceContext = buildMarketPriceContext;
|
|
39
|
+
/**
|
|
40
|
+
* @dev Creates an empty market price context
|
|
41
|
+
* @returns Empty market price context
|
|
42
|
+
*/
|
|
43
|
+
const createEmptyMarketPriceContext = () => {
|
|
44
|
+
return {
|
|
45
|
+
skewDepth: 0,
|
|
46
|
+
pairOiToken: {
|
|
47
|
+
oiLongToken: 0,
|
|
48
|
+
oiShortToken: 0,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
exports.createEmptyMarketPriceContext = createEmptyMarketPriceContext;
|
|
53
|
+
/**
|
|
54
|
+
* @dev Creates a market price context with custom values
|
|
55
|
+
* @param skewDepth Skew depth in tokens
|
|
56
|
+
* @param oiLongToken Long OI in tokens
|
|
57
|
+
* @param oiShortToken Short OI in tokens
|
|
58
|
+
* @returns Market price context
|
|
59
|
+
*/
|
|
60
|
+
const createMarketPriceContext = (skewDepth, oiLongToken, oiShortToken) => {
|
|
61
|
+
return {
|
|
62
|
+
skewDepth,
|
|
63
|
+
pairOiToken: {
|
|
64
|
+
oiLongToken,
|
|
65
|
+
oiShortToken,
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
exports.createMarketPriceContext = createMarketPriceContext;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Market price module exports
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
__exportStar(require("./types"), exports);
|
|
21
|
+
__exportStar(require("./marketPrice"), exports);
|
|
22
|
+
__exportStar(require("./builder"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Current market price calculation with skew impact
|
|
3
|
+
*/
|
|
4
|
+
import { MarketPriceResult, MarketPriceContext } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* @dev Calculates the current market price adjusted for skew impact
|
|
7
|
+
* @param pairIndex Trading pair index
|
|
8
|
+
* @param oraclePrice Oracle price for the pair
|
|
9
|
+
* @param context Market price context with depths and OI data
|
|
10
|
+
* @returns Current market price with skew impact applied
|
|
11
|
+
*/
|
|
12
|
+
export declare const getCurrentMarketPrice: (pairIndex: number, oraclePrice: number, context: MarketPriceContext) => MarketPriceResult;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Current market price calculation with skew impact
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getCurrentMarketPrice = void 0;
|
|
7
|
+
const skew_1 = require("../../trade/priceImpact/skew");
|
|
8
|
+
/**
|
|
9
|
+
* @dev Calculates the current market price adjusted for skew impact
|
|
10
|
+
* @param pairIndex Trading pair index
|
|
11
|
+
* @param oraclePrice Oracle price for the pair
|
|
12
|
+
* @param context Market price context with depths and OI data
|
|
13
|
+
* @returns Current market price with skew impact applied
|
|
14
|
+
*/
|
|
15
|
+
const getCurrentMarketPrice = (pairIndex, oraclePrice, context) => {
|
|
16
|
+
let skewImpactP = 0;
|
|
17
|
+
if (context.skewDepth > 0) {
|
|
18
|
+
const skewResult = (0, skew_1.getTradeSkewPriceImpact)({
|
|
19
|
+
collateralIndex: 0,
|
|
20
|
+
pairIndex,
|
|
21
|
+
long: true,
|
|
22
|
+
open: true,
|
|
23
|
+
positionSizeToken: 0, // Size 0 for current market price
|
|
24
|
+
}, context);
|
|
25
|
+
skewImpactP = skewResult.priceImpactP;
|
|
26
|
+
}
|
|
27
|
+
const marketPrice = oraclePrice * (1 + skewImpactP / 100);
|
|
28
|
+
return {
|
|
29
|
+
marketPrice,
|
|
30
|
+
skewImpactP,
|
|
31
|
+
oraclePrice,
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
exports.getCurrentMarketPrice = getCurrentMarketPrice;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Market price types for calculating price with skew impact
|
|
3
|
+
*/
|
|
4
|
+
export type MarketPriceInput = {
|
|
5
|
+
collateralIndex: number;
|
|
6
|
+
pairIndex: number;
|
|
7
|
+
oraclePrice: number;
|
|
8
|
+
long: boolean;
|
|
9
|
+
open: boolean;
|
|
10
|
+
positionSizeCollateral: number;
|
|
11
|
+
};
|
|
12
|
+
export type MarketPriceResult = {
|
|
13
|
+
marketPrice: number;
|
|
14
|
+
skewImpactP: number;
|
|
15
|
+
oraclePrice: number;
|
|
16
|
+
};
|
|
17
|
+
export type MarketPriceContext = {
|
|
18
|
+
skewDepth: number;
|
|
19
|
+
pairOiToken: {
|
|
20
|
+
oiLongToken: number;
|
|
21
|
+
oiShortToken: number;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -78,26 +78,28 @@ export declare const getTradeFundingFeesCollateral: (trade: {
|
|
|
78
78
|
}, currentPairPrice: number, context: GetPairFundingFeeContext) => number;
|
|
79
79
|
/**
|
|
80
80
|
* @dev Main function to calculate funding fees for a trade within context
|
|
81
|
+
* @param input Trade funding fee input parameters
|
|
81
82
|
* @param context Funding fee context with params and data
|
|
82
|
-
* @param collateralIndex Collateral index
|
|
83
|
-
* @param pairIndex Pair index
|
|
84
|
-
* @param trade Trade details
|
|
85
|
-
* @param tradeInfo Trade info (contracts version)
|
|
86
|
-
* @param initialAccFundingFeeP Initial accumulated funding fee
|
|
87
|
-
* @param currentPairPrice Current pair price
|
|
88
|
-
* @param pairOiToken Pair OI after v10
|
|
89
|
-
* @param netExposureToken Net exposure in tokens
|
|
90
|
-
* @param netExposureUsd Net exposure in USD
|
|
91
83
|
* @returns Complete funding fee calculation result
|
|
92
84
|
*/
|
|
93
|
-
export declare const getTradeFundingFees: (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
85
|
+
export declare const getTradeFundingFees: (input: {
|
|
86
|
+
collateralIndex: number;
|
|
87
|
+
pairIndex: number;
|
|
88
|
+
trade: {
|
|
89
|
+
collateralAmount: number;
|
|
90
|
+
leverage: number;
|
|
91
|
+
openPrice: number;
|
|
92
|
+
long: boolean;
|
|
93
|
+
};
|
|
94
|
+
tradeInfo: {
|
|
95
|
+
contractsVersion: number;
|
|
96
|
+
};
|
|
97
|
+
initialAccFundingFeeP: number;
|
|
98
|
+
currentPairPrice: number;
|
|
99
|
+
pairOiToken: PairOiAfterV10;
|
|
100
|
+
netExposureToken: number;
|
|
101
|
+
netExposureUsd: number;
|
|
102
|
+
}, context: GetFundingFeeContext) => TradeFundingFeeResult;
|
|
101
103
|
/**
|
|
102
104
|
* @dev Simple version of getTradeFundingFeesCollateral for backward compatibility
|
|
103
105
|
* @param trade Trade parameters
|
|
@@ -253,42 +253,34 @@ const getTradeFundingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentP
|
|
|
253
253
|
exports.getTradeFundingFeesCollateral = getTradeFundingFeesCollateral;
|
|
254
254
|
/**
|
|
255
255
|
* @dev Main function to calculate funding fees for a trade within context
|
|
256
|
+
* @param input Trade funding fee input parameters
|
|
256
257
|
* @param context Funding fee context with params and data
|
|
257
|
-
* @param collateralIndex Collateral index
|
|
258
|
-
* @param pairIndex Pair index
|
|
259
|
-
* @param trade Trade details
|
|
260
|
-
* @param tradeInfo Trade info (contracts version)
|
|
261
|
-
* @param initialAccFundingFeeP Initial accumulated funding fee
|
|
262
|
-
* @param currentPairPrice Current pair price
|
|
263
|
-
* @param pairOiToken Pair OI after v10
|
|
264
|
-
* @param netExposureToken Net exposure in tokens
|
|
265
|
-
* @param netExposureUsd Net exposure in USD
|
|
266
258
|
* @returns Complete funding fee calculation result
|
|
267
259
|
*/
|
|
268
|
-
const getTradeFundingFees = (
|
|
260
|
+
const getTradeFundingFees = (input, context) => {
|
|
269
261
|
var _a, _b;
|
|
270
262
|
// Get params and data from context
|
|
271
|
-
const params = (_a = context.fundingParams[collateralIndex]) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
272
|
-
const data = (_b = context.fundingData[collateralIndex]) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
263
|
+
const params = (_a = context.fundingParams[input.collateralIndex]) === null || _a === void 0 ? void 0 : _a[input.pairIndex];
|
|
264
|
+
const data = (_b = context.fundingData[input.collateralIndex]) === null || _b === void 0 ? void 0 : _b[input.pairIndex];
|
|
273
265
|
if (!params || !data) {
|
|
274
|
-
throw new Error(`Missing funding fee data for collateral ${collateralIndex} pair ${pairIndex}`);
|
|
266
|
+
throw new Error(`Missing funding fee data for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
275
267
|
}
|
|
276
268
|
// Calculate pending accumulated fees
|
|
277
|
-
const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(params, data, currentPairPrice, pairOiToken, netExposureToken, netExposureUsd, context.currentTimestamp);
|
|
278
|
-
const currentAccFundingFeeP = trade.long
|
|
269
|
+
const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(params, data, input.currentPairPrice, input.pairOiToken, input.netExposureToken, input.netExposureUsd, context.currentTimestamp);
|
|
270
|
+
const currentAccFundingFeeP = input.trade.long
|
|
279
271
|
? accFundingFeeLongP
|
|
280
272
|
: accFundingFeeShortP;
|
|
281
273
|
// Calculate funding fee in collateral
|
|
282
|
-
const fundingFeeCollateral = (0, exports.getTradeFundingFeesCollateralSimple)(trade, tradeInfo, initialAccFundingFeeP, currentAccFundingFeeP);
|
|
274
|
+
const fundingFeeCollateral = (0, exports.getTradeFundingFeesCollateralSimple)(input.trade, input.tradeInfo, input.initialAccFundingFeeP, currentAccFundingFeeP);
|
|
283
275
|
// Calculate funding fee as percentage
|
|
284
|
-
const fundingFeeP = trade.collateralAmount > 0
|
|
285
|
-
? (fundingFeeCollateral / trade.collateralAmount) * 100
|
|
276
|
+
const fundingFeeP = input.trade.collateralAmount > 0
|
|
277
|
+
? (fundingFeeCollateral / input.trade.collateralAmount) * 100
|
|
286
278
|
: 0;
|
|
287
279
|
return {
|
|
288
280
|
fundingFeeCollateral,
|
|
289
281
|
fundingFeeP,
|
|
290
282
|
currentAccFundingFeeP,
|
|
291
|
-
initialAccFundingFeeP,
|
|
283
|
+
initialAccFundingFeeP: input.initialAccFundingFeeP,
|
|
292
284
|
};
|
|
293
285
|
};
|
|
294
286
|
exports.getTradeFundingFees = getTradeFundingFees;
|
|
@@ -8,5 +8,6 @@ export { getTradeCumulVolPriceImpactP, getCumulVolPriceImpact, // Convenience fu
|
|
|
8
8
|
getSpreadWithCumulVolPriceImpactP, getSpreadWithPriceImpactP, // Legacy alias
|
|
9
9
|
getProtectionCloseFactor, isProtectionCloseFactorActive, getCumulativeFactor, getLegacyFactor, getFixedSpreadP, getSpreadP, convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, CumulVolContext, } from "./cumulVol";
|
|
10
10
|
export { getNetSkewToken, getNetSkewCollateral, getTradeSkewDirection, calculateSkewPriceImpactP, getTradeSkewPriceImpact, getTradeSkewPriceImpactWithChecks, calculatePartialSizeToken, SkewPriceImpact, } from "./skew";
|
|
11
|
-
export { convertPairOiToken, convertPairOiTokenArray, convertPairOiCollateral, convertPairOiCollateralArray,
|
|
12
|
-
export
|
|
11
|
+
export { convertPairOiToken, convertPairOiTokenArray, convertPairOiCollateral, convertPairOiCollateralArray, convertSkewDepth, convertPairSkewDepths, } from "./skew/converter";
|
|
12
|
+
export { buildSkewPriceImpactContext } from "./skew/builder";
|
|
13
|
+
export type { PairOiToken, PairOiCollateral, SkewPriceImpactInput, SkewPriceImpactResult, SkewPriceImpactContext, TradeSkewParams, PositionSizeResult, } from "./skew/types";
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @dev Exports cumulative volume, skew, and combined opening/closing price impact functionality
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.
|
|
7
|
+
exports.buildSkewPriceImpactContext = exports.convertPairSkewDepths = exports.convertSkewDepth = exports.convertPairOiCollateralArray = exports.convertPairOiCollateral = exports.convertPairOiTokenArray = exports.convertPairOiToken = exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpactWithChecks = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = exports.getSpreadP = exports.getFixedSpreadP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = exports.getSpreadWithPriceImpactP = exports.getSpreadWithCumulVolPriceImpactP = exports.getCumulVolPriceImpact = exports.getTradeCumulVolPriceImpactP = exports.getTradeClosingPriceImpactAtOracle = exports.getTradeClosingPriceImpact = exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = void 0;
|
|
8
8
|
// Export trade opening price impact functionality
|
|
9
9
|
var open_1 = require("./open");
|
|
10
10
|
// Core functions
|
|
@@ -51,9 +51,8 @@ Object.defineProperty(exports, "convertPairOiToken", { enumerable: true, get: fu
|
|
|
51
51
|
Object.defineProperty(exports, "convertPairOiTokenArray", { enumerable: true, get: function () { return converter_1.convertPairOiTokenArray; } });
|
|
52
52
|
Object.defineProperty(exports, "convertPairOiCollateral", { enumerable: true, get: function () { return converter_1.convertPairOiCollateral; } });
|
|
53
53
|
Object.defineProperty(exports, "convertPairOiCollateralArray", { enumerable: true, get: function () { return converter_1.convertPairOiCollateralArray; } });
|
|
54
|
-
Object.defineProperty(exports, "
|
|
55
|
-
Object.defineProperty(exports, "
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
Object.defineProperty(exports, "
|
|
59
|
-
Object.defineProperty(exports, "mergeSkewPriceImpactContexts", { enumerable: true, get: function () { return converter_1.mergeSkewPriceImpactContexts; } });
|
|
54
|
+
Object.defineProperty(exports, "convertSkewDepth", { enumerable: true, get: function () { return converter_1.convertSkewDepth; } });
|
|
55
|
+
Object.defineProperty(exports, "convertPairSkewDepths", { enumerable: true, get: function () { return converter_1.convertPairSkewDepths; } });
|
|
56
|
+
// Export builders
|
|
57
|
+
var builder_1 = require("./skew/builder");
|
|
58
|
+
Object.defineProperty(exports, "buildSkewPriceImpactContext", { enumerable: true, get: function () { return builder_1.buildSkewPriceImpactContext; } });
|
|
@@ -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
|
*/
|
|
@@ -91,22 +91,13 @@ const calculateSkewPriceImpactP = (existingSkewToken, tradeSizeToken, skewDepth,
|
|
|
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;
|