Skip to content

Commit

Permalink
feat: handle create errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Zer0dot committed May 21, 2024
1 parent ff938ce commit 920aad0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/common/BaseLightAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
error NotAuthorized(address caller);
error ZeroAddressNotAllowed();
error OnlyCallableBySelf();
error CreateFailed();

modifier onlyAuthorized() {
_onlyAuthorized();
Expand Down Expand Up @@ -81,15 +82,22 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
/// depending on gas savings, if any.
function create(bytes calldata initCode) external payable virtual {
assembly ("memory-safe") {
// Check that the caller is this account, compiles to the same as inverting the condition.
// Check that the caller is this account, this compiles to the same as inverting the condition
if iszero(eq(caller(), address())) {
mstore(0, 0x913e98f1)
mstore(0, 0x913e98f1) // OnlyCallableBySelf()
revert(28, 4)
}

// Copy the initCode to memory, then deploy the contract
let len := initCode.length
calldatacopy(0, initCode.offset, len)
let succ := create(callvalue(), 0, len)
return(0, 0)

// If the creation fails, revert
if iszero(succ) {
mstore(0, 0x7e16b8cd) // CreateFailed()
revert(28, 4)
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/LightAccount.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,19 @@ contract LightAccountTest is Test {
account.create(hex"1234");
}

function testRevertCreateContract_CreateFailed() public {
vm.prank(eoaAddress);
vm.expectRevert(BaseLightAccount.CreateFailed.selector);
account.execute(
address(account),
0,
abi.encodeCall(
account.create,
(hex"01") // Attempt to deploy a contract with a single "ADD" opcode as the whole initcode, which will revert.
)
);
}

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

0 comments on commit 920aad0

Please sign in to comment.