Skip to content

Commit

Permalink
Merge pull request #26 from abstraction-hq/25-create2-contract
Browse files Browse the repository at this point in the history
create2 contract
  • Loading branch information
imduchuyyy authored Jul 12, 2024
2 parents 5ad173e + a376cec commit 48f3f32
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
8 changes: 8 additions & 0 deletions script/BaseDeployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: Apache
pragma solidity ^0.8.0;

import "../src/GenericFactory.sol";

contract BaseDeployer {
GenericFactory public genericFactory = GenericFactory(0x9aE7a293de3D5789Cd24324C167B7BC98A0c70B7);
}
12 changes: 6 additions & 6 deletions script/Deploy.s.sol → script/DeployEntrypoint.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import "forge-std/console.sol";
import "account-abstraction/core/EntryPoint.sol";
import "../src/WalletFactory.sol";
import "../src/modules/Passkey.sol";
import "./BaseDeployer.sol";

contract Deployer is Script {
contract Deployer is Script, BaseDeployer {
function setUp() external {}

function run() external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
bytes32 salt = vm.envBytes32("SALT");
vm.startBroadcast(deployerPrivateKey);

EntryPoint entryPoint = new EntryPoint();
console.log("EntryPoint: ", address(entryPoint));

WalletFactory walletFactory = new WalletFactory(address(entryPoint));
console.log("WalletFactory: ", address(walletFactory));
bytes memory code = type(EntryPoint).creationCode;
address entryPoint = genericFactory.create2(code, salt);
console.log("EntryPoint: ", entryPoint);

vm.stopBroadcast();
}
Expand Down
21 changes: 14 additions & 7 deletions script/DeployFactory.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,32 @@ import "account-abstraction/core/EntryPoint.sol";
import "../src/WalletFactory.sol";
import "../src/Bootstrap.sol";
import "../src/modules/Passkey.sol";
import "./BaseDeployer.sol";

contract Deployer is Script {
contract Deployer is Script, BaseDeployer {
function setUp() external {}

function run() external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
address exitedEntryPoint = vm.envAddress("ENTRY_POINT");
bytes32 salt = vm.envBytes32("SALT");
require(exitedEntryPoint != address(0), "ENTRY_POINT is required");
vm.startBroadcast(deployerPrivateKey);

console.log("EntryPoint: ", exitedEntryPoint);

WalletFactory walletFactory = new WalletFactory(exitedEntryPoint);
PasskeyModule passkey = new PasskeyModule();
Bootstrap bootstrap = new Bootstrap(address(passkey));
bytes memory walletFactoryCode = abi.encodePacked(type(WalletFactory).creationCode, abi.encode(exitedEntryPoint));
address walletFactory = genericFactory.create2(walletFactoryCode, salt);

console.log("WalletFactory: ", address(walletFactory));
console.log("Passkey: ", address(passkey));
console.log("Bootstrap: ", address(bootstrap));
bytes memory passkeyCode = type(PasskeyModule).creationCode;
address passkey = genericFactory.create2(passkeyCode, salt);

bytes memory bootstrapCode = abi.encodePacked(type(Bootstrap).creationCode, abi.encode(passkey));
address bootstrap = genericFactory.create2(bootstrapCode, salt);

console.log("WalletFactory: ", walletFactory);
console.log("Passkey: ", passkey);
console.log("Bootstrap: ", bootstrap);

vm.stopBroadcast();
}
Expand Down
24 changes: 24 additions & 0 deletions script/DeployGenericFactory.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache
pragma solidity ^0.8.0;

import {Script, console2} from "forge-std/Script.sol";

import "forge-std/console.sol";
import "account-abstraction/core/EntryPoint.sol";
import "../src/GenericFactory.sol";

contract Deployer is Script {
function setUp() external {}

function run() external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
address exitedEntryPoint = vm.envAddress("ENTRY_POINT");
require(exitedEntryPoint != address(0), "ENTRY_POINT is required");
vm.startBroadcast(deployerPrivateKey);

GenericFactory factory = new GenericFactory();
console.log("Generic factory: ", address(factory));

vm.stopBroadcast();
}
}
22 changes: 22 additions & 0 deletions src/GenericFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: Apache
pragma solidity ^0.8.0;

contract GenericFactory {
function create(bytes memory code) external returns (address addr) {
assembly {
addr := create(0, add(code, 0x20), mload(code))
if iszero(extcodesize(addr)) {
revert(0, 0)
}
}
}

function create2(bytes memory code, bytes32 salt) external returns (address addr) {
assembly {
addr := create2(0, add(code, 0x20), mload(code), salt)
if iszero(extcodesize(addr)) {
revert(0, 0)
}
}
}
}

0 comments on commit 48f3f32

Please sign in to comment.