Skip to content

Commit

Permalink
chore: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dutterbutter committed Sep 28, 2023
1 parent 6ba41c0 commit 3554e85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions contracts/contracts/paymasters/TimeBasedPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 20 additions & 14 deletions contracts/test/timeBased.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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;
});
});

0 comments on commit 3554e85

Please sign in to comment.