@gainsnetwork/sdk 0.0.0-v10.rc1 → 0.0.0-v10.rc11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/backend/globalTrades/index.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +3 -0
- package/lib/backend/index.js +28 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +318 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +34 -0
- package/lib/backend/tradingVariables/converter.js +338 -0
- package/lib/backend/tradingVariables/index.d.ts +5 -0
- package/lib/backend/tradingVariables/index.js +96 -0
- package/lib/backend/tradingVariables/types.d.ts +113 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.d.ts +18 -1
- package/lib/constants.js +19 -2
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/openTrades.d.ts +1 -0
- package/lib/contracts/utils/openTrades.js +94 -56
- package/lib/contracts/utils/pairs.js +17 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -0
- package/lib/markets/collateral/converter.d.ts +5 -0
- package/lib/markets/collateral/converter.js +11 -0
- package/lib/markets/collateral/index.d.ts +1 -0
- package/lib/markets/collateral/index.js +17 -0
- package/lib/markets/collateral/types.d.ts +7 -0
- package/lib/markets/collateral/types.js +2 -0
- package/lib/markets/index.d.ts +3 -0
- package/lib/markets/index.js +3 -0
- package/lib/markets/oi/converter.d.ts +63 -0
- package/lib/markets/oi/converter.js +103 -0
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/index.d.ts +10 -0
- package/lib/markets/oi/index.js +37 -0
- package/lib/markets/oi/types.d.ts +82 -0
- package/lib/markets/oi/types.js +6 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/markets/price/builder.d.ts +25 -0
- package/lib/markets/price/builder.js +69 -0
- package/lib/markets/price/index.d.ts +6 -0
- package/lib/markets/price/index.js +22 -0
- package/lib/markets/price/marketPrice.d.ts +12 -0
- package/lib/markets/price/marketPrice.js +34 -0
- package/lib/markets/price/types.d.ts +23 -0
- package/lib/markets/price/types.js +5 -0
- package/lib/trade/fees/borrowing/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +33 -0
- package/lib/trade/fees/borrowing/index.d.ts +24 -2
- package/lib/trade/fees/borrowing/index.js +51 -16
- package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
- package/lib/trade/fees/borrowingV2/builder.js +24 -0
- package/lib/trade/fees/borrowingV2/converter.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/converter.js +132 -0
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/fetcher.js +185 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +48 -0
- package/lib/trade/fees/borrowingV2/index.js +112 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +95 -0
- package/lib/trade/fees/borrowingV2/types.js +5 -0
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +114 -0
- package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
- package/lib/trade/fees/fundingFees/builder.js +35 -0
- package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
- package/lib/trade/fees/fundingFees/converter.js +196 -0
- package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
- package/lib/trade/fees/fundingFees/fetcher.js +150 -0
- package/lib/trade/fees/fundingFees/index.d.ts +124 -0
- package/lib/trade/fees/fundingFees/index.js +309 -0
- package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
- package/lib/trade/fees/fundingFees/pairContext.js +17 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +7 -2
- package/lib/trade/fees/index.js +66 -16
- package/lib/trade/fees/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- package/lib/trade/fees/tiers/index.d.ts +18 -0
- package/lib/trade/fees/tiers/index.js +45 -1
- package/lib/trade/fees/trading/builder.d.ts +18 -0
- package/lib/trade/fees/trading/builder.js +20 -0
- package/lib/trade/fees/trading/converter.d.ts +32 -0
- package/lib/trade/fees/trading/converter.js +47 -0
- package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFees.js +66 -0
- package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
- package/lib/trade/fees/trading/index.d.ts +62 -0
- package/lib/trade/fees/trading/index.js +155 -0
- package/lib/trade/fees/trading/types.d.ts +48 -0
- package/lib/trade/fees/trading/types.js +5 -0
- package/lib/trade/index.d.ts +3 -2
- package/lib/trade/index.js +3 -2
- package/lib/trade/liquidation/builder.d.ts +25 -0
- package/lib/trade/liquidation/builder.js +59 -0
- package/lib/trade/liquidation/converter.d.ts +23 -0
- package/lib/trade/liquidation/converter.js +46 -0
- package/lib/trade/liquidation/index.d.ts +16 -0
- package/lib/trade/liquidation/index.js +123 -0
- package/lib/trade/liquidation/types.d.ts +42 -0
- package/lib/trade/liquidation/types.js +2 -0
- package/lib/trade/pnl/builder.d.ts +16 -0
- package/lib/trade/pnl/builder.js +44 -0
- package/lib/trade/pnl/converter.d.ts +47 -0
- package/lib/trade/pnl/converter.js +72 -0
- package/lib/trade/pnl/index.d.ts +88 -0
- package/lib/trade/pnl/index.js +286 -0
- package/lib/trade/pnl/types.d.ts +75 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/builder.d.ts +23 -0
- package/lib/trade/priceImpact/close/builder.js +45 -0
- package/lib/trade/priceImpact/close/index.d.ts +22 -0
- package/lib/trade/priceImpact/close/index.js +137 -0
- package/lib/trade/priceImpact/close/types.d.ts +44 -0
- package/lib/trade/priceImpact/close/types.js +5 -0
- package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
- package/lib/trade/priceImpact/cumulVol/builder.js +43 -0
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
- package/lib/trade/priceImpact/cumulVol/index.d.ts +108 -0
- package/lib/trade/priceImpact/cumulVol/index.js +231 -0
- package/lib/trade/priceImpact/index.d.ts +13 -0
- package/lib/trade/priceImpact/index.js +64 -0
- package/lib/trade/priceImpact/open/builder.d.ts +21 -0
- package/lib/trade/priceImpact/open/builder.js +43 -0
- package/lib/trade/priceImpact/open/index.d.ts +23 -0
- package/lib/trade/priceImpact/open/index.js +79 -0
- package/lib/trade/priceImpact/open/types.d.ts +42 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
- package/lib/trade/priceImpact/skew/builder.js +28 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +46 -0
- package/lib/trade/priceImpact/skew/converter.js +81 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +60 -0
- package/lib/trade/priceImpact/skew/fetcher.js +169 -0
- package/lib/trade/priceImpact/skew/index.d.ts +60 -0
- package/lib/trade/priceImpact/skew/index.js +173 -0
- package/lib/trade/priceImpact/skew/types.d.ts +42 -0
- package/lib/trade/priceImpact/skew/types.js +5 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +44 -9
- package/lib/trade/types.js +17 -0
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +2 -2
- package/package.json +2 -1
- package/lib/trade/liquidation.d.ts +0 -12
- package/lib/trade/liquidation.js +0 -55
- package/lib/trade/pnl.d.ts +0 -10
- package/lib/trade/pnl.js +0 -33
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Trading fee calculations for opening and closing positions
|
|
3
|
+
*/
|
|
4
|
+
import { Fee, PairIndex, Trade, TradeInfo, TradeFeesData } from "../../types";
|
|
5
|
+
import { GetBorrowingFeeContext } from "../borrowing";
|
|
6
|
+
import * as BorrowingFee from "../borrowing/types";
|
|
7
|
+
import { GetPairBorrowingFeeV2Context } from "../borrowingV2";
|
|
8
|
+
import { GetPairFundingFeeContext } from "../fundingFees/pairContext";
|
|
9
|
+
import { GetTradeFeesContext, GetLiquidationFeesContext, GetClosingFeeContext, TradeFeesBreakdown, TradeHoldingFees } from "./types";
|
|
10
|
+
import { ContractsVersion } from "../../../contracts/types";
|
|
11
|
+
/**
|
|
12
|
+
* @dev Returns the total fee for a trade in collateral tokens
|
|
13
|
+
* @dev Mirrors the contract's getTotalTradeFeesCollateral function
|
|
14
|
+
* @param collateralIndex Collateral index (not used in calculation, for consistency)
|
|
15
|
+
* @param trader Trader address (for fee tier lookup)
|
|
16
|
+
* @param pairIndex Index of the trading pair
|
|
17
|
+
* @param positionSizeCollateral Position size in collateral tokens
|
|
18
|
+
* @param isCounterTrade Whether the trade is a counter trade
|
|
19
|
+
* @param context Context containing fee parameters and settings
|
|
20
|
+
* @returns Total fee in collateral tokens
|
|
21
|
+
*/
|
|
22
|
+
export declare const getTotalTradeFeesCollateral: (collateralIndex: number, trader: string, pairIndex: PairIndex, positionSizeCollateral: number, isCounterTrade: boolean, context: GetTradeFeesContext) => number;
|
|
23
|
+
/**
|
|
24
|
+
* @dev Returns the fee breakdown for a trade
|
|
25
|
+
* @dev Mirrors the contract's getTradeFeesCollateral function
|
|
26
|
+
*/
|
|
27
|
+
export declare const getTradeFeesCollateral: (collateralIndex: number, trader: string, pairIndex: PairIndex, positionSizeCollateral: number, isCounterTrade: boolean, context: GetTradeFeesContext) => TradeFeesBreakdown;
|
|
28
|
+
/**
|
|
29
|
+
* @dev Returns total liquidation fee for a trade in collateral tokens
|
|
30
|
+
* @dev Mirrors the contract's getTotalTradeLiqFeesCollateral function
|
|
31
|
+
*/
|
|
32
|
+
export declare const getTotalTradeLiqFeesCollateral: (collateralIndex: number, trader: string, pairIndex: PairIndex, collateralAmount: number, context: GetLiquidationFeesContext) => number;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Legacy function for backward compatibility
|
|
35
|
+
* @deprecated Use getTotalTradeFeesCollateral instead
|
|
36
|
+
*/
|
|
37
|
+
export declare const getClosingFee: (collateralAmount: number, leverage: number, pairIndex: PairIndex, pairFee: Fee | undefined, _collateralPriceUsd?: number | undefined, isCounterTrade?: boolean, trader?: string, context?: GetClosingFeeContext) => number;
|
|
38
|
+
/**
|
|
39
|
+
* @dev Context for holding fees calculation with structured sub-contexts
|
|
40
|
+
*/
|
|
41
|
+
export type GetStructuredHoldingFeesContext = {
|
|
42
|
+
contractsVersion: ContractsVersion;
|
|
43
|
+
currentTimestamp: number;
|
|
44
|
+
collateralPriceUsd: number;
|
|
45
|
+
borrowingV1?: GetBorrowingFeeContext;
|
|
46
|
+
borrowingV2?: GetPairBorrowingFeeV2Context;
|
|
47
|
+
funding?: GetPairFundingFeeContext;
|
|
48
|
+
initialAccFees?: BorrowingFee.InitialAccFees;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* @dev Calculates total holding fees for a trade (funding + borrowing fees)
|
|
52
|
+
* @param trade The trade to calculate fees for
|
|
53
|
+
* @param tradeInfo Trade info containing contracts version
|
|
54
|
+
* @param tradeFeesData Trade fees data containing initial acc fees
|
|
55
|
+
* @param currentPairPrice Current pair price
|
|
56
|
+
* @param context Structured context with sub-contexts for each fee type
|
|
57
|
+
* @returns Object containing all holding fee components
|
|
58
|
+
*/
|
|
59
|
+
export declare const getTradePendingHoldingFeesCollateral: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: GetStructuredHoldingFeesContext) => TradeHoldingFees;
|
|
60
|
+
export * from "./types";
|
|
61
|
+
export * from "./converter";
|
|
62
|
+
export * from "./builder";
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Trading fee calculations for opening and closing positions
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.getTradePendingHoldingFeesCollateral = exports.getClosingFee = exports.getTotalTradeLiqFeesCollateral = exports.getTradeFeesCollateral = exports.getTotalTradeFeesCollateral = void 0;
|
|
21
|
+
const borrowing_1 = require("../borrowing");
|
|
22
|
+
const borrowingV2_1 = require("../borrowingV2");
|
|
23
|
+
const fundingFees_1 = require("../fundingFees");
|
|
24
|
+
const tiers_1 = require("../tiers");
|
|
25
|
+
const types_1 = require("../../../contracts/types");
|
|
26
|
+
/**
|
|
27
|
+
* @dev Returns the total fee for a trade in collateral tokens
|
|
28
|
+
* @dev Mirrors the contract's getTotalTradeFeesCollateral function
|
|
29
|
+
* @param collateralIndex Collateral index (not used in calculation, for consistency)
|
|
30
|
+
* @param trader Trader address (for fee tier lookup)
|
|
31
|
+
* @param pairIndex Index of the trading pair
|
|
32
|
+
* @param positionSizeCollateral Position size in collateral tokens
|
|
33
|
+
* @param isCounterTrade Whether the trade is a counter trade
|
|
34
|
+
* @param context Context containing fee parameters and settings
|
|
35
|
+
* @returns Total fee in collateral tokens
|
|
36
|
+
*/
|
|
37
|
+
const getTotalTradeFeesCollateral = (collateralIndex, trader, pairIndex, positionSizeCollateral, isCounterTrade, context) => {
|
|
38
|
+
var _a;
|
|
39
|
+
const { fee, collateralPriceUsd } = context;
|
|
40
|
+
const { totalPositionSizeFeeP, minPositionSizeUsd } = fee;
|
|
41
|
+
// Get counter trade fee rate multiplier (default 1 = 1x)
|
|
42
|
+
const counterTradeFeeRateMultiplier = isCounterTrade && ((_a = context.counterTradeSettings) === null || _a === void 0 ? void 0 : _a[pairIndex])
|
|
43
|
+
? context.counterTradeSettings[pairIndex].feeRateMultiplier
|
|
44
|
+
: 1;
|
|
45
|
+
// Apply counter trade multiplier to position size
|
|
46
|
+
const adjustedPositionSizeCollateral = positionSizeCollateral * counterTradeFeeRateMultiplier;
|
|
47
|
+
// Calculate minimum position size in collateral
|
|
48
|
+
const minPositionSizeCollateral = minPositionSizeUsd / collateralPriceUsd;
|
|
49
|
+
// Use max of adjusted position size and minimum position size
|
|
50
|
+
const positionSizeBasis = Math.max(adjustedPositionSizeCollateral, minPositionSizeCollateral);
|
|
51
|
+
// Calculate raw fee
|
|
52
|
+
const rawFee = totalPositionSizeFeeP * positionSizeBasis;
|
|
53
|
+
// Apply trader fee tier if available
|
|
54
|
+
return (0, tiers_1.calculateFeeAmount)(trader, rawFee, context.traderFeeMultiplier);
|
|
55
|
+
};
|
|
56
|
+
exports.getTotalTradeFeesCollateral = getTotalTradeFeesCollateral;
|
|
57
|
+
/**
|
|
58
|
+
* @dev Returns the fee breakdown for a trade
|
|
59
|
+
* @dev Mirrors the contract's getTradeFeesCollateral function
|
|
60
|
+
*/
|
|
61
|
+
const getTradeFeesCollateral = (collateralIndex, trader, pairIndex, positionSizeCollateral, isCounterTrade, context) => {
|
|
62
|
+
const totalFees = (0, exports.getTotalTradeFeesCollateral)(collateralIndex, trader, pairIndex, positionSizeCollateral, isCounterTrade, context);
|
|
63
|
+
const { globalTradeFeeParams } = context;
|
|
64
|
+
const totalP = globalTradeFeeParams.referralFeeP +
|
|
65
|
+
globalTradeFeeParams.govFeeP +
|
|
66
|
+
globalTradeFeeParams.triggerOrderFeeP +
|
|
67
|
+
globalTradeFeeParams.gnsOtcFeeP +
|
|
68
|
+
globalTradeFeeParams.gTokenFeeP;
|
|
69
|
+
// Distribute fees proportionally
|
|
70
|
+
return {
|
|
71
|
+
referralFeeCollateral: (totalFees * globalTradeFeeParams.referralFeeP) / totalP,
|
|
72
|
+
govFeeCollateral: (totalFees * globalTradeFeeParams.govFeeP) / totalP,
|
|
73
|
+
triggerFeeCollateral: (totalFees * globalTradeFeeParams.triggerOrderFeeP) / totalP,
|
|
74
|
+
gnsOtcFeeCollateral: (totalFees * globalTradeFeeParams.gnsOtcFeeP) / totalP,
|
|
75
|
+
gTokenFeeCollateral: (totalFees * globalTradeFeeParams.gTokenFeeP) / totalP,
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
exports.getTradeFeesCollateral = getTradeFeesCollateral;
|
|
79
|
+
/**
|
|
80
|
+
* @dev Returns total liquidation fee for a trade in collateral tokens
|
|
81
|
+
* @dev Mirrors the contract's getTotalTradeLiqFeesCollateral function
|
|
82
|
+
*/
|
|
83
|
+
const getTotalTradeLiqFeesCollateral = (collateralIndex, trader, pairIndex, collateralAmount, context) => {
|
|
84
|
+
const { totalLiqCollateralFeeP } = context;
|
|
85
|
+
// Calculate raw liquidation fee
|
|
86
|
+
const rawFee = collateralAmount * totalLiqCollateralFeeP;
|
|
87
|
+
// Apply trader fee tier if available
|
|
88
|
+
return (0, tiers_1.calculateFeeAmount)(trader, rawFee, context.traderFeeMultiplier);
|
|
89
|
+
};
|
|
90
|
+
exports.getTotalTradeLiqFeesCollateral = getTotalTradeLiqFeesCollateral;
|
|
91
|
+
/**
|
|
92
|
+
* @dev Legacy function for backward compatibility
|
|
93
|
+
* @deprecated Use getTotalTradeFeesCollateral instead
|
|
94
|
+
*/
|
|
95
|
+
const getClosingFee = (collateralAmount, leverage, pairIndex, pairFee,
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
97
|
+
_collateralPriceUsd = 0, // Kept for backward compatibility
|
|
98
|
+
isCounterTrade = false, trader, context) => {
|
|
99
|
+
if (!pairFee || !context)
|
|
100
|
+
return 0;
|
|
101
|
+
const positionSizeCollateral = collateralAmount * leverage;
|
|
102
|
+
return (0, exports.getTotalTradeFeesCollateral)(0, // collateralIndex not used
|
|
103
|
+
trader || "", pairIndex, positionSizeCollateral, isCounterTrade, context);
|
|
104
|
+
};
|
|
105
|
+
exports.getClosingFee = getClosingFee;
|
|
106
|
+
/**
|
|
107
|
+
* @dev Calculates total holding fees for a trade (funding + borrowing fees)
|
|
108
|
+
* @param trade The trade to calculate fees for
|
|
109
|
+
* @param tradeInfo Trade info containing contracts version
|
|
110
|
+
* @param tradeFeesData Trade fees data containing initial acc fees
|
|
111
|
+
* @param currentPairPrice Current pair price
|
|
112
|
+
* @param context Structured context with sub-contexts for each fee type
|
|
113
|
+
* @returns Object containing all holding fee components
|
|
114
|
+
*/
|
|
115
|
+
const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
|
|
116
|
+
const positionSizeCollateral = trade.collateralAmount * trade.leverage;
|
|
117
|
+
// Calculate funding fees (v10+ only)
|
|
118
|
+
let fundingFeeCollateral = 0;
|
|
119
|
+
if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
|
|
120
|
+
context.funding &&
|
|
121
|
+
tradeFeesData.initialAccFundingFeeP !== undefined) {
|
|
122
|
+
fundingFeeCollateral = (0, fundingFees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, Object.assign(Object.assign({}, context.funding), { currentTimestamp: context.currentTimestamp }));
|
|
123
|
+
}
|
|
124
|
+
// Calculate borrowing fees v2 (v10+ only)
|
|
125
|
+
let borrowingFeeCollateral = 0;
|
|
126
|
+
if (context.contractsVersion >= types_1.ContractsVersion.V10 &&
|
|
127
|
+
context.borrowingV2 &&
|
|
128
|
+
tradeFeesData.initialAccBorrowingFeeP !== undefined) {
|
|
129
|
+
borrowingFeeCollateral = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
|
|
130
|
+
positionSizeCollateral,
|
|
131
|
+
openPrice: trade.openPrice,
|
|
132
|
+
currentPairPrice,
|
|
133
|
+
initialAccBorrowingFeeP: tradeFeesData.initialAccBorrowingFeeP,
|
|
134
|
+
currentTimestamp: context.currentTimestamp,
|
|
135
|
+
}, context.borrowingV2);
|
|
136
|
+
}
|
|
137
|
+
// Calculate v1 borrowing fees (some markets use v1 indefinitely)
|
|
138
|
+
let borrowingFeeCollateral_old = 0;
|
|
139
|
+
if (context.borrowingV1 && context.initialAccFees) {
|
|
140
|
+
borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFees, context.borrowingV1);
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
fundingFeeCollateral,
|
|
144
|
+
borrowingFeeCollateral,
|
|
145
|
+
borrowingFeeCollateral_old,
|
|
146
|
+
totalFeeCollateral: fundingFeeCollateral +
|
|
147
|
+
borrowingFeeCollateral +
|
|
148
|
+
borrowingFeeCollateral_old,
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
exports.getTradePendingHoldingFeesCollateral = getTradePendingHoldingFeesCollateral;
|
|
152
|
+
// Export types
|
|
153
|
+
__exportStar(require("./types"), exports);
|
|
154
|
+
__exportStar(require("./converter"), exports);
|
|
155
|
+
__exportStar(require("./builder"), exports);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Types for trading fee calculations (open/close position fees)
|
|
3
|
+
*/
|
|
4
|
+
import { Fee, CounterTradeSettings, GlobalTradeFeeParams } from "../../types";
|
|
5
|
+
/**
|
|
6
|
+
* @dev Breakdown of trading fees into components
|
|
7
|
+
*/
|
|
8
|
+
export type TradeFeesBreakdown = {
|
|
9
|
+
referralFeeCollateral: number;
|
|
10
|
+
govFeeCollateral: number;
|
|
11
|
+
triggerFeeCollateral: number;
|
|
12
|
+
gnsOtcFeeCollateral: number;
|
|
13
|
+
gTokenFeeCollateral: number;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* @dev Context for calculating trading fees
|
|
17
|
+
*/
|
|
18
|
+
export type GetTradeFeesContext = {
|
|
19
|
+
fee: Fee;
|
|
20
|
+
collateralPriceUsd: number;
|
|
21
|
+
globalTradeFeeParams: GlobalTradeFeeParams;
|
|
22
|
+
counterTradeSettings?: {
|
|
23
|
+
[pairIndex: number]: CounterTradeSettings;
|
|
24
|
+
};
|
|
25
|
+
traderFeeMultiplier?: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* @dev Context for calculating liquidation fees
|
|
29
|
+
*/
|
|
30
|
+
export type GetLiquidationFeesContext = {
|
|
31
|
+
totalLiqCollateralFeeP: number;
|
|
32
|
+
globalTradeFeeParams: GlobalTradeFeeParams;
|
|
33
|
+
traderFeeMultiplier?: number;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* @dev Legacy support
|
|
37
|
+
*/
|
|
38
|
+
export type GetClosingFeeContext = GetTradeFeesContext;
|
|
39
|
+
/**
|
|
40
|
+
* @dev Holding fees breakdown (funding + borrowing)
|
|
41
|
+
*/
|
|
42
|
+
export type TradeHoldingFees = {
|
|
43
|
+
fundingFeeCollateral: number;
|
|
44
|
+
borrowingFeeCollateral: number;
|
|
45
|
+
borrowingFeeCollateral_old: number;
|
|
46
|
+
totalFeeCollateral: number;
|
|
47
|
+
};
|
|
48
|
+
export type { GlobalTradeFeeParams };
|
package/lib/trade/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export * from "./fees";
|
|
2
|
-
export * from "./pnl";
|
|
2
|
+
export * from "./pnl/index";
|
|
3
3
|
export * from "./spread";
|
|
4
4
|
export * from "./liquidation";
|
|
5
|
-
export * from "./fees";
|
|
6
5
|
export * from "./types";
|
|
7
6
|
export * from "./oiWindows";
|
|
7
|
+
export * from "./priceImpact";
|
|
8
|
+
export * from "./utils";
|
package/lib/trade/index.js
CHANGED
|
@@ -15,9 +15,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./fees"), exports);
|
|
18
|
-
__exportStar(require("./pnl"), exports);
|
|
18
|
+
__exportStar(require("./pnl/index"), exports);
|
|
19
19
|
__exportStar(require("./spread"), exports);
|
|
20
20
|
__exportStar(require("./liquidation"), exports);
|
|
21
|
-
__exportStar(require("./fees"), exports);
|
|
22
21
|
__exportStar(require("./types"), exports);
|
|
23
22
|
__exportStar(require("./oiWindows"), exports);
|
|
23
|
+
__exportStar(require("./priceImpact"), exports);
|
|
24
|
+
__exportStar(require("./utils"), exports);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Liquidation price context builder module
|
|
3
|
+
* @dev Provides builder functions for creating liquidation price contexts
|
|
4
|
+
*/
|
|
5
|
+
import { GlobalTradingVariablesType } from "../../backend/tradingVariables/types";
|
|
6
|
+
import { TradeContainer, UserPriceImpact } from "../types";
|
|
7
|
+
import { GetLiquidationPriceContext } from "./types";
|
|
8
|
+
/**
|
|
9
|
+
* @dev Builds a complete context for liquidation price calculations
|
|
10
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
11
|
+
* @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
|
|
12
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
13
|
+
* @returns Complete context ready for getLiquidationPrice
|
|
14
|
+
*/
|
|
15
|
+
export declare const buildLiquidationPriceContext: (globalTradingVariables: GlobalTradingVariablesType, tradeContainer: TradeContainer, additionalParams: {
|
|
16
|
+
currentBlock: number;
|
|
17
|
+
currentTimestamp: number;
|
|
18
|
+
currentPairPrice: number;
|
|
19
|
+
spreadP: number;
|
|
20
|
+
traderFeeMultiplier?: number;
|
|
21
|
+
additionalFeeCollateral?: number;
|
|
22
|
+
partialCloseMultiplier?: number;
|
|
23
|
+
beforeOpened?: boolean;
|
|
24
|
+
userPriceImpact?: UserPriceImpact;
|
|
25
|
+
}) => GetLiquidationPriceContext;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Liquidation price context builder module
|
|
4
|
+
* @dev Provides builder functions for creating liquidation price contexts
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.buildLiquidationPriceContext = void 0;
|
|
8
|
+
const builder_1 = require("../fees/borrowing/builder");
|
|
9
|
+
const builder_2 = require("../fees/borrowingV2/builder");
|
|
10
|
+
const builder_3 = require("../fees/fundingFees/builder");
|
|
11
|
+
const builder_4 = require("../fees/trading/builder");
|
|
12
|
+
/**
|
|
13
|
+
* @dev Builds a complete context for liquidation price calculations
|
|
14
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
15
|
+
* @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
|
|
16
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
17
|
+
* @returns Complete context ready for getLiquidationPrice
|
|
18
|
+
*/
|
|
19
|
+
const buildLiquidationPriceContext = (globalTradingVariables, tradeContainer, additionalParams) => {
|
|
20
|
+
var _a;
|
|
21
|
+
const { trade, tradeInfo } = tradeContainer;
|
|
22
|
+
const collateralIndex = trade.collateralIndex || 1;
|
|
23
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
24
|
+
if (!tradeContainer.liquidationParams) {
|
|
25
|
+
throw new Error("Liquidation params are required for liquidation price calculation");
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
// Core shared context
|
|
29
|
+
core: {
|
|
30
|
+
currentBlock: additionalParams.currentBlock,
|
|
31
|
+
currentTimestamp: additionalParams.currentTimestamp,
|
|
32
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
33
|
+
contractsVersion: tradeInfo.contractsVersion,
|
|
34
|
+
spreadP: additionalParams.spreadP,
|
|
35
|
+
},
|
|
36
|
+
// Build sub-contexts using dedicated builders
|
|
37
|
+
borrowingV1: (0, builder_1.buildBorrowingV1Context)(globalTradingVariables, collateralIndex, additionalParams.currentBlock),
|
|
38
|
+
borrowingV2: (0, builder_2.buildBorrowingV2Context)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
39
|
+
funding: (0, builder_3.buildFundingContext)(globalTradingVariables, collateralIndex, trade.pairIndex, additionalParams.currentTimestamp),
|
|
40
|
+
trading: Object.assign(Object.assign({}, (0, builder_4.buildTradingFeesContext)(globalTradingVariables, trade.pairIndex, additionalParams.traderFeeMultiplier)), { counterTradeSettings: globalTradingVariables.counterTradeSettings, userPriceImpact: additionalParams.userPriceImpact }),
|
|
41
|
+
// Trade-specific data
|
|
42
|
+
tradeData: {
|
|
43
|
+
tradeInfo,
|
|
44
|
+
tradeFeesData: tradeContainer.tradeFeesData,
|
|
45
|
+
liquidationParams: tradeContainer.liquidationParams,
|
|
46
|
+
initialAccFees: tradeContainer.initialAccFees,
|
|
47
|
+
},
|
|
48
|
+
// Additional parameters for liquidation calculation
|
|
49
|
+
liquidationSpecific: {
|
|
50
|
+
currentPairPrice: additionalParams.currentPairPrice,
|
|
51
|
+
additionalFeeCollateral: additionalParams.additionalFeeCollateral || 0,
|
|
52
|
+
partialCloseMultiplier: additionalParams.partialCloseMultiplier || 1,
|
|
53
|
+
beforeOpened: additionalParams.beforeOpened || false,
|
|
54
|
+
isCounterTrade: trade.isCounterTrade || false,
|
|
55
|
+
userPriceImpact: additionalParams.userPriceImpact,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
exports.buildLiquidationPriceContext = buildLiquidationPriceContext;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Converters for liquidation data between contract and SDK formats
|
|
3
|
+
*/
|
|
4
|
+
import { IPairsStorage } from "../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
5
|
+
import { LiquidationParams } from "../types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Converts contract liquidation params to SDK format
|
|
8
|
+
* @param params Group liquidation params from contract
|
|
9
|
+
* @returns Normalized liquidation params
|
|
10
|
+
*/
|
|
11
|
+
export declare const convertLiquidationParams: (params: IPairsStorage.GroupLiquidationParamsStructOutput) => LiquidationParams;
|
|
12
|
+
/**
|
|
13
|
+
* @dev Converts array of liquidation params from contract
|
|
14
|
+
* @param paramsArray Array of group liquidation params
|
|
15
|
+
* @returns Array of normalized liquidation params
|
|
16
|
+
*/
|
|
17
|
+
export declare const convertLiquidationParamsArray: (paramsArray: IPairsStorage.GroupLiquidationParamsStructOutput[]) => LiquidationParams[];
|
|
18
|
+
/**
|
|
19
|
+
* @dev Converts liquidation params to contract format (for encoding)
|
|
20
|
+
* @param params SDK liquidation params
|
|
21
|
+
* @returns Contract-formatted liquidation params
|
|
22
|
+
*/
|
|
23
|
+
export declare const encodeLiquidationParams: (params: LiquidationParams) => IPairsStorage.GroupLiquidationParamsStruct;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Converters for liquidation data between contract and SDK formats
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.encodeLiquidationParams = exports.convertLiquidationParamsArray = exports.convertLiquidationParams = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Converts contract liquidation params to SDK format
|
|
9
|
+
* @param params Group liquidation params from contract
|
|
10
|
+
* @returns Normalized liquidation params
|
|
11
|
+
*/
|
|
12
|
+
const convertLiquidationParams = (params) => {
|
|
13
|
+
const ONCHAIN_LIQ_THRESHOLD = 0.9;
|
|
14
|
+
return {
|
|
15
|
+
maxLiqSpreadP: Number(params.maxLiqSpreadP) / 1e10 / 100,
|
|
16
|
+
startLiqThresholdP: Number(params.startLiqThresholdP) / 1e10 / 100 || ONCHAIN_LIQ_THRESHOLD,
|
|
17
|
+
endLiqThresholdP: Number(params.endLiqThresholdP) / 1e10 / 100 || ONCHAIN_LIQ_THRESHOLD,
|
|
18
|
+
startLeverage: Number(params.startLeverage) / 1e3,
|
|
19
|
+
endLeverage: Number(params.endLeverage) / 1e3, // 1e3 → float
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
exports.convertLiquidationParams = convertLiquidationParams;
|
|
23
|
+
/**
|
|
24
|
+
* @dev Converts array of liquidation params from contract
|
|
25
|
+
* @param paramsArray Array of group liquidation params
|
|
26
|
+
* @returns Array of normalized liquidation params
|
|
27
|
+
*/
|
|
28
|
+
const convertLiquidationParamsArray = (paramsArray) => {
|
|
29
|
+
return paramsArray.map(exports.convertLiquidationParams);
|
|
30
|
+
};
|
|
31
|
+
exports.convertLiquidationParamsArray = convertLiquidationParamsArray;
|
|
32
|
+
/**
|
|
33
|
+
* @dev Converts liquidation params to contract format (for encoding)
|
|
34
|
+
* @param params SDK liquidation params
|
|
35
|
+
* @returns Contract-formatted liquidation params
|
|
36
|
+
*/
|
|
37
|
+
const encodeLiquidationParams = (params) => {
|
|
38
|
+
return {
|
|
39
|
+
maxLiqSpreadP: Math.round(params.maxLiqSpreadP * 100 * 1e10),
|
|
40
|
+
startLiqThresholdP: Math.round(params.startLiqThresholdP * 100 * 1e10),
|
|
41
|
+
endLiqThresholdP: Math.round(params.endLiqThresholdP * 100 * 1e10),
|
|
42
|
+
startLeverage: Math.round(params.startLeverage * 1e3),
|
|
43
|
+
endLeverage: Math.round(params.endLeverage * 1e3), // float → 1e3
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
exports.encodeLiquidationParams = encodeLiquidationParams;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Main export file for liquidation module
|
|
3
|
+
*/
|
|
4
|
+
import { Trade, LiquidationParams } from "..";
|
|
5
|
+
import { GetLiquidationPriceContext } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Calculate liquidation price with structured context
|
|
8
|
+
* @param trade The trade to calculate liquidation price for
|
|
9
|
+
* @param context Structured context with all required data
|
|
10
|
+
* @returns Liquidation price
|
|
11
|
+
*/
|
|
12
|
+
export declare const getLiquidationPrice: (trade: Trade, context: GetLiquidationPriceContext) => number;
|
|
13
|
+
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|
|
14
|
+
export { convertLiquidationParams, convertLiquidationParamsArray, encodeLiquidationParams, } from "./converter";
|
|
15
|
+
export * from "./types";
|
|
16
|
+
export * from "./builder";
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Main export file for liquidation module
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.encodeLiquidationParams = exports.convertLiquidationParamsArray = exports.convertLiquidationParams = exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
|
|
21
|
+
const types_1 = require("../../contracts/types");
|
|
22
|
+
const __1 = require("..");
|
|
23
|
+
/**
|
|
24
|
+
* @dev Calculate liquidation price with structured context
|
|
25
|
+
* @param trade The trade to calculate liquidation price for
|
|
26
|
+
* @param context Structured context with all required data
|
|
27
|
+
* @returns Liquidation price
|
|
28
|
+
*/
|
|
29
|
+
const getLiquidationPrice = (trade, context) => {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
// Extract parameters from structured context
|
|
32
|
+
const { currentPairPrice, additionalFeeCollateral = 0, partialCloseMultiplier = 1, beforeOpened = false, isCounterTrade = false, } = context.liquidationSpecific;
|
|
33
|
+
// 1. Calculate closing fees
|
|
34
|
+
const closingFee = (0, __1.getTotalTradeFeesCollateral)(trade.collateralIndex, "", // No fee tiers applied for liquidation calculation
|
|
35
|
+
trade.pairIndex, trade.collateralAmount * trade.leverage, isCounterTrade, {
|
|
36
|
+
fee: context.trading.fee,
|
|
37
|
+
collateralPriceUsd: context.core.collateralPriceUsd,
|
|
38
|
+
globalTradeFeeParams: context.trading.globalTradeFeeParams,
|
|
39
|
+
traderFeeMultiplier: 1,
|
|
40
|
+
counterTradeSettings: context.trading.counterTradeSettings,
|
|
41
|
+
});
|
|
42
|
+
// 2. Calculate holding fees and realized PnL for opened trades
|
|
43
|
+
let holdingFeesTotal = 0;
|
|
44
|
+
let totalRealizedPnlCollateral = 0;
|
|
45
|
+
if (!beforeOpened) {
|
|
46
|
+
// Calculate holding fees
|
|
47
|
+
const holdingFees = (0, __1.getTradePendingHoldingFeesCollateral)(trade, context.tradeData.tradeInfo, context.tradeData.tradeFeesData, currentPairPrice, {
|
|
48
|
+
contractsVersion: context.core.contractsVersion,
|
|
49
|
+
currentTimestamp: context.core.currentTimestamp,
|
|
50
|
+
collateralPriceUsd: context.core.collateralPriceUsd,
|
|
51
|
+
borrowingV1: context.borrowingV1,
|
|
52
|
+
borrowingV2: context.borrowingV2,
|
|
53
|
+
funding: context.funding,
|
|
54
|
+
initialAccFees: context.tradeData.initialAccFees,
|
|
55
|
+
});
|
|
56
|
+
holdingFeesTotal = holdingFees.totalFeeCollateral;
|
|
57
|
+
// Calculate total realized PnL (realized PnL minus realized trading fees)
|
|
58
|
+
totalRealizedPnlCollateral =
|
|
59
|
+
context.tradeData.tradeFeesData.realizedPnlCollateral -
|
|
60
|
+
context.tradeData.tradeFeesData.realizedTradingFeesCollateral;
|
|
61
|
+
}
|
|
62
|
+
// 3. Apply unified formula for all trades
|
|
63
|
+
const totalFeesCollateral = closingFee +
|
|
64
|
+
(holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
|
|
65
|
+
additionalFeeCollateral;
|
|
66
|
+
// 4. Calculate liquidation threshold
|
|
67
|
+
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.tradeData.liquidationParams, trade.leverage);
|
|
68
|
+
// 5. Calculate liquidation price distance
|
|
69
|
+
const collateralLiqNegativePnl = trade.collateralAmount * liqThresholdP;
|
|
70
|
+
let liqPriceDistance = (trade.openPrice * (collateralLiqNegativePnl - totalFeesCollateral)) /
|
|
71
|
+
trade.collateralAmount /
|
|
72
|
+
trade.leverage;
|
|
73
|
+
// 6. Apply closing spread for v9.2+
|
|
74
|
+
if (context.core.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
75
|
+
((((_a = context.tradeData.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
76
|
+
context.tradeData.liquidationParams.maxLiqSpreadP > 0) ||
|
|
77
|
+
(((_b = context.liquidationSpecific.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !==
|
|
78
|
+
undefined &&
|
|
79
|
+
context.liquidationSpecific.userPriceImpact.fixedSpreadP > 0))) {
|
|
80
|
+
const closingSpreadP = (0, __1.getSpreadP)(context.core.spreadP, true, context.tradeData.liquidationParams, context.liquidationSpecific.userPriceImpact);
|
|
81
|
+
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
82
|
+
}
|
|
83
|
+
// 7. Calculate final liquidation price
|
|
84
|
+
return trade.long
|
|
85
|
+
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
86
|
+
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
87
|
+
};
|
|
88
|
+
exports.getLiquidationPrice = getLiquidationPrice;
|
|
89
|
+
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
90
|
+
if (liquidationParams === undefined ||
|
|
91
|
+
leverage === undefined ||
|
|
92
|
+
liquidationParams.maxLiqSpreadP === 0 ||
|
|
93
|
+
liquidationParams.startLiqThresholdP === 0 ||
|
|
94
|
+
liquidationParams.endLiqThresholdP === 0 ||
|
|
95
|
+
liquidationParams.startLeverage === 0 ||
|
|
96
|
+
liquidationParams.endLeverage === 0) {
|
|
97
|
+
return 0.9;
|
|
98
|
+
}
|
|
99
|
+
if (leverage < liquidationParams.startLeverage) {
|
|
100
|
+
return liquidationParams.startLiqThresholdP;
|
|
101
|
+
}
|
|
102
|
+
if (leverage > liquidationParams.endLeverage) {
|
|
103
|
+
return liquidationParams.endLiqThresholdP;
|
|
104
|
+
}
|
|
105
|
+
if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
|
|
106
|
+
return liquidationParams.endLiqThresholdP;
|
|
107
|
+
}
|
|
108
|
+
return (liquidationParams.startLiqThresholdP -
|
|
109
|
+
((leverage - liquidationParams.startLeverage) *
|
|
110
|
+
(liquidationParams.startLiqThresholdP -
|
|
111
|
+
liquidationParams.endLiqThresholdP)) /
|
|
112
|
+
(liquidationParams.endLeverage - liquidationParams.startLeverage));
|
|
113
|
+
};
|
|
114
|
+
exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
|
|
115
|
+
// Converters
|
|
116
|
+
var converter_1 = require("./converter");
|
|
117
|
+
Object.defineProperty(exports, "convertLiquidationParams", { enumerable: true, get: function () { return converter_1.convertLiquidationParams; } });
|
|
118
|
+
Object.defineProperty(exports, "convertLiquidationParamsArray", { enumerable: true, get: function () { return converter_1.convertLiquidationParamsArray; } });
|
|
119
|
+
Object.defineProperty(exports, "encodeLiquidationParams", { enumerable: true, get: function () { return converter_1.encodeLiquidationParams; } });
|
|
120
|
+
// Types
|
|
121
|
+
__exportStar(require("./types"), exports);
|
|
122
|
+
// Builder
|
|
123
|
+
__exportStar(require("./builder"), exports);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { GetBorrowingFeeContext, BorrowingFee } from "./../fees";
|
|
2
|
+
import { LiquidationParams, UserPriceImpact, TradeFeesData, TradeInfo, CounterTradeSettings } from "./../types";
|
|
3
|
+
import { ContractsVersion } from "../../contracts/types";
|
|
4
|
+
import { GetPairBorrowingFeeV2Context } from "../fees/borrowingV2";
|
|
5
|
+
import { GetPairFundingFeeContext } from "../fees/fundingFees";
|
|
6
|
+
import { TradingFeesSubContext } from "../fees/trading/builder";
|
|
7
|
+
/**
|
|
8
|
+
* @dev Structured context for liquidation price calculations
|
|
9
|
+
* @dev Follows the same pattern as GetComprehensivePnlContext
|
|
10
|
+
*/
|
|
11
|
+
export type GetLiquidationPriceContext = {
|
|
12
|
+
core: {
|
|
13
|
+
currentBlock: number;
|
|
14
|
+
currentTimestamp: number;
|
|
15
|
+
collateralPriceUsd: number;
|
|
16
|
+
contractsVersion: ContractsVersion;
|
|
17
|
+
spreadP: number;
|
|
18
|
+
};
|
|
19
|
+
borrowingV1?: GetBorrowingFeeContext;
|
|
20
|
+
borrowingV2?: GetPairBorrowingFeeV2Context;
|
|
21
|
+
funding?: GetPairFundingFeeContext;
|
|
22
|
+
trading: TradingFeesSubContext & {
|
|
23
|
+
userPriceImpact?: UserPriceImpact;
|
|
24
|
+
counterTradeSettings?: {
|
|
25
|
+
[pairIndex: number]: CounterTradeSettings;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
tradeData: {
|
|
29
|
+
tradeInfo: TradeInfo;
|
|
30
|
+
tradeFeesData: TradeFeesData;
|
|
31
|
+
liquidationParams: LiquidationParams;
|
|
32
|
+
initialAccFees?: BorrowingFee.InitialAccFees;
|
|
33
|
+
};
|
|
34
|
+
liquidationSpecific: {
|
|
35
|
+
currentPairPrice: number;
|
|
36
|
+
additionalFeeCollateral: number;
|
|
37
|
+
partialCloseMultiplier: number;
|
|
38
|
+
beforeOpened: boolean;
|
|
39
|
+
isCounterTrade: boolean;
|
|
40
|
+
userPriceImpact?: UserPriceImpact;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TradeContainer } from "../types";
|
|
2
|
+
import { GlobalTradingVariablesType } from "src/backend/tradingVariables/types";
|
|
3
|
+
import { GetComprehensivePnlContext } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* @dev Builds a complete context for comprehensive PnL calculations
|
|
6
|
+
* @dev Uses sub-context builders to create properly scoped contexts
|
|
7
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
8
|
+
* @param tradeContainer Full trade container with trade, tradeInfo, fees data and liquidation params
|
|
9
|
+
* @param additionalParams Additional parameters not available in trading variables
|
|
10
|
+
* @returns Complete context ready for getComprehensivePnl
|
|
11
|
+
*/
|
|
12
|
+
export declare const buildComprehensivePnlContext: (globalTradingVariables: GlobalTradingVariablesType, tradeContainer: TradeContainer, additionalParams: {
|
|
13
|
+
currentBlock: number;
|
|
14
|
+
currentTimestamp: number;
|
|
15
|
+
traderFeeMultiplier?: number;
|
|
16
|
+
}) => GetComprehensivePnlContext;
|