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::time::Duration;
17
18use nautilus_hyperliquid::{
19    common::consts::{HyperliquidNetwork, 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    tracing::info!("Starting Hyperliquid WebSocket execution example");
32
33    // Determine network and get WebSocket URL
34    let network = HyperliquidNetwork::from_env();
35    let ws_url = ws_url(network);
36
37    let mut client = HyperliquidWebSocketClient::connect(ws_url).await?;
38    tracing::info!("Connected to Hyperliquid WebSocket");
39
40    // Subscribe to execution channels
41    let user_addr = std::env::var("HYPERLIQUID_USER_ADDRESS")
42        .unwrap_or_else(|_| "0x0000000000000000000000000000000000000000".to_string());
43
44    // Subscribe to all user channels using the convenience method
45    client.subscribe_all_user_channels(&user_addr).await?;
46    tracing::info!("Subscribed to all user channels for {}", user_addr);
47
48    // Wait briefly to ensure subscriptions are active
49    tokio::time::sleep(Duration::from_secs(1)).await;
50
51    // Create a future that completes on CTRL+C
52    let sigint = signal::ctrl_c();
53    pin!(sigint);
54
55    loop {
56        tokio::select! {
57            Some(message) = client.next_event() => {
58                tracing::debug!("{message:?}");
59            }
60            _ = &mut sigint => {
61                tracing::info!("Received SIGINT, closing connection...");
62                client.disconnect().await?;
63                break;
64            }
65            else => break,
66        }
67    }
68
69    Ok(())
70}