nautilus_execution/engine/
stubs.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
16use std::{cell::RefCell, rc::Rc};
17
18use async_trait::async_trait;
19use nautilus_common::{
20    cache::Cache,
21    clock::{Clock, TestClock},
22    messages::execution::{
23        BatchCancelOrders, CancelAllOrders, CancelOrder, ModifyOrder, QueryAccount, QueryOrder,
24        SubmitOrder, SubmitOrderList,
25    },
26};
27use nautilus_core::UnixNanos;
28use nautilus_model::{
29    accounts::AccountAny,
30    enums::OmsType,
31    identifiers::{AccountId, ClientId, Venue},
32    types::{AccountBalance, MarginBalance},
33};
34
35use crate::client::ExecutionClient;
36
37/// A stub execution client for testing purposes.
38///
39/// This client provides a minimal implementation of the `ExecutionClient` trait
40/// that can be used in unit tests without requiring actual venue connectivity.
41#[derive(Clone, Debug)]
42#[allow(dead_code)]
43pub struct StubExecutionClient {
44    client_id: ClientId,
45    account_id: AccountId,
46    venue: Venue,
47    oms_type: OmsType,
48    is_connected: bool,
49    clock: Rc<RefCell<dyn Clock>>,
50    cache: Rc<RefCell<Cache>>,
51}
52
53impl StubExecutionClient {
54    /// Creates a new [`StubExecutionClient`] instance.
55    #[allow(dead_code)]
56    pub fn new(
57        client_id: ClientId,
58        account_id: AccountId,
59        venue: Venue,
60        oms_type: OmsType,
61        clock: Option<Rc<RefCell<dyn Clock>>>,
62    ) -> Self {
63        Self {
64            client_id,
65            account_id,
66            venue,
67            oms_type,
68            is_connected: false,
69            clock: clock.unwrap_or_else(|| Rc::new(RefCell::new(TestClock::new()))),
70            cache: Rc::new(RefCell::new(Cache::new(None, None))),
71        }
72    }
73}
74
75#[async_trait(?Send)]
76impl ExecutionClient for StubExecutionClient {
77    fn is_connected(&self) -> bool {
78        self.is_connected
79    }
80
81    fn client_id(&self) -> ClientId {
82        self.client_id
83    }
84
85    fn account_id(&self) -> AccountId {
86        self.account_id
87    }
88
89    fn venue(&self) -> Venue {
90        self.venue
91    }
92
93    fn oms_type(&self) -> OmsType {
94        self.oms_type
95    }
96
97    fn get_account(&self) -> Option<AccountAny> {
98        None // Stub implementation returns None
99    }
100
101    fn generate_account_state(
102        &self,
103        _balances: Vec<AccountBalance>,
104        _margins: Vec<MarginBalance>,
105        _reported: bool,
106        _ts_event: UnixNanos,
107    ) -> anyhow::Result<()> {
108        Ok(()) // Stub implementation always succeeds
109    }
110
111    fn start(&mut self) -> anyhow::Result<()> {
112        self.is_connected = true;
113        Ok(())
114    }
115
116    fn stop(&mut self) -> anyhow::Result<()> {
117        self.is_connected = false;
118        Ok(())
119    }
120
121    fn submit_order(&self, _cmd: &SubmitOrder) -> anyhow::Result<()> {
122        Ok(()) // Stub implementation always succeeds
123    }
124
125    fn submit_order_list(&self, _cmd: &SubmitOrderList) -> anyhow::Result<()> {
126        Ok(()) // Stub implementation always succeeds
127    }
128
129    fn modify_order(&self, _cmd: &ModifyOrder) -> anyhow::Result<()> {
130        Ok(()) // Stub implementation always succeeds
131    }
132
133    fn cancel_order(&self, _cmd: &CancelOrder) -> anyhow::Result<()> {
134        Ok(()) // Stub implementation always succeeds
135    }
136
137    fn cancel_all_orders(&self, _cmd: &CancelAllOrders) -> anyhow::Result<()> {
138        Ok(()) // Stub implementation always succeeds
139    }
140
141    fn batch_cancel_orders(&self, _cmd: &BatchCancelOrders) -> anyhow::Result<()> {
142        Ok(()) // Stub implementation always succeeds
143    }
144
145    fn query_account(&self, _cmd: &QueryAccount) -> anyhow::Result<()> {
146        Ok(()) // Stub implementation always succeeds
147    }
148
149    fn query_order(&self, _cmd: &QueryOrder) -> anyhow::Result<()> {
150        Ok(()) // Stub implementation always succeeds
151    }
152}