bothan_htx/api/
types.rs

1//! Types for HTX WebSocket API interaction.
2//!
3//! This module provides types for deserializing events and responses from the HTX WebSocket API,
4//! including subscription responses, data updates, ping/pong messages, and error responses.
5//! The module supports gzip-compressed binary messages from the HTX WebSocket stream.
6
7use serde::{Deserialize, Serialize};
8
9/// The default URL for the HTX WebSocket API.
10pub const DEFAULT_URL: &str = "wss://api.huobi.pro/ws";
11
12/// Represents the different types of responses that can be received from the HTX WebSocket API.
13///
14/// The `Response` enum can represent various types of messages from the HTX WebSocket API,
15/// including subscription confirmations, data updates, ping messages, and error responses.
16/// Each variant corresponds to a specific type of message, allowing for flexible handling
17/// of various response types.
18#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
19#[serde(rename_all = "camelCase", untagged)]
20pub enum Response {
21    /// Represents a successful subscription confirmation.
22    Subscribed(Subscribed),
23    /// Represents a successful unsubscription confirmation.
24    Unsubscribed(Unsubscribed),
25    /// Represents a market data update with ticker information.
26    DataUpdate(Data),
27    /// Represents a ping message from the WebSocket API.
28    Ping(Ping),
29    /// Represents an error response from the WebSocket API.
30    Error(Error),
31}
32
33/// Represents a successful subscription response from the HTX WebSocket API.
34///
35/// This struct contains information about a successful subscription to a market data stream,
36/// including the subscription ID, status, subscribed channel, and timestamp.
37#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
38#[serde(rename_all = "camelCase")]
39pub struct Subscribed {
40    /// Optional subscription identifier.
41    pub id: Option<String>,
42    /// Status of the subscription request (typically "ok").
43    pub status: String,
44    /// The channel that was successfully subscribed to.
45    pub subbed: String,
46    /// Unix timestamp in milliseconds when the subscription was processed.
47    #[serde(rename = "ts")]
48    pub timestamp: i64,
49}
50
51/// Represents a successful unsubscription response from the HTX WebSocket API.
52///
53/// This struct contains information about a successful unsubscription from a market data stream,
54/// including the unsubscription ID, status, unsubscribed channel, and timestamp.
55#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
56#[serde(rename_all = "camelCase")]
57pub struct Unsubscribed {
58    /// Optional unsubscription identifier.
59    pub id: Option<String>,
60    /// Status of the unsubscription request (typically "ok").
61    pub status: String,
62    /// The channel that was successfully unsubscribed from.
63    pub unsubbed: String,
64    /// Unix timestamp in milliseconds when the unsubscription was processed.
65    #[serde(rename = "ts")]
66    pub timestamp: i64,
67}
68
69/// Represents a market data update from the HTX WebSocket API.
70///
71/// This struct contains market data information including the channel name,
72/// timestamp, and detailed ticker information.
73#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
74#[serde(rename_all = "camelCase")]
75pub struct Data {
76    /// The channel name for the market data (e.g., "market.btcusdt.ticker").
77    pub ch: String,
78    /// Unix timestamp in milliseconds when the data was generated.
79    #[serde(rename = "ts")]
80    pub timestamp: i64,
81    /// Detailed ticker information containing market data.
82    pub tick: Tick,
83}
84
85/// Represents detailed ticker information from the HTX WebSocket API.
86///
87/// This struct contains comprehensive market data including price information,
88/// volume data, and bid/ask details for a specific trading pair.
89#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
90#[serde(rename_all = "camelCase")]
91pub struct Tick {
92    /// Opening price of last 24 hours (rotating 24h).
93    pub open: f64,
94    /// Highest price of last 24 hours (rotating 24h).
95    pub high: f64,
96    /// Lowest price of last 24 hours (rotating 24h).
97    pub low: f64,
98    /// Closing price of last 24 hours (rotating 24h).
99    pub close: f64,
100    /// Accumulated trading volume of last 24 hours (rotating 24h), in base currency.
101    pub amount: f64,
102    /// Accumulated trading value of last 24 hours (rotating 24h), in quote currency.
103    pub vol: f64,
104    /// The number of completed trades (rotating 24h).
105    pub count: u64,
106    /// Current best bid price.
107    pub bid: f64,
108    /// Current best bid size.
109    pub bid_size: f64,
110    /// Current best ask price.
111    pub ask: f64,
112    /// Current best ask size.
113    pub ask_size: f64,
114    /// Last traded price.
115    pub last_price: f64,
116    /// Size of the last trade.
117    pub last_size: f64,
118}
119
120/// Represents a ping message from the HTX WebSocket API.
121///
122/// This struct contains a ping message used for connection keep-alive.
123/// The ping value is typically a timestamp that should be echoed back in a pong response.
124#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
125#[serde(rename_all = "camelCase")]
126pub struct Ping {
127    /// The ping value, typically a timestamp.
128    pub ping: u64,
129}
130
131/// Represents a pong message to send to the HTX WebSocket API.
132///
133/// This struct contains a pong message used to respond to ping messages
134/// for connection keep-alive. The pong value should match the ping value received.
135#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
136#[serde(rename_all = "camelCase")]
137pub struct Pong {
138    /// The pong value, typically echoing the ping value received.
139    pub pong: u64,
140}
141
142/// Represents an error response from the HTX WebSocket API.
143///
144/// This struct contains error information when the API returns an error response,
145/// including error codes, messages, and timestamps.
146#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
147#[serde(rename_all = "kebab-case")]
148pub struct Error {
149    /// Status of the error response.
150    pub status: String,
151    /// Unix timestamp in milliseconds when the error occurred.
152    pub ts: u64,
153    /// Error code indicating the type of error.
154    pub err_code: String,
155    /// Human-readable error message.
156    pub err_msg: String,
157}