I presume Solidity events are fired the moment the block that includes its transaction is mined -- please correct me if I'm wrong.

I need to sync the transaction status locally but wish to only save them to database after X number of confirmations.

I'm not sure if there's a native way to listen for events in web3.js (1.0.0-beta) while specifying the number of confirmed blocks I'd like to wait.

The only approach I can think of is save the event in local database when it's heard, recording its block number and mark it as notConfirmed. Then have an auxiliary process to keep polling for the latest block number and mark all notConfirmed entries when lastestBlockNumber - eventBlockNumber > requiredConfirmation. The problem with this design is it involves constantly sweeping the database, which will become heavier as the event table grows.

The question is: How do you think of my design? Is there a better approach to listen for confirmed events?

  • One solution is to use a block filter, and for each new block n, you query the range [0, n-12 ] for events with at least 12 "confirmations". – Ismael May 22 '18 at 5:57
  • @Ismael , doesn't your solution imply keep calling web3.js' events.<MyEvent>({fromBlock: , toBlock: }) with dynamic fromBlock, toBlock values? – Jerry Ji May 22 '18 at 7:34
  • Yes, querying events should be cheap because they use bloom filters. But I didn't try with a real program, so I can't say it is a better approach. – Ismael May 22 '18 at 13:15
  • @Ismael my concern is not really on the cost of bloom filter, but on keeping calling events.<MyEvent> with changing parameters forever – Jerry Ji May 22 '18 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.