hyperliquid_ws_data/
ws_data.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{env, time::Duration};
17
18use nautilus_hyperliquid::{
19    common::{HyperliquidProductType, consts::ws_url},
20    http::HyperliquidHttpClient,
21    websocket::client::HyperliquidWebSocketClient,
22};
23use nautilus_model::instruments::Instrument;
24use tokio::{pin, signal};
25use tracing::level_filters::LevelFilter;
26
27#[tokio::main]
28async fn main() -> Result<(), Box<dyn std::error::Error>> {
29    tracing_subscriber::fmt()
30        .with_max_level(LevelFilter::DEBUG)
31        .init();
32
33    let args: Vec<String> = env::args().collect();
34    let testnet = args.get(1).is_some_and(|s| s == "testnet");
35
36    tracing::info!("Starting Hyperliquid WebSocket data example");
37    tracing::info!("Testnet: {testnet}");
38
39    // Load instruments first
40    let http_client = HyperliquidHttpClient::new(testnet, None, None)?;
41    let instruments = http_client.request_instruments().await?;
42    tracing::info!("Loaded {} instruments", instruments.len());
43
44    // Find BTC-USD-PERP instrument (raw_symbol is "BTC" for BTC-USD-PERP)
45    let btc_inst = instruments
46        .iter()
47        .find(|i| i.raw_symbol().as_str() == "BTC")
48        .ok_or("BTC-USD-PERP instrument not found")?;
49    let instrument_id = match btc_inst {
50        nautilus_model::instruments::InstrumentAny::CryptoPerpetual(inst) => inst.id,
51        _ => return Err("Expected CryptoPerpetual instrument".into()),
52    };
53    tracing::info!("Using instrument: {}", instrument_id);
54
55    let ws_url = ws_url(testnet);
56    tracing::info!("WebSocket URL: {ws_url}");
57
58    let mut client = HyperliquidWebSocketClient::new(
59        Some(ws_url.to_string()),
60        testnet,
61        HyperliquidProductType::Perp,
62        None,
63    );
64
65    // Cache instruments before connecting
66    client.cache_instruments(instruments);
67
68    client.connect().await?;
69    tracing::info!("Connected to Hyperliquid WebSocket");
70
71    // Wait for connection to be fully established
72    tokio::time::sleep(Duration::from_millis(500)).await;
73
74    tracing::info!("Subscribing to trades for {}", instrument_id);
75    client.subscribe_trades(instrument_id).await?;
76
77    tracing::info!("Subscribing to BBO for {}", instrument_id);
78    client.subscribe_quotes(instrument_id).await?;
79
80    // Wait briefly to ensure subscriptions are processed
81    tokio::time::sleep(Duration::from_secs(1)).await;
82
83    // Create a future that completes on CTRL+C
84    let sigint = signal::ctrl_c();
85    pin!(sigint);
86
87    let mut message_count = 0;
88    loop {
89        tokio::select! {
90            Some(message) = client.next_event() => {
91                message_count += 1;
92                tracing::info!("Message #{}: {:?}", message_count, message);
93            }
94            _ = &mut sigint => {
95                tracing::info!("Received SIGINT, closing connection...");
96                client.disconnect().await?;
97                break;
98            }
99            else => break,
100        }
101    }
102
103    tracing::info!("Received {} total messages", message_count);
104    Ok(())
105}