rusty_common/websocket/exchanges/
bybit.rs

1//! Bybit WebSocket configuration
2//!
3//! Bybit-specific WebSocket settings and handlers.
4
5use crate::types::Exchange;
6use crate::websocket::{CompressionConfig, WebSocketConfig};
7use std::time::Duration;
8
9/// Get default Bybit WebSocket configuration
10pub fn default_config(url: String) -> WebSocketConfig {
11    WebSocketConfig::builder(Exchange::Bybit, url)
12        .connect_timeout(Duration::from_secs(10))
13        .timeout(Duration::from_secs(30))
14        .ping_interval(Duration::from_secs(20)) // Bybit recommends 20 seconds
15        .pong_timeout(Duration::from_secs(10))
16        .max_frame_size(65536) // 64KB
17        .max_message_size(10 * 1024 * 1024) // 10MB
18        // Bybit supports compression
19        .compression(CompressionConfig::default())
20        .build()
21}
22
23/// Bybit WebSocket URLs
24pub mod urls {
25    /// V5 Public WebSocket URL
26    pub const V5_PUBLIC: &str = "wss://stream.bybit.com/v5/public/spot";
27
28    /// V5 Private WebSocket URL
29    pub const V5_PRIVATE: &str = "wss://stream.bybit.com/v5/private";
30
31    /// V5 Public Linear (USDT Perpetual) URL
32    pub const V5_PUBLIC_LINEAR: &str = "wss://stream.bybit.com/v5/public/linear";
33
34    /// V5 Public Inverse URL
35    pub const V5_PUBLIC_INVERSE: &str = "wss://stream.bybit.com/v5/public/inverse";
36
37    /// V5 Public Option URL
38    pub const V5_PUBLIC_OPTION: &str = "wss://stream.bybit.com/v5/public/option";
39
40    /// Testnet V5 Public URL
41    pub const TESTNET_V5_PUBLIC: &str = "wss://stream-testnet.bybit.com/v5/public/spot";
42
43    /// Testnet V5 Private URL
44    pub const TESTNET_V5_PRIVATE: &str = "wss://stream-testnet.bybit.com/v5/private";
45}
46
47/// Create a subscription message for Bybit
48pub fn create_subscription(req_id: &str, topics: Vec<String>) -> simd_json::OwnedValue {
49    simd_json::json!({
50        "req_id": req_id,
51        "op": "subscribe",
52        "args": topics
53    })
54}
55
56/// Create an authentication message for Bybit
57pub fn create_auth_message(api_key: &str, expires: i64, signature: &str) -> simd_json::OwnedValue {
58    simd_json::json!({
59        "req_id": "auth",
60        "op": "auth",
61        "args": [api_key, expires, signature]
62    })
63}