Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix contract address retrieval for Zilliqa transactions. #2206

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions zilliqa/src/api/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use jsonrpsee::{
PendingSubscriptionSink, RpcModule, SubscriptionMessage,
};
use serde::Deserialize;
use sha2::{Digest, Sha256};
use tracing::*;

use super::{
Expand Down Expand Up @@ -756,22 +755,25 @@ 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();
let r = signed_transaction.tx.sig_r();
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
}
Expand Down
22 changes: 17 additions & 5 deletions zilliqa/src/api/zilliqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,28 @@ fn get_contract_address_from_transaction_id(
) -> Result<String> {
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 {
saeed-zil marked this conversation as resolved.
Show resolved Hide resolved
SignedTransaction::Zilliqa { tx, .. } => {
tx.get_contract_address(&signed_transaction.signer)?
}
_ => contract_address,
};

Ok(contract_address.to_hex_no_prefix())
}

Expand Down
12 changes: 12 additions & 0 deletions zilliqa/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,18 @@ impl TxZilliqa {
Err(anyhow!("Unknown transaction type"))
}
}

pub fn get_contract_address(&self, signer: &Address) -> Result<Address> {
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)
Expand Down