df_hyperliquid_core/earn/staking/
actions.rs1use 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
9pub fn hype_wei(hype: Dec) -> Result<u64, BadUnits> {
12 exact_units(hype, HYPE_DECIMALS)
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct CDeposit {
18 wei: u64,
19 nonce: u64,
20}
21
22impl CDeposit {
23 pub const KEY: Signers = ActionKind::CDeposit.signers();
25
26 pub fn new(hype: Dec, nonce: u64) -> Result<CDeposit, BadUnits> {
29 Ok(CDeposit { wei: hype_wei(hype)?, nonce })
30 }
31
32 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub struct CDeposit2 {
50 wei: u64,
51}
52
53impl CDeposit2 {
54 pub const KEY: Signers = ActionKind::CDeposit2.signers();
56
57 pub fn new(hype: Dec) -> Result<CDeposit2, BadUnits> {
59 Ok(CDeposit2 { wei: hype_wei(hype)? })
60 }
61
62 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78pub struct CWithdraw {
79 wei: u64,
80 nonce: u64,
81}
82
83impl CWithdraw {
84 pub const KEY: Signers = ActionKind::CWithdraw.signers();
86
87 pub fn new(hype: Dec, nonce: u64) -> Result<CWithdraw, BadUnits> {
90 Ok(CWithdraw { wei: hype_wei(hype)?, nonce })
91 }
92
93 #[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#[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 pub const KEY: Signers = ActionKind::TokenDelegate.signers();
120
121 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 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 #[must_use]
135 pub const fn wei(&self) -> u64 {
136 self.wei
137 }
138}
139
140impl 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}