4

I have a time-dependent contract that has two require statements

    function launch(uint256 _goal, uint32 _startAt, uint32 _endAt) external {
        require(_startAt >= block.timestamp, "start at < now");
        require(
            _endAt >= _startAt + minDuration &&
                _endAt <= _startAt + maxDuration,
            "not in min & max duration"
        );

        count += 1;
        campaigns[count] = Campaign({
            id: count,
            creator: msg.sender,
            goal: _goal,
            pledged: 0,
            startAt: _startAt,
            endAt: _endAt,
            claimed: false,
            cancelled: false
        });

        emit Launch(count, msg.sender, _goal, _startAt, _endAt);
    }

I am testing the contract in hardhat and I am trying to test the condition that the campaign is too short (i.e. endAt < startAt + minDuration). However, I keep getting the error "Error: VM Exception while processing transaction: reverted with reason string 'start at < now'" instead of the "not in min & max duration" error. In hardhat I am using the following code to test this condition:

        it.only('should revert if campaign too short', async () => {
          blockTimestamp = (await ethers.provider.getBlock('latest')).timestamp;
          const endAt = blockTimestamp + (Number(minDuration) - 10);

          console.log(endAt); // 1676757538
          console.log(blockTimestamp); // 1676757518

          expect(
            await crowdfund.launch(campaignGoal, blockTimestamp, endAt)
          ).to.be.revertedWith('not in min & max duration');
        });

for reference minDuration = ethers.BigNumber.from('30')

2 Answers 2

2

I think the first require statement fails, because you are passing the blockTimestamp value to the launch function as startAt.

Try to test the condition where the campaign is too short by adding a few seconds to the current blockTimestamp to ensure the startAt is in the future:

it.only('should revert if campaign too short', async () => {
  blockTimestamp = (await ethers.provider.getBlock('latest')).timestamp;
  const startAt = blockTimestamp + 10;
  const endAt = startAt + Number(minDuration) - 10;

  console.log(startAt);
  console.log(endAt);

  await expect(
    crowdfund.launch(campaignGoal, startAt, endAt)
  ).to.be.revertedWith('not in min & max duration');
});
2
  • thanks worked liked a charm! Feb 19, 2023 at 0:58
  • glad that it worked for you!
    – timsntech
    Feb 19, 2023 at 1:45
0

You can manipulate time with this. 606024 = 1 day.

    await hre.ethers.provider.send("evm_increaseTime", [60 * 60 * 24])
    await hre.ethers.provider.send("evm_mine")
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.