对于Ganache,有几个解决方案。
那安全帽呢?他们实现了自己的本地块链,即“草帽网络”,这与Ganache不同。
发布于 2021-01-29 22:16:43
使用硬帽子网络助手的
最简单的方法是在安全帽网络帮手中使用时间助手。
安装它们时:
npm install @nomicfoundation/hardhat-network-helpers
然后你可以像这样使用它们:
import { time } from "@nomicfoundation/hardhat-network-helpers";
async function main() {
// advance time by one hour and mine a new block
await helpers.time.increase(3600);
// mine a new block with timestamp `newTimestamp`
await helpers.time.increaseTo(newTimestamp);
// set the timestamp of the next block but don't mine a new block
await helpers.time.setNextBlockTimestamp(newTimestamp);
}
main();
您可以检查引用这里。
这里有两个相关的RPC方法:evm_increaseTime
和evm_setNextBlockTimestamp
。在这两种情况下,它们都会影响下一个区块,但不要开采一个区块。
evm_increaseTime
接收到若干秒,这些秒将被添加到最新块的时间戳中。evm_setNextBlockTimestamp
接收一个绝对的UNIX时间戳(同样,以秒为单位),因此它不受当前块的影响。
// suppose the current block has a timestamp of 01:00 PM
await network.provider.send("evm_increaseTime", [3600])
await network.provider.send("evm_mine") // this one will have 02:00 PM as its timestamp
await network.provider.send("evm_setNextBlockTimestamp", [1625097600])
await network.provider.send("evm_mine") // this one will have 2021-07-01 12:00 AM as its timestamp, no matter what the previous block has
请记住,“草帽网络”验证新的时间戳是否大于前一个时间戳,因此您不能发送任何值。
发布于 2021-11-03 17:11:58
发布于 2021-08-12 05:26:35
const { expect } = require("chai");
const { ethers } = require('hardhat');
const sevenDays = 7 * 24 * 60 * 60;
const blockNumBefore = await ethers.provider.getBlockNumber();
const blockBefore = await ethers.provider.getBlock(blockNumBefore);
const timestampBefore = blockBefore.timestamp;
await ethers.provider.send('evm_increaseTime', [sevenDays]);
await ethers.provider.send('evm_mine');
const blockNumAfter = await ethers.provider.getBlockNumber();
const blockAfter = await ethers.provider.getBlock(blockNumAfter);
const timestampAfter = blockAfter.timestamp;
expect(blockNumAfter).to.be.equal(blockNumBefore + 1);
expect(timestampAfter).to.be.equal(timestampBefore + sevenDays);
https://ethereum.stackexchange.com/questions/86633
复制相似问题