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`, but the node refuses it (see [`ValidatorProfile::node_ip`]).
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. Give an IPv4 address: Hyperliquid's node refuses the
21 /// action with `422 Failed to deserialize` when this is IPv6, including an IPv4-mapped one
22 /// (checked on testnet 2026-11-24 by `just testnet`). The type stays `IpAddr` until 0.2.
23 pub node_ip: IpAddr,
24 /// The validator's name.
25 pub name: String,
26 /// The validator's description.
27 pub description: String,
28 /// Whether the validator refuses delegations.
29 pub delegations_disabled: bool,
30 /// The commission in basis points: 1 is 0.01%, 500 is 5%, 10000 (the most) is 100%.
31 /// `validatorSummaries` shows it divided by 10000. Once registered it can only be lowered.
32 /// Confirmed against mainnet registers and `validatorSummaries`.
33 pub commission_bps: u64,
34 /// The address that signs for the validator node.
35 pub signer: Address,
36}
37
38impl ValidatorProfile {
39 /// `{"node_ip","name","description","delegations_disabled","commission_bps","signer"}`.
40 #[must_use]
41 pub fn wire(&self) -> Map {
42 Map::new()
43 .put("node_ip", node_ip(self.node_ip))
44 .put("name", self.name.clone())
45 .put("description", self.description.clone())
46 .put("delegations_disabled", self.delegations_disabled)
47 .put("commission_bps", self.commission_bps)
48 .put("signer", self.signer.to_hex())
49 }
50}
51
52/// A change to a validator's profile. Every `None` is sent as null and
53/// leaves that field as it is; `unjailed` is always sent.
54#[derive(Debug, Clone, PartialEq, Eq, Default)]
55pub struct ProfileChange {
56 /// The new node IP address (`node_ip`, sent as `{"Ip":..}`). IPv4 only, as for
57 /// [`ValidatorProfile::node_ip`].
58 pub node_ip: Option<IpAddr>,
59 /// The new name.
60 pub name: Option<String>,
61 /// The new description.
62 pub description: Option<String>,
63 /// Whether the validator is unjailed after the change.
64 pub unjailed: bool,
65 /// Whether the validator refuses delegations (`disable_delegations`).
66 pub disable_delegations: Option<bool>,
67 /// The new commission (`commission_bps`) in basis points: 1 is 0.01%, 10000 is 100%. The
68 /// venue refuses a raise; it can only be lowered.
69 pub commission_bps: Option<u64>,
70 /// The new address that signs for the validator node.
71 pub signer: Option<Address>,
72}
73
74impl ProfileChange {
75 /// `{"node_ip","name","description","unjailed","disable_delegations","commission_bps","signer"}`.
76 #[must_use]
77 pub fn wire(&self) -> Map {
78 fn or_nil<T>(value: Option<T>, wire: impl FnOnce(T) -> Value) -> Value {
79 value.map_or(Value::Nil, wire)
80 }
81 Map::new()
82 .put("node_ip", or_nil(self.node_ip, |ip| node_ip(ip).into()))
83 .put("name", or_nil(self.name.clone(), Value::from))
84 .put("description", or_nil(self.description.clone(), Value::from))
85 .put("unjailed", self.unjailed)
86 .put("disable_delegations", or_nil(self.disable_delegations, Value::from))
87 .put("commission_bps", or_nil(self.commission_bps, Value::from))
88 .put("signer", or_nil(self.signer, |signer| signer.to_hex().into()))
89 }
90}
91
92/// `{"type":"CValidatorAction",<variant>:..}`, sent by the validator's own
93/// key.
94#[derive(Debug, Clone, PartialEq, Eq)]
95#[non_exhaustive]
96pub enum CValidatorAction {
97 /// `register`: a new validator with its profile, whether it starts
98 /// unjailed, and its initial self-stake in HYPE wei (8 decimals, as
99 /// [`hype_wei`](crate::earn::staking::hype_wei) converts).
100 Register {
101 /// The new validator's profile.
102 profile: ValidatorProfile,
103 /// Whether the validator starts unjailed.
104 unjailed: bool,
105 /// The initial self-stake (`initial_wei`), in HYPE wei: 8 decimals, so 10,000 HYPE is
106 /// `1_000_000_000_000`. Confirmed against mainnet registers and `delegations`.
107 initial_wei: u64,
108 },
109 /// `changeProfile`.
110 ChangeProfile(ProfileChange),
111 /// `unregister`: the key is sent, with null.
112 Unregister,
113}
114
115impl L1Action for CValidatorAction {
116 fn action(&self) -> Map {
117 let map = Map::new().put("type", "CValidatorAction");
118 match self {
119 CValidatorAction::Register { profile, unjailed, initial_wei } => map.put(
120 "register",
121 Map::new().put("profile", profile.wire()).put("unjailed", *unjailed).put("initial_wei", *initial_wei),
122 ),
123 CValidatorAction::ChangeProfile(change) => map.put("changeProfile", change.wire()),
124 CValidatorAction::Unregister => map.put("unregister", Value::Nil),
125 }
126 }
127}
128
129/// `{"type":"CSignerAction",<variant>:null}`, sent by a validator's signer
130/// key.
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132#[non_exhaustive]
133pub enum CSignerAction {
134 /// `jailSelf`.
135 JailSelf,
136 /// `unjailSelf`.
137 UnjailSelf,
138}
139
140impl L1Action for CSignerAction {
141 fn action(&self) -> Map {
142 let variant = match self {
143 CSignerAction::JailSelf => "jailSelf",
144 CSignerAction::UnjailSelf => "unjailSelf",
145 };
146 Map::new().put("type", "CSignerAction").put(variant, Value::Nil)
147 }
148}
149
150#[cfg(test)]
151#[path = "tests/validator.rs"]
152mod tests;