From 398a018fccda957f6f28dfaab2d8d1747293bd62 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Wed, 20 Mar 2024 13:12:53 -0400 Subject: [PATCH] feat: add sim_only to print assembled transaction --- .../src/commands/contract/deploy/asset.rs | 2 +- .../src/commands/contract/deploy/wasm.rs | 4 +- .../src/commands/contract/install.rs | 2 +- .../src/commands/contract/invoke.rs | 2 +- cmd/soroban-cli/src/fee.rs | 20 +++++++-- docs/soroban-cli-full-docs.md | 42 +++++++++++++++---- 6 files changed, 57 insertions(+), 15 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/deploy/asset.rs b/cmd/soroban-cli/src/commands/contract/deploy/asset.rs index aa002a25c7..138bf6a670 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/asset.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/asset.rs @@ -103,7 +103,7 @@ impl NetworkRunnable for Cmd { )?; self.fee.exit_if_build_only(&tx)?; let txn = client.create_assembled_transaction(&tx).await?; - let txn = self.fee.apply_to_assembled_txn(txn); + let txn = self.fee.apply_to_assembled_txn(txn)?; client .send_assembled_transaction(txn, &key, &[], network_passphrase, None, None) .await?; diff --git a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs index 14963b1224..95b27b470d 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs @@ -113,7 +113,7 @@ impl NetworkRunnable for Cmd { ) -> Result { let config = config.unwrap_or(&self.config); let wasm_hash = if let Some(wasm) = &self.wasm { - let mut fee = self.fee.clone(); + let mut fee = self.fee.clone(); fee.build_only = false; let hash = install::Cmd { wasm: wasm::Args { wasm: wasm.clone() }, @@ -168,7 +168,7 @@ impl NetworkRunnable for Cmd { )?; self.fee.exit_if_build_only(&txn)?; let txn = client.create_assembled_transaction(&txn).await?; - let txn = self.fee.apply_to_assembled_txn(txn); + let txn = self.fee.apply_to_assembled_txn(txn)?; client .send_assembled_transaction(txn, &key, &[], &network.network_passphrase, None, None) .await?; diff --git a/cmd/soroban-cli/src/commands/contract/install.rs b/cmd/soroban-cli/src/commands/contract/install.rs index d834c9cd35..afaf6f4816 100644 --- a/cmd/soroban-cli/src/commands/contract/install.rs +++ b/cmd/soroban-cli/src/commands/contract/install.rs @@ -129,7 +129,7 @@ impl NetworkRunnable for Cmd { let txn = client .create_assembled_transaction(&tx_without_preflight) .await?; - let txn = self.fee.apply_to_assembled_txn(txn); + let txn = self.fee.apply_to_assembled_txn(txn)?; // Currently internal errors are not returned if the contract code is expired if let Some(TransactionResult { diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 21b3ab550c..0216176ca0 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -336,7 +336,7 @@ impl NetworkRunnable for Cmd { )?; self.fee.exit_if_build_only(&tx)?; let txn = client.create_assembled_transaction(&tx).await?; - let txn = self.fee.apply_to_assembled_txn(txn); + let txn = self.fee.apply_to_assembled_txn(txn)?; let (return_value, events) = if self.is_view() { ( txn.sim_response().results()?[0].xdr.clone(), diff --git a/cmd/soroban-cli/src/fee.rs b/cmd/soroban-cli/src/fee.rs index f3f8520561..4937c0458a 100644 --- a/cmd/soroban-cli/src/fee.rs +++ b/cmd/soroban-cli/src/fee.rs @@ -17,9 +17,12 @@ pub struct Args { /// Number of instructions to simulate #[arg(long, help_heading = HEADING_RPC)] pub instructions: Option, - /// Build the transaction only write the output to stdout + /// Build the transaction only write the base64 xdr to stdout #[arg(long, help_heading = HEADING_RPC)] pub build_only: bool, + /// Simulation the transaction only write the base64 to stdout + #[arg(long, help_heading = HEADING_RPC, conflicts_with = "build_only")] + pub sim_only: bool, } impl Args { @@ -31,12 +34,22 @@ impl Args { Ok(()) } - pub fn apply_to_assembled_txn(&self, txn: Assembled) -> Assembled { - if let Some(instructions) = self.instructions { + pub fn apply_to_assembled_txn(&self, txn: Assembled) -> Result { + let simulated_txn = if let Some(instructions) = self.instructions { txn.set_max_instructions(instructions) } else { add_padding_to_instructions(txn) + }; + if self.sim_only { + println!( + "{}", + simulated_txn + .transaction() + .to_xdr_base64(xdr::Limits::none())? + ); + std::process::exit(0); } + Ok(simulated_txn) } } @@ -60,6 +73,7 @@ impl Default for Args { cost: false, instructions: None, build_only: false, + sim_only: false, } } } diff --git a/docs/soroban-cli-full-docs.md b/docs/soroban-cli-full-docs.md index cfb12e5444..423b92ff81 100644 --- a/docs/soroban-cli-full-docs.md +++ b/docs/soroban-cli-full-docs.md @@ -579,7 +579,11 @@ Deploy builtin Soroban Asset Contract Possible values: `true`, `false` * `--instructions ` — Number of instructions to simulate -* `--build-only` — Build the transaction only write the output to stdout +* `--build-only` — Build the transaction only write the base64 xdr to stdout + + Possible values: `true`, `false` + +* `--sim-only` — Simulation the transaction only write the base64 to stdout Possible values: `true`, `false` @@ -734,7 +738,11 @@ If no keys are specified the contract itself is extended. Possible values: `true`, `false` * `--instructions ` — Number of instructions to simulate -* `--build-only` — Build the transaction only write the output to stdout +* `--build-only` — Build the transaction only write the base64 xdr to stdout + + Possible values: `true`, `false` + +* `--sim-only` — Simulation the transaction only write the base64 to stdout Possible values: `true`, `false` @@ -770,7 +778,11 @@ Deploy a wasm contract Possible values: `true`, `false` * `--instructions ` — Number of instructions to simulate -* `--build-only` — Build the transaction only write the output to stdout +* `--build-only` — Build the transaction only write the base64 xdr to stdout + + Possible values: `true`, `false` + +* `--sim-only` — Simulation the transaction only write the base64 to stdout Possible values: `true`, `false` @@ -938,7 +950,11 @@ Install a WASM file to the ledger without creating a contract instance Possible values: `true`, `false` * `--instructions ` — Number of instructions to simulate -* `--build-only` — Build the transaction only write the output to stdout +* `--build-only` — Build the transaction only write the base64 xdr to stdout + + Possible values: `true`, `false` + +* `--sim-only` — Simulation the transaction only write the base64 to stdout Possible values: `true`, `false` @@ -991,7 +1007,11 @@ soroban contract invoke ... -- --help Possible values: `true`, `false` * `--instructions ` — Number of instructions to simulate -* `--build-only` — Build the transaction only write the output to stdout +* `--build-only` — Build the transaction only write the base64 xdr to stdout + + Possible values: `true`, `false` + +* `--sim-only` — Simulation the transaction only write the base64 to stdout Possible values: `true`, `false` @@ -1107,7 +1127,11 @@ If no keys are specificed the contract itself is restored. Possible values: `true`, `false` * `--instructions ` — Number of instructions to simulate -* `--build-only` — Build the transaction only write the output to stdout +* `--build-only` — Build the transaction only write the base64 xdr to stdout + + Possible values: `true`, `false` + +* `--sim-only` — Simulation the transaction only write the base64 to stdout Possible values: `true`, `false` @@ -1400,7 +1424,11 @@ Deploy a token contract to wrap an existing Stellar classic asset for smart cont Possible values: `true`, `false` * `--instructions ` — Number of instructions to simulate -* `--build-only` — Build the transaction only write the output to stdout +* `--build-only` — Build the transaction only write the base64 xdr to stdout + + Possible values: `true`, `false` + +* `--sim-only` — Simulation the transaction only write the base64 to stdout Possible values: `true`, `false`