nautilus_hyperliquid/config.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
16//! Configuration structures for the Hyperliquid adapter.
17
18use crate::common::consts::{info_url, ws_url};
19
20/// Configuration for the Hyperliquid data client.
21#[derive(Clone, Debug)]
22pub struct HyperliquidDataClientConfig {
23 /// Optional private key for authenticated endpoints.
24 pub private_key: Option<String>,
25 /// Override for the WebSocket URL.
26 pub base_url_ws: Option<String>,
27 /// Override for the HTTP info URL.
28 pub base_url_http: Option<String>,
29 /// Optional HTTP proxy URL.
30 pub http_proxy_url: Option<String>,
31 /// Optional WebSocket proxy URL.
32 ///
33 /// Note: WebSocket proxy support is not yet implemented. This field is reserved
34 /// for future functionality. Use `http_proxy_url` for REST API proxy support.
35 pub ws_proxy_url: Option<String>,
36 /// When true the client will use Hyperliquid testnet endpoints.
37 pub is_testnet: bool,
38 /// HTTP timeout in seconds.
39 pub http_timeout_secs: Option<u64>,
40 /// WebSocket timeout in seconds.
41 pub ws_timeout_secs: Option<u64>,
42 /// Optional interval for refreshing instruments.
43 pub update_instruments_interval_mins: Option<u64>,
44}
45
46impl Default for HyperliquidDataClientConfig {
47 fn default() -> Self {
48 Self {
49 private_key: None,
50 base_url_ws: None,
51 base_url_http: None,
52 http_proxy_url: None,
53 ws_proxy_url: None,
54 is_testnet: false,
55 http_timeout_secs: Some(60),
56 ws_timeout_secs: Some(30),
57 update_instruments_interval_mins: Some(60),
58 }
59 }
60}
61
62impl HyperliquidDataClientConfig {
63 /// Creates a new configuration with default settings.
64 #[must_use]
65 pub fn new() -> Self {
66 Self::default()
67 }
68
69 /// Returns `true` when private key is populated.
70 #[must_use]
71 pub fn has_credentials(&self) -> bool {
72 self.private_key.is_some()
73 }
74
75 /// Returns the WebSocket URL, respecting the testnet flag and overrides.
76 #[must_use]
77 pub fn ws_url(&self) -> String {
78 self.base_url_ws
79 .clone()
80 .unwrap_or_else(|| ws_url(self.is_testnet).to_string())
81 }
82
83 /// Returns the HTTP info URL, respecting the testnet flag and overrides.
84 #[must_use]
85 pub fn http_url(&self) -> String {
86 self.base_url_http
87 .clone()
88 .unwrap_or_else(|| info_url(self.is_testnet).to_string())
89 }
90}
91
92/// Configuration for the Hyperliquid execution client.
93#[derive(Clone, Debug)]
94pub struct HyperliquidExecClientConfig {
95 /// Private key for signing transactions (required for execution).
96 pub private_key: String,
97 /// Optional vault address for vault operations.
98 pub vault_address: Option<String>,
99 /// Override for the WebSocket URL.
100 pub base_url_ws: Option<String>,
101 /// Override for the HTTP info URL.
102 pub base_url_http: Option<String>,
103 /// Override for the exchange API URL.
104 pub base_url_exchange: Option<String>,
105 /// Optional HTTP proxy URL.
106 pub http_proxy_url: Option<String>,
107 /// Optional WebSocket proxy URL.
108 ///
109 /// Note: WebSocket proxy support is not yet implemented. This field is reserved
110 /// for future functionality. Use `http_proxy_url` for REST API proxy support.
111 pub ws_proxy_url: Option<String>,
112 /// When true the client will use Hyperliquid testnet endpoints.
113 pub is_testnet: bool,
114 /// HTTP timeout in seconds.
115 pub http_timeout_secs: u64,
116 /// Maximum number of retry attempts for HTTP requests.
117 pub max_retries: u32,
118 /// Initial retry delay in milliseconds.
119 pub retry_delay_initial_ms: u64,
120 /// Maximum retry delay in milliseconds.
121 pub retry_delay_max_ms: u64,
122}
123
124impl Default for HyperliquidExecClientConfig {
125 fn default() -> Self {
126 Self {
127 private_key: String::new(),
128 vault_address: None,
129 base_url_ws: None,
130 base_url_http: None,
131 base_url_exchange: None,
132 http_proxy_url: None,
133 ws_proxy_url: None,
134 is_testnet: false,
135 http_timeout_secs: 60,
136 max_retries: 3,
137 retry_delay_initial_ms: 100,
138 retry_delay_max_ms: 5000,
139 }
140 }
141}
142
143impl HyperliquidExecClientConfig {
144 /// Creates a new configuration with the provided private key.
145 #[must_use]
146 pub fn new(private_key: String) -> Self {
147 Self {
148 private_key,
149 ..Self::default()
150 }
151 }
152
153 /// Returns `true` when private key is populated.
154 #[must_use]
155 pub fn has_credentials(&self) -> bool {
156 !self.private_key.is_empty()
157 }
158
159 /// Returns the WebSocket URL, respecting the testnet flag and overrides.
160 #[must_use]
161 pub fn ws_url(&self) -> String {
162 self.base_url_ws
163 .clone()
164 .unwrap_or_else(|| ws_url(self.is_testnet).to_string())
165 }
166
167 /// Returns the HTTP info URL, respecting the testnet flag and overrides.
168 #[must_use]
169 pub fn http_url(&self) -> String {
170 self.base_url_http
171 .clone()
172 .unwrap_or_else(|| info_url(self.is_testnet).to_string())
173 }
174}