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