hyperliquid_ws_exec/
ws_exec.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    websocket::client::HyperliquidWebSocketClient,
21};
22use tokio::{pin, signal};
23use tracing::level_filters::LevelFilter;
24
25#[tokio::main]
26async fn main() -> Result<(), Box<dyn std::error::Error>> {
27    tracing_subscriber::fmt()
28        .with_max_level(LevelFilter::TRACE)
29        .init();
30
31    let args: Vec<String> = env::args().collect();
32    let testnet = args.get(1).is_some_and(|s| s == "testnet");
33
34    tracing::info!("Starting Hyperliquid WebSocket execution example");
35    tracing::info!("Testnet: {testnet}");
36
37    let ws_url = ws_url(testnet);
38    tracing::info!("WebSocket URL: {ws_url}");
39
40    let mut client = HyperliquidWebSocketClient::new(
41        Some(ws_url.to_string()),
42        testnet,
43        HyperliquidProductType::Perp,
44        None,
45    );
46    client.connect().await?;
47    tracing::info!("Connected to Hyperliquid WebSocket");
48
49    // Subscribe to execution channels
50    let user_addr = env::var("HYPERLIQUID_USER_ADDRESS")
51        .unwrap_or_else(|_| "0x0000000000000000000000000000000000000000".to_string());
52
53    // Subscribe to all user channels using the convenience method
54    client.subscribe_all_user_channels(&user_addr).await?;
55    tracing::info!("Subscribed to all user channels for {}", user_addr);
56
57    // Wait briefly to ensure subscriptions are active
58    tokio::time::sleep(Duration::from_secs(1)).await;
59
60    // Create a future that completes on CTRL+C
61    let sigint = signal::ctrl_c();
62    pin!(sigint);
63
64    loop {
65        tokio::select! {
66            Some(message) = client.next_event() => {
67                tracing::debug!("{message:?}");
68            }
69            _ = &mut sigint => {
70                tracing::info!("Received SIGINT, closing connection...");
71                client.disconnect().await?;
72                break;
73            }
74            else => break,
75        }
76    }
77
78    Ok(())
79}