use super::{
display_contract_exec_result,
display_events,
error_details,
parse_balance,
prompt_confirm_tx,
runtime_api::api,
state_call,
submit_extrinsic,
Balance,
Client,
CodeHash,
ContractAccount,
ContractMessageTranscoder,
CrateMetadata,
DefaultConfig,
ExtrinsicOpts,
PairSigner,
MAX_KEY_COL_WIDTH,
};
use crate::{
name_value_println,
util::decode_hex,
Verbosity,
DEFAULT_KEY_COL_WIDTH,
};
use anyhow::{
anyhow,
Context,
Result,
};
use pallet_contracts_primitives::ContractInstantiateResult;
use scale::Encode;
use sp_core::{
crypto::Ss58Codec,
Bytes,
};
use std::{
fs,
path::{
Path,
PathBuf,
},
};
use subxt::{
Config,
OnlineClient,
};
#[derive(Debug, clap::Args)]
pub struct InstantiateCommand {
#[clap(parse(from_os_str))]
wasm_path: Option<PathBuf>,
#[clap(long, parse(try_from_str = parse_code_hash))]
code_hash: Option<<DefaultConfig as Config>::Hash>,
#[clap(name = "constructor", long, default_value = "new")]
constructor: String,
#[clap(long, multiple_values = true)]
args: Vec<String>,
#[clap(flatten)]
extrinsic_opts: ExtrinsicOpts,
#[clap(name = "value", long, default_value = "0", parse(try_from_str = parse_balance))]
value: Balance,
#[clap(name = "gas", long)]
gas_limit: Option<u64>,
#[clap(long, parse(try_from_str = parse_hex_bytes))]
salt: Option<Bytes>,
}
fn parse_code_hash(input: &str) -> Result<<DefaultConfig as Config>::Hash> {
let bytes = decode_hex(input)?;
if bytes.len() != 32 {
anyhow::bail!("Code hash should be 32 bytes in length")
}
let mut arr = [0u8; 32];
arr.copy_from_slice(&bytes);
Ok(arr.into())
}
fn parse_hex_bytes(input: &str) -> Result<Bytes> {
let bytes = decode_hex(input)?;
Ok(bytes.into())
}
impl InstantiateCommand {
pub fn run(&self) -> Result<()> {
let crate_metadata = CrateMetadata::from_manifest_path(
self.extrinsic_opts.manifest_path.as_ref(),
)?;
let transcoder = ContractMessageTranscoder::load(crate_metadata.metadata_path())?;
let data = transcoder.encode(&self.constructor, &self.args)?;
let signer = super::pair_signer(self.extrinsic_opts.signer()?);
let url = self.extrinsic_opts.url_to_string();
let verbosity = self.extrinsic_opts.verbosity()?;
fn load_code(wasm_path: &Path) -> Result<Code> {
tracing::debug!("Contract code path: {}", wasm_path.display());
let code = fs::read(&wasm_path)
.context(format!("Failed to read from {}", wasm_path.display()))?;
Ok(Code::Upload(code))
}
let code = match (self.wasm_path.as_ref(), self.code_hash.as_ref()) {
(Some(_), Some(_)) => {
Err(anyhow!(
"Specify either `--wasm-path` or `--code-hash` but not both"
))
}
(Some(wasm_path), None) => load_code(wasm_path),
(None, None) => {
load_code(&crate_metadata.dest_wasm)
}
(None, Some(code_hash)) => Ok(Code::Existing(*code_hash)),
}?;
let salt = self.salt.clone().map(|s| s.0).unwrap_or_default();
let args = InstantiateArgs {
constructor: self.constructor.clone(),
raw_args: self.args.clone(),
value: self.value,
gas_limit: self.gas_limit,
storage_deposit_limit: self.extrinsic_opts.storage_deposit_limit,
data,
salt,
};
async_std::task::block_on(async move {
let client = OnlineClient::from_url(url.clone()).await?;
let exec = Exec {
args,
opts: self.extrinsic_opts.clone(),
url,
client,
verbosity,
signer,
transcoder,
};
exec.exec(code, self.extrinsic_opts.dry_run).await
})
}
}
struct InstantiateArgs {
constructor: String,
raw_args: Vec<String>,
value: Balance,
gas_limit: Option<u64>,
storage_deposit_limit: Option<Balance>,
data: Vec<u8>,
salt: Vec<u8>,
}
pub struct Exec {
opts: ExtrinsicOpts,
args: InstantiateArgs,
verbosity: Verbosity,
url: String,
client: Client,
signer: PairSigner,
transcoder: ContractMessageTranscoder,
}
impl Exec {
async fn exec(&self, code: Code, dry_run: bool) -> Result<()> {
tracing::debug!("instantiate data {:?}", self.args.data);
if dry_run {
let result = self.instantiate_dry_run(code).await?;
match result.result {
Ok(ref ret_val) => {
name_value_println!(
"Result",
String::from("Success!"),
DEFAULT_KEY_COL_WIDTH
);
name_value_println!(
"Contract",
ret_val.account_id.to_ss58check(),
DEFAULT_KEY_COL_WIDTH
);
name_value_println!(
"Reverted",
format!("{:?}", ret_val.result.did_revert()),
DEFAULT_KEY_COL_WIDTH
);
name_value_println!(
"Data",
format!("{:?}", ret_val.result.data),
DEFAULT_KEY_COL_WIDTH
);
display_contract_exec_result::<_, DEFAULT_KEY_COL_WIDTH>(&result)
}
Err(ref err) => {
let metadata = self.client.metadata();
let err = error_details(err, &metadata)?;
name_value_println!("Result", err, MAX_KEY_COL_WIDTH);
display_contract_exec_result::<_, MAX_KEY_COL_WIDTH>(&result)
}
}
} else {
match code {
Code::Upload(code) => {
let (code_hash, contract_account) =
self.instantiate_with_code(code).await?;
if let Some(code_hash) = code_hash {
name_value_println!("Code hash", format!("{:?}", code_hash));
}
name_value_println!("Contract", contract_account.to_ss58check());
}
Code::Existing(code_hash) => {
let contract_account = self.instantiate(code_hash).await?;
name_value_println!("Contract", contract_account.to_ss58check());
}
}
Ok(())
}
}
async fn instantiate_with_code(
&self,
code: Vec<u8>,
) -> Result<(Option<CodeHash>, ContractAccount)> {
let gas_limit = self
.pre_submit_dry_run_gas_estimate(Code::Upload(code.clone()))
.await?;
if !self.opts.skip_confirm {
prompt_confirm_tx(|| self.print_default_instantiate_preview(gas_limit))?;
}
let call = api::tx().contracts().instantiate_with_code(
self.args.value,
gas_limit,
self.args.storage_deposit_limit,
code.to_vec(),
self.args.data.clone(),
self.args.salt.clone(),
);
let result = submit_extrinsic(&self.client, &call, &self.signer).await?;
display_events(
&result,
&self.transcoder,
&self.client.metadata(),
&self.verbosity,
)?;
let code_hash = result
.find_first::<api::contracts::events::CodeStored>()?
.map(|code_stored| code_stored.code_hash);
let instantiated = result
.find_first::<api::contracts::events::Instantiated>()?
.ok_or_else(|| anyhow!("Failed to find Instantiated event"))?;
Ok((code_hash, instantiated.contract))
}
async fn instantiate(&self, code_hash: CodeHash) -> Result<ContractAccount> {
let gas_limit = self
.pre_submit_dry_run_gas_estimate(Code::Existing(code_hash))
.await?;
if !self.opts.skip_confirm {
prompt_confirm_tx(|| {
self.print_default_instantiate_preview(gas_limit);
name_value_println!(
"Code hash",
format!("{:?}", code_hash),
DEFAULT_KEY_COL_WIDTH
);
})?;
}
let call = api::tx().contracts().instantiate(
self.args.value,
gas_limit,
self.args.storage_deposit_limit,
code_hash,
self.args.data.clone(),
self.args.salt.clone(),
);
let result = submit_extrinsic(&self.client, &call, &self.signer).await?;
display_events(
&result,
&self.transcoder,
&self.client.metadata(),
&self.verbosity,
)?;
let instantiated = result
.find_first::<api::contracts::events::Instantiated>()?
.ok_or_else(|| anyhow!("Failed to find Instantiated event"))?;
Ok(instantiated.contract)
}
fn print_default_instantiate_preview(&self, gas_limit: u64) {
name_value_println!("Constructor", self.args.constructor, DEFAULT_KEY_COL_WIDTH);
name_value_println!("Args", self.args.raw_args.join(" "), DEFAULT_KEY_COL_WIDTH);
name_value_println!("Gas limit", gas_limit.to_string(), DEFAULT_KEY_COL_WIDTH);
}
async fn instantiate_dry_run(
&self,
code: Code,
) -> Result<ContractInstantiateResult<<DefaultConfig as Config>::AccountId, Balance>>
{
let gas_limit = *self.args.gas_limit.as_ref().unwrap_or(&5_000_000_000_000);
let storage_deposit_limit = self.args.storage_deposit_limit;
let call_request = InstantiateRequest {
origin: self.signer.account_id().clone(),
value: self.args.value,
gas_limit,
storage_deposit_limit,
code,
data: self.args.data.clone(),
salt: self.args.salt.clone(),
};
state_call(&self.url, "ContractsApi_instantiate", &call_request).await
}
async fn pre_submit_dry_run_gas_estimate(&self, code: Code) -> Result<u64> {
if self.opts.skip_dry_run {
return match self.args.gas_limit {
Some(gas) => Ok(gas),
None => {
Err(anyhow!(
"Gas limit `--gas` argument required if `--skip-dry-run` specified"
))
}
}
}
super::print_dry_running_status(&self.args.constructor);
let instantiate_result = self.instantiate_dry_run(code).await?;
match instantiate_result.result {
Ok(_) => {
super::print_gas_required_success(instantiate_result.gas_required);
let gas_limit = self
.args
.gas_limit
.unwrap_or(instantiate_result.gas_required);
Ok(gas_limit)
}
Err(ref err) => {
let err = error_details(err, &self.client.metadata())?;
name_value_println!("Result", err, MAX_KEY_COL_WIDTH);
display_contract_exec_result::<_, MAX_KEY_COL_WIDTH>(
&instantiate_result,
)?;
Err(anyhow!("Pre-submission dry-run failed. Use --skip-dry-run to skip this step."))
}
}
}
}
#[derive(Encode)]
struct InstantiateRequest {
origin: <DefaultConfig as Config>::AccountId,
value: Balance,
gas_limit: u64,
storage_deposit_limit: Option<Balance>,
code: Code,
data: Vec<u8>,
salt: Vec<u8>,
}
#[derive(Encode)]
enum Code {
Upload(Vec<u8>),
Existing(<DefaultConfig as Config>::Hash),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_code_hash_works() {
assert!(parse_code_hash(
"0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"
)
.is_ok());
assert!(parse_code_hash(
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"
)
.is_ok())
}
}