Search code examples
node.jsethereumsolidityweb3js

How to filter by string parameter, web3 2.0.0-alpha.1 Solidity events?


I'm trying to filter some events, and I noticed since I updated the web3 to version 2.0.0-alpha 1 the event catch is a little bit different.

I have a Smart Contract with this event:

event catchMeIfYouCan (address indexed a, string indexed b, uint indexed c);

And I want to filter by its parameters, so far so good.

But when I try to filter by b ( the string indexed ), this is not working. I'm doing that in NodeJS with ExpressJS and the Web3 version mentioned above.

If I do that:

const event = smartContract.events.catchMeIfYouCan({ filter : {
 a : accountAddress ,
 b : web3.utils.toHex(stringValue) ,
 c : web3.utils.toWei("" + numberValue) } 
}, (error, event) => {
 // do some things
});

I get:

Node error: {"code":-32602,"message":"invalid argument 1: hex has invalid length 96 after decoding"}

Otherwise, if I let the b parameter, in NodeJS event catch as:

 b : stringValue,

It doesn't catch the event anymore , same with c ( e.g : no more web3.utils.toWei() ).

Do you have any idea how to filter the event by a string parameter in Web3 2.0.0-Alpha 1 version?


Solution

  • I tested it and I believe its a bug with filter

    But I tried use topics and it works

      contract.events.CatchMeIfYouCan({
        topics: [, web3.utils.sha3(stringValue)], // first element is empty, because its place for `address` index
        fromBlock: 2000000
      }, (error, event) => {
        console.log(event)
      })
    

    I have created an issue in the web3.js GitHub repository