Skip to main content

bothan_bybit/api/
types.rs

1//! Types for the Bybit WebSocket API.
2//!
3//! This module provides types for deserializing events and responses from the Bybit WebSocket API,
4//! including public messages, ticker updates, and other market data.
5
6use serde::{Deserialize, Serialize};
7
8/// The default URL for the Bybit WebSocket API.
9pub const DEFAULT_URL: &str = "wss://stream.bybit.com/v5/public/spot";
10/// The maximum number of arguments allowed in a single WebSocket request.
11pub const MAX_ARGS: usize = 10;
12
13/// Represents the different types of responses from the Bybit API.
14#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
15#[serde(rename_all = "snake_case", untagged)]
16pub enum Response {
17    /// Represents a public message response with an operation status.
18    PublicMessage(PublicMessageResponse),
19    /// Represents a public ticker response with market data.
20    PublicTicker(PublicTickerResponse),
21    /// Represents a ping message from the WebSocket API.
22    Ping,
23}
24
25/// Represents a public message response with an operation status.
26#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
27#[serde(rename_all = "snake_case")]
28pub struct PublicMessageResponse {
29    /// Whether the operation was successful.
30    pub success: bool,
31    /// The return message from the server.
32    pub ret_msg: String,
33    /// The connection ID.
34    pub conn_id: String,
35    /// The request ID, if present.
36    pub req_id: Option<String>,
37    /// The operation type.
38    pub op: String,
39}
40
41/// Represents a public ticker response with market data.
42#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
43#[serde(rename_all = "snake_case")]
44pub struct PublicTickerResponse {
45    /// The topic of the ticker.
46    pub topic: String,
47    /// The timestamp of the ticker event (in milliseconds).
48    pub ts: i64,
49    /// The type of the ticker event.
50    #[serde(rename = "type")]
51    pub ticker_type: String,
52    /// The cross sequence value, which is a unique identifier for the latest update in the stream.
53    pub cs: i64,
54    /// The ticker data.
55    pub data: Ticker,
56}
57
58/// Represents the market data structure for the PublicTickerResponse.
59#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
60#[serde(rename_all = "camelCase")]
61pub struct Ticker {
62    /// The trading symbol (e.g., SOLUSDT_SOL/USDT).
63    pub symbol: String,
64    /// The last traded price.
65    pub last_price: String,
66    /// The highest price in the last 24 hours.
67    pub high_price24h: String,
68    /// The lowest price in the last 24 hours.
69    pub low_price24h: String,
70    /// The previous price 24 hours ago.
71    pub prev_price24h: String,
72    /// The trading volume in the last 24 hours.
73    pub volume24h: String,
74    /// The trading turnover in the last 24 hours.
75    pub turnover24h: String,
76    /// The price change percentage in the last 24 hours.
77    pub price24h_pcnt: String,
78    /// The USD index price.
79    pub usd_index_price: String,
80}