Skip to content

Commit

Permalink
basic state tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivaanshK committed Jun 28, 2024
1 parent 967379c commit 2e32ee2
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 16 deletions.
92 changes: 87 additions & 5 deletions test/foundry/SponsorshipPaymasterWithPremium.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,99 @@ import { NexusTestBase } from "./base/NexusTestBase.sol";

import { BiconomySponsorshipPaymaster } from "../../contracts/sponsorship/SponsorshipPaymasterWithPremium.sol";


contract SponsorshipPaymasterWithPremiumTest is NexusTestBase {
BiconomySponsorshipPaymaster public bicoPaymaster;

function setUp() public {
setupTestEnvironment();
// Deploy Sponsorship Paymaster
bicoPaymaster = new BiconomySponsorshipPaymaster(ALICE_ADDRESS, ENTRYPOINT, BOB_ADDRESS, CHARLIE_ADDRESS);

// Deposit funds for paymaster id
vm.startPrank(CHARLIE_ADDRESS);
ENTRYPOINT.depositTo{ value: 10 ether }(address(bicoPaymaster));
vm.stopPrank();
}

function testDeploy() external {
BiconomySponsorshipPaymaster testArtifact = new BiconomySponsorshipPaymaster(BOB_ADDRESS, ENTRYPOINT, ALICE_ADDRESS, CHARLIE_ADDRESS);
assertEq(address(testArtifact.owner()), BOB_ADDRESS);
BiconomySponsorshipPaymaster testArtifact =
new BiconomySponsorshipPaymaster(ALICE_ADDRESS, ENTRYPOINT, BOB_ADDRESS, CHARLIE_ADDRESS);
assertEq(testArtifact.owner(), ALICE_ADDRESS);
assertEq(address(testArtifact.entryPoint()), ENTRYPOINT_ADDRESS);
assertEq(address(testArtifact.verifyingSigner()), ALICE_ADDRESS);
assertEq(address(testArtifact.feeCollector()), CHARLIE_ADDRESS);
assertEq(testArtifact.verifyingSigner(), BOB_ADDRESS);
assertEq(testArtifact.feeCollector(), CHARLIE_ADDRESS);
}

function testCheckStates() external {
assertEq(bicoPaymaster.owner(), ALICE_ADDRESS);
assertEq(address(bicoPaymaster.entryPoint()), ENTRYPOINT_ADDRESS);
assertEq(bicoPaymaster.verifyingSigner(), BOB_ADDRESS);
assertEq(bicoPaymaster.feeCollector(), CHARLIE_ADDRESS);
}

function testOwnershipTransfer() external {
vm.startPrank(ALICE_ADDRESS);
assertEq(bicoPaymaster.owner(), ALICE_ADDRESS);
bicoPaymaster.transferOwnership(DAN_ADDRESS);
assertEq(bicoPaymaster.owner(), DAN_ADDRESS);
vm.stopPrank();
}

function testInvalidOwnershipTransfer() external {
vm.startPrank(ALICE_ADDRESS);
assertEq(bicoPaymaster.owner(), ALICE_ADDRESS);
vm.expectRevert(abi.encodeWithSignature("NewOwnerIsZeroAddress()"));
bicoPaymaster.transferOwnership(address(0));
vm.stopPrank();

vm.startPrank(DAN_ADDRESS);
assertEq(bicoPaymaster.owner(), ALICE_ADDRESS);
vm.expectRevert(abi.encodeWithSignature("Unauthorized()"));
bicoPaymaster.transferOwnership(DAN_ADDRESS);
vm.stopPrank();
}

function testChangingVerifyingSigner() external {
vm.startPrank(ALICE_ADDRESS);
assertEq(bicoPaymaster.verifyingSigner(), BOB_ADDRESS);
bicoPaymaster.setSigner(DAN_ADDRESS);
assertEq(bicoPaymaster.verifyingSigner(), DAN_ADDRESS);
vm.stopPrank();
}

function testInvalidChangingVerifyingSigner() external {
vm.startPrank(ALICE_ADDRESS);
assertEq(bicoPaymaster.verifyingSigner(), BOB_ADDRESS);
vm.expectRevert(abi.encodeWithSignature("VerifyingSignerCannotBeZero()"));
bicoPaymaster.setSigner(address(0));
vm.stopPrank();

vm.startPrank(DAN_ADDRESS);
assertEq(bicoPaymaster.verifyingSigner(), BOB_ADDRESS);
vm.expectRevert(abi.encodeWithSignature("Unauthorized()"));
bicoPaymaster.setSigner(DAN_ADDRESS);
vm.stopPrank();
}

function testChangingFeeCollector() external {
vm.startPrank(ALICE_ADDRESS);
assertEq(bicoPaymaster.feeCollector(), CHARLIE_ADDRESS);
bicoPaymaster.setFeeCollector(DAN_ADDRESS);
assertEq(bicoPaymaster.feeCollector(), DAN_ADDRESS);
vm.stopPrank();
}

function testInvalidChangingFeeCollector() external {
vm.startPrank(ALICE_ADDRESS);
assertEq(bicoPaymaster.feeCollector(), CHARLIE_ADDRESS);
vm.expectRevert(abi.encodeWithSignature("FeeCollectorCannotBeZero()"));
bicoPaymaster.setFeeCollector(DAN_ADDRESS);
vm.stopPrank();

vm.startPrank(DAN_ADDRESS);
assertEq(bicoPaymaster.feeCollector(), CHARLIE_ADDRESS);
vm.expectRevert(abi.encodeWithSignature("Unauthorized()"));
bicoPaymaster.setFeeCollector(DAN_ADDRESS);
vm.stopPrank();
}
}
41 changes: 30 additions & 11 deletions test/foundry/base/NexusTestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ import { MockHook } from "@nexus/contracts/mocks/MockHook.sol";
// import { MockExecutor } from "@nexus/contracts/mocks/MockExecutor.sol";
import { MockHandler } from "@nexus/contracts/mocks/MockHandler.sol";
import { BootstrapLib } from "@nexus/contracts/lib/BootstrapLib.sol";
import { ModeLib, ExecutionMode, ExecType, CallType, CALLTYPE_BATCH, CALLTYPE_SINGLE, EXECTYPE_DEFAULT, EXECTYPE_TRY } from "@nexus/contracts/lib/ModeLib.sol";
import {
ModeLib,
ExecutionMode,
ExecType,
CallType,
CALLTYPE_BATCH,
CALLTYPE_SINGLE,
EXECTYPE_DEFAULT,
EXECTYPE_TRY
} from "@nexus/contracts/lib/ModeLib.sol";
// import { ExecLib, Execution } from "@nexus/contracts/lib/ExecLib.sol";
import { Bootstrap, BootstrapConfig } from "@nexus/contracts/utils/Bootstrap.sol";
import { CheatCodes } from "@nexus/test/foundry/utils/CheatCodes.sol";
import { EventsAndErrors } from "@nexus/test/foundry/utils/EventsAndErrors.sol";

