Skip to main content

nautilus_lighter/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 Lighter configuration.
17
18use nautilus_core::{python::to_pyvalue_err, string::secret::SecretString};
19use nautilus_model::identifiers::{AccountId, Venue};
20use nautilus_network::websocket::TransportBackend;
21use pyo3::{PyResult, pymethods};
22
23use crate::{
24    common::enums::{LighterDeployment, LighterEnvironment},
25    config::{
26        LighterDataClientConfig, LighterExecutionClientConfig, validate_execution_venue,
27        validate_execution_ws_url, validate_market_order_slippage_bps, validate_proxy_url,
28        validate_scheduled_cancel_timeout,
29    },
30};
31
32#[pymethods]
33#[pyo3_stub_gen::derive::gen_stub_pymethods]
34impl LighterDataClientConfig {
35    /// Configuration for the Lighter live data client.
36    #[new]
37    #[pyo3(signature = (
38        base_url_http = None,
39        base_url_ws = None,
40        proxy_url = None,
41        environment = None,
42        account_index = None,
43        api_key_index = None,
44        private_key = None,
45        http_timeout_secs = None,
46        ws_timeout_secs = None,
47        update_instruments_interval_mins = None,
48        rest_quota_per_min = None,
49        transport_backend = None,
50        deployment = None,
51        venue = None,
52        book_snapshot_timeout_secs = None,
53    ))]
54    #[expect(clippy::too_many_arguments)]
55    fn py_new(
56        base_url_http: Option<String>,
57        base_url_ws: Option<String>,
58        proxy_url: Option<String>,
59        environment: Option<LighterEnvironment>,
60        account_index: Option<u64>,
61        api_key_index: Option<u8>,
62        private_key: Option<String>,
63        http_timeout_secs: Option<u64>,
64        ws_timeout_secs: Option<u64>,
65        update_instruments_interval_mins: Option<u64>,
66        rest_quota_per_min: Option<u32>,
67        transport_backend: Option<TransportBackend>,
68        deployment: Option<LighterDeployment>,
69        venue: Option<Venue>,
70        book_snapshot_timeout_secs: Option<u64>,
71    ) -> PyResult<Self> {
72        let proxy_url = proxy_url.map(SecretString::from);
73        validate_proxy_url(proxy_url.as_ref()).map_err(to_pyvalue_err)?;
74        let defaults = Self::default();
75        Ok(Self {
76            environment: environment.unwrap_or(defaults.environment),
77            deployment: deployment.unwrap_or(defaults.deployment),
78            venue,
79            account_index,
80            api_key_index,
81            private_key: private_key.map(SecretString::from),
82            base_url_http,
83            base_url_ws,
84            proxy_url,
85            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
86            ws_timeout_secs: ws_timeout_secs.unwrap_or(defaults.ws_timeout_secs),
87            update_instruments_interval_mins: update_instruments_interval_mins
88                .unwrap_or(defaults.update_instruments_interval_mins),
89            book_snapshot_timeout_secs: book_snapshot_timeout_secs
90                .unwrap_or(defaults.book_snapshot_timeout_secs),
91            rest_quota_per_min,
92            transport_backend: transport_backend.unwrap_or(defaults.transport_backend),
93        })
94    }
95
96    #[getter]
97    const fn has_proxy_url(&self) -> bool {
98        self.proxy_url.is_some()
99    }
100
101    fn __repr__(&self) -> String {
102        stringify!(LighterDataClientConfig).to_string()
103    }
104}
105
106#[pymethods]
107#[pyo3_stub_gen::derive::gen_stub_pymethods]
108impl LighterExecutionClientConfig {
109    /// Configuration for the Lighter live execution client.
110    #[new]
111    #[pyo3(signature = (
112        account_id,
113        account_index = None,
114        api_key_index = None,
115        private_key = None,
116        base_url_http = None,
117        base_url_ws = None,
118        proxy_url = None,
119        environment = None,
120        http_timeout_secs = None,
121        ws_timeout_secs = None,
122        market_order_slippage_bps = None,
123        rest_quota_per_min = None,
124        sendtx_quota_per_min = None,
125        transport_backend = None,
126        deployment = None,
127        venue = None,
128        use_gtd = true,
129        scheduled_cancel_timeout_secs = None,
130    ))]
131    #[expect(clippy::too_many_arguments)]
132    fn py_new(
133        account_id: AccountId,
134        account_index: Option<u64>,
135        api_key_index: Option<u8>,
136        private_key: Option<String>,
137        base_url_http: Option<String>,
138        base_url_ws: Option<String>,
139        proxy_url: Option<String>,
140        environment: Option<LighterEnvironment>,
141        http_timeout_secs: Option<u64>,
142        ws_timeout_secs: Option<u64>,
143        market_order_slippage_bps: Option<u32>,
144        rest_quota_per_min: Option<u32>,
145        sendtx_quota_per_min: Option<u32>,
146        transport_backend: Option<TransportBackend>,
147        deployment: Option<LighterDeployment>,
148        venue: Option<Venue>,
149        use_gtd: bool,
150        scheduled_cancel_timeout_secs: Option<u64>,
151    ) -> PyResult<Self> {
152        validate_execution_venue(venue).map_err(to_pyvalue_err)?;
153        validate_execution_ws_url(base_url_ws.as_deref()).map_err(to_pyvalue_err)?;
154        let proxy_url = proxy_url.map(SecretString::from);
155        validate_proxy_url(proxy_url.as_ref()).map_err(to_pyvalue_err)?;
156        validate_scheduled_cancel_timeout(scheduled_cancel_timeout_secs).map_err(to_pyvalue_err)?;
157        let defaults = Self::default();
158        let market_order_slippage_bps =
159            market_order_slippage_bps.unwrap_or(defaults.market_order_slippage_bps);
160        validate_market_order_slippage_bps(market_order_slippage_bps).map_err(to_pyvalue_err)?;
161        Ok(Self {
162            environment: environment.unwrap_or(defaults.environment),
163            deployment: deployment.unwrap_or(defaults.deployment),
164            venue,
165            account_id,
166            account_index,
167            api_key_index,
168            private_key: private_key.map(SecretString::from),
169            base_url_http,
170            base_url_ws,
171            proxy_url,
172            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
173            ws_timeout_secs: ws_timeout_secs.unwrap_or(defaults.ws_timeout_secs),
174            market_order_slippage_bps,
175            rest_quota_per_min,
176            sendtx_quota_per_min,
177            transport_backend: transport_backend.unwrap_or(defaults.transport_backend),
178            use_gtd,
179            scheduled_cancel_timeout_secs,
180        })
181    }
182
183    #[getter]
184    const fn has_proxy_url(&self) -> bool {
185        self.proxy_url.is_some()
186    }
187
188    fn __repr__(&self) -> String {
189        stringify!(LighterExecutionClientConfig).to_string()
190    }
191}