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::{common::consts::ws_url, websocket::client::HyperliquidWebSocketClient};
19use tokio::{pin, signal};
20use tracing::level_filters::LevelFilter;
21
22#[tokio::main]
23async fn main() -> Result<(), Box<dyn std::error::Error>> {
24    tracing_subscriber::fmt()
25        .with_max_level(LevelFilter::TRACE)
26        .init();
27
28    let args: Vec<String> = env::args().collect();
29    let testnet = args.get(1).is_some_and(|s| s == "testnet");
30
31    tracing::info!("Starting Hyperliquid WebSocket execution example");
32    tracing::info!("Testnet: {testnet}");
33
34    let ws_url = ws_url(testnet);
35    tracing::info!("WebSocket URL: {ws_url}");
36
37    let client = HyperliquidWebSocketClient::connect(ws_url).await?;
38    tracing::info!("Connected to Hyperliquid WebSocket");
39
40    // Subscribe to execution channels
41    let user_addr = 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}