binance_async/websocket/
models.rs

1use crate::{
2    models::{
3        Asks, Bids, Kline, OrderExecType, OrderRejectReason, OrderStatus, OrderType, Side,
4        TimeInForce,
5    },
6    parser::string_or_decimal,
7};
8use rust_decimal::Decimal;
9use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Serialize, Deserialize, Clone)]
12#[serde(rename_all = "camelCase")]
13pub struct TradeMessage {
14    #[serde(rename = "e")]
15    pub event_type: String,
16    #[serde(rename = "E")]
17    pub event_time: u64,
18    #[serde(rename = "s")]
19    pub symbol: String,
20    #[serde(rename = "t")]
21    pub trade_id: u64,
22    #[serde(rename = "p", with = "string_or_decimal")]
23    pub price: Decimal,
24    #[serde(rename = "q", with = "string_or_decimal")]
25    pub qty: Decimal,
26    #[serde(rename = "b")]
27    pub buyer_order_id: u64,
28    #[serde(rename = "a")]
29    pub seller_order_id: u64,
30    #[serde(rename = "T")]
31    pub trade_order_time: u64,
32    #[serde(rename = "m")]
33    pub is_buyer_maker: bool,
34    #[serde(skip_serializing, rename = "M")]
35    pub m_ignore: bool,
36}
37
38/// The Aggregate Trade Streams push trade information that is aggregated for a single taker order.
39///
40/// Stream Name: \<symbol\>@aggTrade
41///
42/// Update Speed: Real-time
43///
44/// <https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#aggregate-trade-streams>
45#[derive(Debug, Serialize, Deserialize, Clone)]
46#[serde(rename_all = "camelCase")]
47pub struct AggregateTrade {
48    #[serde(rename = "e")]
49    pub event_type: String,
50
51    #[serde(rename = "E")]
52    pub event_time: u64,
53
54    #[serde(rename = "s")]
55    pub symbol: String,
56
57    #[serde(rename = "a")]
58    pub aggregated_trade_id: u64,
59
60    #[serde(rename = "p", with = "string_or_decimal")]
61    pub price: Decimal,
62
63    #[serde(rename = "q", with = "string_or_decimal")]
64    pub qty: Decimal,
65
66    #[serde(rename = "f")]
67    pub first_break_trade_id: u64,
68
69    #[serde(rename = "l")]
70    pub last_break_trade_id: u64,
71
72    #[serde(rename = "T")]
73    pub trade_order_time: u64,
74
75    #[serde(rename = "m")]
76    pub is_buyer_maker: bool,
77
78    #[serde(skip, rename = "M")]
79    pub m_ignore: bool,
80}
81
82#[derive(Debug, Serialize, Deserialize, Clone)]
83#[serde(rename_all = "camelCase")]
84pub struct UserOrderUpdate {
85    #[serde(rename = "e")]
86    pub event_type: String,
87    #[serde(rename = "E")]
88    pub event_time: u64,
89    #[serde(rename = "s")]
90    pub symbol: String,
91    #[serde(rename = "c")]
92    pub new_client_order_id: String,
93    #[serde(rename = "S")]
94    pub side: Side,
95    #[serde(rename = "o")]
96    pub order_type: OrderType,
97    #[serde(rename = "f")]
98    pub time_in_force: TimeInForce,
99    #[serde(rename = "q", with = "string_or_decimal")]
100    pub qty: Decimal,
101    #[serde(rename = "p", with = "string_or_decimal")]
102    pub price: Decimal,
103    #[serde(rename = "P", with = "string_or_decimal")]
104    pub stop_price: Decimal,
105    #[serde(rename = "F", with = "string_or_decimal")]
106    pub iceberg_qty: Decimal,
107    #[serde(skip_serializing)]
108    pub g: i32,
109    #[serde(skip_serializing, rename = "C")]
110    pub c_ignore: Option<String>,
111    #[serde(rename = "x")]
112    pub execution_type: OrderExecType,
113    #[serde(rename = "X")]
114    pub order_status: OrderStatus,
115    #[serde(rename = "r")]
116    pub order_reject_reason: OrderRejectReason,
117    #[serde(rename = "i")]
118    pub order_id: u64,
119    #[serde(rename = "l", with = "string_or_decimal")]
120    pub qty_last_filled_trade: Decimal,
121    #[serde(rename = "z", with = "string_or_decimal")]
122    pub accumulated_qty_filled_trades: Decimal,
123    #[serde(rename = "L", with = "string_or_decimal")]
124    pub price_last_filled_trade: Decimal,
125    #[serde(rename = "n", with = "string_or_decimal")]
126    pub commission: Decimal,
127    #[serde(skip_serializing, rename = "N")]
128    pub asset_commisioned: Option<String>,
129    #[serde(rename = "T")]
130    pub trade_order_time: u64,
131    #[serde(rename = "t")]
132    pub trade_id: i64,
133    #[serde(skip_serializing, rename = "I")]
134    pub i_ignore: u64,
135    #[serde(skip_serializing)]
136    pub w: bool,
137    #[serde(rename = "m")]
138    pub is_buyer_maker: bool,
139    #[serde(skip_serializing, rename = "M")]
140    pub m_ignore: bool,
141    #[serde(skip_serializing, rename = "O")]
142    pub order_creation_time: u64,
143    #[serde(skip_serializing, rename = "Z", with = "string_or_decimal")]
144    pub cumulative_quote_asset_transacted_qty: Decimal,
145}
146
147#[derive(Debug, Serialize, Deserialize, Clone)]
148#[serde(rename_all = "camelCase")]
149pub struct Depth {
150    #[serde(rename = "e")]
151    pub event_type: String,
152    #[serde(rename = "E")]
153    pub event_time: u64,
154    #[serde(rename = "s")]
155    pub symbol: String,
156    #[serde(rename = "U")]
157    pub first_update_id: u64,
158    #[serde(rename = "u")]
159    pub final_update_id: u64,
160    #[serde(rename = "b")]
161    pub bids: Vec<Bids>,
162    #[serde(rename = "a")]
163    pub asks: Vec<Asks>,
164}
165
166#[derive(Debug, Serialize, Deserialize, Clone)]
167#[serde(rename_all = "camelCase")]
168pub struct Ticker {
169    #[serde(rename = "e")]
170    pub event_type: String,
171    #[serde(rename = "E")]
172    pub event_time: u64,
173    #[serde(rename = "s")]
174    pub symbol: String,
175    #[serde(rename = "p", with = "string_or_decimal")]
176    pub price_change: Decimal,
177    #[serde(rename = "P", with = "string_or_decimal")]
178    pub price_change_percent: Decimal,
179    #[serde(rename = "w", with = "string_or_decimal")]
180    pub average_price: Decimal,
181    #[serde(rename = "x", with = "string_or_decimal")]
182    pub prev_close: Decimal,
183    #[serde(rename = "c", with = "string_or_decimal")]
184    pub current_close: Decimal,
185    #[serde(rename = "Q", with = "string_or_decimal")]
186    pub current_close_qty: Decimal,
187    #[serde(rename = "b", with = "string_or_decimal")]
188    pub best_bid: Decimal,
189    #[serde(rename = "B", with = "string_or_decimal")]
190    pub best_bid_qty: Decimal,
191    #[serde(rename = "a", with = "string_or_decimal")]
192    pub best_ask: Decimal,
193    #[serde(rename = "A", with = "string_or_decimal")]
194    pub best_ask_qty: Decimal,
195    #[serde(rename = "o", with = "string_or_decimal")]
196    pub open: Decimal,
197    #[serde(rename = "h", with = "string_or_decimal")]
198    pub high: Decimal,
199    #[serde(rename = "l", with = "string_or_decimal")]
200    pub low: Decimal,
201    #[serde(rename = "v", with = "string_or_decimal")]
202    pub volume: Decimal,
203    #[serde(rename = "q", with = "string_or_decimal")]
204    pub quote_volume: Decimal,
205    #[serde(rename = "O")]
206    pub open_time: u64,
207    #[serde(rename = "C")]
208    pub close_time: u64,
209    #[serde(rename = "F")]
210    pub first_trade_id: u64,
211    #[serde(rename = "L")]
212    pub last_trade_id: u64,
213    #[serde(rename = "n")]
214    pub num_trades: u64,
215}
216
217#[derive(Debug, Serialize, Deserialize, Clone)]
218#[serde(rename_all = "camelCase")]
219pub struct CandelStickMessage {
220    #[serde(rename = "e")]
221    pub event_type: String,
222    #[serde(rename = "E")]
223    pub event_time: u64,
224    #[serde(rename = "s")]
225    pub symbol: String,
226    #[serde(rename = "k")]
227    pub kline: Kline,
228}
229
230#[derive(Debug, Serialize, Deserialize, Clone)]
231#[serde(rename_all = "camelCase")]
232pub struct AccountUpdate {
233    #[serde(rename = "e")]
234    pub event_type: String,
235    #[serde(rename = "E")]
236    pub event_time: u64,
237    #[serde(rename = "m")]
238    pub maker_commision_rate: u64,
239    #[serde(rename = "t")]
240    pub taker_commision_rate: u64,
241    #[serde(rename = "b")]
242    pub buyer_commision_rate: u64,
243    #[serde(rename = "s")]
244    pub seller_commision_rate: u64,
245    #[serde(rename = "T")]
246    pub can_trade: bool,
247    #[serde(rename = "W")]
248    pub can_withdraw: bool,
249    #[serde(rename = "D")]
250    pub can_deposit: bool,
251    #[serde(rename = "u")]
252    pub last_account_update: u64,
253    #[serde(rename = "B")]
254    pub balance: Vec<AccountUpdateBalance>,
255}
256
257#[derive(Debug, Serialize, Deserialize, Clone)]
258#[serde(rename_all = "camelCase")]
259pub struct AccountUpdateBalance {
260    #[serde(rename = "a")]
261    pub asset: String,
262    #[serde(rename = "f", with = "string_or_decimal")]
263    pub free: Decimal,
264    #[serde(rename = "l", with = "string_or_decimal")]
265    pub locked: Decimal,
266}
267
268#[derive(Debug, Serialize, Deserialize, Clone)]
269#[serde(rename_all = "camelCase")]
270pub struct MiniTicker {
271    #[serde(rename = "e")]
272    pub event_type: String,
273    #[serde(rename = "E")]
274    pub event_time: u64,
275    #[serde(rename = "s")]
276    pub symbol: String,
277    #[serde(rename = "c", with = "string_or_decimal")]
278    pub close: Decimal,
279    #[serde(rename = "o", with = "string_or_decimal")]
280    pub open: Decimal,
281    #[serde(rename = "l", with = "string_or_decimal")]
282    pub low: Decimal,
283    #[serde(rename = "h", with = "string_or_decimal")]
284    pub high: Decimal,
285    #[serde(rename = "v", with = "string_or_decimal")]
286    pub volume: Decimal,
287    #[serde(rename = "q", with = "string_or_decimal")]
288    pub quote_volume: Decimal,
289}