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::{
19    enums::OrderType,
20    identifiers::{ClientId, Venue},
21};
22use ustr::Ustr;
23
24use super::enums::HyperliquidEnvironment;
25
26/// Venue identifier string.
27pub const HYPERLIQUID: &str = "HYPERLIQUID";
28
29/// Static venue instance.
30pub static HYPERLIQUID_VENUE: LazyLock<Venue> =
31    LazyLock::new(|| Venue::new(Ustr::from(HYPERLIQUID)));
32
33/// Static client ID instance.
34pub static HYPERLIQUID_CLIENT_ID: LazyLock<ClientId> =
35    LazyLock::new(|| ClientId::new(Ustr::from(HYPERLIQUID)));
36
37pub const HYPERLIQUID_WS_URL: &str = "wss://api.hyperliquid.xyz/ws";
38pub const HYPERLIQUID_INFO_URL: &str = "https://api.hyperliquid.xyz/info";
39pub const HYPERLIQUID_EXCHANGE_URL: &str = "https://api.hyperliquid.xyz/exchange";
40
41pub const HYPERLIQUID_TESTNET_WS_URL: &str = "wss://api.hyperliquid-testnet.xyz/ws";
42pub const HYPERLIQUID_TESTNET_INFO_URL: &str = "https://api.hyperliquid-testnet.xyz/info";
43pub const HYPERLIQUID_TESTNET_EXCHANGE_URL: &str = "https://api.hyperliquid-testnet.xyz/exchange";
44
45// Builder code address for order attribution (zero-fee)
46// Address MUST be lowercase for msgpack serialization
47pub const NAUTILUS_BUILDER_ADDRESS: &str = "0x0c8d970c462726e014ad36f6c5a63e99db48a8e7";
48
49/// Hyperliquid signing chain ID (0x66eee = 421614 decimal).
50pub const HYPERLIQUID_CHAIN_ID: u64 = 421614;
51
52// Error message substrings for detecting specific rejection reasons
53pub const HYPERLIQUID_POST_ONLY_WOULD_MATCH: &str =
54    "Post only order would have immediately matched";
55
56/// Hyperliquid supported order types.
57///
58/// # Notes
59///
60/// - All order types support trigger prices except Market and Limit.
61/// - Conditional orders follow patterns from OKX, Bybit, and BitMEX adapters.
62/// - Stop orders (StopMarket/StopLimit) are protective stops (sl).
63/// - If Touched orders (MarketIfTouched/LimitIfTouched) are profit-taking or entry orders (tp).
64/// - Post-only orders are implemented via ALO (Add Liquidity Only) time-in-force.
65///
66/// Trailing stops (TrailingStopMarket/TrailingStopLimit) are supported by the exchange
67/// and can be parsed from incoming WS messages, but the outgoing request model does not
68/// yet serialize the trailing offset parameters. Add them once HyperliquidExecTriggerParams
69/// is extended with trailing offset fields.
70pub const HYPERLIQUID_SUPPORTED_ORDER_TYPES: &[OrderType] = &[
71    OrderType::Market,          // IOC limit order
72    OrderType::Limit,           // Standard limit with GTC/IOC/ALO
73    OrderType::StopMarket,      // Protective stop with market execution
74    OrderType::StopLimit,       // Protective stop with limit price
75    OrderType::MarketIfTouched, // Profit-taking/entry with market execution
76    OrderType::LimitIfTouched,  // Profit-taking/entry with limit price
77];
78
79/// Conditional order types that use trigger orders on Hyperliquid.
80///
81/// These order types require a trigger_price and are implemented using
82/// HyperliquidExecOrderKind::Trigger with appropriate parameters.
83pub const HYPERLIQUID_CONDITIONAL_ORDER_TYPES: &[OrderType] = &[
84    OrderType::StopMarket,
85    OrderType::StopLimit,
86    OrderType::MarketIfTouched,
87    OrderType::LimitIfTouched,
88];
89
90/// Gets WebSocket URL for the specified environment.
91pub fn ws_url(environment: HyperliquidEnvironment) -> &'static str {
92    match environment {
93        HyperliquidEnvironment::Testnet => HYPERLIQUID_TESTNET_WS_URL,
94        HyperliquidEnvironment::Mainnet => HYPERLIQUID_WS_URL,
95    }
96}
97
98/// Gets info API URL for the specified environment.
99pub fn info_url(environment: HyperliquidEnvironment) -> &'static str {
100    match environment {
101        HyperliquidEnvironment::Testnet => HYPERLIQUID_TESTNET_INFO_URL,
102        HyperliquidEnvironment::Mainnet => HYPERLIQUID_INFO_URL,
103    }
104}
105
106/// Gets exchange API URL for the specified environment.
107pub fn exchange_url(environment: HyperliquidEnvironment) -> &'static str {
108    match environment {
109        HyperliquidEnvironment::Testnet => HYPERLIQUID_TESTNET_EXCHANGE_URL,
110        HyperliquidEnvironment::Mainnet => HYPERLIQUID_EXCHANGE_URL,
111    }
112}
113
114// Default configuration values
115// Server closes if no message in last 60s, so ping every 30s
116pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(30);
117pub const RECONNECT_BASE_BACKOFF: Duration = Duration::from_millis(250);
118pub const RECONNECT_MAX_BACKOFF: Duration = Duration::from_secs(30);
119pub const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
120// Max 100 inflight WS post messages per Hyperliquid docs
121pub const INFLIGHT_MAX: usize = 100;
122pub const QUEUE_MAX: usize = 1000;
123
124#[cfg(test)]
125mod tests {
126    use rstest::rstest;
127
128    use super::*;
129
130    #[rstest]
131    fn test_ws_url() {
132        assert_eq!(ws_url(HyperliquidEnvironment::Mainnet), HYPERLIQUID_WS_URL);
133        assert_eq!(
134            ws_url(HyperliquidEnvironment::Testnet),
135            HYPERLIQUID_TESTNET_WS_URL
136        );
137    }
138
139    #[rstest]
140    fn test_info_url() {
141        assert_eq!(
142            info_url(HyperliquidEnvironment::Mainnet),
143            HYPERLIQUID_INFO_URL
144        );
145        assert_eq!(
146            info_url(HyperliquidEnvironment::Testnet),
147            HYPERLIQUID_TESTNET_INFO_URL
148        );
149    }
150
151    #[rstest]
152    fn test_exchange_url() {
153        assert_eq!(
154            exchange_url(HyperliquidEnvironment::Mainnet),
155            HYPERLIQUID_EXCHANGE_URL
156        );
157        assert_eq!(
158            exchange_url(HyperliquidEnvironment::Testnet),
159            HYPERLIQUID_TESTNET_EXCHANGE_URL
160        );
161    }
162
163    #[rstest]
164    fn test_constants_values() {
165        assert_eq!(HEARTBEAT_INTERVAL, Duration::from_secs(30));
166        assert_eq!(RECONNECT_BASE_BACKOFF, Duration::from_millis(250));
167        assert_eq!(RECONNECT_MAX_BACKOFF, Duration::from_secs(30));
168        assert_eq!(HTTP_TIMEOUT, Duration::from_secs(10));
169        assert_eq!(INFLIGHT_MAX, 100);
170        assert_eq!(QUEUE_MAX, 1000);
171    }
172}