import { BiconomySponsorshipPaymaster } from "../../../contracts/sponsorship/SponsorshipPaymasterWithPremium.sol";



abstract contract NexusTestBase is CheatCodes, EventsAndErrors {
// -----------------------------------------
// State Variables
Expand All @@ -39,23 +46,28 @@ abstract contract NexusTestBase is CheatCodes, EventsAndErrors {
Vm.Wallet internal BOB;
Vm.Wallet internal ALICE;
Vm.Wallet internal CHARLIE;
Vm.Wallet internal DAN;
Vm.Wallet internal EMMA;
Vm.Wallet internal BUNDLER;
Vm.Wallet internal FACTORY_OWNER;

address constant ENTRYPOINT_ADDRESS = address(0x0000000071727De22E5E9d8BAf0edAc6f37da032);

address internal BOB_ADDRESS;
address internal ALICE_ADDRESS;
address internal CHARLIE_ADDRESS;
address internal DAN_ADDRESS;
address internal EMMA_ADDRESS;

Nexus internal BOB_ACCOUNT;
Nexus internal ALICE_ACCOUNT;
Nexus internal CHARLIE_ACCOUNT;
Nexus internal DAN_ACCOUNT;
Nexus internal EMMA_ACCOUNT;

address constant ENTRYPOINT_ADDRESS = address(0x0000000071727De22E5E9d8BAf0edAc6f37da032);
IEntryPoint internal ENTRYPOINT;

NexusAccountFactory internal FACTORY;
BiconomyMetaFactory internal META_FACTORY;
MockHook internal HOOK_MODULE;
MockHandler internal HANDLER_MODULE;
// MockExecutor internal EXECUTOR_MODULE;
MockValidator internal VALIDATOR_MODULE;
Expand Down Expand Up @@ -83,14 +95,17 @@ abstract contract NexusTestBase is CheatCodes, EventsAndErrors {
function setupPredefinedWallets() internal {
DEPLOYER = createAndFundWallet("DEPLOYER", 1000 ether);

BOB = createAndFundWallet("BOB", 1000 ether);
BOB_ADDRESS = BOB.addr;

ALICE = createAndFundWallet("ALICE", 1000 ether);
BOB = createAndFundWallet("BOB", 1000 ether);
CHARLIE = createAndFundWallet("CHARLIE", 1000 ether);
DAN = createAndFundWallet("DAN", 1000 ether);
EMMA = createAndFundWallet("EMMA", 1000 ether);

ALICE_ADDRESS = ALICE.addr;
BOB_ADDRESS = BOB.addr;
CHARLIE_ADDRESS = CHARLIE.addr;
DAN_ADDRESS = DAN.addr;
EMMA_ADDRESS = EMMA.addr;

FACTORY_OWNER = createAndFundWallet("FACTORY_OWNER", 1000 ether);
}
Expand All @@ -104,7 +119,6 @@ abstract contract NexusTestBase is CheatCodes, EventsAndErrors {
META_FACTORY = new BiconomyMetaFactory(address(FACTORY_OWNER.addr));
vm.prank(FACTORY_OWNER.addr);
META_FACTORY.addFactoryToWhitelist(address(FACTORY));
HOOK_MODULE = new MockHook();
HANDLER_MODULE = new MockHandler();
// EXECUTOR_MODULE = new MockExecutor();
VALIDATOR_MODULE = new MockValidator();
Expand Down Expand Up @@ -140,6 +154,10 @@ abstract contract NexusTestBase is CheatCodes, EventsAndErrors {
vm.label(address(ALICE_ACCOUNT), "ALICE_ACCOUNT");
CHARLIE_ACCOUNT = deployNexus(CHARLIE, 100 ether, address(VALIDATOR_MODULE));
vm.label(address(CHARLIE_ACCOUNT), "CHARLIE_ACCOUNT");
DAN_ACCOUNT = deployNexus(DAN, 100 ether, address(VALIDATOR_MODULE));
vm.label(address(DAN_ACCOUNT), "DAN_ACCOUNT");
EMMA_ACCOUNT = deployNexus(EMMA, 100 ether, address(VALIDATOR_MODULE));
vm.label(address(EMMA_ACCOUNT), "EMMA_ACCOUNT");
}
// -----------------------------------------
// Utility Functions
Expand Down Expand Up @@ -599,7 +617,8 @@ abstract contract NexusTestBase is CheatCodes, EventsAndErrors {
}

// /// @notice Generates and signs the paymaster data for a user operation.
// /// @dev This function prepares the `paymasterAndData` field for a `PackedUserOperation` with the correct signature.
// /// @dev This function prepares the `paymasterAndData` field for a `PackedUserOperation` with the correct
// signature.
// /// @param userOp The user operation to be signed.
// /// @param signer The wallet that will sign the paymaster hash.
// /// @param paymaster The paymaster contract.
Expand Down

0 comments on commit 2e32ee2

Please sign in to comment.