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
41impl Cmd {
42    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
43        let res = self.execute(global_args, &self.config).await?;
44        let tx_env: TransactionEnvelope = res.transaction().clone().into();
45        println!("{}", tx_env.to_xdr_base64(xdr::Limits::none())?);
46        Ok(())
47    }
48
49    pub async fn execute(
50        &self,
51        global_args: &global::Args,
52        config: &config::Args,
53    ) -> Result<Assembled, Error> {
54        let print = print::Print::new(global_args.quiet);
55        let network = config.get_network()?;
56        let client = network.rpc_client()?;
57        let tx = super::xdr::unwrap_envelope_v1(super::xdr::tx_envelope_from_input(&self.tx_xdr)?)?;
58        let resource_config = self
59            .instruction_leeway
60            .map(|instruction_leeway| soroban_rpc::ResourceConfig { instruction_leeway });
61        let tx = simulate_and_assemble_transaction(&client, &tx, resource_config, None).await?;
62        if let Some(fee_bump_fee) = tx.fee_bump_fee() {
63            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)));
64        }
65        Ok(tx)
66    }
67}