cargo-contract 1.5.0

Setup and deployment tool for developing Wasm based smart contracts via ink!
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
// This file is part of cargo-contract.
//
// cargo-contract is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// cargo-contract is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with cargo-contract.  If not, see <http://www.gnu.org/licenses/>.

use super::{
    display_contract_exec_result,
    display_events,
    load_metadata,
    parse_balance,
    prompt_confirm_tx,
    submit_extrinsic,
    Balance,
    Client,
    ContractMessageTranscoder,
    ContractsRpcError,
    DefaultConfig,
    ExtrinsicOpts,
    PairSigner,
    MAX_KEY_COL_WIDTH,
};
use crate::{
    name_value_println,
    DEFAULT_KEY_COL_WIDTH,
};
use anyhow::{
    anyhow,
    Result,
};
use jsonrpsee::{
    core::client::ClientT,
    rpc_params,
    ws_client::WsClientBuilder,
};
use pallet_contracts_primitives::{
    ContractResult,
    ExecReturnValue,
};
use serde::Serialize;
use sp_core::Bytes;
use std::{
    fmt::Debug,
    result,
};
use subxt::{
    rpc::NumberOrHex,
    Config,
    OnlineClient,
};

type ContractExecResult =
    ContractResult<result::Result<ExecReturnValue, ContractsRpcError>, Balance>;

#[derive(Debug, clap::Args)]
#[clap(name = "call", about = "Call a contract")]
pub struct CallCommand {
    /// The address of the the contract to call.
    #[clap(name = "contract", long, env = "CONTRACT")]
    contract: <DefaultConfig as Config>::AccountId,
    /// The name of the contract message to call.
    #[clap(long, short)]
    message: String,
    /// The arguments of the contract message to call.
    #[clap(long, multiple_values = true)]
    args: Vec<String>,
    #[clap(flatten)]
    extrinsic_opts: ExtrinsicOpts,
    /// Maximum amount of gas to be used for this command.
    /// If not specified will perform a dry-run to estimate the gas consumed for the instantiation.
    #[clap(name = "gas", long)]
    gas_limit: Option<u64>,
    /// The value to be transferred as part of the call.
    #[clap(name = "value", long, parse(try_from_str = parse_balance), default_value = "0")]
    value: Balance,
}

impl CallCommand {
    pub fn run(&self) -> Result<()> {
        let (_, contract_metadata) =
            load_metadata(self.extrinsic_opts.manifest_path.as_ref())?;
        let transcoder = ContractMessageTranscoder::new(&contract_metadata);
        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[..])?;
                        name_value_println!(
                            "Result",
                            String::from("Success!"),
                            DEFAULT_KEY_COL_WIDTH
                        );
                        name_value_println!(
                            "Reverted",
                            format!("{:?}", ret_val.did_revert()),
                            DEFAULT_KEY_COL_WIDTH
                        );
                        name_value_println!(
                            "Data",
                            format!("{}", value),
                            DEFAULT_KEY_COL_WIDTH
                        );
                        display_contract_exec_result::<_, DEFAULT_KEY_COL_WIDTH>(&result)
                    }
                    Err(ref err) => {
                        let err = err.error_details(&client.metadata())?;
                        name_value_println!("Result", err, MAX_KEY_COL_WIDTH);
                        display_contract_exec_result::<_, MAX_KEY_COL_WIDTH>(&result)
                    }
                }
            } else {
                self.call(&client, call_data, &signer, &transcoder).await
            }
        })
    }

    async fn call_dry_run(
        &self,
        data: Vec<u8>,
        signer: &PairSigner,
    ) -> Result<ContractExecResult> {
        let url = self.extrinsic_opts.url_to_string();
        let cli = WsClientBuilder::default().build(&url).await?;
        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
            .as_ref()
            .map(|limit| NumberOrHex::Hex((*limit).into()));
        let call_request = RpcCallRequest {
            origin: signer.account_id().clone(),
            dest: self.contract.clone(),
            value: NumberOrHex::Hex(self.value.into()),
            gas_limit: NumberOrHex::Number(*gas_limit),
            storage_deposit_limit,
            input_data: Bytes(data),
        };
        let params = rpc_params![call_request];
        let result = cli.request("contracts_call", params).await?;
        Ok(result)
    }

    async fn call(
        &self,
        client: &Client,
        data: Vec<u8>,
        signer: &PairSigner,
        transcoder: &ContractMessageTranscoder<'_>,
    ) -> Result<()> {
        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 = super::runtime_api::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?;

        display_events(
            &result,
            transcoder,
            &client.metadata(),
            &self.extrinsic_opts.verbosity()?,
        )
    }

    /// Dry run the call before tx submission. Returns the gas required estimate.
    async fn pre_submit_dry_run_gas_estimate(
        &self,
        client: &Client,
        data: Vec<u8>,
        signer: &PairSigner,
    ) -> Result<u64> {
        if self.extrinsic_opts.skip_dry_run {
            return match self.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.message);
        let call_result = self.call_dry_run(data, signer).await?;
        match call_result.result {
            Ok(_) => {
                super::print_gas_required_success(call_result.gas_required);
                let gas_limit = self.gas_limit.unwrap_or(call_result.gas_required);
                Ok(gas_limit)
            }
            Err(ref err) => {
                let err = err.error_details(&client.metadata())?;
                name_value_println!("Result", err, 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."))
            }
        }
    }
}

/// A struct that encodes RPC parameters required for a call to a smart contract.
///
/// Copied from `pallet-contracts-rpc`.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RpcCallRequest {
    origin: <DefaultConfig as Config>::AccountId,
    dest: <DefaultConfig as Config>::AccountId,
    value: NumberOrHex,
    gas_limit: NumberOrHex,
    storage_deposit_limit: Option<NumberOrHex>,
    input_data: Bytes,
}