diff --git a/zilliqa/src/api/eth.rs b/zilliqa/src/api/eth.rs index 9e0e7ca9f..bbc352186 100644 --- a/zilliqa/src/api/eth.rs +++ b/zilliqa/src/api/eth.rs @@ -24,7 +24,6 @@ use jsonrpsee::{ PendingSubscriptionSink, RpcModule, SubscriptionMessage, }; use serde::Deserialize; -use sha2::{Digest, Sha256}; use tracing::*; use super::{ @@ -756,7 +755,15 @@ pub(super) fn get_transaction_receipt_inner( }) .collect(); - let is_zilliqa_txn = matches!(signed_transaction.tx, SignedTransaction::Zilliqa { .. }); + let (is_zilliqa_txn, contract_address) = + if let SignedTransaction::Zilliqa { ref tx, .. } = signed_transaction.tx { + ( + true, + Some(tx.get_contract_address(&signed_transaction.signer)?), + ) + } else { + (false, None) + }; let from = signed_transaction.signer; let v = signed_transaction.tx.sig_v(); @@ -764,14 +771,9 @@ pub(super) fn get_transaction_receipt_inner( let s = signed_transaction.tx.sig_s(); let transaction = signed_transaction.tx.into_transaction(); - // Temporary on-the-fly fix for returning proper contract address for deployments from zil transactions let contract_address = { if is_zilliqa_txn && transaction.to_addr().is_none() { - let mut hasher = Sha256::new(); - hasher.update(signed_transaction.signer.as_slice()); - hasher.update(transaction.nonce().unwrap().to_be_bytes()); - let hashed = hasher.finalize(); - Some(Address::from_slice(&hashed[12..])) + contract_address } else { receipt.contract_address } diff --git a/zilliqa/src/api/zilliqa.rs b/zilliqa/src/api/zilliqa.rs index f8c3025f6..762423f10 100644 --- a/zilliqa/src/api/zilliqa.rs +++ b/zilliqa/src/api/zilliqa.rs @@ -376,16 +376,28 @@ fn get_contract_address_from_transaction_id( ) -> Result { let hash: B256 = params.one()?; let hash: Hash = Hash(hash.0); - let receipt = node - .lock() - .unwrap() - .get_transaction_receipt(hash)? - .ok_or_else(|| anyhow!("Txn Hash not Present"))?; + let (receipt, signed_transaction) = { + let node = node.lock().unwrap(); + let receipt = node + .get_transaction_receipt(hash)? + .ok_or_else(|| anyhow!("Txn Hash not Present"))?; + let signed_transaction = node + .get_transaction_by_hash(hash)? + .ok_or_else(|| anyhow!("Txn Hash not Present"))?; + (receipt, signed_transaction) + }; let contract_address = receipt .contract_address .ok_or_else(|| anyhow!("ID is not a contract txn"))?; + let contract_address = match signed_transaction.tx { + SignedTransaction::Zilliqa { tx, .. } => { + tx.get_contract_address(&signed_transaction.signer)? + } + _ => contract_address, + }; + Ok(contract_address.to_hex_no_prefix()) } diff --git a/zilliqa/src/transaction.rs b/zilliqa/src/transaction.rs index 31a079565..f43914b5e 100644 --- a/zilliqa/src/transaction.rs +++ b/zilliqa/src/transaction.rs @@ -811,6 +811,18 @@ impl TxZilliqa { Err(anyhow!("Unknown transaction type")) } } + + pub fn get_contract_address(&self, signer: &Address) -> Result
{ + let mut hasher = Sha256::new(); + hasher.update(signer.as_slice()); + if self.nonce > 0 { + hasher.update((self.nonce - 1).to_be_bytes()); + } else { + return Err(anyhow!("Nonce must be greater than 0")); + } + let hashed = hasher.finalize(); + Ok(Address::from_slice(&hashed[12..])) + } } /// A wrapper for ZIL amounts in the Zilliqa API. These are represented in units of (10^-12) ZILs, rather than (10^-18)