-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstream_websocket_all.rs
More file actions
128 lines (104 loc) · 3.8 KB
/
stream_websocket_all.rs
File metadata and controls
128 lines (104 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! WebSocket All Channels Example
//!
//! Subscribe to multiple WebSocket channels simultaneously.
//!
//! # Usage
//! ```bash
//! export ENDPOINT="https://your-endpoint/TOKEN"
//! cargo run --example stream_websocket_all
//! ```
use hyperliquid_sdk::HyperliquidSDK;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let endpoint = std::env::var("ENDPOINT").ok();
if endpoint.is_none() {
eprintln!("Usage:");
eprintln!(" export ENDPOINT='https://your-endpoint/TOKEN'");
eprintln!(" cargo run --example stream_websocket_all");
std::process::exit(1);
}
println!("WebSocket All Channels Example");
println!("{}", "=".repeat(50));
// Available channels
println!("\n1. Available Channels:");
println!(" - l2Book: L2 orderbook updates");
println!(" - trades: Trade executions");
println!(" - orderUpdates: Order updates (requires auth)");
println!(" - fills: Fill notifications (requires auth)");
println!(" - allMids: All mid prices");
println!(" - candle: Candlestick data");
// Create SDK
let sdk = HyperliquidSDK::new()
.endpoint(endpoint.as_ref().unwrap())
.build()
.await?;
// Create stream via SDK
println!("\n2. Creating WebSocket stream...");
let channel_counts: Arc<Mutex<HashMap<String, usize>>> = Arc::new(Mutex::new(HashMap::new()));
let total_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let counts_l2 = channel_counts.clone();
let total_l2 = total_count.clone();
let counts_trades = channel_counts.clone();
let total_trades = total_count.clone();
let counts_mids = channel_counts.clone();
let total_mids = total_count.clone();
let mut stream = sdk.stream()
.on_open(|| {
println!(" [Connected]");
})
.on_error(|e| {
eprintln!(" [Error] {}", e);
});
// Subscribe to multiple channels
println!("\n3. Subscribing to channels...");
// L2 Book
let _h1 = stream.l2_book("BTC", move |_data| {
let mut counts = counts_l2.lock().unwrap();
*counts.entry("l2Book".to_string()).or_insert(0) += 1;
let t = total_l2.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1;
if t <= 10 {
println!(" [{}] l2Book", t);
}
});
println!(" Subscribed: l2Book BTC");
// Trades
let _h2 = stream.trades(&["BTC"], move |_data| {
let mut counts = counts_trades.lock().unwrap();
*counts.entry("trades".to_string()).or_insert(0) += 1;
let t = total_trades.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1;
if t <= 10 {
println!(" [{}] trades", t);
}
});
println!(" Subscribed: trades BTC");
// All mids
let _h3 = stream.all_mids(move |_data| {
let mut counts = counts_mids.lock().unwrap();
*counts.entry("allMids".to_string()).or_insert(0) += 1;
let t = total_mids.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1;
if t <= 10 {
println!(" [{}] allMids", t);
}
});
println!(" Subscribed: allMids");
// Start streaming
println!("\n4. Receiving messages (30 seconds):");
stream.start()?;
// Run for 30 seconds
tokio::time::sleep(Duration::from_secs(30)).await;
stream.stop();
// Summary
let total = total_count.load(std::sync::atomic::Ordering::SeqCst);
let counts = channel_counts.lock().unwrap();
println!("\n5. Message Summary:");
for (channel, count) in counts.iter() {
println!(" {}: {} messages", channel, count);
}
println!(" Total: {} messages", total);
println!("\n{}", "=".repeat(50));
println!("Done!");
Ok(())
}