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