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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use chrono::{DateTime, FixedOffset, Utc};
use log::{debug, info, error};
use thiserror::Error;
use tokio::sync::mpsc::{self, Receiver, Sender};
use tokio::task::JoinHandle;
use tokio::time::sleep;
use hyperliquid_rust_sdk::InfoClient;
// Note: These types may not be available in the current SDK version
// use hyperliquid_rust_sdk::{WsManager, Subscription};
use crate::unified_data::{MarketData, OrderBookLevel, OrderBookSnapshot, Trade, OrderSide};
// Placeholder types for missing SDK types
#[derive(Debug, Clone)]
pub struct WsMessage {
pub data: String,
}
#[derive(Debug)]
pub struct WsManager {
// Placeholder implementation
}
impl WsManager {
pub fn new(_url: &str) -> Result<Self, Box<dyn std::error::Error>> {
Ok(Self {})
}
pub async fn connect(&mut self) -> Result<(), Box<dyn std::error::Error>> {
// Placeholder implementation
Ok(())
}
}
/// Error types specific to real-time data operations
#[derive(Debug, Error)]
pub enum RealTimeDataError {
/// Error when WebSocket connection fails
#[error("WebSocket connection error: {0}")]
WebSocketError(String),
/// Error when subscription fails
#[error("Subscription error for {symbol}: {message}")]
SubscriptionError {
symbol: String,
message: String,
},
/// Error when data processing fails
#[error("Data processing error: {0}")]
DataProcessingError(String),
/// Error when Hyperliquid API fails
#[error("Hyperliquid API error: {0}")]
HyperliquidApiError(String),
/// Error when subscription is not found
#[error("Subscription not found for {0}")]
SubscriptionNotFound(String),
}
/// Subscription type for real-time data
#[derive(Debug, Clone)]
pub enum SubscriptionType {
/// Ticker subscription (price, volume, etc.)
Ticker,
/// Trades subscription
Trades,
/// Order book subscription
OrderBook,
/// Candles subscription
Candles,
/// Funding rate subscription
FundingRate,
}
/// Subscription information
#[derive(Debug, Clone)]
pub struct DataSubscription {
/// Symbol/ticker
pub symbol: String,
/// Subscription type
pub subscription_type: SubscriptionType,
/// Subscription ID
pub id: String,
/// Subscription timestamp
pub timestamp: DateTime<FixedOffset>,
/// Is active
pub active: bool,
}
/// Real-time data stream for market data
pub struct RealTimeDataStream {
/// WebSocket manager
ws_manager: Option<WsManager>,
/// Info client for REST API calls
info_client: InfoClient,
/// Active subscriptions
subscriptions: HashMap<String, DataSubscription>,
/// Market data cache
market_data: Arc<Mutex<HashMap<String, MarketData>>>,
/// Order book snapshots
order_books: Arc<Mutex<HashMap<String, OrderBookSnapshot>>>,
/// Recent trades
recent_trades: Arc<Mutex<HashMap<String, Vec<Trade>>>>,
/// Message channel sender
message_sender: Option<Sender<WsMessage>>,
/// Message processing task handle
message_task: Option<JoinHandle<()>>,
/// Is connected
is_connected: bool,
/// Last heartbeat
last_heartbeat: Instant,
/// Connection URL
connection_url: String,
/// Reconnect attempts
reconnect_attempts: u32,
/// Maximum reconnect attempts
max_reconnect_attempts: u32,
}
impl RealTimeDataStream {
/// Create a new real-time data stream
pub async fn new() -> Result<Self, RealTimeDataError> {
let info_client = InfoClient::new(None, Some(hyperliquid_rust_sdk::BaseUrl::Mainnet)).await
.map_err(|e| RealTimeDataError::HyperliquidApiError(format!("Failed to create InfoClient: {}", e)))?;
Ok(Self {
ws_manager: None,
info_client,
subscriptions: HashMap::new(),
market_data: Arc::new(Mutex::new(HashMap::new())),
order_books: Arc::new(Mutex::new(HashMap::new())),
recent_trades: Arc::new(Mutex::new(HashMap::new())),
message_sender: None,
message_task: None,
is_connected: false,
last_heartbeat: Instant::now(),
connection_url: "wss://api.hyperliquid.xyz/ws".to_string(),
reconnect_attempts: 0,
max_reconnect_attempts: 5,
})
}
/// Connect to the WebSocket server
pub async fn connect(&mut self) -> Result<(), RealTimeDataError> {
if self.is_connected {
return Ok(());
}
info!("Connecting to WebSocket server: {}", self.connection_url);
// Create WebSocket manager
let ws_manager = WsManager::new(&self.connection_url).map_err(|e| {
RealTimeDataError::WebSocketError(format!("Failed to create WsManager: {}", e))
})?;
// Create message channel
let (tx, rx) = mpsc::channel::<WsMessage>(100);
// Start message processing task
let market_data = self.market_data.clone();
let order_books = self.order_books.clone();
let recent_trades = self.recent_trades.clone();
let message_task = tokio::spawn(async move {
Self::process_messages(rx, market_data, order_books, recent_trades).await;
});
self.ws_manager = Some(ws_manager);
self.message_sender = Some(tx);
self.message_task = Some(message_task);
self.is_connected = true;
self.last_heartbeat = Instant::now();
self.reconnect_attempts = 0;
info!("Connected to WebSocket server");
// Start heartbeat task
self.start_heartbeat_task();
Ok(())
}
/// Disconnect from the WebSocket server
pub async fn disconnect(&mut self) -> Result<(), RealTimeDataError> {
if !self.is_connected {
return Ok(());
}
info!("Disconnecting from WebSocket server");
// Close WebSocket connection
if let Some(ws_manager) = &self.ws_manager {
// In a real implementation, we would close the WebSocket connection
// For now, we'll just set the flag
}
// Cancel message processing task
if let Some(task) = &self.message_task {
task.abort();
}
self.ws_manager = None;
self.message_sender = None;
self.message_task = None;
self.is_connected = false;
info!("Disconnected from WebSocket server");
Ok(())
}
/// Subscribe to ticker updates
pub async fn subscribe_ticker(&mut self, symbol: &str) -> Result<(), RealTimeDataError> {
self.ensure_connected().await?;
let subscription_id = format!("ticker_{}", symbol);
// Check if already subscribed
if self.subscriptions.contains_key(&subscription_id) {
return Ok(());
}
info!("Subscribing to ticker updates for {}", symbol);
// Create subscription
let subscription = DataSubscription {
symbol: symbol.to_string(),
subscription_type: SubscriptionType::Ticker,
id: subscription_id.clone(),
timestamp: Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap()),
active: true,
};
// In a real implementation, we would send a subscription message to the WebSocket server
// For now, we'll just add it to our subscriptions map
self.subscriptions.insert(subscription_id, subscription);
// Initialize market data
let mut market_data_lock = self.market_data.lock().unwrap();
if !market_data_lock.contains_key(symbol) {
// Fetch initial data from REST API
// Placeholder for candles data - in real implementation this would fetch from API
let candles: Vec<hyperliquid_rust_sdk::CandlesSnapshotResponse> = vec![];
if let Some(candle) = candles.first() {
let now = Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap());
let price = candle.close.parse::<f64>().unwrap_or(0.0);
let market_data = MarketData::new(
symbol,
price,
price * 0.9999, // Simulated bid
price * 1.0001, // Simulated ask
0.0, // Placeholder volume since candle.volume doesn't exist
now,
);
market_data_lock.insert(symbol.to_string(), market_data);
}
}
Ok(())
}
/// Subscribe to order book updates
pub async fn subscribe_order_book(&mut self, symbol: &str) -> Result<(), RealTimeDataError> {
self.ensure_connected().await?;
let subscription_id = format!("orderbook_{}", symbol);
// Check if already subscribed
if self.subscriptions.contains_key(&subscription_id) {
return Ok(());
}
info!("Subscribing to order book updates for {}", symbol);
// Create subscription
let subscription = DataSubscription {
symbol: symbol.to_string(),
subscription_type: SubscriptionType::OrderBook,
id: subscription_id.clone(),
timestamp: Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap()),
active: true,
};
// In a real implementation, we would send a subscription message to the WebSocket server
// For now, we'll just add it to our subscriptions map
self.subscriptions.insert(subscription_id, subscription);
// Initialize order book
let mut order_books_lock = self.order_books.lock().unwrap();
if !order_books_lock.contains_key(symbol) {
// Fetch initial data from REST API
// In a real implementation, we would fetch the order book snapshot
// For now, we'll create a simulated order book
let now = Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap());
// Get the current price from market data
let price = {
let market_data_lock = self.market_data.lock().unwrap();
market_data_lock.get(symbol)
.map(|data| data.price)
.unwrap_or(50000.0) // Default price if not available
};
// Create simulated order book
let mut bids = Vec::new();
let mut asks = Vec::new();
// Add 10 levels on each side
for i in 1..=10 {
let bid_price = price * (1.0 - 0.0001 * i as f64);
let ask_price = price * (1.0 + 0.0001 * i as f64);
let quantity = 1.0 / i as f64;
bids.push(OrderBookLevel {
price: bid_price,
quantity,
});
asks.push(OrderBookLevel {
price: ask_price,
quantity,
});
}
let order_book = OrderBookSnapshot {
bids,
asks,
timestamp: now,
};
order_books_lock.insert(symbol.to_string(), order_book);
}
Ok(())
}
/// Subscribe to trades
pub async fn subscribe_trades(&mut self, symbol: &str) -> Result<(), RealTimeDataError> {
self.ensure_connected().await?;
let subscription_id = format!("trades_{}", symbol);
// Check if already subscribed
if self.subscriptions.contains_key(&subscription_id) {
return Ok(());
}
info!("Subscribing to trades for {}", symbol);
// Create subscription
let subscription = DataSubscription {
symbol: symbol.to_string(),
subscription_type: SubscriptionType::Trades,
id: subscription_id.clone(),
timestamp: Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap()),
active: true,
};
// In a real implementation, we would send a subscription message to the WebSocket server
// For now, we'll just add it to our subscriptions map
self.subscriptions.insert(subscription_id, subscription);
// Initialize trades
let mut trades_lock = self.recent_trades.lock().unwrap();
if !trades_lock.contains_key(symbol) {
trades_lock.insert(symbol.to_string(), Vec::new());
}
Ok(())
}
/// Subscribe to funding rate updates
pub async fn subscribe_funding_rate(&mut self, symbol: &str) -> Result<(), RealTimeDataError> {
self.ensure_connected().await?;
let subscription_id = format!("funding_{}", symbol);
// Check if already subscribed
if self.subscriptions.contains_key(&subscription_id) {
return Ok(());
}
info!("Subscribing to funding rate updates for {}", symbol);
// Create subscription
let subscription = DataSubscription {
symbol: symbol.to_string(),
subscription_type: SubscriptionType::FundingRate,
id: subscription_id.clone(),
timestamp: Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap()),
active: true,
};
// In a real implementation, we would send a subscription message to the WebSocket server
// For now, we'll just add it to our subscriptions map
self.subscriptions.insert(subscription_id, subscription);
// Update market data with funding rate
let mut market_data_lock = self.market_data.lock().unwrap();
if let Some(market_data) = market_data_lock.get_mut(symbol) {
// Fetch funding rate from REST API
// In a real implementation, we would fetch the actual funding rate
// For now, we'll use a simulated value
let funding_rate = 0.0001; // 0.01% per 8 hours
let next_funding_time = Utc::now()
.with_timezone(&FixedOffset::east_opt(0).unwrap())
.checked_add_signed(chrono::Duration::hours(8))
.unwrap_or_else(|| Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap()));
*market_data = market_data.clone().with_funding_rate(funding_rate, next_funding_time);
}
Ok(())
}
/// Unsubscribe from updates
pub async fn unsubscribe(&mut self, symbol: &str, subscription_type: SubscriptionType) -> Result<(), RealTimeDataError> {
let subscription_id = match subscription_type {
SubscriptionType::Ticker => format!("ticker_{}", symbol),
SubscriptionType::OrderBook => format!("orderbook_{}", symbol),
SubscriptionType::Trades => format!("trades_{}", symbol),
SubscriptionType::Candles => format!("candles_{}", symbol),
SubscriptionType::FundingRate => format!("funding_{}", symbol),
};
// Check if subscribed
if !self.subscriptions.contains_key(&subscription_id) {
return Err(RealTimeDataError::SubscriptionNotFound(subscription_id));
}
info!("Unsubscribing from {:?} updates for {}", subscription_type, symbol);
// In a real implementation, we would send an unsubscribe message to the WebSocket server
// For now, we'll just remove it from our subscriptions map
self.subscriptions.remove(&subscription_id);
Ok(())
}
/// Get the latest market data for a symbol
pub fn get_market_data(&self, symbol: &str) -> Option<MarketData> {
let market_data_lock = self.market_data.lock().unwrap();
market_data_lock.get(symbol).cloned()
}
/// Get the latest order book for a symbol
pub fn get_order_book(&self, symbol: &str) -> Option<OrderBookSnapshot> {
let order_books_lock = self.order_books.lock().unwrap();
order_books_lock.get(symbol).cloned()
}
/// Get recent trades for a symbol
pub fn get_recent_trades(&self, symbol: &str) -> Option<Vec<Trade>> {
let trades_lock = self.recent_trades.lock().unwrap();
trades_lock.get(symbol).cloned()
}
/// Get all subscribed symbols
pub fn get_subscribed_symbols(&self) -> Vec<String> {
self.subscriptions.values()
.map(|sub| sub.symbol.clone())
.collect::<std::collections::HashSet<String>>()
.into_iter()
.collect()
}
/// Check if connected to WebSocket server
pub fn is_connected(&self) -> bool {
self.is_connected
}
/// Ensure connected to WebSocket server
async fn ensure_connected(&mut self) -> Result<(), RealTimeDataError> {
if !self.is_connected {
self.connect().await?;
}
Ok(())
}
/// Start heartbeat task
fn start_heartbeat_task(&self) {
let market_data = self.market_data.clone();
let order_books = self.order_books.clone();
let recent_trades = self.recent_trades.clone();
tokio::spawn(async move {
loop {
sleep(Duration::from_secs(1)).await;
// Simulate market data updates
let mut market_data_lock = market_data.lock().unwrap();
for (symbol, data) in market_data_lock.iter_mut() {
// Simulate price movement
let price_change = (rand::random::<f64>() - 0.5) * 0.001 * data.price;
let new_price = data.price + price_change;
// Update market data
*data = MarketData::new(
&symbol,
new_price,
new_price * 0.9999, // Simulated bid
new_price * 1.0001, // Simulated ask
data.volume * (0.9 + rand::random::<f64>() * 0.2), // Simulated volume
Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap()),
);
// Preserve funding rate if it exists
if let Some(funding_rate) = data.funding_rate {
if let Some(next_funding_time) = data.next_funding_time {
*data = data.clone().with_funding_rate(funding_rate, next_funding_time);
}
}
}
// Simulate order book updates
let mut order_books_lock = order_books.lock().unwrap();
for (symbol, book) in order_books_lock.iter_mut() {
// Get the current price from market data
let price = market_data_lock.get(symbol)
.map(|data| data.price)
.unwrap_or(50000.0);
// Update order book
let mut bids = Vec::new();
let mut asks = Vec::new();
// Add 10 levels on each side
for i in 1..=10 {
let bid_price = price * (1.0 - 0.0001 * i as f64);
let ask_price = price * (1.0 + 0.0001 * i as f64);
let quantity = 1.0 / i as f64 * (0.9 + rand::random::<f64>() * 0.2);
bids.push(OrderBookLevel {
price: bid_price,
quantity,
});
asks.push(OrderBookLevel {
price: ask_price,
quantity,
});
}
*book = OrderBookSnapshot {
bids,
asks,
timestamp: Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap()),
};
}
// Simulate trades
let mut trades_lock = recent_trades.lock().unwrap();
for (symbol, trades) in trades_lock.iter_mut() {
// Get the current price from market data
if let Some(data) = market_data_lock.get(symbol) {
// Simulate a new trade
if rand::random::<f64>() < 0.3 {
// 30% chance of a new trade
let side = if rand::random::<bool>() {
OrderSide::Buy
} else {
OrderSide::Sell
};
let price = data.price * (0.9995 + rand::random::<f64>() * 0.001);
let quantity = 0.01 + rand::random::<f64>() * 0.1;
let trade = Trade {
id: format!("trade_{}", Utc::now().timestamp_millis()),
price,
quantity,
timestamp: Utc::now().with_timezone(&FixedOffset::east_opt(0).unwrap()),
side: Some(side),
};
trades.push(trade);
// Keep only the last 100 trades
if trades.len() > 100 {
trades.remove(0);
}
}
}
}
}
});
}
/// Process WebSocket messages
async fn process_messages(
mut rx: Receiver<WsMessage>,
market_data: Arc<Mutex<HashMap<String, MarketData>>>,
order_books: Arc<Mutex<HashMap<String, OrderBookSnapshot>>>,
recent_trades: Arc<Mutex<HashMap<String, Vec<Trade>>>>,
) {
while let Some(message) = rx.recv().await {
// In a real implementation, we would process the WebSocket messages
// For now, we'll just log them
debug!("Received WebSocket message: {:?}", message);
// Process message based on type
// This is a placeholder for the actual message processing logic
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_real_time_data_stream_creation() {
let stream = RealTimeDataStream::new().await;
assert!(stream.is_ok());
}
#[tokio::test]
async fn test_market_data_subscription() {
let mut stream = RealTimeDataStream::new().await.unwrap();
// Subscribe to ticker
let result = stream.subscribe_ticker("BTC").await;
assert!(result.is_ok());
// Check that we have market data
let market_data = stream.get_market_data("BTC");
assert!(market_data.is_some());
// Check subscription
let subscribed_symbols = stream.get_subscribed_symbols();
assert!(subscribed_symbols.contains(&"BTC".to_string()));
}
#[tokio::test]
async fn test_order_book_subscription() {
let mut stream = RealTimeDataStream::new().await.unwrap();
// Subscribe to order book
let result = stream.subscribe_order_book("BTC").await;
assert!(result.is_ok());
// Check that we have an order book
let order_book = stream.get_order_book("BTC");
assert!(order_book.is_some());
// Check that the order book has bids and asks
let order_book = order_book.unwrap();
assert!(!order_book.bids.is_empty());
assert!(!order_book.asks.is_empty());
}
#[tokio::test]
async fn test_trades_subscription() {
let mut stream = RealTimeDataStream::new().await.unwrap();
// Subscribe to trades
let result = stream.subscribe_trades("BTC").await;
assert!(result.is_ok());
// Check that we have a trades vector
let trades = stream.get_recent_trades("BTC");
assert!(trades.is_some());
}
#[tokio::test]
async fn test_funding_rate_subscription() {
let mut stream = RealTimeDataStream::new().await.unwrap();
// Subscribe to ticker first
stream.subscribe_ticker("BTC").await.unwrap();
// Subscribe to funding rate
let result = stream.subscribe_funding_rate("BTC").await;
assert!(result.is_ok());
// Check that market data has funding rate
let market_data = stream.get_market_data("BTC").unwrap();
assert!(market_data.funding_rate.is_some());
assert!(market_data.next_funding_time.is_some());
}
#[tokio::test]
async fn test_unsubscribe() {
let mut stream = RealTimeDataStream::new().await.unwrap();
// Subscribe to ticker
stream.subscribe_ticker("BTC").await.unwrap();
// Check subscription
let subscribed_symbols = stream.get_subscribed_symbols();
assert!(subscribed_symbols.contains(&"BTC".to_string()));
// Unsubscribe
let result = stream.unsubscribe("BTC", SubscriptionType::Ticker).await;
assert!(result.is_ok());
// Check subscription is removed
let subscribed_symbols = stream.get_subscribed_symbols();
assert!(!subscribed_symbols.contains(&"BTC".to_string()));
}
}