soroban_cli/commands/tx/
simulate.rs

1use crate::{
2    assembled::{simulate_and_assemble_transaction, Assembled},
3    xdr::{self, TransactionEnvelope, WriteXdr},
4};
5use async_trait::async_trait;
6use std::ffi::OsString;
7
8use crate::commands::{config, global, NetworkRunnable};
9
10#[derive(thiserror::Error, Debug)]
11pub enum Error {
12    #[error(transparent)]
13    XdrArgs(#[from] super::xdr::Error),
14    #[error(transparent)]
15    Config(#[from] super::super::config::Error),
16    #[error(transparent)]
17    Rpc(#[from] crate::rpc::Error),
18    #[error(transparent)]
19    Xdr(#[from] xdr::Error),
20    #[error(transparent)]
21    Network(#[from] config::network::Error),
22}
23
24/// Command to simulate a transaction envelope via rpc
25/// e.g. `stellar tx simulate file.txt` or `cat file.txt | stellar tx simulate`
26#[derive(Debug, clap::Parser, Clone, Default)]
27#[group(skip)]
28pub struct Cmd {
29    /// Base-64 transaction envelope XDR or file containing XDR to decode, or stdin if empty
30    #[arg()]
31    pub tx_xdr: Option<OsString>,
32    #[clap(flatten)]
33    pub config: config::Args,
34}
35
36impl Cmd {
37    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
38        let res = self
39            .run_against_rpc_server(Some(global_args), Some(&self.config))
40            .await?;
41        let tx_env: TransactionEnvelope = res.transaction().clone().into();
42        println!("{}", tx_env.to_xdr_base64(xdr::Limits::none())?);
43        Ok(())
44    }
45}
46
47#[async_trait]
48impl NetworkRunnable for Cmd {
49    type Error = Error;
50
51    type Result = Assembled;
52    async fn run_against_rpc_server(
53        &self,
54        _: Option<&global::Args>,
55        config: Option<&config::Args>,
56    ) -> Result<Self::Result, Self::Error> {
57        let config = config.unwrap_or(&self.config);
58        let network = config.get_network()?;
59        let client = network.rpc_client()?;
60        let tx = super::xdr::unwrap_envelope_v1(super::xdr::tx_envelope_from_input(&self.tx_xdr)?)?;
61        let tx = simulate_and_assemble_transaction(&client, &tx).await?;
62        Ok(tx)
63    }
64}