Skip to main content

nautilus_hyperliquid/python/
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//! Python bindings for Hyperliquid configuration.
17
18use nautilus_core::string::secret::SecretString;
19use nautilus_model::identifiers::AccountId;
20use nautilus_network::websocket::TransportBackend;
21use pyo3::prelude::*;
22
23use crate::{
24    common::enums::HyperliquidEnvironment,
25    config::{HyperliquidDataClientConfig, HyperliquidExecutionClientConfig},
26};
27
28#[pymethods]
29#[pyo3_stub_gen::derive::gen_stub_pymethods]
30impl HyperliquidDataClientConfig {
31    /// Configuration for the Hyperliquid data client.
32    ///
33    /// The `stale_stream_*` options control the stream health monitor. With recovery
34    /// enabled, a stale stream is warned about first, then acted on once per recovery
35    /// cooldown. A stale order book delta stream, including depth that shares its stream,
36    /// starts shared book recovery, which resubscribes until a fresh snapshot arrives.
37    /// Depth-only and BBO streams receive a targeted resubscribe (preserving the original
38    /// `l2Book` options) and escalate to a full WebSocket reconnect after
39    /// `stale_stream_max_targeted_resubscribes` failed attempts. Fresh data resets the ladder. See the Hyperliquid integration guide
40    /// ("Stream health and recovery") for details.
41    #[new]
42    #[pyo3(signature = (
43        environment = None,
44        private_key = None,
45        base_url_http = None,
46        base_url_ws = None,
47        proxy_url = None,
48        http_timeout_secs = None,
49        ws_timeout_secs = None,
50        update_instruments_interval_mins = None,
51        transport_backend = None,
52        stale_stream_receive_timeout_secs = None,
53        stream_health_check_interval_secs = None,
54        stale_stream_warning_cooldown_secs = None,
55        stale_stream_recovery_enabled = None,
56        stale_stream_recovery_cooldown_secs = None,
57        stale_stream_max_targeted_resubscribes = None,
58        book_snapshot_timeout_secs = None,
59    ))]
60    #[expect(clippy::too_many_arguments)]
61    fn py_new(
62        environment: Option<HyperliquidEnvironment>,
63        private_key: Option<String>,
64        base_url_http: Option<String>,
65        base_url_ws: Option<String>,
66        proxy_url: Option<String>,
67        http_timeout_secs: Option<u64>,
68        ws_timeout_secs: Option<u64>,
69        update_instruments_interval_mins: Option<u64>,
70        transport_backend: Option<TransportBackend>,
71        stale_stream_receive_timeout_secs: Option<u64>,
72        stream_health_check_interval_secs: Option<u64>,
73        stale_stream_warning_cooldown_secs: Option<u64>,
74        stale_stream_recovery_enabled: Option<bool>,
75        stale_stream_recovery_cooldown_secs: Option<u64>,
76        stale_stream_max_targeted_resubscribes: Option<u32>,
77        book_snapshot_timeout_secs: Option<u64>,
78    ) -> Self {
79        let defaults = Self::default();
80        Self {
81            private_key: private_key.map(SecretString::from),
82            base_url_http,
83            base_url_ws,
84            proxy_url: proxy_url.map(SecretString::from),
85            environment: environment.unwrap_or(defaults.environment),
86            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
87            ws_timeout_secs: ws_timeout_secs.unwrap_or(defaults.ws_timeout_secs),
88            stale_stream_receive_timeout_secs: stale_stream_receive_timeout_secs
89                .unwrap_or(defaults.stale_stream_receive_timeout_secs),
90            stream_health_check_interval_secs: stream_health_check_interval_secs
91                .unwrap_or(defaults.stream_health_check_interval_secs),
92            stale_stream_warning_cooldown_secs: stale_stream_warning_cooldown_secs
93                .unwrap_or(defaults.stale_stream_warning_cooldown_secs),
94            stale_stream_recovery_enabled: stale_stream_recovery_enabled
95                .unwrap_or(defaults.stale_stream_recovery_enabled),
96            stale_stream_recovery_cooldown_secs: stale_stream_recovery_cooldown_secs
97                .unwrap_or(defaults.stale_stream_recovery_cooldown_secs),
98            stale_stream_max_targeted_resubscribes: stale_stream_max_targeted_resubscribes
99                .unwrap_or(defaults.stale_stream_max_targeted_resubscribes),
100            book_snapshot_timeout_secs: book_snapshot_timeout_secs
101                .unwrap_or(defaults.book_snapshot_timeout_secs),
102            update_instruments_interval_mins: update_instruments_interval_mins
103                .unwrap_or(defaults.update_instruments_interval_mins),
104            transport_backend: transport_backend.unwrap_or(defaults.transport_backend),
105        }
106    }
107
108    #[getter]
109    const fn has_proxy_url(&self) -> bool {
110        self.proxy_url.is_some()
111    }
112
113    fn __repr__(&self) -> String {
114        stringify!(HyperliquidDataClientConfig).to_string()
115    }
116}
117
118#[pymethods]
119#[pyo3_stub_gen::derive::gen_stub_pymethods]
120impl HyperliquidExecutionClientConfig {
121    /// Configuration for the Hyperliquid execution client.
122    #[new]
123    #[pyo3(signature = (
124        account_id = None,
125        private_key = None,
126        vault_address = None,
127        account_address = None,
128        environment = None,
129        base_url_http = None,
130        base_url_ws = None,
131        base_url_exchange = None,
132        proxy_url = None,
133        http_timeout_secs = None,
134        max_retries = None,
135        retry_delay_initial_ms = None,
136        retry_delay_max_ms = None,
137        normalize_prices = None,
138        market_order_slippage_bps = None,
139        include_builder_attribution = None,
140        ws_post_timeout_secs = None,
141        transport_backend = None,
142    ))]
143    #[expect(clippy::too_many_arguments)]
144    fn py_new(
145        account_id: Option<AccountId>,
146        private_key: Option<String>,
147        vault_address: Option<String>,
148        account_address: Option<String>,
149        environment: Option<HyperliquidEnvironment>,
150        base_url_http: Option<String>,
151        base_url_ws: Option<String>,
152        base_url_exchange: Option<String>,
153        proxy_url: Option<String>,
154        http_timeout_secs: Option<u64>,
155        max_retries: Option<u32>,
156        retry_delay_initial_ms: Option<u64>,
157        retry_delay_max_ms: Option<u64>,
158        normalize_prices: Option<bool>,
159        market_order_slippage_bps: Option<u32>,
160        include_builder_attribution: Option<bool>,
161        ws_post_timeout_secs: Option<u64>,
162        transport_backend: Option<TransportBackend>,
163    ) -> Self {
164        let defaults = Self::default();
165        Self {
166            account_id: account_id.unwrap_or(defaults.account_id),
167            private_key: private_key.map(SecretString::from),
168            vault_address,
169            account_address,
170            base_url_http,
171            base_url_ws,
172            base_url_exchange,
173            proxy_url: proxy_url.map(SecretString::from),
174            environment: environment.unwrap_or(defaults.environment),
175            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
176            max_retries: max_retries.unwrap_or(defaults.max_retries),
177            retry_delay_initial_ms: retry_delay_initial_ms
178                .unwrap_or(defaults.retry_delay_initial_ms),
179            retry_delay_max_ms: retry_delay_max_ms.unwrap_or(defaults.retry_delay_max_ms),
180            normalize_prices: normalize_prices.unwrap_or(defaults.normalize_prices),
181            market_order_slippage_bps: market_order_slippage_bps
182                .unwrap_or(defaults.market_order_slippage_bps),
183            include_builder_attribution: include_builder_attribution
184                .unwrap_or(defaults.include_builder_attribution),
185            ws_post_timeout_secs: ws_post_timeout_secs.unwrap_or(defaults.ws_post_timeout_secs),
186            transport_backend: transport_backend.unwrap_or(defaults.transport_backend),
187            outcome_settlement_poll_secs: defaults.outcome_settlement_poll_secs,
188        }
189    }
190
191    #[getter]
192    const fn has_proxy_url(&self) -> bool {
193        self.proxy_url.is_some()
194    }
195
196    fn __repr__(&self) -> String {
197        stringify!(HyperliquidExecutionClientConfig).to_string()
198    }
199}