question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting block timestamp in contract events without calling the getBlock() promise

See original GitHub issue

👋 love the library! is there any way to get the timestamp of the block in the contract.on event feed without having to use the async getBlock() function? Or is the block already cached and getBlock() essentially returns right away?

i.e. something like:

contract.on(filter, async (event) => {
  const ts = event.blockTimestamp;
});

For context, I’m loading a snapshot from a contract function which returns the block timestamp of the snapshot (instead of the block number), and trying to diff the event blocks with the snapshot block timestamp (to discard any events from before the snapshot).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
zemsecommented, Jan 24, 2021

The logs RPC on a eth node (eth_getLogs) does not include a timestamp. So just with an eth node endpoint, AFAIK you will have to do the eth_getBlockByNumber RPC (used in provider.getBlock). I really wish if timestamp would be there in logs since it’s much wanted, but maybe it’s not due to performance reasons.

If you frequently need to fetch the logs + timestamp, you may consider using TheGraph. Here’s an example of getting timestamp on an event.

2reactions
tonypengcommented, Feb 1, 2021

yes, forgot to respond, thanks! ended up working around it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

solidity - How to get timestamp of an event log in emitted ...
You can do this by checking the timestamp of the block that the event was included in. For something like web3.js, you can...
Read more >
Provider - Ethers.js
Returns the contract code of address as of the blockTag block height. If there is no contract currently deployed, the result is 0x...
Read more >
web3.eth — web3.js 1.0.0 documentation - Read the Docs
Promise returns Object - the returned uncle. For a return value see web3.eth.getBlock(). Note. An uncle doesn't contain individual transactions.
Read more >
Providers — ethers.js 4.0.0 documentation
getBlock ( blockHashOrBlockNumber ) => Promise<Block>: Returns a Promise with ... to call the FireflyRegistrar.fee() function // FireflyRegistrar contract ...
Read more >
JS SDK | PlatON - Alaya Developer Docs
getBlock() Returns a block matching the block number or block hash. ... Either a ABI byte string containing the data of the function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found