@gainsnetwork/sdk 0.2.73-rc2 → 1.0.0-rc10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 -5
- package/lib/constants.js +0 -5
- 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 -5
- 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 +34 -0
- package/lib/trade/fees/borrowing/index.d.ts +26 -3
- package/lib/trade/fees/borrowing/index.js +72 -22
- 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 +9 -2
- package/lib/trade/fees/index.js +69 -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 +91 -0
- package/lib/trade/pnl/index.js +303 -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 +79 -0
- package/lib/trade/priceImpact/open/types.d.ts +45 -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 -14
- package/lib/trade/types.js +0 -5
- 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,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildTradeOpeningPriceImpactContext = void 0;
|
|
4
|
+
const builder_1 = require("../cumulVol/builder");
|
|
5
|
+
const builder_2 = require("../skew/builder");
|
|
6
|
+
/**
|
|
7
|
+
* @dev Builds a complete context for trade opening price impact calculations
|
|
8
|
+
* @dev Uses sub-context builders to create properly scoped contexts
|
|
9
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
10
|
+
* @param collateralIndex The collateral index (1-based)
|
|
11
|
+
* @param pairIndex The pair index
|
|
12
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
13
|
+
* @returns Complete context ready for getTradeOpeningPriceImpact
|
|
14
|
+
*/
|
|
15
|
+
const buildTradeOpeningPriceImpactContext = (globalTradingVariables, collateralIndex, pairIndex, additionalParams) => {
|
|
16
|
+
var _a;
|
|
17
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
18
|
+
if (!collateral) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
// Build cumulative volume subcontext
|
|
22
|
+
const cumulVolContext = (0, builder_1.buildCumulVolContext)(globalTradingVariables, collateralIndex, pairIndex, {
|
|
23
|
+
currentBlock: additionalParams.currentBlock,
|
|
24
|
+
contractsVersion: additionalParams.contractsVersion,
|
|
25
|
+
isPnlPositive: false,
|
|
26
|
+
isOpen: true,
|
|
27
|
+
createdBlock: undefined,
|
|
28
|
+
userPriceImpact: additionalParams.userPriceImpact,
|
|
29
|
+
protectionCloseFactorWhitelist: additionalParams.protectionCloseFactorWhitelist,
|
|
30
|
+
});
|
|
31
|
+
// Build skew price impact subcontext
|
|
32
|
+
const skewContext = (0, builder_2.buildSkewPriceImpactContext)(collateral, pairIndex);
|
|
33
|
+
if (!cumulVolContext || !skewContext) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
// Return structured context with proper subcontexts
|
|
37
|
+
return {
|
|
38
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
39
|
+
cumulVolContext,
|
|
40
|
+
skewContext,
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
exports.buildTradeOpeningPriceImpactContext = buildTradeOpeningPriceImpactContext;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Trade opening price impact calculations
|
|
3
|
+
* @dev Mirrors contract's TradingCommonUtils.getTradeOpeningPriceImpact
|
|
4
|
+
*/
|
|
5
|
+
import { TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult } from "./types";
|
|
6
|
+
export type { TradeOpeningPriceImpactInput, TradeOpeningPriceImpactContext, TradeOpeningPriceImpactResult, };
|
|
7
|
+
export { buildTradeOpeningPriceImpactContext } from "./builder";
|
|
8
|
+
/**
|
|
9
|
+
* @dev Calculates all price impacts for trade opening
|
|
10
|
+
* @dev Mirrors contract's getTradeOpeningPriceImpact function
|
|
11
|
+
* @param input Trade parameters
|
|
12
|
+
* @param context Combined context for calculations
|
|
13
|
+
* @returns Price impact breakdown and final price
|
|
14
|
+
*/
|
|
15
|
+
export declare const getTradeOpeningPriceImpact: (input: TradeOpeningPriceImpactInput, context: TradeOpeningPriceImpactContext) => TradeOpeningPriceImpactResult;
|
|
16
|
+
/**
|
|
17
|
+
* @dev Simplified version using current market price
|
|
18
|
+
* @param input Trade parameters
|
|
19
|
+
* @param context Combined context
|
|
20
|
+
* @param currentMarketPrice Current market price to use as open price
|
|
21
|
+
* @returns Price impact breakdown and final price
|
|
22
|
+
*/
|
|
23
|
+
export declare const getTradeOpeningPriceImpactAtMarket: (input: Omit<TradeOpeningPriceImpactInput, "openPrice">, context: TradeOpeningPriceImpactContext, currentMarketPrice: number) => TradeOpeningPriceImpactResult;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Trade opening price impact calculations
|
|
4
|
+
* @dev Mirrors contract's TradingCommonUtils.getTradeOpeningPriceImpact
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getTradeOpeningPriceImpactAtMarket = exports.getTradeOpeningPriceImpact = exports.buildTradeOpeningPriceImpactContext = void 0;
|
|
8
|
+
const cumulVol_1 = require("../cumulVol");
|
|
9
|
+
const skew_1 = require("../skew");
|
|
10
|
+
const __1 = require("../");
|
|
11
|
+
// Export builder
|
|
12
|
+
var builder_1 = require("./builder");
|
|
13
|
+
Object.defineProperty(exports, "buildTradeOpeningPriceImpactContext", { enumerable: true, get: function () { return builder_1.buildTradeOpeningPriceImpactContext; } });
|
|
14
|
+
/**
|
|
15
|
+
* @dev Calculates all price impacts for trade opening
|
|
16
|
+
* @dev Mirrors contract's getTradeOpeningPriceImpact function
|
|
17
|
+
* @param input Trade parameters
|
|
18
|
+
* @param context Combined context for calculations
|
|
19
|
+
* @returns Price impact breakdown and final price
|
|
20
|
+
*/
|
|
21
|
+
const getTradeOpeningPriceImpact = (input, context) => {
|
|
22
|
+
const positionSizeCollateral = input.collateralAmount * input.leverage;
|
|
23
|
+
// Calculate fixed spread
|
|
24
|
+
const spreadP = (0, cumulVol_1.getFixedSpreadP)(input.pairSpreadP, input.long, true // opening
|
|
25
|
+
);
|
|
26
|
+
// Calculate position size in USD
|
|
27
|
+
const positionSizeUsd = positionSizeCollateral * context.collateralPriceUsd;
|
|
28
|
+
// Calculate cumulative volume price impact
|
|
29
|
+
const cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)("", // trader - not needed for calculation
|
|
30
|
+
input.pairIndex, input.long, positionSizeUsd, false, // isPnlPositive - not relevant for opening
|
|
31
|
+
true, // open
|
|
32
|
+
0, // lastPosIncreaseBlock - not relevant for opening
|
|
33
|
+
context.cumulVolContext);
|
|
34
|
+
// Calculate price after spread and cumulative volume impact (before skew)
|
|
35
|
+
const priceAfterSpreadAndCumulVolPriceImpact = (0, __1.getPriceAfterImpact)(input.openPrice, spreadP + cumulVolPriceImpactP);
|
|
36
|
+
// Calculate position size in tokens using the price after fixed spread and cumul vol impact
|
|
37
|
+
const positionSizeToken = positionSizeCollateral / priceAfterSpreadAndCumulVolPriceImpact;
|
|
38
|
+
// Calculate skew price impact (v10+ only)
|
|
39
|
+
const skewPriceImpactObject = (0, skew_1.getTradeSkewPriceImpact)({
|
|
40
|
+
collateralIndex: input.collateralIndex,
|
|
41
|
+
pairIndex: input.pairIndex,
|
|
42
|
+
long: input.long,
|
|
43
|
+
open: true,
|
|
44
|
+
positionSizeToken,
|
|
45
|
+
}, context.skewContext);
|
|
46
|
+
const skewPriceImpactP = skewPriceImpactObject.totalPriceImpactP;
|
|
47
|
+
// Total price impact (signed - can be positive or negative)
|
|
48
|
+
const totalPriceImpactP = spreadP + cumulVolPriceImpactP + skewPriceImpactP;
|
|
49
|
+
const totalPriceImpactPFromMarketPrice = spreadP + cumulVolPriceImpactP + skewPriceImpactObject.tradePriceImpactP;
|
|
50
|
+
// Calculate final price after impact using the same formula as Solidity
|
|
51
|
+
const priceAfterImpact = (0, __1.getPriceAfterImpact)(input.openPrice, totalPriceImpactP);
|
|
52
|
+
// Calculate percent profit from impact
|
|
53
|
+
// For longs: negative impact = profit (price goes down, good for buyer)
|
|
54
|
+
// For shorts: positive impact = profit (price goes up, good for seller)
|
|
55
|
+
const percentProfitP = -totalPriceImpactP;
|
|
56
|
+
return {
|
|
57
|
+
priceAfterImpact,
|
|
58
|
+
percentProfitP,
|
|
59
|
+
fixedSpreadP: spreadP,
|
|
60
|
+
cumulVolPriceImpactP,
|
|
61
|
+
baseSkewPriceImpactP: skewPriceImpactObject.basePriceImpactP,
|
|
62
|
+
tradeSkewPriceImpactP: skewPriceImpactObject.tradePriceImpactP,
|
|
63
|
+
totalSkewPriceImpactP: skewPriceImpactObject.totalPriceImpactP,
|
|
64
|
+
totalPriceImpactP,
|
|
65
|
+
totalPriceImpactPFromMarketPrice,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
exports.getTradeOpeningPriceImpact = getTradeOpeningPriceImpact;
|
|
69
|
+
/**
|
|
70
|
+
* @dev Simplified version using current market price
|
|
71
|
+
* @param input Trade parameters
|
|
72
|
+
* @param context Combined context
|
|
73
|
+
* @param currentMarketPrice Current market price to use as open price
|
|
74
|
+
* @returns Price impact breakdown and final price
|
|
75
|
+
*/
|
|
76
|
+
const getTradeOpeningPriceImpactAtMarket = (input, context, currentMarketPrice) => {
|
|
77
|
+
return (0, exports.getTradeOpeningPriceImpact)(Object.assign(Object.assign({}, input), { openPrice: currentMarketPrice }), context);
|
|
78
|
+
};
|
|
79
|
+
exports.getTradeOpeningPriceImpactAtMarket = getTradeOpeningPriceImpactAtMarket;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Types for trade opening price impact calculations
|
|
3
|
+
*/
|
|
4
|
+
import { CumulVolContext } from "../cumulVol";
|
|
5
|
+
import { SkewPriceImpactContext } from "../skew/types";
|
|
6
|
+
import { Fee, PairIndex } from "../../types";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Input parameters for trade opening price impact
|
|
9
|
+
*/
|
|
10
|
+
export type TradeOpeningPriceImpactInput = {
|
|
11
|
+
collateralIndex: number;
|
|
12
|
+
pairIndex: PairIndex;
|
|
13
|
+
long: boolean;
|
|
14
|
+
collateralAmount: number;
|
|
15
|
+
leverage: number;
|
|
16
|
+
openPrice: number;
|
|
17
|
+
pairSpreadP: number;
|
|
18
|
+
fee: Fee;
|
|
19
|
+
contractsVersion: number;
|
|
20
|
+
isCounterTrade?: boolean;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* @dev Context for trade opening price impact calculation
|
|
24
|
+
* Combines contexts from spread, cumul vol, and skew
|
|
25
|
+
*/
|
|
26
|
+
export type TradeOpeningPriceImpactContext = {
|
|
27
|
+
collateralPriceUsd: number;
|
|
28
|
+
cumulVolContext: CumulVolContext;
|
|
29
|
+
skewContext: SkewPriceImpactContext;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* @dev Result of trade opening price impact calculation
|
|
33
|
+
* Mirrors contract's PriceImpact struct
|
|
34
|
+
*/
|
|
35
|
+
export type TradeOpeningPriceImpactResult = {
|
|
36
|
+
priceAfterImpact: number;
|
|
37
|
+
percentProfitP: number;
|
|
38
|
+
fixedSpreadP: number;
|
|
39
|
+
cumulVolPriceImpactP: number;
|
|
40
|
+
baseSkewPriceImpactP: number;
|
|
41
|
+
tradeSkewPriceImpactP: number;
|
|
42
|
+
totalSkewPriceImpactP: number;
|
|
43
|
+
totalPriceImpactP: number;
|
|
44
|
+
totalPriceImpactPFromMarketPrice: number;
|
|
45
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Builders for skew price impact contexts
|
|
3
|
+
*/
|
|
4
|
+
import { TradingVariablesCollateral } from "../../../backend/tradingVariables/types";
|
|
5
|
+
import { SkewPriceImpactContext } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Builds skew price impact context from trading variables for a specific pair
|
|
8
|
+
* @param tradingVariables Trading variables containing collateral data
|
|
9
|
+
* @param pairIndex Index of the pair to build context for
|
|
10
|
+
* @returns Skew price impact context for the pair
|
|
11
|
+
*/
|
|
12
|
+
export declare const buildSkewPriceImpactContext: (tradingVariables: TradingVariablesCollateral, pairIndex: number) => SkewPriceImpactContext;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Builders for skew price impact contexts
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.buildSkewPriceImpactContext = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Builds skew price impact context from trading variables for a specific pair
|
|
9
|
+
* @param tradingVariables Trading variables containing collateral data
|
|
10
|
+
* @param pairIndex Index of the pair to build context for
|
|
11
|
+
* @returns Skew price impact context for the pair
|
|
12
|
+
*/
|
|
13
|
+
const buildSkewPriceImpactContext = (tradingVariables, pairIndex) => {
|
|
14
|
+
var _a, _b, _c;
|
|
15
|
+
const skewDepth = (_b = (_a = tradingVariables.pairSkewDepths) === null || _a === void 0 ? void 0 : _a[pairIndex]) !== null && _b !== void 0 ? _b : 0;
|
|
16
|
+
const pairOi = (_c = tradingVariables.pairOis) === null || _c === void 0 ? void 0 : _c[pairIndex];
|
|
17
|
+
if (!pairOi) {
|
|
18
|
+
throw new Error(`Pair OI data not found for pair index ${pairIndex}`);
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
skewDepth,
|
|
22
|
+
pairOiToken: {
|
|
23
|
+
oiLongToken: pairOi.token.long,
|
|
24
|
+
oiShortToken: pairOi.token.short,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
exports.buildSkewPriceImpactContext = buildSkewPriceImpactContext;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Converters for skew 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 { PairOiToken, PairOiCollateral } from "./types";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Converts contract pair OI token data to SDK format
|
|
9
|
+
* @param contractData Contract pair OI token struct
|
|
10
|
+
* @returns Normalized pair OI token data
|
|
11
|
+
*/
|
|
12
|
+
export declare const convertPairOiToken: (contractData: IPriceImpact.PairOiTokenStruct) => PairOiToken;
|
|
13
|
+
/**
|
|
14
|
+
* @dev Converts array of contract pair OI token data to SDK format
|
|
15
|
+
* @param contractDataArray Array of contract pair OI token data
|
|
16
|
+
* @returns Array of normalized pair OI token data
|
|
17
|
+
*/
|
|
18
|
+
export declare const convertPairOiTokenArray: (contractDataArray: IPriceImpact.PairOiTokenStruct[]) => PairOiToken[];
|
|
19
|
+
/**
|
|
20
|
+
* @dev Converts contract pair OI collateral data to SDK format
|
|
21
|
+
* @param contractData Contract pair OI collateral struct
|
|
22
|
+
* @param collateralDecimals Number of decimals for the collateral (e.g., 18 for DAI, 6 for USDC)
|
|
23
|
+
* @returns Normalized pair OI collateral data
|
|
24
|
+
*/
|
|
25
|
+
export declare const convertPairOiCollateral: (contractData: IPriceImpact.PairOiCollateralStruct, collateralDecimals: number) => PairOiCollateral;
|
|
26
|
+
/**
|
|
27
|
+
* @dev Converts array of contract pair OI collateral data to SDK format
|
|
28
|
+
* @param contractDataArray Array of contract pair OI collateral data
|
|
29
|
+
* @param collateralDecimals Array of collateral decimals for each entry
|
|
30
|
+
* @returns Array of normalized pair OI collateral data
|
|
31
|
+
*/
|
|
32
|
+
export declare const convertPairOiCollateralArray: (contractDataArray: IPriceImpact.PairOiCollateralStruct[], collateralDecimals: number[]) => PairOiCollateral[];
|
|
33
|
+
/**
|
|
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
|
|
37
|
+
*/
|
|
38
|
+
export declare const convertSkewDepth: (depth: string) => number;
|
|
39
|
+
/**
|
|
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
|
|
43
|
+
*/
|
|
44
|
+
export declare const convertPairSkewDepths: (depths: string[]) => {
|
|
45
|
+
[pairIndex: number]: number;
|
|
46
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Converters for skew price impact data between contract and SDK formats
|
|
4
|
+
* @dev All BigNumber values are normalized to floats with appropriate precision
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.convertPairSkewDepths = exports.convertSkewDepth = exports.convertPairOiCollateralArray = exports.convertPairOiCollateral = exports.convertPairOiTokenArray = exports.convertPairOiToken = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* @dev Converts contract pair OI token data to SDK format
|
|
10
|
+
* @param contractData Contract pair OI token struct
|
|
11
|
+
* @returns Normalized pair OI token data
|
|
12
|
+
*/
|
|
13
|
+
const convertPairOiToken = (contractData) => {
|
|
14
|
+
// Token amounts are stored as 1e18 in contract
|
|
15
|
+
return {
|
|
16
|
+
oiLongToken: Number(contractData.oiLongToken) / 1e18,
|
|
17
|
+
oiShortToken: Number(contractData.oiShortToken) / 1e18,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.convertPairOiToken = convertPairOiToken;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts array of contract pair OI token data to SDK format
|
|
23
|
+
* @param contractDataArray Array of contract pair OI token data
|
|
24
|
+
* @returns Array of normalized pair OI token data
|
|
25
|
+
*/
|
|
26
|
+
const convertPairOiTokenArray = (contractDataArray) => {
|
|
27
|
+
return contractDataArray.map(exports.convertPairOiToken);
|
|
28
|
+
};
|
|
29
|
+
exports.convertPairOiTokenArray = convertPairOiTokenArray;
|
|
30
|
+
/**
|
|
31
|
+
* @dev Converts contract pair OI collateral data to SDK format
|
|
32
|
+
* @param contractData Contract pair OI collateral struct
|
|
33
|
+
* @param collateralDecimals Number of decimals for the collateral (e.g., 18 for DAI, 6 for USDC)
|
|
34
|
+
* @returns Normalized pair OI collateral data
|
|
35
|
+
*/
|
|
36
|
+
const convertPairOiCollateral = (contractData, collateralDecimals) => {
|
|
37
|
+
const divisor = Math.pow(10, collateralDecimals);
|
|
38
|
+
return {
|
|
39
|
+
oiLongCollateral: Number(contractData.oiLongCollateral) / divisor,
|
|
40
|
+
oiShortCollateral: Number(contractData.oiShortCollateral) / divisor,
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
exports.convertPairOiCollateral = convertPairOiCollateral;
|
|
44
|
+
/**
|
|
45
|
+
* @dev Converts array of contract pair OI collateral data to SDK format
|
|
46
|
+
* @param contractDataArray Array of contract pair OI collateral data
|
|
47
|
+
* @param collateralDecimals Array of collateral decimals for each entry
|
|
48
|
+
* @returns Array of normalized pair OI collateral data
|
|
49
|
+
*/
|
|
50
|
+
const convertPairOiCollateralArray = (contractDataArray, collateralDecimals) => {
|
|
51
|
+
if (contractDataArray.length !== collateralDecimals.length) {
|
|
52
|
+
throw new Error("Contract data array and collateral decimals array must have the same length");
|
|
53
|
+
}
|
|
54
|
+
return contractDataArray.map((data, index) => (0, exports.convertPairOiCollateral)(data, collateralDecimals[index]));
|
|
55
|
+
};
|
|
56
|
+
exports.convertPairOiCollateralArray = convertPairOiCollateralArray;
|
|
57
|
+
/**
|
|
58
|
+
* @dev Converts skew depth from contract format to SDK format
|
|
59
|
+
* @param depth Skew depth from contract (in token units with 1e18 precision)
|
|
60
|
+
* @returns Normalized skew depth in tokens
|
|
61
|
+
*/
|
|
62
|
+
const convertSkewDepth = (depth) => {
|
|
63
|
+
// Token depths are always stored with 1e18 precision in the contract
|
|
64
|
+
return Number(depth) / 1e18;
|
|
65
|
+
};
|
|
66
|
+
exports.convertSkewDepth = convertSkewDepth;
|
|
67
|
+
/**
|
|
68
|
+
* @dev Converts array of skew depths from contract format to SDK format
|
|
69
|
+
* @param depths Array of skew depths from contract (in token units with 1e18 precision)
|
|
70
|
+
* @returns Object mapping pair index to normalized depth
|
|
71
|
+
*/
|
|
72
|
+
const convertPairSkewDepths = (depths) => {
|
|
73
|
+
const result = {};
|
|
74
|
+
depths.forEach((depth, index) => {
|
|
75
|
+
if (depth && depth !== "0") {
|
|
76
|
+
result[index] = (0, exports.convertSkewDepth)(depth);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return result;
|
|
80
|
+
};
|
|
81
|
+
exports.convertPairSkewDepths = convertPairSkewDepths;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { GNSMultiCollatDiamond } from "../../../contracts/types/generated";
|
|
2
|
+
import type { PairOiToken, SkewPriceImpactContext } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Fetches pair open interest in tokens for a specific pair
|
|
5
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
6
|
+
* @param collateralIndex Collateral index
|
|
7
|
+
* @param pairIndex Pair index
|
|
8
|
+
* @returns Promise resolving to pair OI in tokens
|
|
9
|
+
*/
|
|
10
|
+
export declare const fetchPairOiAfterV10Token: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number) => Promise<PairOiToken>;
|
|
11
|
+
/**
|
|
12
|
+
* @dev Fetches pair open interest in tokens for multiple pairs
|
|
13
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
14
|
+
* @param collateralIndices Array of collateral indices
|
|
15
|
+
* @param pairIndices Array of pair indices
|
|
16
|
+
* @returns Promise resolving to array of pair OI in tokens
|
|
17
|
+
*/
|
|
18
|
+
export declare const fetchPairOisAfterV10Token: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<PairOiToken[]>;
|
|
19
|
+
/**
|
|
20
|
+
* @dev Fetches skew depth for a specific pair
|
|
21
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
22
|
+
* @param collateralIndex Collateral index
|
|
23
|
+
* @param pairIndex Pair index
|
|
24
|
+
* @returns Promise resolving to normalized skew depth
|
|
25
|
+
*/
|
|
26
|
+
export declare const fetchPairSkewDepth: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number) => Promise<number>;
|
|
27
|
+
/**
|
|
28
|
+
* @dev Fetches skew depths for multiple pairs
|
|
29
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
30
|
+
* @param collateralIndices Array of collateral indices
|
|
31
|
+
* @param pairIndices Array of pair indices
|
|
32
|
+
* @returns Promise resolving to array of normalized skew depths
|
|
33
|
+
*/
|
|
34
|
+
export declare const fetchPairSkewDepths: (contract: GNSMultiCollatDiamond, collateralIndices: number[], pairIndices: number[]) => Promise<number[]>;
|
|
35
|
+
/**
|
|
36
|
+
* @dev Fetches skew price impact context for a single pair
|
|
37
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
38
|
+
* @param collateralIndex Collateral index
|
|
39
|
+
* @param pairIndex Pair index
|
|
40
|
+
* @returns Promise resolving to skew price impact context
|
|
41
|
+
*/
|
|
42
|
+
export declare const fetchSkewPriceImpactContext: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number) => Promise<SkewPriceImpactContext>;
|
|
43
|
+
/**
|
|
44
|
+
* @dev Fetches collateral decimals for given collateral indices
|
|
45
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
46
|
+
* @param collateralIndices Array of collateral indices
|
|
47
|
+
* @returns Promise resolving to array of decimals
|
|
48
|
+
*/
|
|
49
|
+
export declare const fetchCollateralDecimals: (contract: GNSMultiCollatDiamond, collateralIndices: number[]) => Promise<number[]>;
|
|
50
|
+
/**
|
|
51
|
+
* @dev Calculates skew price impact for a trade using contract call
|
|
52
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
53
|
+
* @param collateralIndex Collateral index
|
|
54
|
+
* @param pairIndex Pair index
|
|
55
|
+
* @param long Whether trade is long
|
|
56
|
+
* @param positionSizeToken Position size in tokens
|
|
57
|
+
* @param open Whether trade is opening
|
|
58
|
+
* @returns Promise resolving to price impact percentage (1e10)
|
|
59
|
+
*/
|
|
60
|
+
export declare const calculateTradeSkewPriceImpact: (contract: GNSMultiCollatDiamond, collateralIndex: number, pairIndex: number, long: boolean, positionSizeToken: number, open: boolean) => Promise<number>;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.calculateTradeSkewPriceImpact = exports.fetchCollateralDecimals = exports.fetchSkewPriceImpactContext = exports.fetchPairSkewDepths = exports.fetchPairSkewDepth = exports.fetchPairOisAfterV10Token = exports.fetchPairOiAfterV10Token = void 0;
|
|
13
|
+
const converter_1 = require("./converter");
|
|
14
|
+
/**
|
|
15
|
+
* @dev Fetches pair open interest in tokens for a specific pair
|
|
16
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
17
|
+
* @param collateralIndex Collateral index
|
|
18
|
+
* @param pairIndex Pair index
|
|
19
|
+
* @returns Promise resolving to pair OI in tokens
|
|
20
|
+
*/
|
|
21
|
+
const fetchPairOiAfterV10Token = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
try {
|
|
23
|
+
const contractData = yield contract.getPairOiAfterV10Token(collateralIndex, pairIndex);
|
|
24
|
+
return (0, converter_1.convertPairOiToken)(contractData);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
console.error("Error fetching pair OI token:", error);
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
exports.fetchPairOiAfterV10Token = fetchPairOiAfterV10Token;
|
|
32
|
+
/**
|
|
33
|
+
* @dev Fetches pair open interest in tokens for multiple pairs
|
|
34
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
35
|
+
* @param collateralIndices Array of collateral indices
|
|
36
|
+
* @param pairIndices Array of pair indices
|
|
37
|
+
* @returns Promise resolving to array of pair OI in tokens
|
|
38
|
+
*/
|
|
39
|
+
const fetchPairOisAfterV10Token = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
+
if (collateralIndices.length !== pairIndices.length) {
|
|
41
|
+
throw new Error("Collateral indices and pair indices arrays must have the same length");
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
const contractDataArray = yield contract.getPairOisAfterV10Token(collateralIndices, pairIndices);
|
|
45
|
+
return contractDataArray.map(converter_1.convertPairOiToken);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.error("Error fetching pair OIs token:", error);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
exports.fetchPairOisAfterV10Token = fetchPairOisAfterV10Token;
|
|
53
|
+
/**
|
|
54
|
+
* @dev Fetches skew depth for a specific pair
|
|
55
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
56
|
+
* @param collateralIndex Collateral index
|
|
57
|
+
* @param pairIndex Pair index
|
|
58
|
+
* @returns Promise resolving to normalized skew depth
|
|
59
|
+
*/
|
|
60
|
+
const fetchPairSkewDepth = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
+
try {
|
|
62
|
+
const contractDepth = yield contract.getPairSkewDepth(collateralIndex, pairIndex);
|
|
63
|
+
// Token depths are always 1e18 precision
|
|
64
|
+
return (0, converter_1.convertSkewDepth)(contractDepth.toString());
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
console.error("Error fetching skew depth:", error);
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
exports.fetchPairSkewDepth = fetchPairSkewDepth;
|
|
72
|
+
/**
|
|
73
|
+
* @dev Fetches skew depths for multiple pairs
|
|
74
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
75
|
+
* @param collateralIndices Array of collateral indices
|
|
76
|
+
* @param pairIndices Array of pair indices
|
|
77
|
+
* @returns Promise resolving to array of normalized skew depths
|
|
78
|
+
*/
|
|
79
|
+
const fetchPairSkewDepths = (contract, collateralIndices, pairIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
80
|
+
if (collateralIndices.length !== pairIndices.length) {
|
|
81
|
+
throw new Error("All input arrays must have the same length");
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
const contractDepths = yield contract.getPairSkewDepths(collateralIndices, pairIndices);
|
|
85
|
+
// Token depths are always 1e18 precision
|
|
86
|
+
return contractDepths.map(depth => (0, converter_1.convertSkewDepth)(depth.toString()));
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
console.error("Error fetching skew depths:", error);
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
exports.fetchPairSkewDepths = fetchPairSkewDepths;
|
|
94
|
+
/**
|
|
95
|
+
* @dev Fetches skew price impact context for a single pair
|
|
96
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
97
|
+
* @param collateralIndex Collateral index
|
|
98
|
+
* @param pairIndex Pair index
|
|
99
|
+
* @returns Promise resolving to skew price impact context
|
|
100
|
+
*/
|
|
101
|
+
const fetchSkewPriceImpactContext = (contract, collateralIndex, pairIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
102
|
+
try {
|
|
103
|
+
// Fetch OI data and skew depth in parallel
|
|
104
|
+
const [pairOiToken, skewDepth] = yield Promise.all([
|
|
105
|
+
(0, exports.fetchPairOiAfterV10Token)(contract, collateralIndex, pairIndex),
|
|
106
|
+
(0, exports.fetchPairSkewDepth)(contract, collateralIndex, pairIndex),
|
|
107
|
+
]);
|
|
108
|
+
return {
|
|
109
|
+
skewDepth,
|
|
110
|
+
pairOiToken,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
console.error("Error fetching skew price impact context:", error);
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
exports.fetchSkewPriceImpactContext = fetchSkewPriceImpactContext;
|
|
119
|
+
/**
|
|
120
|
+
* @dev Fetches collateral decimals for given collateral indices
|
|
121
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
122
|
+
* @param collateralIndices Array of collateral indices
|
|
123
|
+
* @returns Promise resolving to array of decimals
|
|
124
|
+
*/
|
|
125
|
+
const fetchCollateralDecimals = (contract, collateralIndices) => __awaiter(void 0, void 0, void 0, function* () {
|
|
126
|
+
try {
|
|
127
|
+
// Get unique collateral indices to minimize calls
|
|
128
|
+
const uniqueIndices = [...new Set(collateralIndices)];
|
|
129
|
+
// Fetch collateral info for unique indices
|
|
130
|
+
const promises = uniqueIndices.map((index) => __awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
const collateral = yield contract.getCollateral(index);
|
|
132
|
+
return { index, decimals: Number(collateral.precision) };
|
|
133
|
+
}));
|
|
134
|
+
const collateralData = yield Promise.all(promises);
|
|
135
|
+
// Create a map for quick lookup
|
|
136
|
+
const decimalsMap = new Map(collateralData.map(data => [data.index, data.decimals]));
|
|
137
|
+
// Return decimals in the same order as input
|
|
138
|
+
return collateralIndices.map(index => decimalsMap.get(index) || 18 // Default to 18 if not found
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
console.error("Error fetching collateral decimals:", error);
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
exports.fetchCollateralDecimals = fetchCollateralDecimals;
|
|
147
|
+
/**
|
|
148
|
+
* @dev Calculates skew price impact for a trade using contract call
|
|
149
|
+
* @param contract GNSMultiCollatDiamond contract instance
|
|
150
|
+
* @param collateralIndex Collateral index
|
|
151
|
+
* @param pairIndex Pair index
|
|
152
|
+
* @param long Whether trade is long
|
|
153
|
+
* @param positionSizeToken Position size in tokens
|
|
154
|
+
* @param open Whether trade is opening
|
|
155
|
+
* @returns Promise resolving to price impact percentage (1e10)
|
|
156
|
+
*/
|
|
157
|
+
const calculateTradeSkewPriceImpact = (contract, collateralIndex, pairIndex, long, positionSizeToken, open) => __awaiter(void 0, void 0, void 0, function* () {
|
|
158
|
+
try {
|
|
159
|
+
const priceImpactP = yield contract.getTradeSkewPriceImpactP(collateralIndex, pairIndex, long, BigInt(Math.round(positionSizeToken * 1e18)), // Convert to 1e18 precision
|
|
160
|
+
open);
|
|
161
|
+
// Convert from int256 1e10 to percentage
|
|
162
|
+
return Number(priceImpactP) / 1e10;
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
console.error("Error calculating trade skew price impact:", error);
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
exports.calculateTradeSkewPriceImpact = calculateTradeSkewPriceImpact;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Skew price impact calculations for v10+ trades
|
|
3
|
+
* @dev Based on formula: (existingSkew + tradeSize/2) / skewDepth
|
|
4
|
+
*/
|
|
5
|
+
import { PairOiToken, SkewPriceImpactInput, SkewPriceImpactResult, SkewPriceImpactContext } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Calculates net skew in tokens (long - short)
|
|
8
|
+
* @param pairOi Pair OI data with long and short token amounts
|
|
9
|
+
* @returns Net skew in tokens (positive = long heavy, negative = short heavy)
|
|
10
|
+
*/
|
|
11
|
+
export declare const getNetSkewToken: (pairOi: PairOiToken) => number;
|
|
12
|
+
/**
|
|
13
|
+
* @dev Calculates net skew in collateral
|
|
14
|
+
* @param netSkewToken Net skew in tokens
|
|
15
|
+
* @param currentPrice Current pair price
|
|
16
|
+
* @returns Net skew in collateral
|
|
17
|
+
*/
|
|
18
|
+
export declare const getNetSkewCollateral: (netSkewToken: number, currentPrice: number) => number;
|
|
19
|
+
/**
|
|
20
|
+
* @dev Determines trade direction impact on skew
|
|
21
|
+
* @param long Is long position
|
|
22
|
+
* @param open Is opening (true) or closing (false)
|
|
23
|
+
* @returns Whether trade increases or decreases skew
|
|
24
|
+
*/
|
|
25
|
+
export declare const getTradeSkewDirection: (long: boolean, open: boolean) => boolean;
|
|
26
|
+
/**
|
|
27
|
+
* @dev Core skew price impact calculation
|
|
28
|
+
* @param existingSkewToken Current net skew in tokens (signed)
|
|
29
|
+
* @param tradeSizeToken Trade size in tokens (always positive)
|
|
30
|
+
* @param skewDepth Skew depth in tokens
|
|
31
|
+
* @param tradePositiveSkew Whether trade increases skew in its direction
|
|
32
|
+
* @returns Price impact percentage (can be positive or negative)
|
|
33
|
+
*/
|
|
34
|
+
export declare const calculateSkewPriceImpactP: (existingSkewToken: number, tradeSizeToken: number, skewDepth: number, tradePositiveSkew: boolean) => number;
|
|
35
|
+
/**
|
|
36
|
+
* @dev Main function to calculate skew price impact for a trade
|
|
37
|
+
* @param input Trade parameters
|
|
38
|
+
* @param context Skew price impact context with depths and OI data
|
|
39
|
+
* @returns Skew price impact result
|
|
40
|
+
*/
|
|
41
|
+
export declare const getTradeSkewPriceImpact: (input: SkewPriceImpactInput, context: SkewPriceImpactContext) => SkewPriceImpactResult;
|
|
42
|
+
/**
|
|
43
|
+
* @dev Calculate position sizes for partial operations
|
|
44
|
+
* @param originalSizeCollateral Original position size in collateral
|
|
45
|
+
* @param deltaCollateral Position size delta in collateral
|
|
46
|
+
* @param originalSizeToken Original position size in tokens
|
|
47
|
+
* @returns Delta in tokens proportional to collateral delta
|
|
48
|
+
*/
|
|
49
|
+
export declare const calculatePartialSizeToken: (originalSizeCollateral: number, deltaCollateral: number, originalSizeToken: number) => number;
|
|
50
|
+
export * as SkewPriceImpact from "./types";
|
|
51
|
+
export * from "./converter";
|
|
52
|
+
export * from "./builder";
|
|
53
|
+
export * from "./fetcher";
|