Block confirmations are crucial for ensuring transaction finality in Ethereum. This guide explains how confirmations work and how to implement them in your applications.
// Check number of confirmations
const getConfirmations = async (txHash) => {
const tx = await web3.eth.getTransaction(txHash);
const currentBlock = await web3.eth.getBlockNumber();
return tx.blockNumber ? currentBlock - tx.blockNumber : 0;
};
// Wait for specific number of confirmations
const waitForConfirmations = async (txHash, confirmations = 6) => {
while ((await getConfirmations(txHash)) < confirmations) {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
return true;
};