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) {
myDatabase.insert(depositInfo)
} else {
myDatabase.findAndRemove(depositInfo);
}
});