Skip to main content

nautilus_hyperliquid/
config.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
16//! Configuration structures for the Hyperliquid adapter.
17
18use nautilus_network::websocket::TransportBackend;
19use serde::{Deserialize, Serialize};
20
21use crate::common::{
22    consts::{info_url, ws_url},
23    enums::HyperliquidEnvironment,
24};
25
26/// Configuration for the Hyperliquid data client.
27#[derive(Debug, Clone, Serialize, Deserialize, bon::Builder)]
28#[serde(default, deny_unknown_fields)]
29#[cfg_attr(
30    feature = "python",
31    pyo3::pyclass(
32        module = "nautilus_trader.core.nautilus_pyo3.hyperliquid",
33        from_py_object
34    )
35)]
36#[cfg_attr(
37    feature = "python",
38    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.hyperliquid")
39)]
40pub struct HyperliquidDataClientConfig {
41    /// Optional private key for authenticated endpoints.
42    pub private_key: Option<String>,
43    /// Override for the WebSocket URL.
44    pub base_url_ws: Option<String>,
45    /// Override for the HTTP info URL.
46    pub base_url_http: Option<String>,
47    /// Optional proxy URL for HTTP and WebSocket transports.
48    pub proxy_url: Option<String>,
49    /// The target environment (mainnet or testnet).
50    #[builder(default)]
51    pub environment: HyperliquidEnvironment,
52    /// HTTP timeout in seconds.
53    #[builder(default = 60)]
54    pub http_timeout_secs: u64,
55    /// WebSocket timeout in seconds.
56    #[builder(default = 30)]
57    pub ws_timeout_secs: u64,
58    /// Interval for refreshing instruments in minutes.
59    #[builder(default = 60)]
60    pub update_instruments_interval_mins: u64,
61    /// WebSocket transport backend (defaults to `Tungstenite`).
62    #[builder(default)]
63    pub transport_backend: TransportBackend,
64}
65
66impl Default for HyperliquidDataClientConfig {
67    fn default() -> Self {
68        Self::builder().build()
69    }
70}
71
72impl HyperliquidDataClientConfig {
73    /// Creates a new configuration with default settings.
74    #[must_use]
75    pub fn new() -> Self {
76        Self::default()
77    }
78
79    /// Returns `true` when private key is populated and non-empty.
80    #[must_use]
81    pub fn has_credentials(&self) -> bool {
82        self.private_key
83            .as_deref()
84            .is_some_and(|s| !s.trim().is_empty())
85    }
86
87    /// Returns the WebSocket URL, respecting the environment and overrides.
88    #[must_use]
89    pub fn ws_url(&self) -> String {
90        self.base_url_ws
91            .clone()
92            .unwrap_or_else(|| ws_url(self.environment).to_string())
93    }
94
95    /// Returns the HTTP info URL, respecting the environment and overrides.
96    #[must_use]
97    pub fn http_url(&self) -> String {
98        self.base_url_http
99            .clone()
100            .unwrap_or_else(|| info_url(self.environment).to_string())
101    }
102}
103
104/// Configuration for the Hyperliquid execution client.
105#[derive(Debug, Clone, Serialize, Deserialize, bon::Builder)]
106#[serde(default, deny_unknown_fields)]
107#[cfg_attr(
108    feature = "python",
109    pyo3::pyclass(
110        module = "nautilus_trader.core.nautilus_pyo3.hyperliquid",
111        from_py_object
112    )
113)]
114#[cfg_attr(
115    feature = "python",
116    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.hyperliquid")
117)]
118pub struct HyperliquidExecClientConfig {
119    /// Private key for signing transactions.
120    ///
121    /// If not provided, falls back to environment variable:
122    /// - Mainnet: `HYPERLIQUID_PK`
123    /// - Testnet: `HYPERLIQUID_TESTNET_PK`
124    pub private_key: Option<String>,
125    /// Optional vault address for vault operations.
126    pub vault_address: Option<String>,
127    /// Optional main account address when using an agent wallet (API sub-key).
128    /// When set, used for balance queries, position reports, and WS subscriptions
129    /// instead of the address derived from the private key.
130    pub account_address: Option<String>,
131    /// Override for the WebSocket URL.
132    pub base_url_ws: Option<String>,
133    /// Override for the HTTP info URL.
134    pub base_url_http: Option<String>,
135    /// Override for the exchange API URL.
136    pub base_url_exchange: Option<String>,
137    /// Optional proxy URL for HTTP and WebSocket transports.
138    pub proxy_url: Option<String>,
139    /// The target environment (mainnet or testnet).
140    #[builder(default)]
141    pub environment: HyperliquidEnvironment,
142    /// HTTP timeout in seconds.
143    #[builder(default = 60)]
144    pub http_timeout_secs: u64,
145    /// Maximum number of retry attempts for HTTP requests.
146    #[builder(default = 3)]
147    pub max_retries: u32,
148    /// Initial retry delay in milliseconds.
149    #[builder(default = 100)]
150    pub retry_delay_initial_ms: u64,
151    /// Maximum retry delay in milliseconds.
152    #[builder(default = 5000)]
153    pub retry_delay_max_ms: u64,
154    /// When true, normalize order prices to 5 significant figures
155    /// before submission (Hyperliquid requirement).
156    #[builder(default = true)]
157    pub normalize_prices: bool,
158    /// Slippage buffer in basis points applied to MARKET orders and
159    /// stop-to-limit trigger derivations. Can be overridden per-order via
160    /// `SubmitOrder.params["market_order_slippage_bps"]`.
161    #[builder(default = 50)]
162    pub market_order_slippage_bps: u32,
163    /// WebSocket transport backend (defaults to `Tungstenite`).
164    #[builder(default)]
165    pub transport_backend: TransportBackend,
166    /// Poll interval in seconds for `outcomeMeta` settlement detection.
167    /// Disabled by default; venue `Settlement` fills drive HIP-4 settlement
168    /// through the standard user-fills stream. Set to a non-zero value only
169    /// when the venue fill stream is unavailable.
170    #[builder(default = 0)]
171    pub outcome_settlement_poll_secs: u64,
172}
173
174impl Default for HyperliquidExecClientConfig {
175    fn default() -> Self {
176        Self::builder().build()
177    }
178}
179
180impl HyperliquidExecClientConfig {
181    /// Returns `true` when private key is populated and non-empty.
182    #[must_use]
183    pub fn has_credentials(&self) -> bool {
184        self.private_key
185            .as_deref()
186            .is_some_and(|s| !s.trim().is_empty())
187    }
188
189    /// Returns the WebSocket URL, respecting the environment and overrides.
190    #[must_use]
191    pub fn ws_url(&self) -> String {
192        self.base_url_ws
193            .clone()
194            .unwrap_or_else(|| ws_url(self.environment).to_string())
195    }
196
197    /// Returns the HTTP info URL, respecting the environment and overrides.
198    #[must_use]
199    pub fn http_url(&self) -> String {
200        self.base_url_http
201            .clone()
202            .unwrap_or_else(|| info_url(self.environment).to_string())
203    }
204}
205
206#[cfg(test)]
207mod tests {
208    use rstest::rstest;
209
210    use super::*;
211
212    #[rstest]
213    fn test_exec_config_default_account_address_is_none() {
214        let config = HyperliquidExecClientConfig::default();
215        assert!(config.account_address.is_none());
216    }
217
218    #[rstest]
219    fn test_exec_config_with_account_address() {
220        let config = HyperliquidExecClientConfig {
221            account_address: Some("0x1234".to_string()),
222            ..HyperliquidExecClientConfig::default()
223        };
224        assert_eq!(config.account_address.as_deref(), Some("0x1234"));
225    }
226
227    #[rstest]
228    fn test_data_config_toml_minimal() {
229        let config: HyperliquidDataClientConfig = toml::from_str(
230            r#"
231environment = "testnet"
232http_timeout_secs = 30
233update_instruments_interval_mins = 10
234transport_backend = "tungstenite"
235"#,
236        )
237        .unwrap();
238
239        assert_eq!(config.environment, HyperliquidEnvironment::Testnet);
240        assert_eq!(config.http_timeout_secs, 30);
241        assert_eq!(config.update_instruments_interval_mins, 10);
242        assert_eq!(config.transport_backend, TransportBackend::Tungstenite);
243    }
244
245    #[rstest]
246    fn test_exec_config_toml_empty_uses_defaults() {
247        let config: HyperliquidExecClientConfig = toml::from_str("").unwrap();
248        let expected = HyperliquidExecClientConfig::default();
249
250        assert_eq!(config.environment, expected.environment);
251        assert_eq!(config.http_timeout_secs, expected.http_timeout_secs);
252        assert_eq!(config.max_retries, expected.max_retries);
253        assert_eq!(config.normalize_prices, expected.normalize_prices);
254        assert_eq!(
255            config.market_order_slippage_bps,
256            expected.market_order_slippage_bps,
257        );
258        assert_eq!(config.transport_backend, expected.transport_backend);
259        assert_eq!(
260            config.outcome_settlement_poll_secs,
261            expected.outcome_settlement_poll_secs,
262        );
263    }
264}