claim() reverts after evm_increaseTime in Hardhat test – is block.timestamp or contract balance the issue? #6807
Closed
CoderGuru007
started this conversation in
General
Replies: 1 comment
-
Hey, this isn't really a Hardhat issue. I think everything we expect Hardhat to do is working. A few points though:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I’m testing a Solidity smart contract for a project. A user buys a short-term policy that expires in 5 seconds. After advancing blockchain time by 10 seconds, I try to call claim(), but the transaction keeps reverting.
What I’m trying to do: • Deploy a CrashInsurance contract • Have a user buy a policy that expires in 5 seconds • Use Hardhat’s evm_increaseTime(10) and evm_mine() to simulate time passing • Call claim() after the expiry time • Expect the payout to succeed if the crash trigger was hit
⸻
Solidity contract (CrashInsurance.sol):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CrashInsurance {
address public owner;
}
And Here Is The Test script (testCrashInsurance.js):
const hre = require("hardhat");
async function main() {
const [owner, user] = await hre.ethers.getSigners();
const CrashInsurance = await hre.ethers.getContractFactory("CrashInsurance");
const crash = await CrashInsurance.deploy();
await crash.waitForDeployment();
console.log(
✅ Contract deployed at: ${crash.address}
);const payout = hre.ethers.parseEther("0.1");
const premium = hre.ethers.parseEther("0.01");
const triggerPrice = 1600;
const latestBlock = await hre.ethers.provider.getBlock("latest");
const expiresAt = latestBlock.timestamp + 5;
console.log("📅 Policy expires at:", expiresAt);
const tx = await crash.connect(user).buyPolicy(payout, triggerPrice, expiresAt, { value: premium });
await tx.wait();
console.log(
✅ Policy purchased by: ${user.address}
);console.log("⏳ Advancing blockchain time by 10 seconds...");
await hre.network.provider.send("evm_increaseTime", [10]);
await hre.network.provider.send("evm_mine");
const newBlock = await hre.ethers.provider.getBlock("latest");
console.log("🕒 Current block timestamp:", newBlock.timestamp);
console.log("💸 Attempting to claim...");
try {
const claimTx = await crash.connect(user).claim(1500);
await claimTx.wait();
console.log("💰 User successfully claimed payout!");
} catch (err) {
console.error("❌ Claim failed:", err.message);
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
And Here Is The Error message output:
Error: Transaction reverted: function call failed to execute
at CrashInsurance.claim (contracts/CrashInsurance.sol:46)
at EdrProviderWrapper.request (.../node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:359:41)
at HardhatEthersSigner.sendTransaction (.../node_modules/@nomicfoundation/hardhat-ethers/src/signers.ts:125:18)
at send (.../node_modules/ethers/src.ts/contract/contract.ts:313:20)
at Proxy.claim (.../node_modules/ethers/src.ts/contract/contract.ts:352:16)
at main (.../scripts/testCrashInsurance.js:23:19)
This Is The Command I Run In Terminal:
npx hardhat run scripts/testCrashInsurance.js
Questions: • Why does the claim revert even after I advance the blockchain time by 10 seconds? • Am I misusing evm_increaseTime or missing something about how block timestamps work? • How can I fix this to properly test time-dependent contracts? Can Someone please help explain this clearly, I am quite new to solidity and visual code editor but I can follow instructions
I deployed the CrashInsurance contract locally using Hardhat. I wrote a test script where a user buys a policy that expires in 5 seconds. Then I advanced the blockchain time by 10 seconds using evm_increaseTime and evm_mine. After that, I called the claim() function, expecting the claim to succeed since the policy had expired and the crash trigger price was met.
However, the transaction reverted with an error saying “function call failed to execute.” This was unexpected because the blockchain time was advanced past the policy expiry, so I expected the claim to pass.
I double-checked that the time was indeed advanced by querying the latest block timestamp, which confirmed the new time was later than the expiry time.
I’m unsure if I’m misusing the time manipulation functions or missing a subtlety in how block timestamps and contract time checks work during tests.
Beta Was this translation helpful? Give feedback.
All reactions