nautilus_okx/
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 OKX adapter.
17
18use crate::common::{
19    enums::{OKXContractType, OKXInstrumentType},
20    urls::{
21        get_http_base_url, get_ws_base_url_business, get_ws_base_url_private,
22        get_ws_base_url_public,
23    },
24};
25
26/// Configuration for the OKX data client.
27#[derive(Clone, Debug)]
28pub struct OKXDataClientConfig {
29    /// Optional API key for authenticated endpoints.
30    pub api_key: Option<String>,
31    /// Optional API secret for authenticated endpoints.
32    pub api_secret: Option<String>,
33    /// Optional API passphrase for authenticated endpoints.
34    pub api_passphrase: Option<String>,
35    /// Instrument types to load and subscribe to.
36    pub instrument_types: Vec<OKXInstrumentType>,
37    /// Contract type filter applied to loaded instruments.
38    pub contract_types: Option<Vec<OKXContractType>>,
39    /// Instrument families to load (e.g., "BTC-USD", "ETH-USD").
40    /// Required for OPTIONS. Optional for FUTURES/SWAP. Not applicable for SPOT/MARGIN.
41    pub instrument_families: Option<Vec<String>>,
42    /// Optional override for the HTTP base URL.
43    pub base_url_http: Option<String>,
44    /// Optional override for the public WebSocket URL.
45    pub base_url_ws_public: Option<String>,
46    /// Optional override for the business WebSocket URL.
47    pub base_url_ws_business: Option<String>,
48    /// When true the client will use OKX demo endpoints.
49    pub is_demo: bool,
50    /// Optional HTTP timeout in seconds.
51    pub http_timeout_secs: Option<u64>,
52    /// Optional interval for refreshing instruments.
53    pub update_instruments_interval_mins: Option<u64>,
54}
55
56impl Default for OKXDataClientConfig {
57    fn default() -> Self {
58        Self {
59            api_key: None,
60            api_secret: None,
61            api_passphrase: None,
62            instrument_types: vec![OKXInstrumentType::Spot],
63            contract_types: None,
64            instrument_families: None,
65            base_url_http: None,
66            base_url_ws_public: None,
67            base_url_ws_business: None,
68            is_demo: false,
69            http_timeout_secs: Some(60),
70            update_instruments_interval_mins: Some(60),
71        }
72    }
73}
74
75impl OKXDataClientConfig {
76    /// Creates a new configuration with default settings.
77    #[must_use]
78    pub fn new() -> Self {
79        Self::default()
80    }
81
82    /// Returns `true` when all API credential fields are populated.
83    #[must_use]
84    pub fn has_api_credentials(&self) -> bool {
85        self.api_key.is_some() && self.api_secret.is_some() && self.api_passphrase.is_some()
86    }
87
88    /// Returns the HTTP base URL, falling back to the default when unset.
89    #[must_use]
90    pub fn http_base_url(&self) -> String {
91        self.base_url_http.clone().unwrap_or_else(get_http_base_url)
92    }
93
94    /// Returns the public WebSocket URL, respecting the demo flag and overrides.
95    #[must_use]
96    pub fn ws_public_url(&self) -> String {
97        self.base_url_ws_public
98            .clone()
99            .unwrap_or_else(|| get_ws_base_url_public(self.is_demo))
100    }
101
102    /// Returns the business WebSocket URL, respecting the demo flag and overrides.
103    #[must_use]
104    pub fn ws_business_url(&self) -> String {
105        self.base_url_ws_business
106            .clone()
107            .unwrap_or_else(|| get_ws_base_url_business(self.is_demo))
108    }
109
110    /// Returns `true` when the business WebSocket should be instantiated.
111    #[must_use]
112    pub fn requires_business_ws(&self) -> bool {
113        self.has_api_credentials()
114    }
115}
116
117/// Configuration for the OKX execution client.
118#[derive(Clone, Debug)]
119pub struct OKXExecClientConfig {
120    /// Optional API key for authenticated endpoints.
121    pub api_key: Option<String>,
122    /// Optional API secret for authenticated endpoints.
123    pub api_secret: Option<String>,
124    /// Optional API passphrase for authenticated endpoints.
125    pub api_passphrase: Option<String>,
126    /// Instrument types the execution client should support.
127    pub instrument_types: Vec<OKXInstrumentType>,
128    /// Contract type filter applied to operations.
129    pub contract_types: Option<Vec<OKXContractType>>,
130    /// Instrument families to load (e.g., "BTC-USD", "ETH-USD").
131    /// Required for OPTIONS. Optional for FUTURES/SWAP. Not applicable for SPOT/MARGIN.
132    pub instrument_families: Option<Vec<String>>,
133    /// Optional override for the HTTP base URL.
134    pub base_url_http: Option<String>,
135    /// Optional override for the private WebSocket URL.
136    pub base_url_ws_private: Option<String>,
137    /// Optional override for the business WebSocket URL.
138    pub base_url_ws_business: Option<String>,
139    /// When true the client will use OKX demo endpoints.
140    pub is_demo: bool,
141    /// Optional HTTP timeout in seconds.
142    pub http_timeout_secs: Option<u64>,
143    /// Enables consumption of the fills WebSocket channel when true.
144    pub use_fills_channel: bool,
145    /// Enables mass-cancel support when true.
146    pub use_mm_mass_cancel: bool,
147    /// Optional maximum retry attempts for requests.
148    pub max_retries: Option<u32>,
149    /// Optional initial retry delay in milliseconds.
150    pub retry_delay_initial_ms: Option<u64>,
151    /// Optional maximum retry delay in milliseconds.
152    pub retry_delay_max_ms: Option<u64>,
153}
154
155impl Default for OKXExecClientConfig {
156    fn default() -> Self {
157        Self {
158            api_key: None,
159            api_secret: None,
160            api_passphrase: None,
161            instrument_types: vec![OKXInstrumentType::Spot],
162            contract_types: None,
163            instrument_families: None,
164            base_url_http: None,
165            base_url_ws_private: None,
166            base_url_ws_business: None,
167            is_demo: false,
168            http_timeout_secs: Some(60),
169            use_fills_channel: false,
170            use_mm_mass_cancel: false,
171            max_retries: Some(3),
172            retry_delay_initial_ms: Some(1_000),
173            retry_delay_max_ms: Some(10_000),
174        }
175    }
176}
177
178impl OKXExecClientConfig {
179    /// Creates a new configuration with default settings.
180    #[must_use]
181    pub fn new() -> Self {
182        Self::default()
183    }
184
185    /// Returns `true` when all API credential fields are populated.
186    #[must_use]
187    pub fn has_api_credentials(&self) -> bool {
188        self.api_key.is_some() && self.api_secret.is_some() && self.api_passphrase.is_some()
189    }
190
191    /// Returns the HTTP base URL, falling back to the default when unset.
192    #[must_use]
193    pub fn http_base_url(&self) -> String {
194        self.base_url_http.clone().unwrap_or_else(get_http_base_url)
195    }
196
197    /// Returns the private WebSocket URL, respecting the demo flag and overrides.
198    #[must_use]
199    pub fn ws_private_url(&self) -> String {
200        self.base_url_ws_private
201            .clone()
202            .unwrap_or_else(|| get_ws_base_url_private(self.is_demo))
203    }
204
205    /// Returns the business WebSocket URL, respecting the demo flag and overrides.
206    #[must_use]
207    pub fn ws_business_url(&self) -> String {
208        self.base_url_ws_business
209            .clone()
210            .unwrap_or_else(|| get_ws_base_url_business(self.is_demo))
211    }
212}