Initializable.sol 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. /**
  4. * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
  5. * behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
  6. * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
  7. * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
  8. *
  9. * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
  10. * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
  11. *
  12. * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
  13. * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
  14. */
  15. abstract contract Initializable {
  16. /**
  17. * @dev Indicates that the contract has been initialized.
  18. */
  19. bool private _initialized;
  20. /**
  21. * @dev Indicates that the contract is in the process of being initialized.
  22. */
  23. bool private _initializing;
  24. /**
  25. * @dev Modifier to protect an initializer function from being invoked twice.
  26. */
  27. modifier initializer() {
  28. require(_initializing || !_initialized, "Initializable: contract is already initialized");
  29. bool isTopLevelCall = !_initializing;
  30. if (isTopLevelCall) {
  31. _initializing = true;
  32. _initialized = true;
  33. }
  34. _;
  35. if (isTopLevelCall) {
  36. _initializing = false;
  37. }
  38. }
  39. }