Skip to main content

nautilus_hyperliquid/common/
consts.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{sync::LazyLock, time::Duration};
17
18use nautilus_model::{enums::OrderType, identifiers::Venue};
19use ustr::Ustr;
20
21pub const HYPERLIQUID: &str = "HYPERLIQUID";
22pub static HYPERLIQUID_VENUE: LazyLock<Venue> =
23    LazyLock::new(|| Venue::new(Ustr::from(HYPERLIQUID)));
24
25pub const HYPERLIQUID_WS_URL: &str = "wss://api.hyperliquid.xyz/ws";
26pub const HYPERLIQUID_INFO_URL: &str = "https://api.hyperliquid.xyz/info";
27pub const HYPERLIQUID_EXCHANGE_URL: &str = "https://api.hyperliquid.xyz/exchange";
28
29pub const HYPERLIQUID_TESTNET_WS_URL: &str = "wss://api.hyperliquid-testnet.xyz/ws";
30pub const HYPERLIQUID_TESTNET_INFO_URL: &str = "https://api.hyperliquid-testnet.xyz/info";
31pub const HYPERLIQUID_TESTNET_EXCHANGE_URL: &str = "https://api.hyperliquid-testnet.xyz/exchange";
32
33// Builder code address for order attribution (zero-fee)
34// Address MUST be lowercase for msgpack serialization
35pub const NAUTILUS_BUILDER_ADDRESS: &str = "0x0c8d970c462726e014ad36f6c5a63e99db48a8e7";
36
37/// Hyperliquid signing chain ID (0x66eee = 421614 decimal).
38pub const HYPERLIQUID_CHAIN_ID: u64 = 421614;
39
40// Error message substrings for detecting specific rejection reasons
41pub const HYPERLIQUID_POST_ONLY_WOULD_MATCH: &str =
42    "Post only order would have immediately matched";
43
44/// Hyperliquid supported order types.
45///
46/// # Notes
47///
48/// - All order types support trigger prices except Market and Limit.
49/// - Conditional orders follow patterns from OKX, Bybit, and BitMEX adapters.
50/// - Stop orders (StopMarket/StopLimit) are protective stops (sl).
51/// - If Touched orders (MarketIfTouched/LimitIfTouched) are profit-taking or entry orders (tp).
52/// - Post-only orders are implemented via ALO (Add Liquidity Only) time-in-force.
53///
54/// Trailing stops (TrailingStopMarket/TrailingStopLimit) are supported by the exchange
55/// and can be parsed from incoming WS messages, but the outgoing request model does not
56/// yet serialize the trailing offset parameters. Add them once HyperliquidExecTriggerParams
57/// is extended with trailing offset fields.
58pub const HYPERLIQUID_SUPPORTED_ORDER_TYPES: &[OrderType] = &[
59    OrderType::Market,          // IOC limit order
60    OrderType::Limit,           // Standard limit with GTC/IOC/ALO
61    OrderType::StopMarket,      // Protective stop with market execution
62    OrderType::StopLimit,       // Protective stop with limit price
63    OrderType::MarketIfTouched, // Profit-taking/entry with market execution
64    OrderType::LimitIfTouched,  // Profit-taking/entry with limit price
65];
66
67/// Conditional order types that use trigger orders on Hyperliquid.
68///
69/// These order types require a trigger_price and are implemented using
70/// HyperliquidExecOrderKind::Trigger with appropriate parameters.
71pub const HYPERLIQUID_CONDITIONAL_ORDER_TYPES: &[OrderType] = &[
72    OrderType::StopMarket,
73    OrderType::StopLimit,
74    OrderType::MarketIfTouched,
75    OrderType::LimitIfTouched,
76];
77
78/// Gets WebSocket URL for the specified network.
79pub fn ws_url(is_testnet: bool) -> &'static str {
80    if is_testnet {
81        HYPERLIQUID_TESTNET_WS_URL
82    } else {
83        HYPERLIQUID_WS_URL
84    }
85}
86
87/// Gets info API URL for the specified network.
88pub fn info_url(is_testnet: bool) -> &'static str {
89    if is_testnet {
90        HYPERLIQUID_TESTNET_INFO_URL
91    } else {
92        HYPERLIQUID_INFO_URL
93    }
94}
95
96/// Gets exchange API URL for the specified network.
97pub fn exchange_url(is_testnet: bool) -> &'static str {
98    if is_testnet {
99        HYPERLIQUID_TESTNET_EXCHANGE_URL
100    } else {
101        HYPERLIQUID_EXCHANGE_URL
102    }
103}
104
105// Default configuration values
106// Server closes if no message in last 60s, so ping every 30s
107pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(30);
108pub const RECONNECT_BASE_BACKOFF: Duration = Duration::from_millis(250);
109pub const RECONNECT_MAX_BACKOFF: Duration = Duration::from_secs(30);
110pub const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
111// Max 100 inflight WS post messages per Hyperliquid docs
112pub const INFLIGHT_MAX: usize = 100;
113pub const QUEUE_MAX: usize = 1000;
114
115#[cfg(test)]
116mod tests {
117    use rstest::rstest;
118
119    use super::*;
120
121    #[rstest]
122    fn test_ws_url() {
123        assert_eq!(ws_url(false), HYPERLIQUID_WS_URL);
124        assert_eq!(ws_url(true), HYPERLIQUID_TESTNET_WS_URL);
125    }
126
127    #[rstest]
128    fn test_info_url() {
129        assert_eq!(info_url(false), HYPERLIQUID_INFO_URL);
130        assert_eq!(info_url(true), HYPERLIQUID_TESTNET_INFO_URL);
131    }
132
133    #[rstest]
134    fn test_exchange_url() {
135        assert_eq!(exchange_url(false), HYPERLIQUID_EXCHANGE_URL);
136        assert_eq!(exchange_url(true), HYPERLIQUID_TESTNET_EXCHANGE_URL);
137    }
138
139    #[rstest]
140    fn test_constants_values() {
141        assert_eq!(HEARTBEAT_INTERVAL, Duration::from_secs(30));
142        assert_eq!(RECONNECT_BASE_BACKOFF, Duration::from_millis(250));
143        assert_eq!(RECONNECT_MAX_BACKOFF, Duration::from_secs(30));
144        assert_eq!(HTTP_TIMEOUT, Duration::from_secs(10));
145        assert_eq!(INFLIGHT_MAX, 100);
146        assert_eq!(QUEUE_MAX, 1000);
147    }
148}