1

I have referred, Web3 Events, but filters object is not working for me, I've got the same result for events before and after applying filter object.

Here's the code:

 instance.getPastEvents(
    "Transfer",
    {filter: {value: [117,50]}},
    { fromBlock: 0 , toBlock: "latest" },
    (errors, events) => {
         if (!errors) {
            console.log(events);
         }
     }
);

What does that filter object really means, According to documentations and references it is custom fields but it must be object parameters.

Suggestion needed to correct the filters use.

2 Answers 2

2

Filters only work if the parameter is indexed. For example, the ERC20.Tranfer event is defined as Transfer(address indexed from, address indexed to, uint tokens), so only from and to can be used in filters.

Edit: I just noticed, you're submitting the filter as a separate object. The correct format is this:

 instance.getPastEvents(
    "Transfer",
    {
      filter: {value: [117,50]},
      fromBlock: 0 ,
      toBlock: "latest"
    }, (errors, events) => {
         if (!errors) {
            console.log(events);
         }
     }
);
5
  • Yes, I am aware about it. I've also used 'to' and 'from' instead 'value', and my contract ABI also has indexed value true for 'from' and 'to'. But still I am getting entire array of an event without filters.
    – Rudrika
    Commented Aug 31, 2019 at 5:28
  • It doesn't matter if just the ABI has them indexed, they have to be indexed at deployment time. Are you sure the parameters were marked indexed at compile time before deployment?
    – natewelch_
    Commented Aug 31, 2019 at 23:44
  • @Rudrika See my edit. I realized your issue is just around the format of parameters to getPastEvents
    – natewelch_
    Commented Sep 1, 2019 at 5:07
  • There is a change in object, for fromBlock and toBlock, But I have problem with the filter object, which is represented by an array after value.
    – Rudrika
    Commented Sep 2, 2019 at 4:28
  • The issue isn't with the filter object, it's with where you're putting it in the parameters. It shouldn't be it's own object as the 2nd parameter to getPastEvents, it should be a property in the object that is the 2nd parameter, which always has fromBlock and toBlock. Read the edit carefully.
    – natewelch_
    Commented Sep 5, 2019 at 18:38
0

Using web3 1.2.2, I observed that if you use a precise identifier e.g. transactionHash then will not work when combined with fromBlock and toBlock.

Precise result returned:

filter: {
    transactionHash: txObj.transactionHash,
},

All results:

{
    filter: {
        transactionHash: txObj.transactionHash,
    },
    fromBlock: 0,
    toBlock: 'latest'
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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