@gainsnetwork/sdk 0.2.71-rc4 → 1.0.0-rc2
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/README.md +2 -2
- 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 +0 -3
- package/lib/constants.js +7 -9
- 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 +2134 -293
- 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/GToken.d.ts +78 -107
- 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.d.ts +90 -53
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +4496 -430
- 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 +69 -142
- 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 +0 -3
- 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 +13 -0
- package/lib/markets/price/marketPrice.js +35 -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 +23 -2
- package/lib/trade/fees/borrowing/index.js +50 -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/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 +8 -2
- package/lib/trade/fees/index.js +67 -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 +5 -2
- package/lib/trade/index.js +5 -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 +29 -0
- package/lib/trade/liquidation/index.js +218 -0
- package/lib/trade/liquidation/types.d.ts +43 -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 +89 -0
- package/lib/trade/pnl/index.js +302 -0
- package/lib/trade/pnl/types.d.ts +79 -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 +47 -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 +21 -0
- package/lib/trade/priceImpact/index.js +79 -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 +78 -0
- package/lib/trade/priceImpact/open/types.d.ts +44 -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 +53 -0
- package/lib/trade/priceImpact/skew/index.js +148 -0
- package/lib/trade/priceImpact/skew/types.d.ts +44 -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 +109 -12
- package/lib/trade/types.js +0 -3
- 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,22 @@
|
|
|
1
|
+
import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
|
|
2
|
+
import { CumulVolContext } from "./index";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Builds cumulative volume price impact sub-context for a specific pair
|
|
5
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
6
|
+
* @param collateralIndex The collateral index (1-based)
|
|
7
|
+
* @param pairIndex The pair index
|
|
8
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
9
|
+
* @returns Cumulative volume context ready for getTradeCumulVolPriceImpactP
|
|
10
|
+
*/
|
|
11
|
+
export declare const buildCumulVolContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, additionalParams: {
|
|
12
|
+
currentBlock: number;
|
|
13
|
+
contractsVersion?: number;
|
|
14
|
+
isPnlPositive?: boolean;
|
|
15
|
+
isOpen?: boolean;
|
|
16
|
+
createdBlock?: number;
|
|
17
|
+
userPriceImpact?: {
|
|
18
|
+
cumulVolPriceImpactMultiplier: number;
|
|
19
|
+
fixedSpreadP: number;
|
|
20
|
+
};
|
|
21
|
+
protectionCloseFactorWhitelist?: boolean;
|
|
22
|
+
}) => CumulVolContext | undefined;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildCumulVolContext = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @dev Builds cumulative volume price impact sub-context for a specific pair
|
|
6
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
7
|
+
* @param collateralIndex The collateral index (1-based)
|
|
8
|
+
* @param pairIndex The pair index
|
|
9
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
10
|
+
* @returns Cumulative volume context ready for getTradeCumulVolPriceImpactP
|
|
11
|
+
*/
|
|
12
|
+
const buildCumulVolContext = (globalTradingVariables, collateralIndex, pairIndex, additionalParams) => {
|
|
13
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
14
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
15
|
+
if (!collateral) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
// Get pair-specific data from global variables
|
|
19
|
+
const pairDepth = (_a = globalTradingVariables.pairDepths) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
20
|
+
const pairFactor = (_b = globalTradingVariables.pairFactors) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
21
|
+
const oiWindows = (_c = globalTradingVariables.oiWindows) === null || _c === void 0 ? void 0 : _c[pairIndex];
|
|
22
|
+
// Get OI windows settings (same for all pairs)
|
|
23
|
+
// OI windows settings from global trading variables are already in SDK format
|
|
24
|
+
const oiWindowsSettings = globalTradingVariables.oiWindowsSettings;
|
|
25
|
+
// Get user-specific parameters from additionalParams
|
|
26
|
+
const userPriceImpact = additionalParams.userPriceImpact;
|
|
27
|
+
const protectionCloseFactorWhitelist = additionalParams.protectionCloseFactorWhitelist;
|
|
28
|
+
// Get liquidation params - check both pair and group level
|
|
29
|
+
const liquidationParams = ((_e = (_d = globalTradingVariables.liquidationParams) === null || _d === void 0 ? void 0 : _d.pairs) === null || _e === void 0 ? void 0 : _e[pairIndex]) ||
|
|
30
|
+
((_g = (_f = globalTradingVariables.liquidationParams) === null || _f === void 0 ? void 0 : _f.groups) === null || _g === void 0 ? void 0 : _g[0]); // fallback to first group
|
|
31
|
+
return Object.assign({
|
|
32
|
+
// Trade state
|
|
33
|
+
isOpen: additionalParams.isOpen, isPnlPositive: additionalParams.isPnlPositive, createdBlock: additionalParams.createdBlock,
|
|
34
|
+
// Protection factors
|
|
35
|
+
liquidationParams, currentBlock: additionalParams.currentBlock, contractsVersion: additionalParams.contractsVersion, protectionCloseFactorWhitelist,
|
|
36
|
+
// Price impact data
|
|
37
|
+
pairDepth,
|
|
38
|
+
oiWindowsSettings,
|
|
39
|
+
oiWindows,
|
|
40
|
+
// User/collateral specific
|
|
41
|
+
userPriceImpact, collateralPriceUsd: ((_h = collateral.prices) === null || _h === void 0 ? void 0 : _h.collateralPriceUsd) || 1 }, pairFactor);
|
|
42
|
+
};
|
|
43
|
+
exports.buildCumulVolContext = buildCumulVolContext;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Converters for cumulative volume price impact data between contract and SDK formats
|
|
3
|
+
* @dev All BigNumber values are normalized to floats with appropriate precision
|
|
4
|
+
*/
|
|
5
|
+
import { IPriceImpact } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
6
|
+
import { OiWindowsSettings, OiWindow, OiWindows } from "../../types";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Converts contract OI windows settings to SDK format
|
|
9
|
+
* @param contractData Contract OiWindowsSettings struct
|
|
10
|
+
* @returns Normalized OI windows settings
|
|
11
|
+
*/
|
|
12
|
+
export declare const convertOiWindowsSettings: (contractData: IPriceImpact.OiWindowsSettingsStructOutput) => OiWindowsSettings;
|
|
13
|
+
/**
|
|
14
|
+
* @dev Converts contract PairOi data to SDK OiWindow format
|
|
15
|
+
* @param contractData Contract PairOi struct with USD values
|
|
16
|
+
* @returns Normalized OI window data
|
|
17
|
+
*/
|
|
18
|
+
export declare const convertOiWindow: (contractData: IPriceImpact.PairOiStructOutput) => OiWindow;
|
|
19
|
+
/**
|
|
20
|
+
* @dev Converts array of OI windows from contract format
|
|
21
|
+
* @param windowIds Array of window IDs (as strings for mapping)
|
|
22
|
+
* @param contractWindows Array of PairOi data from contract
|
|
23
|
+
* @returns Normalized OI windows mapping
|
|
24
|
+
*/
|
|
25
|
+
export declare const convertOiWindows: (windowIds: string[], contractWindows: IPriceImpact.PairOiStructOutput[]) => OiWindows;
|
|
26
|
+
/**
|
|
27
|
+
* @dev Batch converter for multiple OI windows settings
|
|
28
|
+
* @param contractDataArray Array of contract OiWindowsSettings
|
|
29
|
+
* @returns Array of normalized OI windows settings
|
|
30
|
+
*/
|
|
31
|
+
export declare const convertOiWindowsSettingsArray: (contractDataArray: IPriceImpact.OiWindowsSettingsStructOutput[]) => OiWindowsSettings[];
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Converters for cumulative volume 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.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* @dev Converts contract OI windows settings to SDK format
|
|
10
|
+
* @param contractData Contract OiWindowsSettings struct
|
|
11
|
+
* @returns Normalized OI windows settings
|
|
12
|
+
*/
|
|
13
|
+
const convertOiWindowsSettings = (contractData) => {
|
|
14
|
+
return {
|
|
15
|
+
startTs: Number(contractData.startTs),
|
|
16
|
+
windowsDuration: Number(contractData.windowsDuration),
|
|
17
|
+
windowsCount: Number(contractData.windowsCount),
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.convertOiWindowsSettings = convertOiWindowsSettings;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts contract PairOi data to SDK OiWindow format
|
|
23
|
+
* @param contractData Contract PairOi struct with USD values
|
|
24
|
+
* @returns Normalized OI window data
|
|
25
|
+
*/
|
|
26
|
+
const convertOiWindow = (contractData) => {
|
|
27
|
+
// USD values are stored as 1e18 in contract
|
|
28
|
+
return {
|
|
29
|
+
oiLongUsd: Number(contractData.oiLongUsd) / 1e18,
|
|
30
|
+
oiShortUsd: Number(contractData.oiShortUsd) / 1e18,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
exports.convertOiWindow = convertOiWindow;
|
|
34
|
+
/**
|
|
35
|
+
* @dev Converts array of OI windows from contract format
|
|
36
|
+
* @param windowIds Array of window IDs (as strings for mapping)
|
|
37
|
+
* @param contractWindows Array of PairOi data from contract
|
|
38
|
+
* @returns Normalized OI windows mapping
|
|
39
|
+
*/
|
|
40
|
+
const convertOiWindows = (windowIds, contractWindows) => {
|
|
41
|
+
if (windowIds.length !== contractWindows.length) {
|
|
42
|
+
throw new Error("Window IDs and data arrays must have the same length");
|
|
43
|
+
}
|
|
44
|
+
const windows = {};
|
|
45
|
+
windowIds.forEach((id, index) => {
|
|
46
|
+
windows[id] = (0, exports.convertOiWindow)(contractWindows[index]);
|
|
47
|
+
});
|
|
48
|
+
return windows;
|
|
49
|
+
};
|
|
50
|
+
exports.convertOiWindows = convertOiWindows;
|
|
51
|
+
/**
|
|
52
|
+
* @dev Batch converter for multiple OI windows settings
|
|
53
|
+
* @param contractDataArray Array of contract OiWindowsSettings
|
|
54
|
+
* @returns Array of normalized OI windows settings
|
|
55
|
+
*/
|
|
56
|
+
const convertOiWindowsSettingsArray = (contractDataArray) => {
|
|
57
|
+
return contractDataArray.map(exports.convertOiWindowsSettings);
|
|
58
|
+
};
|
|
59
|
+
exports.convertOiWindowsSettingsArray = convertOiWindowsSettingsArray;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Cumulative volume price impact calculations
|
|
3
|
+
* @dev Mirrors contract's getTradeCumulVolPriceImpactP functionality
|
|
4
|
+
*/
|
|
5
|
+
import { LiquidationParams, OiWindows, OiWindowsSettings, PairDepth, PairFactor, UserPriceImpact } from "../../types";
|
|
6
|
+
import { ContractsVersion } from "../../../contracts/types";
|
|
7
|
+
export type CumulVolContext = {
|
|
8
|
+
isOpen?: boolean;
|
|
9
|
+
isPnlPositive?: boolean;
|
|
10
|
+
createdBlock?: number;
|
|
11
|
+
liquidationParams?: LiquidationParams | undefined;
|
|
12
|
+
currentBlock?: number | undefined;
|
|
13
|
+
contractsVersion?: ContractsVersion | undefined;
|
|
14
|
+
protectionCloseFactorWhitelist?: boolean;
|
|
15
|
+
pairDepth?: PairDepth | undefined;
|
|
16
|
+
oiWindowsSettings?: OiWindowsSettings | undefined;
|
|
17
|
+
oiWindows?: OiWindows | undefined;
|
|
18
|
+
userPriceImpact?: UserPriceImpact | undefined;
|
|
19
|
+
collateralPriceUsd?: number;
|
|
20
|
+
} & Partial<PairFactor>;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Gets the protection close factor with user multiplier
|
|
23
|
+
* @param context Cumulative volume context
|
|
24
|
+
* @returns Protection close factor (1 = 100%)
|
|
25
|
+
*/
|
|
26
|
+
export declare const getProtectionCloseFactor: (context: CumulVolContext | undefined) => number;
|
|
27
|
+
/**
|
|
28
|
+
* @dev Checks if protection close factor is active
|
|
29
|
+
* @param context Cumulative volume context
|
|
30
|
+
* @returns True if protection close factor should be applied
|
|
31
|
+
*/
|
|
32
|
+
export declare const isProtectionCloseFactorActive: (context: CumulVolContext | undefined) => boolean | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Gets the cumulative factor for price impact calculation
|
|
35
|
+
* @param context Cumulative volume context
|
|
36
|
+
* @returns Cumulative factor (default 1)
|
|
37
|
+
*/
|
|
38
|
+
export declare const getCumulativeFactor: (context: CumulVolContext | undefined) => number;
|
|
39
|
+
/**
|
|
40
|
+
* @dev Gets the legacy factor for v9.2 compatibility
|
|
41
|
+
* @param context Cumulative volume context
|
|
42
|
+
* @returns 1 for pre-v9.2, 2 for v9.2+
|
|
43
|
+
*/
|
|
44
|
+
export declare const getLegacyFactor: (context: CumulVolContext | undefined) => number;
|
|
45
|
+
/**
|
|
46
|
+
* @dev Calculates cumulative volume price impact percentage
|
|
47
|
+
* @dev Mirrors contract's getTradeCumulVolPriceImpactP function
|
|
48
|
+
* @param trader Trader address
|
|
49
|
+
* @param pairIndex Trading pair index
|
|
50
|
+
* @param long True for long, false for short
|
|
51
|
+
* @param tradeOpenInterestUsd Position size in USD
|
|
52
|
+
* @param isPnlPositive Whether PnL is positive (only relevant when closing)
|
|
53
|
+
* @param open True for opening, false for closing
|
|
54
|
+
* @param lastPosIncreaseBlock Last block when position was increased (only relevant when closing)
|
|
55
|
+
* @param context Additional context with depths, OI data, and factors
|
|
56
|
+
* @returns Cumulative volume price impact percentage (not including spread)
|
|
57
|
+
*/
|
|
58
|
+
export declare const getTradeCumulVolPriceImpactP: (trader: string, pairIndex: number, long: boolean, tradeOpenInterestUsd: number, isPnlPositive: boolean, open: boolean, lastPosIncreaseBlock: number, context: CumulVolContext) => number;
|
|
59
|
+
/**
|
|
60
|
+
* @dev Gets the fixed spread percentage with direction
|
|
61
|
+
* @dev Mirrors contract's getFixedSpreadP function
|
|
62
|
+
* @param spreadP Total spread percentage (includes base + user spread)
|
|
63
|
+
* @param long True for long position
|
|
64
|
+
* @param open True for opening, false for closing
|
|
65
|
+
* @returns Signed spread percentage (positive or negative based on direction)
|
|
66
|
+
*/
|
|
67
|
+
export declare const getFixedSpreadP: (spreadP: number, long: boolean, open: boolean) => number;
|
|
68
|
+
/**
|
|
69
|
+
* @dev Gets the base spread percentage
|
|
70
|
+
* @param pairSpreadP Pair spread percentage
|
|
71
|
+
* @param isLiquidation True if liquidation
|
|
72
|
+
* @param liquidationParams Liquidation parameters
|
|
73
|
+
* @param userPriceImpact User-specific price impact settings
|
|
74
|
+
* @returns Base spread percentage
|
|
75
|
+
* @todo Review if this function still makes sense or should use getFixedSpreadP pattern
|
|
76
|
+
* Currently it may double-count user fixed spread if pairSpreadP already includes it
|
|
77
|
+
*/
|
|
78
|
+
export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation?: boolean | undefined, liquidationParams?: LiquidationParams | undefined, userPriceImpact?: UserPriceImpact | undefined) => number;
|
|
79
|
+
/**
|
|
80
|
+
* @dev Gets spread with cumulative volume price impact
|
|
81
|
+
* @dev This combines base spread + cumulative volume impact
|
|
82
|
+
* @param pairSpreadP Base pair spread percentage
|
|
83
|
+
* @param buy True for long, false for short
|
|
84
|
+
* @param collateral Collateral amount
|
|
85
|
+
* @param leverage Position leverage
|
|
86
|
+
* @param pairDepth 1% depth values for the pair
|
|
87
|
+
* @param oiWindowsSettings OI windows configuration
|
|
88
|
+
* @param oiWindows Current OI windows data
|
|
89
|
+
* @param context Additional context for the calculation
|
|
90
|
+
* @returns Total spread + cumulative volume price impact percentage
|
|
91
|
+
*/
|
|
92
|
+
export declare const getSpreadWithCumulVolPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
|
|
93
|
+
/**
|
|
94
|
+
* @dev Convenience function for calculating cumulative volume price impact
|
|
95
|
+
* @dev Uses collateral and leverage instead of USD position size
|
|
96
|
+
* @param buy True for long, false for short
|
|
97
|
+
* @param collateral Collateral amount
|
|
98
|
+
* @param leverage Position leverage
|
|
99
|
+
* @param open True for opening, false for closing
|
|
100
|
+
* @param context Full context including depths, OI data, and collateral price
|
|
101
|
+
* @returns Cumulative volume price impact percentage
|
|
102
|
+
*/
|
|
103
|
+
export declare const getCumulVolPriceImpact: (buy: boolean, collateral: number, leverage: number, open: boolean, context: CumulVolContext & {
|
|
104
|
+
collateralPriceUsd: number;
|
|
105
|
+
}) => number;
|
|
106
|
+
export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, context?: CumulVolContext | undefined) => number;
|
|
107
|
+
export { convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, } from "./converter";
|
|
108
|
+
export { buildCumulVolContext } from "./builder";
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Cumulative volume price impact calculations
|
|
4
|
+
* @dev Mirrors contract's getTradeCumulVolPriceImpactP functionality
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.buildCumulVolContext = exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = exports.getSpreadWithPriceImpactP = exports.getCumulVolPriceImpact = exports.getSpreadWithCumulVolPriceImpactP = exports.getSpreadP = exports.getFixedSpreadP = exports.getTradeCumulVolPriceImpactP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = void 0;
|
|
8
|
+
const oiWindows_1 = require("../../oiWindows");
|
|
9
|
+
const constants_1 = require("../../../constants");
|
|
10
|
+
const types_1 = require("../../../contracts/types");
|
|
11
|
+
/**
|
|
12
|
+
* @dev Gets the protection close factor with user multiplier
|
|
13
|
+
* @param context Cumulative volume context
|
|
14
|
+
* @returns Protection close factor (1 = 100%)
|
|
15
|
+
*/
|
|
16
|
+
const getProtectionCloseFactor = (context) => {
|
|
17
|
+
var _a;
|
|
18
|
+
const protectionCloseFactor = context === undefined ||
|
|
19
|
+
context.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2 ||
|
|
20
|
+
context.isOpen === undefined ||
|
|
21
|
+
context.isPnlPositive === undefined ||
|
|
22
|
+
context.protectionCloseFactor === undefined ||
|
|
23
|
+
(0, exports.isProtectionCloseFactorActive)(context) !== true
|
|
24
|
+
? constants_1.DEFAULT_PROTECTION_CLOSE_FACTOR
|
|
25
|
+
: context.protectionCloseFactor;
|
|
26
|
+
const protectionCloseFactorMultiplier = ((_a = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _a === void 0 ? void 0 : _a.cumulVolPriceImpactMultiplier) !== undefined &&
|
|
27
|
+
context.userPriceImpact.cumulVolPriceImpactMultiplier > 0
|
|
28
|
+
? context.userPriceImpact.cumulVolPriceImpactMultiplier
|
|
29
|
+
: 1;
|
|
30
|
+
return protectionCloseFactor * protectionCloseFactorMultiplier;
|
|
31
|
+
};
|
|
32
|
+
exports.getProtectionCloseFactor = getProtectionCloseFactor;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Checks if protection close factor is active
|
|
35
|
+
* @param context Cumulative volume context
|
|
36
|
+
* @returns True if protection close factor should be applied
|
|
37
|
+
*/
|
|
38
|
+
const isProtectionCloseFactorActive = (context) => {
|
|
39
|
+
if (context === undefined ||
|
|
40
|
+
context.currentBlock === undefined ||
|
|
41
|
+
context.createdBlock === undefined ||
|
|
42
|
+
context.protectionCloseFactorBlocks === undefined ||
|
|
43
|
+
context.protectionCloseFactor === undefined) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
return (context.isPnlPositive === true &&
|
|
47
|
+
context.isOpen === false &&
|
|
48
|
+
context.protectionCloseFactor > 0 &&
|
|
49
|
+
context.currentBlock <=
|
|
50
|
+
context.createdBlock + context.protectionCloseFactorBlocks &&
|
|
51
|
+
context.protectionCloseFactorWhitelist !== true);
|
|
52
|
+
};
|
|
53
|
+
exports.isProtectionCloseFactorActive = isProtectionCloseFactorActive;
|
|
54
|
+
/**
|
|
55
|
+
* @dev Gets the cumulative factor for price impact calculation
|
|
56
|
+
* @param context Cumulative volume context
|
|
57
|
+
* @returns Cumulative factor (default 1)
|
|
58
|
+
*/
|
|
59
|
+
const getCumulativeFactor = (context) => {
|
|
60
|
+
if (context === undefined ||
|
|
61
|
+
context.cumulativeFactor === undefined ||
|
|
62
|
+
context.cumulativeFactor === 0) {
|
|
63
|
+
return constants_1.DEFAULT_CUMULATIVE_FACTOR;
|
|
64
|
+
}
|
|
65
|
+
return context.cumulativeFactor;
|
|
66
|
+
};
|
|
67
|
+
exports.getCumulativeFactor = getCumulativeFactor;
|
|
68
|
+
/**
|
|
69
|
+
* @dev Gets the legacy factor for v9.2 compatibility
|
|
70
|
+
* @param context Cumulative volume context
|
|
71
|
+
* @returns 1 for pre-v9.2, 2 for v9.2+
|
|
72
|
+
*/
|
|
73
|
+
const getLegacyFactor = (context) => {
|
|
74
|
+
return (context === null || context === void 0 ? void 0 : context.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
|
|
75
|
+
};
|
|
76
|
+
exports.getLegacyFactor = getLegacyFactor;
|
|
77
|
+
/**
|
|
78
|
+
* @dev Calculates cumulative volume price impact percentage
|
|
79
|
+
* @dev Mirrors contract's getTradeCumulVolPriceImpactP function
|
|
80
|
+
* @param trader Trader address
|
|
81
|
+
* @param pairIndex Trading pair index
|
|
82
|
+
* @param long True for long, false for short
|
|
83
|
+
* @param tradeOpenInterestUsd Position size in USD
|
|
84
|
+
* @param isPnlPositive Whether PnL is positive (only relevant when closing)
|
|
85
|
+
* @param open True for opening, false for closing
|
|
86
|
+
* @param lastPosIncreaseBlock Last block when position was increased (only relevant when closing)
|
|
87
|
+
* @param context Additional context with depths, OI data, and factors
|
|
88
|
+
* @returns Cumulative volume price impact percentage (not including spread)
|
|
89
|
+
*/
|
|
90
|
+
const getTradeCumulVolPriceImpactP = (trader, pairIndex, long, tradeOpenInterestUsd, isPnlPositive, open, lastPosIncreaseBlock, context) => {
|
|
91
|
+
var _a, _b;
|
|
92
|
+
// Update context with passed parameters
|
|
93
|
+
const updatedContext = Object.assign(Object.assign({}, context), { isOpen: open, isPnlPositive: isPnlPositive, createdBlock: context.createdBlock || lastPosIncreaseBlock });
|
|
94
|
+
if (
|
|
95
|
+
// No price impact when closing pre-v9.2 trades
|
|
96
|
+
(!open && (context === null || context === void 0 ? void 0 : context.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) ||
|
|
97
|
+
// No price impact for opens when `pair.exemptOnOpen` is true
|
|
98
|
+
(open && (context === null || context === void 0 ? void 0 : context.exemptOnOpen) === true) ||
|
|
99
|
+
// No price impact for closes after `protectionCloseFactor` has expired
|
|
100
|
+
// when `pair.exemptAfterProtectionCloseFactor` is true
|
|
101
|
+
(!open &&
|
|
102
|
+
(context === null || context === void 0 ? void 0 : context.exemptAfterProtectionCloseFactor) === true &&
|
|
103
|
+
(0, exports.isProtectionCloseFactorActive)(updatedContext) !== true)) {
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
// Calculate trade skew direction (matches Solidity logic)
|
|
107
|
+
const tradePositiveSkew = (long && open) || (!long && !open);
|
|
108
|
+
const tradeSkewMultiplier = tradePositiveSkew ? 1 : -1;
|
|
109
|
+
// Select depth based on trade direction
|
|
110
|
+
// For positive skew (long open or short close), use depth above
|
|
111
|
+
// For negative skew (short open or long close), use depth below
|
|
112
|
+
const onePercentDepth = tradePositiveSkew
|
|
113
|
+
? (_a = context.pairDepth) === null || _a === void 0 ? void 0 : _a.onePercentDepthAboveUsd
|
|
114
|
+
: (_b = context.pairDepth) === null || _b === void 0 ? void 0 : _b.onePercentDepthBelowUsd;
|
|
115
|
+
let activeOi = undefined;
|
|
116
|
+
if (context.oiWindowsSettings !== undefined) {
|
|
117
|
+
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(context.oiWindowsSettings), context.oiWindowsSettings.windowsCount, context.oiWindows, open ? long : !long);
|
|
118
|
+
}
|
|
119
|
+
if (!onePercentDepth || activeOi === undefined) {
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
// Apply trade skew multiplier to match Solidity's signed calculation
|
|
123
|
+
const signedActiveOi = activeOi * tradeSkewMultiplier;
|
|
124
|
+
const signedTradeOi = tradeOpenInterestUsd * tradeSkewMultiplier;
|
|
125
|
+
// Calculate impact with proper signs (matching Solidity's _getTradePriceImpactP)
|
|
126
|
+
const finalPriceImpactP = ((signedActiveOi * (0, exports.getCumulativeFactor)(updatedContext) +
|
|
127
|
+
signedTradeOi / 2) /
|
|
128
|
+
onePercentDepth /
|
|
129
|
+
(0, exports.getLegacyFactor)(updatedContext)) *
|
|
130
|
+
(0, exports.getProtectionCloseFactor)(updatedContext);
|
|
131
|
+
return finalPriceImpactP;
|
|
132
|
+
};
|
|
133
|
+
exports.getTradeCumulVolPriceImpactP = getTradeCumulVolPriceImpactP;
|
|
134
|
+
/**
|
|
135
|
+
* @dev Gets the fixed spread percentage with direction
|
|
136
|
+
* @dev Mirrors contract's getFixedSpreadP function
|
|
137
|
+
* @param spreadP Total spread percentage (includes base + user spread)
|
|
138
|
+
* @param long True for long position
|
|
139
|
+
* @param open True for opening, false for closing
|
|
140
|
+
* @returns Signed spread percentage (positive or negative based on direction)
|
|
141
|
+
*/
|
|
142
|
+
const getFixedSpreadP = (spreadP, long, open) => {
|
|
143
|
+
// Reverse spread direction on close
|
|
144
|
+
const effectiveLong = open ? long : !long;
|
|
145
|
+
// Calculate half spread
|
|
146
|
+
const fixedSpreadP = spreadP / 2;
|
|
147
|
+
// Apply direction
|
|
148
|
+
return effectiveLong ? fixedSpreadP : -fixedSpreadP;
|
|
149
|
+
};
|
|
150
|
+
exports.getFixedSpreadP = getFixedSpreadP;
|
|
151
|
+
/**
|
|
152
|
+
* @dev Gets the base spread percentage
|
|
153
|
+
* @param pairSpreadP Pair spread percentage
|
|
154
|
+
* @param isLiquidation True if liquidation
|
|
155
|
+
* @param liquidationParams Liquidation parameters
|
|
156
|
+
* @param userPriceImpact User-specific price impact settings
|
|
157
|
+
* @returns Base spread percentage
|
|
158
|
+
* @todo Review if this function still makes sense or should use getFixedSpreadP pattern
|
|
159
|
+
* Currently it may double-count user fixed spread if pairSpreadP already includes it
|
|
160
|
+
*/
|
|
161
|
+
const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
|
|
162
|
+
var _a;
|
|
163
|
+
const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
|
|
164
|
+
if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
const spreadP = pairSpreadP / 2 + fixedSpreadP;
|
|
168
|
+
return isLiquidation === true &&
|
|
169
|
+
liquidationParams !== undefined &&
|
|
170
|
+
liquidationParams.maxLiqSpreadP > 0 &&
|
|
171
|
+
spreadP > liquidationParams.maxLiqSpreadP
|
|
172
|
+
? liquidationParams.maxLiqSpreadP
|
|
173
|
+
: spreadP;
|
|
174
|
+
};
|
|
175
|
+
exports.getSpreadP = getSpreadP;
|
|
176
|
+
/**
|
|
177
|
+
* @dev Gets spread with cumulative volume price impact
|
|
178
|
+
* @dev This combines base spread + cumulative volume impact
|
|
179
|
+
* @param pairSpreadP Base pair spread percentage
|
|
180
|
+
* @param buy True for long, false for short
|
|
181
|
+
* @param collateral Collateral amount
|
|
182
|
+
* @param leverage Position leverage
|
|
183
|
+
* @param pairDepth 1% depth values for the pair
|
|
184
|
+
* @param oiWindowsSettings OI windows configuration
|
|
185
|
+
* @param oiWindows Current OI windows data
|
|
186
|
+
* @param context Additional context for the calculation
|
|
187
|
+
* @returns Total spread + cumulative volume price impact percentage
|
|
188
|
+
*/
|
|
189
|
+
const getSpreadWithCumulVolPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, context) => {
|
|
190
|
+
if (pairSpreadP === undefined) {
|
|
191
|
+
return 0;
|
|
192
|
+
}
|
|
193
|
+
const baseSpread = (0, exports.getSpreadP)(pairSpreadP, undefined, undefined, context === null || context === void 0 ? void 0 : context.userPriceImpact);
|
|
194
|
+
// Calculate position size in USD
|
|
195
|
+
const positionSizeUsd = collateral * leverage * ((context === null || context === void 0 ? void 0 : context.collateralPriceUsd) || 1);
|
|
196
|
+
const cumulVolImpact = (0, exports.getTradeCumulVolPriceImpactP)("", // trader - not used in calculation
|
|
197
|
+
0, // pairIndex - not used in calculation
|
|
198
|
+
buy, positionSizeUsd, (context === null || context === void 0 ? void 0 : context.isPnlPositive) || false, (context === null || context === void 0 ? void 0 : context.isOpen) !== false, (context === null || context === void 0 ? void 0 : context.createdBlock) || 0, Object.assign(Object.assign({}, context), { pairDepth,
|
|
199
|
+
oiWindowsSettings,
|
|
200
|
+
oiWindows }));
|
|
201
|
+
// If no depth or OI data, return just half spread
|
|
202
|
+
if (cumulVolImpact === 0 && (!pairDepth || !oiWindowsSettings)) {
|
|
203
|
+
return pairSpreadP / 2;
|
|
204
|
+
}
|
|
205
|
+
return baseSpread + cumulVolImpact;
|
|
206
|
+
};
|
|
207
|
+
exports.getSpreadWithCumulVolPriceImpactP = getSpreadWithCumulVolPriceImpactP;
|
|
208
|
+
/**
|
|
209
|
+
* @dev Convenience function for calculating cumulative volume price impact
|
|
210
|
+
* @dev Uses collateral and leverage instead of USD position size
|
|
211
|
+
* @param buy True for long, false for short
|
|
212
|
+
* @param collateral Collateral amount
|
|
213
|
+
* @param leverage Position leverage
|
|
214
|
+
* @param open True for opening, false for closing
|
|
215
|
+
* @param context Full context including depths, OI data, and collateral price
|
|
216
|
+
* @returns Cumulative volume price impact percentage
|
|
217
|
+
*/
|
|
218
|
+
const getCumulVolPriceImpact = (buy, collateral, leverage, open, context) => {
|
|
219
|
+
const positionSizeUsd = collateral * leverage * context.collateralPriceUsd;
|
|
220
|
+
return (0, exports.getTradeCumulVolPriceImpactP)("", // trader - not used in calculation
|
|
221
|
+
0, // pairIndex - not used in calculation
|
|
222
|
+
buy, positionSizeUsd, context.isPnlPositive || false, open, context.createdBlock || 0, context);
|
|
223
|
+
};
|
|
224
|
+
exports.getCumulVolPriceImpact = getCumulVolPriceImpact;
|
|
225
|
+
// Legacy export for backward compatibility
|
|
226
|
+
exports.getSpreadWithPriceImpactP = exports.getSpreadWithCumulVolPriceImpactP;
|
|
227
|
+
// Export converters
|
|
228
|
+
var converter_1 = require("./converter");
|
|
229
|
+
Object.defineProperty(exports, "convertOiWindowsSettings", { enumerable: true, get: function () { return converter_1.convertOiWindowsSettings; } });
|
|
230
|
+
Object.defineProperty(exports, "convertOiWindow", { enumerable: true, get: function () { return converter_1.convertOiWindow; } });
|
|
231
|
+
Object.defineProperty(exports, "convertOiWindows", { enumerable: true, get: function () { return converter_1.convertOiWindows; } });
|
|
232
|
+
Object.defineProperty(exports, "convertOiWindowsSettingsArray", { enumerable: true, get: function () { return converter_1.convertOiWindowsSettingsArray; } });
|
|
233
|
+
// Export builder
|
|
234
|
+
var builder_1 = require("./builder");
|
|
235
|
+
Object.defineProperty(exports, "buildCumulVolContext", { enumerable: true, get: function () { return builder_1.buildCumulVolContext; } });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Main price impact module
|
|
3
|
+
* @dev Exports cumulative volume, skew, and combined opening/closing price impact functionality
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @dev Calculates price after impact using the same formula as the Solidity contract
|
|
7
|
+
* @dev Mirrors contract's getPriceAfterImpact function
|
|
8
|
+
* @param oraclePrice Base oracle price (no decimals requirement)
|
|
9
|
+
* @param totalPriceImpactP Total price impact percentage (can be positive or negative)
|
|
10
|
+
* @returns Price after impact has been applied
|
|
11
|
+
*/
|
|
12
|
+
export declare const getPriceAfterImpact: (oraclePrice: number, totalPriceImpactP: number) => number;
|
|
13
|
+
export { getTradeOpeningPriceImpact, getTradeOpeningPriceImpactAtMarket, buildTradeOpeningPriceImpactContext, TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult, } from "./open";
|
|
14
|
+
export { getTradeClosingPriceImpact, getTradeClosingPriceImpactAtOracle, buildTradeClosingPriceImpactContext, TradeClosingPriceImpactInput, TradeClosingPriceImpactContext, TradeClosingPriceImpactResult, } from "./close";
|
|
15
|
+
export { getTradeCumulVolPriceImpactP, getCumulVolPriceImpact, // Convenience function
|
|
16
|
+
getSpreadWithCumulVolPriceImpactP, getSpreadWithPriceImpactP, // Legacy alias
|
|
17
|
+
getProtectionCloseFactor, isProtectionCloseFactorActive, getCumulativeFactor, getLegacyFactor, getFixedSpreadP, getSpreadP, convertOiWindowsSettings, convertOiWindow, convertOiWindows, convertOiWindowsSettingsArray, buildCumulVolContext, CumulVolContext, } from "./cumulVol";
|
|
18
|
+
export { getNetSkewToken, getNetSkewCollateral, getTradeSkewDirection, calculateSkewPriceImpactP, getTradeSkewPriceImpact, calculatePartialSizeToken, SkewPriceImpact, } from "./skew";
|
|
19
|
+
export { convertPairOiToken, convertPairOiTokenArray, convertPairOiCollateral, convertPairOiCollateralArray, convertSkewDepth, convertPairSkewDepths, } from "./skew/converter";
|
|
20
|
+
export { buildSkewPriceImpactContext } from "./skew/builder";
|
|
21
|
+
export type { PairOiToken, PairOiCollateral, SkewPriceImpactInput, SkewPriceImpactResult, SkewPriceImpactContext, TradeSkewParams, PositionSizeResult, } from "./skew/types";
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Main price impact module
|
|
4
|
+
* @dev Exports cumulative volume, skew, and combined opening/closing price impact functionality
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.buildSkewPriceImpactContext = exports.convertPairSkewDepths = exports.convertSkewDepth = exports.convertPairOiCollateralArray = exports.convertPairOiCollateral = exports.convertPairOiTokenArray = exports.convertPairOiToken = exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = exports.buildCumulVolContext = exports.convertOiWindowsSettingsArray = exports.convertOiWindows = exports.convertOiWindow = exports.convertOiWindowsSettings = exports.getSpreadP = exports.getFixedSpreadP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = exports.getSpreadWithPriceImpactP = exports.getSpreadWithCumulVolPriceImpactP = exports.getCumulVolPriceImpact = exports.getTradeCumulVolPriceImpactP = exports.buildTradeClosingPriceImpactContext = exports.getTradeClosingPriceImpactAtOracle = exports.getTradeClosingPriceImpact = exports.buildTradeOpeningPriceImpactContext = exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = exports.getPriceAfterImpact = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* @dev Calculates price after impact using the same formula as the Solidity contract
|
|
10
|
+
* @dev Mirrors contract's getPriceAfterImpact function
|
|
11
|
+
* @param oraclePrice Base oracle price (no decimals requirement)
|
|
12
|
+
* @param totalPriceImpactP Total price impact percentage (can be positive or negative)
|
|
13
|
+
* @returns Price after impact has been applied
|
|
14
|
+
*/
|
|
15
|
+
const getPriceAfterImpact = (oraclePrice, totalPriceImpactP) => {
|
|
16
|
+
// Match Solidity: price = oraclePrice + (oraclePrice * totalPriceImpactP / 100)
|
|
17
|
+
const priceAfterImpact = oraclePrice * (1 + totalPriceImpactP / 100);
|
|
18
|
+
if (priceAfterImpact <= 0) {
|
|
19
|
+
throw new Error("Price after impact must be positive");
|
|
20
|
+
}
|
|
21
|
+
return priceAfterImpact;
|
|
22
|
+
};
|
|
23
|
+
exports.getPriceAfterImpact = getPriceAfterImpact;
|
|
24
|
+
// Export trade opening price impact functionality
|
|
25
|
+
var open_1 = require("./open");
|
|
26
|
+
// Core functions
|
|
27
|
+
Object.defineProperty(exports, "getTradeOpeningPriceImpact", { enumerable: true, get: function () { return open_1.getTradeOpeningPriceImpact; } });
|
|
28
|
+
Object.defineProperty(exports, "getTradeOpeningPriceImpactAtMarket", { enumerable: true, get: function () { return open_1.getTradeOpeningPriceImpactAtMarket; } });
|
|
29
|
+
// Builder
|
|
30
|
+
Object.defineProperty(exports, "buildTradeOpeningPriceImpactContext", { enumerable: true, get: function () { return open_1.buildTradeOpeningPriceImpactContext; } });
|
|
31
|
+
// Export trade closing price impact functionality
|
|
32
|
+
var close_1 = require("./close");
|
|
33
|
+
// Core functions
|
|
34
|
+
Object.defineProperty(exports, "getTradeClosingPriceImpact", { enumerable: true, get: function () { return close_1.getTradeClosingPriceImpact; } });
|
|
35
|
+
Object.defineProperty(exports, "getTradeClosingPriceImpactAtOracle", { enumerable: true, get: function () { return close_1.getTradeClosingPriceImpactAtOracle; } });
|
|
36
|
+
// Builder
|
|
37
|
+
Object.defineProperty(exports, "buildTradeClosingPriceImpactContext", { enumerable: true, get: function () { return close_1.buildTradeClosingPriceImpactContext; } });
|
|
38
|
+
// Export cumulative volume price impact functionality
|
|
39
|
+
var cumulVol_1 = require("./cumulVol");
|
|
40
|
+
// Core functions
|
|
41
|
+
Object.defineProperty(exports, "getTradeCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getTradeCumulVolPriceImpactP; } });
|
|
42
|
+
Object.defineProperty(exports, "getCumulVolPriceImpact", { enumerable: true, get: function () { return cumulVol_1.getCumulVolPriceImpact; } });
|
|
43
|
+
Object.defineProperty(exports, "getSpreadWithCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithCumulVolPriceImpactP; } });
|
|
44
|
+
Object.defineProperty(exports, "getSpreadWithPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithPriceImpactP; } });
|
|
45
|
+
Object.defineProperty(exports, "getProtectionCloseFactor", { enumerable: true, get: function () { return cumulVol_1.getProtectionCloseFactor; } });
|
|
46
|
+
Object.defineProperty(exports, "isProtectionCloseFactorActive", { enumerable: true, get: function () { return cumulVol_1.isProtectionCloseFactorActive; } });
|
|
47
|
+
Object.defineProperty(exports, "getCumulativeFactor", { enumerable: true, get: function () { return cumulVol_1.getCumulativeFactor; } });
|
|
48
|
+
Object.defineProperty(exports, "getLegacyFactor", { enumerable: true, get: function () { return cumulVol_1.getLegacyFactor; } });
|
|
49
|
+
Object.defineProperty(exports, "getFixedSpreadP", { enumerable: true, get: function () { return cumulVol_1.getFixedSpreadP; } });
|
|
50
|
+
Object.defineProperty(exports, "getSpreadP", { enumerable: true, get: function () { return cumulVol_1.getSpreadP; } });
|
|
51
|
+
// Converters
|
|
52
|
+
Object.defineProperty(exports, "convertOiWindowsSettings", { enumerable: true, get: function () { return cumulVol_1.convertOiWindowsSettings; } });
|
|
53
|
+
Object.defineProperty(exports, "convertOiWindow", { enumerable: true, get: function () { return cumulVol_1.convertOiWindow; } });
|
|
54
|
+
Object.defineProperty(exports, "convertOiWindows", { enumerable: true, get: function () { return cumulVol_1.convertOiWindows; } });
|
|
55
|
+
Object.defineProperty(exports, "convertOiWindowsSettingsArray", { enumerable: true, get: function () { return cumulVol_1.convertOiWindowsSettingsArray; } });
|
|
56
|
+
// Builder
|
|
57
|
+
Object.defineProperty(exports, "buildCumulVolContext", { enumerable: true, get: function () { return cumulVol_1.buildCumulVolContext; } });
|
|
58
|
+
// Export skew price impact functionality
|
|
59
|
+
var skew_1 = require("./skew");
|
|
60
|
+
// Core functions
|
|
61
|
+
Object.defineProperty(exports, "getNetSkewToken", { enumerable: true, get: function () { return skew_1.getNetSkewToken; } });
|
|
62
|
+
Object.defineProperty(exports, "getNetSkewCollateral", { enumerable: true, get: function () { return skew_1.getNetSkewCollateral; } });
|
|
63
|
+
Object.defineProperty(exports, "getTradeSkewDirection", { enumerable: true, get: function () { return skew_1.getTradeSkewDirection; } });
|
|
64
|
+
Object.defineProperty(exports, "calculateSkewPriceImpactP", { enumerable: true, get: function () { return skew_1.calculateSkewPriceImpactP; } });
|
|
65
|
+
Object.defineProperty(exports, "getTradeSkewPriceImpact", { enumerable: true, get: function () { return skew_1.getTradeSkewPriceImpact; } });
|
|
66
|
+
Object.defineProperty(exports, "calculatePartialSizeToken", { enumerable: true, get: function () { return skew_1.calculatePartialSizeToken; } });
|
|
67
|
+
// Types namespace
|
|
68
|
+
Object.defineProperty(exports, "SkewPriceImpact", { enumerable: true, get: function () { return skew_1.SkewPriceImpact; } });
|
|
69
|
+
// Export converters
|
|
70
|
+
var converter_1 = require("./skew/converter");
|
|
71
|
+
Object.defineProperty(exports, "convertPairOiToken", { enumerable: true, get: function () { return converter_1.convertPairOiToken; } });
|
|
72
|
+
Object.defineProperty(exports, "convertPairOiTokenArray", { enumerable: true, get: function () { return converter_1.convertPairOiTokenArray; } });
|
|
73
|
+
Object.defineProperty(exports, "convertPairOiCollateral", { enumerable: true, get: function () { return converter_1.convertPairOiCollateral; } });
|
|
74
|
+
Object.defineProperty(exports, "convertPairOiCollateralArray", { enumerable: true, get: function () { return converter_1.convertPairOiCollateralArray; } });
|
|
75
|
+
Object.defineProperty(exports, "convertSkewDepth", { enumerable: true, get: function () { return converter_1.convertSkewDepth; } });
|
|
76
|
+
Object.defineProperty(exports, "convertPairSkewDepths", { enumerable: true, get: function () { return converter_1.convertPairSkewDepths; } });
|
|
77
|
+
// Export builders
|
|
78
|
+
var builder_1 = require("./skew/builder");
|
|
79
|
+
Object.defineProperty(exports, "buildSkewPriceImpactContext", { enumerable: true, get: function () { return builder_1.buildSkewPriceImpactContext; } });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
|
|
2
|
+
import { TradeOpeningPriceImpactContext } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Builds a complete context for trade opening price impact calculations
|
|
5
|
+
* @dev Uses sub-context builders to create properly scoped contexts
|
|
6
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
7
|
+
* @param collateralIndex The collateral index (1-based)
|
|
8
|
+
* @param pairIndex The pair index
|
|
9
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
10
|
+
* @returns Complete context ready for getTradeOpeningPriceImpact
|
|
11
|
+
*/
|
|
12
|
+
export declare const buildTradeOpeningPriceImpactContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, additionalParams: {
|
|
13
|
+
currentBlock: number;
|
|
14
|
+
contractsVersion?: number;
|
|
15
|
+
trader?: string;
|
|
16
|
+
userPriceImpact?: {
|
|
17
|
+
cumulVolPriceImpactMultiplier: number;
|
|
18
|
+
fixedSpreadP: number;
|
|
19
|
+
};
|
|
20
|
+
protectionCloseFactorWhitelist?: boolean;
|
|
21
|
+
}) => TradeOpeningPriceImpactContext | undefined;
|