Skip to main content

soroban_cli/
resources.rs

1use std::io::stderr;
2
3use soroban_rpc::GetTransactionResponse;
4
5use crate::assembled::Assembled;
6use crate::commands::tx::fetch;
7use crate::commands::tx::fetch::fee::FeeTable;
8use crate::commands::HEADING_RPC;
9
10#[derive(Debug, clap::Args, Clone, Default)]
11#[group(skip)]
12pub struct Args {
13    /// Set the fee for smart contract resource consumption, in stroops. 1 stroop = 0.0000001 xlm. Overrides the simulated resource fee
14    #[arg(long, env = "STELLAR_RESOURCE_FEE", value_parser = clap::value_parser!(i64).range(0..u32::MAX.into()), help_heading = HEADING_RPC)]
15    pub resource_fee: Option<i64>,
16    /// ⚠️ Deprecated, use `--instruction-leeway` to increase instructions. Number of instructions to allocate for the transaction
17    #[arg(long, help_heading = HEADING_RPC)]
18    pub instructions: Option<u32>,
19    /// Allow this many extra instructions when budgeting resources with transaction simulation
20    #[arg(long, help_heading = HEADING_RPC)]
21    pub instruction_leeway: Option<u64>,
22    /// Output the cost execution to stderr
23    #[arg(long, help_heading = HEADING_RPC)]
24    pub cost: bool,
25}
26
27impl Args {
28    // TODO: Remove once `--instructions` is fully removed
29    pub fn apply_to_assembled_txn(&self, txn: Assembled) -> Assembled {
30        if let Some(instructions) = self.instructions {
31            txn.set_max_instructions(instructions)
32        } else {
33            txn
34        }
35    }
36
37    pub fn resource_config(&self) -> Option<soroban_rpc::ResourceConfig> {
38        self.instruction_leeway
39            .map(|instruction_leeway| soroban_rpc::ResourceConfig { instruction_leeway })
40    }
41
42    pub fn print_cost_info(&self, res: &GetTransactionResponse) -> Result<(), fetch::Error> {
43        if !self.cost {
44            return Ok(());
45        }
46
47        let fee_table = FeeTable::new_from_transaction_response(res)?;
48
49        fee_table.table().print(&mut stderr())?;
50
51        Ok(())
52    }
53}