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