Skip to main content

df_hyperliquid_core/client/evm/precompiles/
staking.rs

1//! Staking: an account's delegations and its staking totals, HYPE at 1e8.
2
3use crate::client::evm::abi::{AbiError, Token};
4use crate::client::evm::precompiles::fields::{Fields, tokens};
5use crate::client::evm::precompiles::read::{Arg, Read, read};
6use crate::utils::Address;
7
8/// One stake delegation.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct Delegation {
11    pub validator: Address,
12    /// HYPE at 1e8.
13    pub amount: u64,
14    /// Unix milliseconds.
15    pub locked_until_timestamp: u64,
16}
17
18/// `0x804`: every delegation the account holds.
19#[must_use]
20pub fn delegations(user: Address) -> Read<Vec<Delegation>> {
21    read(0x804, &[Arg::Address(user)], |answer| {
22        let tokens = tokens(0x804, answer)?;
23        let [Token::Array(items)] = tokens.as_slice() else {
24            return Err(AbiError::Type("delegations: not a list".to_owned()));
25        };
26        items
27            .iter()
28            .map(|item| {
29                let mut fields = Fields::of("delegations", std::slice::from_ref(item))?;
30                Ok(Delegation {
31                    validator: fields.address()?,
32                    amount: fields.u64()?,
33                    locked_until_timestamp: fields.u64()?,
34                })
35            })
36            .collect()
37    })
38}
39
40/// Staking totals, HYPE at 1e8.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub struct DelegatorSummary {
43    pub delegated: u64,
44    pub undelegated: u64,
45    pub total_pending_withdrawal: u64,
46    pub n_pending_withdrawals: u64,
47}
48
49/// `0x805`: the account's staking totals.
50#[must_use]
51pub fn delegator_summary(user: Address) -> Read<DelegatorSummary> {
52    read(0x805, &[Arg::Address(user)], |answer| {
53        let tokens = tokens(0x805, answer)?;
54        let mut fields = Fields::of("delegatorSummary", &tokens)?;
55        Ok(DelegatorSummary {
56            delegated: fields.u64()?,
57            undelegated: fields.u64()?,
58            total_pending_withdrawal: fields.u64()?,
59            n_pending_withdrawals: fields.u64()?,
60        })
61    })
62}
63
64#[cfg(test)]
65#[path = "tests/staking.rs"]
66mod tests;