nautilus_okx/
factories.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//! Factory functions for creating OKX clients and components.
17
18use std::{any::Any, cell::RefCell, rc::Rc};
19
20use nautilus_common::{cache::Cache, clock::Clock};
21use nautilus_data::client::DataClient;
22use nautilus_model::identifiers::ClientId;
23use nautilus_system::factories::{ClientConfig, DataClientFactory};
24
25use crate::{config::OKXDataClientConfig, data::OKXDataClient};
26
27impl ClientConfig for OKXDataClientConfig {
28    fn as_any(&self) -> &dyn Any {
29        self
30    }
31}
32
33/// Factory for creating OKX data clients.
34#[derive(Debug)]
35pub struct OKXDataClientFactory;
36
37impl OKXDataClientFactory {
38    /// Creates a new [`OKXDataClientFactory`] instance.
39    #[must_use]
40    pub const fn new() -> Self {
41        Self
42    }
43}
44
45impl Default for OKXDataClientFactory {
46    fn default() -> Self {
47        Self::new()
48    }
49}
50
51impl DataClientFactory for OKXDataClientFactory {
52    fn create(
53        &self,
54        name: &str,
55        config: &dyn ClientConfig,
56        _cache: Rc<RefCell<Cache>>,
57        _clock: Rc<RefCell<dyn Clock>>,
58    ) -> anyhow::Result<Box<dyn DataClient>> {
59        let okx_config = config
60            .as_any()
61            .downcast_ref::<OKXDataClientConfig>()
62            .ok_or_else(|| {
63                anyhow::anyhow!(
64                    "Invalid config type for OKXDataClientFactory. Expected OKXDataClientConfig, was {config:?}",
65                )
66            })?
67            .clone();
68
69        let client_id = ClientId::from(name);
70        let client = OKXDataClient::new(client_id, okx_config)?;
71        Ok(Box::new(client))
72    }
73
74    fn name(&self) -> &'static str {
75        "OKX"
76    }
77
78    fn config_type(&self) -> &'static str {
79        "OKXDataClientConfig"
80    }
81}
82
83// TODO: Implement OKXExecutionClientFactory when needed for execution testing