Immutable Arguments

Immutable arguments are variables which are set at each module instance creation (via the HatsModuleFactory) and are then constant through out the instance's lifetime.

Accessing these variables is significantly cheaper than accessing regular storage variables, and so using this kind of variables is recommended for any variables which can be defined once, during instance creation, and then stay constant.

Standard HatsModule Immutable Args

HatsModule includes several immutable arguments as standard, which are then included in any inheriting module.

  • IMPLEMENTATION: The address of the implementation contract of which the instance is a clone.

  • HATS: The Hats Protocol address (Hats.sol).

  • hatId: The hat ID for which the instance has been deployed. This can be utilized in various ways depending on the module's use case.

Immutable Arg "Storage"

LibClone.sol handles "storage" of these values in the instance's bytecode, with the following layout:

OffsetConstantTypeLengthSource

0

IMPLEMENTATION

address

20

HatsModule

20

HATS

address

20

HatsModule

40

hatId

address

32

HatsModule

[72+]

[other args]

[type]

[len]

[your module]

Each is accessible by a pure function based on utility functions from LibClone.sol.

function IMPLEMENTATION() public pure returns (address) {
    return _getArgAddress(0);
}

function HATS() public pure returns (IHats) {
    return IHats(_getArgAddress(20));
}

function hatId() public pure returns (uint256) {
    return _getArgUint256(40);
}

Custom Immutable Arguments

Modules can include as many additional immutable arguments as they need, and each can be made accessible by similar pure functions.

Note that the immutable arguments of an instance are passed to the createHatsModule function in the factory, rather than to the instance's contract itself. See the next section for more details on how that works.

Last updated