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)]
95pub enum CValidatorAction {
96 /// `register`: a new validator with its profile, whether it starts
97 /// unjailed, and its initial self-stake in HYPE wei (8 decimals, as
98 /// [`hype_wei`](crate::earn::staking::hype_wei) converts).
99 Register {
100 /// The new validator's profile.
101 profile: ValidatorProfile,
102 /// Whether the validator starts unjailed.
103 unjailed: bool,
104 /// The initial self-stake (`initial_wei`), in HYPE wei: 8 decimals, so 10,000 HYPE is
105 /// `1_000_000_000_000`. Confirmed against mainnet registers and `delegations`.
106 initial_wei: u64,
107 },
108 /// `changeProfile`.
109 ChangeProfile(ProfileChange),
110 /// `unregister`: the key is sent, with null.
111 Unregister,
112}
113
114impl L1Action for CValidatorAction {
115 fn action(&self) -> Map {
116 let map = Map::new().put("type", "CValidatorAction");
117 match self {
118 CValidatorAction::Register { profile, unjailed, initial_wei } => map.put(
119 "register",
120 Map::new().put("profile", profile.wire()).put("unjailed", *unjailed).put("initial_wei", *initial_wei),
121 ),
122 CValidatorAction::ChangeProfile(change) => map.put("changeProfile", change.wire()),
123 CValidatorAction::Unregister => map.put("unregister", Value::Nil),
124 }
125 }
126}
127
128/// `{"type":"CSignerAction",<variant>:null}`, sent by a validator's signer
129/// key.
130#[derive(Debug, Clone, Copy, PartialEq, Eq)]
131pub enum CSignerAction {
132 /// `jailSelf`.
133 JailSelf,
134 /// `unjailSelf`.
135 UnjailSelf,
136}
137
138impl L1Action for CSignerAction {
139 fn action(&self) -> Map {
140 let variant = match self {
141 CSignerAction::JailSelf => "jailSelf",
142 CSignerAction::UnjailSelf => "unjailSelf",
143 };
144 Map::new().put("type", "CSignerAction").put(variant, Value::Nil)
145 }
146}
147
148#[cfg(test)]
149#[path = "tests/validator.rs"]
150mod tests;