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