Skip to content

Commit

Permalink
fix(extrinsics): return Result<> to avoid unwrapping
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Aug 30, 2023
1 parent a967332 commit d799a8d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 52 deletions.
2 changes: 1 addition & 1 deletion crates/cargo-contract/src/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-contract/src/cmd/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-contract/src/cmd/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-contract/src/cmd/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
18 changes: 8 additions & 10 deletions crates/extrinsics/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,18 @@ impl CallCommandBuilder<state::Message, state::ExtrinsicOptions> {
///
/// 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<CallExec> {
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(),
Expand All @@ -194,7 +192,7 @@ impl CallCommandBuilder<state::Message, state::ExtrinsicOptions> {
transcoder,
call_data,
signer,
}
})
}
}

Expand Down
27 changes: 12 additions & 15 deletions crates/extrinsics/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,30 +160,28 @@ impl InstantiateCommandBuilder<state::ExtrinsicOptions> {
///
/// 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<InstantiateExec> {
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
Expand All @@ -192,21 +190,20 @@ impl InstantiateCommandBuilder<state::ExtrinsicOptions> {
.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,
}
})
}
}

Expand Down
16 changes: 8 additions & 8 deletions crates/extrinsics/src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ impl RemoveCommandBuilder<state::ExtrinsicOptions> {
///
/// 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<RemoveExec> {
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();

Expand All @@ -117,17 +117,17 @@ impl RemoveCommandBuilder<state::ExtrinsicOptions> {
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,
}
})
}
}

Expand Down
27 changes: 12 additions & 15 deletions crates/extrinsics/src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,25 @@ impl UploadCommandBuilder<state::ExtrinsicOptions> {
///
/// 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<UploadExec> {
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,
}
})
}
}

Expand Down

0 comments on commit d799a8d

Please sign in to comment.