df_hyperliquid_core/deploy/perp/
margin.rs1use crate::deploy::error::BadDeploy;
5use crate::utils::sign::{Map, Value};
6
7pub const MAX_LEVERAGE: u32 = 50;
9pub const MAX_TIERS: usize = 3;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct MarginTier {
16 pub lower_bound: u64,
18 pub max_leverage: u32,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct MarginTable {
26 description: String,
27 tiers: Vec<MarginTier>,
28}
29
30impl MarginTable {
31 pub fn new(description: impl Into<String>, tiers: Vec<MarginTier>) -> Result<Self, BadDeploy> {
34 let in_range = tiers.iter().all(|tier| (1..=MAX_LEVERAGE).contains(&tier.max_leverage));
35 let ordered = tiers
36 .windows(2)
37 .all(|two| two[0].lower_bound < two[1].lower_bound && two[0].max_leverage > two[1].max_leverage);
38 if tiers.is_empty() || tiers.len() > MAX_TIERS || !in_range || !ordered {
39 return Err(BadDeploy::MarginTiers);
40 }
41 Ok(MarginTable { description: description.into(), tiers })
42 }
43
44 #[must_use]
46 pub fn description(&self) -> &str {
47 &self.description
48 }
49
50 #[must_use]
52 pub fn tiers(&self) -> &[MarginTier] {
53 &self.tiers
54 }
55
56 #[must_use]
58 pub fn wire(&self) -> Map {
59 let tiers = self
60 .tiers
61 .iter()
62 .map(|tier| Map::new().put("lowerBound", tier.lower_bound).put("maxLeverage", tier.max_leverage));
63 Map::new().put("description", self.description.clone()).put("marginTiers", Value::array(tiers))
64 }
65}