I am trying to test a contract in which every 4 minutes the price of token is changing.
But I am not able to test the contract as I am unable to manipulate the time.The test without time manipulations are running fine but I have no idea about the time manipulation in hardhat. please help.
Here is the code.
const { expect } = require("chai");
const { ethers } = require("hardhat");
const {helpers}= require ("@nomicfoundation/hardhat-network-helpers");
console.log(mine)
describe("Token contract", function () {
it("Deployment should assign the total supply of tokens to itself", async function () {
const [owner] = await ethers.getSigners();
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy();
const contractBalance = await token.balanceOf(token.address);
expect(await token.totalSupply()).to.equal(contractBalance);
});
it("buyer can buy the token", async function () {
const [owner, buyer] = await ethers.getSigners();
const Crowdsale = await ethers.getContractFactory("Crowdsale");
const crowdsale = await Crowdsale.deploy();
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy();
await crowdsale.setTokenAddress(token.address);
await token.approveContract(crowdsale.address);
await crowdsale.setStart();
await crowdsale.connect(buyer).buyToken({ value: ethers.utils.parseEther("1") })
expect(await token.balanceOf(buyer.address)).to.equal("1000000000000000000000")
})
it("Prices should change accoording to time", async function () {
const [owner, buyer] = await ethers.getSigners();
const Crowdsale = await ethers.getContractFactory("Crowdsale");
const crowdsale = await Crowdsale.deploy();
const Token = await ethers.getContractFactory("Token");
const token = await Token.deploy();
await crowdsale.setTokenAddress(token.address);
await token.approveContract(crowdsale.address);
await crowdsale.setStart();
await helpers.time.increase(240);
await crowdsale.connect(buyer).buyToken({ value: ethers.utils.parseEther("1") })
expect(await token.balanceOf(buyer.address)).to.equal("1000000000000000000000")
await helpers.time.increase(240);
await crowdsale.connect(buyer).buyToken({ value: ethers.utils.parseEther("1") })
expect(await token.balanceOf(buyer.address)).to.equal("500000000000000000000")
await helpers.time.increase(240);
await crowdsale.connect(buyer).buyToken({ value: ethers.utils.parseEther("1") })
expect(await token.balanceOf(buyer.address)).to.equal("200000000000000000000")
})
});
Best Answer
to use the hardhat-network-helpers library you should change your
require
to thisNow you have access to all helper functions like
All the helper functions can be found at the hardhat docs @ https://hardhat.org/hardhat-network-helpers/docs/reference