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