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