1

I have a function where a specific time should be passed to run it.

function placeBid(...) {
  require(auction.startedAt <= block.timestamp, "not started yet");
  ...
}

say startedAt is a unix value of 1648198972 I'm testing with

    beforeEach(async function () {
      await ethers.provider.send("evm_setNextBlockTimestamp", [parseInt(auction.startedAt)]);
      await ethers.provider.send("evm_mine");
    });
      expect(await Auction.connect(user2).placeBid(...))});
describe("Success", function () {
      it("Should send bid to previous bidder", async function () {
        expect(bidder).to.equal(user2.address);
        block = await ethers.provider.getBlock("latest");
        console.log("current: ", block.timestamp)
      });
      it("....", async function () {
       ...
      });
});

This can actually run placeBid which seems it passed the require statement, but I get an error related to timestamp!

Truth is, it always passes the first it statement, but it throws this timestamp error when it goes to the next it statement.

InvalidInputError: Timestamp 1648198972 is lower than or equal to previous block's timestamp 1648198986

what's wrong?

bbusdriver
  • 1,144
  • 2
  • 18
  • 33
  • Are you sure it's being run correctly? Might wanna make sure that 'parseInt(auction.startedAt)' is valid (maybe you wanted 'parseInt(await auction.startedAt())'?) and print out current timestamp (to make sure that the new one is bigger). – phaze Feb 25 '22 at 09:50
  • thanks for the comment, but those are not the problems – bbusdriver Feb 25 '22 at 10:44
  • So it's failing at the "evm_setNextBlockTimestamp" line? Your timestamp needs to be larger than the current timestamp, which would explain the error. Simply removing that line setting the timestamp should work then. – phaze Feb 25 '22 at 11:52
  • it''s failing at the second it statement and that line is needed since i need to move current time to the future time i.e. 2 days later so require statement for auction.startedAt can be passed – bbusdriver Feb 25 '22 at 11:56
  • The error is basically saying that you are trying to set the time to the past, meaning that auction.startedAt is already in the past. – phaze Feb 25 '22 at 12:04
  • the unix value i'm using for startedAt is 1648198972 which is March 25 one month away. – bbusdriver Feb 25 '22 at 12:14

1 Answers1

1

As per this answer -

Keep in mind that Hardhat Network validates that the new timestamp is bigger than the previous one, so you can't send any value.

It appears that the issue stems from the fact that you're trying to mine a new block in the past (by setting a timestamp before the last block) leading to the given error.

A simple fix could be to modify startedAt to a timestamp in the distant future so that this error can be avoided altogether.

ruvaag
  • 354
  • 1
  • 5
  • ok but the unix value i'm using for startedAt is 1648198972 which is March 25 one month away. – bbusdriver Feb 25 '22 at 12:13
  • As per your HH error the the local network has already passed that timestamp (the last block was mined at 1648198986) – ruvaag Feb 25 '22 at 12:19
  • yeah weird, i don't understand why it even successfully ran placeBid and also the first it statement, then failing at the second it when the block was mined at 1648198986 – bbusdriver Feb 25 '22 at 12:24
  • Just noticed that you've used the beforeEach() handle. I'd suspect that if there were other tests running before/after this one they'd also call this handle and try to set the block timestamp to startedAt which could explain this behavior. – ruvaag Feb 25 '22 at 12:29
  • yeah i'm running other tests after that then what should i do? – bbusdriver Feb 25 '22 at 12:36
  • Suggest refactoring tests so that the timestamp is only set in that particular test i.e. move it out of the beforeEach() block. Also recommend going through this to refactor other tests. – ruvaag Feb 25 '22 at 12:41