mithril_client_cli/commands/cardano_stake_distribution/
list.rs

1use clap::Parser;
2use cli_table::{Cell, Table, format::Justify, print_stdout};
3
4use crate::{CommandContext, commands::client_builder_with_fallback_genesis_key};
5use mithril_client::MithrilResult;
6
7/// Cardano stake distribution LIST command
8#[derive(Parser, Debug, Clone)]
9pub struct CardanoStakeDistributionListCommand {}
10
11impl CardanoStakeDistributionListCommand {
12    /// Main command execution
13    pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
14        let client = client_builder_with_fallback_genesis_key(context.config_parameters())?
15            .with_logger(context.logger().clone())
16            .build()?;
17        let lines = client.cardano_stake_distribution().list().await?;
18
19        if context.is_json_output_enabled() {
20            println!("{}", serde_json::to_string(&lines)?);
21        } else {
22            let lines = lines
23                .into_iter()
24                .map(|item| {
25                    vec![
26                        format!("{}", item.epoch).cell(),
27                        item.hash.cell(),
28                        item.certificate_hash.cell(),
29                        item.created_at.to_string().cell(),
30                    ]
31                })
32                .collect::<Vec<_>>()
33                .table()
34                .title(vec![
35                    "Epoch".cell(),
36                    "Hash".cell(),
37                    "Certificate Hash".cell(),
38                    "Created".cell().justify(Justify::Right),
39                ]);
40            print_stdout(lines)?;
41        }
42
43        Ok(())
44    }
45}