From d799a8d9bee32b5cd37ba5cf59255899dfecaa7b Mon Sep 17 00:00:00 2001 From: Tarek Date: Wed, 30 Aug 2023 11:56:38 +0300 Subject: [PATCH] fix(extrinsics): return Result<> to avoid unwrapping Signed-off-by: Tarek --- crates/cargo-contract/src/cmd/call.rs | 2 +- crates/cargo-contract/src/cmd/instantiate.rs | 2 +- crates/cargo-contract/src/cmd/remove.rs | 2 +- crates/cargo-contract/src/cmd/upload.rs | 2 +- crates/extrinsics/src/call.rs | 18 ++++++------- crates/extrinsics/src/instantiate.rs | 27 +++++++++----------- crates/extrinsics/src/remove.rs | 16 ++++++------ crates/extrinsics/src/upload.rs | 27 +++++++++----------- 8 files changed, 44 insertions(+), 52 deletions(-) diff --git a/crates/cargo-contract/src/cmd/call.rs b/crates/cargo-contract/src/cmd/call.rs index d69ddf31c..6b9b30058 100644 --- a/crates/cargo-contract/src/cmd/call.rs +++ b/crates/cargo-contract/src/cmd/call.rs @@ -92,7 +92,7 @@ pub async fn handle_call(call_command: &CallCommand) -> Result<(), ErrorVariant> .proof_size(call_command.proof_size) .value(call_command.value.clone()) .done() - .await; + .await?; if !call_command.extrinsic_cli_opts.execute { let result = call_exec.call_dry_run().await?; diff --git a/crates/cargo-contract/src/cmd/instantiate.rs b/crates/cargo-contract/src/cmd/instantiate.rs index f1f83adc1..8341a333b 100644 --- a/crates/cargo-contract/src/cmd/instantiate.rs +++ b/crates/cargo-contract/src/cmd/instantiate.rs @@ -103,7 +103,7 @@ pub async fn handle_instantiate( .proof_size(instantiate_command.proof_size) .salt(instantiate_command.salt.clone()) .done() - .await; + .await?; if !instantiate_command.extrinsic_cli_opts.execute { let result = instantiate_exec.instantiate_dry_run().await?; diff --git a/crates/cargo-contract/src/cmd/remove.rs b/crates/cargo-contract/src/cmd/remove.rs index ae33875c2..dd475f42c 100644 --- a/crates/cargo-contract/src/cmd/remove.rs +++ b/crates/cargo-contract/src/cmd/remove.rs @@ -50,7 +50,7 @@ pub async fn handle_remove(remove_command: &RemoveCommand) -> Result<(), ErrorVa .code_hash(remove_command.code_hash) .extrinsic_opts(extrinsic_opts) .done() - .await; + .await?; let remove_result = remove_exec.remove_code().await?; let display_events = remove_result.display_events; let output = if remove_command.output_json() { diff --git a/crates/cargo-contract/src/cmd/upload.rs b/crates/cargo-contract/src/cmd/upload.rs index 9c1234aa5..ee07f38c2 100644 --- a/crates/cargo-contract/src/cmd/upload.rs +++ b/crates/cargo-contract/src/cmd/upload.rs @@ -47,7 +47,7 @@ pub async fn handle_upload(upload_command: &UploadCommand) -> Result<(), ErrorVa let upload_exec = UploadCommandBuilder::default() .extrinsic_opts(extrinsic_opts) .done() - .await; + .await?; let code_hash = upload_exec.code().code_hash(); diff --git a/crates/extrinsics/src/call.rs b/crates/extrinsics/src/call.rs index 7f34ad7b0..e29fbece3 100644 --- a/crates/extrinsics/src/call.rs +++ b/crates/extrinsics/src/call.rs @@ -169,20 +169,18 @@ impl CallCommandBuilder { /// /// Returns the `CallExec` containing the preprocessed data for the contract call, /// or an error in case of failure. - pub async fn done(self) -> CallExec { - let artifacts = self.opts.extrinsic_opts.contract_artifacts().unwrap(); - let transcoder = artifacts.contract_transcoder().unwrap(); + pub async fn done(self) -> Result { + let artifacts = self.opts.extrinsic_opts.contract_artifacts()?; + let transcoder = artifacts.contract_transcoder()?; - let call_data = transcoder - .encode(&self.opts.message, &self.opts.args) - .unwrap(); + let call_data = transcoder.encode(&self.opts.message, &self.opts.args)?; tracing::debug!("Message data: {:?}", hex::encode(&call_data)); - let signer = self.opts.extrinsic_opts.signer().unwrap(); + let signer = self.opts.extrinsic_opts.signer()?; let url = self.opts.extrinsic_opts.url_to_string(); - let client = OnlineClient::from_url(url.clone()).await.unwrap(); - CallExec { + let client = OnlineClient::from_url(url.clone()).await?; + Ok(CallExec { contract: self.opts.contract.clone(), message: self.opts.message.clone(), args: self.opts.args.clone(), @@ -194,7 +192,7 @@ impl CallCommandBuilder { transcoder, call_data, signer, - } + }) } } diff --git a/crates/extrinsics/src/instantiate.rs b/crates/extrinsics/src/instantiate.rs index 1a8b6d616..178f9e087 100644 --- a/crates/extrinsics/src/instantiate.rs +++ b/crates/extrinsics/src/instantiate.rs @@ -160,30 +160,28 @@ impl InstantiateCommandBuilder { /// /// Returns the [`InstantiateExec`] containing the preprocessed data for the /// instantiation, or an error in case of failure. - pub async fn done(self) -> InstantiateExec { - let artifacts = self.opts.extrinsic_opts.contract_artifacts().unwrap(); - let transcoder = artifacts.contract_transcoder().unwrap(); - let data = transcoder - .encode(&self.opts.constructor, &self.opts.args) - .unwrap(); - let signer = self.opts.extrinsic_opts.signer().unwrap(); + pub async fn done(self) -> Result { + let artifacts = self.opts.extrinsic_opts.contract_artifacts()?; + let transcoder = artifacts.contract_transcoder()?; + let data = transcoder.encode(&self.opts.constructor, &self.opts.args)?; + let signer = self.opts.extrinsic_opts.signer()?; let url = self.opts.extrinsic_opts.url_to_string(); let code = if let Some(code) = artifacts.code { Code::Upload(code.0) } else { - let code_hash = artifacts.code_hash().unwrap(); + let code_hash = artifacts.code_hash()?; Code::Existing(code_hash.into()) }; let salt = self.opts.salt.clone().map(|s| s.0).unwrap_or_default(); - let client = OnlineClient::from_url(url.clone()).await.unwrap(); + let client = OnlineClient::from_url(url.clone()).await?; - let token_metadata = TokenMetadata::query(&client).await.unwrap(); + let token_metadata = TokenMetadata::query(&client).await?; let args = InstantiateArgs { constructor: self.opts.constructor.clone(), raw_args: self.opts.args.clone(), - value: self.opts.value.denominate_balance(&token_metadata).unwrap(), + value: self.opts.value.denominate_balance(&token_metadata)?, gas_limit: self.opts.gas_limit, proof_size: self.opts.proof_size, storage_deposit_limit: self @@ -192,21 +190,20 @@ impl InstantiateCommandBuilder { .storage_deposit_limit() .as_ref() .map(|bv| bv.denominate_balance(&token_metadata)) - .transpose() - .unwrap(), + .transpose()?, code, data, salt, }; - InstantiateExec { + Ok(InstantiateExec { args, opts: self.opts.extrinsic_opts.clone(), url, client, signer, transcoder, - } + }) } } diff --git a/crates/extrinsics/src/remove.rs b/crates/extrinsics/src/remove.rs index a25331d6f..47ebf14fb 100644 --- a/crates/extrinsics/src/remove.rs +++ b/crates/extrinsics/src/remove.rs @@ -101,10 +101,10 @@ impl RemoveCommandBuilder { /// /// Returns the `RemoveExec` containing the preprocessed data for the contract code /// removal, or an error in case of failure. - pub async fn done(self) -> RemoveExec { - let artifacts = self.opts.extrinsic_opts.contract_artifacts().unwrap(); - let transcoder = artifacts.contract_transcoder().unwrap(); - let signer = self.opts.extrinsic_opts.signer().unwrap(); + pub async fn done(self) -> Result { + let artifacts = self.opts.extrinsic_opts.contract_artifacts()?; + let transcoder = artifacts.contract_transcoder()?; + let signer = self.opts.extrinsic_opts.signer()?; let artifacts_path = artifacts.artifact_path().to_path_buf(); @@ -117,17 +117,17 @@ impl RemoveCommandBuilder { path for artifacts files with --manifest-path", artifacts_path.display() )), - }.unwrap(); + }?; let url = self.opts.extrinsic_opts.url_to_string(); - let client = OnlineClient::from_url(url.clone()).await.unwrap(); + let client = OnlineClient::from_url(url.clone()).await?; - RemoveExec { + Ok(RemoveExec { final_code_hash, opts: self.opts.extrinsic_opts.clone(), client, transcoder, signer, - } + }) } } diff --git a/crates/extrinsics/src/upload.rs b/crates/extrinsics/src/upload.rs index 332015c3e..70fb66cc1 100644 --- a/crates/extrinsics/src/upload.rs +++ b/crates/extrinsics/src/upload.rs @@ -94,28 +94,25 @@ impl UploadCommandBuilder { /// /// Returns the `UploadExec` containing the preprocessed data for the upload or /// execution. - pub async fn done(self) -> UploadExec { - let artifacts = self.opts.extrinsic_opts.contract_artifacts().unwrap(); - let signer = self.opts.extrinsic_opts.signer().unwrap(); + pub async fn done(self) -> Result { + let artifacts = self.opts.extrinsic_opts.contract_artifacts()?; + let signer = self.opts.extrinsic_opts.signer()?; let artifacts_path = artifacts.artifact_path().to_path_buf(); - let code = artifacts - .code - .ok_or_else(|| { - anyhow::anyhow!( - "Contract code not found from artifact file {}", - artifacts_path.display() - ) - }) - .unwrap(); + let code = artifacts.code.ok_or_else(|| { + anyhow::anyhow!( + "Contract code not found from artifact file {}", + artifacts_path.display() + ) + })?; let url = self.opts.extrinsic_opts.url_to_string(); - let client = OnlineClient::from_url(url.clone()).await.unwrap(); - UploadExec { + let client = OnlineClient::from_url(url.clone()).await?; + Ok(UploadExec { opts: self.opts.extrinsic_opts.clone(), client, code, signer, - } + }) } }