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?
Asked
Active
Viewed 1.2k times
2 Answers
8
In order to know whether a transaction (transactionHash) has been mined and confirmed, the following steps are required:
- Get the transaction receipt
- 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.
balajipachai
- 937
- 5
- 12
2
Now its:
const txnCheck = async (txnHash) => {
const provider = new ethers.providers.Web3Provider(ethereum);
signer = provider.getSigner();
let txn_test = await provider.getTransaction(txnHash);
if (txn_test) {
if (txn_test.blockNumber) {
console.log("txn_test: ");
console.log(txn_test);
return txn_test
}
}
}
Mariusz Sidorowicz
- 121
- 2
-
Doesn't fully answer the question -- how would you know if the transaction failed for some reason, or just hadn't completed yet? – GGizmos Oct 03 '22 at 04:20
-
2Another way would be to scan the transaction receipt, and look for any events which are emitted only when a transaction succeeds. Thus, if such a topic is present in the events array of the transaction receipt, then the transaction has succeeded. This assumes an event is emitted though. – Abhik Banerjee Oct 09 '22 at 09:23
txReceiptitself may benull. I just ran a version of this solution and ended up withCannot read property 'blockNumber' of null. – Cliff Hall Apr 15 '20 at 22:24