Skip to content
Lattice

AccountFactory

Git Source

Inherits: IAccountFactory

Title: AccountFactory

Author: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80)

Deterministic factory for EIP-2535 Diamond smart accounts, suitable for ERC-4337 counterfactual initCode onboarding. Holds a fixed facet blueprint + initializer; each createAccount CREATE2- deploys a AccountDiamond proxy and initializes it with the blueprint and the owner.

The proxy has no constructor args, so its initcode hash is constant and the CREATE2 address depends only on (factory, keccak256(owner, salt)). Deploy this factory itself deterministically (e.g. via CreateX) and the same (owner, salt) lands at the SAME account address on every chain. Folding the owner into the salt binds each counterfactual address to its owner, so no third party can occupy it under a different owner. Not a Diamond facet — a standalone singleton with its own storage.

Constants

accountInit

The initializer contract delegatecalled during each account's diamondCut initialization.

address public immutable accountInit;

Returns

NameTypeDescription
<none>address

_proxyInitCodeHash

keccak256(type(AccountDiamond).creationCode) — the CREATE2 initcode hash, fixed at construction.

bytes32 private immutable _proxyInitCodeHash;

State Variables

_blueprint

The facet cuts applied to every account. Fixed at construction; a different blueprint ⇒ a different factory ⇒ a different address set, preserving the counterfactual guarantee.

FacetCut[] private _blueprint;

Functions

constructor

constructor(FacetCut[] memory __blueprint, address _accountInit);

Parameters

NameTypeDescription
__blueprintFacetCut[]The facet cuts (Add) wiring a complete account.
_accountInitaddressInitializer delegatecalled during diamondCut; MUST expose init(address owner).

createAccount

Deploys (or returns the existing) Diamond account for (owner, salt) and initializes it with the factory's fixed facet blueprint and the owner.

function createAccount(address owner, bytes32 salt) external returns (address account);

Parameters

NameTypeDescription
owneraddressThe initial signing owner of the account.
saltbytes32Caller-chosen salt; distinct salts yield distinct accounts for the same owner.

Returns

NameTypeDescription
accountaddressThe deterministic account address (equals getAddress).

getAddress

The counterfactual address createAccount(owner, salt) will deploy to.

function getAddress(address owner, bytes32 salt) external view returns (address account);

Parameters

NameTypeDescription
owneraddressThe prospective owner.
saltbytes32The prospective salt.

Returns

NameTypeDescription
accountaddressThe deterministic CREATE2 address.

_saltFor

Binds the account address to its owner: keccak256(owner, salt).

function _saltFor(address owner, bytes32 salt) private pure returns (bytes32);

Parameters

NameTypeDescription
owneraddress
saltbytes32

Returns

NameTypeDescription
<none>bytes32

_predict

Standard CREATE2 address derivation for the AccountDiamond proxy.

function _predict(bytes32 s) private view returns (address);

Parameters

NameTypeDescription
sbytes32

Returns

NameTypeDescription
<none>address