Skip to content

Commit

Permalink
Improving tests and deployment scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
eloi010 committed Jul 18, 2023
1 parent e5e2c88 commit 5f6e97d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
4 changes: 2 additions & 2 deletions contracts/core/eip6551/EIP6551OpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import "account-abstraction/core/Helpers.sol" as Helpers;
/**
* @title EIP6551OpenfortAccount (Non-upgradeable)
* @author Eloi<[email protected]>
* @notice Minimal smart contract wallet with session keys following the ERC-4337 standard.
* @notice Smart contract wallet with session keys following the ERC-4337 and EIP-6551 standards.
* It inherits from:
* - BaseAccount to comply with ERC-4337
* - Initializable because accounts are meant to be created using Factories
* - IERC6551Account to have permissions using ERC-721
* - IERC6551Account to have permissions using ERC-721 tokens
* - EIP712Upgradeable to use typed structured signatures EIP-712 (supporting ERC-5267 too)
* - IERC1271Upgradeable for Signature Validation (ERC-1271)
* - TokenCallbackHandler to support ERC-777, ERC-721 and ERC-1155
Expand Down
10 changes: 8 additions & 2 deletions script/deployEIP6551.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ contract EIP6551OpenfortDeploy is Script {
testToken = new VIPNFT();

// The first call should create a new account, while the second will just return the corresponding account address
address account2 =
erc6551Registry.createAccount(address(eip6551OpenfortAccount), chainId, address(testToken), 1, 1, "");
address account2 = erc6551Registry.createAccount(
address(eip6551OpenfortAccount),
chainId,
address(testToken),
1,
1,
abi.encodeWithSignature("initialize(address)", address(entryPoint))
);
console.log("Registry at address %s has created an account at address %s", address(erc6551Registry), account2);

vm.stopBroadcast();
Expand Down
89 changes: 87 additions & 2 deletions test/foundry/core/eip6551/EIP6551OpenfortAccountTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.19;
import {Test, console} from "lib/forge-std/src/Test.sol";
import {SigUtils} from "../../utils/SigUtils.sol";
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {EntryPoint, UserOperation} from "account-abstraction/core/EntryPoint.sol";
import {EntryPoint, IEntryPoint, UserOperation} from "account-abstraction/core/EntryPoint.sol";
import {VIPNFT} from "contracts/mock/VipNFT.sol";
import {ERC6551Registry} from "contracts/core/eip6551/ERC6551Registry.sol";
import {EIP6551OpenfortAccount} from "contracts/core/eip6551/EIP6551OpenfortAccount.sol";
Expand All @@ -15,6 +15,7 @@ contract EIP6551OpenfortAccountTest is Test {
EntryPoint public entryPoint;
ERC6551Registry public erc6551Registry;
EIP6551OpenfortAccount public eip6551OpenfortAccount;
EIP6551OpenfortAccount implEIP6551OpenfortAccount;
address public account;
VIPNFT testToken;

Expand Down Expand Up @@ -149,7 +150,7 @@ contract EIP6551OpenfortAccountTest is Test {
// deploy a new VIPNFT collection
testToken = new VIPNFT();

EIP6551OpenfortAccount implEIP6551OpenfortAccount = new EIP6551OpenfortAccount();
implEIP6551OpenfortAccount = new EIP6551OpenfortAccount();

erc6551Registry = new ERC6551Registry();

Expand All @@ -164,6 +165,90 @@ contract EIP6551OpenfortAccountTest is Test {
vm.stopPrank();
}

/*
* Test reinitialize. It should fail.
*
*/
function testFailReinitialize() public {
eip6551OpenfortAccount.initialize(address(entryPoint));
}

/*
* Test initialize implementation. It should fail.
*/
function testFailInitializeImplementation() public {
implEIP6551OpenfortAccount.initialize(address(entryPoint));
}

/*
* Check implementation has not been initialized.
* EntryPoint address should be 0. Should pass.
*/
function testImplementationNoEntryPointAddr() public {
IEntryPoint e = implEIP6551OpenfortAccount.entryPoint();
assertEq(address(e), address(0));
}

/*
* Create a 2nd account using the same technique than in setup with a new salt (2).
*/
function testCreate2ndAcc() public {
uint256 chainId;
assembly {
chainId := chainid()
}
address eip6551OpenfortAccountAddress2 =
erc6551Registry.createAccount(address(implEIP6551OpenfortAccount), chainId, address(testToken), 1, 2, "");

EIP6551OpenfortAccount eip6551OpenfortAccount2 = EIP6551OpenfortAccount(payable(eip6551OpenfortAccountAddress2));
eip6551OpenfortAccount2.initialize(address(entryPoint));
IEntryPoint e = eip6551OpenfortAccount2.entryPoint();
assertEq(address(e), address(entryPoint));
}

/*
* Create a new account using createAccount() and the initializer.
*/
function testCreateAccInitializer() public {
uint256 chainId;
assembly {
chainId := chainid()
}
address eip6551OpenfortAccountAddress2 = erc6551Registry.createAccount(
address(implEIP6551OpenfortAccount),
chainId,
address(testToken),
1,
2,
abi.encodeWithSignature("initialize(address)", address(entryPoint))
);
EIP6551OpenfortAccount eip6551OpenfortAccount2 = EIP6551OpenfortAccount(payable(eip6551OpenfortAccountAddress2));
IEntryPoint e = eip6551OpenfortAccount2.entryPoint();
assertEq(address(e), address(entryPoint));
}

/*
* Create a new account using createAccount() and the initializer.
* Test initialize again should fail.
*/
function testFailCreateAccInitializerNoReinit() public {
uint256 chainId;
assembly {
chainId := chainid()
}
address eip6551OpenfortAccountAddress2 = erc6551Registry.createAccount(
address(implEIP6551OpenfortAccount),
chainId,
address(testToken),
1,
2,
abi.encodeWithSignature("initialize(address)", address(entryPoint))
);

EIP6551OpenfortAccount eip6551OpenfortAccount2 = EIP6551OpenfortAccount(payable(eip6551OpenfortAccountAddress2));
eip6551OpenfortAccount2.initialize(address(entryPoint));
}

/*
* Test getDeposit() function.
* First ERC4337 function called by this EIP6551-compatible account.
Expand Down

0 comments on commit 5f6e97d

Please sign in to comment.