mithril_client_cli/commands/cardano_transaction/
snapshot_show.rs

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