soroban_cli/commands/tx/fetch/
mod.rs

1use crate::{commands::global, config::network, xdr::Hash};
2use clap::Subcommand;
3pub use soroban_rpc::GetTransactionEvents;
4use std::fmt::Debug;
5
6mod args;
7mod envelope;
8mod events;
9pub mod fee;
10mod meta;
11mod result;
12
13#[derive(Debug, clap::Args)]
14#[command(args_conflicts_with_subcommands = true)]
15pub struct Cmd {
16    #[command(subcommand)]
17    subcommand: Option<FetchCommands>,
18
19    #[command(flatten)]
20    default: DefaultArgs,
21}
22
23#[derive(Debug, Subcommand)]
24pub enum FetchCommands {
25    /// Fetch the transaction result
26    Result(result::Cmd),
27    /// Fetch the transaction meta
28    Meta(meta::Cmd),
29    /// Fetch the transaction fee information
30    Fee(fee::Cmd),
31    /// Fetch the transaction events
32    Events(events::Cmd),
33    /// Fetch the transaction envelope
34    #[command(hide = true)]
35    Envelope(envelope::Cmd),
36}
37
38#[derive(Debug, clap::Args)]
39struct DefaultArgs {
40    /// Hash of transaction to fetch
41    #[arg(long)]
42    pub hash: Option<Hash>,
43
44    #[command(flatten)]
45    pub network: Option<network::Args>,
46
47    /// Format of the output
48    #[arg(long, default_value = "json")]
49    pub output: Option<args::OutputFormat>,
50}
51
52#[derive(thiserror::Error, Debug)]
53pub enum Error {
54    #[error(transparent)]
55    Args(#[from] args::Error),
56
57    #[error(transparent)]
58    Result(#[from] result::Error),
59
60    #[error(transparent)]
61    Meta(#[from] meta::Error),
62
63    #[error(transparent)]
64    Envelope(#[from] envelope::Error),
65
66    #[error(transparent)]
67    Events(#[from] events::Error),
68    #[error(transparent)]
69    NotSupported(#[from] fee::Error),
70
71    #[error("the following required argument was not provided: {0}")]
72    MissingArg(String),
73
74    #[error(transparent)]
75    Io(#[from] std::io::Error),
76}
77
78impl Cmd {
79    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
80        match &self.subcommand {
81            Some(FetchCommands::Result(cmd)) => cmd.run(global_args).await?,
82            Some(FetchCommands::Meta(cmd)) => cmd.run(global_args).await?,
83            Some(FetchCommands::Envelope(cmd)) => cmd.run(global_args).await?,
84            Some(FetchCommands::Fee(cmd)) => cmd.run(global_args).await?,
85            Some(FetchCommands::Events(cmd)) => cmd.run(global_args).await?,
86            None => {
87                envelope::Cmd {
88                    args: args::Args {
89                        hash: self
90                            .default
91                            .hash
92                            .clone()
93                            .ok_or(Error::MissingArg("--hash <HASH>".to_string()))?,
94                        network: self.default.network.clone().unwrap_or_default(),
95                    },
96                    output: self.default.output.unwrap_or_default(),
97                }
98                .run(global_args)
99                .await?;
100            }
101        }
102        Ok(())
103    }
104}