[Ethereum] How to watch for 12th confirmation with web3 filters

filtersweb3js

It doesn't seem possible to update the options in web3.eth.filter() dynamically, so how would one watch for a transaction involving a particular account appearing on the 12th latest block?

Best Answer

Answering own question.

const Web3 = require('web3')
let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))

let account = '0x05430f5201585c2601bb75d9658007202864c993'
let filter = web3.eth.filter('latest')
filter.watch(function(error, result) {
  if (!error) {
    let confirmedBlock = web3.eth.getBlock(web3.eth.blockNumber - 11)
    if (confirmedBlock.transactions.length > 0) {
      confirmedBlock.transactions.forEach(function(txId) {
        let transaction = web3.eth.getTransaction(txId)
        if (transaction.to == account) {
          // Do something useful.
        }
      })
    }
  }
})
Related Topic