0

I have written a code in hardhat deploy script to fetch the exact block number at which the contract has been executed, but it usually returns the block next to the original. Any idea how to get the exact block?

My Code:

  token = await new DevToken__factory(owner).deploy(1000);
  let contract = await token.deployed();
  let block = await contract.provider?.getBlockNumber();

1 Answer 1

1

You can get the TransactionReceipt using its hash and inside it, you can see the block at which the contract is deployed

  token = await new DevToken__factory(owner).deploy(1000);
  const hashOfTx = token.deployTransaction.hash
  let contract = await token.deployed();
  
  let txReceipt = await contract.provider.getTransactionReceipt(hashOfTx);
  console.log(txReceipt.blockNumber)
1
  • I think it worked. The block number returned is exactly the one expected. Thanks. :) Nov 25, 2022 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.