Factory without a business model

In order to code your Factory without a fee-based business model, you can:

  • Take the standard Factory contract provided by the swissKnife Library and deploy it

or:

  • Implement the standard Factory contract in your custom Factory contract to build your custom logic

Even your Factory without a business model can be deployed through the Factory-Of-Factories in order to be listed in the Factory-Of-Factories frontend (link coming soon).

In the following paragraph, you can find an example on how to implement the standard Factory contract in your custom Factory.

How to Code a custom Factory without a business model

Your custom Factory contract must integrate the standard Factory contract as follows:

contract customFactory is Factory {

And at the interface level, if any, as follows:

contract IcustomFactory is IFactory {

Through this integration, your contract acquires all LazyInitCapableElement capabilities.

You also need to insert the constructor into your smart contract. This allows you to deploy and initialize it in the “traditional” way as well as through another factory.

contract customFactory is Factory {
    constructor(bytes memory lazyInitData) Factory(lazyInitData) { 
    }
    ......
    ......
}

Coded like this, the constructor calls the standard Factory constructor and thus can use all logic of the Factory, such as that which allows it to execute lazyInitData.

After integrating the constructor, you need to override _factoryLazyInit virtual function, which manages data initialization of your model contract.

In the example below, _factoryLazyInit initializes three parameters: customFactoryParameter1, customFactoryParameter2 and customFactoryParameter3.

contract customFactory is Factory {

    //parameters to initialize 
    uint256 private customFactoryParameter1;
    uint256 private customFactoryParameter2;
    uint256 private customFactoryParameter3;
    
    constructor(bytes memory lazyInitData) Factory(lazyInitData) { 
    }
    
    function _factoryLazyInit(bytes memory lazyInitData) internal override virtual returns (bytes memory) {
        (customFactoryParameter1, customFactoryParameter2, customFactoryParameter3) = abi.decode(lazyInitData, (uint256, uint256, uint256));
        ....
    }
    ......
    ......
}

Of course, if your custom Factory doesn’t have any additional data to initialize, the _factoryLazyInit override function doesn’t have to be included.

Now you have a custom Factory contract which can clone and initialize a model contract.

Last updated