Ethers.js: How to get block timestamp for WebSockets event?

Created on 28 May 2020  路  4Comments  路  Source: ethers-io/ethers.js

Hi @ricmoo ,

WebSockets events are being received perfectly.

However, when an event comes in, the following returns null:

block = await jsonRpcProvider.getBlock(event.blockNumber); // block is null; the regular provider apparently doesn't know about this block yet.
const creationTime = block.timestamp; // Block is null, so this fails.

Any ideas on how to get the block timestamp when a Websocket event is received? Thanks for your help!

bug fixed

Most helpful comment

The polling-based providers track this extra state internally to prevent this from happening. I didn't properly cache the block emitted event in the WebSocketProvider to leverage this behaviour from the BaseProvider.

I've got a fix locally and it should be published tomorrow. I'll update this ticket with the progress.

Thanks! :)

All 4 comments

The polling-based providers track this extra state internally to prevent this from happening. I didn't properly cache the block emitted event in the WebSocketProvider to leverage this behaviour from the BaseProvider.

I've got a fix locally and it should be published tomorrow. I'll update this ticket with the progress.

Thanks! :)

This should be fixed now in 5.0.0-beta.189. Please try it out and let me know if you still have any problems. :)

I'm going to close this now, since I think it is fixed. If not, please re-open.

Thanks! :)

Thanks @ricmoo ; all is good on my end!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abhishekp1996 picture abhishekp1996  路  3Comments

GFJHogue picture GFJHogue  路  3Comments

crazyrabbitLTC picture crazyrabbitLTC  路  3Comments

naddison36 picture naddison36  路  3Comments

jochenonline picture jochenonline  路  3Comments