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