Skip to content

Commit

Permalink
feat: update signatures and tests to match
Browse files Browse the repository at this point in the history
  • Loading branch information
Zer0dot committed May 29, 2024
1 parent f73ef69 commit cbdd9d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/common/BaseLightAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
}

/// @notice Creates a contract.
/// @param initCode The initCode to deploy.
/// @param value The value to send to the new contract constructor.
/// @param initCode The initCode to deploy.
/// @return createdAddr The created contract address.
///
/// @dev Assembly procedure:
Expand All @@ -87,7 +87,7 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
/// 3. Copy the initCode from callata to memory at the free memory pointer.
/// 4. Create the contract.
/// 5. If creation failed (the address returned is zero), revert with CreateFailed().
function create(bytes calldata initCode, uint256 value) external payable virtual onlyAuthorized returns (address createdAddr) {
function performCreate(uint256 value, bytes calldata initCode) external payable virtual onlyAuthorized returns (address createdAddr) {
assembly ("memory-safe") {

let fmp := mload(0x40)
Expand All @@ -104,9 +104,9 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
}

/// @notice Creates a contract using create2 deterministic deployment.
/// @param value The value to send to the new contract constructor.
/// @param initCode The initCode to deploy.
/// @param salt The salt to use for the create2 operation.
/// @param value The value to send to the new contract constructor.
/// @return createdAddr The created contract address.
///
/// @dev Assembly procedure:
Expand All @@ -115,7 +115,7 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
/// 3. Copy the initCode from callata to memory at the free memory pointer.
/// 4. Create the contract using Create2 with the passed salt parameter.
/// 5. If creation failed (the address returned is zero), revert with CreateFailed().
function create2(bytes calldata initCode, bytes32 salt, uint256 value) external payable virtual onlyAuthorized returns (address createdAddr) {
function performCreate2(uint256 value, bytes calldata initCode, bytes32 salt) external payable virtual onlyAuthorized returns (address createdAddr) {
assembly ("memory-safe") {

let fmp := mload(0x40)
Expand Down
49 changes: 36 additions & 13 deletions test/LightAccount.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -479,38 +479,45 @@ contract LightAccountTest is Test {

function testRevertCreate_IncorrectCaller() public {
vm.expectRevert(abi.encodeWithSelector(BaseLightAccount.NotAuthorized.selector, address(this)));
account.create(hex"1234", 0);
account.performCreate(0, hex"1234");
}

function testRevertCreate_CreateFailed() public {
vm.prank(eoaAddress);
vm.expectRevert(BaseLightAccount.CreateFailed.selector);
account.execute(
address(account),
0,
abi.encodeCall(
account.create,
(hex"3d3dfd", 0) // Attempt to deploy a contract with creation code that reverts.
)
);
account.performCreate(0, hex"3d3dfd");
}

function testRevertCreate2_IncorrectCaller() public {
vm.expectRevert(abi.encodeWithSelector(BaseLightAccount.NotAuthorized.selector, address(this)));
account.create2(hex"1234", bytes32(0), 0);
account.performCreate2(0, hex"1234", bytes32(0));
}

function testRevertCreate2_CreateFailed() public {
vm.prank(eoaAddress);
vm.expectRevert(BaseLightAccount.CreateFailed.selector);
account.create2(hex"3d3dfd", bytes32(0), 0);
account.performCreate2(0, hex"3d3dfd", bytes32(0));
}

function testCreate() public {
vm.prank(eoaAddress);
address expected = vm.computeCreateAddress(address(account), vm.getNonce(address(account)));
account.create(abi.encodePacked(type(LightAccount).creationCode, abi.encode(address(entryPoint))), 0);

address returnedAddress = account.performCreate(0, abi.encodePacked(type(LightAccount).creationCode, abi.encode(address(entryPoint))));
assertEq(address(LightAccount(payable(expected)).entryPoint()), address(entryPoint));
assertEq(returnedAddress, expected);
}

function testCreateValue() public {
vm.prank(eoaAddress);
address expected = vm.computeCreateAddress(address(account), vm.getNonce(address(account)));

uint256 value = 1 ether;
deal(address(account), value);

address returnedAddress = account.performCreate(value, "");
assertEq(returnedAddress, expected);
assertEq(returnedAddress.balance, value);
}

function testCreate2() public {
Expand All @@ -520,8 +527,24 @@ contract LightAccountTest is Test {
bytes32 salt = bytes32(hex"04546b");
address expected = vm.computeCreate2Address(salt, initCodeHash, address(account));

account.create2(abi.encodePacked(type(LightAccount).creationCode, abi.encode(address(entryPoint))), salt, 0);
address returnedAddress = account.performCreate2(0, initCode, salt);
assertEq(address(LightAccount(payable(expected)).entryPoint()), address(entryPoint));
assertEq(returnedAddress, expected);
}

function testCreate2Value() public {
vm.prank(eoaAddress);
bytes memory initCode = "";
bytes32 initCodeHash = keccak256(initCode);
bytes32 salt = bytes32(hex"04546b");
address expected = vm.computeCreate2Address(salt, initCodeHash, address(account));

uint256 value = 1 ether;
deal(address(account), value);

address returnedAddress = account.performCreate2(value, initCode, salt);
assertEq(returnedAddress, expected);
assertEq(returnedAddress.balance, value);
}

function _useContractOwner() internal {
Expand Down

0 comments on commit cbdd9d6

Please sign in to comment.