Skip to content

Instantly share code, notes, and snippets.

@dodikk
Forked from dsemenovsky/getConfirmations.js
Created October 2, 2018 14:41
Show Gist options
  • Select an option

  • Save dodikk/e8bd09d7d5a82ec5159ad5522cd3ba4e to your computer and use it in GitHub Desktop.

Select an option

Save dodikk/e8bd09d7d5a82ec5159ad5522cd3ba4e to your computer and use it in GitHub Desktop.

Revisions

  1. @dsemenovsky dsemenovsky created this gist Jun 1, 2018.
    19 changes: 19 additions & 0 deletions getConfirmations.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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)
    }
    }