@metamask-previews/perps-controller 0.0.0-preview-e4aa1532 → 0.0.0-preview-6dfa4aeba
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/CHANGELOG.md +1 -11
- package/dist/PerpsController.cjs +66 -2224
- package/dist/PerpsController.cjs.map +1 -1
- package/dist/PerpsController.d.cts +79 -933
- package/dist/PerpsController.d.cts.map +1 -1
- package/dist/PerpsController.d.mts +79 -933
- package/dist/PerpsController.d.mts.map +1 -1
- package/dist/PerpsController.mjs +64 -2221
- package/dist/PerpsController.mjs.map +1 -1
- package/dist/index.cjs +1 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -6
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -6
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -19
- package/dist/__mocks__/hyperliquidMock.cjs +0 -68
- package/dist/__mocks__/hyperliquidMock.cjs.map +0 -1
- package/dist/__mocks__/hyperliquidMock.d.cts +0 -15
- package/dist/__mocks__/hyperliquidMock.d.cts.map +0 -1
- package/dist/__mocks__/hyperliquidMock.d.mts +0 -15
- package/dist/__mocks__/hyperliquidMock.d.mts.map +0 -1
- package/dist/__mocks__/hyperliquidMock.mjs +0 -65
- package/dist/__mocks__/hyperliquidMock.mjs.map +0 -1
- package/dist/__mocks__/providerMocks.cjs +0 -144
- package/dist/__mocks__/providerMocks.cjs.map +0 -1
- package/dist/__mocks__/providerMocks.d.cts +0 -44
- package/dist/__mocks__/providerMocks.d.cts.map +0 -1
- package/dist/__mocks__/providerMocks.d.mts +0 -44
- package/dist/__mocks__/providerMocks.d.mts.map +0 -1
- package/dist/__mocks__/providerMocks.mjs +0 -136
- package/dist/__mocks__/providerMocks.mjs.map +0 -1
- package/dist/__mocks__/serviceMocks.cjs +0 -187
- package/dist/__mocks__/serviceMocks.cjs.map +0 -1
- package/dist/__mocks__/serviceMocks.d.cts +0 -58
- package/dist/__mocks__/serviceMocks.d.cts.map +0 -1
- package/dist/__mocks__/serviceMocks.d.mts +0 -58
- package/dist/__mocks__/serviceMocks.d.mts.map +0 -1
- package/dist/__mocks__/serviceMocks.mjs +0 -180
- package/dist/__mocks__/serviceMocks.mjs.map +0 -1
- package/dist/aggregation/SubscriptionMultiplexer.cjs +0 -459
- package/dist/aggregation/SubscriptionMultiplexer.cjs.map +0 -1
- package/dist/aggregation/SubscriptionMultiplexer.d.cts +0 -222
- package/dist/aggregation/SubscriptionMultiplexer.d.cts.map +0 -1
- package/dist/aggregation/SubscriptionMultiplexer.d.mts +0 -222
- package/dist/aggregation/SubscriptionMultiplexer.d.mts.map +0 -1
- package/dist/aggregation/SubscriptionMultiplexer.mjs +0 -455
- package/dist/aggregation/SubscriptionMultiplexer.mjs.map +0 -1
- package/dist/aggregation/index.cjs +0 -9
- package/dist/aggregation/index.cjs.map +0 -1
- package/dist/aggregation/index.d.cts +0 -6
- package/dist/aggregation/index.d.cts.map +0 -1
- package/dist/aggregation/index.d.mts +0 -6
- package/dist/aggregation/index.d.mts.map +0 -1
- package/dist/aggregation/index.mjs +0 -5
- package/dist/aggregation/index.mjs.map +0 -1
- package/dist/constants/eventNames.cjs +0 -117
- package/dist/constants/eventNames.cjs.map +0 -1
- package/dist/constants/eventNames.d.cts +0 -106
- package/dist/constants/eventNames.d.cts.map +0 -1
- package/dist/constants/eventNames.d.mts +0 -106
- package/dist/constants/eventNames.d.mts.map +0 -1
- package/dist/constants/eventNames.mjs +0 -114
- package/dist/constants/eventNames.mjs.map +0 -1
- package/dist/constants/hyperLiquidConfig.cjs +0 -366
- package/dist/constants/hyperLiquidConfig.cjs.map +0 -1
- package/dist/constants/hyperLiquidConfig.d.cts +0 -276
- package/dist/constants/hyperLiquidConfig.d.cts.map +0 -1
- package/dist/constants/hyperLiquidConfig.d.mts +0 -276
- package/dist/constants/hyperLiquidConfig.d.mts.map +0 -1
- package/dist/constants/hyperLiquidConfig.mjs +0 -357
- package/dist/constants/hyperLiquidConfig.mjs.map +0 -1
- package/dist/constants/index.cjs +0 -23
- package/dist/constants/index.cjs.map +0 -1
- package/dist/constants/index.d.cts +0 -7
- package/dist/constants/index.d.cts.map +0 -1
- package/dist/constants/index.d.mts +0 -7
- package/dist/constants/index.d.mts.map +0 -1
- package/dist/constants/index.mjs +0 -7
- package/dist/constants/index.mjs.map +0 -1
- package/dist/constants/orderTypes.cjs +0 -31
- package/dist/constants/orderTypes.cjs.map +0 -1
- package/dist/constants/orderTypes.d.cts +0 -19
- package/dist/constants/orderTypes.d.cts.map +0 -1
- package/dist/constants/orderTypes.d.mts +0 -19
- package/dist/constants/orderTypes.d.mts.map +0 -1
- package/dist/constants/orderTypes.mjs +0 -27
- package/dist/constants/orderTypes.mjs.map +0 -1
- package/dist/constants/performanceMetrics.cjs +0 -61
- package/dist/constants/performanceMetrics.cjs.map +0 -1
- package/dist/constants/performanceMetrics.d.cts +0 -44
- package/dist/constants/performanceMetrics.d.cts.map +0 -1
- package/dist/constants/performanceMetrics.d.mts +0 -44
- package/dist/constants/performanceMetrics.d.mts.map +0 -1
- package/dist/constants/performanceMetrics.mjs +0 -58
- package/dist/constants/performanceMetrics.mjs.map +0 -1
- package/dist/constants/perpsConfig.cjs +0 -461
- package/dist/constants/perpsConfig.cjs.map +0 -1
- package/dist/constants/perpsConfig.d.cts +0 -343
- package/dist/constants/perpsConfig.d.cts.map +0 -1
- package/dist/constants/perpsConfig.d.mts +0 -343
- package/dist/constants/perpsConfig.d.mts.map +0 -1
- package/dist/constants/perpsConfig.mjs +0 -458
- package/dist/constants/perpsConfig.mjs.map +0 -1
- package/dist/constants/perpsErrorCodes.cjs +0 -79
- package/dist/constants/perpsErrorCodes.cjs.map +0 -1
- package/dist/constants/perpsErrorCodes.d.cts +0 -64
- package/dist/constants/perpsErrorCodes.d.cts.map +0 -1
- package/dist/constants/perpsErrorCodes.d.mts +0 -64
- package/dist/constants/perpsErrorCodes.d.mts.map +0 -1
- package/dist/constants/perpsErrorCodes.mjs +0 -76
- package/dist/constants/perpsErrorCodes.mjs.map +0 -1
- package/dist/platform-services/HyperLiquidClientService.cjs +0 -857
- package/dist/platform-services/HyperLiquidClientService.cjs.map +0 -1
- package/dist/platform-services/HyperLiquidClientService.d.cts +0 -263
- package/dist/platform-services/HyperLiquidClientService.d.cts.map +0 -1
- package/dist/platform-services/HyperLiquidClientService.d.mts +0 -263
- package/dist/platform-services/HyperLiquidClientService.d.mts.map +0 -1
- package/dist/platform-services/HyperLiquidClientService.mjs +0 -853
- package/dist/platform-services/HyperLiquidClientService.mjs.map +0 -1
- package/dist/platform-services/HyperLiquidSubscriptionService.cjs +0 -2170
- package/dist/platform-services/HyperLiquidSubscriptionService.cjs.map +0 -1
- package/dist/platform-services/HyperLiquidSubscriptionService.d.cts +0 -424
- package/dist/platform-services/HyperLiquidSubscriptionService.d.cts.map +0 -1
- package/dist/platform-services/HyperLiquidSubscriptionService.d.mts +0 -424
- package/dist/platform-services/HyperLiquidSubscriptionService.d.mts.map +0 -1
- package/dist/platform-services/HyperLiquidSubscriptionService.mjs +0 -2166
- package/dist/platform-services/HyperLiquidSubscriptionService.mjs.map +0 -1
- package/dist/platform-services/HyperLiquidWalletService.cjs +0 -140
- package/dist/platform-services/HyperLiquidWalletService.cjs.map +0 -1
- package/dist/platform-services/HyperLiquidWalletService.d.cts +0 -78
- package/dist/platform-services/HyperLiquidWalletService.d.cts.map +0 -1
- package/dist/platform-services/HyperLiquidWalletService.d.mts +0 -78
- package/dist/platform-services/HyperLiquidWalletService.d.mts.map +0 -1
- package/dist/platform-services/HyperLiquidWalletService.mjs +0 -136
- package/dist/platform-services/HyperLiquidWalletService.mjs.map +0 -1
- package/dist/providers/AggregatedPerpsProvider.cjs +0 -777
- package/dist/providers/AggregatedPerpsProvider.cjs.map +0 -1
- package/dist/providers/AggregatedPerpsProvider.d.cts +0 -466
- package/dist/providers/AggregatedPerpsProvider.d.cts.map +0 -1
- package/dist/providers/AggregatedPerpsProvider.d.mts +0 -466
- package/dist/providers/AggregatedPerpsProvider.d.mts.map +0 -1
- package/dist/providers/AggregatedPerpsProvider.mjs +0 -773
- package/dist/providers/AggregatedPerpsProvider.mjs.map +0 -1
- package/dist/providers/HyperLiquidProvider.cjs +0 -5172
- package/dist/providers/HyperLiquidProvider.cjs.map +0 -1
- package/dist/providers/HyperLiquidProvider.d.cts +0 -881
- package/dist/providers/HyperLiquidProvider.d.cts.map +0 -1
- package/dist/providers/HyperLiquidProvider.d.mts +0 -881
- package/dist/providers/HyperLiquidProvider.d.mts.map +0 -1
- package/dist/providers/HyperLiquidProvider.mjs +0 -5168
- package/dist/providers/HyperLiquidProvider.mjs.map +0 -1
- package/dist/providers/index.cjs +0 -11
- package/dist/providers/index.cjs.map +0 -1
- package/dist/providers/index.d.cts +0 -6
- package/dist/providers/index.d.cts.map +0 -1
- package/dist/providers/index.d.mts +0 -6
- package/dist/providers/index.d.mts.map +0 -1
- package/dist/providers/index.mjs +0 -6
- package/dist/providers/index.mjs.map +0 -1
- package/dist/routing/ProviderRouter.cjs +0 -148
- package/dist/routing/ProviderRouter.cjs.map +0 -1
- package/dist/routing/ProviderRouter.d.cts +0 -124
- package/dist/routing/ProviderRouter.d.cts.map +0 -1
- package/dist/routing/ProviderRouter.d.mts +0 -124
- package/dist/routing/ProviderRouter.d.mts.map +0 -1
- package/dist/routing/ProviderRouter.mjs +0 -144
- package/dist/routing/ProviderRouter.mjs.map +0 -1
- package/dist/routing/index.cjs +0 -9
- package/dist/routing/index.cjs.map +0 -1
- package/dist/routing/index.d.cts +0 -6
- package/dist/routing/index.d.cts.map +0 -1
- package/dist/routing/index.d.mts +0 -6
- package/dist/routing/index.d.mts.map +0 -1
- package/dist/routing/index.mjs +0 -5
- package/dist/routing/index.mjs.map +0 -1
- package/dist/selectors.cjs +0 -161
- package/dist/selectors.cjs.map +0 -1
- package/dist/selectors.d.cts +0 -345
- package/dist/selectors.d.cts.map +0 -1
- package/dist/selectors.d.mts +0 -345
- package/dist/selectors.d.mts.map +0 -1
- package/dist/selectors.mjs +0 -153
- package/dist/selectors.mjs.map +0 -1
- package/dist/services/AccountService.cjs +0 -313
- package/dist/services/AccountService.cjs.map +0 -1
- package/dist/services/AccountService.d.cts +0 -53
- package/dist/services/AccountService.d.cts.map +0 -1
- package/dist/services/AccountService.d.mts +0 -53
- package/dist/services/AccountService.d.mts.map +0 -1
- package/dist/services/AccountService.mjs +0 -309
- package/dist/services/AccountService.mjs.map +0 -1
- package/dist/services/DataLakeService.cjs +0 -219
- package/dist/services/DataLakeService.cjs.map +0 -1
- package/dist/services/DataLakeService.d.cts +0 -49
- package/dist/services/DataLakeService.d.cts.map +0 -1
- package/dist/services/DataLakeService.d.mts +0 -49
- package/dist/services/DataLakeService.d.mts.map +0 -1
- package/dist/services/DataLakeService.mjs +0 -215
- package/dist/services/DataLakeService.mjs.map +0 -1
- package/dist/services/DepositService.cjs +0 -96
- package/dist/services/DepositService.cjs.map +0 -1
- package/dist/services/DepositService.d.cts +0 -37
- package/dist/services/DepositService.d.cts.map +0 -1
- package/dist/services/DepositService.d.mts +0 -37
- package/dist/services/DepositService.d.mts.map +0 -1
- package/dist/services/DepositService.mjs +0 -92
- package/dist/services/DepositService.mjs.map +0 -1
- package/dist/services/EligibilityService.cjs +0 -156
- package/dist/services/EligibilityService.cjs.map +0 -1
- package/dist/services/EligibilityService.d.cts +0 -48
- package/dist/services/EligibilityService.d.cts.map +0 -1
- package/dist/services/EligibilityService.d.mts +0 -48
- package/dist/services/EligibilityService.d.mts.map +0 -1
- package/dist/services/EligibilityService.mjs +0 -152
- package/dist/services/EligibilityService.mjs.map +0 -1
- package/dist/services/FeatureFlagConfigurationService.cjs +0 -258
- package/dist/services/FeatureFlagConfigurationService.cjs.map +0 -1
- package/dist/services/FeatureFlagConfigurationService.d.cts +0 -100
- package/dist/services/FeatureFlagConfigurationService.d.cts.map +0 -1
- package/dist/services/FeatureFlagConfigurationService.d.mts +0 -100
- package/dist/services/FeatureFlagConfigurationService.d.mts.map +0 -1
- package/dist/services/FeatureFlagConfigurationService.mjs +0 -254
- package/dist/services/FeatureFlagConfigurationService.mjs.map +0 -1
- package/dist/services/MarketDataService.cjs +0 -875
- package/dist/services/MarketDataService.cjs.map +0 -1
- package/dist/services/MarketDataService.d.cts +0 -289
- package/dist/services/MarketDataService.d.cts.map +0 -1
- package/dist/services/MarketDataService.d.mts +0 -289
- package/dist/services/MarketDataService.d.mts.map +0 -1
- package/dist/services/MarketDataService.mjs +0 -871
- package/dist/services/MarketDataService.mjs.map +0 -1
- package/dist/services/RewardsIntegrationService.cjs +0 -94
- package/dist/services/RewardsIntegrationService.cjs.map +0 -1
- package/dist/services/RewardsIntegrationService.d.cts +0 -30
- package/dist/services/RewardsIntegrationService.d.cts.map +0 -1
- package/dist/services/RewardsIntegrationService.d.mts +0 -30
- package/dist/services/RewardsIntegrationService.d.mts.map +0 -1
- package/dist/services/RewardsIntegrationService.mjs +0 -90
- package/dist/services/RewardsIntegrationService.mjs.map +0 -1
- package/dist/services/ServiceContext.cjs +0 -3
- package/dist/services/ServiceContext.cjs.map +0 -1
- package/dist/services/ServiceContext.d.cts +0 -110
- package/dist/services/ServiceContext.d.cts.map +0 -1
- package/dist/services/ServiceContext.d.mts +0 -110
- package/dist/services/ServiceContext.d.mts.map +0 -1
- package/dist/services/ServiceContext.mjs +0 -2
- package/dist/services/ServiceContext.mjs.map +0 -1
- package/dist/services/TradingService.cjs +0 -1481
- package/dist/services/TradingService.cjs.map +0 -1
- package/dist/services/TradingService.d.cts +0 -308
- package/dist/services/TradingService.d.cts.map +0 -1
- package/dist/services/TradingService.d.mts +0 -308
- package/dist/services/TradingService.d.mts.map +0 -1
- package/dist/services/TradingService.mjs +0 -1477
- package/dist/services/TradingService.mjs.map +0 -1
- package/dist/services/index.cjs +0 -26
- package/dist/services/index.cjs.map +0 -1
- package/dist/services/index.d.cts +0 -11
- package/dist/services/index.d.cts.map +0 -1
- package/dist/services/index.d.mts +0 -11
- package/dist/services/index.d.mts.map +0 -1
- package/dist/services/index.mjs +0 -10
- package/dist/services/index.mjs.map +0 -1
- package/dist/services/utils.cjs +0 -16
- package/dist/services/utils.cjs.map +0 -1
- package/dist/services/utils.d.cts +0 -8
- package/dist/services/utils.d.cts.map +0 -1
- package/dist/services/utils.d.mts +0 -8
- package/dist/services/utils.d.mts.map +0 -1
- package/dist/services/utils.mjs +0 -12
- package/dist/services/utils.mjs.map +0 -1
- package/dist/types/chart.cjs +0 -42
- package/dist/types/chart.cjs.map +0 -1
- package/dist/types/chart.d.cts +0 -62
- package/dist/types/chart.d.cts.map +0 -1
- package/dist/types/chart.d.mts +0 -62
- package/dist/types/chart.d.mts.map +0 -1
- package/dist/types/chart.mjs +0 -39
- package/dist/types/chart.mjs.map +0 -1
- package/dist/types/hyperliquid-types.cjs +0 -3
- package/dist/types/hyperliquid-types.cjs.map +0 -1
- package/dist/types/hyperliquid-types.d.cts +0 -52
- package/dist/types/hyperliquid-types.d.cts.map +0 -1
- package/dist/types/hyperliquid-types.d.mts +0 -52
- package/dist/types/hyperliquid-types.d.mts.map +0 -1
- package/dist/types/hyperliquid-types.mjs +0 -2
- package/dist/types/hyperliquid-types.mjs.map +0 -1
- package/dist/types/index.cjs +0 -96
- package/dist/types/index.cjs.map +0 -1
- package/dist/types/index.d.cts +0 -1153
- package/dist/types/index.d.cts.map +0 -1
- package/dist/types/index.d.mts +0 -1153
- package/dist/types/index.d.mts.map +0 -1
- package/dist/types/index.mjs +0 -79
- package/dist/types/index.mjs.map +0 -1
- package/dist/types/transactionTypes.cjs +0 -28
- package/dist/types/transactionTypes.cjs.map +0 -1
- package/dist/types/transactionTypes.d.cts +0 -64
- package/dist/types/transactionTypes.d.cts.map +0 -1
- package/dist/types/transactionTypes.d.mts +0 -64
- package/dist/types/transactionTypes.d.mts.map +0 -1
- package/dist/types/transactionTypes.mjs +0 -23
- package/dist/types/transactionTypes.mjs.map +0 -1
- package/dist/utils/accountUtils.cjs +0 -65
- package/dist/utils/accountUtils.cjs.map +0 -1
- package/dist/utils/accountUtils.d.cts +0 -26
- package/dist/utils/accountUtils.d.cts.map +0 -1
- package/dist/utils/accountUtils.d.mts +0 -26
- package/dist/utils/accountUtils.d.mts.map +0 -1
- package/dist/utils/accountUtils.mjs +0 -61
- package/dist/utils/accountUtils.mjs.map +0 -1
- package/dist/utils/amountConversion.cjs +0 -61
- package/dist/utils/amountConversion.cjs.map +0 -1
- package/dist/utils/amountConversion.d.cts +0 -16
- package/dist/utils/amountConversion.d.cts.map +0 -1
- package/dist/utils/amountConversion.d.mts +0 -16
- package/dist/utils/amountConversion.d.mts.map +0 -1
- package/dist/utils/amountConversion.mjs +0 -54
- package/dist/utils/amountConversion.mjs.map +0 -1
- package/dist/utils/chartUtils.cjs +0 -98
- package/dist/utils/chartUtils.cjs.map +0 -1
- package/dist/utils/chartUtils.d.cts +0 -14
- package/dist/utils/chartUtils.d.cts.map +0 -1
- package/dist/utils/chartUtils.d.mts +0 -14
- package/dist/utils/chartUtils.d.mts.map +0 -1
- package/dist/utils/chartUtils.mjs +0 -94
- package/dist/utils/chartUtils.mjs.map +0 -1
- package/dist/utils/errorUtils.cjs +0 -22
- package/dist/utils/errorUtils.cjs.map +0 -1
- package/dist/utils/errorUtils.d.cts +0 -13
- package/dist/utils/errorUtils.d.cts.map +0 -1
- package/dist/utils/errorUtils.d.mts +0 -13
- package/dist/utils/errorUtils.d.mts.map +0 -1
- package/dist/utils/errorUtils.mjs +0 -18
- package/dist/utils/errorUtils.mjs.map +0 -1
- package/dist/utils/formatUtils.cjs +0 -987
- package/dist/utils/formatUtils.cjs.map +0 -1
- package/dist/utils/formatUtils.d.cts +0 -387
- package/dist/utils/formatUtils.d.cts.map +0 -1
- package/dist/utils/formatUtils.d.mts +0 -387
- package/dist/utils/formatUtils.d.mts.map +0 -1
- package/dist/utils/formatUtils.mjs +0 -966
- package/dist/utils/formatUtils.mjs.map +0 -1
- package/dist/utils/hyperLiquidAdapter.cjs +0 -454
- package/dist/utils/hyperLiquidAdapter.cjs.map +0 -1
- package/dist/utils/hyperLiquidAdapter.d.cts +0 -158
- package/dist/utils/hyperLiquidAdapter.d.cts.map +0 -1
- package/dist/utils/hyperLiquidAdapter.d.mts +0 -158
- package/dist/utils/hyperLiquidAdapter.d.mts.map +0 -1
- package/dist/utils/hyperLiquidAdapter.mjs +0 -439
- package/dist/utils/hyperLiquidAdapter.mjs.map +0 -1
- package/dist/utils/hyperLiquidOrderBookProcessor.cjs +0 -89
- package/dist/utils/hyperLiquidOrderBookProcessor.cjs.map +0 -1
- package/dist/utils/hyperLiquidOrderBookProcessor.d.cts +0 -58
- package/dist/utils/hyperLiquidOrderBookProcessor.d.cts.map +0 -1
- package/dist/utils/hyperLiquidOrderBookProcessor.d.mts +0 -58
- package/dist/utils/hyperLiquidOrderBookProcessor.d.mts.map +0 -1
- package/dist/utils/hyperLiquidOrderBookProcessor.mjs +0 -84
- package/dist/utils/hyperLiquidOrderBookProcessor.mjs.map +0 -1
- package/dist/utils/hyperLiquidValidation.cjs +0 -426
- package/dist/utils/hyperLiquidValidation.cjs.map +0 -1
- package/dist/utils/hyperLiquidValidation.d.cts +0 -143
- package/dist/utils/hyperLiquidValidation.d.cts.map +0 -1
- package/dist/utils/hyperLiquidValidation.d.mts +0 -143
- package/dist/utils/hyperLiquidValidation.d.mts.map +0 -1
- package/dist/utils/hyperLiquidValidation.mjs +0 -413
- package/dist/utils/hyperLiquidValidation.mjs.map +0 -1
- package/dist/utils/idUtils.cjs +0 -50
- package/dist/utils/idUtils.cjs.map +0 -1
- package/dist/utils/idUtils.d.cts +0 -38
- package/dist/utils/idUtils.d.cts.map +0 -1
- package/dist/utils/idUtils.d.mts +0 -38
- package/dist/utils/idUtils.d.mts.map +0 -1
- package/dist/utils/idUtils.mjs +0 -42
- package/dist/utils/idUtils.mjs.map +0 -1
- package/dist/utils/marginUtils.cjs +0 -137
- package/dist/utils/marginUtils.cjs.map +0 -1
- package/dist/utils/marginUtils.d.cts +0 -64
- package/dist/utils/marginUtils.d.cts.map +0 -1
- package/dist/utils/marginUtils.d.mts +0 -64
- package/dist/utils/marginUtils.d.mts.map +0 -1
- package/dist/utils/marginUtils.mjs +0 -131
- package/dist/utils/marginUtils.mjs.map +0 -1
- package/dist/utils/marketDataTransform.cjs +0 -227
- package/dist/utils/marketDataTransform.cjs.map +0 -1
- package/dist/utils/marketDataTransform.d.cts +0 -45
- package/dist/utils/marketDataTransform.d.cts.map +0 -1
- package/dist/utils/marketDataTransform.d.mts +0 -45
- package/dist/utils/marketDataTransform.d.mts.map +0 -1
- package/dist/utils/marketDataTransform.mjs +0 -220
- package/dist/utils/marketDataTransform.mjs.map +0 -1
- package/dist/utils/marketUtils.cjs +0 -420
- package/dist/utils/marketUtils.cjs.map +0 -1
- package/dist/utils/marketUtils.d.cts +0 -227
- package/dist/utils/marketUtils.d.cts.map +0 -1
- package/dist/utils/marketUtils.d.mts +0 -227
- package/dist/utils/marketUtils.d.mts.map +0 -1
- package/dist/utils/marketUtils.mjs +0 -404
- package/dist/utils/marketUtils.mjs.map +0 -1
- package/dist/utils/orderBookGrouping.cjs +0 -151
- package/dist/utils/orderBookGrouping.cjs.map +0 -1
- package/dist/utils/orderBookGrouping.d.cts +0 -76
- package/dist/utils/orderBookGrouping.d.cts.map +0 -1
- package/dist/utils/orderBookGrouping.d.mts +0 -76
- package/dist/utils/orderBookGrouping.d.mts.map +0 -1
- package/dist/utils/orderBookGrouping.mjs +0 -143
- package/dist/utils/orderBookGrouping.mjs.map +0 -1
- package/dist/utils/orderCalculations.cjs +0 -283
- package/dist/utils/orderCalculations.cjs.map +0 -1
- package/dist/utils/orderCalculations.d.cts +0 -107
- package/dist/utils/orderCalculations.d.cts.map +0 -1
- package/dist/utils/orderCalculations.d.mts +0 -107
- package/dist/utils/orderCalculations.d.mts.map +0 -1
- package/dist/utils/orderCalculations.mjs +0 -274
- package/dist/utils/orderCalculations.mjs.map +0 -1
- package/dist/utils/orderUtils.cjs +0 -159
- package/dist/utils/orderUtils.cjs.map +0 -1
- package/dist/utils/orderUtils.d.cts +0 -66
- package/dist/utils/orderUtils.d.cts.map +0 -1
- package/dist/utils/orderUtils.d.mts +0 -66
- package/dist/utils/orderUtils.d.mts.map +0 -1
- package/dist/utils/orderUtils.mjs +0 -151
- package/dist/utils/orderUtils.mjs.map +0 -1
- package/dist/utils/pnlCalculations.cjs +0 -107
- package/dist/utils/pnlCalculations.cjs.map +0 -1
- package/dist/utils/pnlCalculations.d.cts +0 -79
- package/dist/utils/pnlCalculations.d.cts.map +0 -1
- package/dist/utils/pnlCalculations.d.mts +0 -79
- package/dist/utils/pnlCalculations.d.mts.map +0 -1
- package/dist/utils/pnlCalculations.mjs +0 -97
- package/dist/utils/pnlCalculations.mjs.map +0 -1
- package/dist/utils/positionCalculations.cjs +0 -176
- package/dist/utils/positionCalculations.cjs.map +0 -1
- package/dist/utils/positionCalculations.d.cts +0 -85
- package/dist/utils/positionCalculations.d.cts.map +0 -1
- package/dist/utils/positionCalculations.d.mts +0 -85
- package/dist/utils/positionCalculations.d.mts.map +0 -1
- package/dist/utils/positionCalculations.mjs +0 -165
- package/dist/utils/positionCalculations.mjs.map +0 -1
- package/dist/utils/sortMarkets.cjs +0 -88
- package/dist/utils/sortMarkets.cjs.map +0 -1
- package/dist/utils/sortMarkets.d.cts +0 -21
- package/dist/utils/sortMarkets.d.cts.map +0 -1
- package/dist/utils/sortMarkets.d.mts +0 -21
- package/dist/utils/sortMarkets.d.mts.map +0 -1
- package/dist/utils/sortMarkets.mjs +0 -84
- package/dist/utils/sortMarkets.mjs.map +0 -1
- package/dist/utils/standaloneInfoClient.cjs +0 -34
- package/dist/utils/standaloneInfoClient.cjs.map +0 -1
- package/dist/utils/standaloneInfoClient.d.cts +0 -31
- package/dist/utils/standaloneInfoClient.d.cts.map +0 -1
- package/dist/utils/standaloneInfoClient.d.mts +0 -31
- package/dist/utils/standaloneInfoClient.d.mts.map +0 -1
- package/dist/utils/standaloneInfoClient.mjs +0 -30
- package/dist/utils/standaloneInfoClient.mjs.map +0 -1
- package/dist/utils/stringParseUtils.cjs +0 -49
- package/dist/utils/stringParseUtils.cjs.map +0 -1
- package/dist/utils/stringParseUtils.d.cts +0 -35
- package/dist/utils/stringParseUtils.d.cts.map +0 -1
- package/dist/utils/stringParseUtils.d.mts +0 -35
- package/dist/utils/stringParseUtils.d.mts.map +0 -1
- package/dist/utils/stringParseUtils.mjs +0 -44
- package/dist/utils/stringParseUtils.mjs.map +0 -1
- package/dist/utils/time.cjs +0 -37
- package/dist/utils/time.cjs.map +0 -1
- package/dist/utils/time.d.cts +0 -17
- package/dist/utils/time.d.cts.map +0 -1
- package/dist/utils/time.d.mts +0 -17
- package/dist/utils/time.d.mts.map +0 -1
- package/dist/utils/time.mjs +0 -33
- package/dist/utils/time.mjs.map +0 -1
- package/dist/utils/tpslValidation.cjs +0 -505
- package/dist/utils/tpslValidation.cjs.map +0 -1
- package/dist/utils/tpslValidation.d.cts +0 -182
- package/dist/utils/tpslValidation.d.cts.map +0 -1
- package/dist/utils/tpslValidation.d.mts +0 -182
- package/dist/utils/tpslValidation.d.mts.map +0 -1
- package/dist/utils/tpslValidation.mjs +0 -482
- package/dist/utils/tpslValidation.mjs.map +0 -1
- package/dist/utils/transactionUtils.cjs +0 -36
- package/dist/utils/transactionUtils.cjs.map +0 -1
- package/dist/utils/transactionUtils.d.cts +0 -17
- package/dist/utils/transactionUtils.d.cts.map +0 -1
- package/dist/utils/transactionUtils.d.mts +0 -17
- package/dist/utils/transactionUtils.d.mts.map +0 -1
- package/dist/utils/transactionUtils.mjs +0 -32
- package/dist/utils/transactionUtils.mjs.map +0 -1
- package/dist/utils/wait.cjs +0 -12
- package/dist/utils/wait.cjs.map +0 -1
- package/dist/utils/wait.d.cts +0 -8
- package/dist/utils/wait.d.cts.map +0 -1
- package/dist/utils/wait.d.mts +0 -8
- package/dist/utils/wait.d.mts.map +0 -1
- package/dist/utils/wait.mjs +0 -8
- package/dist/utils/wait.mjs.map +0 -1
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateNewLiquidationPrice = exports.calculateMaxRemovableMargin = exports.assessMarginRemovalRisk = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Margin adjustment calculation utilities
|
|
6
|
-
* Provides risk assessment and margin calculation functions for position management
|
|
7
|
-
*/
|
|
8
|
-
const perpsConfig_1 = require("../constants/perpsConfig.cjs");
|
|
9
|
-
/**
|
|
10
|
-
* Assess liquidation risk after margin removal
|
|
11
|
-
* Compares new liquidation price against current market price to determine risk level
|
|
12
|
-
*
|
|
13
|
-
* @param params - New liquidation price, current market price, and position direction
|
|
14
|
-
* @returns Risk assessment with level (safe/warning/danger), price difference, and risk ratio
|
|
15
|
-
*/
|
|
16
|
-
function assessMarginRemovalRisk(params) {
|
|
17
|
-
const { newLiquidationPrice, currentPrice, isLong } = params;
|
|
18
|
-
if (!newLiquidationPrice ||
|
|
19
|
-
!currentPrice ||
|
|
20
|
-
isNaN(newLiquidationPrice) ||
|
|
21
|
-
isNaN(currentPrice)) {
|
|
22
|
-
return { riskLevel: 'safe', priceDiff: 0, riskRatio: 0 };
|
|
23
|
-
}
|
|
24
|
-
// Calculate price difference based on position direction
|
|
25
|
-
// For long: current price should be above liquidation price
|
|
26
|
-
// For short: liquidation price should be above current price
|
|
27
|
-
const priceDiff = isLong
|
|
28
|
-
? currentPrice - newLiquidationPrice
|
|
29
|
-
: newLiquidationPrice - currentPrice;
|
|
30
|
-
// Risk ratio: how far away is price from liquidation, relative to liquidation price
|
|
31
|
-
// Higher ratio = safer (price is far from liquidation)
|
|
32
|
-
// Lower ratio = riskier (price is close to liquidation)
|
|
33
|
-
const riskRatio = priceDiff / newLiquidationPrice;
|
|
34
|
-
let riskLevel;
|
|
35
|
-
if (riskRatio < perpsConfig_1.MARGIN_ADJUSTMENT_CONFIG.LiquidationRiskThreshold - 1) {
|
|
36
|
-
riskLevel = 'danger'; // <20% buffer - critical risk
|
|
37
|
-
}
|
|
38
|
-
else if (riskRatio <
|
|
39
|
-
perpsConfig_1.MARGIN_ADJUSTMENT_CONFIG.LiquidationWarningThreshold - 1) {
|
|
40
|
-
riskLevel = 'warning'; // <50% buffer - moderate risk
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
riskLevel = 'safe'; // >=50% buffer - safe
|
|
44
|
-
}
|
|
45
|
-
return { riskLevel, priceDiff, riskRatio };
|
|
46
|
-
}
|
|
47
|
-
exports.assessMarginRemovalRisk = assessMarginRemovalRisk;
|
|
48
|
-
/**
|
|
49
|
-
* Calculate maximum margin that can be safely removed from a position
|
|
50
|
-
*
|
|
51
|
-
* HyperLiquid enforces: transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)
|
|
52
|
-
* See: https://hyperliquid.gitbook.io/hyperliquid-docs/trading/margin-and-pnl
|
|
53
|
-
* See also: docs/perps/hyperliquid/margining.md
|
|
54
|
-
*
|
|
55
|
-
* Key insight from Hyperliquid support (Xulian, Dec 6, 2025):
|
|
56
|
-
* "you need to account for initial margin for withdrawal, maintenance is what is needed to not be liquidated"
|
|
57
|
-
*
|
|
58
|
-
* The initial margin is calculated using the POSITION'S leverage (not the asset's max leverage).
|
|
59
|
-
* For example, a position opened at 10x leverage requires 10% initial margin,
|
|
60
|
-
* not 2% (which would be for 50x max leverage).
|
|
61
|
-
*
|
|
62
|
-
* @param params - Current margin, position size, prices, and position leverage
|
|
63
|
-
* @returns Maximum removable margin amount in USD
|
|
64
|
-
*/
|
|
65
|
-
function calculateMaxRemovableMargin(params) {
|
|
66
|
-
const { currentMargin, positionSize, currentPrice, positionLeverage, notionalValue: providedNotionalValue, } = params;
|
|
67
|
-
// Validate inputs
|
|
68
|
-
if (isNaN(currentMargin) ||
|
|
69
|
-
isNaN(positionLeverage) ||
|
|
70
|
-
currentMargin <= 0 ||
|
|
71
|
-
positionLeverage <= 0) {
|
|
72
|
-
return 0;
|
|
73
|
-
}
|
|
74
|
-
// Use provided notional value (e.g., from position.positionValue) or calculate from price
|
|
75
|
-
// This allows immediate display before live prices load
|
|
76
|
-
let notionalValue = providedNotionalValue;
|
|
77
|
-
if (notionalValue === undefined ||
|
|
78
|
-
isNaN(notionalValue) ||
|
|
79
|
-
notionalValue <= 0) {
|
|
80
|
-
// Fall back to calculating from price if not provided or invalid
|
|
81
|
-
if (isNaN(positionSize) ||
|
|
82
|
-
isNaN(currentPrice) ||
|
|
83
|
-
positionSize <= 0 ||
|
|
84
|
-
currentPrice <= 0) {
|
|
85
|
-
return 0;
|
|
86
|
-
}
|
|
87
|
-
notionalValue = positionSize * currentPrice;
|
|
88
|
-
}
|
|
89
|
-
// Hyperliquid's transfer margin requirement formula:
|
|
90
|
-
// transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)
|
|
91
|
-
//
|
|
92
|
-
// IMPORTANT: Use the position's actual leverage, not the asset's max leverage
|
|
93
|
-
// A position at 10x leverage needs 10% initial margin ($100 for $1000 notional)
|
|
94
|
-
// NOT the 2% that 50x max leverage would imply
|
|
95
|
-
const initialMarginRequired = notionalValue / positionLeverage;
|
|
96
|
-
const tenPercentMargin = notionalValue * perpsConfig_1.MARGIN_ADJUSTMENT_CONFIG.MarginRemovalSafetyBuffer;
|
|
97
|
-
// Transfer margin required is the MAX of these two constraints
|
|
98
|
-
const transferMarginRequired = Math.max(initialMarginRequired, tenPercentMargin);
|
|
99
|
-
// Note: Unrealized PnL is NOT counted as part of "remaining margin" for withdrawals
|
|
100
|
-
// Per Hyperliquid docs, unrealized PnL helps prevent liquidation but doesn't
|
|
101
|
-
// increase your available withdrawal limit for margin transfers
|
|
102
|
-
// Maximum removable = current margin - required (must be non-negative)
|
|
103
|
-
return Math.max(0, currentMargin - transferMarginRequired);
|
|
104
|
-
}
|
|
105
|
-
exports.calculateMaxRemovableMargin = calculateMaxRemovableMargin;
|
|
106
|
-
/**
|
|
107
|
-
* Calculate new liquidation price after margin adjustment
|
|
108
|
-
* Estimates where the liquidation price will move based on margin change
|
|
109
|
-
* Note: This is a simplified calculation; actual liquidation price may vary based on protocol
|
|
110
|
-
*
|
|
111
|
-
* @param params - New margin amount, position size, entry price, direction, and current liquidation price
|
|
112
|
-
* @returns Estimated new liquidation price
|
|
113
|
-
*/
|
|
114
|
-
function calculateNewLiquidationPrice(params) {
|
|
115
|
-
const { newMargin, positionSize, entryPrice, isLong, currentLiquidationPrice, } = params;
|
|
116
|
-
// Validate inputs
|
|
117
|
-
if (isNaN(newMargin) ||
|
|
118
|
-
isNaN(positionSize) ||
|
|
119
|
-
isNaN(entryPrice) ||
|
|
120
|
-
newMargin <= 0 ||
|
|
121
|
-
positionSize <= 0 ||
|
|
122
|
-
entryPrice <= 0) {
|
|
123
|
-
return currentLiquidationPrice; // Return current if invalid inputs
|
|
124
|
-
}
|
|
125
|
-
// Calculate margin per unit of position
|
|
126
|
-
const marginPerUnit = newMargin / positionSize;
|
|
127
|
-
// For long positions: liquidation price is below entry price
|
|
128
|
-
// liquidationPrice = entryPrice - marginPerUnit
|
|
129
|
-
// For short positions: liquidation price is above entry price
|
|
130
|
-
// liquidationPrice = entryPrice + marginPerUnit
|
|
131
|
-
if (isLong) {
|
|
132
|
-
return Math.max(0, entryPrice - marginPerUnit);
|
|
133
|
-
}
|
|
134
|
-
return entryPrice + marginPerUnit;
|
|
135
|
-
}
|
|
136
|
-
exports.calculateNewLiquidationPrice = calculateNewLiquidationPrice;
|
|
137
|
-
//# sourceMappingURL=marginUtils.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"marginUtils.cjs","sourceRoot":"","sources":["../../src/utils/marginUtils.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,8DAAoE;AAmCpE;;;;;;GAMG;AACH,SAAgB,uBAAuB,CACrC,MAAqC;IAErC,MAAM,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IAE7D,IACE,C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ACD,OAAO,uBAAuB,CAAC,CAAC,mCAAmC;IACrE,CAAC;IAED,wCAAwC;IACxC,MAAM,aAAa,GAAG,SAAS,GAAG,YAAY,CAAC;IAE/C,6DAA6D;IAC7D,gDAAgD;IAChD,8DAA8D;IAC9D,gDAAgD;IAChD,IAAI,MAAM,EAAE,CAAC;QACX,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,UAAU,GAAG,aAAa,CAAC,CAAC;IACjD,CAAC;IACD,OAAO,UAAU,GAAG,aAAa,CAAC;AACpC,CAAC;AAlCD,oEAkCC","sourcesContent":["/**\n * Margin adjustment calculation utilities\n * Provides risk assessment and margin calculation functions for position management\n */\nimport { MARGIN_ADJUSTMENT_CONFIG } from '../constants/perpsConfig';\n\nexport type RiskLevel = 'safe' | 'warning' | 'danger';\n\nexport type MarginRiskAssessment = {\n riskLevel: RiskLevel;\n priceDiff: number;\n riskRatio: number;\n};\n\nexport type AssessMarginRemovalRiskParams = {\n newLiquidationPrice: number;\n currentPrice: number;\n isLong: boolean;\n};\n\nexport type CalculateMaxRemovableMarginParams = {\n currentMargin: number;\n positionSize: number;\n entryPrice: number;\n currentPrice: number;\n /** The actual leverage of the position (not the asset's max leverage) */\n positionLeverage: number;\n /** Optional pre-calculated notional value (e.g., from position.positionValue) for immediate display before live prices load */\n notionalValue?: number;\n};\n\nexport type CalculateNewLiquidationPriceParams = {\n newMargin: number;\n positionSize: number;\n entryPrice: number;\n isLong: boolean;\n currentLiquidationPrice: number;\n};\n\n/**\n * Assess liquidation risk after margin removal\n * Compares new liquidation price against current market price to determine risk level\n *\n * @param params - New liquidation price, current market price, and position direction\n * @returns Risk assessment with level (safe/warning/danger), price difference, and risk ratio\n */\nexport function assessMarginRemovalRisk(\n params: AssessMarginRemovalRiskParams,\n): MarginRiskAssessment {\n const { newLiquidationPrice, currentPrice, isLong } = params;\n\n if (\n !newLiquidationPrice ||\n !currentPrice ||\n isNaN(newLiquidationPrice) ||\n isNaN(currentPrice)\n ) {\n return { riskLevel: 'safe', priceDiff: 0, riskRatio: 0 };\n }\n\n // Calculate price difference based on position direction\n // For long: current price should be above liquidation price\n // For short: liquidation price should be above current price\n const priceDiff = isLong\n ? currentPrice - newLiquidationPrice\n : newLiquidationPrice - currentPrice;\n\n // Risk ratio: how far away is price from liquidation, relative to liquidation price\n // Higher ratio = safer (price is far from liquidation)\n // Lower ratio = riskier (price is close to liquidation)\n const riskRatio = priceDiff / newLiquidationPrice;\n\n let riskLevel: RiskLevel;\n if (riskRatio < MARGIN_ADJUSTMENT_CONFIG.LiquidationRiskThreshold - 1) {\n riskLevel = 'danger'; // <20% buffer - critical risk\n } else if (\n riskRatio <\n MARGIN_ADJUSTMENT_CONFIG.LiquidationWarningThreshold - 1\n ) {\n riskLevel = 'warning'; // <50% buffer - moderate risk\n } else {\n riskLevel = 'safe'; // >=50% buffer - safe\n }\n\n return { riskLevel, priceDiff, riskRatio };\n}\n\n/**\n * Calculate maximum margin that can be safely removed from a position\n *\n * HyperLiquid enforces: transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)\n * See: https://hyperliquid.gitbook.io/hyperliquid-docs/trading/margin-and-pnl\n * See also: docs/perps/hyperliquid/margining.md\n *\n * Key insight from Hyperliquid support (Xulian, Dec 6, 2025):\n * \"you need to account for initial margin for withdrawal, maintenance is what is needed to not be liquidated\"\n *\n * The initial margin is calculated using the POSITION'S leverage (not the asset's max leverage).\n * For example, a position opened at 10x leverage requires 10% initial margin,\n * not 2% (which would be for 50x max leverage).\n *\n * @param params - Current margin, position size, prices, and position leverage\n * @returns Maximum removable margin amount in USD\n */\nexport function calculateMaxRemovableMargin(\n params: CalculateMaxRemovableMarginParams,\n): number {\n const {\n currentMargin,\n positionSize,\n currentPrice,\n positionLeverage,\n notionalValue: providedNotionalValue,\n } = params;\n\n // Validate inputs\n if (\n isNaN(currentMargin) ||\n isNaN(positionLeverage) ||\n currentMargin <= 0 ||\n positionLeverage <= 0\n ) {\n return 0;\n }\n\n // Use provided notional value (e.g., from position.positionValue) or calculate from price\n // This allows immediate display before live prices load\n let notionalValue = providedNotionalValue;\n if (\n notionalValue === undefined ||\n isNaN(notionalValue) ||\n notionalValue <= 0\n ) {\n // Fall back to calculating from price if not provided or invalid\n if (\n isNaN(positionSize) ||\n isNaN(currentPrice) ||\n positionSize <= 0 ||\n currentPrice <= 0\n ) {\n return 0;\n }\n notionalValue = positionSize * currentPrice;\n }\n\n // Hyperliquid's transfer margin requirement formula:\n // transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)\n //\n // IMPORTANT: Use the position's actual leverage, not the asset's max leverage\n // A position at 10x leverage needs 10% initial margin ($100 for $1000 notional)\n // NOT the 2% that 50x max leverage would imply\n const initialMarginRequired = notionalValue / positionLeverage;\n const tenPercentMargin =\n notionalValue * MARGIN_ADJUSTMENT_CONFIG.MarginRemovalSafetyBuffer;\n\n // Transfer margin required is the MAX of these two constraints\n const transferMarginRequired = Math.max(\n initialMarginRequired,\n tenPercentMargin,\n );\n\n // Note: Unrealized PnL is NOT counted as part of \"remaining margin\" for withdrawals\n // Per Hyperliquid docs, unrealized PnL helps prevent liquidation but doesn't\n // increase your available withdrawal limit for margin transfers\n // Maximum removable = current margin - required (must be non-negative)\n return Math.max(0, currentMargin - transferMarginRequired);\n}\n\n/**\n * Calculate new liquidation price after margin adjustment\n * Estimates where the liquidation price will move based on margin change\n * Note: This is a simplified calculation; actual liquidation price may vary based on protocol\n *\n * @param params - New margin amount, position size, entry price, direction, and current liquidation price\n * @returns Estimated new liquidation price\n */\nexport function calculateNewLiquidationPrice(\n params: CalculateNewLiquidationPriceParams,\n): number {\n const {\n newMargin,\n positionSize,\n entryPrice,\n isLong,\n currentLiquidationPrice,\n } = params;\n\n // Validate inputs\n if (\n isNaN(newMargin) ||\n isNaN(positionSize) ||\n isNaN(entryPrice) ||\n newMargin <= 0 ||\n positionSize <= 0 ||\n entryPrice <= 0\n ) {\n return currentLiquidationPrice; // Return current if invalid inputs\n }\n\n // Calculate margin per unit of position\n const marginPerUnit = newMargin / positionSize;\n\n // For long positions: liquidation price is below entry price\n // liquidationPrice = entryPrice - marginPerUnit\n // For short positions: liquidation price is above entry price\n // liquidationPrice = entryPrice + marginPerUnit\n if (isLong) {\n return Math.max(0, entryPrice - marginPerUnit);\n }\n return entryPrice + marginPerUnit;\n}\n"]}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
export type RiskLevel = 'safe' | 'warning' | 'danger';
|
|
2
|
-
export type MarginRiskAssessment = {
|
|
3
|
-
riskLevel: RiskLevel;
|
|
4
|
-
priceDiff: number;
|
|
5
|
-
riskRatio: number;
|
|
6
|
-
};
|
|
7
|
-
export type AssessMarginRemovalRiskParams = {
|
|
8
|
-
newLiquidationPrice: number;
|
|
9
|
-
currentPrice: number;
|
|
10
|
-
isLong: boolean;
|
|
11
|
-
};
|
|
12
|
-
export type CalculateMaxRemovableMarginParams = {
|
|
13
|
-
currentMargin: number;
|
|
14
|
-
positionSize: number;
|
|
15
|
-
entryPrice: number;
|
|
16
|
-
currentPrice: number;
|
|
17
|
-
/** The actual leverage of the position (not the asset's max leverage) */
|
|
18
|
-
positionLeverage: number;
|
|
19
|
-
/** Optional pre-calculated notional value (e.g., from position.positionValue) for immediate display before live prices load */
|
|
20
|
-
notionalValue?: number;
|
|
21
|
-
};
|
|
22
|
-
export type CalculateNewLiquidationPriceParams = {
|
|
23
|
-
newMargin: number;
|
|
24
|
-
positionSize: number;
|
|
25
|
-
entryPrice: number;
|
|
26
|
-
isLong: boolean;
|
|
27
|
-
currentLiquidationPrice: number;
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Assess liquidation risk after margin removal
|
|
31
|
-
* Compares new liquidation price against current market price to determine risk level
|
|
32
|
-
*
|
|
33
|
-
* @param params - New liquidation price, current market price, and position direction
|
|
34
|
-
* @returns Risk assessment with level (safe/warning/danger), price difference, and risk ratio
|
|
35
|
-
*/
|
|
36
|
-
export declare function assessMarginRemovalRisk(params: AssessMarginRemovalRiskParams): MarginRiskAssessment;
|
|
37
|
-
/**
|
|
38
|
-
* Calculate maximum margin that can be safely removed from a position
|
|
39
|
-
*
|
|
40
|
-
* HyperLiquid enforces: transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)
|
|
41
|
-
* See: https://hyperliquid.gitbook.io/hyperliquid-docs/trading/margin-and-pnl
|
|
42
|
-
* See also: docs/perps/hyperliquid/margining.md
|
|
43
|
-
*
|
|
44
|
-
* Key insight from Hyperliquid support (Xulian, Dec 6, 2025):
|
|
45
|
-
* "you need to account for initial margin for withdrawal, maintenance is what is needed to not be liquidated"
|
|
46
|
-
*
|
|
47
|
-
* The initial margin is calculated using the POSITION'S leverage (not the asset's max leverage).
|
|
48
|
-
* For example, a position opened at 10x leverage requires 10% initial margin,
|
|
49
|
-
* not 2% (which would be for 50x max leverage).
|
|
50
|
-
*
|
|
51
|
-
* @param params - Current margin, position size, prices, and position leverage
|
|
52
|
-
* @returns Maximum removable margin amount in USD
|
|
53
|
-
*/
|
|
54
|
-
export declare function calculateMaxRemovableMargin(params: CalculateMaxRemovableMarginParams): number;
|
|
55
|
-
/**
|
|
56
|
-
* Calculate new liquidation price after margin adjustment
|
|
57
|
-
* Estimates where the liquidation price will move based on margin change
|
|
58
|
-
* Note: This is a simplified calculation; actual liquidation price may vary based on protocol
|
|
59
|
-
*
|
|
60
|
-
* @param params - New margin amount, position size, entry price, direction, and current liquidation price
|
|
61
|
-
* @returns Estimated new liquidation price
|
|
62
|
-
*/
|
|
63
|
-
export declare function calculateNewLiquidationPrice(params: CalculateNewLiquidationPriceParams): number;
|
|
64
|
-
//# sourceMappingURL=marginUtils.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"marginUtils.d.cts","sourceRoot":"","sources":["../../src/utils/marginUtils.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAC;AAEtD,MAAM,MAAM,oBAAoB,GAAG;IACjC,SAAS,EAAE,SAAS,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG;IAC1C,mBAAmB,EAAE,MAAM,CAAC;IAC5B,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,iCAAiC,GAAG;IAC9C,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,yEAAyE;IACzE,gBAAgB,EAAE,MAAM,CAAC;IACzB,+HAA+H;IAC/H,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,OAAO,CAAC;IAChB,uBAAuB,EAAE,MAAM,CAAC;CACjC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,6BAA6B,GACpC,oBAAoB,CAqCtB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,iCAAiC,GACxC,MAAM,CA4DR;AAED;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,kCAAkC,GACzC,MAAM,CAgCR"}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
export type RiskLevel = 'safe' | 'warning' | 'danger';
|
|
2
|
-
export type MarginRiskAssessment = {
|
|
3
|
-
riskLevel: RiskLevel;
|
|
4
|
-
priceDiff: number;
|
|
5
|
-
riskRatio: number;
|
|
6
|
-
};
|
|
7
|
-
export type AssessMarginRemovalRiskParams = {
|
|
8
|
-
newLiquidationPrice: number;
|
|
9
|
-
currentPrice: number;
|
|
10
|
-
isLong: boolean;
|
|
11
|
-
};
|
|
12
|
-
export type CalculateMaxRemovableMarginParams = {
|
|
13
|
-
currentMargin: number;
|
|
14
|
-
positionSize: number;
|
|
15
|
-
entryPrice: number;
|
|
16
|
-
currentPrice: number;
|
|
17
|
-
/** The actual leverage of the position (not the asset's max leverage) */
|
|
18
|
-
positionLeverage: number;
|
|
19
|
-
/** Optional pre-calculated notional value (e.g., from position.positionValue) for immediate display before live prices load */
|
|
20
|
-
notionalValue?: number;
|
|
21
|
-
};
|
|
22
|
-
export type CalculateNewLiquidationPriceParams = {
|
|
23
|
-
newMargin: number;
|
|
24
|
-
positionSize: number;
|
|
25
|
-
entryPrice: number;
|
|
26
|
-
isLong: boolean;
|
|
27
|
-
currentLiquidationPrice: number;
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Assess liquidation risk after margin removal
|
|
31
|
-
* Compares new liquidation price against current market price to determine risk level
|
|
32
|
-
*
|
|
33
|
-
* @param params - New liquidation price, current market price, and position direction
|
|
34
|
-
* @returns Risk assessment with level (safe/warning/danger), price difference, and risk ratio
|
|
35
|
-
*/
|
|
36
|
-
export declare function assessMarginRemovalRisk(params: AssessMarginRemovalRiskParams): MarginRiskAssessment;
|
|
37
|
-
/**
|
|
38
|
-
* Calculate maximum margin that can be safely removed from a position
|
|
39
|
-
*
|
|
40
|
-
* HyperLiquid enforces: transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)
|
|
41
|
-
* See: https://hyperliquid.gitbook.io/hyperliquid-docs/trading/margin-and-pnl
|
|
42
|
-
* See also: docs/perps/hyperliquid/margining.md
|
|
43
|
-
*
|
|
44
|
-
* Key insight from Hyperliquid support (Xulian, Dec 6, 2025):
|
|
45
|
-
* "you need to account for initial margin for withdrawal, maintenance is what is needed to not be liquidated"
|
|
46
|
-
*
|
|
47
|
-
* The initial margin is calculated using the POSITION'S leverage (not the asset's max leverage).
|
|
48
|
-
* For example, a position opened at 10x leverage requires 10% initial margin,
|
|
49
|
-
* not 2% (which would be for 50x max leverage).
|
|
50
|
-
*
|
|
51
|
-
* @param params - Current margin, position size, prices, and position leverage
|
|
52
|
-
* @returns Maximum removable margin amount in USD
|
|
53
|
-
*/
|
|
54
|
-
export declare function calculateMaxRemovableMargin(params: CalculateMaxRemovableMarginParams): number;
|
|
55
|
-
/**
|
|
56
|
-
* Calculate new liquidation price after margin adjustment
|
|
57
|
-
* Estimates where the liquidation price will move based on margin change
|
|
58
|
-
* Note: This is a simplified calculation; actual liquidation price may vary based on protocol
|
|
59
|
-
*
|
|
60
|
-
* @param params - New margin amount, position size, entry price, direction, and current liquidation price
|
|
61
|
-
* @returns Estimated new liquidation price
|
|
62
|
-
*/
|
|
63
|
-
export declare function calculateNewLiquidationPrice(params: CalculateNewLiquidationPriceParams): number;
|
|
64
|
-
//# sourceMappingURL=marginUtils.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"marginUtils.d.mts","sourceRoot":"","sources":["../../src/utils/marginUtils.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAC;AAEtD,MAAM,MAAM,oBAAoB,GAAG;IACjC,SAAS,EAAE,SAAS,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG;IAC1C,mBAAmB,EAAE,MAAM,CAAC;IAC5B,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,iCAAiC,GAAG;IAC9C,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,yEAAyE;IACzE,gBAAgB,EAAE,MAAM,CAAC;IACzB,+HAA+H;IAC/H,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,OAAO,CAAC;IAChB,uBAAuB,EAAE,MAAM,CAAC;CACjC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,6BAA6B,GACpC,oBAAoB,CAqCtB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,iCAAiC,GACxC,MAAM,CA4DR;AAED;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,kCAAkC,GACzC,MAAM,CAgCR"}
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Margin adjustment calculation utilities
|
|
3
|
-
* Provides risk assessment and margin calculation functions for position management
|
|
4
|
-
*/
|
|
5
|
-
import { MARGIN_ADJUSTMENT_CONFIG } from "../constants/perpsConfig.mjs";
|
|
6
|
-
/**
|
|
7
|
-
* Assess liquidation risk after margin removal
|
|
8
|
-
* Compares new liquidation price against current market price to determine risk level
|
|
9
|
-
*
|
|
10
|
-
* @param params - New liquidation price, current market price, and position direction
|
|
11
|
-
* @returns Risk assessment with level (safe/warning/danger), price difference, and risk ratio
|
|
12
|
-
*/
|
|
13
|
-
export function assessMarginRemovalRisk(params) {
|
|
14
|
-
const { newLiquidationPrice, currentPrice, isLong } = params;
|
|
15
|
-
if (!newLiquidationPrice ||
|
|
16
|
-
!currentPrice ||
|
|
17
|
-
isNaN(newLiquidationPrice) ||
|
|
18
|
-
isNaN(currentPrice)) {
|
|
19
|
-
return { riskLevel: 'safe', priceDiff: 0, riskRatio: 0 };
|
|
20
|
-
}
|
|
21
|
-
// Calculate price difference based on position direction
|
|
22
|
-
// For long: current price should be above liquidation price
|
|
23
|
-
// For short: liquidation price should be above current price
|
|
24
|
-
const priceDiff = isLong
|
|
25
|
-
? currentPrice - newLiquidationPrice
|
|
26
|
-
: newLiquidationPrice - currentPrice;
|
|
27
|
-
// Risk ratio: how far away is price from liquidation, relative to liquidation price
|
|
28
|
-
// Higher ratio = safer (price is far from liquidation)
|
|
29
|
-
// Lower ratio = riskier (price is close to liquidation)
|
|
30
|
-
const riskRatio = priceDiff / newLiquidationPrice;
|
|
31
|
-
let riskLevel;
|
|
32
|
-
if (riskRatio < MARGIN_ADJUSTMENT_CONFIG.LiquidationRiskThreshold - 1) {
|
|
33
|
-
riskLevel = 'danger'; // <20% buffer - critical risk
|
|
34
|
-
}
|
|
35
|
-
else if (riskRatio <
|
|
36
|
-
MARGIN_ADJUSTMENT_CONFIG.LiquidationWarningThreshold - 1) {
|
|
37
|
-
riskLevel = 'warning'; // <50% buffer - moderate risk
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
riskLevel = 'safe'; // >=50% buffer - safe
|
|
41
|
-
}
|
|
42
|
-
return { riskLevel, priceDiff, riskRatio };
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Calculate maximum margin that can be safely removed from a position
|
|
46
|
-
*
|
|
47
|
-
* HyperLiquid enforces: transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)
|
|
48
|
-
* See: https://hyperliquid.gitbook.io/hyperliquid-docs/trading/margin-and-pnl
|
|
49
|
-
* See also: docs/perps/hyperliquid/margining.md
|
|
50
|
-
*
|
|
51
|
-
* Key insight from Hyperliquid support (Xulian, Dec 6, 2025):
|
|
52
|
-
* "you need to account for initial margin for withdrawal, maintenance is what is needed to not be liquidated"
|
|
53
|
-
*
|
|
54
|
-
* The initial margin is calculated using the POSITION'S leverage (not the asset's max leverage).
|
|
55
|
-
* For example, a position opened at 10x leverage requires 10% initial margin,
|
|
56
|
-
* not 2% (which would be for 50x max leverage).
|
|
57
|
-
*
|
|
58
|
-
* @param params - Current margin, position size, prices, and position leverage
|
|
59
|
-
* @returns Maximum removable margin amount in USD
|
|
60
|
-
*/
|
|
61
|
-
export function calculateMaxRemovableMargin(params) {
|
|
62
|
-
const { currentMargin, positionSize, currentPrice, positionLeverage, notionalValue: providedNotionalValue, } = params;
|
|
63
|
-
// Validate inputs
|
|
64
|
-
if (isNaN(currentMargin) ||
|
|
65
|
-
isNaN(positionLeverage) ||
|
|
66
|
-
currentMargin <= 0 ||
|
|
67
|
-
positionLeverage <= 0) {
|
|
68
|
-
return 0;
|
|
69
|
-
}
|
|
70
|
-
// Use provided notional value (e.g., from position.positionValue) or calculate from price
|
|
71
|
-
// This allows immediate display before live prices load
|
|
72
|
-
let notionalValue = providedNotionalValue;
|
|
73
|
-
if (notionalValue === undefined ||
|
|
74
|
-
isNaN(notionalValue) ||
|
|
75
|
-
notionalValue <= 0) {
|
|
76
|
-
// Fall back to calculating from price if not provided or invalid
|
|
77
|
-
if (isNaN(positionSize) ||
|
|
78
|
-
isNaN(currentPrice) ||
|
|
79
|
-
positionSize <= 0 ||
|
|
80
|
-
currentPrice <= 0) {
|
|
81
|
-
return 0;
|
|
82
|
-
}
|
|
83
|
-
notionalValue = positionSize * currentPrice;
|
|
84
|
-
}
|
|
85
|
-
// Hyperliquid's transfer margin requirement formula:
|
|
86
|
-
// transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)
|
|
87
|
-
//
|
|
88
|
-
// IMPORTANT: Use the position's actual leverage, not the asset's max leverage
|
|
89
|
-
// A position at 10x leverage needs 10% initial margin ($100 for $1000 notional)
|
|
90
|
-
// NOT the 2% that 50x max leverage would imply
|
|
91
|
-
const initialMarginRequired = notionalValue / positionLeverage;
|
|
92
|
-
const tenPercentMargin = notionalValue * MARGIN_ADJUSTMENT_CONFIG.MarginRemovalSafetyBuffer;
|
|
93
|
-
// Transfer margin required is the MAX of these two constraints
|
|
94
|
-
const transferMarginRequired = Math.max(initialMarginRequired, tenPercentMargin);
|
|
95
|
-
// Note: Unrealized PnL is NOT counted as part of "remaining margin" for withdrawals
|
|
96
|
-
// Per Hyperliquid docs, unrealized PnL helps prevent liquidation but doesn't
|
|
97
|
-
// increase your available withdrawal limit for margin transfers
|
|
98
|
-
// Maximum removable = current margin - required (must be non-negative)
|
|
99
|
-
return Math.max(0, currentMargin - transferMarginRequired);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Calculate new liquidation price after margin adjustment
|
|
103
|
-
* Estimates where the liquidation price will move based on margin change
|
|
104
|
-
* Note: This is a simplified calculation; actual liquidation price may vary based on protocol
|
|
105
|
-
*
|
|
106
|
-
* @param params - New margin amount, position size, entry price, direction, and current liquidation price
|
|
107
|
-
* @returns Estimated new liquidation price
|
|
108
|
-
*/
|
|
109
|
-
export function calculateNewLiquidationPrice(params) {
|
|
110
|
-
const { newMargin, positionSize, entryPrice, isLong, currentLiquidationPrice, } = params;
|
|
111
|
-
// Validate inputs
|
|
112
|
-
if (isNaN(newMargin) ||
|
|
113
|
-
isNaN(positionSize) ||
|
|
114
|
-
isNaN(entryPrice) ||
|
|
115
|
-
newMargin <= 0 ||
|
|
116
|
-
positionSize <= 0 ||
|
|
117
|
-
entryPrice <= 0) {
|
|
118
|
-
return currentLiquidationPrice; // Return current if invalid inputs
|
|
119
|
-
}
|
|
120
|
-
// Calculate margin per unit of position
|
|
121
|
-
const marginPerUnit = newMargin / positionSize;
|
|
122
|
-
// For long positions: liquidation price is below entry price
|
|
123
|
-
// liquidationPrice = entryPrice - marginPerUnit
|
|
124
|
-
// For short positions: liquidation price is above entry price
|
|
125
|
-
// liquidationPrice = entryPrice + marginPerUnit
|
|
126
|
-
if (isLong) {
|
|
127
|
-
return Math.max(0, entryPrice - marginPerUnit);
|
|
128
|
-
}
|
|
129
|
-
return entryPrice + marginPerUnit;
|
|
130
|
-
}
|
|
131
|
-
//# sourceMappingURL=marginUtils.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"marginUtils.mjs","sourceRoot":"","sources":["../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,GAAG,MAAM,CAAC;IAEX,kBAAkB;IAClB,IACE,KAAK,CAAC,SAAS,CAAC;QAChB,KAAK,CAAC,YAAY,CAAC;QACnB,KAAK,CAAC,UAAU,CAAC;QACjB,SAAS,IAAI,CAAC;QACd,YAAY,IAAI,CAAC;QACjB,UAAU,IAAI,CAAC,EACf,CAAC;QACD,OAAO,uBAAuB,CAAC,CAAC,mCAAmC;IACrE,CAAC;IAED,wCAAwC;IACxC,MAAM,aAAa,GAAG,SAAS,GAAG,YAAY,CAAC;IAE/C,6DAA6D;IAC7D,gDAAgD;IAChD,8DAA8D;IAC9D,gDAAgD;IAChD,IAAI,MAAM,EAAE,CAAC;QACX,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,UAAU,GAAG,aAAa,CAAC,CAAC;IACjD,CAAC;IACD,OAAO,UAAU,GAAG,aAAa,CAAC;AACpC,CAAC","sourcesContent":["/**\n * Margin adjustment calculation utilities\n * Provides risk assessment and margin calculation functions for position management\n */\nimport { MARGIN_ADJUSTMENT_CONFIG } from '../constants/perpsConfig';\n\nexport type RiskLevel = 'safe' | 'warning' | 'danger';\n\nexport type MarginRiskAssessment = {\n riskLevel: RiskLevel;\n priceDiff: number;\n riskRatio: number;\n};\n\nexport type AssessMarginRemovalRiskParams = {\n newLiquidationPrice: number;\n currentPrice: number;\n isLong: boolean;\n};\n\nexport type CalculateMaxRemovableMarginParams = {\n currentMargin: number;\n positionSize: number;\n entryPrice: number;\n currentPrice: number;\n /** The actual leverage of the position (not the asset's max leverage) */\n positionLeverage: number;\n /** Optional pre-calculated notional value (e.g., from position.positionValue) for immediate display before live prices load */\n notionalValue?: number;\n};\n\nexport type CalculateNewLiquidationPriceParams = {\n newMargin: number;\n positionSize: number;\n entryPrice: number;\n isLong: boolean;\n currentLiquidationPrice: number;\n};\n\n/**\n * Assess liquidation risk after margin removal\n * Compares new liquidation price against current market price to determine risk level\n *\n * @param params - New liquidation price, current market price, and position direction\n * @returns Risk assessment with level (safe/warning/danger), price difference, and risk ratio\n */\nexport function assessMarginRemovalRisk(\n params: AssessMarginRemovalRiskParams,\n): MarginRiskAssessment {\n const { newLiquidationPrice, currentPrice, isLong } = params;\n\n if (\n !newLiquidationPrice ||\n !currentPrice ||\n isNaN(newLiquidationPrice) ||\n isNaN(currentPrice)\n ) {\n return { riskLevel: 'safe', priceDiff: 0, riskRatio: 0 };\n }\n\n // Calculate price difference based on position direction\n // For long: current price should be above liquidation price\n // For short: liquidation price should be above current price\n const priceDiff = isLong\n ? currentPrice - newLiquidationPrice\n : newLiquidationPrice - currentPrice;\n\n // Risk ratio: how far away is price from liquidation, relative to liquidation price\n // Higher ratio = safer (price is far from liquidation)\n // Lower ratio = riskier (price is close to liquidation)\n const riskRatio = priceDiff / newLiquidationPrice;\n\n let riskLevel: RiskLevel;\n if (riskRatio < MARGIN_ADJUSTMENT_CONFIG.LiquidationRiskThreshold - 1) {\n riskLevel = 'danger'; // <20% buffer - critical risk\n } else if (\n riskRatio <\n MARGIN_ADJUSTMENT_CONFIG.LiquidationWarningThreshold - 1\n ) {\n riskLevel = 'warning'; // <50% buffer - moderate risk\n } else {\n riskLevel = 'safe'; // >=50% buffer - safe\n }\n\n return { riskLevel, priceDiff, riskRatio };\n}\n\n/**\n * Calculate maximum margin that can be safely removed from a position\n *\n * HyperLiquid enforces: transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)\n * See: https://hyperliquid.gitbook.io/hyperliquid-docs/trading/margin-and-pnl\n * See also: docs/perps/hyperliquid/margining.md\n *\n * Key insight from Hyperliquid support (Xulian, Dec 6, 2025):\n * \"you need to account for initial margin for withdrawal, maintenance is what is needed to not be liquidated\"\n *\n * The initial margin is calculated using the POSITION'S leverage (not the asset's max leverage).\n * For example, a position opened at 10x leverage requires 10% initial margin,\n * not 2% (which would be for 50x max leverage).\n *\n * @param params - Current margin, position size, prices, and position leverage\n * @returns Maximum removable margin amount in USD\n */\nexport function calculateMaxRemovableMargin(\n params: CalculateMaxRemovableMarginParams,\n): number {\n const {\n currentMargin,\n positionSize,\n currentPrice,\n positionLeverage,\n notionalValue: providedNotionalValue,\n } = params;\n\n // Validate inputs\n if (\n isNaN(currentMargin) ||\n isNaN(positionLeverage) ||\n currentMargin <= 0 ||\n positionLeverage <= 0\n ) {\n return 0;\n }\n\n // Use provided notional value (e.g., from position.positionValue) or calculate from price\n // This allows immediate display before live prices load\n let notionalValue = providedNotionalValue;\n if (\n notionalValue === undefined ||\n isNaN(notionalValue) ||\n notionalValue <= 0\n ) {\n // Fall back to calculating from price if not provided or invalid\n if (\n isNaN(positionSize) ||\n isNaN(currentPrice) ||\n positionSize <= 0 ||\n currentPrice <= 0\n ) {\n return 0;\n }\n notionalValue = positionSize * currentPrice;\n }\n\n // Hyperliquid's transfer margin requirement formula:\n // transfer_margin_required = max(initial_margin_required, 0.1 * total_position_value)\n //\n // IMPORTANT: Use the position's actual leverage, not the asset's max leverage\n // A position at 10x leverage needs 10% initial margin ($100 for $1000 notional)\n // NOT the 2% that 50x max leverage would imply\n const initialMarginRequired = notionalValue / positionLeverage;\n const tenPercentMargin =\n notionalValue * MARGIN_ADJUSTMENT_CONFIG.MarginRemovalSafetyBuffer;\n\n // Transfer margin required is the MAX of these two constraints\n const transferMarginRequired = Math.max(\n initialMarginRequired,\n tenPercentMargin,\n );\n\n // Note: Unrealized PnL is NOT counted as part of \"remaining margin\" for withdrawals\n // Per Hyperliquid docs, unrealized PnL helps prevent liquidation but doesn't\n // increase your available withdrawal limit for margin transfers\n // Maximum removable = current margin - required (must be non-negative)\n return Math.max(0, currentMargin - transferMarginRequired);\n}\n\n/**\n * Calculate new liquidation price after margin adjustment\n * Estimates where the liquidation price will move based on margin change\n * Note: This is a simplified calculation; actual liquidation price may vary based on protocol\n *\n * @param params - New margin amount, position size, entry price, direction, and current liquidation price\n * @returns Estimated new liquidation price\n */\nexport function calculateNewLiquidationPrice(\n params: CalculateNewLiquidationPriceParams,\n): number {\n const {\n newMargin,\n positionSize,\n entryPrice,\n isLong,\n currentLiquidationPrice,\n } = params;\n\n // Validate inputs\n if (\n isNaN(newMargin) ||\n isNaN(positionSize) ||\n isNaN(entryPrice) ||\n newMargin <= 0 ||\n positionSize <= 0 ||\n entryPrice <= 0\n ) {\n return currentLiquidationPrice; // Return current if invalid inputs\n }\n\n // Calculate margin per unit of position\n const marginPerUnit = newMargin / positionSize;\n\n // For long positions: liquidation price is below entry price\n // liquidationPrice = entryPrice - marginPerUnit\n // For short positions: liquidation price is above entry price\n // liquidationPrice = entryPrice + marginPerUnit\n if (isLong) {\n return Math.max(0, entryPrice - marginPerUnit);\n }\n return entryPrice + marginPerUnit;\n}\n"]}
|