Skip to content

Commit

Permalink
Updates for new deploy script.
Browse files Browse the repository at this point in the history
 Changes to be committed:
	modified:   contracts/core/HCHelper.sol
	modified:   contracts/samples/HybridAccount.sol
	new file:   contracts/samples/HybridAccountFactory.sol
  • Loading branch information
mmontour1306 committed Jul 13, 2024
1 parent a0305bf commit 134dee9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
3 changes: 2 additions & 1 deletion contracts/core/HCHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ contract HCHelper {
RegisteredCallers[contract_addr].url = url;
}

function initialize(address _owner) public {
function initialize(address _owner, address _systemAccount) public {
require(msg.sender == owner || address(0) == owner, "Only owner");
owner = _owner;
systemAccount = _systemAccount;
}

function SetSystemAccount(address _systemAccount) public {
Expand Down
3 changes: 1 addition & 2 deletions contracts/samples/HybridAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract HybridAccount is BaseAccount, TokenCallbackHandler, UUPSUpgradeable, In
address public owner;

IEntryPoint private immutable _entryPoint;
address public _helperAddr;
address public immutable _helperAddr;

event HybridAccountInitialized(IEntryPoint indexed entryPoint, address indexed owner);

Expand All @@ -40,7 +40,6 @@ contract HybridAccount is BaseAccount, TokenCallbackHandler, UUPSUpgradeable, In

/// @inheritdoc BaseAccount
function entryPoint() public view virtual override returns (IEntryPoint) {
require(2>1, "foo");
return _entryPoint;
}

Expand Down
54 changes: 54 additions & 0 deletions contracts/samples/HybridAccountFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;

import "@openzeppelin/contracts/utils/Create2.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

import "./HybridAccount.sol";

/**
* A sample factory contract for HybridAccount
* A UserOperations "initCode" holds the address of the factory, and a method call (to createAccount, in this sample factory).
* The factory's createAccount returns the target account address even if it is already installed.
* This way, the entryPoint.getSenderAddress() can be called either before or after the account is created.
*/
contract HybridAccountFactory {
HybridAccount public immutable accountImplementation;
address public Helper;

constructor(IEntryPoint _entryPoint, address _helper) {
accountImplementation = new HybridAccount(_entryPoint, _helper);
Helper = _helper;
}

/**
* create an account, and return its address.
* returns the address even if the account is already deployed.
* Note that during UserOperation execution, this method is called only if the account is not deployed.
* This method returns an existing account address so that entryPoint.getSenderAddress() would work even after account creation
*/
function createAccount(address owner,uint256 salt) public returns (HybridAccount ret) {
address addr = getAddress(owner, salt);
uint codeSize = addr.code.length;
if (codeSize > 0) {
return HybridAccount(payable(addr));
}
ret = HybridAccount(payable(new ERC1967Proxy{salt : bytes32(salt)}(
address(accountImplementation),
abi.encodeCall(HybridAccount.initialize, (owner))
)));
}

/**
* calculate the counterfactual address of this account as it would be returned by createAccount()
*/
function getAddress(address owner,uint256 salt) public view returns (address) {
return Create2.computeAddress(bytes32(salt), keccak256(abi.encodePacked(
type(ERC1967Proxy).creationCode,
abi.encode(
address(accountImplementation),
abi.encodeCall(HybridAccount.initialize, (owner))
)
)));
}
}

0 comments on commit 134dee9

Please sign in to comment.