nautilus_common/messages/
mod.rs1use 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
38pub use data::{DataResponse, SubscribeCommand, UnsubscribeCommand};
40pub use execution::ExecutionReport;
41
42#[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 Instrument(InstrumentAny),
53 FundingRate(FundingRateUpdate),
54 InstrumentStatus(InstrumentStatus),
55 OptionGreeks(OptionGreeks),
56 #[cfg(feature = "defi")]
58 DeFi(nautilus_model::defi::data::DefiData),
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
63pub enum SystemCommand {
64 #[strum(transparent)]
65 ReconnectSocket(system::ReconnectSocket),
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Display)]
70pub enum SystemEvent {
71 #[strum(transparent)]
72 SocketState(system::SocketStateChange),
73}
74
75#[allow(clippy::large_enum_variant)]
77#[derive(Debug, Display)]
78pub enum ExecutionEvent {
79 #[strum(transparent)]
80 Order(OrderEventAny),
81 #[strum(transparent)]
82 OrderSubmittedBatch(OrderSubmittedBatch),
83 #[strum(transparent)]
84 OrderAcceptedBatch(OrderAcceptedBatch),
85 #[strum(transparent)]
86 OrderCanceledBatch(OrderCanceledBatch),
87 #[strum(transparent)]
88 Report(ExecutionReport),
89 #[strum(transparent)]
90 Account(AccountState),
91}
92
93#[cfg(test)]
94mod tests {
95 use nautilus_core::{UUID4, UnixNanos};
96 use nautilus_model::{
97 enums::AccountType,
98 events::OrderInitialized,
99 identifiers::{AccountId, ClientId, TraderId, Venue},
100 reports::ExecutionMassStatus,
101 };
102 use rstest::rstest;
103 use ustr::Ustr;
104
105 use super::*;
106 use crate::messages::system::{ReconnectSocket, SocketState, SocketStateChange};
107
108 #[rstest]
109 fn system_messages_delegate_display_to_inner() {
110 let command = ReconnectSocket::new(
111 TraderId::from("TRADER-001"),
112 ClientId::from("BINANCE"),
113 Ustr::from("orders"),
114 UnixNanos::from(1),
115 );
116 let event = SocketStateChange::new(
117 ClientId::from("BINANCE"),
118 Some(Venue::from("BINANCE")),
119 Ustr::from("orders"),
120 SocketState::Connected,
121 );
122 let command_expected = command.to_string();
123 let event_expected = event.to_string();
124
125 assert_eq!(
126 SystemCommand::ReconnectSocket(command).to_string(),
127 command_expected
128 );
129 assert_eq!(SystemEvent::SocketState(event).to_string(), event_expected);
130 }
131
132 #[rstest]
133 fn execution_events_delegate_display_to_inner() {
134 let order = OrderEventAny::Initialized(OrderInitialized::default());
135 let submitted_batch = OrderSubmittedBatch::new(Vec::new());
136 let accepted_batch = OrderAcceptedBatch::new(Vec::new());
137 let canceled_batch = OrderCanceledBatch::new(Vec::new());
138 let report = ExecutionReport::MassStatus(Box::new(ExecutionMassStatus::new(
139 ClientId::from("BINANCE"),
140 AccountId::from("BINANCE-001"),
141 Venue::from("BINANCE"),
142 UnixNanos::from(2),
143 Some(UUID4::from("00000000-0000-4000-8000-000000000001")),
144 )));
145 let account = AccountState::new(
146 AccountId::from("BINANCE-001"),
147 AccountType::Cash,
148 Vec::new(),
149 Vec::new(),
150 true,
151 UUID4::from("00000000-0000-4000-8000-000000000002"),
152 UnixNanos::from(3),
153 UnixNanos::from(4),
154 None,
155 );
156 let cases = [
157 (ExecutionEvent::Order(order.clone()), order.to_string()),
158 (
159 ExecutionEvent::OrderSubmittedBatch(submitted_batch.clone()),
160 submitted_batch.to_string(),
161 ),
162 (
163 ExecutionEvent::OrderAcceptedBatch(accepted_batch.clone()),
164 accepted_batch.to_string(),
165 ),
166 (
167 ExecutionEvent::OrderCanceledBatch(canceled_batch.clone()),
168 canceled_batch.to_string(),
169 ),
170 (ExecutionEvent::Report(report.clone()), report.to_string()),
171 (
172 ExecutionEvent::Account(account.clone()),
173 account.to_string(),
174 ),
175 ];
176
177 for (event, expected) in cases {
178 assert_eq!(event.to_string(), expected);
179 }
180 }
181}