hyperliquid_sdk_rs/types/
responses.rs

1use serde::Deserialize;
2
3// ==================== Order Status Types ====================
4
5#[derive(Debug, Clone, Deserialize)]
6pub struct RestingOrder {
7    pub oid: u64,
8}
9
10#[derive(Debug, Clone, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct FilledOrder {
13    pub total_sz: String,
14    pub avg_px: String,
15    pub oid: u64,
16}
17
18#[derive(Debug, Clone, Deserialize)]
19#[serde(rename_all = "camelCase")]
20pub enum ExchangeDataStatus {
21    Success,
22    WaitingForFill,
23    WaitingForTrigger,
24    Error(String),
25    Resting(RestingOrder),
26    Filled(FilledOrder),
27}
28
29// ==================== Exchange Response Types ====================
30
31#[derive(Debug, Clone, Deserialize)]
32pub struct ExchangeDataStatuses {
33    pub statuses: Vec<ExchangeDataStatus>,
34}
35
36#[derive(Debug, Clone, Deserialize)]
37pub struct ExchangeResponse {
38    #[serde(rename = "type")]
39    pub response_type: String,
40    pub data: Option<ExchangeDataStatuses>,
41}
42
43#[derive(Debug, Clone, Deserialize)]
44#[serde(rename_all = "camelCase")]
45#[serde(tag = "status", content = "response")]
46pub enum ExchangeResponseStatus {
47    Ok(ExchangeResponse),
48    Err(String),
49}
50
51// ==================== Convenience Methods ====================
52
53impl ExchangeResponseStatus {
54    /// Check if the response was successful
55    pub fn is_ok(&self) -> bool {
56        matches!(self, Self::Ok(_))
57    }
58
59    /// Get the error message if this was an error response
60    pub fn error(&self) -> Option<&str> {
61        match self {
62            Self::Err(msg) => Some(msg),
63            _ => None,
64        }
65    }
66
67    /// Get the inner response if successful
68    pub fn into_result(self) -> Result<ExchangeResponse, String> {
69        match self {
70            Self::Ok(response) => Ok(response),
71            Self::Err(msg) => Err(msg),
72        }
73    }
74}
75
76impl ExchangeDataStatus {
77    /// Check if this status represents a successful order
78    pub fn is_success(&self) -> bool {
79        matches!(self, Self::Success | Self::Resting(_) | Self::Filled(_))
80    }
81
82    /// Get order ID if available
83    pub fn order_id(&self) -> Option<u64> {
84        match self {
85            Self::Resting(order) => Some(order.oid),
86            Self::Filled(order) => Some(order.oid),
87            _ => None,
88        }
89    }
90}