65 lines
2.0 KiB
Solidity
65 lines
2.0 KiB
Solidity
|
// SPDX-License-Identifier: MIT
|
||
|
|
||
|
pragma solidity >=0.4.24 <0.7.0;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @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, or ensure that all initializers are idempotent,
|
||
|
* 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 || isConstructor() || !initialized, "Contract instance has already been initialized");
|
||
|
|
||
|
bool isTopLevelCall = !initializing;
|
||
|
if (isTopLevelCall) {
|
||
|
initializing = true;
|
||
|
initialized = true;
|
||
|
}
|
||
|
|
||
|
_;
|
||
|
|
||
|
if (isTopLevelCall) {
|
||
|
initializing = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// @dev Returns true if and only if the function is running in the constructor
|
||
|
function isConstructor() private view returns (bool) {
|
||
|
// extcodesize checks the size of the code stored in an address, and
|
||
|
// address returns the current address. Since the code is still not
|
||
|
// deployed when running a constructor, any checks on its code size will
|
||
|
// yield zero, making it an effective way to detect if a contract is
|
||
|
// under construction or not.
|
||
|
address self = address(this);
|
||
|
uint256 cs;
|
||
|
assembly { cs := extcodesize(self) }
|
||
|
return cs == 0;
|
||
|
}
|
||
|
|
||
|
// Reserved storage space to allow for layout changes in the future.
|
||
|
uint256[50] private ______gap;
|
||
|
}
|