Skip to content

Instantly share code, notes, and snippets.

@nanom1t
Forked from dsemenovsky/getConfirmations.js
Created December 24, 2023 18:31
Show Gist options
  • Save nanom1t/ddd016d18d201a5509d197db3d4acbd8 to your computer and use it in GitHub Desktop.
Save nanom1t/ddd016d18d201a5509d197db3d4acbd8 to your computer and use it in GitHub Desktop.
Get Ethereum transaction confirmations count
async function getConfirmations(txHash) {
try {
// Instantiate web3 with HttpProvider
const web3 = new Web3('https://rinkeby.infura.io/')
// Get transaction details
const trx = await web3.eth.getTransaction(txHash)
// Get current block number
const currentBlock = await web3.eth.getBlockNumber()
// When transaction is unconfirmed, its block number is null.
// In this case we return 0 as number of confirmations
return trx.blockNumber === null ? 0 : currentBlock - trx.blockNumber
}
catch (error) {
console.log(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment