bothan_binance/api/msgs.rs
1//! Types for Binance WebSocket API interaction.
2//!
3//! This module provides types for deserializing events and responses from the Binance WebSocket API,
4//! including success responses, errors, stream events, and specific event data like mini ticker updates.
5
6use serde::{Deserialize, Serialize};
7
8/// Represents the different types of events that can be received from the Binance WebSocket API.
9#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
10#[serde(untagged)]
11pub enum Event {
12 /// Represents a successful event response with an optional result and an identifier.
13 Success(SuccessEvent),
14 /// Represents an error event with an error code, message, and identifier.
15 Error(ErrorEvent),
16 /// Represents a stream event containing the stream name and associated data.
17 Stream(StreamEvent),
18 /// Represents a ping message from the WebSocket API.
19 Ping,
20}
21
22/// Represents a successful event response from the Binance WebSocket API.
23#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
24pub struct SuccessEvent {
25 /// The optional result of the event, as some events may not return a result.
26 pub result: Option<String>,
27 /// The identifier for the event.
28 pub id: i64,
29}
30
31/// Represents an error event from the Binance WebSocket API.
32#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
33pub struct ErrorEvent {
34 /// The error code indicating the type of error that occurred.
35 pub code: i16,
36 /// A human-readable message describing the error.
37 pub msg: String,
38 /// The identifier for the event associated with the error.
39 pub id: i64,
40}
41
42/// Represents a stream event received from the Binance WebSocket API.
43#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
44pub struct StreamEvent {
45 /// The name of the stream event.
46 pub stream: String,
47 /// The data associated with the stream event from the Binance WebSocket API.
48 pub data: StreamEventData,
49}
50
51/// Represents the data associated with a stream event from the Binance WebSocket API.
52/// The `StreamEventData` enum can represent different types of stream events,
53/// such as a mini ticker event. Each variant of the enum corresponds to a specific type of event,
54/// allowing for flexible handling of various event types.
55#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
56#[serde(tag = "e")]
57pub enum StreamEventData {
58 /// The `MiniTickerInfo` struct is used to deserialize the data for this event type.
59 #[serde(rename = "24hrMiniTicker")]
60 MiniTicker(MiniTickerInfo),
61}
62
63/// Represents price information retrieved from the Binance WebSocket API.
64///
65/// `MiniTickerInfo` struct contains fields matching those returned by the Binance WebSocket API
66/// for the 24hr mini ticker event. It serves as an interface for JSON deserialization of event data.
67#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
68pub struct MiniTickerInfo {
69 /// The unix timestamp of the event in milliseconds.
70 #[serde(rename = "E")]
71 pub event_time: i64,
72
73 /// The trading pair symbol (e.g., "BTCUSDT").
74 #[serde(rename = "s")]
75 pub symbol: String,
76
77 /// The last price of the trading pair.
78 #[serde(rename = "c")]
79 pub close_price: String,
80
81 /// The opening price of the trading pair.
82 #[serde(rename = "o")]
83 pub open_price: String,
84
85 /// The highest price of the trading pair during the event.
86 #[serde(rename = "h")]
87 pub high_price: String,
88
89 /// The lowest price of the trading pair during the event.
90 #[serde(rename = "l")]
91 pub low_price: String,
92
93 /// Total traded base asset volume
94 #[serde(rename = "v")]
95 pub base_volume: String,
96
97 /// Total traded quote asset volume
98 #[serde(rename = "q")]
99 pub quote_volume: String,
100}