-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfo_batch_queries.rs
More file actions
110 lines (98 loc) · 3.73 KB
/
info_batch_queries.rs
File metadata and controls
110 lines (98 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! Info API Batch Queries Example
//!
//! Efficiently fetch multiple pieces of data.
//!
//! # Usage
//! ```bash
//! export ENDPOINT="https://your-endpoint.hype-mainnet.quiknode.pro/TOKEN"
//! cargo run --example info_batch_queries
//! ```
use hyperliquid_sdk::HyperliquidSDK;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let endpoint = std::env::var("ENDPOINT").ok();
if endpoint.is_none() {
eprintln!("Usage:");
eprintln!(" export ENDPOINT='https://your-endpoint.hype-mainnet.quiknode.pro/TOKEN'");
eprintln!(" cargo run --example info_batch_queries");
std::process::exit(1);
}
println!("Info API Batch Queries Example");
println!("{}", "=".repeat(50));
let mut builder = HyperliquidSDK::new();
if let Some(ep) = &endpoint {
builder = builder.endpoint(ep);
}
let sdk = builder.build().await?;
let info = sdk.info();
// Get all mid prices
println!("\n1. All Mid Prices:");
match info.all_mids(None).await {
Ok(mids) => {
let assets = ["BTC", "ETH", "SOL", "DOGE", "ARB"];
for asset in &assets {
if let Some(mid) = mids.get(*asset).and_then(|v| v.as_str()) {
println!(" {}: ${}", asset, mid);
}
}
}
Err(e) => println!(" Error: {}", e),
}
// Get exchange metadata
println!("\n2. Exchange Metadata:");
match info.meta().await {
Ok(meta) => {
if let Some(universe) = meta.get("universe").and_then(|v| v.as_array()) {
println!(" Perp markets: {}", universe.len());
}
}
Err(e) => println!(" Error: {}", e),
}
match info.spot_meta().await {
Ok(spot) => {
if let Some(tokens) = spot.get("tokens").and_then(|v| v.as_array()) {
println!(" Spot tokens: {}", tokens.len());
}
}
Err(e) => println!(" Error: {}", e),
}
// Get L2 orderbook for multiple assets
println!("\n3. Orderbook Spreads:");
for asset in &["BTC", "ETH", "SOL"] {
match info.l2_book(asset, None, None).await {
Ok(book) => {
if let Some(levels) = book.get("levels").and_then(|v| v.as_array()) {
if levels.len() >= 2 {
let bids = levels[0].as_array();
let asks = levels[1].as_array();
if let (Some(bids), Some(asks)) = (bids, asks) {
if let (Some(bid), Some(ask)) = (bids.first(), asks.first()) {
let bid_px = bid.get("px").and_then(|v| v.as_str()).unwrap_or("?");
let ask_px = ask.get("px").and_then(|v| v.as_str()).unwrap_or("?");
println!(" {}: bid={} ask={}", asset, bid_px, ask_px);
}
}
}
}
}
Err(e) => println!(" {}: Error - {}", asset, e),
}
}
// Get predicted funding rates
println!("\n4. Predicted Funding Rates:");
match info.predicted_fundings().await {
Ok(fundings) => {
if let Some(arr) = fundings.as_array() {
for (i, funding) in arr.iter().take(5).enumerate() {
let asset = funding.get("asset").and_then(|v| v.as_str()).unwrap_or("?");
let rate = funding.get("predictedFunding").and_then(|v| v.as_str()).unwrap_or("?");
println!(" [{}] {}: {}", i + 1, asset, rate);
}
}
}
Err(e) => println!(" Error: {}", e),
}
println!("\n{}", "=".repeat(50));
println!("Done!");
Ok(())
}