df_hyperliquid_core/earn/staking/
actions.rs1use crate::account::{ActionKind, Signers};
5use crate::utils::sign::{Field, NonceKey, UserAction, UserSigned};
6use crate::utils::{Address, BadUnits, Dec, HYPE_DECIMALS, exact_units};
7
8pub fn hype_wei(hype: Dec) -> Result<u64, BadUnits> {
11 exact_units(hype, HYPE_DECIMALS)
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct CDeposit {
17 wei: u64,
18 nonce: u64,
19}
20
21impl CDeposit {
22 pub const KEY: Signers = ActionKind::CDeposit.signers();
24
25 pub fn new(hype: Dec, nonce: u64) -> Result<CDeposit, BadUnits> {
26 Ok(CDeposit { wei: hype_wei(hype)?, nonce })
27 }
28
29 #[must_use]
30 pub const fn wei(&self) -> u64 {
31 self.wei
32 }
33}
34
35impl UserAction for CDeposit {
36 fn user_signed(&self) -> UserSigned<'_> {
37 UserSigned::new("cDeposit", "HyperliquidTransaction:CDeposit", NonceKey::Nonce, self.nonce)
38 .field("wei", Field::Uint64(self.wei))
39 }
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub struct CWithdraw {
46 wei: u64,
47 nonce: u64,
48}
49
50impl CWithdraw {
51 pub const KEY: Signers = ActionKind::CWithdraw.signers();
53
54 pub fn new(hype: Dec, nonce: u64) -> Result<CWithdraw, BadUnits> {
55 Ok(CWithdraw { wei: hype_wei(hype)?, nonce })
56 }
57
58 #[must_use]
59 pub const fn wei(&self) -> u64 {
60 self.wei
61 }
62}
63
64impl UserAction for CWithdraw {
65 fn user_signed(&self) -> UserSigned<'_> {
66 UserSigned::new("cWithdraw", "HyperliquidTransaction:CWithdraw", NonceKey::Nonce, self.nonce)
67 .field("wei", Field::Uint64(self.wei))
68 }
69}
70
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74pub struct TokenDelegate {
75 validator: Address,
76 wei: u64,
77 is_undelegate: bool,
78 nonce: u64,
79}
80
81impl TokenDelegate {
82 pub const KEY: Signers = ActionKind::TokenDelegate.signers();
84
85 pub fn delegate(validator: Address, hype: Dec, nonce: u64) -> Result<TokenDelegate, BadUnits> {
86 Ok(TokenDelegate { validator, wei: hype_wei(hype)?, is_undelegate: false, nonce })
87 }
88
89 pub fn undelegate(validator: Address, hype: Dec, nonce: u64) -> Result<TokenDelegate, BadUnits> {
90 Ok(TokenDelegate { validator, wei: hype_wei(hype)?, is_undelegate: true, nonce })
91 }
92
93 #[must_use]
94 pub const fn wei(&self) -> u64 {
95 self.wei
96 }
97}
98
99impl UserAction for TokenDelegate {
101 fn user_signed(&self) -> UserSigned<'_> {
102 UserSigned::new("tokenDelegate", "HyperliquidTransaction:TokenDelegate", NonceKey::Nonce, self.nonce)
103 .field("validator", Field::Address(self.validator))
104 .field("wei", Field::Uint64(self.wei))
105 .field("isUndelegate", Field::Bool(self.is_undelegate))
106 }
107}