Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add contract creation execution functions #61

Merged
merged 13 commits into from
May 31, 2024
25 changes: 14 additions & 11 deletions src/common/BaseLightAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,31 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
function create(bytes calldata initCode, uint256 value) external payable virtual onlyAuthorized {
Zer0dot marked this conversation as resolved.
Show resolved Hide resolved
assembly ("memory-safe") {
// Copy the initCode to memory, then deploy the contract
let ptr := mload(0x40)
Zer0dot marked this conversation as resolved.
Show resolved Hide resolved
let len := initCode.length
calldatacopy(0, initCode.offset, len)
let succ := create(value, 0, len)
calldatacopy(ptr, initCode.offset, len)
let succ := create(value, ptr, len)
Zer0dot marked this conversation as resolved.
Show resolved Hide resolved

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

function create2(bytes calldata initCode, bytes32 salt, uint256 value) external payable virtual onlyAuthorized {
assembly ("memory-safe") {
// Copy the initCode to memory, then deploy the contract
let ptr := mload(0x40)
let len := initCode.length
calldatacopy(0, initCode.offset, len)
let succ := create2(value, 0, len, salt)
calldatacopy(ptr, initCode.offset, len)
let succ := create2(value, ptr, len, salt)

// If the creation fails, revert
if iszero(succ) {
mstore(0, 0x7e16b8cd) // CreateFailed()
revert(28, 4)
mstore(0x00, 0x7e16b8cd) // CreateFailed()
revert(0x1c, 0x04)
}
}
}
Expand Down Expand Up @@ -158,11 +160,12 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg

function _call(address target, uint256 value, bytes memory data) internal {
assembly ("memory-safe") {
let succ := call(gas(), target, value, add(data, 32), mload(data), 0, 0)
let succ := call(gas(), target, value, add(data, 0x20), mload(data), 0x00, 0)
if iszero(succ) {
// We can overwrite memory since we're going to revert out of this call frame anyway
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
let ptr := mload(0x40)
returndatacopy(ptr, 0x00, returndatasize())
revert(ptr, returndatasize())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/LightAccount.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ contract LightAccountTest is Test {
0,
abi.encodeCall(
account.create,
(hex"01", 0) // Attempt to deploy a contract with a single "ADD" opcode as the whole initcode, which will revert.
(hex"3d3dfd", 0) // Attempt to deploy a contract with creation code that reverts.
)
);
}
Expand All @@ -503,7 +503,7 @@ contract LightAccountTest is Test {
function testRevertCreate2_CreateFailed() public {
vm.prank(eoaAddress);
vm.expectRevert(BaseLightAccount.CreateFailed.selector);
account.create2(hex"01", bytes32(0), 0);
account.create2(hex"3d3dfd", bytes32(0), 0);
}

function testCreate() public {
Expand Down
Loading