6

I am trying to watch all events of a contract with many events. The following code is supposed to do just that:

var events = myContract.allEvents();
events.watch(function(error, event){
    if (error) {
        console.log("Error: " + error);
    } else {
        console.log('some event occured');
    }
});

But if I try it in geth, and trigger an event, this is what the console tells me:

cannot find event for log
undefined: undefined

Can someone please tell me what s wrong with this code? (I run mist at the same time, I just geth attached)

Note that I have no difficulty watching individual events. I am instead trying to watch them all with the code above.

4

How is myContract defined? Is there an allEvents() event type?

I will assume that you have defined the myContract variable but you are trying to use allEvents to retrieve all events.

See How do I retrieve the Voted events from The DAO for an example of how to retrieve specific events generated by a contract.

See web3.eth.filter to use the filter(...) function to retrieve all events.



Update Responding To Comments Below

Here's a bit of code you to check out web3.eth.filter(...) with:

var filter = web3.eth.filter({ address: ["0xc4af56cd5254aef959d4bce2f75874007808b701"], fromBlock: 2615848, toBlock: "latest" });
var i = 0;
filter.watch(function (error, result) {
  console.log("RESULT: Filter " + i++ + ": " + JSON.stringify(result));
});
filter.stopWatching();

The results produce include:

RESULT: Filter 0: {"address":"0xc4af56cd5254aef959d4bce2f75874007808b701","blockHash":"0x201154027d3c69cca298fab9983848269eee04cc26a9e1e7f2d06c9e1529b8f5","blockNumber":2615921,"data":"0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f000000000000000000000000a74476443119a942de498590fe1f2454d7d4ac0d00000000000000000000000000000000000000000000000000000000000186a0","logIndex":2,"removed":false,"topics":["0x242ddf37c1721f450a35afdb783ef36e84d032a300600da464ff17d600cceed7"],"transactionHash":"0x6a7f83c93837f4ab11e1bc9dae2a34df4b4610b0d87512e451a36b5f53fb9661","transactionIndex":17}
RESULT: Filter 1: {"address":"0xc4af56cd5254aef959d4bce2f75874007808b701","blockHash":"0x201154027d3c69cca298fab9983848269eee04cc26a9e1e7f2d06c9e1529b8f5","blockNumber":2615921,"data":"0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f0000000000000000000000000020dba1d308339182239056a00fcc146d2e26e0000000000000000000000000399156ee3339f4b29a53e307b98cce09fda3bac7","logIndex":3,"removed":false,"topics":["0x2d6a3482be690073d89f3c5420f70aca9f53be93321c5f45b5be55907b5f2b5b"],"transactionHash":"0x6a7f83c93837f4ab11e1bc9dae2a34df4b4610b0d87512e451a36b5f53fb9661","transactionIndex":17}
RESULT: Filter 2: {"address":"0xc4af56cd5254aef959d4bce2f75874007808b701","blockHash":"0x58fd992519b8bb06b8a8cddac850efad4f2b3b8dd5069a0d0d8f4deb15616b81","blockNumber":2617375,"data":"0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f0000000000000000000000000020dba1d308339182239056a00fcc146d2e26e0000000000000000000000000b362bba1c7c5c857e3ac74d740c0007c48db7362","logIndex":5,"removed":false,"topics":["0x2d6a3482be690073d89f3c5420f70aca9f53be93321c5f45b5be55907b5f2b5b"],"transactionHash":"0xb7ef581cd0e6597525e254add6f820ac6ecf860d89d01656035dfd880dac2747","transactionIndex":6}
...

The events above are the Golem Network Token (GNT) TokenTraderFactory with source code available from 0xc4af56cd5254aef959d4bce2f75874007808b701. You can find further information on this TokenTraderFactory at Trustless Golem Network Token (GNT) Selling Contract.

Note that the topics in the events above include the hex strings below:

  • 0x242ddf37c1721f450a35afdb783ef36e84d032a300600da464ff17d600cceed7
  • 0x2d6a3482be690073d89f3c5420f70aca9f53be93321c5f45b5be55907b5f2b5b

Here are the events declared in the TokenTraderFactory contract:

  • event TradeListing(bytes32 bookid, address owner, address addr);
  • event NewBook(bytes32 bookid, address asset, uint256 units);

In geth:

> web3.sha3('NewBook(bytes32,address,uint256)')
"0x242ddf37c1721f450a35afdb783ef36e84d032a300600da464ff17d600cceed7"
> web3.sha3('TradeListing(bytes32,address,address)')
"0x2d6a3482be690073d89f3c5420f70aca9f53be93321c5f45b5be55907b5f2b5b"

You will have to interpret the data fields by yourself. From the second listing above, the data is 0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f0000000000000000000000000020dba1d308339182239056a00fcc146d2e26e0000000000000000000000000399156ee3339f4b29a53e307b98cce09fda3bac7

This can be separated into:

0x
2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f
0000000000000000000000000020dba1d308339182239056a00fcc146d2e26e0
000000000000000000000000399156ee3339f4b29a53e307b98cce09fda3bac7

In the TokenTraderFactory event TradeListing(bytes32 bookid, address owner, address addr) event,

bookid = 0x2fd64880bc17977672b2e25fff32b433975c4004b4fbd094936429e92f58d39f
owner  = 0x0020dba1d308339182239056a00fcc146d2e26e0
addr   = 0x399156ee3339f4b29a53e307b98cce09fda3bac7
  • 1
    Hello! I guess my question was not v. clear. myContract could be any contract with several events. The is no "allEvents()" event type. The point it that I want to avoid having to write the above code for each separate event. I ll look into your links though, thx, – hartmut Dec 19 '16 at 11:12
  • 1
    I've updated my answer to demonstrate the usage of web3.eth.filter(...) that allows you to extract events related to one or more addresses. There is a bit more work involved in interpreting the event type and the data. – Bokky WeAreAllAssange PooBah Dec 19 '16 at 12:12
  • When you do filter.stopWatching(); right away it won't print any thing. @BokkyPooBah – alper Jun 6 '17 at 17:08
  • 1
    @Avatar - works for me. See github.com/bokkypoobah/TokenCardICOAnalysis/blob/master/scripts/… with the results in github.com/bokkypoobah/TokenCardICOAnalysis/blob/master/results/… . You can also use get() to get all the results immediately. – Bokky WeAreAllAssange PooBah Jun 6 '17 at 22:58
  • 1
    I know the issue, if you start from fromBlock: to the latest and if there is no event exist/created in between, the watcher halts, pauses no matter what. @BokkyPooBah – alper Jun 7 '17 at 7:20
0

Also remember that if the 'toBlock' block number is not yet discovered in your local Geth then the callback will not fire any results until geth finishes syncing up to that block!

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.