How to parse event timestamp and block? #3954
Unanswered
DumplingsHunter
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Can you include the network and contract address, and the ABI for |
Beta Was this translation helpful? Give feedback.
1 reply
-
There may be some issue with the framework or another library you're using. I just tested this with ethers 6.3.0 and it works as expected: // Instantiate ethers
const ethers = await import('https://cdn.jsdelivr.net/npm/ethers@6.3.0/+esm');
const provider = new ethers.JsonRpcProvider('https://data-seed-prebsc-2-s3.binance.org:8545');
// Instantiate the contract
const abi = ['event Send(address indexed token, address indexed tokenOnSecondChain, address indexed to, uint256 amount, uint256 nonce)'];
const contract = new ethers.Contract('0x5890C624d04677379ba9a9b78c7Af50e4c7df6E2', abi, provider);
// I picked the topics from a recent event I found on BscScan:
// https://testnet.bscscan.com/address/0x5890C624d04677379ba9a9b78c7Af50e4c7df6E2#events
const filter = contract.filters.Send('0xa506da5f0a1da881ace15f1bec28c069f56249d5', '0xa506da5f0a1da881ace15f1bec28c069f56249d5', '0x2ca935e866ac764f563344d7be6f0d0c9253d796');
const event = (await contract.queryFilter(filter)).pop(); // Gets the latest event matching the filter
// The event is successfully queried
console.log(event.blockNumber, event.blockHash, event.transactionHash);
// 28700998 '0x9dbf6ee35b1d1220b7d196ebc0e5b84d9529b69b8069a72ae80440949a5b9e8b' '0xbc1160006ddf06ad93aa231d48aa8fdbf3a92f18080dc6035c577c49ddcafc9f'
// If you need the timestamp, you can query the block
const block = await provider.getBlock(event.blockNumber);
console.log(block.timestamp); // Unix timestamp: 1680794171
console.log(new Date(block.timestamp * 1000).toISOString()); // Remember to multiply by 1000 before passing it to the Date constructor
// 2023-04-06T15:16:11.000Z |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello!
A have a code:
But event.blockNumber and event.transactionHash is undefined. What can it be? I use version 6.2.3
Beta Was this translation helpful? Give feedback.
All reactions