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 codes fee configuration for rebates
34// See: https://hyperliquid.gitbook.io/hyperliquid-docs/trading/builder-codes
35// Fee is specified in tenths of a basis point (0.1 bps)
36// Note: Address MUST be lowercase for msgpack serialization to match Python SDK
37pub const NAUTILUS_BUILDER_FEE_ADDRESS: &str = "0x0c8d970c462726e014ad36f6c5a63e99db48a8e7";
38pub const NAUTILUS_BUILDER_FEE_TENTHS_BP: u32 = 10; // 1 bp = 0.01%
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";
43pub const HYPERLIQUID_BUILDER_FEE_NOT_APPROVED: &str = "Builder fee has not been approved";
44
45/// Hyperliquid supported order types.
46///
47/// # Notes
48///
49/// - All order types support trigger prices except Market and Limit.
50/// - Conditional orders follow patterns from OKX, Bybit, and BitMEX adapters.
51/// - Stop orders (StopMarket/StopLimit) are protective stops (sl).
52/// - If Touched orders (MarketIfTouched/LimitIfTouched) are profit-taking or entry orders (tp).
53/// - Post-only orders are implemented via ALO (Add Liquidity Only) time-in-force.
54pub const HYPERLIQUID_SUPPORTED_ORDER_TYPES: &[OrderType] = &[
55    OrderType::Market,          // IOC limit order
56    OrderType::Limit,           // Standard limit with GTC/IOC/ALO
57    OrderType::StopMarket,      // Protective stop with market execution
58    OrderType::StopLimit,       // Protective stop with limit price
59    OrderType::MarketIfTouched, // Profit-taking/entry with market execution
60    OrderType::LimitIfTouched,  // Profit-taking/entry with limit price
61];
62
63/// Conditional order types that use trigger orders on Hyperliquid.
64///
65/// These order types require a trigger_price and are implemented using
66/// HyperliquidExecOrderKind::Trigger with appropriate parameters.
67pub const HYPERLIQUID_CONDITIONAL_ORDER_TYPES: &[OrderType] = &[
68    OrderType::StopMarket,
69    OrderType::StopLimit,
70    OrderType::MarketIfTouched,
71    OrderType::LimitIfTouched,
72];
73
74/// Gets WebSocket URL for the specified network.
75pub fn ws_url(is_testnet: bool) -> &'static str {
76    if is_testnet {
77        HYPERLIQUID_TESTNET_WS_URL
78    } else {
79        HYPERLIQUID_WS_URL
80    }
81}
82
83/// Gets info API URL for the specified network.
84pub fn info_url(is_testnet: bool) -> &'static str {
85    if is_testnet {
86        HYPERLIQUID_TESTNET_INFO_URL
87    } else {
88        HYPERLIQUID_INFO_URL
89    }
90}
91
92/// Gets exchange API URL for the specified network.
93pub fn exchange_url(is_testnet: bool) -> &'static str {
94    if is_testnet {
95        HYPERLIQUID_TESTNET_EXCHANGE_URL
96    } else {
97        HYPERLIQUID_EXCHANGE_URL
98    }
99}
100
101// Default configuration values
102// Server closes if no message in last 60s, so ping every 30s
103pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(30);
104pub const RECONNECT_BASE_BACKOFF: Duration = Duration::from_millis(250);
105pub const RECONNECT_MAX_BACKOFF: Duration = Duration::from_secs(30);
106pub const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
107// Max 100 inflight WS post messages per Hyperliquid docs
108pub const INFLIGHT_MAX: usize = 100;
109pub const QUEUE_MAX: usize = 1000;
110
111#[cfg(test)]
112mod tests {
113    use rstest::rstest;
114
115    use super::*;
116
117    #[rstest]
118    fn test_ws_url() {
119        assert_eq!(ws_url(false), HYPERLIQUID_WS_URL);
120        assert_eq!(ws_url(true), HYPERLIQUID_TESTNET_WS_URL);
121    }
122
123    #[rstest]
124    fn test_info_url() {
125        assert_eq!(info_url(false), HYPERLIQUID_INFO_URL);
126        assert_eq!(info_url(true), HYPERLIQUID_TESTNET_INFO_URL);
127    }
128
129    #[rstest]
130    fn test_exchange_url() {
131        assert_eq!(exchange_url(false), HYPERLIQUID_EXCHANGE_URL);
132        assert_eq!(exchange_url(true), HYPERLIQUID_TESTNET_EXCHANGE_URL);
133    }
134
135    #[rstest]
136    fn test_constants_values() {
137        assert_eq!(HEARTBEAT_INTERVAL, Duration::from_secs(30));
138        assert_eq!(RECONNECT_BASE_BACKOFF, Duration::from_millis(250));
139        assert_eq!(RECONNECT_MAX_BACKOFF, Duration::from_secs(30));
140        assert_eq!(HTTP_TIMEOUT, Duration::from_secs(10));
141        assert_eq!(INFLIGHT_MAX, 100);
142        assert_eq!(QUEUE_MAX, 1000);
143    }
144}