Skip to main content

df_hyperliquid_core/earn/
validator.rs

1//! Running a validator: registering it, changing its profile and unregistering it
2//! (`CValidatorAction`), and its signer jailing or unjailing itself (`CSignerAction`). Both are L1
3//! actions. Neither is in the API docs; the shapes are the ones `@nktkas/hyperliquid` and
4//! Hyperliquid's Python SDK both send. Keys are snake case, as the venue takes them.
5
6use std::net::IpAddr;
7
8use crate::utils::Address;
9use crate::utils::sign::{L1Action, Map, Value};
10
11/// `{"Ip":..}`, the node address as sent. An IPv6 address is written in its
12/// short form, `2001:db8::1`.
13fn node_ip(ip: IpAddr) -> Map {
14    Map::new().put("Ip", ip.to_string())
15}
16
17/// A new validator's profile.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct ValidatorProfile {
20    /// The validator node's IP address.
21    pub node_ip: IpAddr,
22    /// The validator's name.
23    pub name: String,
24    /// The validator's description.
25    pub description: String,
26    /// Whether the validator refuses delegations.
27    pub delegations_disabled: bool,
28    /// The commission, as the integer the venue takes.
29    pub commission_bps: u64,
30    /// The address that signs for the validator node.
31    pub signer: Address,
32}
33
34impl ValidatorProfile {
35    /// `{"node_ip","name","description","delegations_disabled","commission_bps","signer"}`.
36    #[must_use]
37    pub fn wire(&self) -> Map {
38        Map::new()
39            .put("node_ip", node_ip(self.node_ip))
40            .put("name", self.name.clone())
41            .put("description", self.description.clone())
42            .put("delegations_disabled", self.delegations_disabled)
43            .put("commission_bps", self.commission_bps)
44            .put("signer", self.signer.to_hex())
45    }
46}
47
48/// A change to a validator's profile. Every `None` is sent as null and
49/// leaves that field as it is; `unjailed` is always sent.
50#[derive(Debug, Clone, PartialEq, Eq, Default)]
51pub struct ProfileChange {
52    /// The new node IP address (`node_ip`, sent as `{"Ip":..}`).
53    pub node_ip: Option<IpAddr>,
54    /// The new name.
55    pub name: Option<String>,
56    /// The new description.
57    pub description: Option<String>,
58    /// Whether the validator is unjailed after the change.
59    pub unjailed: bool,
60    /// Whether the validator refuses delegations (`disable_delegations`).
61    pub disable_delegations: Option<bool>,
62    /// The new commission (`commission_bps`), as the integer the venue takes.
63    pub commission_bps: Option<u64>,
64    /// The new address that signs for the validator node.
65    pub signer: Option<Address>,
66}
67
68impl ProfileChange {
69    /// `{"node_ip","name","description","unjailed","disable_delegations","commission_bps","signer"}`.
70    #[must_use]
71    pub fn wire(&self) -> Map {
72        fn or_nil<T>(value: Option<T>, wire: impl FnOnce(T) -> Value) -> Value {
73            value.map_or(Value::Nil, wire)
74        }
75        Map::new()
76            .put("node_ip", or_nil(self.node_ip, |ip| node_ip(ip).into()))
77            .put("name", or_nil(self.name.clone(), Value::from))
78            .put("description", or_nil(self.description.clone(), Value::from))
79            .put("unjailed", self.unjailed)
80            .put("disable_delegations", or_nil(self.disable_delegations, Value::from))
81            .put("commission_bps", or_nil(self.commission_bps, Value::from))
82            .put("signer", or_nil(self.signer, |signer| signer.to_hex().into()))
83    }
84}
85
86/// `{"type":"CValidatorAction",<variant>:..}`, sent by the validator's own
87/// key.
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub enum CValidatorAction {
90    /// `register`: a new validator with its profile, whether it starts
91    /// unjailed, and its initial self-stake in HYPE wei (8 decimals, as
92    /// [`hype_wei`](crate::earn::staking::hype_wei) converts).
93    Register {
94        /// The new validator's profile.
95        profile: ValidatorProfile,
96        /// Whether the validator starts unjailed.
97        unjailed: bool,
98        /// The initial self-stake (`initial_wei`), in HYPE wei: 8 decimals.
99        initial_wei: u64,
100    },
101    /// `changeProfile`.
102    ChangeProfile(ProfileChange),
103    /// `unregister`: the key is sent, with null.
104    Unregister,
105}
106
107impl L1Action for CValidatorAction {
108    fn action(&self) -> Map {
109        let map = Map::new().put("type", "CValidatorAction");
110        match self {
111            CValidatorAction::Register { profile, unjailed, initial_wei } => map.put(
112                "register",
113                Map::new().put("profile", profile.wire()).put("unjailed", *unjailed).put("initial_wei", *initial_wei),
114            ),
115            CValidatorAction::ChangeProfile(change) => map.put("changeProfile", change.wire()),
116            CValidatorAction::Unregister => map.put("unregister", Value::Nil),
117        }
118    }
119}
120
121/// `{"type":"CSignerAction",<variant>:null}`, sent by a validator's signer
122/// key.
123#[derive(Debug, Clone, Copy, PartialEq, Eq)]
124pub enum CSignerAction {
125    /// `jailSelf`.
126    JailSelf,
127    /// `unjailSelf`.
128    UnjailSelf,
129}
130
131impl L1Action for CSignerAction {
132    fn action(&self) -> Map {
133        let variant = match self {
134            CSignerAction::JailSelf => "jailSelf",
135            CSignerAction::UnjailSelf => "unjailSelf",
136        };
137        Map::new().put("type", "CSignerAction").put(variant, Value::Nil)
138    }
139}
140
141#[cfg(test)]
142#[path = "tests/validator.rs"]
143mod tests;