solana_cli/
inflation.rs

1use {
2    crate::cli::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult},
3    clap::{App, Arg, ArgMatches, SubCommand},
4    solana_clap_utils::{
5        input_parsers::{pubkeys_of, value_of},
6        input_validators::is_valid_pubkey,
7        keypair::*,
8    },
9    solana_cli_output::{
10        CliEpochRewardsMetadata, CliInflation, CliKeyedEpochReward, CliKeyedEpochRewards,
11    },
12    solana_clock::{Epoch, Slot, UnixTimestamp},
13    solana_pubkey::Pubkey,
14    solana_remote_wallet::remote_wallet::RemoteWalletManager,
15    solana_rpc_client::rpc_client::RpcClient,
16    std::{collections::HashMap, rc::Rc},
17};
18
19#[derive(Debug, PartialEq, Eq)]
20pub enum InflationCliCommand {
21    Show,
22    Rewards(Vec<Pubkey>, Option<Epoch>),
23}
24
25pub trait InflationSubCommands {
26    fn inflation_subcommands(self) -> Self;
27}
28
29impl InflationSubCommands for App<'_, '_> {
30    fn inflation_subcommands(self) -> Self {
31        self.subcommand(
32            SubCommand::with_name("inflation")
33                .about("Show inflation information")
34                .subcommand(
35                    SubCommand::with_name("rewards")
36                        .about("Show inflation rewards for a set of addresses")
37                        .arg(pubkey!(
38                            Arg::with_name("addresses")
39                                .value_name("ADDRESS")
40                                .index(1)
41                                .multiple(true)
42                                .required(true),
43                            "Account to query for rewards."
44                        ))
45                        .arg(
46                            Arg::with_name("rewards_epoch")
47                                .long("rewards-epoch")
48                                .takes_value(true)
49                                .value_name("EPOCH")
50                                .help("Display rewards for specific epoch [default: latest epoch]"),
51                        ),
52                ),
53        )
54    }
55}
56
57pub fn parse_inflation_subcommand(
58    matches: &ArgMatches<'_>,
59    _default_signer: &DefaultSigner,
60    _wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
61) -> Result<CliCommandInfo, CliError> {
62    let command = match matches.subcommand() {
63        ("rewards", Some(matches)) => {
64            let addresses = pubkeys_of(matches, "addresses").unwrap();
65            let rewards_epoch = value_of(matches, "rewards_epoch");
66            InflationCliCommand::Rewards(addresses, rewards_epoch)
67        }
68        _ => InflationCliCommand::Show,
69    };
70    Ok(CliCommandInfo::without_signers(CliCommand::Inflation(
71        command,
72    )))
73}
74
75pub fn process_inflation_subcommand(
76    rpc_client: &RpcClient,
77    config: &CliConfig,
78    inflation_subcommand: &InflationCliCommand,
79) -> ProcessResult {
80    match inflation_subcommand {
81        InflationCliCommand::Show => process_show(rpc_client, config),
82        InflationCliCommand::Rewards(ref addresses, rewards_epoch) => {
83            process_rewards(rpc_client, config, addresses, *rewards_epoch)
84        }
85    }
86}
87
88fn process_show(rpc_client: &RpcClient, config: &CliConfig) -> ProcessResult {
89    let governor = rpc_client.get_inflation_governor()?;
90    let current_rate = rpc_client.get_inflation_rate()?;
91
92    let inflation = CliInflation {
93        governor,
94        current_rate,
95    };
96
97    Ok(config.output_format.formatted_string(&inflation))
98}
99
100fn process_rewards(
101    rpc_client: &RpcClient,
102    config: &CliConfig,
103    addresses: &[Pubkey],
104    rewards_epoch: Option<Epoch>,
105) -> ProcessResult {
106    let rewards = rpc_client
107        .get_inflation_reward(addresses, rewards_epoch)
108        .map_err(|err| {
109            if let Some(epoch) = rewards_epoch {
110                format!("Rewards not available for epoch {epoch}")
111            } else {
112                format!("Rewards not available {err}")
113            }
114        })?;
115    let epoch_schedule = rpc_client.get_epoch_schedule()?;
116
117    let mut epoch_rewards: Vec<CliKeyedEpochReward> = vec![];
118    let mut block_times: HashMap<Slot, UnixTimestamp> = HashMap::new();
119    let epoch_metadata = if let Some(Some(first_reward)) = rewards.iter().find(|&v| v.is_some()) {
120        let (epoch_start_time, epoch_end_time) =
121            crate::stake::get_epoch_boundary_timestamps(rpc_client, first_reward, &epoch_schedule)?;
122        for (reward, address) in rewards.iter().zip(addresses) {
123            let cli_reward = if let Some(reward) = reward {
124                let block_time = if let Some(block_time) = block_times.get(&reward.effective_slot) {
125                    *block_time
126                } else {
127                    let block_time = rpc_client.get_block_time(reward.effective_slot)?;
128                    block_times.insert(reward.effective_slot, block_time);
129                    block_time
130                };
131                crate::stake::make_cli_reward(reward, block_time, epoch_start_time, epoch_end_time)
132            } else {
133                None
134            };
135            epoch_rewards.push(CliKeyedEpochReward {
136                address: address.to_string(),
137                reward: cli_reward,
138            });
139        }
140        Some(CliEpochRewardsMetadata {
141            epoch: first_reward.epoch,
142            ..CliEpochRewardsMetadata::default()
143        })
144    } else {
145        None
146    };
147    let cli_rewards = CliKeyedEpochRewards {
148        epoch_metadata,
149        rewards: epoch_rewards,
150    };
151    Ok(config.output_format.formatted_string(&cli_rewards))
152}