Skip to content

Commit

Permalink
fix(extrinsics): call dry_run_instantiate only once
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Aug 25, 2023
1 parent 8e6f767 commit 99826d8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
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 @@ -108,7 +108,7 @@ pub async fn handle_instantiate(

if !instantiate_command.extrinsic_cli_opts.execute {
let result = instantiate_exec.instantiate_dry_run().await?;
match instantiate_exec.simulate_instantiation().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()?);
Expand Down
66 changes: 38 additions & 28 deletions crates/extrinsics/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,23 @@ pub struct InstantiateExec {
}

impl InstantiateExec {
/// Simulates the instantiation of a contract without executing it on the blockchain.
/// Decodes the result of a simulated contract instantiation.
///
/// This function performs a dry-run simulation of the contract instantiation process
/// and returns an [`InstantiateDryRunResult`] object containing essential
/// information, including the contract address, gas consumption, and storage
/// deposit.
/// This function decodes the result of a simulated contract instantiation dry run.
/// It processes the returned data, including the constructor's return value, contract
/// address, gas consumption, and storage deposit, and packages them into an
/// [`InstantiateDryRunResult`].
///
/// It does not modify the state of the blockchain.
pub async fn simulate_instantiation(
/// Returns the decoded dry run result, or an error in case of failure.
pub async fn decode_instantiate_dry_run(
&self,
result: &ContractInstantiateResult<
<DefaultConfig as Config>::AccountId,
Balance,
(),
>,
) -> Result<InstantiateDryRunResult, ErrorVariant> {
tracing::debug!("instantiate data {:?}", self.args.data);
let result = self.instantiate_dry_run().await?;
match result.result {
Ok(ref ret_val) => {
let value = self
Expand All @@ -317,6 +321,32 @@ impl InstantiateExec {
}
}

/// Simulates a contract instantiation without modifying the blockchain.
///
/// This function performs a dry run simulation of a contract instantiation, capturing
/// essential information such as the contract address, gas consumption, and storage
/// deposit. The simulation is executed without actually executing the
/// instantiation on the blockchain.
///
/// Returns the dry run simulation result, or an error in case of failure.
pub async fn instantiate_dry_run(
&self,
) -> Result<
ContractInstantiateResult<<DefaultConfig as Config>::AccountId, Balance, ()>,
> {
let storage_deposit_limit = self.args.storage_deposit_limit;
let call_request = InstantiateRequest {
origin: account_id(&self.signer),
value: self.args.value,
gas_limit: None,
storage_deposit_limit,
code: self.args.code.clone(),
data: self.args.data.clone(),
salt: self.args.salt.clone(),
};
state_call(&self.url, "ContractsApi_instantiate", &call_request).await
}

async fn instantiate_with_code(
&self,
code: Vec<u8>,
Expand Down Expand Up @@ -408,26 +438,6 @@ impl InstantiateExec {
}
}

/// Performs a dry run of the contract instantiation process without modifying the
/// blockchain.
pub async fn instantiate_dry_run(
&self,
) -> Result<
ContractInstantiateResult<<DefaultConfig as Config>::AccountId, Balance, ()>,
> {
let storage_deposit_limit = self.args.storage_deposit_limit;
let call_request = InstantiateRequest {
origin: account_id(&self.signer),
value: self.args.value,
gas_limit: None,
storage_deposit_limit,
code: self.args.code.clone(),
data: self.args.data.clone(),
salt: self.args.salt.clone(),
};
state_call(&self.url, "ContractsApi_instantiate", &call_request).await
}

/// Estimates the gas required for the contract instantiation process without
/// modifying the blockchain.
///
Expand Down

0 comments on commit 99826d8

Please sign in to comment.