Hardhat - How to get the block in timestamp in deployment file
Asked Answered
A

2

7

I try to deploy my masterchef contract. I have 2 arguments to specify in the constructor (the token address and the actual block number in secondes). I use hardhat for doing this and I actually trying to get the value of the actuel block number in secondes ? How can I do this ?

const StakingRewards = await hre.ethers.getContractFactory("Masterchef");
const stakingRewards = await StakingRewards.deploy(token.address, block.timestamp);
Alper answered 26/12, 2021 at 18:49 Comment(0)
G
14

You can get the timestamp of the previous block like this:

// getting timestamp
const blockNumBefore = await ethers.provider.getBlockNumber();
const blockBefore = await ethers.provider.getBlock(blockNumBefore);
const timestampBefore = blockBefore.timestamp;
Grasshopper answered 6/2, 2022 at 14:59 Comment(0)
E
2

You can use the time helpers from the Hardhat Network Helpers library:

const { time } = require('@nomicfoundation/hardhat-network-helpers');
const timestamp = await time.latest();
Ernaline answered 26/2, 2023 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.