mithril_client_cli/commands/cardano_transaction/
snapshot_show.rs

1use anyhow::{anyhow, Context};
2use clap::Parser;
3use cli_table::{print_stdout, Cell, Table};
4
5use crate::{
6    commands::{client_builder_with_fallback_genesis_key, SharedArgs},
7    utils::ExpanderUtils,
8    CommandContext,
9};
10use mithril_client::MithrilResult;
11
12/// Clap command to show a given Cardano transaction snapshot
13#[derive(Parser, Debug, Clone)]
14pub struct CardanoTransactionsSnapshotShowCommand {
15    #[clap(flatten)]
16    shared_args: SharedArgs,
17
18    /// Hash of the Cardano transaction snapshot to show or `latest` for the latest artifact
19    hash: String,
20}
21
22impl CardanoTransactionsSnapshotShowCommand {
23    /// Is JSON output enabled
24    pub fn is_json_output_enabled(&self) -> bool {
25        self.shared_args.json
26    }
27
28    /// Cardano transaction snapshot Show command
29    pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
30        let params = context.config_parameters()?;
31        let client = client_builder_with_fallback_genesis_key(&params)?
32            .with_logger(context.logger().clone())
33            .build()?;
34
35        let get_list_of_artifact_ids = || async {
36            let transactions_sets = client.cardano_transaction().list_snapshots().await.with_context(|| {
37                "Can not get the list of artifacts while retrieving the latest Cardano transaction snapshot hash"
38            })?;
39
40            Ok(transactions_sets
41                .iter()
42                .map(|tx_sets| tx_sets.hash.to_owned())
43                .collect::<Vec<String>>())
44        };
45
46        let tx_sets = client
47            .cardano_transaction()
48            .get_snapshot(
49                &ExpanderUtils::expand_eventual_id_alias(&self.hash, get_list_of_artifact_ids())
50                    .await?,
51            )
52            .await?
53            .ok_or_else(|| {
54                anyhow!(
55                    "Cardano transaction snapshot not found for hash: '{}'",
56                    &self.hash
57                )
58            })?;
59
60        if self.is_json_output_enabled() {
61            println!("{}", serde_json::to_string(&tx_sets)?);
62        } else {
63            let transaction_sets_table = vec![
64                vec!["Epoch".cell(), format!("{}", &tx_sets.epoch).cell()],
65                vec![
66                    "Block Number".cell(),
67                    format!("{}", &tx_sets.block_number).cell(),
68                ],
69                vec!["Merkle Root".cell(), tx_sets.merkle_root.to_string().cell()],
70                vec![
71                    "Certificate Hash".cell(),
72                    tx_sets.certificate_hash.to_string().cell(),
73                ],
74                vec!["Hash".cell(), tx_sets.hash.cell()],
75                vec!["Created".cell(), tx_sets.created_at.to_string().cell()],
76            ]
77            .table();
78
79            print_stdout(transaction_sets_table)?
80        }
81
82        Ok(())
83    }
84}