Skip to content

Commit

Permalink
fix(extrinsics): handling extrinsics to be methods of the cli structs
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 d799a8d commit b712746
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 306 deletions.
192 changes: 97 additions & 95 deletions crates/cargo-contract/src/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,106 +69,108 @@ impl CallCommand {
pub fn output_json(&self) -> bool {
self.output_json
}
}
pub async fn handle_call(call_command: &CallCommand) -> Result<(), ErrorVariant> {
let extrinsic_opts = ExtrinsicOptsBuilder::default()
.file(call_command.extrinsic_cli_opts.file.clone())
.manifest_path(call_command.extrinsic_cli_opts.manifest_path.clone())
.url(call_command.extrinsic_cli_opts.url.clone())
.suri(call_command.extrinsic_cli_opts.suri.clone())
.storage_deposit_limit(
call_command
.extrinsic_cli_opts
.storage_deposit_limit
.clone(),
)
.done();
let call_exec = CallCommandBuilder::default()
.contract(call_command.contract.clone())
.message(call_command.message.clone())
.args(call_command.args.clone())
.extrinsic_opts(extrinsic_opts)
.gas_limit(call_command.gas_limit)
.proof_size(call_command.proof_size)
.value(call_command.value.clone())
.done()
.await?;

if !call_command.extrinsic_cli_opts.execute {
let result = call_exec.call_dry_run().await?;
match result.result {
Ok(ref ret_val) => {
let value = call_exec
.transcoder()
.decode_message_return(call_exec.message(), &mut &ret_val.data[..])
.context(format!("Failed to decode return value {:?}", &ret_val))?;
let dry_run_result = CallDryRunResult {
reverted: ret_val.did_revert(),
data: value,
gas_consumed: result.gas_consumed,
gas_required: result.gas_required,
storage_deposit: StorageDeposit::from(&result.storage_deposit),
};
if call_command.output_json() {
println!("{}", dry_run_result.to_json()?);
} else {
dry_run_result.print();
display_contract_exec_result_debug::<_, DEFAULT_KEY_COL_WIDTH>(
&result,
)?;
display_dry_run_result_warning("message");
};
}
Err(ref err) => {
let metadata = call_exec.client().metadata();
let object = ErrorVariant::from_dispatch_error(err, &metadata)?;
if call_command.output_json() {
return Err(object)
} else {
name_value_println!("Result", object, MAX_KEY_COL_WIDTH);
display_contract_exec_result::<_, MAX_KEY_COL_WIDTH>(&result)?;
pub async fn handle(&self) -> Result<(), ErrorVariant> {
let extrinsic_opts = ExtrinsicOptsBuilder::default()
.file(self.extrinsic_cli_opts.file.clone())
.manifest_path(self.extrinsic_cli_opts.manifest_path.clone())
.url(self.extrinsic_cli_opts.url.clone())
.suri(self.extrinsic_cli_opts.suri.clone())
.storage_deposit_limit(self.extrinsic_cli_opts.storage_deposit_limit.clone())
.done();
let call_exec = CallCommandBuilder::default()
.contract(self.contract.clone())
.message(self.message.clone())
.args(self.args.clone())
.extrinsic_opts(extrinsic_opts)
.gas_limit(self.gas_limit)
.proof_size(self.proof_size)
.value(self.value.clone())
.done()
.await?;

if !self.extrinsic_cli_opts.execute {
let result = call_exec.call_dry_run().await?;
match result.result {
Ok(ref ret_val) => {
let value = call_exec
.transcoder()
.decode_message_return(
call_exec.message(),
&mut &ret_val.data[..],
)
.context(format!(
"Failed to decode return value {:?}",
&ret_val
))?;
let dry_run_result = CallDryRunResult {
reverted: ret_val.did_revert(),
data: value,
gas_consumed: result.gas_consumed,
gas_required: result.gas_required,
storage_deposit: StorageDeposit::from(&result.storage_deposit),
};
if self.output_json() {
println!("{}", dry_run_result.to_json()?);
} else {
dry_run_result.print();
display_contract_exec_result_debug::<_, DEFAULT_KEY_COL_WIDTH>(
&result,
)?;
display_dry_run_result_warning("message");
};
}
Err(ref err) => {
let metadata = call_exec.client().metadata();
let object = ErrorVariant::from_dispatch_error(err, &metadata)?;
if self.output_json() {
return Err(object)
} else {
name_value_println!("Result", object, MAX_KEY_COL_WIDTH);
display_contract_exec_result::<_, MAX_KEY_COL_WIDTH>(&result)?;
}
}
}
}
} else {
let gas_limit = pre_submit_dry_run_gas_estimate_call(
&call_exec,
call_command.output_json(),
call_command.extrinsic_cli_opts.skip_dry_run,
)
.await?;
if !call_command.extrinsic_cli_opts.skip_confirm {
prompt_confirm_tx(|| {
name_value_println!(
"Message",
call_exec.message(),
DEFAULT_KEY_COL_WIDTH
);
name_value_println!(
"Args",
call_exec.args().join(" "),
DEFAULT_KEY_COL_WIDTH
);
name_value_println!(
"Gas limit",
gas_limit.to_string(),
DEFAULT_KEY_COL_WIDTH
);
})?;
}
let token_metadata = TokenMetadata::query(call_exec.client()).await?;
let display_events = call_exec.call(Some(gas_limit)).await?;
let output = if call_command.output_json() {
display_events.to_json()?
} else {
display_events.display_events(
call_command.extrinsic_cli_opts.verbosity().unwrap(),
&token_metadata,
)?
};
println!("{output}");
let gas_limit = pre_submit_dry_run_gas_estimate_call(
&call_exec,
self.output_json(),
self.extrinsic_cli_opts.skip_dry_run,
)
.await?;
if !self.extrinsic_cli_opts.skip_confirm {
prompt_confirm_tx(|| {
name_value_println!(
"Message",
call_exec.message(),
DEFAULT_KEY_COL_WIDTH
);
name_value_println!(
"Args",
call_exec.args().join(" "),
DEFAULT_KEY_COL_WIDTH
);
name_value_println!(
"Gas limit",
gas_limit.to_string(),
DEFAULT_KEY_COL_WIDTH
);
})?;
}
let token_metadata = TokenMetadata::query(call_exec.client()).await?;
let display_events = call_exec.call(Some(gas_limit)).await?;
let output = if self.output_json() {
display_events.to_json()?
} else {
display_events.display_events(
self.extrinsic_cli_opts.verbosity().unwrap(),
&token_metadata,
)?
};
println!("{output}");
}
Ok(())
}
Ok(())
}

/// A helper function to estimate the gas required for a contract call.
Expand Down
149 changes: 72 additions & 77 deletions crates/cargo-contract/src/cmd/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,89 +77,84 @@ impl InstantiateCommand {
pub fn output_json(&self) -> bool {
self.output_json
}
}

pub async fn handle_instantiate(
instantiate_command: &InstantiateCommand,
) -> Result<(), ErrorVariant> {
let extrinsic_opts = ExtrinsicOptsBuilder::default()
.file(instantiate_command.extrinsic_cli_opts.file.clone())
.manifest_path(instantiate_command.extrinsic_cli_opts.manifest_path.clone())
.url(instantiate_command.extrinsic_cli_opts.url.clone())
.suri(instantiate_command.extrinsic_cli_opts.suri.clone())
.storage_deposit_limit(
instantiate_command
.extrinsic_cli_opts
.storage_deposit_limit
.clone(),
)
.done();
let instantiate_exec = InstantiateCommandBuilder::default()
.constructor(instantiate_command.constructor.clone())
.args(instantiate_command.args.clone())
.extrinsic_opts(extrinsic_opts)
.value(instantiate_command.value.clone())
.gas_limit(instantiate_command.gas_limit)
.proof_size(instantiate_command.proof_size)
.salt(instantiate_command.salt.clone())
.done()
.await?;
pub async fn handle(&self) -> Result<(), ErrorVariant> {
let extrinsic_opts = ExtrinsicOptsBuilder::default()
.file(self.extrinsic_cli_opts.file.clone())
.manifest_path(self.extrinsic_cli_opts.manifest_path.clone())
.url(self.extrinsic_cli_opts.url.clone())
.suri(self.extrinsic_cli_opts.suri.clone())
.storage_deposit_limit(self.extrinsic_cli_opts.storage_deposit_limit.clone())
.done();
let instantiate_exec = InstantiateCommandBuilder::default()
.constructor(self.constructor.clone())
.args(self.args.clone())
.extrinsic_opts(extrinsic_opts)
.value(self.value.clone())
.gas_limit(self.gas_limit)
.proof_size(self.proof_size)
.salt(self.salt.clone())
.done()
.await?;

if !instantiate_command.extrinsic_cli_opts.execute {
let result = instantiate_exec.instantiate_dry_run().await?;
match instantiate_exec.decode_instantiate_dry_run(&result).await {
Ok(dry_run_result) => {
if instantiate_command.output_json() {
println!("{}", dry_run_result.to_json()?);
} else {
print_instantiate_dry_run_result(&dry_run_result);
display_contract_exec_result_debug::<_, DEFAULT_KEY_COL_WIDTH>(
&result,
)?;
display_dry_run_result_warning("instantiate");
if !self.extrinsic_cli_opts.execute {
let result = instantiate_exec.instantiate_dry_run().await?;
match instantiate_exec.decode_instantiate_dry_run(&result).await {
Ok(dry_run_result) => {
if self.output_json() {
println!("{}", dry_run_result.to_json()?);
} else {
print_instantiate_dry_run_result(&dry_run_result);
display_contract_exec_result_debug::<_, DEFAULT_KEY_COL_WIDTH>(
&result,
)?;
display_dry_run_result_warning("instantiate");
}
Ok(())
}
Ok(())
}
Err(object) => {
if instantiate_command.output_json() {
return Err(object)
} else {
name_value_println!("Result", object, MAX_KEY_COL_WIDTH);
display_contract_exec_result::<_, MAX_KEY_COL_WIDTH>(&result)?;
Err(object) => {
if self.output_json() {
return Err(object)
} else {
name_value_println!("Result", object, MAX_KEY_COL_WIDTH);
display_contract_exec_result::<_, MAX_KEY_COL_WIDTH>(&result)?;
}
Err(object)
}
Err(object)
}
} else {
tracing::debug!("instantiate data {:?}", instantiate_exec.args().data());
let gas_limit = pre_submit_dry_run_gas_estimate_instantiate(
&instantiate_exec,
self.output_json(),
self.extrinsic_cli_opts.skip_dry_run,
)
.await?;
if !self.extrinsic_cli_opts.skip_confirm {
prompt_confirm_tx(|| {
print_default_instantiate_preview(&instantiate_exec, gas_limit);
if let Code::Existing(code_hash) =
instantiate_exec.args().code().clone()
{
name_value_println!(
"Code hash",
format!("{code_hash:?}"),
DEFAULT_KEY_COL_WIDTH
);
}
})?;
}
let instantiate_result =
instantiate_exec.instantiate(Some(gas_limit)).await?;
display_result(
&instantiate_exec,
instantiate_result,
self.output_json(),
self.extrinsic_cli_opts.verbosity().unwrap(),
)
.await?;
Ok(())
}
} else {
tracing::debug!("instantiate data {:?}", instantiate_exec.args().data());
let gas_limit = pre_submit_dry_run_gas_estimate_instantiate(
&instantiate_exec,
instantiate_command.output_json(),
instantiate_command.extrinsic_cli_opts.skip_dry_run,
)
.await?;
if !instantiate_command.extrinsic_cli_opts.skip_confirm {
prompt_confirm_tx(|| {
print_default_instantiate_preview(&instantiate_exec, gas_limit);
if let Code::Existing(code_hash) = instantiate_exec.args().code().clone()
{
name_value_println!(
"Code hash",
format!("{code_hash:?}"),
DEFAULT_KEY_COL_WIDTH
);
}
})?;
}
let instantiate_result = instantiate_exec.instantiate(Some(gas_limit)).await?;
display_result(
&instantiate_exec,
instantiate_result,
instantiate_command.output_json(),
instantiate_command.extrinsic_cli_opts.verbosity().unwrap(),
)
.await?;
Ok(())
}
}

Expand Down
20 changes: 4 additions & 16 deletions crates/cargo-contract/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,12 @@ pub(crate) use self::{
BuildCommand,
CheckCommand,
},
call::{
handle_call,
CallCommand,
},
call::CallCommand,
decode::DecodeCommand,
info::InfoCommand,
instantiate::{
handle_instantiate,
InstantiateCommand,
},
remove::{
handle_remove,
RemoveCommand,
},
upload::{
handle_upload,
UploadCommand,
},
instantiate::InstantiateCommand,
remove::RemoveCommand,
upload::UploadCommand,
};

use crate::{
Expand Down
Loading

0 comments on commit b712746

Please sign in to comment.