1

When listening for events in Web3 the listener can sometimes emit a 'changed' event. In the docs in the example code there is a comment explaining that the event should be removed from local database.

When observing console.log of 'data' and 'changed' events i sometimes see that a event is emitted as 'data' then 'changed' and then as 'data' again.

I want to implement caching the information properly in my local db but I do not understand why such a situation occurs. Additionally can I be sure that the order in which the 'data' and 'changed' events occur will result in properly cached data (can i be sure that data regarding the same event wont be sent as 'data' => 'data' => 'changed' which would result in me saving the event twice and then deleting the event from local database).

https://web3js.readthedocs.io/en/v1.4.0/web3-eth-contract.html#id50

myContract.events.MyEvent({
    fromBlock: 0
})
.on('changed', function(event){
    // remove event from local database
})
TylerH
  • 20,799
  • 66
  • 75
  • 101
Eternal
  • 303
  • 1
  • 2
  • 16
  • unfortunately no. sorry. I developed a workaround tho. I'm listening for events 24/7 but every hour i run a script that fetches all past events and verifies that i got all of them, and that i didn't store any that should've been later deleted (the ones that occured in `changed`). – Eternal Oct 18 '21 at 15:19

1 Answers1

1

I just ran into this same issue, had the same transaction hash across two different blockhashes in this order: data => data => change. What I would recommend is using a compound unique identifier per transaction for the transaction hash and the block hash. Then, when the changed event fires, you will delete the correct transaction/block hash document and the other will remain.

Dev12345
  • 11
  • 2