Skip to main content

hyper_exchange/
types.rs

1use serde::{Deserialize, Serialize};
2
3/// Wire format for an order sent to the Hyperliquid exchange.
4#[derive(Debug, Clone, Serialize, Deserialize)]
5#[serde(rename_all = "camelCase")]
6pub struct OrderWire {
7    /// Asset index (perp index or spot index with offset).
8    pub asset: u32,
9    /// Whether this is a buy order.
10    pub is_buy: bool,
11    /// Limit price as a decimal string.
12    pub limit_px: String,
13    /// Size as a decimal string.
14    pub sz: String,
15    /// Whether the order is reduce-only.
16    pub reduce_only: bool,
17    /// Order type wire format.
18    pub order_type: OrderTypeWire,
19    /// Optional client order ID.
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub cloid: Option<String>,
22}
23
24/// Wire format for order type.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct OrderTypeWire {
27    /// Limit order with time-in-force, or trigger order.
28    pub limit: Option<LimitOrderType>,
29    pub trigger: Option<TriggerOrderType>,
30}
31
32/// Limit order type wire format.
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct LimitOrderType {
35    pub tif: String,
36}
37
38/// Trigger order type wire format.
39#[derive(Debug, Clone, Serialize, Deserialize)]
40#[serde(rename_all = "camelCase")]
41pub struct TriggerOrderType {
42    pub trigger_px: String,
43    pub is_market: bool,
44    pub tpsl: String,
45}
46
47/// ECDSA signature split into r, s, v components (hex-encoded).
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct Signature {
50    /// r component as 0x-prefixed hex string.
51    pub r: String,
52    /// s component as 0x-prefixed hex string.
53    pub s: String,
54    /// Recovery id (27 or 28).
55    pub v: u8,
56}
57
58/// Errors that can occur in exchange operations.
59#[derive(Debug, thiserror::Error)]
60pub enum ExchangeError {
61    #[error("Signing error: {0}")]
62    SigningError(String),
63    #[error("Serialization error: {0}")]
64    SerializationError(String),
65    #[error("HTTP error: {0}")]
66    HttpError(String),
67    #[error("API error: {0}")]
68    ApiError(String),
69    #[error("Invalid address: {0}")]
70    InvalidAddress(String),
71}