diff --git a/contracts/Initializable.sol b/contracts/Initializable.sol new file mode 100644 index 000000000..a50149e5a --- /dev/null +++ b/contracts/Initializable.sol @@ -0,0 +1,42 @@ +pragma solidity ^0.4.24; + + +/** + * @title Initializable + * + * @dev Helper contract to support initializer functions. To use it, replace + * the constructor with a function that has the `initializer` modifier. + * WARNING: Unlike constructors, initializer functions must be manually + * invoked. This applies both to deploying an Initializable contract, as well + * as extending an Initializable contract via inheritance. + * WARNING: When used with inheritance, manual care must be taken to not invoke + * a parent initializer twice, because this is not dealt with automatically as + * with constructors. + */ +contract Initializable { + + /** + * @dev Indicates that the contract has been initialized. + */ + bool private initialized; + + /** + * @dev Indicates that the contract is in the process of being initialized. + */ + bool private initializing; + + /** + * @dev Modifier to use in the initializer function of a contract. + */ + modifier initializer() { + require(initializing || !initialized, "Contract instance has already been initialized"); + + bool wasInitializing = initializing; + initializing = true; + initialized = true; + + _; + + initializing = wasInitializing; + } +}