[Ethereum] How to know a hash mined and confirmed by ethers.js

ethers.js

In ethers.js is there any way I can check whether a hash is mined and confirmed so that I know it is not reverted and valid?

Best Answer

In order to know whether a transaction (transactionHash) has been mined and confirmed, the following steps are required:

  1. Get the transaction receipt
  2. Check whether the transaction has been included in a block i.e. the blockNumber is not null

Below is the code snippet for achieving this:

const isTransactionMined = async(transactionHash) => {
    const txReceipt = await provider.getTransactionReceipt(transactionHash);
    if (txReceipt && txReceipt.blockNumber) {
        return txReceipt;
    }
}

In this way you can check whether the transaction has been mined.