Watch BlockHeaders

Hi Gang,

TLDR: what’s the best and fastest way to watch new Blocks in Moralis?
A subscribe functions similar to afterSave would be fire!

My use case: I am watching/syncing a contract that has quite a view events (opensea) - due to the volume there’s a huge delay (~6 hours) between the actual event and when it get inserted into the DB.
Also tested it briefly with the new beta servers (there was also a delay, smaller but still too much for my intended use). So I figured it would be best to watch each block - pick the transactions I need and bulk insert them. In theory this sound way more efficient. Downside of this approach is that I need another server to:

  • Natively watch the block headers with web3.eth.subscribe
  • call await Moralis.Web3API.native.getBlock(options); which gives me an empty object for the latest 1-2ish blocks.

Is there a better approach to get the latest block data asap?


in python you can get ~latest block number with this syntax:

web3 = Web3(Web3.HTTPProvider(''))

for x in range(20):
    n = web3.eth.block_number

event sync should work fine too, you may need to add some indexes and delete older events

thanks for your reply!
getting the latest block number is not a problem. Also getting the data is rather straightforward with moralis :slight_smile:

I did add indexes as you suggested (couple days ago in discord) which made a huge difference in query times. :v:

However, I do have 2 Tables (1 Raw Event-Sync data + 1 for the actual live data )

  • The Raw-Event Table has already a delay
  • There is no delay between the Raw-Event-Table & the Live-Data (same created at)
  • I do need to make sure that there are no duplicate events - query against an index (hash) in the live-data db
  • get the transaction via web3.eth.getTransactionReceipt for every event
  • Save it to the live-data table (in an beforeSave - so that I could throw an error to prevent inserting into the Raw-Event-Table) - so there is some work involved
  • while writing this I switched from beforeSave to afterSave - to check if that makes any difference

In any way it would make more sense for my use-case to analyse transactions on a per block basis - is this something you have on your radar? A “listener” for new blocks - similar to afterSave for Events?

My current Server is constantly at 100% CPU. Listening for complete blocks would also be way more resource efficient in my case (I guess) since I would have 1 call in every 13seconds and not multiple per second.

do you have an upgraded server? how big is the delay for synced data now?

no it’s a free server - delay was down to 15minutes - now it’s up again (~40is minutes) since I crashed the server with the before/afterSave update :see_no_evil:

I am going to switch to the per Block analysis since it makes way more sense in general for my use case. So if Moralis would provide a way to efficiently do that would be awesome! If thats something that’s not on your radar I’ll put it on the roadmap as a suggestion.

this should work faster for sync: Moralis Nitro Beta Testing

but it doesn’t work at this moment