diff --git a/crates/iota/src/client_commands.rs b/crates/iota/src/client_commands.rs index 910cc4075ec..04a78ce9dfc 100644 --- a/crates/iota/src/client_commands.rs +++ b/crates/iota/src/client_commands.rs @@ -1723,8 +1723,7 @@ impl IotaClientCommands { IotaClientCommandResult::NoOutput } }; - let client = context.get_client().await?; - Ok(ret.prerender_clever_errors(client.read_api()).await) + Ok(ret.prerender_clever_errors(context).await) } pub fn switch_env(config: &mut IotaClientConfig, env: &str) -> Result<(), anyhow::Error> { @@ -2384,14 +2383,16 @@ impl IotaClientCommandResult { } } - pub async fn prerender_clever_errors(mut self, read_api: &ReadApi) -> Self { + pub async fn prerender_clever_errors(mut self, context: &mut WalletContext) -> Self { match &mut self { IotaClientCommandResult::DryRun(DryRunTransactionBlockResponse { effects, .. }) | IotaClientCommandResult::TransactionBlock(IotaTransactionBlockResponse { effects: Some(effects), .. - }) => prerender_clever_errors(effects, read_api).await, - + }) => { + let client = context.get_client().await.expect("Cannot connect to RPC"); + prerender_clever_errors(effects, client.read_api()).await + } IotaClientCommandResult::TransactionBlock(IotaTransactionBlockResponse { effects: None, .. @@ -2777,7 +2778,7 @@ fn format_balance( /// Helper function to reduce code duplication for executing dry run pub async fn execute_dry_run( - client: &IotaClient, + context: &mut WalletContext, signer: IotaAddress, kind: TransactionKind, gas_budget: Option, @@ -2785,9 +2786,10 @@ pub async fn execute_dry_run( gas_payment: Option>, sponsor: Option, ) -> Result { + let client = context.get_client().await?; let gas_budget = match gas_budget { Some(gas_budget) => gas_budget, - None => max_gas_budget(client).await?, + None => max_gas_budget(&client).await?, }; let dry_run_tx_data = client .transaction_builder() @@ -2799,7 +2801,7 @@ pub async fn execute_dry_run( .await .map_err(|e| anyhow!("Dry run failed: {e}"))?; let resp = IotaClientCommandResult::DryRun(response) - .prerender_clever_errors(client.read_api()) + .prerender_clever_errors(context) .await; Ok(resp) } @@ -2815,15 +2817,16 @@ pub async fn execute_dry_run( /// This gas estimate is computed exactly as in the TypeScript SDK /// pub async fn estimate_gas_budget( - client: &IotaClient, + context: &mut WalletContext, signer: IotaAddress, kind: TransactionKind, gas_price: u64, gas_payment: Option>, sponsor: Option, ) -> Result { + let client = context.get_client().await?; let Ok(IotaClientCommandResult::DryRun(dry_run)) = - execute_dry_run(client, signer, kind, None, gas_price, gas_payment, sponsor).await + execute_dry_run(context, signer, kind, None, gas_price, gas_payment, sponsor).await else { bail!( "Could not automatically determine the gas budget. Please supply one using the --gas-budget flag." @@ -2901,7 +2904,7 @@ pub(crate) async fn dry_run_or_execute_or_serialize( let client = context.get_client().await?; if dry_run { return execute_dry_run( - &client, + context, signer, tx_kind, gas_budget, @@ -2916,7 +2919,7 @@ pub(crate) async fn dry_run_or_execute_or_serialize( Some(gas_budget) => gas_budget, None => { estimate_gas_budget( - &client, + context, signer, tx_kind.clone(), gas_price, diff --git a/crates/iota/src/iota_commands.rs b/crates/iota/src/iota_commands.rs index 36030ac3b38..916176ae3e7 100644 --- a/crates/iota/src/iota_commands.rs +++ b/crates/iota/src/iota_commands.rs @@ -456,8 +456,8 @@ impl IotaCommand { !matches!(cmd, Some(IotaClientCommands::NewAddress { .. })), ) .await?; - let mut context = WalletContext::new(&config_path, None, None)?; if let Some(cmd) = cmd { + let mut context = WalletContext::new(&config_path, None, None)?; cmd.execute(&mut context).await?.print(!json); } else { // Print help diff --git a/crates/iota/tests/cli_tests.rs b/crates/iota/tests/cli_tests.rs index aaded6cecdb..8d90368ad9d 100644 --- a/crates/iota/tests/cli_tests.rs +++ b/crates/iota/tests/cli_tests.rs @@ -3853,7 +3853,7 @@ async fn test_gas_estimation() -> Result<(), anyhow::Error> { let sender = context.active_address().unwrap(); let tx_builder = client.transaction_builder(); let tx_kind = tx_builder.transfer_iota_tx_kind(address2, Some(amount)); - let gas_estimate = estimate_gas_budget(&client, sender, tx_kind, rgp, None, None).await; + let gas_estimate = estimate_gas_budget(context, sender, tx_kind, rgp, None, None).await; assert!(gas_estimate.is_ok()); let transfer_iota_cmd = IotaClientCommands::TransferIota {