From d3fc06cdbfd564f39976a9ccd4de5d2e7e4ee2e6 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 30 May 2022 10:36:53 +0100 Subject: [PATCH] Fix `instantiate_with_code` with already uploaded code (#594) --- src/cmd/extrinsics/instantiate.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/cmd/extrinsics/instantiate.rs b/src/cmd/extrinsics/instantiate.rs index 6033198b1..9b1a5f9c0 100644 --- a/src/cmd/extrinsics/instantiate.rs +++ b/src/cmd/extrinsics/instantiate.rs @@ -251,7 +251,9 @@ impl<'a> Exec<'a> { Code::Upload(code) => { let (code_hash, contract_account) = self.instantiate_with_code(code).await?; - name_value_println!("Code hash", format!("{:?}", code_hash)); + 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) => { @@ -265,7 +267,7 @@ impl<'a> Exec<'a> { async fn instantiate_with_code( &self, code: Bytes, - ) -> Result<(CodeHash, ContractAccount)> { + ) -> Result<(Option, ContractAccount)> { let api = self.subxt_api().await?; let tx_progress = api .tx() @@ -287,14 +289,16 @@ impl<'a> Exec<'a> { display_events(&result, &self.transcoder, metadata, &self.verbosity)?; - let code_stored = result + // The CodeStored event is only raised if the contract has not already been uploaded. + let code_hash = result .find_first::()? - .ok_or_else(|| anyhow!("Failed to find CodeStored event"))?; + .map(|code_stored| code_stored.code_hash); + let instantiated = result .find_first::()? .ok_or_else(|| anyhow!("Failed to find Instantiated event"))?; - Ok((code_stored.code_hash, instantiated.contract)) + Ok((code_hash, instantiated.contract)) } async fn instantiate(&self, code_hash: CodeHash) -> Result {