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