Skip to main content

nautilus_sandbox/
factory.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//! Factory for creating sandbox execution clients.
17
18use std::{any::Any, cell::RefCell, rc::Rc};
19
20use nautilus_common::{
21    cache::Cache, clients::ExecutionClient, clock::Clock, live::clock::LiveClock,
22};
23use nautilus_execution::client::core::ExecutionClientCore;
24use nautilus_model::identifiers::ClientId;
25use nautilus_system::factories::{ClientConfig, ExecutionClientFactory};
26
27use crate::{config::SandboxExecutionClientConfig, execution::SandboxExecutionClient};
28
29impl ClientConfig for SandboxExecutionClientConfig {
30    fn as_any(&self) -> &dyn Any {
31        self
32    }
33}
34
35/// Factory for creating sandbox execution clients.
36#[derive(Debug, Default)]
37pub struct SandboxExecutionClientFactory;
38
39impl SandboxExecutionClientFactory {
40    /// Creates a new [`SandboxExecutionClientFactory`] instance.
41    #[must_use]
42    pub const fn new() -> Self {
43        Self
44    }
45}
46
47impl ExecutionClientFactory for SandboxExecutionClientFactory {
48    fn create(
49        &self,
50        name: &str,
51        config: &dyn ClientConfig,
52        cache: Rc<RefCell<Cache>>,
53    ) -> anyhow::Result<Box<dyn ExecutionClient>> {
54        let sandbox_config = config
55            .as_any()
56            .downcast_ref::<SandboxExecutionClientConfig>()
57            .ok_or_else(|| {
58                anyhow::anyhow!(
59                    "Invalid config type for SandboxExecutionClientFactory. Expected SandboxExecutionClientConfig, was {config:?}",
60                )
61            })?
62            .clone();
63
64        let client_id = ClientId::from(name);
65        let clock: Rc<RefCell<dyn Clock>> = Rc::new(RefCell::new(LiveClock::default()));
66
67        let core = ExecutionClientCore::new(
68            sandbox_config.trader_id,
69            client_id,
70            sandbox_config.venue,
71            sandbox_config.oms_type,
72            sandbox_config.account_id,
73            sandbox_config.account_type,
74            sandbox_config.base_currency,
75            cache.clone(),
76        );
77
78        let client = SandboxExecutionClient::new(core, sandbox_config, clock, cache);
79        Ok(Box::new(client))
80    }
81
82    fn name(&self) -> &'static str {
83        "SANDBOX"
84    }
85
86    fn config_type(&self) -> &'static str {
87        "SandboxExecutionClientConfig"
88    }
89}