Skip to main content

nautilus_common/messages/
mod.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//! Message types for system communication.
17//!
18//! This module provides message types used for communication between different
19//! parts of the NautilusTrader system, including data requests, execution commands,
20//! and system control messages.
21
22use nautilus_model::{
23    data::{Data, FundingRateUpdate, InstrumentStatus, option_chain::OptionGreeks},
24    events::{
25        AccountState, OrderAcceptedBatch, OrderCanceledBatch, OrderEventAny, OrderSubmittedBatch,
26    },
27    instruments::InstrumentAny,
28};
29use strum::Display;
30
31pub mod data;
32pub mod execution;
33pub mod system;
34
35#[cfg(feature = "defi")]
36pub mod defi;
37
38// Re-exports
39pub use data::{DataResponse, SubscribeCommand, UnsubscribeCommand};
40pub use execution::ExecutionReport;
41
42// TODO: Refine this to reduce disparity between enum sizes
43#[allow(
44    clippy::large_enum_variant,
45    reason = "event enum keeps all data variants in one routing type"
46)]
47#[derive(Debug, Display)]
48pub enum DataEvent {
49    Response(DataResponse),
50    Data(Data),
51    // Kept separate from `Data` pending the decision on generic dispatch versus this routing enum
52    Instrument(InstrumentAny),
53    FundingRate(FundingRateUpdate),
54    InstrumentStatus(InstrumentStatus),
55    OptionGreeks(OptionGreeks),
56    // nautilus-import-ok: conditional compilation import
57    #[cfg(feature = "defi")]
58    DeFi(nautilus_model::defi::data::DefiData),
59    #[cfg(feature = "defi")]
60    PoolSnapshotResponse(defi::PoolSnapshotResponse),
61}
62
63/// System command variants routed to a live node.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
65pub enum SystemCommand {
66    #[strum(transparent)]
67    ReconnectSocket(system::ReconnectSocket),
68}
69
70/// System event variants routed to a live node.
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
72pub enum SystemEvent {
73    #[strum(transparent)]
74    SocketState(system::SocketStateChange),
75}
76
77/// Execution event variants for order events and reports.
78#[allow(clippy::large_enum_variant)]
79#[derive(Debug, Display)]
80pub enum ExecutionEvent {
81    #[strum(transparent)]
82    Order(OrderEventAny),
83    #[strum(transparent)]
84    OrderSubmittedBatch(OrderSubmittedBatch),
85    #[strum(transparent)]
86    OrderAcceptedBatch(OrderAcceptedBatch),
87    #[strum(transparent)]
88    OrderCanceledBatch(OrderCanceledBatch),
89    #[strum(transparent)]
90    Report(ExecutionReport),
91    #[strum(transparent)]
92    Account(AccountState),
93}
94
95#[cfg(test)]
96mod tests {
97    use nautilus_core::{UUID4, UnixNanos};
98    use nautilus_model::{
99        enums::AccountType,
100        events::OrderInitialized,
101        identifiers::{AccountId, ClientId, TraderId, Venue},
102        reports::ExecutionMassStatus,
103    };
104    use rstest::rstest;
105    use ustr::Ustr;
106
107    use super::*;
108    use crate::messages::system::{ReconnectSocket, SocketState, SocketStateChange};
109
110    #[rstest]
111    fn system_messages_delegate_display_to_inner() {
112        let command = ReconnectSocket::new(
113            TraderId::from("TRADER-001"),
114            ClientId::from("BINANCE"),
115            Ustr::from("orders"),
116            UnixNanos::from(1),
117        );
118        let event = SocketStateChange::new(
119            ClientId::from("BINANCE"),
120            Some(Venue::from("BINANCE")),
121            Ustr::from("orders"),
122            SocketState::Connected,
123        );
124        let command_expected = command.to_string();
125        let event_expected = event.to_string();
126
127        assert_eq!(
128            SystemCommand::ReconnectSocket(command).to_string(),
129            command_expected
130        );
131        assert_eq!(SystemEvent::SocketState(event).to_string(), event_expected);
132    }
133
134    #[rstest]
135    fn execution_events_delegate_display_to_inner() {
136        let order = OrderEventAny::Initialized(OrderInitialized::default());
137        let submitted_batch = OrderSubmittedBatch::new(Vec::new());
138        let accepted_batch = OrderAcceptedBatch::new(Vec::new());
139        let canceled_batch = OrderCanceledBatch::new(Vec::new());
140        let report = ExecutionReport::MassStatus(Box::new(ExecutionMassStatus::new(
141            ClientId::from("BINANCE"),
142            AccountId::from("BINANCE-001"),
143            Venue::from("BINANCE"),
144            UnixNanos::from(2),
145            Some(UUID4::from("00000000-0000-4000-8000-000000000001")),
146        )));
147        let account = AccountState::new(
148            AccountId::from("BINANCE-001"),
149            AccountType::Cash,
150            Vec::new(),
151            Vec::new(),
152            true,
153            UUID4::from("00000000-0000-4000-8000-000000000002"),
154            UnixNanos::from(3),
155            UnixNanos::from(4),
156            None,
157        );
158        let cases = [
159            (ExecutionEvent::Order(order.clone()), order.to_string()),
160            (
161                ExecutionEvent::OrderSubmittedBatch(submitted_batch.clone()),
162                submitted_batch.to_string(),
163            ),
164            (
165                ExecutionEvent::OrderAcceptedBatch(accepted_batch.clone()),
166                accepted_batch.to_string(),
167            ),
168            (
169                ExecutionEvent::OrderCanceledBatch(canceled_batch.clone()),
170                canceled_batch.to_string(),
171            ),
172            (ExecutionEvent::Report(report.clone()), report.to_string()),
173            (
174                ExecutionEvent::Account(account.clone()),
175                account.to_string(),
176            ),
177        ];
178
179        for (event, expected) in cases {
180            assert_eq!(event.to_string(), expected);
181        }
182    }
183}