Skip to main content

BaseAccountFactory

import "@thirdweb-dev/contracts/smart-wallet/utils/BaseAccountFactory.sol";

The BaseAccountFactory smart contract is an extension usable with the Dynamic, Managed and Non-Upgradable Account smart contracts. When creating your Smart Wallet account factory contract, inherit from this extension to include all of the base logic to create your factory.

info

This smart contract extension conforms to the IAccountFactory interface which is detectable on the dashboard as the SmartWalletFactory extension.


Usage

The BaseAccountFactory extension is an abstract contract, and expects you to implement the following functions by yourself:

NameTypeDescription
_initializeAccountinternal virtualCalled in createAccount. Initializes the account contract created in createAccount.

This is an example factory smart contract demonstrating how to inherit from this extension and override the functions to add (optional) custom functionality.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@thirdweb-dev/contracts/smart-wallet/utils/BaseAccountFactory.sol";

contract MyAccountFactory is BaseAccountFactory { ... }

SDK Usage

This extension unlocks the use of the smart wallet in the wallet SDK.

Base Contracts Implementing This Extension

Full API reference

createAccount

Deploys a new Account for admin.

function createAccount(address _admin, bytes calldata _data) external virtual override returns (address) {
address impl = accountImplementation;
bytes32 salt = keccak256(abi.encode(_admin));
address account = Clones.predictDeterministicAddress(impl, salt);
if (account.code.length > 0) {
return account;
}
account = Clones.cloneDeterministic(impl, salt);
_initializeAccount(account, _admin, _data);
emit AccountCreated(account, _admin);
return account;
}

_admin

The address to be set as default admin role for the contract. Must be of type address.

_data

If extra storage variables are required in your account contract, override this function, abi encode the variables, pass them to this function as a bytes type.

addSigner

Callback function for an Account to register its signers.

function addSigner(address _signer) external {
address account = msg.sender;
bool isAlreadyAccount = accountsOfSigner[_signer].add(account);
bool isAlreadySigner = signersOfAccount[account].add(_signer);
if (!isAlreadyAccount || !isAlreadySigner) {
revert("AccountFactory: signer already added");
}
emit SignerAdded(account, _signer);
}

signer

The address of the signer to add to the account.

removeSigner

Callback function for an Account to un-register its signers.

function removeSigner(address _signer) external {
address account = msg.sender;
bool isAccount = accountsOfSigner[_signer].remove(account);
bool isSigner = signersOfAccount[account].remove(_signer);
if (!isAccount || !isSigner) {
revert("AccountFactory: signer not found");
}
emit SignerRemoved(account, _signer);
}

_signer

The address of the signer to remove from the account.

getAddress

Returns the address of an Account that would be deployed with the given admin signer.

function getAddress(address _adminSigner) public view returns (address) {
bytes32 salt = keccak256(abi.encode(_adminSigner));
return Clones.predictDeterministicAddress(accountImplementation, salt);
}

_adminSigner

The admin signer for the account.

getSignersOfAccount

Returns all signers of an account.

function getSignersOfAccount(address account) external view returns (address[] memory signers) {
return signersOfAccount[account].values();
}

account

The address of the account to get the signers of.

getAccountsOfSigner

Returns all accounts that the given address is a signer of.

function getAccountsOfSigner(address signer) external view returns (address[] memory accounts) {
return accountsOfSigner[signer].values();
}

signer

The address of the signer to get the accounts of.

_initializeAccount

Called in createAccount. Initializes the account contract created in createAccount.

function _initializeAccount(
address _account,
address _admin,
bytes calldata _data
) internal virtual;

_account

The address of the smart account smart contract to initialize.

_admin

The address to be set as default admin role for the contract. Must be of type address.

_data

If extra storage variables are required in your account contract, abi encode the variables, pass them to this function as the bytes argument.

For example:


function initializeAccount(
address _admin,
bytes calldata _data
) public override initializer {
Account.initialize(_admin, _data);
}