nautilus_binance/spot/websocket/trading/messages.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//! Binance Spot WebSocket API message types.
17//!
18//! This module defines:
19//! - [`BinanceSpotWsTradingCommand`]: Commands sent from the client to the handler.
20//! - [`BinanceSpotWsTradingMessage`]: Output messages emitted by the handler to the client.
21//! - Request/response structures for the Binance Spot WebSocket Trading API.
22
23use nautilus_network::websocket::WebSocketClient;
24use serde::{Deserialize, Serialize};
25
26use super::user_data::{
27 BinanceSpotAccountPositionMsg, BinanceSpotBalanceUpdateMsg, BinanceSpotExecutionReport,
28};
29use crate::spot::http::{
30 models::{BinanceCancelOrderResponse, BinanceNewOrderResponse},
31 query::{CancelOrderParams, CancelReplaceOrderParams, NewOrderParams},
32};
33
34/// Commands sent from the outer client to the inner handler.
35///
36/// The handler runs in a dedicated Tokio task and processes these commands
37/// to perform WebSocket API operations (request/response pattern).
38#[allow(
39 missing_debug_implementations,
40 clippy::large_enum_variant,
41 reason = "Commands are ephemeral and immediately consumed"
42)]
43pub enum BinanceSpotWsTradingCommand {
44 /// Sets the WebSocket client after connection.
45 SetClient(WebSocketClient),
46 /// Disconnects and cleans up.
47 Disconnect,
48 /// Places a new order.
49 PlaceOrder {
50 /// Request ID for correlation.
51 id: String,
52 /// Order parameters.
53 params: NewOrderParams,
54 },
55 /// Cancels an order.
56 CancelOrder {
57 /// Request ID for correlation.
58 id: String,
59 /// Cancel parameters.
60 params: CancelOrderParams,
61 },
62 /// Cancels and replaces an order atomically.
63 CancelReplaceOrder {
64 /// Request ID for correlation.
65 id: String,
66 /// Cancel-replace parameters.
67 params: CancelReplaceOrderParams,
68 },
69 /// Cancels all open orders for a symbol.
70 CancelAllOrders {
71 /// Request ID for correlation.
72 id: String,
73 /// Symbol to cancel all orders for.
74 symbol: String,
75 },
76 /// Authenticates the WebSocket session via `session.logon`.
77 SessionLogon,
78 /// Subscribes to the user data stream via `userDataStream.subscribe`.
79 SubscribeUserData,
80}
81
82/// Normalized output message from the WebSocket API handler.
83///
84/// These messages are emitted by the handler and consumed by the client
85/// for routing to callers or the execution engine.
86#[derive(Debug, Clone)]
87pub enum BinanceSpotWsTradingMessage {
88 /// Connection established.
89 Connected,
90 /// Session authenticated successfully.
91 Authenticated,
92 /// Connection was re-established after disconnect.
93 Reconnected,
94 /// Order accepted by venue.
95 OrderAccepted {
96 /// Request ID for correlation.
97 request_id: String,
98 /// Order response from venue.
99 response: BinanceNewOrderResponse,
100 },
101 /// Order rejected by venue.
102 OrderRejected {
103 /// Request ID for correlation.
104 request_id: String,
105 /// Error code from venue.
106 code: i32,
107 /// Error message from venue.
108 msg: String,
109 },
110 /// Order canceled successfully.
111 OrderCanceled {
112 /// Request ID for correlation.
113 request_id: String,
114 /// Cancel response from venue.
115 response: BinanceCancelOrderResponse,
116 },
117 /// Cancel rejected by venue.
118 CancelRejected {
119 /// Request ID for correlation.
120 request_id: String,
121 /// Error code from venue.
122 code: i32,
123 /// Error message from venue.
124 msg: String,
125 },
126 /// Cancel-replace response (new order after cancel).
127 CancelReplaceAccepted {
128 /// Request ID for correlation.
129 request_id: String,
130 /// Cancel response.
131 cancel_response: BinanceCancelOrderResponse,
132 /// New order response.
133 new_order_response: BinanceNewOrderResponse,
134 },
135 /// Cancel-replace rejected.
136 CancelReplaceRejected {
137 /// Request ID for correlation.
138 request_id: String,
139 /// Error code from venue.
140 code: i32,
141 /// Error message from venue.
142 msg: String,
143 },
144 /// All orders canceled for a symbol.
145 AllOrdersCanceled {
146 /// Request ID for correlation.
147 request_id: String,
148 /// Canceled order responses.
149 responses: Vec<BinanceCancelOrderResponse>,
150 },
151 /// User data stream subscribed.
152 UserDataSubscribed {
153 /// Subscription ID from Binance.
154 subscription_id: String,
155 },
156 /// Order execution report from user data stream.
157 ExecutionReport(Box<BinanceSpotExecutionReport>),
158 /// Account position update from user data stream.
159 AccountPosition(BinanceSpotAccountPositionMsg),
160 /// Balance update from user data stream.
161 BalanceUpdate(BinanceSpotBalanceUpdateMsg),
162 /// Error from venue or network.
163 Error(String),
164}
165
166/// Metadata for a pending request.
167///
168/// Stored in the handler to match responses to their originating requests.
169#[derive(Debug, Clone, Copy)]
170pub enum BinanceSpotWsTradingRequestMeta {
171 /// Pending order placement.
172 PlaceOrder,
173 /// Pending order cancellation.
174 CancelOrder,
175 /// Pending cancel-replace.
176 CancelReplaceOrder,
177 /// Pending cancel-all.
178 CancelAllOrders,
179 /// Pending session logon.
180 SessionLogon,
181 /// Pending user data subscription.
182 SubscribeUserData,
183}
184
185/// WebSocket API request wrapper.
186///
187/// Requests are sent as JSON text frames, responses come back as SBE binary.
188#[derive(Debug, Clone, Serialize)]
189pub struct BinanceSpotWsTradingRequest {
190 /// Unique request ID for correlation.
191 pub id: String,
192 /// API method name (e.g., "order.place").
193 pub method: String,
194 /// Request parameters.
195 pub params: serde_json::Value,
196}
197
198impl BinanceSpotWsTradingRequest {
199 /// Creates a new WebSocket API request.
200 #[must_use]
201 pub fn new(
202 id: impl Into<String>,
203 method: impl Into<String>,
204 params: serde_json::Value,
205 ) -> Self {
206 Self {
207 id: id.into(),
208 method: method.into(),
209 params,
210 }
211 }
212}
213
214/// WebSocket API error response (JSON).
215#[derive(Debug, Clone, Deserialize)]
216pub struct BinanceSpotWsTradingResponseError {
217 /// Error code from venue.
218 pub code: i32,
219 /// Error message from venue.
220 pub msg: String,
221 /// Request ID if available.
222 pub id: Option<String>,
223}
224
225/// WebSocket API method names.
226pub mod method {
227 /// Places a new order.
228 pub const ORDER_PLACE: &str = "order.place";
229 /// Cancels an order.
230 pub const ORDER_CANCEL: &str = "order.cancel";
231 /// Cancels and replaces an order.
232 pub const ORDER_CANCEL_REPLACE: &str = "order.cancelReplace";
233 /// Cancels all open orders for a symbol.
234 pub const OPEN_ORDERS_CANCEL_ALL: &str = "openOrders.cancelAll";
235 /// Initiates session logon.
236 pub const SESSION_LOGON: &str = "session.logon";
237 /// Queries session status.
238 pub const SESSION_STATUS: &str = "session.status";
239 /// Initiates session logout.
240 pub const SESSION_LOGOUT: &str = "session.logout";
241}