@gainsnetwork/sdk 0.0.0-v10.rc1 → 0.0.0-v10.rc3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/backend/globalTrades/index.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +2 -0
- package/lib/backend/index.js +18 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +312 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +30 -0
- package/lib/backend/tradingVariables/converter.js +329 -0
- package/lib/backend/tradingVariables/index.d.ts +3 -0
- package/lib/backend/tradingVariables/index.js +78 -0
- package/lib/backend/tradingVariables/types.d.ts +109 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.d.ts +8 -1
- package/lib/constants.js +8 -1
- package/lib/contracts/fetch/fees/borrowingFeesV2.d.ts +75 -0
- package/lib/contracts/fetch/fees/borrowingFeesV2.js +193 -0
- package/lib/contracts/fetch/fees/fundingFees.d.ts +66 -0
- package/lib/contracts/fetch/fees/fundingFees.js +150 -0
- package/lib/contracts/fetch/priceImpact/skew.d.ts +63 -0
- package/lib/contracts/fetch/priceImpact/skew.js +168 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/openTrades.d.ts +1 -0
- package/lib/contracts/utils/openTrades.js +94 -56
- package/lib/contracts/utils/pairs.js +7 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/markets/collateral/converter.d.ts +5 -0
- package/lib/markets/collateral/converter.js +11 -0
- package/lib/markets/collateral/index.d.ts +1 -0
- package/lib/markets/collateral/index.js +17 -0
- package/lib/markets/collateral/types.d.ts +7 -0
- package/lib/markets/collateral/types.js +2 -0
- package/lib/markets/index.d.ts +2 -0
- package/lib/markets/index.js +2 -0
- package/lib/markets/oi/converter.d.ts +63 -0
- package/lib/markets/oi/converter.js +103 -0
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/index.d.ts +10 -0
- package/lib/markets/oi/index.js +37 -0
- package/lib/markets/oi/types.d.ts +82 -0
- package/lib/markets/oi/types.js +6 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/trade/fees/borrowing/index.d.ts +22 -1
- package/lib/trade/fees/borrowing/index.js +39 -13
- package/lib/trade/fees/borrowingV2/converter.d.ts +66 -0
- package/lib/trade/fees/borrowingV2/converter.js +121 -0
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/fetcher.js +193 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +60 -0
- package/lib/trade/fees/borrowingV2/index.js +140 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +79 -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 +110 -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 +136 -0
- package/lib/trade/fees/fundingFees/index.js +326 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +7 -2
- package/lib/trade/fees/index.js +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/converter.d.ts +30 -0
- package/lib/trade/fees/trading/converter.js +43 -0
- package/lib/trade/fees/trading/index.d.ts +54 -0
- package/lib/trade/fees/trading/index.js +147 -0
- package/lib/trade/fees/trading/types.d.ts +48 -0
- package/lib/trade/fees/trading/types.js +5 -0
- package/lib/trade/index.d.ts +3 -2
- package/lib/trade/index.js +3 -2
- package/lib/trade/liquidation/converter.d.ts +23 -0
- package/lib/trade/liquidation/converter.js +46 -0
- package/lib/trade/liquidation/index.d.ts +31 -0
- package/lib/trade/liquidation/index.js +187 -0
- package/lib/trade/liquidation/types.d.ts +44 -0
- package/lib/trade/liquidation/types.js +2 -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 +86 -0
- package/lib/trade/pnl/index.js +201 -0
- package/lib/trade/pnl/types.d.ts +86 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/index.d.ts +21 -0
- package/lib/trade/priceImpact/close/index.js +131 -0
- package/lib/trade/priceImpact/close/types.d.ts +43 -0
- package/lib/trade/priceImpact/close/types.js +5 -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 +107 -0
- package/lib/trade/priceImpact/cumulVol/index.js +228 -0
- package/lib/trade/priceImpact/index.d.ts +12 -0
- package/lib/trade/priceImpact/index.js +59 -0
- package/lib/trade/priceImpact/open/index.d.ts +22 -0
- package/lib/trade/priceImpact/open/index.js +76 -0
- package/lib/trade/priceImpact/open/types.d.ts +41 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +77 -0
- package/lib/trade/priceImpact/skew/converter.js +171 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +63 -0
- package/lib/trade/priceImpact/skew/fetcher.js +168 -0
- package/lib/trade/priceImpact/skew/index.d.ts +58 -0
- package/lib/trade/priceImpact/skew/index.js +179 -0
- package/lib/trade/priceImpact/skew/types.d.ts +55 -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 +34 -9
- package/lib/trade/types.js +7 -0
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/package.json +2 -1
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Types for PnL calculations
|
|
3
|
+
*/
|
|
4
|
+
import { Trade, TradeInfo, TradeFeesData } from "../types";
|
|
5
|
+
/**
|
|
6
|
+
* @dev Input for trade value calculation with all fees
|
|
7
|
+
* @dev Mirrors contract's TradeValueInput struct
|
|
8
|
+
*/
|
|
9
|
+
export type TradeValueInput = {
|
|
10
|
+
trade: Trade;
|
|
11
|
+
currentPrice: number;
|
|
12
|
+
collateralPriceUsd: number;
|
|
13
|
+
fees: {
|
|
14
|
+
openingFeeCollateral: number;
|
|
15
|
+
closingFeeCollateral: number;
|
|
16
|
+
holdingFeesCollateral: number;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* @dev Result of trade value calculation
|
|
21
|
+
*/
|
|
22
|
+
export type TradeValueResult = {
|
|
23
|
+
tradeValue: number;
|
|
24
|
+
pnlPercent: number;
|
|
25
|
+
pnlCollateral: number;
|
|
26
|
+
totalFees: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* @dev Detailed fee breakdown
|
|
30
|
+
*/
|
|
31
|
+
export type FeeBreakdown = {
|
|
32
|
+
borrowingV1: number;
|
|
33
|
+
borrowingV2: number;
|
|
34
|
+
funding: number;
|
|
35
|
+
closing: number;
|
|
36
|
+
opening?: number;
|
|
37
|
+
total: number;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* @dev Price impact breakdown for v10
|
|
41
|
+
*/
|
|
42
|
+
export type PriceImpactBreakdown = {
|
|
43
|
+
fixedSpread: number;
|
|
44
|
+
skewImpact: number;
|
|
45
|
+
cumulVolImpact: number;
|
|
46
|
+
total: number;
|
|
47
|
+
priceAfterImpact: number;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* @dev Comprehensive PnL result with all details
|
|
51
|
+
*/
|
|
52
|
+
export type ComprehensivePnlResult = {
|
|
53
|
+
pnlPercent: number;
|
|
54
|
+
pnlCollateral: number;
|
|
55
|
+
tradeValue: number;
|
|
56
|
+
fees: FeeBreakdown;
|
|
57
|
+
priceImpact?: PriceImpactBreakdown;
|
|
58
|
+
isLiquidated: boolean;
|
|
59
|
+
isProfitable: boolean;
|
|
60
|
+
leveragedPositionSize: number;
|
|
61
|
+
netPnlAfterFees: number;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* @dev Simple PnL result for backward compatibility
|
|
65
|
+
*/
|
|
66
|
+
export type SimplePnlResult = [pnlCollateral: number, pnlPercent: number];
|
|
67
|
+
/**
|
|
68
|
+
* @dev Input for PnL calculation with price impact
|
|
69
|
+
*/
|
|
70
|
+
export type PnlWithPriceImpactInput = {
|
|
71
|
+
trade: Trade;
|
|
72
|
+
tradeInfo: TradeInfo;
|
|
73
|
+
oraclePrice: number;
|
|
74
|
+
currentPairPrice: number;
|
|
75
|
+
usePriceImpact: boolean;
|
|
76
|
+
includeOpeningFees?: boolean;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* @dev Context for v10 PnL calculations
|
|
80
|
+
*/
|
|
81
|
+
export type V10PnlContext = {
|
|
82
|
+
tradeFeesData: TradeFeesData;
|
|
83
|
+
priceImpactContext?: any;
|
|
84
|
+
skewContext?: any;
|
|
85
|
+
cumulVolContext?: any;
|
|
86
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Trade closing price impact calculations
|
|
3
|
+
* @dev Mirrors contract's TradingCommonUtils.getTradeClosingPriceImpact
|
|
4
|
+
*/
|
|
5
|
+
import { TradeClosingPriceImpactInput, TradeClosingPriceImpactContext, TradeClosingPriceImpactResult } from "./types";
|
|
6
|
+
export type { TradeClosingPriceImpactInput, TradeClosingPriceImpactContext, TradeClosingPriceImpactResult, };
|
|
7
|
+
/**
|
|
8
|
+
* @dev Calculates all price impacts for trade closing
|
|
9
|
+
* @dev Mirrors contract's getTradeClosingPriceImpact function
|
|
10
|
+
* @param input Trade parameters
|
|
11
|
+
* @param context Combined context for calculations
|
|
12
|
+
* @returns Price impact breakdown and trade value
|
|
13
|
+
*/
|
|
14
|
+
export declare const getTradeClosingPriceImpact: (input: TradeClosingPriceImpactInput, context: TradeClosingPriceImpactContext) => TradeClosingPriceImpactResult;
|
|
15
|
+
/**
|
|
16
|
+
* @dev Simplified version using oracle price as current price
|
|
17
|
+
* @param input Trade parameters (without currentPairPrice)
|
|
18
|
+
* @param context Combined context
|
|
19
|
+
* @returns Price impact breakdown and trade value
|
|
20
|
+
*/
|
|
21
|
+
export declare const getTradeClosingPriceImpactAtOracle: (input: Omit<TradeClosingPriceImpactInput, "currentPairPrice">, context: TradeClosingPriceImpactContext) => TradeClosingPriceImpactResult;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Trade closing price impact calculations
|
|
4
|
+
* @dev Mirrors contract's TradingCommonUtils.getTradeClosingPriceImpact
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getTradeClosingPriceImpactAtOracle = exports.getTradeClosingPriceImpact = void 0;
|
|
8
|
+
const cumulVol_1 = require("../cumulVol");
|
|
9
|
+
const skew_1 = require("../skew");
|
|
10
|
+
const types_1 = require("../../../contracts/types");
|
|
11
|
+
/**
|
|
12
|
+
* @dev Calculates position size in tokens for partial close
|
|
13
|
+
* @param originalPositionSizeToken Original position size in tokens
|
|
14
|
+
* @param originalCollateral Original collateral amount
|
|
15
|
+
* @param closingCollateral Collateral amount being closed
|
|
16
|
+
* @returns Proportional position size in tokens
|
|
17
|
+
*/
|
|
18
|
+
const calculateClosingPositionSizeToken = (originalPositionSizeToken, originalCollateral, closingCollateral) => {
|
|
19
|
+
if (originalCollateral === 0)
|
|
20
|
+
return 0;
|
|
21
|
+
// Proportional calculation: (closingCollateral / originalCollateral) * originalPositionSizeToken
|
|
22
|
+
return (closingCollateral * originalPositionSizeToken) / originalCollateral;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* @dev Calculates all price impacts for trade closing
|
|
26
|
+
* @dev Mirrors contract's getTradeClosingPriceImpact function
|
|
27
|
+
* @param input Trade parameters
|
|
28
|
+
* @param context Combined context for calculations
|
|
29
|
+
* @returns Price impact breakdown and trade value
|
|
30
|
+
*/
|
|
31
|
+
const getTradeClosingPriceImpact = (input, context) => {
|
|
32
|
+
// For trades before V9.2, return oracle price without any impact
|
|
33
|
+
if (input.contractsVersion === types_1.ContractsVersion.BEFORE_V9_2) {
|
|
34
|
+
return {
|
|
35
|
+
positionSizeToken: 0,
|
|
36
|
+
fixedSpreadP: 0,
|
|
37
|
+
cumulVolPriceImpactP: 0,
|
|
38
|
+
skewPriceImpactP: 0,
|
|
39
|
+
totalPriceImpactP: 0,
|
|
40
|
+
priceAfterImpact: input.oraclePrice,
|
|
41
|
+
tradeValueCollateralNoFactor: 0,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
// Calculate position size in tokens (proportional to collateral being closed)
|
|
45
|
+
const positionSizeToken = input.trade.positionSizeToken
|
|
46
|
+
? calculateClosingPositionSizeToken(input.trade.positionSizeToken, input.trade.collateralAmount, input.positionSizeCollateral)
|
|
47
|
+
: 0;
|
|
48
|
+
// Calculate fixed spread (reversed for closing)
|
|
49
|
+
const fixedSpreadP = (0, cumulVol_1.getFixedSpreadP)(input.pairSpreadP, input.trade.long, false // closing
|
|
50
|
+
);
|
|
51
|
+
let cumulVolPriceImpactP = 0;
|
|
52
|
+
let tradeValueCollateralNoFactor = 0;
|
|
53
|
+
if (input.useCumulativeVolPriceImpact) {
|
|
54
|
+
// First pass: Calculate with negative PnL assumption
|
|
55
|
+
const positionSizeUsd = input.positionSizeCollateral * context.collateralPriceUsd;
|
|
56
|
+
cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)(input.trade.user, input.pairIndex, input.trade.long, positionSizeUsd, false, // Assume negative PnL initially
|
|
57
|
+
false, // closing
|
|
58
|
+
context.tradeInfo.lastPosIncreaseBlock || context.tradeInfo.createdBlock, Object.assign(Object.assign({}, context), { isOpen: false, isPnlPositive: false, contractsVersion: input.contractsVersion, createdBlock: context.tradeInfo.createdBlock }));
|
|
59
|
+
// Calculate price with conservative impact
|
|
60
|
+
const priceWithImpact = input.trade.long
|
|
61
|
+
? input.currentPairPrice *
|
|
62
|
+
(1 - (fixedSpreadP + cumulVolPriceImpactP) / 100)
|
|
63
|
+
: input.currentPairPrice /
|
|
64
|
+
(1 - (fixedSpreadP + cumulVolPriceImpactP) / 100);
|
|
65
|
+
// Calculate trade value in collateral
|
|
66
|
+
// For long: value = positionSizeToken * priceWithImpact
|
|
67
|
+
// For short: value = positionSizeToken / priceWithImpact * openPrice^2 / currentPrice
|
|
68
|
+
if (positionSizeToken > 0) {
|
|
69
|
+
if (input.trade.long) {
|
|
70
|
+
tradeValueCollateralNoFactor = positionSizeToken * priceWithImpact;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
// Short calculation: profit from price decrease
|
|
74
|
+
const pnlFactor = (2 * input.trade.openPrice - priceWithImpact) / input.trade.openPrice;
|
|
75
|
+
tradeValueCollateralNoFactor = input.positionSizeCollateral * pnlFactor;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
tradeValueCollateralNoFactor = input.positionSizeCollateral;
|
|
80
|
+
}
|
|
81
|
+
// Determine actual PnL
|
|
82
|
+
const isPnlPositive = tradeValueCollateralNoFactor > input.trade.collateralAmount;
|
|
83
|
+
// Second pass: Recalculate with actual PnL if positive
|
|
84
|
+
if (isPnlPositive) {
|
|
85
|
+
cumulVolPriceImpactP = (0, cumulVol_1.getTradeCumulVolPriceImpactP)(input.trade.user, input.pairIndex, input.trade.long, positionSizeUsd, true, // Positive PnL
|
|
86
|
+
false, // closing
|
|
87
|
+
context.tradeInfo.lastPosIncreaseBlock ||
|
|
88
|
+
context.tradeInfo.createdBlock, Object.assign(Object.assign({}, context), { isOpen: false, isPnlPositive: true, contractsVersion: input.contractsVersion, createdBlock: context.tradeInfo.createdBlock }));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Calculate skew price impact (v10+ only)
|
|
92
|
+
const skewPriceImpactP = input.contractsVersion === types_1.ContractsVersion.V10
|
|
93
|
+
? (0, skew_1.getTradeSkewPriceImpactWithChecks)({
|
|
94
|
+
collateralIndex: input.collateralIndex,
|
|
95
|
+
pairIndex: input.pairIndex,
|
|
96
|
+
long: input.trade.long,
|
|
97
|
+
open: false,
|
|
98
|
+
positionSizeCollateral: input.positionSizeCollateral,
|
|
99
|
+
currentPrice: input.currentPairPrice,
|
|
100
|
+
contractsVersion: input.contractsVersion,
|
|
101
|
+
isCounterTrade: input.trade.isCounterTrade,
|
|
102
|
+
}, context.skewContext)
|
|
103
|
+
: 0;
|
|
104
|
+
// Total price impact (all components)
|
|
105
|
+
const totalPriceImpactP = fixedSpreadP + cumulVolPriceImpactP + skewPriceImpactP;
|
|
106
|
+
// Calculate final price after all impacts
|
|
107
|
+
// For closing: longs get worse price when impact is positive, shorts get better
|
|
108
|
+
const priceAfterImpact = input.trade.long
|
|
109
|
+
? input.currentPairPrice * (1 - totalPriceImpactP / 100)
|
|
110
|
+
: input.currentPairPrice / (1 - totalPriceImpactP / 100);
|
|
111
|
+
return {
|
|
112
|
+
positionSizeToken,
|
|
113
|
+
fixedSpreadP,
|
|
114
|
+
cumulVolPriceImpactP,
|
|
115
|
+
skewPriceImpactP,
|
|
116
|
+
totalPriceImpactP,
|
|
117
|
+
priceAfterImpact,
|
|
118
|
+
tradeValueCollateralNoFactor,
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
exports.getTradeClosingPriceImpact = getTradeClosingPriceImpact;
|
|
122
|
+
/**
|
|
123
|
+
* @dev Simplified version using oracle price as current price
|
|
124
|
+
* @param input Trade parameters (without currentPairPrice)
|
|
125
|
+
* @param context Combined context
|
|
126
|
+
* @returns Price impact breakdown and trade value
|
|
127
|
+
*/
|
|
128
|
+
const getTradeClosingPriceImpactAtOracle = (input, context) => {
|
|
129
|
+
return (0, exports.getTradeClosingPriceImpact)(Object.assign(Object.assign({}, input), { currentPairPrice: input.oraclePrice }), context);
|
|
130
|
+
};
|
|
131
|
+
exports.getTradeClosingPriceImpactAtOracle = getTradeClosingPriceImpactAtOracle;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Types for trade closing price impact calculations
|
|
3
|
+
*/
|
|
4
|
+
import { CumulVolContext } from "../cumulVol";
|
|
5
|
+
import { SkewPriceImpactContext } from "../skew/types";
|
|
6
|
+
import { Trade, TradeInfo, PairIndex } from "../../types";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Input parameters for trade closing price impact
|
|
9
|
+
* @dev Mirrors contract's TradePriceImpactInput struct
|
|
10
|
+
*/
|
|
11
|
+
export type TradeClosingPriceImpactInput = {
|
|
12
|
+
trade: Trade;
|
|
13
|
+
oraclePrice: number;
|
|
14
|
+
positionSizeCollateral: number;
|
|
15
|
+
currentPairPrice: number;
|
|
16
|
+
useCumulativeVolPriceImpact: boolean;
|
|
17
|
+
collateralIndex: number;
|
|
18
|
+
pairIndex: PairIndex;
|
|
19
|
+
pairSpreadP: number;
|
|
20
|
+
contractsVersion: number;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* @dev Context for trade closing price impact calculation
|
|
24
|
+
* Combines contexts from spread, cumul vol, and skew
|
|
25
|
+
*/
|
|
26
|
+
export type TradeClosingPriceImpactContext = CumulVolContext & {
|
|
27
|
+
skewContext: SkewPriceImpactContext;
|
|
28
|
+
collateralPriceUsd: number;
|
|
29
|
+
tradeInfo: TradeInfo;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* @dev Result of trade closing price impact calculation
|
|
33
|
+
* @dev Mirrors contract's TradePriceImpact struct with additional return value
|
|
34
|
+
*/
|
|
35
|
+
export type TradeClosingPriceImpactResult = {
|
|
36
|
+
positionSizeToken: number;
|
|
37
|
+
fixedSpreadP: number;
|
|
38
|
+
cumulVolPriceImpactP: number;
|
|
39
|
+
skewPriceImpactP: number;
|
|
40
|
+
totalPriceImpactP: number;
|
|
41
|
+
priceAfterImpact: number;
|
|
42
|
+
tradeValueCollateralNoFactor: number;
|
|
43
|
+
};
|
|
@@ -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,107 @@
|
|
|
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";
|