0

I have the following code:

var filter = web3.eth.filter({
    fromBlock:4076955, 
    toBlock:"latest",
    address:"0x0123456789012345678901234567890123456789"
});

filter.get(function(error,logs){
  if (!error) {
    console.log("Found block " + logs.blockNumber);
    console.log(JSON.stringify(logs,null,2));
  }
}

and when I run the code, I get:

Found block undefined
[]

I would have expected to get a number for logs.blockNumber and an object as the return value (source: https://github.com/ethereum/wiki/wiki/JavaScript-API#watch-callback-return-value)

Question: Why might I not be getting the expected result?

Webeng
  • 895
  • 2
  • 12
  • 26

2 Answers2

0

Perhaps, the issue is because filter.get() returns logs data, only if web3.eth.filter is initialized before the transactions you are looking are appeared.

More info in this issue.

Victor Baranov
  • 2,027
  • 1
  • 17
  • 31
  • oh you mean only if the transactions haven't been added to the blockchain yet? Also if I change the toBlock: 'latest' to toBlock: 4076956 I get the same result. – Webeng Jul 26 '17 at 16:45
  • yes, it's not clear from docs, but, seems, you can listen only that transactions which were born after you set filter with web3.eth.filter – Victor Baranov Jul 26 '17 at 17:05
  • hmmm, but I was told that filter.watch was for listening, while filter.get was for getting from blocks. Here: https://ethereum.stackexchange.com/questions/21694/using-web3-eth-filter – Webeng Jul 26 '17 at 17:13
0

I think this is because the results passed to the callback for filter.get are an array of entries. I can't find any code samples from where I've done this before, but I'm pretty sure logs in your callback is an array of blocks or logs, not just a single one. Try just passing console.log.bind(console) to filter.get, to see what the raw values are?

DeviateFish
  • 1,128
  • 5
  • 11