Skip to main content

df_hyperliquid_core/earn/staking/
actions.rs

1//! `cDeposit`, `cWithdraw` and `tokenDelegate`: user-signed, keys in the order
2//! `@nktkas/hyperliquid` 0.33.2 posts them. `cDeposit2`: the L1 form of `cDeposit`, as the exchange
3//! endpoint docs write it.
4
5use crate::account::{ActionKind, Signers};
6use crate::utils::sign::{Field, L1Action, Map, NonceKey, UserAction, UserSigned};
7use crate::utils::{Address, BadUnits, Dec, HYPE_DECIMALS, exact_units};
8
9/// `hype` as the `wei` an action carries, refused rather than rounded when
10/// it has more than eight places.
11pub fn hype_wei(hype: Dec) -> Result<u64, BadUnits> {
12    exact_units(hype, HYPE_DECIMALS)
13}
14
15/// `cDeposit`: HYPE from spot into the staking balance, undelegated.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct CDeposit {
18    wei: u64,
19    nonce: u64,
20}
21
22impl CDeposit {
23    /// User-signed: only the account's own key.
24    pub const KEY: Signers = ActionKind::CDeposit.signers();
25
26    /// `hype` from spot into the staking balance, signed at `nonce`, refused rather than rounded past
27    /// eight places.
28    pub fn new(hype: Dec, nonce: u64) -> Result<CDeposit, BadUnits> {
29        Ok(CDeposit { wei: hype_wei(hype)?, nonce })
30    }
31
32    /// The amount as the action carries it, in hundred-millionths of HYPE.
33    #[must_use]
34    pub const fn wei(&self) -> u64 {
35        self.wei
36    }
37}
38
39impl UserAction for CDeposit {
40    fn user_signed(&self) -> UserSigned<'_> {
41        UserSigned::new("cDeposit", "HyperliquidTransaction:CDeposit", NonceKey::Nonce, self.nonce)
42            .field("wei", Field::Uint64(self.wei))
43    }
44}
45
46/// `cDeposit2`: [`CDeposit`] as an L1 action, `{type, wei}`, which an agent may sign for its
47/// master. It carries no nonce of its own: the one it is signed at.
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub struct CDeposit2 {
50    wei: u64,
51}
52
53impl CDeposit2 {
54    /// L1: an agent may sign it for its master.
55    pub const KEY: Signers = ActionKind::CDeposit2.signers();
56
57    /// `hype` from spot into the staking balance, refused rather than rounded past eight places.
58    pub fn new(hype: Dec) -> Result<CDeposit2, BadUnits> {
59        Ok(CDeposit2 { wei: hype_wei(hype)? })
60    }
61
62    /// The amount as the action carries it, in hundred-millionths of HYPE.
63    #[must_use]
64    pub const fn wei(&self) -> u64 {
65        self.wei
66    }
67}
68
69impl L1Action for CDeposit2 {
70    fn action(&self) -> Map {
71        Map::new().put("type", "cDeposit2").put("wei", self.wei)
72    }
73}
74
75/// `cWithdraw`: undelegated HYPE from staking back toward spot, after
76/// [`unstaking_ms`](crate::earn::staking::unstaking_ms) in the queue.
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78pub struct CWithdraw {
79    wei: u64,
80    nonce: u64,
81}
82
83impl CWithdraw {
84    /// User-signed: only the account's own key.
85    pub const KEY: Signers = ActionKind::CWithdraw.signers();
86
87    /// `hype` from the staking balance toward spot, signed at `nonce`, refused rather than rounded past
88    /// eight places.
89    pub fn new(hype: Dec, nonce: u64) -> Result<CWithdraw, BadUnits> {
90        Ok(CWithdraw { wei: hype_wei(hype)?, nonce })
91    }
92
93    /// The amount as the action carries it, in hundred-millionths of HYPE.
94    #[must_use]
95    pub const fn wei(&self) -> u64 {
96        self.wei
97    }
98}
99
100impl UserAction for CWithdraw {
101    fn user_signed(&self) -> UserSigned<'_> {
102        UserSigned::new("cWithdraw", "HyperliquidTransaction:CWithdraw", NonceKey::Nonce, self.nonce)
103            .field("wei", Field::Uint64(self.wei))
104    }
105}
106
107/// `tokenDelegate`: staking balance to a validator, or back. A delegation is locked for
108/// [`delegation_lock_ms`](crate::earn::staking::delegation_lock_ms) before it can be undelegated.
109#[derive(Debug, Clone, Copy, PartialEq, Eq)]
110pub struct TokenDelegate {
111    validator: Address,
112    wei: u64,
113    is_undelegate: bool,
114    nonce: u64,
115}
116
117impl TokenDelegate {
118    /// User-signed: only the account's own key.
119    pub const KEY: Signers = ActionKind::TokenDelegate.signers();
120
121    /// Delegates `hype` of the staking balance to `validator`, signed at `nonce`, refused rather than
122    /// rounded past eight places.
123    pub fn delegate(validator: Address, hype: Dec, nonce: u64) -> Result<TokenDelegate, BadUnits> {
124        Ok(TokenDelegate { validator, wei: hype_wei(hype)?, is_undelegate: false, nonce })
125    }
126
127    /// Undelegates `hype` from `validator` back to the staking balance (`isUndelegate` true), signed at
128    /// `nonce`, refused rather than rounded past eight places.
129    pub fn undelegate(validator: Address, hype: Dec, nonce: u64) -> Result<TokenDelegate, BadUnits> {
130        Ok(TokenDelegate { validator, wei: hype_wei(hype)?, is_undelegate: true, nonce })
131    }
132
133    /// The amount as the action carries it, in hundred-millionths of HYPE.
134    #[must_use]
135    pub const fn wei(&self) -> u64 {
136        self.wei
137    }
138}
139
140/// `validator` is an `address` in the typed data, posted lowercase.
141impl UserAction for TokenDelegate {
142    fn user_signed(&self) -> UserSigned<'_> {
143        UserSigned::new("tokenDelegate", "HyperliquidTransaction:TokenDelegate", NonceKey::Nonce, self.nonce)
144            .field("validator", Field::Address(self.validator))
145            .field("wei", Field::Uint64(self.wei))
146            .field("isUndelegate", Field::Bool(self.is_undelegate))
147    }
148}