Standard Factory contract

The standard Factory contract is not an abstract contract. So it can be taken and deployed or it can be implemented in a custom Factory contract.

The standard Factory implements DynamicMetadataCapableElement, implementing all the capabilities of DynamicMetadataCapableElement.

The standard Factory contract has the following capabilities:

  • it can be initialized with a model contract to clone

  • it provides the deploy method to clone the model contract and initialize it

  • it provides the setModelAddress method to change the model contract

  • it provides the empty virtual _factoryLazyInit function. It can be overridden by your custom Factory to initialize your Factory specific parameter

Setting the Model Contract

Before a model can be cloned, its address must be passed to the desired Factory, and this can be done either:

  • When the Factory is initialized: (see here for more info about initialization).

(modelAddress, lazyInitResponse) = abi.decode(lazyInitData, (address, bytes));
  • At any time thereafter, by using the setModelAddress function, which only the host address of the Factory can call.

function setModelAddress(address newValue) external override authorizedOnly returns(address oldValue) {
    oldValue = modelAddress;
    modelAddress = newValue;
}

The setModelAddress function can also be used by the host to change the current model contract address to clone.

Cloning and Initializing the Model Contract

To clone a Factory’s model contract, the deploy function is used. As input, this function takes the deployData bytes parameter, which can contain custom data for initializing the cloned contract.

function deploy(bytes calldata deployData) external payable returns(address deployedAddress, bytes memory deployedLazyInitResponse);

Customizable data is the key feature of Factory design: the ability to not only correctly clone and initialize a model contract, but for the particular needs of a developer / organization / protocol / application.

To learn more about initialization, see here.

Last updated