Skip to main content

hypercall/hypercore/
types.rs

1use serde::{Deserialize, Serialize};
2
3/// Full account state from Hyperliquid clearinghouse.
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct HypercoreAccountState {
6    pub account_value: f64,
7    pub total_margin_used: f64,
8    pub withdrawable: f64,
9    pub perp_positions: Vec<PerpPosition>,
10    pub last_updated: u64,
11}
12
13impl Default for HypercoreAccountState {
14    fn default() -> Self {
15        Self {
16            account_value: 0.0,
17            total_margin_used: 0.0,
18            withdrawable: 0.0,
19            perp_positions: Vec::new(),
20            last_updated: 0,
21        }
22    }
23}
24
25/// Single perp position from Hyperliquid clearinghouse.
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct PerpPosition {
28    pub coin: String,
29    pub size: f64,
30    pub entry_price: Option<f64>,
31    pub position_value: f64,
32    pub unrealized_pnl: f64,
33    pub margin_used: f64,
34    pub liquidation_price: Option<f64>,
35}
36
37// API request/response types for Hyperliquid API
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct ClearinghouseRequest {
41    #[serde(rename = "type")]
42    pub request_type: String,
43    pub user: String,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase")]
48pub struct MarginSummary {
49    pub account_value: String,
50    pub total_ntl_pos: String,
51    pub total_raw_usd: String,
52    pub total_margin_used: String,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56#[serde(rename_all = "camelCase")]
57pub struct AssetPosition {
58    pub position: Position,
59    #[serde(rename = "type")]
60    pub position_type: String,
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize)]
64#[serde(rename_all = "camelCase")]
65pub struct Position {
66    pub coin: String,
67    pub szi: String,
68    pub leverage: Leverage,
69    pub entry_px: Option<String>,
70    pub position_value: String,
71    pub unrealized_pnl: String,
72    pub return_on_equity: String,
73    pub liquidation_px: Option<String>,
74    pub margin_used: String,
75    #[serde(default)]
76    pub max_trade_szs: Vec<String>,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
80#[serde(rename_all = "camelCase")]
81pub struct Leverage {
82    #[serde(rename = "type")]
83    pub leverage_type: String,
84    pub value: i32,
85    #[serde(default)]
86    pub raw_usd: Option<String>,
87}
88
89#[derive(Debug, Clone, Serialize, Deserialize)]
90#[serde(rename_all = "camelCase")]
91pub struct ClearinghouseState {
92    pub margin_summary: MarginSummary,
93    pub cross_margin_summary: MarginSummary,
94    pub cross_maintenance_margin_used: String,
95    pub withdrawable: String,
96    pub asset_positions: Vec<AssetPosition>,
97    pub time: u64,
98}