nautilus_hyperliquid/common/
consts.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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
25// Mainnet URLs
26pub const HYPERLIQUID_WS_URL: &str = "wss://api.hyperliquid.xyz/ws";
27pub const HYPERLIQUID_INFO_URL: &str = "https://api.hyperliquid.xyz/info";
28pub const HYPERLIQUID_EXCHANGE_URL: &str = "https://api.hyperliquid.xyz/exchange";
29
30// Testnet URLs
31pub const HYPERLIQUID_TESTNET_WS_URL: &str = "wss://api.hyperliquid-testnet.xyz/ws";
32pub const HYPERLIQUID_TESTNET_INFO_URL: &str = "https://api.hyperliquid-testnet.xyz/info";
33pub const HYPERLIQUID_TESTNET_EXCHANGE_URL: &str = "https://api.hyperliquid-testnet.xyz/exchange";
34
35/// Hyperliquid supported order types.
36///
37/// # Notes
38///
39/// - All order types support trigger prices except Market and Limit.
40/// - Conditional orders follow patterns from OKX, Bybit, and BitMEX adapters.
41/// - Stop orders (StopMarket/StopLimit) are protective stops (sl).
42/// - If Touched orders (MarketIfTouched/LimitIfTouched) are profit-taking or entry orders (tp).
43/// - Post-only orders are implemented via ALO (Add Liquidity Only) time-in-force.
44///
45/// # Trigger Semantics
46///
47/// Hyperliquid uses last traded price for trigger evaluation.
48/// Future enhancement: Add support for mark/index price triggers if API supports it.
49pub const HYPERLIQUID_SUPPORTED_ORDER_TYPES: &[OrderType] = &[
50    OrderType::Market,          // IOC limit order
51    OrderType::Limit,           // Standard limit with GTC/IOC/ALO
52    OrderType::StopMarket,      // Protective stop with market execution
53    OrderType::StopLimit,       // Protective stop with limit price
54    OrderType::MarketIfTouched, // Profit-taking/entry with market execution
55    OrderType::LimitIfTouched,  // Profit-taking/entry with limit price
56];
57
58/// Conditional order types that use trigger orders on Hyperliquid.
59///
60/// These order types require a trigger_price and are implemented using
61/// HyperliquidExecOrderKind::Trigger with appropriate parameters.
62pub const HYPERLIQUID_CONDITIONAL_ORDER_TYPES: &[OrderType] = &[
63    OrderType::StopMarket,
64    OrderType::StopLimit,
65    OrderType::MarketIfTouched,
66    OrderType::LimitIfTouched,
67];
68
69/// Gets WebSocket URL for the specified network.
70pub fn ws_url(is_testnet: bool) -> &'static str {
71    if is_testnet {
72        HYPERLIQUID_TESTNET_WS_URL
73    } else {
74        HYPERLIQUID_WS_URL
75    }
76}
77
78/// Gets info API URL for the specified network.
79pub fn info_url(is_testnet: bool) -> &'static str {
80    if is_testnet {
81        HYPERLIQUID_TESTNET_INFO_URL
82    } else {
83        HYPERLIQUID_INFO_URL
84    }
85}
86
87/// Gets exchange API URL for the specified network.
88pub fn exchange_url(is_testnet: bool) -> &'static str {
89    if is_testnet {
90        HYPERLIQUID_TESTNET_EXCHANGE_URL
91    } else {
92        HYPERLIQUID_EXCHANGE_URL
93    }
94}
95
96// Default configuration values
97// Server closes if no message in last 60s, so ping every 30s
98pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(30);
99pub const RECONNECT_BASE_BACKOFF: Duration = Duration::from_millis(250);
100pub const RECONNECT_MAX_BACKOFF: Duration = Duration::from_secs(30);
101pub const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
102// Max 100 inflight WS post messages per Hyperliquid docs
103pub const INFLIGHT_MAX: usize = 100;
104pub const QUEUE_MAX: usize = 1000;
105
106////////////////////////////////////////////////////////////////////////////////
107// Tests
108////////////////////////////////////////////////////////////////////////////////
109
110#[cfg(test)]
111mod tests {
112    use rstest::rstest;
113
114    use super::*;
115
116    #[rstest]
117    fn test_ws_url() {
118        assert_eq!(ws_url(false), HYPERLIQUID_WS_URL);
119        assert_eq!(ws_url(true), HYPERLIQUID_TESTNET_WS_URL);
120    }
121
122    #[rstest]
123    fn test_info_url() {
124        assert_eq!(info_url(false), HYPERLIQUID_INFO_URL);
125        assert_eq!(info_url(true), HYPERLIQUID_TESTNET_INFO_URL);
126    }
127
128    #[rstest]
129    fn test_exchange_url() {
130        assert_eq!(exchange_url(false), HYPERLIQUID_EXCHANGE_URL);
131        assert_eq!(exchange_url(true), HYPERLIQUID_TESTNET_EXCHANGE_URL);
132    }
133
134    #[rstest]
135    fn test_constants_values() {
136        assert_eq!(HEARTBEAT_INTERVAL, Duration::from_secs(30));
137        assert_eq!(RECONNECT_BASE_BACKOFF, Duration::from_millis(250));
138        assert_eq!(RECONNECT_MAX_BACKOFF, Duration::from_secs(30));
139        assert_eq!(HTTP_TIMEOUT, Duration::from_secs(10));
140        assert_eq!(INFLIGHT_MAX, 100);
141        assert_eq!(QUEUE_MAX, 1000);
142    }
143}