miraland_program/sysvar/epoch_rewards.rs
1//! Epoch rewards for current epoch
2//!
3//! The _epoch rewards_ sysvar provides access to the [`EpochRewards`] type,
4//! which tracks the progress of epoch rewards distribution. It includes the
5//! - total rewards for the current epoch, in lamports
6//! - rewards for the current epoch distributed so far, in lamports
7//! - distribution completed block height, i.e. distribution of all staking rewards for the current
8//! epoch will be completed at this block height
9//!
10//! [`EpochRewards`] implements [`Sysvar::get`] and can be loaded efficiently without
11//! passing the sysvar account ID to the program.
12//!
13//! See also the Miraland [documentation on the epoch rewards sysvar][sdoc].
14//!
15//! [sdoc]: https://docs.solana.com/developing/runtime-facilities/sysvars#epochrewards
16//!
17//! # Examples
18//!
19//! Accessing via on-chain program directly:
20//!
21//! ```no_run
22//! # use miraland_program::{
23//! # account_info::{AccountInfo, next_account_info},
24//! # entrypoint::ProgramResult,
25//! # msg,
26//! # program_error::ProgramError,
27//! # pubkey::Pubkey,
28//! # sysvar::epoch_rewards::{self, EpochRewards},
29//! # sysvar::Sysvar,
30//! # };
31//! #
32//! fn process_instruction(
33//! program_id: &Pubkey,
34//! accounts: &[AccountInfo],
35//! instruction_data: &[u8],
36//! ) -> ProgramResult {
37//!
38//! let epoch_rewards = EpochRewards::get()?;
39//! msg!("epoch_rewards: {:#?}", epoch_rewards);
40//!
41//! Ok(())
42//! }
43//! #
44//! # use miraland_program::sysvar::SysvarId;
45//! # let p = EpochRewards::id();
46//! # let l = &mut 1120560;
47//! # let d = &mut vec![0, 202, 154, 59, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0];
48//! # let a = AccountInfo::new(&p, false, false, l, d, &p, false, 0);
49//! # let accounts = &[a.clone(), a];
50//! # process_instruction(
51//! # &Pubkey::new_unique(),
52//! # accounts,
53//! # &[],
54//! # )?;
55//! # Ok::<(), ProgramError>(())
56//! ```
57//!
58//! Accessing via on-chain program's account parameters:
59//!
60//! ```
61//! # use miraland_program::{
62//! # account_info::{AccountInfo, next_account_info},
63//! # entrypoint::ProgramResult,
64//! # msg,
65//! # pubkey::Pubkey,
66//! # sysvar::epoch_rewards::{self, EpochRewards},
67//! # sysvar::Sysvar,
68//! # };
69//! # use miraland_program::program_error::ProgramError;
70//! #
71//! fn process_instruction(
72//! program_id: &Pubkey,
73//! accounts: &[AccountInfo],
74//! instruction_data: &[u8],
75//! ) -> ProgramResult {
76//! let account_info_iter = &mut accounts.iter();
77//! let epoch_rewards_account_info = next_account_info(account_info_iter)?;
78//!
79//! assert!(epoch_rewards::check_id(epoch_rewards_account_info.key));
80//!
81//! let epoch_rewards = EpochRewards::from_account_info(epoch_rewards_account_info)?;
82//! msg!("epoch_rewards: {:#?}", epoch_rewards);
83//!
84//! Ok(())
85//! }
86//! #
87//! # use miraland_program::sysvar::SysvarId;
88//! # let p = EpochRewards::id();
89//! # let l = &mut 1120560;
90//! # let d = &mut vec![0, 202, 154, 59, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0];
91//! # let a = AccountInfo::new(&p, false, false, l, d, &p, false, 0);
92//! # let accounts = &[a.clone(), a];
93//! # process_instruction(
94//! # &Pubkey::new_unique(),
95//! # accounts,
96//! # &[],
97//! # )?;
98//! # Ok::<(), ProgramError>(())
99//! ```
100//!
101//! Accessing via the RPC client:
102//!
103//! ```
104//! # use miraland_program::example_mocks::miraland_sdk;
105//! # use miraland_program::example_mocks::miraland_rpc_client;
106//! # use miraland_sdk::account::Account;
107//! # use miraland_rpc_client::rpc_client::RpcClient;
108//! # use miraland_sdk::sysvar::epoch_rewards::{self, EpochRewards};
109//! # use anyhow::Result;
110//! #
111//! fn print_sysvar_epoch_rewards(client: &RpcClient) -> Result<()> {
112//! # client.set_get_account_response(epoch_rewards::ID, Account {
113//! # lamports: 1120560,
114//! # data: vec![0, 202, 154, 59, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0],
115//! # owner: miraland_sdk::system_program::ID,
116//! # executable: false,
117//! # rent_epoch: 307,
118//! # });
119//! #
120//! let epoch_rewards = client.get_account(&epoch_rewards::ID)?;
121//! let data: EpochRewards = bincode::deserialize(&epoch_rewards.data)?;
122//!
123//! Ok(())
124//! }
125//! #
126//! # let client = RpcClient::new(String::new());
127//! # print_sysvar_epoch_rewards(&client)?;
128//! #
129//! # Ok::<(), anyhow::Error>(())
130//! ```
131
132pub use crate::epoch_rewards::EpochRewards;
133use crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar};
134
135crate::declare_sysvar_id!("SysvarEpochRewards1111111111111111111111111", EpochRewards);
136
137impl Sysvar for EpochRewards {
138 impl_sysvar_get!(sol_get_epoch_rewards_sysvar);
139}