-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
26 lines (20 loc) · 788 Bytes
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const { ethers } = require("ethers");
async function getCurrentBlockTimestamp() {
const polygonNodeUrl = "https://polygon-rpc.com"; // Replace with your Infura project ID or your Polygon node URL
const provider = new ethers.providers.JsonRpcProvider(polygonNodeUrl);
// Get the current block number
const currentBlockNumber = await provider.getBlockNumber();
// Get the current block
const currentBlock = await provider.getBlock(currentBlockNumber);
// Get the timestamp of the current block
const currentBlockTimestamp = currentBlock.timestamp;
return currentBlockTimestamp;
}
// Usage
getCurrentBlockTimestamp()
.then((timestamp) => {
console.log("Current Block Timestamp:", timestamp);
})
.catch((error) => {
console.error("Error:", error);
});