Skip to content

Commit

Permalink
fix(cli): don't attempt connection for client envs command (#4605)
Browse files Browse the repository at this point in the history
* fix(cli): don't attempt connection for `client envs` command

* remove test
  • Loading branch information
thibault-martinez authored Jan 6, 2025
1 parent 64e2e82 commit 049c74d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
27 changes: 15 additions & 12 deletions crates/iota/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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,
..
Expand Down Expand Up @@ -2777,17 +2778,18 @@ 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<u64>,
gas_price: u64,
gas_payment: Option<Vec<ObjectID>>,
sponsor: Option<IotaAddress>,
) -> Result<IotaClientCommandResult, anyhow::Error> {
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()
Expand All @@ -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)
}
Expand All @@ -2815,15 +2817,16 @@ pub async fn execute_dry_run(
/// This gas estimate is computed exactly as in the TypeScript SDK
/// <https://github.com/iotaledger/iota/blob/3c4369270605f78a243842098b7029daf8d883d9/sdk/typescript/src/transactions/TransactionBlock.ts#L845-L858>
pub async fn estimate_gas_budget(
client: &IotaClient,
context: &mut WalletContext,
signer: IotaAddress,
kind: TransactionKind,
gas_price: u64,
gas_payment: Option<Vec<ObjectID>>,
sponsor: Option<IotaAddress>,
) -> Result<u64, anyhow::Error> {
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."
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/iota/src/iota_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/iota/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 049c74d

Please sign in to comment.