Initializable.sol 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0-rc.0) (proxy/utils/Initializable.sol)
  3. pragma solidity ^0.8.0;
  4. import "../../utils/Address.sol";
  5. /**
  6. * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
  7. * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
  8. * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
  9. * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
  10. *
  11. * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
  12. * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
  13. *
  14. * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
  15. * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
  16. *
  17. * [CAUTION]
  18. * ====
  19. * Avoid leaving a contract uninitialized.
  20. *
  21. * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
  22. * contract, which may impact the proxy. To initialize the implementation contract, you can either invoke the
  23. * initializer manually, or you can include a constructor to automatically mark it as initialized when it is deployed:
  24. *
  25. * [.hljs-theme-light.nopadding]
  26. * ```
  27. * /// @custom:oz-upgrades-unsafe-allow constructor
  28. * constructor() initializer {}
  29. * ```
  30. * ====
  31. */
  32. abstract contract Initializable {
  33. /**
  34. * @dev Indicates that the contract has been initialized.
  35. */
  36. bool private _initialized;
  37. /**
  38. * @dev Indicates that the contract is in the process of being initialized.
  39. */
  40. bool private _initializing;
  41. /**
  42. * @dev Modifier to protect an initializer function from being invoked twice.
  43. */
  44. modifier initializer() {
  45. // If the contract is initializing we ignore whether _initialized is set in order to support multiple
  46. // inheritance patterns, but we only do this in the context of a constructor, because in other contexts the
  47. // contract may have been reentered.
  48. require(_initializing ? _isConstructor() : !_initialized, "Initializable: contract is already initialized");
  49. bool isTopLevelCall = !_initializing;
  50. if (isTopLevelCall) {
  51. _initializing = true;
  52. _initialized = true;
  53. }
  54. _;
  55. if (isTopLevelCall) {
  56. _initializing = false;
  57. }
  58. }
  59. /**
  60. * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
  61. * {initializer} modifier, directly or indirectly.
  62. */
  63. modifier onlyInitializing() {
  64. require(_initializing, "Initializable: contract is not initializing");
  65. _;
  66. }
  67. function _isConstructor() private view returns (bool) {
  68. return !Address.isContract(address(this));
  69. }
  70. }