Skip to main content

guilder_client_hyperliquid/
client.rs

1use alloy_primitives::Address;
2use guilder_abstraction::{self, L2Update, Fill, AssetContext, Liquidation, BoxStream, Side, OrderSide, OrderStatus, OrderType, TimeInForce, OrderPlacement, Position, OpenOrder, UserFill, OrderUpdate, FundingPayment, Deposit, Withdrawal};
3use futures_util::{stream, SinkExt, StreamExt};
4use reqwest::Client;
5use rust_decimal::Decimal;
6use serde::Deserialize;
7use serde_json::Value;
8use std::collections::HashMap;
9use std::str::FromStr;
10use tokio_tungstenite::{connect_async, tungstenite::Message};
11
12const HYPERLIQUID_INFO_URL: &str = "https://api.hyperliquid.xyz/info";
13const HYPERLIQUID_EXCHANGE_URL: &str = "https://api.hyperliquid.xyz/exchange";
14const HYPERLIQUID_WS_URL: &str = "wss://api.hyperliquid.xyz/ws";
15
16async fn parse_response<T: for<'de> serde::Deserialize<'de>>(resp: reqwest::Response) -> Result<T, String> {
17    let text = resp.text().await.map_err(|e| e.to_string())?;
18    serde_json::from_str(&text).map_err(|e| format!("{e}: {text}"))
19}
20
21pub struct HyperliquidClient {
22    client: Client,
23    user_address: Option<Address>,
24    private_key: Option<String>,
25}
26
27impl HyperliquidClient {
28    pub fn new() -> Self {
29        HyperliquidClient { client: Client::new(), user_address: None, private_key: None }
30    }
31
32    pub fn with_auth(user_address: Address, private_key: String) -> Self {
33        HyperliquidClient { client: Client::new(), user_address: Some(user_address), private_key: Some(private_key) }
34    }
35
36    fn require_user_address(&self) -> Result<String, String> {
37        self.user_address
38            .map(|a| format!("{:#x}", a))
39            .ok_or_else(|| "user address required: use HyperliquidClient::with_auth".to_string())
40    }
41
42    fn require_private_key(&self) -> Result<&str, String> {
43        self.private_key.as_deref().ok_or_else(|| "private key required: use HyperliquidClient::with_auth".to_string())
44    }
45
46    async fn get_asset_index(&self, symbol: &str) -> Result<usize, String> {
47        let resp = self.client
48            .post(HYPERLIQUID_INFO_URL)
49            .json(&serde_json::json!({"type": "meta"}))
50            .send()
51            .await
52            .map_err(|e| e.to_string())?;
53        let meta: MetaResponse = parse_response(resp).await?;
54        meta.universe.iter()
55            .position(|a| a.name == symbol)
56            .ok_or_else(|| format!("symbol {} not found", symbol))
57    }
58
59    async fn submit_signed_action(&self, action: Value, vault_address: Option<&str>) -> Result<Value, String> {
60        let private_key = self.require_private_key()?;
61        let nonce = std::time::SystemTime::now()
62            .duration_since(std::time::UNIX_EPOCH)
63            .unwrap()
64            .as_millis() as u64;
65
66        let (r, s, v) = sign_action(private_key, &action, vault_address, nonce)?;
67
68        let payload = serde_json::json!({
69            "action": action,
70            "nonce": nonce,
71            "signature": {"r": r, "s": s, "v": v},
72            "vaultAddress": null
73        });
74
75        let resp = self.client
76            .post(HYPERLIQUID_EXCHANGE_URL)
77            .json(&payload)
78            .send()
79            .await
80            .map_err(|e| e.to_string())?;
81
82        let body: Value = parse_response(resp).await?;
83        if body["status"].as_str() == Some("err") {
84            return Err(body["response"].as_str().unwrap_or("unknown error").to_string());
85        }
86        Ok(body)
87    }
88}
89
90// --- REST deserialization types ---
91
92#[derive(Deserialize)]
93struct MetaResponse {
94    universe: Vec<AssetInfo>,
95}
96
97#[derive(Deserialize)]
98struct AssetInfo {
99    name: String,
100}
101
102type MetaAndAssetCtxsResponse = (MetaResponse, Vec<RestAssetCtx>);
103
104#[derive(Deserialize)]
105#[serde(rename_all = "camelCase")]
106#[allow(dead_code)]
107struct RestAssetCtx {
108    open_interest: String,
109    funding: String,
110    mark_px: String,
111    day_ntl_vlm: String,
112}
113
114#[derive(Deserialize)]
115#[serde(rename_all = "camelCase")]
116struct ClearinghouseStateResponse {
117    margin_summary: MarginSummary,
118    asset_positions: Vec<AssetPosition>,
119}
120
121#[derive(Deserialize)]
122#[serde(rename_all = "camelCase")]
123struct MarginSummary {
124    account_value: String,
125}
126
127#[derive(Deserialize)]
128struct AssetPosition {
129    position: PositionDetail,
130}
131
132#[derive(Deserialize)]
133#[serde(rename_all = "camelCase")]
134struct PositionDetail {
135    coin: String,
136    /// positive = long, negative = short
137    szi: String,
138    entry_px: Option<String>,
139}
140
141#[derive(Deserialize)]
142#[serde(rename_all = "camelCase")]
143struct RestOpenOrder {
144    coin: String,
145    side: String,
146    limit_px: String,
147    sz: String,
148    oid: i64,
149    orig_sz: String,
150}
151
152// --- WebSocket envelope and data shapes ---
153
154#[derive(Deserialize)]
155struct WsEnvelope {
156    channel: String,
157    data: Value,
158}
159
160#[derive(Deserialize)]
161struct WsBook {
162    coin: String,
163    levels: Vec<Vec<WsLevel>>,
164    time: i64,
165}
166
167#[derive(Deserialize)]
168struct WsLevel {
169    px: String,
170    sz: String,
171}
172
173#[derive(Deserialize)]
174#[serde(rename_all = "camelCase")]
175struct WsAssetCtx {
176    coin: String,
177    ctx: WsPerpsCtx,
178}
179
180#[derive(Deserialize)]
181#[serde(rename_all = "camelCase")]
182struct WsPerpsCtx {
183    open_interest: String,
184    funding: String,
185    mark_px: String,
186    day_ntl_vlm: String,
187}
188
189#[derive(Deserialize)]
190struct WsUserEvent {
191    liquidation: Option<WsLiquidation>,
192    fills: Option<Vec<WsUserFill>>,
193    funding: Option<WsFunding>,
194}
195
196#[derive(Deserialize)]
197struct WsLiquidation {
198    liquidated_user: String,
199    liquidated_ntl_pos: String,
200    liquidated_account_value: String,
201}
202
203#[derive(Deserialize)]
204struct WsUserFill {
205    coin: String,
206    px: String,
207    sz: String,
208    side: String,
209    time: i64,
210    oid: i64,
211    fee: String,
212}
213
214#[derive(Deserialize)]
215struct WsFunding {
216    time: i64,
217    coin: String,
218    usdc: String,
219}
220
221#[derive(Deserialize)]
222struct WsTrade {
223    coin: String,
224    side: String,
225    px: String,
226    sz: String,
227    time: i64,
228    tid: i64,
229}
230
231#[derive(Deserialize)]
232struct WsOrderUpdate {
233    order: WsOrderInfo,
234    status: String,
235    #[serde(rename = "statusTimestamp")]
236    status_timestamp: i64,
237}
238
239#[derive(Deserialize)]
240#[serde(rename_all = "camelCase")]
241struct WsOrderInfo {
242    coin: String,
243    side: String,
244    limit_px: String,
245    sz: String,
246    oid: i64,
247    orig_sz: String,
248}
249
250// --- WebSocket ledger update shapes (deposits / withdrawals) ---
251
252#[derive(Deserialize)]
253struct WsLedgerUpdates {
254    updates: Vec<WsLedgerEntry>,
255}
256
257#[derive(Deserialize)]
258struct WsLedgerEntry {
259    time: i64,
260    delta: WsLedgerDelta,
261}
262
263#[derive(Deserialize)]
264struct WsLedgerDelta {
265    #[serde(rename = "type")]
266    kind: String,
267    usdc: Option<String>,
268}
269
270// --- Helpers ---
271
272fn parse_decimal(s: &str) -> Option<Decimal> {
273    Decimal::from_str(s).ok()
274}
275
276fn keccak256(data: &[u8]) -> [u8; 32] {
277    use sha3::{Digest, Keccak256};
278    Keccak256::digest(data).into()
279}
280
281/// EIP-712 domain separator for Hyperliquid mainnet (Arbitrum, chainId=42161).
282fn hyperliquid_domain_separator() -> [u8; 32] {
283    let type_hash = keccak256(b"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
284    let name_hash = keccak256(b"Exchange");
285    let version_hash = keccak256(b"1");
286    let mut chain_id = [0u8; 32];
287    chain_id[28..32].copy_from_slice(&42161u32.to_be_bytes());
288    let verifying_contract = [0u8; 32];
289
290    let mut data = [0u8; 160];
291    data[..32].copy_from_slice(&type_hash);
292    data[32..64].copy_from_slice(&name_hash);
293    data[64..96].copy_from_slice(&version_hash);
294    data[96..128].copy_from_slice(&chain_id);
295    data[128..160].copy_from_slice(&verifying_contract);
296    keccak256(&data)
297}
298
299/// Signs a Hyperliquid exchange action using EIP-712.
300/// Returns (r, s, v) where r and s are "0x"-prefixed hex strings and v is 27 or 28.
301fn sign_action(private_key: &str, action: &Value, vault_address: Option<&str>, nonce: u64) -> Result<(String, String, u8), String> {
302    use k256::ecdsa::SigningKey;
303
304    // Step 1: msgpack-encode the action, append nonce + vault flag
305    let msgpack_bytes = rmp_serde::to_vec(action).map_err(|e| e.to_string())?;
306    let mut data = msgpack_bytes;
307    data.extend_from_slice(&nonce.to_be_bytes());
308    match vault_address {
309        None => data.push(0u8),
310        Some(addr) => {
311            data.push(1u8);
312            let addr_bytes = hex::decode(addr.trim_start_matches("0x"))
313                .map_err(|e| format!("invalid vault address: {}", e))?;
314            data.extend_from_slice(&addr_bytes);
315        }
316    }
317    let connection_id = keccak256(&data);
318
319    // Step 2: hash the Agent struct
320    let agent_type_hash = keccak256(b"Agent(string source,bytes32 connectionId)");
321    let source_hash = keccak256(b"a"); // "a" = mainnet
322    let mut struct_data = [0u8; 96];
323    struct_data[..32].copy_from_slice(&agent_type_hash);
324    struct_data[32..64].copy_from_slice(&source_hash);
325    struct_data[64..96].copy_from_slice(&connection_id);
326    let struct_hash = keccak256(&struct_data);
327
328    // Step 3: EIP-712 final hash
329    let domain_sep = hyperliquid_domain_separator();
330    let mut final_data = Vec::with_capacity(66);
331    final_data.extend_from_slice(b"\x19\x01");
332    final_data.extend_from_slice(&domain_sep);
333    final_data.extend_from_slice(&struct_hash);
334    let final_hash = keccak256(&final_data);
335
336    // Step 4: sign with secp256k1
337    let key_bytes = hex::decode(private_key.trim_start_matches("0x"))
338        .map_err(|e| format!("invalid private key: {}", e))?;
339    let signing_key = SigningKey::from_bytes(key_bytes.as_slice().into())
340        .map_err(|e| e.to_string())?;
341    let (sig, recovery_id) = signing_key.sign_prehash_recoverable(&final_hash)
342        .map_err(|e| e.to_string())?;
343
344    let sig_bytes = sig.to_bytes();
345    let r = format!("0x{}", hex::encode(&sig_bytes[..32]));
346    let s = format!("0x{}", hex::encode(&sig_bytes[32..64]));
347    let v = 27u8 + recovery_id.to_byte();
348
349    Ok((r, s, v))
350}
351
352// --- Trait implementations ---
353
354#[allow(async_fn_in_trait)]
355impl guilder_abstraction::TestServer for HyperliquidClient {
356    /// Sends a lightweight allMids request; returns true if the server responds 200 OK.
357    async fn ping(&self) -> Result<bool, String> {
358        self.client
359            .post(HYPERLIQUID_INFO_URL)
360            .json(&serde_json::json!({"type": "allMids"}))
361            .send()
362            .await
363            .map(|r| r.status().is_success())
364            .map_err(|e| e.to_string())
365    }
366
367    /// Hyperliquid has no dedicated server-time endpoint; returns local UTC ms.
368    async fn get_server_time(&self) -> Result<i64, String> {
369        Ok(std::time::SystemTime::now()
370            .duration_since(std::time::UNIX_EPOCH)
371            .map(|d| d.as_millis() as i64)
372            .unwrap_or(0))
373    }
374}
375
376#[allow(async_fn_in_trait)]
377impl guilder_abstraction::GetMarketData for HyperliquidClient {
378    /// Returns all perpetual asset names from Hyperliquid's meta endpoint.
379    async fn get_symbol(&self) -> Result<Vec<String>, String> {
380        let resp = self.client
381            .post(HYPERLIQUID_INFO_URL)
382            .json(&serde_json::json!({"type": "meta"}))
383            .send()
384            .await
385            .map_err(|e| e.to_string())?;
386        parse_response::<MetaResponse>(resp).await
387            .map(|r| r.universe.into_iter().map(|a| a.name).collect())
388    }
389
390    /// Returns the current open interest for `symbol` from metaAndAssetCtxs.
391    async fn get_open_interest(&self, symbol: String) -> Result<Decimal, String> {
392        let resp = self.client
393            .post(HYPERLIQUID_INFO_URL)
394            .json(&serde_json::json!({"type": "metaAndAssetCtxs"}))
395            .send()
396            .await
397            .map_err(|e| e.to_string())?;
398        let (meta, ctxs) = parse_response::<MetaAndAssetCtxsResponse>(resp).await?;
399        meta.universe.iter()
400            .position(|a| a.name == symbol)
401            .and_then(|i| ctxs.get(i))
402            .and_then(|ctx| parse_decimal(&ctx.open_interest))
403            .ok_or_else(|| format!("symbol {} not found", symbol))
404    }
405
406    /// Returns a full AssetContext snapshot for `symbol` from metaAndAssetCtxs.
407    async fn get_asset_context(&self, symbol: String) -> Result<AssetContext, String> {
408        let resp = self.client
409            .post(HYPERLIQUID_INFO_URL)
410            .json(&serde_json::json!({"type": "metaAndAssetCtxs"}))
411            .send()
412            .await
413            .map_err(|e| e.to_string())?;
414        let (meta, ctxs) = parse_response::<MetaAndAssetCtxsResponse>(resp).await?;
415        let idx = meta.universe.iter()
416            .position(|a| a.name == symbol)
417            .ok_or_else(|| format!("symbol {} not found", symbol))?;
418        let ctx = ctxs.get(idx).ok_or_else(|| format!("symbol {} not found", symbol))?;
419        Ok(AssetContext {
420            symbol,
421            open_interest: parse_decimal(&ctx.open_interest).ok_or("invalid open_interest")?,
422            funding_rate: parse_decimal(&ctx.funding).ok_or("invalid funding")?,
423            mark_price: parse_decimal(&ctx.mark_px).ok_or("invalid mark_px")?,
424            day_volume: parse_decimal(&ctx.day_ntl_vlm).ok_or("invalid day_ntl_vlm")?,
425        })
426    }
427
428    /// Returns the mid-price of `symbol` (e.g. "BTC") from allMids.
429    async fn get_price(&self, symbol: String) -> Result<Decimal, String> {
430        let resp = self.client
431            .post(HYPERLIQUID_INFO_URL)
432            .json(&serde_json::json!({"type": "allMids"}))
433            .send()
434            .await
435            .map_err(|e| e.to_string())?;
436        parse_response::<HashMap<String, String>>(resp).await?
437            .get(&symbol)
438            .and_then(|s| parse_decimal(s))
439            .ok_or_else(|| format!("symbol {} not found", symbol))
440    }
441}
442
443#[allow(async_fn_in_trait)]
444impl guilder_abstraction::ManageOrder for HyperliquidClient {
445    /// Places an order on Hyperliquid. Requires `with_auth`. Returns an `OrderPlacement` with
446    /// the exchange-assigned order ID. Market orders are submitted as aggressive limit orders (IOC).
447    async fn place_order(&self, symbol: String, side: OrderSide, price: Decimal, volume: Decimal, order_type: OrderType, time_in_force: TimeInForce) -> Result<OrderPlacement, String> {
448        let asset_idx = self.get_asset_index(&symbol).await?;
449        let is_buy = matches!(side, OrderSide::Buy);
450
451        let tif_str = match time_in_force {
452            TimeInForce::Gtc => "Gtc",
453            TimeInForce::Ioc => "Ioc",
454            TimeInForce::Fok => "Fok",
455        };
456        // Market orders are IOC limit orders at a wide price
457        let order_type_val = match order_type {
458            OrderType::Limit => serde_json::json!({"limit": {"tif": tif_str}}),
459            OrderType::Market => serde_json::json!({"limit": {"tif": "Ioc"}}),
460        };
461
462        let action = serde_json::json!({
463            "type": "order",
464            "orders": [{
465                "a": asset_idx,
466                "b": is_buy,
467                "p": price.to_string(),
468                "s": volume.to_string(),
469                "r": false,
470                "t": order_type_val
471            }],
472            "grouping": "na"
473        });
474
475        let resp = self.submit_signed_action(action, None).await?;
476        let oid = resp["response"]["data"]["statuses"][0]["resting"]["oid"]
477            .as_i64()
478            .or_else(|| resp["response"]["data"]["statuses"][0]["filled"]["oid"].as_i64())
479            .ok_or_else(|| format!("unexpected response: {}", resp))?;
480
481        let timestamp_ms = std::time::SystemTime::now()
482            .duration_since(std::time::UNIX_EPOCH)
483            .unwrap()
484            .as_millis() as i64;
485
486        Ok(OrderPlacement { order_id: oid, symbol, side, price, quantity: volume, timestamp_ms })
487    }
488
489    /// Modifies price and size of an existing order by its order ID. Requires `with_auth`.
490    /// Fetches the order's current coin and side before submitting the modify action.
491    async fn change_order_by_cloid(&self, cloid: i64, price: Decimal, volume: Decimal) -> Result<i64, String> {
492        let user = self.require_user_address()?;
493
494        let resp = self.client
495            .post(HYPERLIQUID_INFO_URL)
496            .json(&serde_json::json!({"type": "openOrders", "user": user}))
497            .send()
498            .await
499            .map_err(|e| e.to_string())?;
500        let orders: Vec<RestOpenOrder> = parse_response(resp).await?;
501        let order = orders.iter()
502            .find(|o| o.oid == cloid)
503            .ok_or_else(|| format!("order {} not found", cloid))?;
504
505        let asset_idx = self.get_asset_index(&order.coin).await?;
506        let is_buy = order.side == "B";
507
508        let action = serde_json::json!({
509            "type": "batchModify",
510            "modifies": [{
511                "oid": cloid,
512                "order": {
513                    "a": asset_idx,
514                    "b": is_buy,
515                    "p": price.to_string(),
516                    "s": volume.to_string(),
517                    "r": false,
518                    "t": {"limit": {"tif": "Gtc"}}
519                }
520            }]
521        });
522
523        self.submit_signed_action(action, None).await?;
524        Ok(cloid)
525    }
526
527    /// Cancels a single order by its order ID. Requires `with_auth`.
528    /// Fetches open orders to resolve the coin/asset before cancelling.
529    async fn cancel_order(&self, cloid: i64) -> Result<i64, String> {
530        let user = self.require_user_address()?;
531
532        let resp = self.client
533            .post(HYPERLIQUID_INFO_URL)
534            .json(&serde_json::json!({"type": "openOrders", "user": user}))
535            .send()
536            .await
537            .map_err(|e| e.to_string())?;
538        let orders: Vec<RestOpenOrder> = parse_response(resp).await?;
539        let order = orders.iter()
540            .find(|o| o.oid == cloid)
541            .ok_or_else(|| format!("order {} not found", cloid))?;
542
543        let asset_idx = self.get_asset_index(&order.coin).await?;
544        let action = serde_json::json!({
545            "type": "cancel",
546            "cancels": [{"a": asset_idx, "o": cloid}]
547        });
548
549        self.submit_signed_action(action, None).await?;
550        Ok(cloid)
551    }
552
553    /// Cancels all open orders. Requires `with_auth`.
554    /// Fetches all open orders and submits a batch cancel in a single signed request.
555    async fn cancel_all_order(&self) -> Result<bool, String> {
556        let user = self.require_user_address()?;
557
558        let resp = self.client
559            .post(HYPERLIQUID_INFO_URL)
560            .json(&serde_json::json!({"type": "openOrders", "user": user}))
561            .send()
562            .await
563            .map_err(|e| e.to_string())?;
564        let orders: Vec<RestOpenOrder> = parse_response(resp).await?;
565        if orders.is_empty() {
566            return Ok(true);
567        }
568
569        let meta_resp = self.client
570            .post(HYPERLIQUID_INFO_URL)
571            .json(&serde_json::json!({"type": "meta"}))
572            .send()
573            .await
574            .map_err(|e| e.to_string())?;
575        let meta: MetaResponse = parse_response(meta_resp).await?;
576
577        let cancels: Vec<Value> = orders.iter()
578            .filter_map(|o| {
579                let asset_idx = meta.universe.iter().position(|a| a.name == o.coin)?;
580                Some(serde_json::json!({"a": asset_idx, "o": o.oid}))
581            })
582            .collect();
583
584        let action = serde_json::json!({"type": "cancel", "cancels": cancels});
585        self.submit_signed_action(action, None).await?;
586        Ok(true)
587    }
588}
589
590#[allow(async_fn_in_trait)]
591impl guilder_abstraction::SubscribeMarketData for HyperliquidClient {
592    /// Streams L2 orderbook updates for `symbol`. Each message from Hyperliquid is a
593    /// full-depth snapshot; every level is emitted as an individual `L2Update` event.
594    /// All levels in the same snapshot share the same `sequence` value.
595    fn subscribe_l2_update(&self, symbol: String) -> BoxStream<L2Update> {
596        Box::pin(async_stream::stream! {
597            let Ok((mut ws, _)) = connect_async(HYPERLIQUID_WS_URL).await else { return; };
598            let sub = serde_json::json!({
599                "method": "subscribe",
600                "subscription": {"type": "l2Book", "coin": symbol}
601            });
602            if ws.send(Message::Text(sub.to_string().into())).await.is_err() { return; }
603
604            while let Some(Ok(Message::Text(text))) = ws.next().await {
605                let Ok(env) = serde_json::from_str::<WsEnvelope>(&text) else { continue; };
606                if env.channel != "l2Book" { continue; }
607                let Ok(book) = serde_json::from_value::<WsBook>(env.data) else { continue; };
608
609                for level in book.levels.first().into_iter().flatten() {
610                    if let (Some(price), Some(volume)) = (parse_decimal(&level.px), parse_decimal(&level.sz)) {
611                        yield L2Update { symbol: book.coin.clone(), price, volume, side: Side::Ask, sequence: book.time };
612                    }
613                }
614                for level in book.levels.get(1).into_iter().flatten() {
615                    if let (Some(price), Some(volume)) = (parse_decimal(&level.px), parse_decimal(&level.sz)) {
616                        yield L2Update { symbol: book.coin.clone(), price, volume, side: Side::Bid, sequence: book.time };
617                    }
618                }
619            }
620        })
621    }
622
623    /// Streams asset context updates for `symbol` via Hyperliquid's `activeAssetCtx` subscription.
624    fn subscribe_asset_context(&self, symbol: String) -> BoxStream<AssetContext> {
625        Box::pin(async_stream::stream! {
626            let Ok((mut ws, _)) = connect_async(HYPERLIQUID_WS_URL).await else { return; };
627            let sub = serde_json::json!({
628                "method": "subscribe",
629                "subscription": {"type": "activeAssetCtx", "coin": symbol}
630            });
631            if ws.send(Message::Text(sub.to_string().into())).await.is_err() { return; }
632
633            while let Some(Ok(Message::Text(text))) = ws.next().await {
634                let Ok(env) = serde_json::from_str::<WsEnvelope>(&text) else { continue; };
635                if env.channel != "activeAssetCtx" { continue; }
636                let Ok(update) = serde_json::from_value::<WsAssetCtx>(env.data) else { continue; };
637                let ctx = &update.ctx;
638                if let (Some(open_interest), Some(funding_rate), Some(mark_price), Some(day_volume)) = (
639                    parse_decimal(&ctx.open_interest),
640                    parse_decimal(&ctx.funding),
641                    parse_decimal(&ctx.mark_px),
642                    parse_decimal(&ctx.day_ntl_vlm),
643                ) {
644                    yield AssetContext { symbol: update.coin, open_interest, funding_rate, mark_price, day_volume };
645                }
646            }
647        })
648    }
649
650    /// Streams liquidation events for a user address via Hyperliquid's `userEvents` subscription.
651    /// Hyperliquid's liquidation event is account-level; `symbol` is empty and `side` is `Sell`.
652    fn subscribe_liquidation(&self, user: String) -> BoxStream<Liquidation> {
653        Box::pin(async_stream::stream! {
654            let Ok((mut ws, _)) = connect_async(HYPERLIQUID_WS_URL).await else { return; };
655            let sub = serde_json::json!({
656                "method": "subscribe",
657                "subscription": {"type": "userEvents", "user": user}
658            });
659            if ws.send(Message::Text(sub.to_string().into())).await.is_err() { return; }
660
661            while let Some(Ok(Message::Text(text))) = ws.next().await {
662                let Ok(env) = serde_json::from_str::<WsEnvelope>(&text) else { continue; };
663                if env.channel != "userEvents" { continue; }
664                let Ok(event) = serde_json::from_value::<WsUserEvent>(env.data) else { continue; };
665                let Some(liq) = event.liquidation else { continue; };
666                if let (Some(notional_position), Some(account_value)) = (
667                    parse_decimal(&liq.liquidated_ntl_pos),
668                    parse_decimal(&liq.liquidated_account_value),
669                ) {
670                    yield Liquidation {
671                        symbol: String::new(),
672                        side: OrderSide::Sell,
673                        liquidated_user: liq.liquidated_user,
674                        notional_position,
675                        account_value,
676                    };
677                }
678            }
679        })
680    }
681
682    /// Streams public trade fills for `symbol`. "B" (buyer aggressor) → Buy, otherwise → Sell.
683    fn subscribe_fill(&self, symbol: String) -> BoxStream<Fill> {
684        Box::pin(async_stream::stream! {
685            let Ok((mut ws, _)) = connect_async(HYPERLIQUID_WS_URL).await else { return; };
686            let sub = serde_json::json!({
687                "method": "subscribe",
688                "subscription": {"type": "trades", "coin": symbol}
689            });
690            if ws.send(Message::Text(sub.to_string().into())).await.is_err() { return; }
691
692            while let Some(Ok(Message::Text(text))) = ws.next().await {
693                let Ok(env) = serde_json::from_str::<WsEnvelope>(&text) else { continue; };
694                if env.channel != "trades" { continue; }
695                let Ok(trades) = serde_json::from_value::<Vec<WsTrade>>(env.data) else { continue; };
696
697                for trade in trades {
698                    let side = if trade.side == "B" { OrderSide::Buy } else { OrderSide::Sell };
699                    if let (Some(price), Some(volume)) = (parse_decimal(&trade.px), parse_decimal(&trade.sz)) {
700                        yield Fill { symbol: trade.coin, price, volume, side, timestamp_ms: trade.time, trade_id: trade.tid };
701                    }
702                }
703            }
704        })
705    }
706}
707
708#[allow(async_fn_in_trait)]
709impl guilder_abstraction::GetAccountSnapshot for HyperliquidClient {
710    /// Returns open positions from `clearinghouseState`. Requires `with_auth`.
711    /// Zero-size positions are filtered out. Positive `szi` = long, negative = short.
712    async fn get_positions(&self) -> Result<Vec<Position>, String> {
713        let user = self.require_user_address()?;
714        let resp = self.client
715            .post(HYPERLIQUID_INFO_URL)
716            .json(&serde_json::json!({"type": "clearinghouseState", "user": user}))
717            .send()
718            .await
719            .map_err(|e| e.to_string())?;
720        let state: ClearinghouseStateResponse = parse_response(resp).await?;
721
722        Ok(state.asset_positions.into_iter()
723            .filter_map(|ap| {
724                let p = ap.position;
725                let size = parse_decimal(&p.szi)?;
726                if size.is_zero() { return None; }
727                let entry_price = p.entry_px.as_deref().and_then(parse_decimal).unwrap_or_default();
728                let side = if size > Decimal::ZERO { OrderSide::Buy } else { OrderSide::Sell };
729                Some(Position { symbol: p.coin, side, size: size.abs(), entry_price })
730            })
731            .collect())
732    }
733
734    /// Returns resting orders from Hyperliquid's `openOrders` endpoint. Requires `with_auth`.
735    /// `filled_quantity` is derived as `origSz - sz` (original size minus remaining size).
736    async fn get_open_orders(&self) -> Result<Vec<OpenOrder>, String> {
737        let user = self.require_user_address()?;
738        let resp = self.client
739            .post(HYPERLIQUID_INFO_URL)
740            .json(&serde_json::json!({"type": "openOrders", "user": user}))
741            .send()
742            .await
743            .map_err(|e| e.to_string())?;
744        let orders: Vec<RestOpenOrder> = parse_response(resp).await?;
745
746        Ok(orders.into_iter()
747            .filter_map(|o| {
748                let price = parse_decimal(&o.limit_px)?;
749                let quantity = parse_decimal(&o.orig_sz)?;
750                let remaining = parse_decimal(&o.sz)?;
751                let filled_quantity = quantity - remaining;
752                let side = if o.side == "B" { OrderSide::Buy } else { OrderSide::Sell };
753                Some(OpenOrder { order_id: o.oid, symbol: o.coin, side, price, quantity, filled_quantity })
754            })
755            .collect())
756    }
757
758    /// Returns total account value (collateral) from `clearinghouseState`. Requires `with_auth`.
759    async fn get_collateral(&self) -> Result<Decimal, String> {
760        let user = self.require_user_address()?;
761        let resp = self.client
762            .post(HYPERLIQUID_INFO_URL)
763            .json(&serde_json::json!({"type": "clearinghouseState", "user": user}))
764            .send()
765            .await
766            .map_err(|e| e.to_string())?;
767        let state: ClearinghouseStateResponse = parse_response(resp).await?;
768        parse_decimal(&state.margin_summary.account_value)
769            .ok_or_else(|| "invalid account value".to_string())
770    }
771}
772
773#[allow(async_fn_in_trait)]
774impl guilder_abstraction::SubscribeUserEvents for HyperliquidClient {
775    /// Streams the user's own order executions via the `userEvents` WS subscription.
776    /// Requires `with_auth` (streams are empty if no user address is set).
777    fn subscribe_user_fills(&self) -> BoxStream<UserFill> {
778        let Some(addr) = self.user_address else { return Box::pin(stream::empty()); };
779        let user = format!("{:#x}", addr);
780        Box::pin(async_stream::stream! {
781            let Ok((mut ws, _)) = connect_async(HYPERLIQUID_WS_URL).await else { return; };
782            let sub = serde_json::json!({
783                "method": "subscribe",
784                "subscription": {"type": "userEvents", "user": user}
785            });
786            if ws.send(Message::Text(sub.to_string().into())).await.is_err() { return; }
787
788            while let Some(Ok(Message::Text(text))) = ws.next().await {
789                let Ok(env) = serde_json::from_str::<WsEnvelope>(&text) else { continue; };
790                if env.channel != "userEvents" { continue; }
791                let Ok(event) = serde_json::from_value::<WsUserEvent>(env.data) else { continue; };
792                for fill in event.fills.unwrap_or_default() {
793                    let side = if fill.side == "B" { OrderSide::Buy } else { OrderSide::Sell };
794                    if let (Some(price), Some(quantity), Some(fee_usd)) = (
795                        parse_decimal(&fill.px),
796                        parse_decimal(&fill.sz),
797                        parse_decimal(&fill.fee),
798                    ) {
799                        yield UserFill { order_id: fill.oid, symbol: fill.coin, side, price, quantity, fee_usd, timestamp_ms: fill.time };
800                    }
801                }
802            }
803        })
804    }
805
806    /// Streams order lifecycle events via the `orderUpdates` WS subscription.
807    /// Requires `with_auth`.
808    fn subscribe_order_updates(&self) -> BoxStream<OrderUpdate> {
809        let Some(addr) = self.user_address else { return Box::pin(stream::empty()); };
810        let user = format!("{:#x}", addr);
811        Box::pin(async_stream::stream! {
812            let Ok((mut ws, _)) = connect_async(HYPERLIQUID_WS_URL).await else { return; };
813            let sub = serde_json::json!({
814                "method": "subscribe",
815                "subscription": {"type": "orderUpdates", "user": user}
816            });
817            if ws.send(Message::Text(sub.to_string().into())).await.is_err() { return; }
818
819            while let Some(Ok(Message::Text(text))) = ws.next().await {
820                let Ok(env) = serde_json::from_str::<WsEnvelope>(&text) else { continue; };
821                if env.channel != "orderUpdates" { continue; }
822                let Ok(updates) = serde_json::from_value::<Vec<WsOrderUpdate>>(env.data) else { continue; };
823                for upd in updates {
824                    let status = match upd.status.as_str() {
825                        "open" => OrderStatus::Placed,
826                        "filled" => OrderStatus::Filled,
827                        "canceled" | "cancelled" => OrderStatus::Cancelled,
828                        _ => OrderStatus::PartiallyFilled,
829                    };
830                    let side = if upd.order.side == "B" { OrderSide::Buy } else { OrderSide::Sell };
831                    yield OrderUpdate {
832                        order_id: upd.order.oid,
833                        symbol: upd.order.coin,
834                        status,
835                        side: Some(side),
836                        price: parse_decimal(&upd.order.limit_px),
837                        quantity: parse_decimal(&upd.order.orig_sz),
838                        remaining_quantity: parse_decimal(&upd.order.sz),
839                        timestamp_ms: upd.status_timestamp,
840                    };
841                }
842            }
843        })
844    }
845
846    /// Streams funding payments applied to positions via the `userEvents` WS subscription.
847    /// Requires `with_auth`.
848    fn subscribe_funding_payments(&self) -> BoxStream<FundingPayment> {
849        let Some(addr) = self.user_address else { return Box::pin(stream::empty()); };
850        let user = format!("{:#x}", addr);
851        Box::pin(async_stream::stream! {
852            let Ok((mut ws, _)) = connect_async(HYPERLIQUID_WS_URL).await else { return; };
853            let sub = serde_json::json!({
854                "method": "subscribe",
855                "subscription": {"type": "userEvents", "user": user}
856            });
857            if ws.send(Message::Text(sub.to_string().into())).await.is_err() { return; }
858
859            while let Some(Ok(Message::Text(text))) = ws.next().await {
860                let Ok(env) = serde_json::from_str::<WsEnvelope>(&text) else { continue; };
861                if env.channel != "userEvents" { continue; }
862                let Ok(event) = serde_json::from_value::<WsUserEvent>(env.data) else { continue; };
863                let Some(funding) = event.funding else { continue; };
864                if let Some(amount_usd) = parse_decimal(&funding.usdc) {
865                    yield FundingPayment { symbol: funding.coin, amount_usd, timestamp_ms: funding.time };
866                }
867            }
868        })
869    }
870
871    fn subscribe_deposits(&self) -> BoxStream<Deposit> {
872        let Some(addr) = self.user_address else { return Box::pin(stream::empty()); };
873        let user = format!("{:#x}", addr);
874        Box::pin(async_stream::stream! {
875            let Ok((mut ws, _)) = connect_async(HYPERLIQUID_WS_URL).await else { return; };
876            let sub = serde_json::json!({
877                "method": "subscribe",
878                "subscription": {"type": "userNonFundingLedgerUpdates", "user": user}
879            });
880            if ws.send(Message::Text(sub.to_string().into())).await.is_err() { return; }
881            while let Some(Ok(Message::Text(text))) = ws.next().await {
882                let Ok(env) = serde_json::from_str::<WsEnvelope>(&text) else { continue; };
883                if env.channel != "userNonFundingLedgerUpdates" { continue; }
884                let Ok(ledger) = serde_json::from_value::<WsLedgerUpdates>(env.data) else { continue; };
885                for entry in ledger.updates {
886                    if entry.delta.kind == "deposit" {
887                        if let Some(amount_usd) = entry.delta.usdc.as_deref().and_then(parse_decimal) {
888                            yield Deposit { asset: "USDC".to_string(), amount_usd, timestamp_ms: entry.time };
889                        }
890                    }
891                }
892            }
893        })
894    }
895
896    fn subscribe_withdrawals(&self) -> BoxStream<Withdrawal> {
897        let Some(addr) = self.user_address else { return Box::pin(stream::empty()); };
898        let user = format!("{:#x}", addr);
899        Box::pin(async_stream::stream! {
900            let Ok((mut ws, _)) = connect_async(HYPERLIQUID_WS_URL).await else { return; };
901            let sub = serde_json::json!({
902                "method": "subscribe",
903                "subscription": {"type": "userNonFundingLedgerUpdates", "user": user}
904            });
905            if ws.send(Message::Text(sub.to_string().into())).await.is_err() { return; }
906            while let Some(Ok(Message::Text(text))) = ws.next().await {
907                let Ok(env) = serde_json::from_str::<WsEnvelope>(&text) else { continue; };
908                if env.channel != "userNonFundingLedgerUpdates" { continue; }
909                let Ok(ledger) = serde_json::from_value::<WsLedgerUpdates>(env.data) else { continue; };
910                for entry in ledger.updates {
911                    if entry.delta.kind == "withdraw" {
912                        if let Some(amount_usd) = entry.delta.usdc.as_deref().and_then(parse_decimal) {
913                            yield Withdrawal { asset: "USDC".to_string(), amount_usd, timestamp_ms: entry.time };
914                        }
915                    }
916                }
917            }
918        })
919    }
920}