@gainsnetwork/sdk 0.0.0-v10.rc2 → 0.0.0-v10.rc21
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 +20 -0
- package/lib/constants.js +24 -3
- package/lib/contracts/addresses.json +20 -0
- package/lib/contracts/index.js +3 -1
- 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/GNSMultiCollatDiamond.d.ts +7 -20
- 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/GNSMultiCollatDiamond__factory.js +7 -16
- 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/generated/factories/GToken__factory.d.ts +7 -0
- package/lib/contracts/types/generated/factories/GToken__factory.js +4 -0
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/index.d.ts +0 -3
- package/lib/contracts/utils/index.js +0 -3
- package/lib/contracts/utils/openTrades.js +14 -30
- package/lib/contracts/utils/pairs.js +20 -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/holdingFees/index.d.ts +46 -0
- package/lib/markets/holdingFees/index.js +105 -0
- package/lib/markets/holdingFees/types.d.ts +23 -0
- package/lib/markets/holdingFees/types.js +5 -0
- package/lib/markets/index.d.ts +5 -0
- package/lib/markets/index.js +5 -0
- package/lib/markets/leverage/builder.d.ts +12 -0
- package/lib/markets/leverage/builder.js +25 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.d.ts +7 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.js +38 -0
- package/lib/markets/leverage/index.d.ts +3 -0
- package/lib/markets/leverage/index.js +19 -0
- package/lib/markets/leverage/types.d.ts +15 -0
- package/lib/markets/leverage/types.js +2 -0
- package/lib/markets/oi/converter.d.ts +62 -0
- package/lib/markets/oi/converter.js +102 -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 +49 -0
- package/lib/markets/oi/index.js +77 -0
- package/lib/markets/oi/types.d.ts +73 -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/counterTrade/index.d.ts +2 -0
- package/lib/trade/counterTrade/index.js +18 -0
- package/lib/trade/counterTrade/types.d.ts +7 -0
- package/lib/trade/counterTrade/types.js +2 -0
- package/lib/trade/counterTrade/validateCounterTrade.d.ts +10 -0
- package/lib/trade/counterTrade/validateCounterTrade.js +29 -0
- package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
- package/lib/trade/effectiveLeverage/builder.js +30 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.d.ts +23 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.js +64 -0
- package/lib/trade/effectiveLeverage/index.d.ts +3 -0
- package/lib/trade/effectiveLeverage/index.js +22 -0
- package/lib/trade/effectiveLeverage/types.d.ts +33 -0
- package/lib/trade/effectiveLeverage/types.js +2 -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 +15 -4
- package/lib/trade/fees/borrowing/index.js +42 -18
- 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 +12 -3
- package/lib/trade/fees/borrowingV2/converter.js +29 -18
- package/lib/{contracts/fetch/fees/borrowingFeesV2.d.ts → trade/fees/borrowingV2/fetcher.d.ts} +2 -3
- package/lib/{contracts/fetch/fees/borrowingFeesV2.js → trade/fees/borrowingV2/fetcher.js} +6 -14
- package/lib/trade/fees/borrowingV2/index.d.ts +6 -17
- package/lib/trade/fees/borrowingV2/index.js +10 -37
- package/lib/trade/fees/borrowingV2/types.d.ts +22 -6
- 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/{contracts/fetch/fees/fundingFees.d.ts → trade/fees/fundingFees/fetcher.d.ts} +2 -2
- package/lib/trade/fees/fundingFees/index.d.ts +28 -39
- package/lib/trade/fees/fundingFees/index.js +33 -46
- package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
- package/lib/trade/fees/fundingFees/pairContext.js +17 -0
- package/lib/trade/fees/holdingFees/index.d.ts +46 -0
- package/lib/trade/fees/holdingFees/index.js +105 -0
- package/lib/trade/fees/holdingFees/types.d.ts +23 -0
- package/lib/trade/fees/holdingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +4 -2
- package/lib/trade/fees/index.js +45 -36
- 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 +1 -0
- package/lib/trade/fees/tiers/index.js +1 -0
- 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 +2 -0
- package/lib/trade/fees/trading/converter.js +5 -1
- 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 +30 -2
- package/lib/trade/fees/trading/index.js +52 -1
- package/lib/trade/fees/trading/types.d.ts +9 -0
- package/lib/trade/index.d.ts +3 -1
- package/lib/trade/index.js +3 -1
- 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 +88 -0
- package/lib/trade/pnl/index.js +286 -0
- package/lib/trade/pnl/types.d.ts +75 -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 +119 -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 +235 -0
- package/lib/trade/priceImpact/index.d.ts +18 -5
- package/lib/trade/priceImpact/index.js +57 -10
- 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 +74 -0
- package/lib/trade/priceImpact/open/types.d.ts +41 -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 +11 -42
- package/lib/trade/priceImpact/skew/converter.js +19 -109
- package/lib/{contracts/fetch/priceImpact/skew.d.ts → trade/priceImpact/skew/fetcher.d.ts} +9 -12
- package/lib/{contracts/fetch/priceImpact/skew.js → trade/priceImpact/skew/fetcher.js} +23 -22
- package/lib/trade/priceImpact/skew/index.d.ts +11 -15
- package/lib/trade/priceImpact/skew/index.js +22 -54
- package/lib/trade/priceImpact/skew/types.d.ts +2 -15
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +30 -8
- package/lib/trade/types.js +20 -0
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +2 -2
- package/package.json +2 -2
- package/lib/trade/liquidation.d.ts +0 -42
- package/lib/trade/liquidation.js +0 -140
- package/lib/trade/pnl.d.ts +0 -10
- package/lib/trade/pnl.js +0 -38
- /package/lib/{contracts/fetch/fees/fundingFees.js → trade/fees/fundingFees/fetcher.js} +0 -0
|
@@ -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
|
-
import type { GNSMultiCollatDiamond } from "
|
|
2
|
-
import type { PairOiToken, SkewPriceImpactContext } from "
|
|
1
|
+
import type { GNSMultiCollatDiamond } from "../../../contracts/types/generated";
|
|
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 priceImpact_1 = require("../../../trade/priceImpact");
|
|
|
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);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @dev Skew price impact calculations for v10+ trades
|
|
3
3
|
* @dev Based on formula: (existingSkew + tradeSize/2) / skewDepth
|
|
4
4
|
*/
|
|
5
|
-
import { PairOiToken, SkewPriceImpactInput, SkewPriceImpactResult, SkewPriceImpactContext
|
|
5
|
+
import { PairOiToken, SkewPriceImpactInput, SkewPriceImpactResult, SkewPriceImpactContext } from "./types";
|
|
6
6
|
/**
|
|
7
7
|
* @dev Calculates net skew in tokens (long - short)
|
|
8
8
|
* @param pairOi Pair OI data with long and short token amounts
|
|
@@ -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,25 +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
|
|
31
|
-
* @param
|
|
30
|
+
* @param skewDepth Skew depth in tokens
|
|
31
|
+
* @param tradePositiveSkew Whether trade increases skew in its direction
|
|
32
32
|
* @returns Price impact percentage (can be positive or negative)
|
|
33
33
|
*/
|
|
34
|
-
export declare const calculateSkewPriceImpactP: (existingSkewToken: number, tradeSizeToken: number, skewDepth: number,
|
|
34
|
+
export declare const calculateSkewPriceImpactP: (existingSkewToken: number, tradeSizeToken: number, skewDepth: number, tradePositiveSkew: 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: (
|
|
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;
|
|
41
|
+
export declare const getTradeSkewPriceImpact: (input: SkewPriceImpactInput, context: SkewPriceImpactContext) => SkewPriceImpactResult;
|
|
49
42
|
/**
|
|
50
43
|
* @dev Calculate position sizes for partial operations
|
|
51
44
|
* @param originalSizeCollateral Original position size in collateral
|
|
@@ -55,3 +48,6 @@ export declare const getTradeSkewPriceImpactWithChecks: (params: TradeSkewParams
|
|
|
55
48
|
*/
|
|
56
49
|
export declare const calculatePartialSizeToken: (originalSizeCollateral: number, deltaCollateral: number, originalSizeToken: number) => number;
|
|
57
50
|
export * as SkewPriceImpact from "./types";
|
|
51
|
+
export * from "./converter";
|
|
52
|
+
export * from "./builder";
|
|
53
|
+
export * from "./fetcher";
|
|
@@ -26,9 +26,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
26
26
|
__setModuleDefault(result, mod);
|
|
27
27
|
return result;
|
|
28
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
|
+
};
|
|
29
32
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.
|
|
31
|
-
const utils_1 = require("../../utils");
|
|
33
|
+
exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = void 0;
|
|
32
34
|
// Constants
|
|
33
35
|
const PRICE_IMPACT_DIVIDER = 2; // Half price impact to match cumulative volume impact scale
|
|
34
36
|
/**
|
|
@@ -41,10 +43,10 @@ const getNetSkewToken = (pairOi) => {
|
|
|
41
43
|
};
|
|
42
44
|
exports.getNetSkewToken = getNetSkewToken;
|
|
43
45
|
/**
|
|
44
|
-
* @dev Calculates net skew in collateral
|
|
46
|
+
* @dev Calculates net skew in collateral
|
|
45
47
|
* @param netSkewToken Net skew in tokens
|
|
46
48
|
* @param currentPrice Current pair price
|
|
47
|
-
* @returns Net skew in collateral
|
|
49
|
+
* @returns Net skew in collateral
|
|
48
50
|
*/
|
|
49
51
|
const getNetSkewCollateral = (netSkewToken, currentPrice) => {
|
|
50
52
|
return netSkewToken * currentPrice;
|
|
@@ -66,49 +68,40 @@ exports.getTradeSkewDirection = getTradeSkewDirection;
|
|
|
66
68
|
* @dev Core skew price impact calculation
|
|
67
69
|
* @param existingSkewToken Current net skew in tokens (signed)
|
|
68
70
|
* @param tradeSizeToken Trade size in tokens (always positive)
|
|
69
|
-
* @param skewDepth Skew depth in
|
|
70
|
-
* @param
|
|
71
|
+
* @param skewDepth Skew depth in tokens
|
|
72
|
+
* @param tradePositiveSkew Whether trade increases skew in its direction
|
|
71
73
|
* @returns Price impact percentage (can be positive or negative)
|
|
72
74
|
*/
|
|
73
|
-
const calculateSkewPriceImpactP = (existingSkewToken, tradeSizeToken, skewDepth,
|
|
75
|
+
const calculateSkewPriceImpactP = (existingSkewToken, tradeSizeToken, skewDepth, tradePositiveSkew) => {
|
|
74
76
|
if (skewDepth === 0) {
|
|
75
77
|
return 0; // No impact if depth is 0
|
|
76
78
|
}
|
|
77
79
|
// Convert signed values based on trade direction
|
|
78
|
-
const tradeSkewMultiplier =
|
|
80
|
+
const tradeSkewMultiplier = tradePositiveSkew ? 1 : -1;
|
|
79
81
|
const signedExistingSkew = existingSkewToken;
|
|
80
82
|
const signedTradeSize = tradeSizeToken * tradeSkewMultiplier;
|
|
81
|
-
//
|
|
83
|
+
// (existingSkew + tradeSize/2) / skewDepth
|
|
82
84
|
const numerator = signedExistingSkew + signedTradeSize / 2;
|
|
83
|
-
const priceImpactP =
|
|
85
|
+
const priceImpactP = numerator / skewDepth;
|
|
84
86
|
// Apply divider to match cumulative volume impact scale
|
|
85
87
|
return priceImpactP / PRICE_IMPACT_DIVIDER;
|
|
86
88
|
};
|
|
87
89
|
exports.calculateSkewPriceImpactP = calculateSkewPriceImpactP;
|
|
88
90
|
/**
|
|
89
91
|
* @dev Main function to calculate skew price impact for a trade
|
|
90
|
-
* @param context Skew price impact context with depths and OI data
|
|
91
92
|
* @param input Trade parameters
|
|
93
|
+
* @param context Skew price impact context with depths and OI data
|
|
92
94
|
* @returns Skew price impact result
|
|
93
95
|
*/
|
|
94
|
-
const getTradeSkewPriceImpact = (
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const skewDepth = (_a = context.skewDepths[input.collateralIndex]) === null || _a === void 0 ? void 0 : _a[input.pairIndex];
|
|
98
|
-
if (skewDepth === undefined) {
|
|
99
|
-
throw new Error(`Missing skew depth for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
100
|
-
}
|
|
101
|
-
// Get pair OI data
|
|
102
|
-
const pairOi = (_b = context.pairOiTokens[input.collateralIndex]) === null || _b === void 0 ? void 0 : _b[input.pairIndex];
|
|
103
|
-
if (!pairOi) {
|
|
104
|
-
throw new Error(`Missing pair OI data for collateral ${input.collateralIndex} pair ${input.pairIndex}`);
|
|
105
|
-
}
|
|
96
|
+
const getTradeSkewPriceImpact = (input, context) => {
|
|
97
|
+
// Get skew depth and pair OI from simplified context
|
|
98
|
+
const { skewDepth, pairOiToken: pairOi } = context;
|
|
106
99
|
// Calculate net skew
|
|
107
100
|
const netSkewToken = (0, exports.getNetSkewToken)(pairOi);
|
|
108
|
-
// Determine trade direction
|
|
109
|
-
const
|
|
101
|
+
// Determine trade direction
|
|
102
|
+
const tradePositiveSkew = (0, exports.getTradeSkewDirection)(input.long, input.open);
|
|
110
103
|
// Calculate price impact
|
|
111
|
-
const priceImpactP = (0, exports.calculateSkewPriceImpactP)(netSkewToken, input.positionSizeToken, skewDepth,
|
|
104
|
+
const priceImpactP = (0, exports.calculateSkewPriceImpactP)(netSkewToken, input.positionSizeToken, skewDepth, tradePositiveSkew);
|
|
112
105
|
// Determine trade direction relative to skew
|
|
113
106
|
let tradeDirection;
|
|
114
107
|
if (priceImpactP > 0) {
|
|
@@ -128,34 +121,6 @@ const getTradeSkewPriceImpact = (context, input) => {
|
|
|
128
121
|
};
|
|
129
122
|
};
|
|
130
123
|
exports.getTradeSkewPriceImpact = getTradeSkewPriceImpact;
|
|
131
|
-
/**
|
|
132
|
-
* @dev Calculate skew price impact for a trade with all parameters
|
|
133
|
-
* @param params Trade parameters including price and version checks
|
|
134
|
-
* @param context Skew price impact context
|
|
135
|
-
* @returns Price impact percentage or 0 if not applicable
|
|
136
|
-
*/
|
|
137
|
-
const getTradeSkewPriceImpactWithChecks = (params, context) => {
|
|
138
|
-
// v10+ trades only
|
|
139
|
-
if (params.contractsVersion < 10) {
|
|
140
|
-
return 0;
|
|
141
|
-
}
|
|
142
|
-
// Counter trades don't pay skew impact
|
|
143
|
-
if (params.isCounterTrade) {
|
|
144
|
-
return 0;
|
|
145
|
-
}
|
|
146
|
-
// Calculate position size in tokens
|
|
147
|
-
const positionSizeToken = (0, utils_1.calculatePositionSizeToken)(params.positionSizeCollateral, params.currentPrice);
|
|
148
|
-
// Get skew price impact
|
|
149
|
-
const result = (0, exports.getTradeSkewPriceImpact)(context, {
|
|
150
|
-
collateralIndex: params.collateralIndex,
|
|
151
|
-
pairIndex: params.pairIndex,
|
|
152
|
-
long: params.long,
|
|
153
|
-
open: params.open,
|
|
154
|
-
positionSizeToken,
|
|
155
|
-
});
|
|
156
|
-
return result.priceImpactP;
|
|
157
|
-
};
|
|
158
|
-
exports.getTradeSkewPriceImpactWithChecks = getTradeSkewPriceImpactWithChecks;
|
|
159
124
|
/**
|
|
160
125
|
* @dev Calculate position sizes for partial operations
|
|
161
126
|
* @param originalSizeCollateral Original position size in collateral
|
|
@@ -173,3 +138,6 @@ const calculatePartialSizeToken = (originalSizeCollateral, deltaCollateral, orig
|
|
|
173
138
|
exports.calculatePartialSizeToken = calculatePartialSizeToken;
|
|
174
139
|
// Export namespace for types
|
|
175
140
|
exports.SkewPriceImpact = __importStar(require("./types"));
|
|
141
|
+
__exportStar(require("./converter"), exports);
|
|
142
|
+
__exportStar(require("./builder"), exports);
|
|
143
|
+
__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/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";
|