LazyInitCapableElement

How It Works

LazyInitCapableElement is an abstract contract that your model contract must integrate in order for your contract to be Factory clonable.

It provides:

  • The constructor, which can be used if your contract is deployed and initialized manually (i.e, not via a Factory)

  • The lazyInit function, which is automatically called when your contract is cloned from a Factory

The lazyInit function internally calls the empty virtual _lazyInit function, which your model contract can override in order to manage and initialize its specific custom data.

Host

The LazyInitCapableElement provides an host address parameters which can be used in your model contract to build custom permission logics.

How to Code a Factory-Compliant Smart Contract

Your model contract must integrate LazyInitCapableElement as follows:

contract Example is....., LazyInitCapableElement {

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

contract IExample is....., ILazyInitCapableElement {

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 the contract even without a Factory, and to initialize it in the โ€œtraditionalโ€ way.

contract Example is....., LazyInitCapableElement {

    constructor(bytes memory lazyInitData) LazyInitCapableElement(lazyInitData) { 
    }
    ......
    ......
}

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

After integrating the constructor, you need to override _lazyInitโ€™s virtual function, which manages data initialization of your model contract.

In the example below, _lazyInit initializes three parameters: callerPercentage, lastSplitBlock and splitInterval.

contract Example is....., LazyInitCapableElement {

    //a costant is not initialized
    uint256 public constant override ONE_HUNDRED = 1e18;
    
    //parameters to initialize 
    uint256 public override callerPercentage;
    uint256 public override lastSplitBlock;
    uint256 public override splitInterval;
    
    constructor(bytes memory lazyInitData) LazyInitCapableElement(lazyInitData) { 
    }
    
    function _lazyInit(bytes memory lazyInitData) internal override virtual returns (bytes memory) {
        (callerPercentage, firstSplitBlock, _splitInterval) = abi.decode(lazyInitData, (uint256, uint256, uint256));
        ....
    }
    
    ......
    ......
}

Of course, if your model contract doesnโ€™t have any additional data to initialize, the _lazyInit override function doesnโ€™t have to be included.

Now you have a contract that is Factory-compliant. It can be cloned via a factory, and then initialized either via that factory or (if it was manually deployed) via the traditional constructor method.

Initialize the model contract

Here, you learn how to clone and initialize the model contract using a Factory.

Last updated