df_hyperliquid_core/deploy/perp/asset.rs
1//! What registering a HIP-3 perp carries: the asset, its margin mode, and the new dex's schema when
2//! the first asset creates the dex.
3
4use crate::utils::sign::{Map, Value};
5use crate::utils::{Address, Dec};
6
7/// How an asset may be margined.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[non_exhaustive]
10pub enum MarginMode {
11 /// `strictIsolated`: isolated only, and isolated margin cannot be taken
12 /// out of an open position.
13 StrictIsolated,
14 /// `noCross`: isolated only.
15 NoCross,
16 /// `normal`: cross or isolated.
17 Normal,
18}
19
20impl MarginMode {
21 /// The name sent.
22 #[must_use]
23 pub const fn wire(self) -> &'static str {
24 match self {
25 MarginMode::StrictIsolated => "strictIsolated",
26 MarginMode::NoCross => "noCross",
27 MarginMode::Normal => "normal",
28 }
29 }
30}
31
32/// A new asset's listing: its coin (`dex:NAME`), size decimals, starting
33/// oracle price and margin table.
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct NewAsset {
36 /// The coin, `dex:NAME`.
37 pub coin: String,
38 /// Size decimals (`szDecimals`): how many decimal places an order size may have.
39 pub sz_decimals: u32,
40 /// The starting oracle price (`oraclePx`).
41 pub oracle_px: Dec,
42 /// The id of the margin table the asset uses (`marginTableId`).
43 pub margin_table_id: u32,
44}
45
46impl NewAsset {
47 /// `{"coin","szDecimals","oraclePx","marginTableId"}`, the margin key
48 /// for the caller to add last.
49 #[must_use]
50 pub fn wire(&self) -> Map {
51 Map::new()
52 .put("coin", self.coin.clone())
53 .put("szDecimals", self.sz_decimals)
54 .put("oraclePx", self.oracle_px.to_wire())
55 .put("marginTableId", self.margin_table_id)
56 }
57}
58
59/// A new dex, registered with its first asset: its full name, the spot
60/// token it is collateralised in, and who updates its oracle (`None` for the
61/// deployer).
62#[derive(Debug, Clone, PartialEq, Eq)]
63pub struct DexSchema {
64 /// The dex's full name (`fullName`).
65 pub full_name: String,
66 /// The spot token index of the collateral (`collateralToken`).
67 pub collateral_token: u32,
68 /// The address that updates the oracle (`oracleUpdater`); `None`, sent as null, for the deployer.
69 pub oracle_updater: Option<Address>,
70}
71
72impl DexSchema {
73 /// `{"fullName","collateralToken","oracleUpdater"}`; no oracle updater is
74 /// sent as null.
75 #[must_use]
76 pub fn wire(&self) -> Map {
77 Map::new()
78 .put("fullName", self.full_name.clone())
79 .put("collateralToken", self.collateral_token)
80 .put("oracleUpdater", self.oracle_updater.map_or(Value::Nil, |updater| updater.to_hex().into()))
81 }
82}