hyperliquid_http_public/
http_public.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;
17
18use nautilus_hyperliquid::http::client::HyperliquidHttpClient;
19use tracing::level_filters::LevelFilter;
20
21#[tokio::main]
22async fn main() -> Result<(), Box<dyn std::error::Error>> {
23    tracing_subscriber::fmt()
24        .with_max_level(LevelFilter::INFO)
25        .init();
26
27    let args: Vec<String> = env::args().collect();
28    let testnet = args.get(1).is_some_and(|s| s == "testnet");
29
30    tracing::info!("Starting Hyperliquid HTTP public example");
31    tracing::info!("Testnet: {testnet}");
32
33    let client = HyperliquidHttpClient::new(testnet, Some(60));
34
35    // Fetch metadata
36    let meta = client.info_meta().await?;
37    tracing::info!("Fetched {} markets", meta.universe.len());
38
39    // Fetch BTC order book
40    if let Ok(book) = client.info_l2_book("BTC").await {
41        let best_bid = book
42            .levels
43            .first()
44            .and_then(|bids| bids.first())
45            .map(|l| l.px.clone())
46            .unwrap_or_default();
47        let best_ask = book
48            .levels
49            .get(1)
50            .and_then(|asks| asks.first())
51            .map(|l| l.px.clone())
52            .unwrap_or_default();
53
54        tracing::info!("BTC best bid: {}, best ask: {}", best_bid, best_ask);
55    }
56
57    Ok(())
58}