Solidity

如何在 Hardhat 部署腳本中獲取確切的塊號?

  • November 25, 2022

我已經在 hardhat 部署腳本中編寫了一個程式碼來獲取執行合約的確切塊號,但它通常會返回原始塊旁邊的塊。知道如何獲得確切的塊嗎?

我的程式碼:

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

您可以使用它的雜湊值獲取 TransactionReceipt,在其中,您可以看到部署合約的區塊

 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)

引用自:https://ethereum.stackexchange.com/questions/140013