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};
9use crate::utils::XDR_DEPTH_LIMIT;
10
11#[derive(thiserror::Error, Debug)]
12pub enum Error {
13    #[error(transparent)]
14    XdrArgs(#[from] super::xdr::Error),
15    #[error(transparent)]
16    Config(#[from] super::super::config::Error),
17    #[error(transparent)]
18    Rpc(#[from] crate::rpc::Error),
19    #[error(transparent)]
20    Xdr(#[from] xdr::Error),
21    #[error(transparent)]
22    Network(#[from] config::network::Error),
23}
24
25/// Command to simulate a transaction envelope via rpc
26/// e.g. `stellar tx simulate file.txt` or `cat file.txt | stellar tx simulate`
27#[derive(Debug, clap::Parser, Clone, Default)]
28#[group(skip)]
29pub struct Cmd {
30    /// Base-64 transaction envelope XDR or file containing XDR to decode, or stdin if empty
31    #[arg()]
32    pub tx_xdr: Option<OsString>,
33
34    #[clap(flatten)]
35    pub config: config::Args,
36
37    /// Allow this many extra instructions when budgeting resources during transaction simulation
38    #[arg(long)]
39    pub instruction_leeway: Option<u64>,
40
41    #[command(flatten)]
42    pub auth_mode: crate::auth_mode::Args,
43}
44
45impl Cmd {
46    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
47        let res = self.execute(global_args, &self.config).await?;
48        let tx_env: TransactionEnvelope = res.transaction().clone().into();
49        println!(
50            "{}",
51            tx_env.to_xdr_base64(xdr::Limits::depth(XDR_DEPTH_LIMIT))?
52        );
53        Ok(())
54    }
55
56    pub async fn execute(
57        &self,
58        global_args: &global::Args,
59        config: &config::Args,
60    ) -> Result<Assembled, Error> {
61        let print = print::Print::new(global_args.quiet);
62        let network = config.get_network()?;
63        let client = network.rpc_client()?;
64        let tx = super::xdr::unwrap_envelope_v1(super::xdr::tx_envelope_from_input(&self.tx_xdr)?)?;
65        let resource_config = self
66            .instruction_leeway
67            .map(|instruction_leeway| soroban_rpc::ResourceConfig { instruction_leeway });
68        let tx = simulate_and_assemble_transaction(
69            &client,
70            &tx,
71            resource_config,
72            None,
73            self.auth_mode.to_rpc(),
74        )
75        .await?;
76        if let Some(fee_bump_fee) = tx.fee_bump_fee() {
77            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)));
78        }
79        Ok(tx)
80    }
81}