Skip to main content

soroban_cli/commands/tx/
simulate.rs

1use crate::{
2    assembled::{simulate_and_assemble_transaction, Assembled},
3    print,
4    xdr::{self, TransactionEnvelope, WriteXdr},
5};
6use std::ffi::OsString;
7
8use crate::commands::{config, global};
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
33    #[clap(flatten)]
34    pub config: config::Args,
35
36    /// Allow this many extra instructions when budgeting resources during transaction simulation
37    #[arg(long)]
38    pub instruction_leeway: Option<u64>,
39
40    #[command(flatten)]
41    pub auth_mode: crate::auth_mode::Args,
42}
43
44impl Cmd {
45    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
46        let res = self.execute(global_args, &self.config).await?;
47        let tx_env: TransactionEnvelope = res.transaction().clone().into();
48        println!("{}", tx_env.to_xdr_base64(xdr::Limits::none())?);
49        Ok(())
50    }
51
52    pub async fn execute(
53        &self,
54        global_args: &global::Args,
55        config: &config::Args,
56    ) -> Result<Assembled, Error> {
57        let print = print::Print::new(global_args.quiet);
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 resource_config = self
62            .instruction_leeway
63            .map(|instruction_leeway| soroban_rpc::ResourceConfig { instruction_leeway });
64        let tx = simulate_and_assemble_transaction(
65            &client,
66            &tx,
67            resource_config,
68            None,
69            self.auth_mode.to_rpc(),
70        )
71        .await?;
72        if let Some(fee_bump_fee) = tx.fee_bump_fee() {
73            print.warnln(format!("The transaction fee of {} is too large and needs to be wrapped in a fee bump transaction.", print::format_number(fee_bump_fee, 7)));
74        }
75        Ok(tx)
76    }
77}