From 3554e859f0ff755f8866f76e5161fcd87cfc9de9 Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Wed, 27 Sep 2023 19:14:34 -0500 Subject: [PATCH] chore: fix unit tests --- .../paymasters/TimeBasedPaymaster.sol | 4 +-- contracts/test/timeBased.test.ts | 34 +++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/contracts/contracts/paymasters/TimeBasedPaymaster.sol b/contracts/contracts/paymasters/TimeBasedPaymaster.sol index 76772ff..a9c1395 100644 --- a/contracts/contracts/paymasters/TimeBasedPaymaster.sol +++ b/contracts/contracts/paymasters/TimeBasedPaymaster.sol @@ -38,8 +38,8 @@ contract TimeBasedPaymaster is IPaymaster, Ownable { bytes4 paymasterInputSelector = bytes4(_transaction.paymasterInput[0:4]); if (paymasterInputSelector == IPaymasterFlow.general.selector) { - uint256 startTime = (block.timestamp / 86400) * 86400 + 9 hours; - uint256 endTime = startTime + 3 minutes; + uint256 startTime = (block.timestamp / 86400) * 86400 + 14 hours; + uint256 endTime = startTime + 10 minutes; require( block.timestamp >= startTime && block.timestamp <= endTime, diff --git a/contracts/test/timeBased.test.ts b/contracts/test/timeBased.test.ts index a22cb64..a2aac75 100644 --- a/contracts/test/timeBased.test.ts +++ b/contracts/test/timeBased.test.ts @@ -9,7 +9,9 @@ import { deployContract, fundAccount, setupDeployer } from "./utils"; import dotenv from "dotenv"; dotenv.config(); -const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY || ""; +const PRIVATE_KEY = + process.env.WALLET_PRIVATE_KEY || + "0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110"; describe("TimeBasedPaymaster", function () { let provider: Provider; @@ -30,11 +32,6 @@ describe("TimeBasedPaymaster", function () { await fundAccount(wallet, paymaster.address, "3"); }); - async function setTimeTo(timestamp) { - await provider.send("evm_setNextBlockTimestamp", [timestamp]); - await provider.send("evm_mine", []); - } - async function executeGreetingTransaction(user: Wallet) { const gasPrice = await provider.getGasPrice(); @@ -61,27 +58,36 @@ describe("TimeBasedPaymaster", function () { it("should cost the user no gas during the time window", async function () { // Arrange const currentDate = new Date(); - currentDate.setUTCHours(23); - currentDate.setUTCMinutes(10); + currentDate.setUTCHours(14); + currentDate.setUTCMinutes(1); currentDate.setUTCSeconds(0); currentDate.setUTCMilliseconds(0); const targetTime = Math.floor(currentDate.getTime() / 1000); - await setTimeTo(targetTime); + await provider.send("evm_setNextBlockTimestamp", [targetTime]); + // Act const initialBalance = await userWallet.getBalance(); await executeGreetingTransaction(userWallet); + await provider.send("evm_mine", []); const newBalance = await userWallet.getBalance(); + // Assert expect(newBalance.toString()).to.equal(initialBalance.toString()); }); - it("should cost the user gas outside the time window", async function () { + it("should fail due to Paymaster validation error outside the time window", async function () { // Arrange - const initialBalance = await wallet.getBalance(); + let errorOccurred = false; + // Act - await executeGreetingTransaction(wallet); - const newBalance = await wallet.getBalance(); + try { + await executeGreetingTransaction(wallet); + } catch (error) { + errorOccurred = true; + expect(error.message).to.include("Paymaster validation error"); + } + // Assert - expect(newBalance.lt(initialBalance)).to.be.true; + expect(errorOccurred).to.be.true; }); });