use super::{
display_contract_exec_result,
parse_balance,
prompt_confirm_tx,
runtime_api::{
api,
Weight,
},
state_call,
submit_extrinsic,
Balance,
Client,
ContractMessageTranscoder,
CrateMetadata,
DefaultConfig,
ExtrinsicOpts,
PairSigner,
MAX_KEY_COL_WIDTH,
};
use crate::{
cmd::extrinsics::{
display_contract_exec_result_debug,
events::DisplayEvents,
ErrorVariant,
},
name_value_println,
DEFAULT_KEY_COL_WIDTH,
};
use anyhow::{
anyhow,
Result,
};
use pallet_contracts_primitives::{
ContractExecResult,
StorageDeposit,
};
use scale::Encode;
use transcode::Value;
use std::fmt::Debug;
use subxt::{
Config,
OnlineClient,
};
#[derive(Debug, clap::Args)]
#[clap(name = "call", about = "Call a contract")]
pub struct CallCommand {
#[clap(name = "contract", long, env = "CONTRACT")]
contract: <DefaultConfig as Config>::AccountId,
#[clap(long, short)]
message: String,
#[clap(long, multiple_values = true)]
args: Vec<String>,
#[clap(flatten)]
extrinsic_opts: ExtrinsicOpts,
#[clap(name = "gas", long)]
gas_limit: Option<u64>,
#[clap(name = "value", long, parse(try_from_str = parse_balance), default_value = "0")]
value: Balance,
#[clap(long, conflicts_with = "verbose")]
output_json: bool,
}
impl CallCommand {
pub fn is_json(&self) -> bool {
self.output_json
}
pub fn run(&self) -> Result<(), ErrorVariant> {
let crate_metadata = CrateMetadata::from_manifest_path(
self.extrinsic_opts.manifest_path.as_ref(),
)?;
let transcoder = ContractMessageTranscoder::load(crate_metadata.metadata_path())?;
let call_data = transcoder.encode(&self.message, &self.args)?;
tracing::debug!("Message data: {:?}", hex::encode(&call_data));
let signer = super::pair_signer(self.extrinsic_opts.signer()?);
async_std::task::block_on(async {
let url = self.extrinsic_opts.url_to_string();
let client = OnlineClient::from_url(url.clone()).await?;
if self.extrinsic_opts.dry_run {
let result = self.call_dry_run(call_data, &signer).await?;
match result.result {
Ok(ref ret_val) => {
let value = transcoder
.decode_return(&self.message, &mut &ret_val.data.0[..])?;
let dry_run_result = CallDryRunResult {
result: String::from("Success!"),
reverted: ret_val.did_revert(),
data: value,
gas_consumed: result.gas_consumed,
gas_required: result.gas_required,
storage_deposit: result.storage_deposit.clone(),
};
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,
)?;
}
Ok(())
}
Err(ref err) => {
let metadata = client.metadata();
let object = ErrorVariant::from_dispatch_error(err, &metadata)?;
if self.output_json {
Err(object)
} else {
name_value_println!("Result", object, MAX_KEY_COL_WIDTH);
display_contract_exec_result::<_, MAX_KEY_COL_WIDTH>(
&result,
)?;
Ok(())
}
}
}
} else {
self.call(&client, call_data, &signer, &transcoder).await
}
})
}
async fn call_dry_run(
&self,
input_data: Vec<u8>,
signer: &PairSigner,
) -> Result<ContractExecResult<Balance>> {
let url = self.extrinsic_opts.url_to_string();
let gas_limit = *self.gas_limit.as_ref().unwrap_or(&5_000_000_000_000);
let storage_deposit_limit = self.extrinsic_opts.storage_deposit_limit;
let call_request = CallRequest {
origin: signer.account_id().clone(),
dest: self.contract.clone(),
value: self.value,
gas_limit: Weight::from_ref_time(gas_limit),
storage_deposit_limit,
input_data,
};
state_call(&url, "ContractsApi_call", call_request).await
}
async fn call(
&self,
client: &Client,
data: Vec<u8>,
signer: &PairSigner,
transcoder: &ContractMessageTranscoder,
) -> Result<(), ErrorVariant> {
tracing::debug!("calling contract {:?}", self.contract);
let gas_limit = self
.pre_submit_dry_run_gas_estimate(client, data.clone(), signer)
.await?;
if !self.extrinsic_opts.skip_confirm {
prompt_confirm_tx(|| {
name_value_println!("Message", self.message, DEFAULT_KEY_COL_WIDTH);
name_value_println!("Args", self.args.join(" "), DEFAULT_KEY_COL_WIDTH);
name_value_println!(
"Gas limit",
gas_limit.to_string(),
DEFAULT_KEY_COL_WIDTH
);
})?;
}
let call = api::tx().contracts().call(
self.contract.clone().into(),
self.value,
gas_limit,
self.extrinsic_opts.storage_deposit_limit,
data,
);
let result = submit_extrinsic(client, &call, signer).await?;
let display_events =
DisplayEvents::from_events(&result, transcoder, &client.metadata())?;
let output = if self.output_json {
display_events.to_json()?
} else {
display_events.display_events(self.extrinsic_opts.verbosity()?)
};
println!("{}", output);
Ok(())
}
async fn pre_submit_dry_run_gas_estimate(
&self,
client: &Client,
data: Vec<u8>,
signer: &PairSigner,
) -> Result<Weight> {
if self.extrinsic_opts.skip_dry_run {
return match self.gas_limit {
Some(gas) => Ok(Weight::from_ref_time(gas)),
None => {
Err(anyhow!(
"Gas limit `--gas` argument required if `--skip-dry-run` specified"
))
}
}
}
if !self.output_json {
super::print_dry_running_status(&self.message);
}
let call_result = self.call_dry_run(data, signer).await?;
match call_result.result {
Ok(_) => {
if !self.output_json {
super::print_gas_required_success(call_result.gas_required);
}
let gas_limit = self.gas_limit.unwrap_or(call_result.gas_required);
Ok(Weight::from_ref_time(gas_limit))
}
Err(ref err) => {
let object = ErrorVariant::from_dispatch_error(err, &client.metadata())?;
if self.output_json {
Err(anyhow!("{}", serde_json::to_string_pretty(&object)?))
} else {
name_value_println!("Result", object, MAX_KEY_COL_WIDTH);
display_contract_exec_result::<_, MAX_KEY_COL_WIDTH>(&call_result)?;
Err(anyhow!("Pre-submission dry-run failed. Use --skip-dry-run to skip this step."))
}
}
}
}
}
#[derive(Encode)]
pub struct CallRequest {
origin: <DefaultConfig as Config>::AccountId,
dest: <DefaultConfig as Config>::AccountId,
value: Balance,
gas_limit: Weight,
storage_deposit_limit: Option<Balance>,
input_data: Vec<u8>,
}
#[derive(serde::Serialize)]
pub struct CallDryRunResult {
pub result: String,
pub reverted: bool,
pub data: Value,
pub gas_consumed: u64,
pub gas_required: u64,
pub storage_deposit: StorageDeposit<Balance>,
}
impl CallDryRunResult {
pub fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string_pretty(self)?)
}
pub fn print(&self) {
name_value_println!("Result", self.result, DEFAULT_KEY_COL_WIDTH);
name_value_println!(
"Reverted",
format!("{:?}", self.reverted),
DEFAULT_KEY_COL_WIDTH
);
name_value_println!("Data", format!("{:?}", self.data), DEFAULT_KEY_COL_WIDTH);
}
}