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?

  • Esiest way is to subscribe to every block and in the callback use a filter and the synchronous get function to obtain only the past 12th block's events. Does this match your need? – Guenole de Cadoudal Feb 28 '17 at 17:17
  • How do you mean subscribe? – fooboo Feb 28 '17 at 18:34
  • 1
    Use the eth.filter('latest', callback) to be notified for every new block being added to the chain. Spend some time in the API. Good luck. – Guenole de Cadoudal Feb 28 '17 at 18:51
  • Ended up with this code. Feels clunky but it works. – fooboo Mar 1 '17 at 14:51
  • You could simplify as the eth.getBlock does work synchronously if no callback is supplied. But otherwise it is ok – Guenole de Cadoudal Mar 1 '17 at 20:48

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.
  • 2
    It could be simplified a bit. First, try to fetch transactionReceipt (web3.eth.getTransactionReceipt(txId)). It's possible only when it was mined. In response you should find block number. Make diff from blockNumber-txBlock if it's greater then 12 it definitely confirmed :) Btw, I've read somehwere that waiting for 8 blocks should be enough. – radmen Mar 7 '17 at 11:28
  • 1
    There was a network fork between geth and Parity clients in Nov 24 2016 where "~165 blocks were mined on the now abandoned chain". See blog.ethereum.org/2016/11/25/… . – The Officious BokkyPooBah Mar 7 '17 at 11:38
  • 1
    @radmen the initial intention was to watch for a transaction to a particular account on a specific block, so have no way of knowing in advance with the transaction id would be. Have updated the original question to make that clearer – fooboo Mar 7 '17 at 11:55
  • 1
    @grift00 ah, now it's more clear :) Thanks for update! – radmen Mar 7 '17 at 12:29
  • It appears that web3.eth.filter has been replaced with web3.eth.subscribe. – Gajus May 16 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.