@gainsnetwork/sdk 0.0.0-v10.rc2 → 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/contracts/fetch/fees/borrowingFeesV2.d.ts +0 -1
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +7 -20
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +7 -16
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
- package/lib/contracts/types/generated/factories/GToken__factory.js +4 -0
- package/lib/contracts/utils/index.d.ts +0 -3
- package/lib/contracts/utils/index.js +0 -3
- package/lib/contracts/utils/openTrades.js +14 -30
- 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 +13 -2
- package/lib/trade/fees/borrowing/index.js +30 -14
- 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 +1 -0
- package/lib/trade/fees/borrowingV2/index.js +1 -0
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +110 -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 +1 -0
- package/lib/trade/fees/fundingFees/index.js +4 -0
- package/lib/trade/fees/index.d.ts +2 -1
- package/lib/trade/fees/index.js +44 -35
- package/lib/trade/fees/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- package/lib/trade/fees/tiers/index.d.ts +1 -0
- package/lib/trade/fees/tiers/index.js +1 -0
- package/lib/trade/fees/trading/index.d.ts +22 -2
- package/lib/trade/fees/trading/index.js +44 -1
- package/lib/trade/fees/trading/types.d.ts +9 -0
- package/lib/trade/index.d.ts +1 -1
- package/lib/trade/index.js +1 -1
- 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/liquidation.d.ts +2 -32
- package/lib/trade/liquidation.js +8 -93
- 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/pnl.d.ts +3 -3
- package/lib/trade/pnl.js +2 -7
- 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 +6 -2
- package/lib/trade/priceImpact/index.js +30 -3
- 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/fetcher.d.ts +63 -0
- package/lib/trade/priceImpact/skew/fetcher.js +168 -0
- package/lib/trade/priceImpact/skew/index.d.ts +1 -0
- package/lib/trade/priceImpact/skew/index.js +4 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +9 -7
- package/package.json +2 -2
|
@@ -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,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Main export file for liquidation module
|
|
3
|
+
*/
|
|
4
|
+
import { Trade, Fee, BorrowingFee, LiquidationParams } from "..";
|
|
5
|
+
import { GetLiquidationPriceContext, LiqPriceInput } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Wrapper function that mirrors the contract's getTradeLiquidationPrice signature
|
|
8
|
+
* @dev This is the main entry point matching the contract interface
|
|
9
|
+
* @param input Liquidation price input parameters
|
|
10
|
+
* @param context Additional context for SDK calculations
|
|
11
|
+
* @returns Liquidation price
|
|
12
|
+
*/
|
|
13
|
+
export declare const getTradeLiquidationPrice: (input: LiqPriceInput, context: Omit<GetLiquidationPriceContext, "currentPairPrice" | "isCounterTrade" | "partialCloseMultiplier" | "additionalFeeCollateral" | "beforeOpened" | "liquidationParams"> & {
|
|
14
|
+
fee: Fee;
|
|
15
|
+
}) => number;
|
|
16
|
+
export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
|
|
17
|
+
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|
|
18
|
+
/**
|
|
19
|
+
* @dev Simplified wrapper for getTradeLiquidationPrice
|
|
20
|
+
* @dev Mirrors the contract's simplified overload
|
|
21
|
+
* @param trade The trade to calculate liquidation price for
|
|
22
|
+
* @param additionalFeeCollateral Additional fees to consider
|
|
23
|
+
* @param currentPairPrice Current pair price
|
|
24
|
+
* @param context Context with all required data
|
|
25
|
+
* @returns Liquidation price
|
|
26
|
+
*/
|
|
27
|
+
export declare const getTradeLiquidationPriceSimple: (trade: Trade, additionalFeeCollateral: number, currentPairPrice: number, context: Omit<GetLiquidationPriceContext, "currentPairPrice" | "additionalFeeCollateral"> & {
|
|
28
|
+
fee: Fee;
|
|
29
|
+
}) => number;
|
|
30
|
+
export { convertLiquidationParams, convertLiquidationParamsArray, encodeLiquidationParams, } from "./converter";
|
|
31
|
+
export * from "./types";
|
|
@@ -0,0 +1,187 @@
|
|
|
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.getTradeLiquidationPriceSimple = exports.getLiqPnlThresholdP = exports.getLiquidationPrice = exports.getTradeLiquidationPrice = void 0;
|
|
21
|
+
const types_1 = require("../../contracts/types");
|
|
22
|
+
const __1 = require("..");
|
|
23
|
+
/**
|
|
24
|
+
* @dev Wrapper function that mirrors the contract's getTradeLiquidationPrice signature
|
|
25
|
+
* @dev This is the main entry point matching the contract interface
|
|
26
|
+
* @param input Liquidation price input parameters
|
|
27
|
+
* @param context Additional context for SDK calculations
|
|
28
|
+
* @returns Liquidation price
|
|
29
|
+
*/
|
|
30
|
+
const getTradeLiquidationPrice = (input, context) => {
|
|
31
|
+
// Create trade object from input
|
|
32
|
+
const trade = {
|
|
33
|
+
user: input.trader,
|
|
34
|
+
index: input.index,
|
|
35
|
+
pairIndex: input.pairIndex,
|
|
36
|
+
leverage: input.leverage,
|
|
37
|
+
long: input.long,
|
|
38
|
+
isOpen: true,
|
|
39
|
+
collateralIndex: input.collateralIndex,
|
|
40
|
+
tradeType: 0,
|
|
41
|
+
collateralAmount: input.collateral,
|
|
42
|
+
openPrice: input.openPrice,
|
|
43
|
+
sl: 0,
|
|
44
|
+
tp: 0,
|
|
45
|
+
isCounterTrade: input.isCounterTrade,
|
|
46
|
+
};
|
|
47
|
+
// Merge input params into context
|
|
48
|
+
const fullContext = Object.assign(Object.assign({}, context), { currentPairPrice: input.currentPairPrice, isCounterTrade: input.isCounterTrade, partialCloseMultiplier: input.partialCloseMultiplier, additionalFeeCollateral: input.additionalFeeCollateral, beforeOpened: input.beforeOpened, liquidationParams: input.liquidationParams });
|
|
49
|
+
// Call the existing implementation
|
|
50
|
+
return (0, exports.getLiquidationPrice)(trade, context.fee, context.initialAccFees || { accPairFee: 0, accGroupFee: 0, block: 0 }, fullContext);
|
|
51
|
+
};
|
|
52
|
+
exports.getTradeLiquidationPrice = getTradeLiquidationPrice;
|
|
53
|
+
const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
54
|
+
var _a, _b, _c, _d, _e, _f;
|
|
55
|
+
// Ensure initialAccFees is in context
|
|
56
|
+
if (!context.initialAccFees) {
|
|
57
|
+
context = Object.assign(Object.assign({}, context), { initialAccFees });
|
|
58
|
+
}
|
|
59
|
+
// 1. Calculate liquidation fees
|
|
60
|
+
const closingFee = (0, __1.getTotalTradeLiqFeesCollateral)(0, // collateralIndex not used in calculation
|
|
61
|
+
trade.user, trade.pairIndex, trade.collateralAmount, context);
|
|
62
|
+
// 2. Calculate holding fees and realized PnL
|
|
63
|
+
let holdingFeesTotal = 0;
|
|
64
|
+
let totalRealizedPnlCollateral = 0;
|
|
65
|
+
if (!context.beforeOpened &&
|
|
66
|
+
context.tradeFeesData &&
|
|
67
|
+
context.currentPairPrice) {
|
|
68
|
+
// V10 data available - calculate full holding fees
|
|
69
|
+
// Create a minimal tradeInfo from context
|
|
70
|
+
const tradeInfo = {
|
|
71
|
+
contractsVersion: (_a = context.contractsVersion) !== null && _a !== void 0 ? _a : types_1.ContractsVersion.V10,
|
|
72
|
+
createdBlock: 0,
|
|
73
|
+
tpLastUpdatedBlock: 0,
|
|
74
|
+
slLastUpdatedBlock: 0,
|
|
75
|
+
maxSlippageP: 0,
|
|
76
|
+
lastOiUpdateTs: 0,
|
|
77
|
+
collateralPriceUsd: (_b = context.collateralPriceUsd) !== null && _b !== void 0 ? _b : 0,
|
|
78
|
+
lastPosIncreaseBlock: 0,
|
|
79
|
+
};
|
|
80
|
+
const holdingFees = (0, __1.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeFeesData, context.currentPairPrice, context);
|
|
81
|
+
holdingFeesTotal = holdingFees.totalFeeCollateral;
|
|
82
|
+
// Calculate total realized PnL (realized PnL minus realized trading fees)
|
|
83
|
+
totalRealizedPnlCollateral =
|
|
84
|
+
context.tradeFeesData.realizedPnlCollateral -
|
|
85
|
+
context.tradeFeesData.realizedTradingFeesCollateral;
|
|
86
|
+
}
|
|
87
|
+
else if (!context.beforeOpened) {
|
|
88
|
+
// Markets using v1 borrowing fees model
|
|
89
|
+
holdingFeesTotal = (0, __1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
90
|
+
}
|
|
91
|
+
// 3. Apply unified formula for all trades
|
|
92
|
+
const partialCloseMultiplier = (_c = context.partialCloseMultiplier) !== null && _c !== void 0 ? _c : 1;
|
|
93
|
+
const additionalFeeCollateral = (_d = context.additionalFeeCollateral) !== null && _d !== void 0 ? _d : 0;
|
|
94
|
+
const totalFeesCollateral = closingFee +
|
|
95
|
+
(holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
|
|
96
|
+
additionalFeeCollateral;
|
|
97
|
+
// 4. Calculate liquidation threshold
|
|
98
|
+
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
|
|
99
|
+
// 5. Calculate liquidation price distance
|
|
100
|
+
const collateralLiqNegativePnl = trade.collateralAmount * liqThresholdP;
|
|
101
|
+
let liqPriceDistance = (trade.openPrice * (collateralLiqNegativePnl - totalFeesCollateral)) /
|
|
102
|
+
trade.collateralAmount /
|
|
103
|
+
trade.leverage;
|
|
104
|
+
// 6. Apply closing spread for v9.2+
|
|
105
|
+
if ((context === null || context === void 0 ? void 0 : context.contractsVersion) !== undefined &&
|
|
106
|
+
context.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
107
|
+
((((_e = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _e === void 0 ? void 0 : _e.maxLiqSpreadP) !== undefined &&
|
|
108
|
+
context.liquidationParams.maxLiqSpreadP > 0) ||
|
|
109
|
+
(((_f = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _f === void 0 ? void 0 : _f.fixedSpreadP) !== undefined &&
|
|
110
|
+
context.userPriceImpact.fixedSpreadP > 0))) {
|
|
111
|
+
const closingSpreadP = (0, __1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams, context.userPriceImpact);
|
|
112
|
+
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
113
|
+
}
|
|
114
|
+
// 7. Calculate final liquidation price
|
|
115
|
+
return trade.long
|
|
116
|
+
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
117
|
+
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
118
|
+
};
|
|
119
|
+
exports.getLiquidationPrice = getLiquidationPrice;
|
|
120
|
+
const getLiqPnlThresholdP = (liquidationParams, leverage) => {
|
|
121
|
+
if (liquidationParams === undefined ||
|
|
122
|
+
leverage === undefined ||
|
|
123
|
+
liquidationParams.maxLiqSpreadP === 0 ||
|
|
124
|
+
liquidationParams.startLiqThresholdP === 0 ||
|
|
125
|
+
liquidationParams.endLiqThresholdP === 0 ||
|
|
126
|
+
liquidationParams.startLeverage === 0 ||
|
|
127
|
+
liquidationParams.endLeverage === 0) {
|
|
128
|
+
return 0.9;
|
|
129
|
+
}
|
|
130
|
+
if (leverage < liquidationParams.startLeverage) {
|
|
131
|
+
return liquidationParams.startLiqThresholdP;
|
|
132
|
+
}
|
|
133
|
+
if (leverage > liquidationParams.endLeverage) {
|
|
134
|
+
return liquidationParams.endLiqThresholdP;
|
|
135
|
+
}
|
|
136
|
+
if (liquidationParams.startLiqThresholdP === liquidationParams.endLiqThresholdP) {
|
|
137
|
+
return liquidationParams.endLiqThresholdP;
|
|
138
|
+
}
|
|
139
|
+
return (liquidationParams.startLiqThresholdP -
|
|
140
|
+
((leverage - liquidationParams.startLeverage) *
|
|
141
|
+
(liquidationParams.startLiqThresholdP -
|
|
142
|
+
liquidationParams.endLiqThresholdP)) /
|
|
143
|
+
(liquidationParams.endLeverage - liquidationParams.startLeverage));
|
|
144
|
+
};
|
|
145
|
+
exports.getLiqPnlThresholdP = getLiqPnlThresholdP;
|
|
146
|
+
/**
|
|
147
|
+
* @dev Simplified wrapper for getTradeLiquidationPrice
|
|
148
|
+
* @dev Mirrors the contract's simplified overload
|
|
149
|
+
* @param trade The trade to calculate liquidation price for
|
|
150
|
+
* @param additionalFeeCollateral Additional fees to consider
|
|
151
|
+
* @param currentPairPrice Current pair price
|
|
152
|
+
* @param context Context with all required data
|
|
153
|
+
* @returns Liquidation price
|
|
154
|
+
*/
|
|
155
|
+
const getTradeLiquidationPriceSimple = (trade, additionalFeeCollateral, currentPairPrice, context) => {
|
|
156
|
+
const input = {
|
|
157
|
+
collateralIndex: trade.collateralIndex,
|
|
158
|
+
trader: trade.user,
|
|
159
|
+
pairIndex: trade.pairIndex,
|
|
160
|
+
index: trade.index,
|
|
161
|
+
openPrice: trade.openPrice,
|
|
162
|
+
long: trade.long,
|
|
163
|
+
collateral: trade.collateralAmount,
|
|
164
|
+
leverage: trade.leverage,
|
|
165
|
+
additionalFeeCollateral,
|
|
166
|
+
liquidationParams: context.liquidationParams || {
|
|
167
|
+
maxLiqSpreadP: 0,
|
|
168
|
+
startLiqThresholdP: 0.9,
|
|
169
|
+
endLiqThresholdP: 0.9,
|
|
170
|
+
startLeverage: 0,
|
|
171
|
+
endLeverage: 0,
|
|
172
|
+
},
|
|
173
|
+
currentPairPrice,
|
|
174
|
+
isCounterTrade: trade.isCounterTrade || false,
|
|
175
|
+
partialCloseMultiplier: 1,
|
|
176
|
+
beforeOpened: false,
|
|
177
|
+
};
|
|
178
|
+
return (0, exports.getTradeLiquidationPrice)(input, context);
|
|
179
|
+
};
|
|
180
|
+
exports.getTradeLiquidationPriceSimple = getTradeLiquidationPriceSimple;
|
|
181
|
+
// Converters
|
|
182
|
+
var converter_1 = require("./converter");
|
|
183
|
+
Object.defineProperty(exports, "convertLiquidationParams", { enumerable: true, get: function () { return converter_1.convertLiquidationParams; } });
|
|
184
|
+
Object.defineProperty(exports, "convertLiquidationParamsArray", { enumerable: true, get: function () { return converter_1.convertLiquidationParamsArray; } });
|
|
185
|
+
Object.defineProperty(exports, "encodeLiquidationParams", { enumerable: true, get: function () { return converter_1.encodeLiquidationParams; } });
|
|
186
|
+
// Types
|
|
187
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { GetBorrowingFeeContext, BorrowingFee } from "./../fees";
|
|
2
|
+
import { GetLiquidationFeesContext } from "./../fees/trading";
|
|
3
|
+
import { BorrowingFeeV2 } from "./../fees/borrowingV2";
|
|
4
|
+
import { LiquidationParams, UserPriceImpact, TradeFeesData } from "./../types";
|
|
5
|
+
import { ContractsVersion } from "../../contracts/types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Input parameters for getTradeLiquidationPrice
|
|
8
|
+
* @dev Mirrors the contract's LiqPriceInput struct
|
|
9
|
+
*/
|
|
10
|
+
export type LiqPriceInput = {
|
|
11
|
+
collateralIndex: number;
|
|
12
|
+
trader: string;
|
|
13
|
+
pairIndex: number;
|
|
14
|
+
index: number;
|
|
15
|
+
openPrice: number;
|
|
16
|
+
long: boolean;
|
|
17
|
+
collateral: number;
|
|
18
|
+
leverage: number;
|
|
19
|
+
additionalFeeCollateral: number;
|
|
20
|
+
liquidationParams: LiquidationParams;
|
|
21
|
+
currentPairPrice: number;
|
|
22
|
+
isCounterTrade: boolean;
|
|
23
|
+
partialCloseMultiplier: number;
|
|
24
|
+
beforeOpened: boolean;
|
|
25
|
+
};
|
|
26
|
+
export type GetLiquidationPriceContext = GetBorrowingFeeContext & BorrowingFeeV2.GetBorrowingFeeV2Context & GetLiquidationFeesContext & {
|
|
27
|
+
liquidationParams: LiquidationParams | undefined;
|
|
28
|
+
pairSpreadP: number | undefined;
|
|
29
|
+
collateralPriceUsd: number | undefined;
|
|
30
|
+
contractsVersion: ContractsVersion | undefined;
|
|
31
|
+
userPriceImpact?: UserPriceImpact | undefined;
|
|
32
|
+
currentPairPrice?: number;
|
|
33
|
+
isCounterTrade?: boolean;
|
|
34
|
+
tradeFeesData?: TradeFeesData;
|
|
35
|
+
partialCloseMultiplier?: number;
|
|
36
|
+
additionalFeeCollateral?: number;
|
|
37
|
+
beforeOpened?: boolean;
|
|
38
|
+
initialAccFees?: BorrowingFee.InitialAccFees;
|
|
39
|
+
fundingParams?: Record<number, Record<number, any>>;
|
|
40
|
+
fundingData?: Record<number, Record<number, any>>;
|
|
41
|
+
pairOiAfterV10?: Record<number, Record<number, any>>;
|
|
42
|
+
netExposureToken?: Record<number, Record<number, number>>;
|
|
43
|
+
netExposureUsd?: Record<number, Record<number, number>>;
|
|
44
|
+
};
|
|
@@ -1,42 +1,12 @@
|
|
|
1
1
|
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
|
-
import {
|
|
3
|
-
import { BorrowingFeeV2 } from "./fees/borrowingV2";
|
|
4
|
-
import { Fee, LiquidationParams, Trade, TradeInfo, UserPriceImpact, TradeFeesData } from "./types";
|
|
2
|
+
import { Fee, LiquidationParams, Trade, UserPriceImpact } from "./types";
|
|
5
3
|
import { ContractsVersion } from "../contracts/types";
|
|
6
|
-
export type GetLiquidationPriceContext = GetBorrowingFeeContext &
|
|
4
|
+
export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
|
|
7
5
|
liquidationParams: LiquidationParams | undefined;
|
|
8
6
|
pairSpreadP: number | undefined;
|
|
9
7
|
collateralPriceUsd: number | undefined;
|
|
10
8
|
contractsVersion: ContractsVersion | undefined;
|
|
11
9
|
userPriceImpact?: UserPriceImpact | undefined;
|
|
12
|
-
currentPairPrice?: number;
|
|
13
|
-
isCounterTrade?: boolean;
|
|
14
|
-
tradeFeesData?: TradeFeesData;
|
|
15
|
-
partialCloseMultiplier?: number;
|
|
16
|
-
additionalFeeCollateral?: number;
|
|
17
|
-
beforeOpened?: boolean;
|
|
18
|
-
initialAccFees?: BorrowingFee.InitialAccFees;
|
|
19
|
-
fundingParams?: Record<number, Record<number, any>>;
|
|
20
|
-
fundingData?: Record<number, Record<number, any>>;
|
|
21
|
-
pairOiAfterV10?: Record<number, Record<number, any>>;
|
|
22
|
-
netExposureToken?: Record<number, Record<number, number>>;
|
|
23
|
-
netExposureUsd?: Record<number, Record<number, number>>;
|
|
24
10
|
};
|
|
25
|
-
export type TradeHoldingFees = {
|
|
26
|
-
fundingFeeCollateral: number;
|
|
27
|
-
borrowingFeeCollateral: number;
|
|
28
|
-
borrowingFeeCollateral_old: number;
|
|
29
|
-
totalFeeCollateral: number;
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* @dev Calculates total holding fees for a trade (funding + borrowing fees)
|
|
33
|
-
* @param trade The trade to calculate fees for
|
|
34
|
-
* @param tradeInfo Trade info containing contracts version
|
|
35
|
-
* @param tradeFeesData Trade fees data containing initial acc fees
|
|
36
|
-
* @param currentPairPrice Current pair price
|
|
37
|
-
* @param context Context with fee parameters
|
|
38
|
-
* @returns Object containing all holding fee components
|
|
39
|
-
*/
|
|
40
|
-
export declare const getTradePendingHoldingFeesCollateral: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: GetLiquidationPriceContext) => TradeHoldingFees;
|
|
41
11
|
export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
|
|
42
12
|
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|
package/lib/trade/liquidation.js
CHANGED
|
@@ -1,112 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLiqPnlThresholdP = exports.getLiquidationPrice =
|
|
3
|
+
exports.getLiqPnlThresholdP = exports.getLiquidationPrice = void 0;
|
|
4
4
|
const fees_1 = require("./fees");
|
|
5
|
-
const trading_1 = require("./fees/trading");
|
|
6
|
-
const borrowingV2_1 = require("./fees/borrowingV2");
|
|
7
5
|
const spread_1 = require("./spread");
|
|
8
6
|
const types_1 = require("../contracts/types");
|
|
9
|
-
/**
|
|
10
|
-
* @dev Calculates total holding fees for a trade (funding + borrowing fees)
|
|
11
|
-
* @param trade The trade to calculate fees for
|
|
12
|
-
* @param tradeInfo Trade info containing contracts version
|
|
13
|
-
* @param tradeFeesData Trade fees data containing initial acc fees
|
|
14
|
-
* @param currentPairPrice Current pair price
|
|
15
|
-
* @param context Context with fee parameters
|
|
16
|
-
* @returns Object containing all holding fee components
|
|
17
|
-
*/
|
|
18
|
-
const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, currentPairPrice, context) => {
|
|
19
|
-
var _a;
|
|
20
|
-
// Calculate funding fees (v10+ only)
|
|
21
|
-
const fundingFeeCollateral = ((_a = context.contractsVersion) !== null && _a !== void 0 ? _a : tradeInfo.contractsVersion) >=
|
|
22
|
-
types_1.ContractsVersion.V10
|
|
23
|
-
? (0, fees_1.getTradeFundingFeesCollateral)(trade, tradeInfo, tradeFeesData, currentPairPrice, context)
|
|
24
|
-
: 0;
|
|
25
|
-
// Calculate borrowing fees v2
|
|
26
|
-
const borrowingFeeCollateral = (0, borrowingV2_1.getTradeBorrowingFeesCollateral)({
|
|
27
|
-
positionSizeCollateral: trade.collateralAmount * trade.leverage,
|
|
28
|
-
openPrice: trade.openPrice,
|
|
29
|
-
collateralIndex: trade.collateralIndex,
|
|
30
|
-
pairIndex: trade.pairIndex,
|
|
31
|
-
currentPairPrice,
|
|
32
|
-
initialAccBorrowingFeeP: tradeFeesData.initialAccBorrowingFeeP,
|
|
33
|
-
currentTimestamp: context.currentTimestamp,
|
|
34
|
-
}, context);
|
|
35
|
-
// Calculate v1 borrowing fees (some markets use v1 indefinitely)
|
|
36
|
-
const borrowingFeeCollateral_old = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, context.initialAccFees || { accPairFee: 0, accGroupFee: 0, block: 0 }, // Use context initial fees or empty
|
|
37
|
-
context);
|
|
38
|
-
return {
|
|
39
|
-
fundingFeeCollateral,
|
|
40
|
-
borrowingFeeCollateral,
|
|
41
|
-
borrowingFeeCollateral_old,
|
|
42
|
-
totalFeeCollateral: fundingFeeCollateral +
|
|
43
|
-
borrowingFeeCollateral +
|
|
44
|
-
borrowingFeeCollateral_old,
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
exports.getTradePendingHoldingFeesCollateral = getTradePendingHoldingFeesCollateral;
|
|
48
7
|
const getLiquidationPrice = (trade, fee, initialAccFees, context) => {
|
|
49
|
-
var _a, _b
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
context = Object.assign(Object.assign({}, context), { initialAccFees });
|
|
53
|
-
}
|
|
54
|
-
// 1. Calculate liquidation fees
|
|
55
|
-
const closingFee = (0, trading_1.getTotalTradeLiqFeesCollateral)(0, // collateralIndex not used in calculation
|
|
56
|
-
trade.user, trade.pairIndex, trade.collateralAmount, context);
|
|
57
|
-
// 2. Calculate holding fees and realized PnL
|
|
58
|
-
let holdingFeesTotal = 0;
|
|
59
|
-
let totalRealizedPnlCollateral = 0;
|
|
60
|
-
if (!context.beforeOpened &&
|
|
61
|
-
context.tradeFeesData &&
|
|
62
|
-
context.currentPairPrice) {
|
|
63
|
-
// V10 data available - calculate full holding fees
|
|
64
|
-
// Create a minimal tradeInfo from context
|
|
65
|
-
const tradeInfo = {
|
|
66
|
-
contractsVersion: (_a = context.contractsVersion) !== null && _a !== void 0 ? _a : types_1.ContractsVersion.V10,
|
|
67
|
-
createdBlock: 0,
|
|
68
|
-
tpLastUpdatedBlock: 0,
|
|
69
|
-
slLastUpdatedBlock: 0,
|
|
70
|
-
maxSlippageP: 0,
|
|
71
|
-
lastOiUpdateTs: 0,
|
|
72
|
-
collateralPriceUsd: (_b = context.collateralPriceUsd) !== null && _b !== void 0 ? _b : 0,
|
|
73
|
-
lastPosIncreaseBlock: 0,
|
|
74
|
-
};
|
|
75
|
-
const holdingFees = (0, exports.getTradePendingHoldingFeesCollateral)(trade, tradeInfo, context.tradeFeesData, context.currentPairPrice, context);
|
|
76
|
-
holdingFeesTotal = holdingFees.totalFeeCollateral;
|
|
77
|
-
// Calculate total realized PnL (realized PnL minus realized trading fees)
|
|
78
|
-
totalRealizedPnlCollateral =
|
|
79
|
-
context.tradeFeesData.realizedPnlCollateral -
|
|
80
|
-
context.tradeFeesData.realizedTradingFeesCollateral;
|
|
81
|
-
}
|
|
82
|
-
else if (!context.beforeOpened) {
|
|
83
|
-
// Markets using v1 borrowing fees model
|
|
84
|
-
holdingFeesTotal = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
85
|
-
}
|
|
86
|
-
// 3. Apply unified formula for all trades
|
|
87
|
-
const partialCloseMultiplier = (_c = context.partialCloseMultiplier) !== null && _c !== void 0 ? _c : 1;
|
|
88
|
-
const additionalFeeCollateral = (_d = context.additionalFeeCollateral) !== null && _d !== void 0 ? _d : 0;
|
|
89
|
-
const totalFeesCollateral = closingFee +
|
|
90
|
-
(holdingFeesTotal - totalRealizedPnlCollateral) * partialCloseMultiplier +
|
|
91
|
-
additionalFeeCollateral;
|
|
92
|
-
// 4. Calculate liquidation threshold
|
|
8
|
+
var _a, _b;
|
|
9
|
+
const closingFee = (0, fees_1.getClosingFee)(trade.collateralAmount, trade.leverage, trade.pairIndex, fee, context.collateralPriceUsd);
|
|
10
|
+
const borrowingFee = (0, fees_1.getBorrowingFee)(trade.collateralAmount * trade.leverage, trade.pairIndex, trade.long, initialAccFees, context);
|
|
93
11
|
const liqThresholdP = (0, exports.getLiqPnlThresholdP)(context.liquidationParams, trade.leverage);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
let liqPriceDistance = (trade.openPrice * (collateralLiqNegativePnl - totalFeesCollateral)) /
|
|
12
|
+
let liqPriceDistance = (trade.openPrice *
|
|
13
|
+
(trade.collateralAmount * liqThresholdP - (borrowingFee + closingFee))) /
|
|
97
14
|
trade.collateralAmount /
|
|
98
15
|
trade.leverage;
|
|
99
|
-
// 6. Apply closing spread for v9.2+
|
|
100
16
|
if ((context === null || context === void 0 ? void 0 : context.contractsVersion) !== undefined &&
|
|
101
17
|
context.contractsVersion >= types_1.ContractsVersion.V9_2 &&
|
|
102
|
-
((((
|
|
18
|
+
((((_a = context === null || context === void 0 ? void 0 : context.liquidationParams) === null || _a === void 0 ? void 0 : _a.maxLiqSpreadP) !== undefined &&
|
|
103
19
|
context.liquidationParams.maxLiqSpreadP > 0) ||
|
|
104
|
-
(((
|
|
20
|
+
(((_b = context === null || context === void 0 ? void 0 : context.userPriceImpact) === null || _b === void 0 ? void 0 : _b.fixedSpreadP) !== undefined &&
|
|
105
21
|
context.userPriceImpact.fixedSpreadP > 0))) {
|
|
106
22
|
const closingSpreadP = (0, spread_1.getSpreadP)(context.pairSpreadP, true, context.liquidationParams, context.userPriceImpact);
|
|
107
23
|
liqPriceDistance -= trade.openPrice * closingSpreadP;
|
|
108
24
|
}
|
|
109
|
-
// 7. Calculate final liquidation price
|
|
110
25
|
return trade.long
|
|
111
26
|
? Math.max(trade.openPrice - liqPriceDistance, 0)
|
|
112
27
|
: Math.max(trade.openPrice + liqPriceDistance, 0);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Converters for PnL data between contract and SDK formats
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @dev Convert PnL percentage from contract precision to SDK format
|
|
6
|
+
* @param pnlPercentContract PnL percentage with 1e10 precision
|
|
7
|
+
* @returns PnL percentage as regular number (e.g., 10 = 10%)
|
|
8
|
+
*/
|
|
9
|
+
export declare const convertPnlPercent: (pnlPercentContract: bigint | number) => number;
|
|
10
|
+
/**
|
|
11
|
+
* @dev Convert PnL percentage from SDK format to contract precision
|
|
12
|
+
* @param pnlPercent PnL percentage as regular number
|
|
13
|
+
* @returns PnL percentage with 1e10 precision
|
|
14
|
+
*/
|
|
15
|
+
export declare const encodePnlPercent: (pnlPercent: number) => bigint;
|
|
16
|
+
/**
|
|
17
|
+
* @dev Convert collateral amount considering precision
|
|
18
|
+
* @param amount Amount in contract format
|
|
19
|
+
* @param collateralDecimals Collateral token decimals (6 or 18)
|
|
20
|
+
* @returns Amount as SDK float
|
|
21
|
+
*/
|
|
22
|
+
export declare const convertCollateralAmount: (amount: bigint | number, collateralDecimals: number) => number;
|
|
23
|
+
/**
|
|
24
|
+
* @dev Convert price from contract format to SDK format
|
|
25
|
+
* @param price Price with 1e10 precision
|
|
26
|
+
* @returns Price as SDK float
|
|
27
|
+
*/
|
|
28
|
+
export declare const convertPrice: (price: bigint | number) => number;
|
|
29
|
+
/**
|
|
30
|
+
* @dev Convert leverage from contract format to SDK format
|
|
31
|
+
* @param leverage Leverage with 1e3 precision
|
|
32
|
+
* @returns Leverage as SDK float (e.g., 10 = 10x)
|
|
33
|
+
*/
|
|
34
|
+
export declare const convertLeverage: (leverage: bigint | number) => number;
|
|
35
|
+
/**
|
|
36
|
+
* @dev Batch convert PnL results from contract format
|
|
37
|
+
* @param results Array of PnL results from contract
|
|
38
|
+
* @param collateralDecimals Collateral token decimals
|
|
39
|
+
* @returns Array of converted PnL results
|
|
40
|
+
*/
|
|
41
|
+
export declare const convertPnlResults: (results: Array<{
|
|
42
|
+
pnlCollateral: bigint;
|
|
43
|
+
pnlPercent: bigint;
|
|
44
|
+
}>, collateralDecimals: number) => Array<{
|
|
45
|
+
pnlCollateral: number;
|
|
46
|
+
pnlPercent: number;
|
|
47
|
+
}>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Converters for PnL data between contract and SDK formats
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.convertPnlResults = exports.convertLeverage = exports.convertPrice = exports.convertCollateralAmount = exports.encodePnlPercent = exports.convertPnlPercent = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Convert PnL percentage from contract precision to SDK format
|
|
9
|
+
* @param pnlPercentContract PnL percentage with 1e10 precision
|
|
10
|
+
* @returns PnL percentage as regular number (e.g., 10 = 10%)
|
|
11
|
+
*/
|
|
12
|
+
const convertPnlPercent = (pnlPercentContract) => {
|
|
13
|
+
const value = typeof pnlPercentContract === "bigint"
|
|
14
|
+
? Number(pnlPercentContract)
|
|
15
|
+
: pnlPercentContract;
|
|
16
|
+
// Contract uses 1e10 precision for percentages
|
|
17
|
+
return value / 1e10;
|
|
18
|
+
};
|
|
19
|
+
exports.convertPnlPercent = convertPnlPercent;
|
|
20
|
+
/**
|
|
21
|
+
* @dev Convert PnL percentage from SDK format to contract precision
|
|
22
|
+
* @param pnlPercent PnL percentage as regular number
|
|
23
|
+
* @returns PnL percentage with 1e10 precision
|
|
24
|
+
*/
|
|
25
|
+
const encodePnlPercent = (pnlPercent) => {
|
|
26
|
+
return BigInt(Math.round(pnlPercent * 1e10));
|
|
27
|
+
};
|
|
28
|
+
exports.encodePnlPercent = encodePnlPercent;
|
|
29
|
+
/**
|
|
30
|
+
* @dev Convert collateral amount considering precision
|
|
31
|
+
* @param amount Amount in contract format
|
|
32
|
+
* @param collateralDecimals Collateral token decimals (6 or 18)
|
|
33
|
+
* @returns Amount as SDK float
|
|
34
|
+
*/
|
|
35
|
+
const convertCollateralAmount = (amount, collateralDecimals) => {
|
|
36
|
+
const value = typeof amount === "bigint" ? Number(amount) : amount;
|
|
37
|
+
return value / Math.pow(10, collateralDecimals);
|
|
38
|
+
};
|
|
39
|
+
exports.convertCollateralAmount = convertCollateralAmount;
|
|
40
|
+
/**
|
|
41
|
+
* @dev Convert price from contract format to SDK format
|
|
42
|
+
* @param price Price with 1e10 precision
|
|
43
|
+
* @returns Price as SDK float
|
|
44
|
+
*/
|
|
45
|
+
const convertPrice = (price) => {
|
|
46
|
+
const value = typeof price === "bigint" ? Number(price) : price;
|
|
47
|
+
return value / 1e10;
|
|
48
|
+
};
|
|
49
|
+
exports.convertPrice = convertPrice;
|
|
50
|
+
/**
|
|
51
|
+
* @dev Convert leverage from contract format to SDK format
|
|
52
|
+
* @param leverage Leverage with 1e3 precision
|
|
53
|
+
* @returns Leverage as SDK float (e.g., 10 = 10x)
|
|
54
|
+
*/
|
|
55
|
+
const convertLeverage = (leverage) => {
|
|
56
|
+
const value = typeof leverage === "bigint" ? Number(leverage) : leverage;
|
|
57
|
+
return value / 1e3;
|
|
58
|
+
};
|
|
59
|
+
exports.convertLeverage = convertLeverage;
|
|
60
|
+
/**
|
|
61
|
+
* @dev Batch convert PnL results from contract format
|
|
62
|
+
* @param results Array of PnL results from contract
|
|
63
|
+
* @param collateralDecimals Collateral token decimals
|
|
64
|
+
* @returns Array of converted PnL results
|
|
65
|
+
*/
|
|
66
|
+
const convertPnlResults = (results, collateralDecimals) => {
|
|
67
|
+
return results.map(result => ({
|
|
68
|
+
pnlCollateral: (0, exports.convertCollateralAmount)(result.pnlCollateral, collateralDecimals),
|
|
69
|
+
pnlPercent: (0, exports.convertPnlPercent)(result.pnlPercent),
|
|
70
|
+
}));
|
|
71
|
+
};
|
|
72
|
+
exports.convertPnlResults = convertPnlResults;
|