@gainsnetwork/sdk 0.0.0-v10.rc6 → 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 +24 -46
- package/lib/trade/fees/fundingFees/index.js +27 -64
- package/lib/trade/fees/trading/index.js +0 -6
- 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 +13 -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
|
@@ -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
|
+
};
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* @dev Based on skew-based funding rate model with velocity and APR multipliers
|
|
4
4
|
*/
|
|
5
5
|
import { FundingFeeParams, PairFundingFeeData, GetFundingFeeContext, TradeFundingFeeResult, PairPendingAccFundingFeesResult, PairOiAfterV10 } from "./types";
|
|
6
|
+
import { GetPairFundingFeeContext } from "./pairContext";
|
|
6
7
|
/**
|
|
7
8
|
* @dev Calculates current funding velocity per year based on skew
|
|
8
9
|
* @param netExposureToken Net exposure (long - short) in tokens
|
|
@@ -57,12 +58,12 @@ export declare const getLongShortAprMultiplier: (avgFundingRatePerSecondP: numbe
|
|
|
57
58
|
*/
|
|
58
59
|
export declare const getPairPendingAccFundingFees: (params: FundingFeeParams, data: PairFundingFeeData, currentPairPrice: number, pairOiToken: PairOiAfterV10, netExposureToken: number, netExposureUsd: number, currentTimestamp: number) => PairPendingAccFundingFeesResult;
|
|
59
60
|
/**
|
|
60
|
-
* @dev Calculates funding fees for a specific trade
|
|
61
|
-
* @param trade Trade parameters (collateral amount, leverage, open price, long/short
|
|
61
|
+
* @dev Calculates funding fees for a specific trade
|
|
62
|
+
* @param trade Trade parameters (collateral amount, leverage, open price, long/short)
|
|
62
63
|
* @param tradeInfo Trade info (contracts version)
|
|
63
64
|
* @param tradeFeesData Trade fees data containing initial acc funding fee
|
|
64
65
|
* @param currentPairPrice Current pair price
|
|
65
|
-
* @param context
|
|
66
|
+
* @param context Pair-specific funding fee context
|
|
66
67
|
* @returns Funding fee in collateral tokens
|
|
67
68
|
*/
|
|
68
69
|
export declare const getTradeFundingFeesCollateral: (trade: {
|
|
@@ -70,58 +71,35 @@ export declare const getTradeFundingFeesCollateral: (trade: {
|
|
|
70
71
|
leverage: number;
|
|
71
72
|
openPrice: number;
|
|
72
73
|
long: boolean;
|
|
73
|
-
collateralIndex?: number;
|
|
74
|
-
pairIndex?: number;
|
|
75
74
|
}, tradeInfo: {
|
|
76
75
|
contractsVersion: number;
|
|
77
76
|
}, tradeFeesData: {
|
|
78
77
|
initialAccFundingFeeP: number;
|
|
79
|
-
}, currentPairPrice: number, context
|
|
80
|
-
pairOiAfterV10?: {
|
|
81
|
-
[collateralIndex: number]: {
|
|
82
|
-
[pairIndex: number]: PairOiAfterV10;
|
|
83
|
-
};
|
|
84
|
-
} | undefined;
|
|
85
|
-
netExposureToken?: {
|
|
86
|
-
[collateralIndex: number]: {
|
|
87
|
-
[pairIndex: number]: number;
|
|
88
|
-
};
|
|
89
|
-
} | undefined;
|
|
90
|
-
netExposureUsd?: {
|
|
91
|
-
[collateralIndex: number]: {
|
|
92
|
-
[pairIndex: number]: number;
|
|
93
|
-
};
|
|
94
|
-
} | undefined;
|
|
95
|
-
}) | {
|
|
96
|
-
currentTimestamp: number;
|
|
97
|
-
params: FundingFeeParams;
|
|
98
|
-
data: PairFundingFeeData;
|
|
99
|
-
pairOi?: PairOiAfterV10 | undefined;
|
|
100
|
-
netExposureToken?: number | undefined;
|
|
101
|
-
netExposureUsd?: number | undefined;
|
|
102
|
-
} | undefined) => number;
|
|
78
|
+
}, currentPairPrice: number, context: GetPairFundingFeeContext) => number;
|
|
103
79
|
/**
|
|
104
80
|
* @dev Main function to calculate funding fees for a trade within context
|
|
81
|
+
* @param input Trade funding fee input parameters
|
|
105
82
|
* @param context Funding fee context with params and data
|
|
106
|
-
* @param collateralIndex Collateral index
|
|
107
|
-
* @param pairIndex Pair index
|
|
108
|
-
* @param trade Trade details
|
|
109
|
-
* @param tradeInfo Trade info (contracts version)
|
|
110
|
-
* @param initialAccFundingFeeP Initial accumulated funding fee
|
|
111
|
-
* @param currentPairPrice Current pair price
|
|
112
|
-
* @param pairOiToken Pair OI after v10
|
|
113
|
-
* @param netExposureToken Net exposure in tokens
|
|
114
|
-
* @param netExposureUsd Net exposure in USD
|
|
115
83
|
* @returns Complete funding fee calculation result
|
|
116
84
|
*/
|
|
117
|
-
export declare const getTradeFundingFees: (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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;
|
|
125
103
|
/**
|
|
126
104
|
* @dev Simple version of getTradeFundingFeesCollateral for backward compatibility
|
|
127
105
|
* @param trade Trade parameters
|
|
@@ -31,6 +31,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
31
31
|
};
|
|
32
32
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
33
|
exports.FundingFees = exports.getTradeFundingFeesCollateralSimple = exports.getTradeFundingFees = exports.getTradeFundingFeesCollateral = exports.getPairPendingAccFundingFees = exports.getLongShortAprMultiplier = exports.getAvgFundingRatePerSecondP = exports.getSecondsToReachZeroRate = exports.getCurrentFundingVelocityPerYear = void 0;
|
|
34
|
+
const types_1 = require("../../../contracts/types");
|
|
34
35
|
// Constants from contract
|
|
35
36
|
const FUNDING_APR_MULTIPLIER_CAP = 100; // Smaller side can earn up to 100x more APR
|
|
36
37
|
const ONE_YEAR = 365 * 24 * 60 * 60; // 1 year in seconds
|
|
@@ -225,99 +226,61 @@ const getPairPendingAccFundingFees = (params, data, currentPairPrice, pairOiToke
|
|
|
225
226
|
};
|
|
226
227
|
exports.getPairPendingAccFundingFees = getPairPendingAccFundingFees;
|
|
227
228
|
/**
|
|
228
|
-
* @dev Calculates funding fees for a specific trade
|
|
229
|
-
* @param trade Trade parameters (collateral amount, leverage, open price, long/short
|
|
229
|
+
* @dev Calculates funding fees for a specific trade
|
|
230
|
+
* @param trade Trade parameters (collateral amount, leverage, open price, long/short)
|
|
230
231
|
* @param tradeInfo Trade info (contracts version)
|
|
231
232
|
* @param tradeFeesData Trade fees data containing initial acc funding fee
|
|
232
233
|
* @param currentPairPrice Current pair price
|
|
233
|
-
* @param context
|
|
234
|
+
* @param context Pair-specific funding fee context
|
|
234
235
|
* @returns Funding fee in collateral tokens
|
|
235
236
|
*/
|
|
236
237
|
const getTradeFundingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
|
|
237
|
-
|
|
238
|
-
// Funding fees are only charged on post-v10 trades
|
|
239
|
-
if (tradeInfo.contractsVersion < 10) {
|
|
238
|
+
if (tradeInfo.contractsVersion < types_1.ContractsVersion.V10) {
|
|
240
239
|
return 0;
|
|
241
240
|
}
|
|
242
241
|
const positionSizeCollateral = trade.collateralAmount * trade.leverage;
|
|
243
|
-
if (!context) {
|
|
244
|
-
return 0;
|
|
245
|
-
}
|
|
246
|
-
// Check if we have a pair-specific context
|
|
247
|
-
if ("params" in context && "data" in context) {
|
|
248
|
-
// Pair-specific context
|
|
249
|
-
const { params, data, pairOi, netExposureToken, netExposureUsd } = context;
|
|
250
|
-
if (!params.fundingFeesEnabled) {
|
|
251
|
-
return 0;
|
|
252
|
-
}
|
|
253
|
-
// Calculate pending accumulated fees
|
|
254
|
-
const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(params, data, currentPairPrice, pairOi || { oiLongToken: 0, oiShortToken: 0 }, netExposureToken || 0, netExposureUsd || 0, context.currentTimestamp);
|
|
255
|
-
const currentAccFundingFeeP = trade.long
|
|
256
|
-
? accFundingFeeLongP
|
|
257
|
-
: accFundingFeeShortP;
|
|
258
|
-
const fundingFeeDelta = currentAccFundingFeeP - tradeFeesData.initialAccFundingFeeP;
|
|
259
|
-
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice;
|
|
260
|
-
}
|
|
261
|
-
// Full context - original logic
|
|
262
|
-
if ("fundingParams" in context &&
|
|
263
|
-
trade.collateralIndex !== undefined &&
|
|
264
|
-
trade.pairIndex !== undefined) {
|
|
265
|
-
const params = (_a = context.fundingParams[trade.collateralIndex]) === null || _a === void 0 ? void 0 : _a[trade.pairIndex];
|
|
266
|
-
const data = (_b = context.fundingData[trade.collateralIndex]) === null || _b === void 0 ? void 0 : _b[trade.pairIndex];
|
|
267
|
-
const pairOi = (_d = (_c = context.pairOiAfterV10) === null || _c === void 0 ? void 0 : _c[trade.collateralIndex]) === null || _d === void 0 ? void 0 : _d[trade.pairIndex];
|
|
268
|
-
const netExposureToken = ((_f = (_e = context.netExposureToken) === null || _e === void 0 ? void 0 : _e[trade.collateralIndex]) === null || _f === void 0 ? void 0 : _f[trade.pairIndex]) || 0;
|
|
269
|
-
const netExposureUsd = ((_h = (_g = context.netExposureUsd) === null || _g === void 0 ? void 0 : _g[trade.collateralIndex]) === null || _h === void 0 ? void 0 : _h[trade.pairIndex]) || 0;
|
|
270
|
-
if (params && data && pairOi) {
|
|
271
|
-
// Calculate pending accumulated fees
|
|
272
|
-
const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(params, data, currentPairPrice, pairOi, netExposureToken, netExposureUsd, context.currentTimestamp);
|
|
273
|
-
const currentAccFundingFeeP = trade.long
|
|
274
|
-
? accFundingFeeLongP
|
|
275
|
-
: accFundingFeeShortP;
|
|
276
|
-
const fundingFeeDelta = currentAccFundingFeeP - tradeFeesData.initialAccFundingFeeP;
|
|
277
|
-
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice;
|
|
278
|
-
}
|
|
242
|
+
if (!context.params.fundingFeesEnabled) {
|
|
243
|
+
return 0;
|
|
279
244
|
}
|
|
280
|
-
|
|
245
|
+
// Calculate pending accumulated fees
|
|
246
|
+
const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(context.params, context.data, currentPairPrice, context.pairOi || { oiLongToken: 0, oiShortToken: 0 }, context.netExposureToken || 0, context.netExposureUsd || 0, context.currentTimestamp);
|
|
247
|
+
const currentAccFundingFeeP = trade.long
|
|
248
|
+
? accFundingFeeLongP
|
|
249
|
+
: accFundingFeeShortP;
|
|
250
|
+
const fundingFeeDelta = currentAccFundingFeeP - tradeFeesData.initialAccFundingFeeP;
|
|
251
|
+
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice / 100;
|
|
281
252
|
};
|
|
282
253
|
exports.getTradeFundingFeesCollateral = getTradeFundingFeesCollateral;
|
|
283
254
|
/**
|
|
284
255
|
* @dev Main function to calculate funding fees for a trade within context
|
|
256
|
+
* @param input Trade funding fee input parameters
|
|
285
257
|
* @param context Funding fee context with params and data
|
|
286
|
-
* @param collateralIndex Collateral index
|
|
287
|
-
* @param pairIndex Pair index
|
|
288
|
-
* @param trade Trade details
|
|
289
|
-
* @param tradeInfo Trade info (contracts version)
|
|
290
|
-
* @param initialAccFundingFeeP Initial accumulated funding fee
|
|
291
|
-
* @param currentPairPrice Current pair price
|
|
292
|
-
* @param pairOiToken Pair OI after v10
|
|
293
|
-
* @param netExposureToken Net exposure in tokens
|
|
294
|
-
* @param netExposureUsd Net exposure in USD
|
|
295
258
|
* @returns Complete funding fee calculation result
|
|
296
259
|
*/
|
|
297
|
-
const getTradeFundingFees = (
|
|
260
|
+
const getTradeFundingFees = (input, context) => {
|
|
298
261
|
var _a, _b;
|
|
299
262
|
// Get params and data from context
|
|
300
|
-
const params = (_a = context.fundingParams[collateralIndex]) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
301
|
-
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];
|
|
302
265
|
if (!params || !data) {
|
|
303
|
-
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}`);
|
|
304
267
|
}
|
|
305
268
|
// Calculate pending accumulated fees
|
|
306
|
-
const { accFundingFeeLongP, accFundingFeeShortP } = (0, exports.getPairPendingAccFundingFees)(params, data, currentPairPrice, pairOiToken, netExposureToken, netExposureUsd, context.currentTimestamp);
|
|
307
|
-
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
|
|
308
271
|
? accFundingFeeLongP
|
|
309
272
|
: accFundingFeeShortP;
|
|
310
273
|
// Calculate funding fee in collateral
|
|
311
|
-
const fundingFeeCollateral = (0, exports.getTradeFundingFeesCollateralSimple)(trade, tradeInfo, initialAccFundingFeeP, currentAccFundingFeeP);
|
|
274
|
+
const fundingFeeCollateral = (0, exports.getTradeFundingFeesCollateralSimple)(input.trade, input.tradeInfo, input.initialAccFundingFeeP, currentAccFundingFeeP);
|
|
312
275
|
// Calculate funding fee as percentage
|
|
313
|
-
const fundingFeeP = trade.collateralAmount > 0
|
|
314
|
-
? (fundingFeeCollateral / trade.collateralAmount) * 100
|
|
276
|
+
const fundingFeeP = input.trade.collateralAmount > 0
|
|
277
|
+
? (fundingFeeCollateral / input.trade.collateralAmount) * 100
|
|
315
278
|
: 0;
|
|
316
279
|
return {
|
|
317
280
|
fundingFeeCollateral,
|
|
318
281
|
fundingFeeP,
|
|
319
282
|
currentAccFundingFeeP,
|
|
320
|
-
initialAccFundingFeeP,
|
|
283
|
+
initialAccFundingFeeP: input.initialAccFundingFeeP,
|
|
321
284
|
};
|
|
322
285
|
};
|
|
323
286
|
exports.getTradeFundingFees = getTradeFundingFees;
|
|
@@ -331,12 +294,12 @@ exports.getTradeFundingFees = getTradeFundingFees;
|
|
|
331
294
|
*/
|
|
332
295
|
const getTradeFundingFeesCollateralSimple = (trade, tradeInfo, initialAccFundingFeeP, currentAccFundingFeeP) => {
|
|
333
296
|
// Funding fees are only charged on post-v10 trades
|
|
334
|
-
if (tradeInfo.contractsVersion <
|
|
297
|
+
if (tradeInfo.contractsVersion < types_1.ContractsVersion.V10) {
|
|
335
298
|
return 0;
|
|
336
299
|
}
|
|
337
300
|
const positionSizeCollateral = trade.collateralAmount * trade.leverage;
|
|
338
301
|
const fundingFeeDelta = currentAccFundingFeeP - initialAccFundingFeeP;
|
|
339
|
-
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice;
|
|
302
|
+
return (positionSizeCollateral * fundingFeeDelta) / trade.openPrice / 100;
|
|
340
303
|
};
|
|
341
304
|
exports.getTradeFundingFeesCollateralSimple = getTradeFundingFeesCollateralSimple;
|
|
342
305
|
// Export namespace for types
|
|
@@ -126,12 +126,6 @@ const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, c
|
|
|
126
126
|
if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
|
|
127
127
|
context.borrowingV2 &&
|
|
128
128
|
tradeFeesData.initialAccBorrowingFeeP !== undefined) {
|
|
129
|
-
console.log("initialAccBorrowingFeeP", tradeFeesData.initialAccBorrowingFeeP);
|
|
130
|
-
console.log("positionSizeCollateral", positionSizeCollateral);
|
|
131
|
-
console.log("openPrice", trade.openPrice);
|
|
132
|
-
console.log("currentPairPrice", currentPairPrice);
|
|
133
|
-
console.log("currentTimestamp", context.currentTimestamp);
|
|
134
|
-
console.log("borrowingv2", context.borrowingV2);
|
|
135
129
|
borrowingFeeCollateral = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
|
|
136
130
|
positionSizeCollateral,
|
|
137
131
|
openPrice: trade.openPrice,
|
|
@@ -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";
|
|
@@ -32,6 +32,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
32
32
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
33
|
exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpactWithChecks = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = void 0;
|
|
34
34
|
const utils_1 = require("../../utils");
|
|
35
|
+
const types_1 = require("../../../contracts/types");
|
|
35
36
|
// Constants
|
|
36
37
|
const PRICE_IMPACT_DIVIDER = 2; // Half price impact to match cumulative volume impact scale
|
|
37
38
|
/**
|
|
@@ -44,10 +45,10 @@ const getNetSkewToken = (pairOi) => {
|
|
|
44
45
|
};
|
|
45
46
|
exports.getNetSkewToken = getNetSkewToken;
|
|
46
47
|
/**
|
|
47
|
-
* @dev Calculates net skew in collateral
|
|
48
|
+
* @dev Calculates net skew in collateral
|
|
48
49
|
* @param netSkewToken Net skew in tokens
|
|
49
50
|
* @param currentPrice Current pair price
|
|
50
|
-
* @returns Net skew in collateral
|
|
51
|
+
* @returns Net skew in collateral
|
|
51
52
|
*/
|
|
52
53
|
const getNetSkewCollateral = (netSkewToken, currentPrice) => {
|
|
53
54
|
return netSkewToken * currentPrice;
|
|
@@ -69,7 +70,7 @@ exports.getTradeSkewDirection = getTradeSkewDirection;
|
|
|
69
70
|
* @dev Core skew price impact calculation
|
|
70
71
|
* @param existingSkewToken Current net skew in tokens (signed)
|
|
71
72
|
* @param tradeSizeToken Trade size in tokens (always positive)
|
|
72
|
-
* @param skewDepth Skew depth in
|
|
73
|
+
* @param skewDepth Skew depth in tokens
|
|
73
74
|
* @param tradeIncreasesSkew Whether trade increases skew in its direction
|
|
74
75
|
* @returns Price impact percentage (can be positive or negative)
|
|
75
76
|
*/
|
|
@@ -90,22 +91,13 @@ const calculateSkewPriceImpactP = (existingSkewToken, tradeSizeToken, skewDepth,
|
|
|
90
91
|
exports.calculateSkewPriceImpactP = calculateSkewPriceImpactP;
|
|
91
92
|
/**
|
|
92
93
|
* @dev Main function to calculate skew price impact for a trade
|
|
93
|
-
* @param context Skew price impact context with depths and OI data
|
|
94
94
|
* @param input Trade parameters
|
|
95
|
+
* @param context Skew price impact context with depths and OI data
|
|
95
96
|
* @returns Skew price impact result
|
|
96
97
|
*/
|
|
97
|
-
const getTradeSkewPriceImpact = (
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
}
|
|
98
|
+
const getTradeSkewPriceImpact = (input, context) => {
|
|
99
|
+
// Get skew depth and pair OI from simplified context
|
|
100
|
+
const { skewDepth, pairOiToken: pairOi } = context;
|
|
109
101
|
// Calculate net skew
|
|
110
102
|
const netSkewToken = (0, exports.getNetSkewToken)(pairOi);
|
|
111
103
|
// Determine trade direction impact
|
|
@@ -139,7 +131,7 @@ exports.getTradeSkewPriceImpact = getTradeSkewPriceImpact;
|
|
|
139
131
|
*/
|
|
140
132
|
const getTradeSkewPriceImpactWithChecks = (params, context) => {
|
|
141
133
|
// v10+ trades only
|
|
142
|
-
if (params.contractsVersion <
|
|
134
|
+
if (params.contractsVersion < types_1.ContractsVersion.V10) {
|
|
143
135
|
return 0;
|
|
144
136
|
}
|
|
145
137
|
// Counter trades don't pay skew impact
|
|
@@ -149,13 +141,13 @@ const getTradeSkewPriceImpactWithChecks = (params, context) => {
|
|
|
149
141
|
// Calculate position size in tokens
|
|
150
142
|
const positionSizeToken = (0, utils_1.calculatePositionSizeToken)(params.positionSizeCollateral, params.currentPrice);
|
|
151
143
|
// Get skew price impact
|
|
152
|
-
const result = (0, exports.getTradeSkewPriceImpact)(
|
|
144
|
+
const result = (0, exports.getTradeSkewPriceImpact)({
|
|
153
145
|
collateralIndex: params.collateralIndex,
|
|
154
146
|
pairIndex: params.pairIndex,
|
|
155
147
|
long: params.long,
|
|
156
148
|
open: params.open,
|
|
157
149
|
positionSizeToken,
|
|
158
|
-
});
|
|
150
|
+
}, context);
|
|
159
151
|
return result.priceImpactP;
|
|
160
152
|
};
|
|
161
153
|
exports.getTradeSkewPriceImpactWithChecks = getTradeSkewPriceImpactWithChecks;
|
|
@@ -176,4 +168,6 @@ const calculatePartialSizeToken = (originalSizeCollateral, deltaCollateral, orig
|
|
|
176
168
|
exports.calculatePartialSizeToken = calculatePartialSizeToken;
|
|
177
169
|
// Export namespace for types
|
|
178
170
|
exports.SkewPriceImpact = __importStar(require("./types"));
|
|
171
|
+
__exportStar(require("./converter"), exports);
|
|
172
|
+
__exportStar(require("./builder"), exports);
|
|
179
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;
|