nautilus_sandbox/
factory.rs1use std::{any::Any, cell::RefCell, rc::Rc};
19
20use nautilus_common::{
21 cache::Cache,
22 clients::ExecutionClient,
23 clock::Clock,
24 factories::{ClientConfig, SimulatedExecutionClientFactory},
25 live::clock::LiveClock,
26};
27use nautilus_execution::client::core::ExecutionClientCore;
28use nautilus_model::identifiers::ClientId;
29
30use crate::{config::SandboxExecutionClientConfig, execution::SandboxExecutionClient};
31
32impl ClientConfig for SandboxExecutionClientConfig {
33 fn as_any(&self) -> &dyn Any {
34 self
35 }
36}
37
38#[derive(Debug, Default, Clone)]
40#[cfg_attr(
41 feature = "python",
42 pyo3::pyclass(
43 module = "nautilus_trader.core.nautilus_pyo3.sandbox",
44 unsendable,
45 from_py_object
46 )
47)]
48#[cfg_attr(
49 feature = "python",
50 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.sandbox")
51)]
52pub struct SandboxExecutionClientFactory;
53
54impl SandboxExecutionClientFactory {
55 #[must_use]
57 pub const fn new() -> Self {
58 Self
59 }
60}
61
62impl SimulatedExecutionClientFactory for SandboxExecutionClientFactory {
63 fn create(
64 &self,
65 name: &str,
66 config: &dyn ClientConfig,
67 cache: Rc<RefCell<Cache>>,
68 ) -> anyhow::Result<Box<dyn ExecutionClient>> {
69 let sandbox_config = config
70 .as_any()
71 .downcast_ref::<SandboxExecutionClientConfig>()
72 .ok_or_else(|| {
73 anyhow::anyhow!(
74 "Invalid config type for SandboxExecutionClientFactory. Expected SandboxExecutionClientConfig, was {config:?}",
75 )
76 })?
77 .clone();
78
79 let client_id = ClientId::from(name);
80 let clock: Rc<RefCell<dyn Clock>> = Rc::new(RefCell::new(LiveClock::default()));
81
82 let core = ExecutionClientCore::new(
83 sandbox_config.trader_id,
84 client_id,
85 sandbox_config.venue,
86 sandbox_config.oms_type,
87 sandbox_config.account_id,
88 sandbox_config.account_type,
89 sandbox_config.base_currency,
90 cache.clone(),
91 );
92
93 let client = SandboxExecutionClient::new(core, sandbox_config, clock, cache);
94 Ok(Box::new(client))
95 }
96
97 fn name(&self) -> &'static str {
98 "SANDBOX"
99 }
100
101 fn config_type(&self) -> &'static str {
102 "SandboxExecutionClientConfig"
103 }
104}