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, ExecutionClientFactory},
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)]
40pub struct SandboxExecutionClientFactory;
41
42impl SandboxExecutionClientFactory {
43 #[must_use]
45 pub const fn new() -> Self {
46 Self
47 }
48}
49
50impl ExecutionClientFactory for SandboxExecutionClientFactory {
51 fn create(
52 &self,
53 name: &str,
54 config: &dyn ClientConfig,
55 cache: Rc<RefCell<Cache>>,
56 ) -> anyhow::Result<Box<dyn ExecutionClient>> {
57 let sandbox_config = config
58 .as_any()
59 .downcast_ref::<SandboxExecutionClientConfig>()
60 .ok_or_else(|| {
61 anyhow::anyhow!(
62 "Invalid config type for SandboxExecutionClientFactory. Expected SandboxExecutionClientConfig, was {config:?}",
63 )
64 })?
65 .clone();
66
67 let client_id = ClientId::from(name);
68 let clock: Rc<RefCell<dyn Clock>> = Rc::new(RefCell::new(LiveClock::default()));
69
70 let core = ExecutionClientCore::new(
71 sandbox_config.trader_id,
72 client_id,
73 sandbox_config.venue,
74 sandbox_config.oms_type,
75 sandbox_config.account_id,
76 sandbox_config.account_type,
77 sandbox_config.base_currency,
78 cache.clone(),
79 );
80
81 let client = SandboxExecutionClient::new(core, sandbox_config, clock, cache);
82 Ok(Box::new(client))
83 }
84
85 fn name(&self) -> &'static str {
86 "SANDBOX"
87 }
88
89 fn config_type(&self) -> &'static str {
90 "SandboxExecutionClientConfig"
91 }
92}