@gainsnetwork/sdk 0.0.0-v10.rc7 → 0.0.0-v10.rc9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/backend/tradingVariables/backend.types.d.ts +1 -0
- package/lib/backend/tradingVariables/converter.d.ts +3 -0
- package/lib/backend/tradingVariables/converter.js +10 -2
- package/lib/backend/tradingVariables/types.d.ts +3 -0
- package/lib/markets/index.d.ts +1 -0
- package/lib/markets/index.js +1 -0
- package/lib/markets/price/builder.d.ts +25 -0
- package/lib/markets/price/builder.js +69 -0
- package/lib/markets/price/index.d.ts +6 -0
- package/lib/markets/price/index.js +22 -0
- package/lib/markets/price/marketPrice.d.ts +12 -0
- package/lib/markets/price/marketPrice.js +34 -0
- package/lib/markets/price/types.d.ts +23 -0
- package/lib/markets/price/types.js +5 -0
- package/lib/trade/fees/fundingFees/index.d.ts +19 -17
- package/lib/trade/fees/fundingFees/index.js +11 -19
- package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
- package/lib/trade/priceImpact/cumulVol/builder.js +43 -0
- package/lib/trade/priceImpact/cumulVol/index.d.ts +1 -0
- package/lib/trade/priceImpact/cumulVol/index.js +7 -4
- package/lib/trade/priceImpact/index.d.ts +5 -4
- package/lib/trade/priceImpact/index.js +10 -7
- package/lib/trade/priceImpact/open/builder.d.ts +21 -0
- package/lib/trade/priceImpact/open/builder.js +43 -0
- package/lib/trade/priceImpact/open/index.d.ts +1 -0
- package/lib/trade/priceImpact/open/index.js +5 -2
- package/lib/trade/priceImpact/open/types.d.ts +3 -2
- package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
- package/lib/trade/priceImpact/skew/builder.js +28 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +11 -42
- package/lib/trade/priceImpact/skew/converter.js +19 -109
- package/lib/trade/priceImpact/skew/fetcher.d.ts +8 -11
- package/lib/trade/priceImpact/skew/fetcher.js +23 -22
- package/lib/trade/priceImpact/skew/index.d.ts +7 -5
- package/lib/trade/priceImpact/skew/index.js +12 -19
- package/lib/trade/priceImpact/skew/types.d.ts +2 -15
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +2 -2
- package/package.json +1 -1
|
@@ -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;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
|
|
2
|
+
import { CumulVolContext } from "./index";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Builds cumulative volume price impact sub-context for a specific pair
|
|
5
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
6
|
+
* @param collateralIndex The collateral index (1-based)
|
|
7
|
+
* @param pairIndex The pair index
|
|
8
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
9
|
+
* @returns Cumulative volume context ready for getTradeCumulVolPriceImpactP
|
|
10
|
+
*/
|
|
11
|
+
export declare const buildCumulVolContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, additionalParams: {
|
|
12
|
+
currentBlock: number;
|
|
13
|
+
contractsVersion?: number;
|
|
14
|
+
isPnlPositive?: boolean;
|
|
15
|
+
isOpen?: boolean;
|
|
16
|
+
createdBlock?: number;
|
|
17
|
+
userPriceImpact?: {
|
|
18
|
+
cumulVolPriceImpactMultiplier: number;
|
|
19
|
+
fixedSpreadP: number;
|
|
20
|
+
};
|
|
21
|
+
protectionCloseFactorWhitelist?: boolean;
|
|
22
|
+
}) => CumulVolContext | undefined;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildCumulVolContext = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @dev Builds cumulative volume price impact sub-context for a specific pair
|
|
6
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
7
|
+
* @param collateralIndex The collateral index (1-based)
|
|
8
|
+
* @param pairIndex The pair index
|
|
9
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
10
|
+
* @returns Cumulative volume context ready for getTradeCumulVolPriceImpactP
|
|
11
|
+
*/
|
|
12
|
+
const buildCumulVolContext = (globalTradingVariables, collateralIndex, pairIndex, additionalParams) => {
|
|
13
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
14
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
15
|
+
if (!collateral) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
// Get pair-specific data from global variables
|
|
19
|
+
const pairDepth = (_a = globalTradingVariables.pairDepths) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
20
|
+
const pairFactor = (_b = globalTradingVariables.pairFactors) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
21
|
+
const oiWindows = (_c = globalTradingVariables.oiWindows) === null || _c === void 0 ? void 0 : _c[pairIndex];
|
|
22
|
+
// Get OI windows settings (same for all pairs)
|
|
23
|
+
// OI windows settings from global trading variables are already in SDK format
|
|
24
|
+
const oiWindowsSettings = globalTradingVariables.oiWindowsSettings;
|
|
25
|
+
// Get user-specific parameters from additionalParams
|
|
26
|
+
const userPriceImpact = additionalParams.userPriceImpact;
|
|
27
|
+
const protectionCloseFactorWhitelist = additionalParams.protectionCloseFactorWhitelist;
|
|
28
|
+
// Get liquidation params - check both pair and group level
|
|
29
|
+
const liquidationParams = ((_e = (_d = globalTradingVariables.liquidationParams) === null || _d === void 0 ? void 0 : _d.pairs) === null || _e === void 0 ? void 0 : _e[pairIndex]) ||
|
|
30
|
+
((_g = (_f = globalTradingVariables.liquidationParams) === null || _f === void 0 ? void 0 : _f.groups) === null || _g === void 0 ? void 0 : _g[0]); // fallback to first group
|
|
31
|
+
return Object.assign({
|
|
32
|
+
// Trade state
|
|
33
|
+
isOpen: additionalParams.isOpen, isPnlPositive: additionalParams.isPnlPositive, createdBlock: additionalParams.createdBlock,
|
|
34
|
+
// Protection factors
|
|
35
|
+
liquidationParams, currentBlock: additionalParams.currentBlock, contractsVersion: additionalParams.contractsVersion, protectionCloseFactorWhitelist,
|
|
36
|
+
// Price impact data
|
|
37
|
+
pairDepth,
|
|
38
|
+
oiWindowsSettings,
|
|
39
|
+
oiWindows,
|
|
40
|
+
// User/collateral specific
|
|
41
|
+
userPriceImpact, collateralPriceUsd: ((_h = collateral.prices) === null || _h === void 0 ? void 0 : _h.collateralPriceUsd) || 1 }, pairFactor);
|
|
42
|
+
};
|
|
43
|
+
exports.buildCumulVolContext = buildCumulVolContext;
|
|
@@ -105,3 +105,4 @@ export declare const getCumulVolPriceImpact: (buy: boolean, collateral: number,
|
|
|
105
105
|
}) => number;
|
|
106
106
|
export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
|
|
107
107
|
export { convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, } from "./converter";
|
|
108
|
+
export { buildCumulVolContext } from "./builder";
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @dev Mirrors contract's getTradeCumulVolPriceImpactP functionality
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = exports.getSpreadWithPriceImpactP = exports.getCumulVolPriceImpact = exports.getSpreadWithCumulVolPriceImpactP = exports.getSpreadP = exports.getFixedSpreadP = exports.getTradeCumulVolPriceImpactP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = void 0;
|
|
7
|
+
exports.buildCumulVolContext = exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = exports.getSpreadWithPriceImpactP = exports.getCumulVolPriceImpact = exports.getSpreadWithCumulVolPriceImpactP = exports.getSpreadP = exports.getFixedSpreadP = exports.getTradeCumulVolPriceImpactP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = void 0;
|
|
8
8
|
const oiWindows_1 = require("../../oiWindows");
|
|
9
9
|
const constants_1 = require("../../../constants");
|
|
10
10
|
const types_1 = require("../../../contracts/types");
|
|
@@ -119,12 +119,12 @@ const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterest
|
|
|
119
119
|
if (!onePercentDepth || activeOi === undefined) {
|
|
120
120
|
return 0;
|
|
121
121
|
}
|
|
122
|
-
|
|
122
|
+
const finalPriceImpactP = ((activeOi * (0, exports.getCumulativeFactor)(updatedContext) +
|
|
123
123
|
tradeOpenInterestUsd / 2) /
|
|
124
124
|
onePercentDepth /
|
|
125
|
-
100 /
|
|
126
125
|
(0, exports.getLegacyFactor)(updatedContext)) *
|
|
127
|
-
(0, exports.getProtectionCloseFactor)(updatedContext)
|
|
126
|
+
(0, exports.getProtectionCloseFactor)(updatedContext);
|
|
127
|
+
return finalPriceImpactP;
|
|
128
128
|
};
|
|
129
129
|
exports.getTradeCumulVolPriceImpactP = getTradeCumulVolPriceImpactP;
|
|
130
130
|
/**
|
|
@@ -226,3 +226,6 @@ Object.defineProperty(exports, "convertOiWindowsSettings", { enumerable: true, g
|
|
|
226
226
|
Object.defineProperty(exports, "convertOiWindow", { enumerable: true, get: function () { return converter_1.convertOiWindow; } });
|
|
227
227
|
Object.defineProperty(exports, "convertOiWindows", { enumerable: true, get: function () { return converter_1.convertOiWindows; } });
|
|
228
228
|
Object.defineProperty(exports, "convertOiWindowsSettingsArray", { enumerable: true, get: function () { return converter_1.convertOiWindowsSettingsArray; } });
|
|
229
|
+
// Export builder
|
|
230
|
+
var builder_1 = require("./builder");
|
|
231
|
+
Object.defineProperty(exports, "buildCumulVolContext", { enumerable: true, get: function () { return builder_1.buildCumulVolContext; } });
|
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
* @dev Main price impact module
|
|
3
3
|
* @dev Exports cumulative volume, skew, and combined opening/closing price impact functionality
|
|
4
4
|
*/
|
|
5
|
-
export { getTradeOpeningPriceImpact, getTradeOpeningPriceImpactAtMarket, TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult, } from "./open";
|
|
5
|
+
export { getTradeOpeningPriceImpact, getTradeOpeningPriceImpactAtMarket, buildTradeOpeningPriceImpactContext, TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult, } from "./open";
|
|
6
6
|
export { getTradeClosingPriceImpact, getTradeClosingPriceImpactAtOracle, TradeClosingPriceImpactInput, TradeClosingPriceImpactContext, TradeClosingPriceImpactResult, } from "./close";
|
|
7
7
|
export { getTradeCumulVolPriceImpactP, getCumulVolPriceImpact, // Convenience function
|
|
8
8
|
getSpreadWithCumulVolPriceImpactP, getSpreadWithPriceImpactP, // Legacy alias
|
|
9
|
-
getProtectionCloseFactor, isProtectionCloseFactorActive, getCumulativeFactor, getLegacyFactor, getFixedSpreadP, getSpreadP, convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, CumulVolContext, } from "./cumulVol";
|
|
9
|
+
getProtectionCloseFactor, isProtectionCloseFactorActive, getCumulativeFactor, getLegacyFactor, getFixedSpreadP, getSpreadP, convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, buildCumulVolContext, 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,12 +4,14 @@
|
|
|
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.buildCumulVolContext = 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.buildTradeOpeningPriceImpactContext = 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
|
|
11
11
|
Object.defineProperty(exports, "getTradeOpeningPriceImpact", { enumerable: true, get: function () { return open_1.getTradeOpeningPriceImpact; } });
|
|
12
12
|
Object.defineProperty(exports, "getTradeOpeningPriceImpactAtMarket", { enumerable: true, get: function () { return open_1.getTradeOpeningPriceImpactAtMarket; } });
|
|
13
|
+
// Builder
|
|
14
|
+
Object.defineProperty(exports, "buildTradeOpeningPriceImpactContext", { enumerable: true, get: function () { return open_1.buildTradeOpeningPriceImpactContext; } });
|
|
13
15
|
// Export trade closing price impact functionality
|
|
14
16
|
var close_1 = require("./close");
|
|
15
17
|
// Core functions
|
|
@@ -33,6 +35,8 @@ Object.defineProperty(exports, "convertOiWindowsSettings", { enumerable: true, g
|
|
|
33
35
|
Object.defineProperty(exports, "convertOiWindow", { enumerable: true, get: function () { return cumulVol_1.convertOiWindow; } });
|
|
34
36
|
Object.defineProperty(exports, "convertOiWindows", { enumerable: true, get: function () { return cumulVol_1.convertOiWindows; } });
|
|
35
37
|
Object.defineProperty(exports, "convertOiWindowsSettingsArray", { enumerable: true, get: function () { return cumulVol_1.convertOiWindowsSettingsArray; } });
|
|
38
|
+
// Builder
|
|
39
|
+
Object.defineProperty(exports, "buildCumulVolContext", { enumerable: true, get: function () { return cumulVol_1.buildCumulVolContext; } });
|
|
36
40
|
// Export skew price impact functionality
|
|
37
41
|
var skew_1 = require("./skew");
|
|
38
42
|
// Core functions
|
|
@@ -51,9 +55,8 @@ Object.defineProperty(exports, "convertPairOiToken", { enumerable: true, get: fu
|
|
|
51
55
|
Object.defineProperty(exports, "convertPairOiTokenArray", { enumerable: true, get: function () { return converter_1.convertPairOiTokenArray; } });
|
|
52
56
|
Object.defineProperty(exports, "convertPairOiCollateral", { enumerable: true, get: function () { return converter_1.convertPairOiCollateral; } });
|
|
53
57
|
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; } });
|
|
58
|
+
Object.defineProperty(exports, "convertSkewDepth", { enumerable: true, get: function () { return converter_1.convertSkewDepth; } });
|
|
59
|
+
Object.defineProperty(exports, "convertPairSkewDepths", { enumerable: true, get: function () { return converter_1.convertPairSkewDepths; } });
|
|
60
|
+
// Export builders
|
|
61
|
+
var builder_1 = require("./skew/builder");
|
|
62
|
+
Object.defineProperty(exports, "buildSkewPriceImpactContext", { enumerable: true, get: function () { return builder_1.buildSkewPriceImpactContext; } });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
|
|
2
|
+
import { TradeOpeningPriceImpactContext } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Builds a complete context for trade opening price impact calculations
|
|
5
|
+
* @dev Uses sub-context builders to create properly scoped contexts
|
|
6
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
7
|
+
* @param collateralIndex The collateral index (1-based)
|
|
8
|
+
* @param pairIndex The pair index
|
|
9
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
10
|
+
* @returns Complete context ready for getTradeOpeningPriceImpact
|
|
11
|
+
*/
|
|
12
|
+
export declare const buildTradeOpeningPriceImpactContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, additionalParams: {
|
|
13
|
+
currentBlock: number;
|
|
14
|
+
contractsVersion?: number;
|
|
15
|
+
trader?: string;
|
|
16
|
+
userPriceImpact?: {
|
|
17
|
+
cumulVolPriceImpactMultiplier: number;
|
|
18
|
+
fixedSpreadP: number;
|
|
19
|
+
};
|
|
20
|
+
protectionCloseFactorWhitelist?: boolean;
|
|
21
|
+
}) => TradeOpeningPriceImpactContext | undefined;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildTradeOpeningPriceImpactContext = void 0;
|
|
4
|
+
const builder_1 = require("../cumulVol/builder");
|
|
5
|
+
const builder_2 = require("../skew/builder");
|
|
6
|
+
/**
|
|
7
|
+
* @dev Builds a complete context for trade opening price impact calculations
|
|
8
|
+
* @dev Uses sub-context builders to create properly scoped contexts
|
|
9
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
10
|
+
* @param collateralIndex The collateral index (1-based)
|
|
11
|
+
* @param pairIndex The pair index
|
|
12
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
13
|
+
* @returns Complete context ready for getTradeOpeningPriceImpact
|
|
14
|
+
*/
|
|
15
|
+
const buildTradeOpeningPriceImpactContext = (globalTradingVariables, collateralIndex, pairIndex, additionalParams) => {
|
|
16
|
+
var _a;
|
|
17
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
18
|
+
if (!collateral) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
// Build cumulative volume subcontext
|
|
22
|
+
const cumulVolContext = (0, builder_1.buildCumulVolContext)(globalTradingVariables, collateralIndex, pairIndex, {
|
|
23
|
+
currentBlock: additionalParams.currentBlock,
|
|
24
|
+
contractsVersion: additionalParams.contractsVersion,
|
|
25
|
+
isPnlPositive: false,
|
|
26
|
+
isOpen: true,
|
|
27
|
+
createdBlock: undefined,
|
|
28
|
+
userPriceImpact: additionalParams.userPriceImpact,
|
|
29
|
+
protectionCloseFactorWhitelist: additionalParams.protectionCloseFactorWhitelist,
|
|
30
|
+
});
|
|
31
|
+
// Build skew price impact subcontext
|
|
32
|
+
const skewContext = (0, builder_2.buildSkewPriceImpactContext)(collateral, pairIndex);
|
|
33
|
+
if (!cumulVolContext || !skewContext) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
// Return structured context with proper subcontexts
|
|
37
|
+
return {
|
|
38
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
39
|
+
cumulVolContext,
|
|
40
|
+
skewContext,
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
exports.buildTradeOpeningPriceImpactContext = buildTradeOpeningPriceImpactContext;
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult } from "./types";
|
|
6
6
|
export type { TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult, };
|
|
7
|
+
export { buildTradeOpeningPriceImpactContext } from "./builder";
|
|
7
8
|
/**
|
|
8
9
|
* @dev Calculates all price impacts for trade opening
|
|
9
10
|
* @dev Mirrors contract's getTradeOpeningPriceImpact function
|