Skip to main content

solana_epoch_stake/
lib.rs

1//! API for retrieving epoch stake information.
2//!
3//! On-chain programs can use this API to retrieve the total stake for the
4//! current epoch or the stake for a specific vote account using the
5//! `sol_get_epoch_stake` syscall.
6#![cfg_attr(docsrs, feature(doc_cfg))]
7#![no_std]
8
9use solana_pubkey::Pubkey;
10
11fn get_epoch_stake(var_addr: *const u8) -> u64 {
12    #[cfg(target_os = "solana")]
13    {
14        unsafe { solana_define_syscall::definitions::sol_get_epoch_stake(var_addr) }
15    }
16
17    #[cfg(not(target_os = "solana"))]
18    {
19        core::hint::black_box(var_addr);
20        0
21    }
22}
23
24/// Get the current epoch's total stake.
25pub fn get_epoch_total_stake() -> u64 {
26    get_epoch_stake(core::ptr::null::<Pubkey>() as *const u8)
27}
28
29/// Get the current epoch stake for a given vote address.
30///
31/// If the provided vote address corresponds to an account that is not a vote
32/// account or does not exist, returns `0` for active stake.
33pub fn get_epoch_stake_for_vote_account(vote_address: &Pubkey) -> u64 {
34    get_epoch_stake(vote_address as *const _ as *const u8)
35}