1

I'm trying to read an event only when it has a specific value, for that I declared my event like so:

event eventName(int indexed filterNumber, string messageTitle, string messageDef);

Call it like so from the solidity smart contract:

Message(filterNumber, messageTitle, messageDef);

The event works, but I want to read it only if the filterNumber is a specefic one, to acomplish this I do the following:

      var events = communicationChannelInstance.allEvents({'filterNumber': 54},  
      function(error, log){
      if (!error)
        eventHandler(null, log);
    });

(Example trying to read only the messages with a filterNumber 54)

But this system still reads all the events even wen they are sent with a different filternumber.

Am I doing the filter corectly? I tried to follow the documentation but this is as far as I got.

1

If you look at the documentation for allEvents here it makes clear that you options object is not valid.

I would get all of the events, and loop through them. You can then check if the property equals 54 and act accordingly.

  • What would be the correct options object? I´ve tried: [{'filterNumber': 54}] but it doesn't work, I tried to follow the obj structure in the docs. It also doesn't work if I try to listen just for one specific event instead of all. – Enrique Alcazar Aug 2 '17 at 8:28
  • Your solution of manual filtering works but I wanted to do it using the filter. The filter I used only works when listening to a specific event not all of them (With allEvents), thanks for pointing in the right direction – Enrique Alcazar Aug 2 '17 at 8:45
0

If the parameters are indexed You can use the eth.filter directly.

If we have the following event in a contract

event logTest(uint indexed id, address indexed from, uint value);

We can filter with this

const allEvents = eth.filter({
  fromBlock: 0,
  toBlock: 'latest',
  address: contractAddress,
  topics: [
    '0x7ac5878e2e51c507cf06f5ee5ec647b36e2da085e098034e84d2d79732797e23', 
    '0x00000000000000000000000000000000000000000000000000000000000004d2',
    '0x0000000000000000000000007fe01972a2bf4840ba358ce516defa9b20ad92d3', 
  ]
});
allEvents.get(function (err, events)) {
  console.log(events);
}

The order of the topics is [event_signature, id_param, from_param].

You can calculate the event signature with web3.sha3('logTest(uint256,address,uint256)') = '0x7ac5878e2e51c507cf06f5ee5ec647b36e2da085e098034e84d2d79732797e23'.

If you do not want to filter by a topic you set the topic to null. For example

topics: [
    null, // Any event
    '0x00000000000000000000000000000000000000000000000000000000000004d2' // first param
]

will return any event with id equal to 1234 = 0x04d2.

If you want to filter by two or more values you set an array of topics. For example if you want to filter id to be 1234 or 4321 (0x10e1) you can use

topics: [
    null,
    [
        '0x00000000000000000000000000000000000000000000000000000000000004d2',
        '0x00000000000000000000000000000000000000000000000000000000000010e1'
    ]
]

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.