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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 theeth_getBlockByNumber
RPC (used inprovider.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.
yes, forgot to respond, thanks! ended up working around it.