Given that events are fired whenever a transaction is included in a newly mined block, how do you handle the situation when the mined block containing the transaction gets orphaned?

Say I have a database that inserts data every time a Deposit event occurs:

myContract.on("Deposit", depositInfo => myDatabase.insert(depositInfo))

But then the block containing the transaction that did the did the deposit gets orphaned 4 blocks later. Now I am storing invalid data. Then 10 blocks later it turns out the chain in which the original transaction was mined on, is now the longest chain. So now the database is accurate again.

How can I be notified every time an event gets invalidated/validated again due to chain reorgs?

Then I can do something like this; Remove the event from my database if it is invalid and insert it if it is valid:

myContract.on("Deposit", (depositInfo, isValid) => {
    if (isValid) {
    } else {

Turns out this is trivial to do in web3js but ethersjs does not offer any functionality to do it.

In web3js:

    .on("data", async (error, event) => {
    .on("changed", async (error, event) => { // Called when event is no longer valid

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.