pcli/command/query/
tx.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use anyhow::Result;
use colored_json::prelude::*;
use penumbra_proto::{util::tendermint_proxy::v1::GetTxRequest, DomainType};
use penumbra_transaction::Transaction;

use crate::App;

use super::OutputFormat;

/// Queries the chain for a transaction by hash.
#[derive(Debug, clap::Args)]
pub struct Tx {
    /// The format to output the transaction in.
    #[clap(short, long, value_enum, default_value_t)]
    output: OutputFormat,
    /// The hex-formatted transaction hash to query.
    hash: String,
}

impl Tx {
    pub async fn exec(&self, app: &mut App) -> Result<()> {
        let mut client = app.tendermint_proxy_client().await?;

        let rsp = client
            .get_tx(GetTxRequest {
                hash: hex::decode(self.hash.clone())?,
                prove: false,
            })
            .await?;

        let rsp = rsp.into_inner();
        let tx = Transaction::decode(rsp.tx.as_slice())?;

        match self.output {
            OutputFormat::Json => {
                let tx_json = serde_json::to_string_pretty(&tx)?;
                println!("{}", tx_json.to_colored_json_auto()?);
            }
            OutputFormat::Base64 => {
                use base64::{display::Base64Display, engine::general_purpose::STANDARD};
                println!("{}", Base64Display::new(&rsp.tx, &STANDARD));
            }
        }

        Ok(())
    }
}