Ownable.sol 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)
  3. pragma solidity ^0.8.19;
  4. import "../utils/Context.sol";
  5. /**
  6. * @dev Contract module which provides a basic access control mechanism, where
  7. * there is an account (an owner) that can be granted exclusive access to
  8. * specific functions.
  9. *
  10. * By default, the owner account will be the one that deploys the contract. This
  11. * can later be changed with {transferOwnership}.
  12. *
  13. * This module is used through inheritance. It will make available the modifier
  14. * `onlyOwner`, which can be applied to your functions to restrict their use to
  15. * the owner.
  16. */
  17. abstract contract Ownable is Context {
  18. address private _owner;
  19. event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
  20. /**
  21. * @dev Initializes the contract setting the deployer as the initial owner.
  22. */
  23. constructor(address initialOwner) {
  24. _transferOwnership(initialOwner);
  25. }
  26. /**
  27. * @dev Throws if called by any account other than the owner.
  28. */
  29. modifier onlyOwner() {
  30. _checkOwner();
  31. _;
  32. }
  33. /**
  34. * @dev Returns the address of the current owner.
  35. */
  36. function owner() public view virtual returns (address) {
  37. return _owner;
  38. }
  39. /**
  40. * @dev Throws if the sender is not the owner.
  41. */
  42. function _checkOwner() internal view virtual {
  43. require(owner() == _msgSender(), "Ownable: caller is not the owner");
  44. }
  45. /**
  46. * @dev Leaves the contract without owner. It will not be possible to call
  47. * `onlyOwner` functions. Can only be called by the current owner.
  48. *
  49. * NOTE: Renouncing ownership will leave the contract without an owner,
  50. * thereby disabling any functionality that is only available to the owner.
  51. */
  52. function renounceOwnership() public virtual onlyOwner {
  53. _transferOwnership(address(0));
  54. }
  55. /**
  56. * @dev Transfers ownership of the contract to a new account (`newOwner`).
  57. * Can only be called by the current owner.
  58. */
  59. function transferOwnership(address newOwner) public virtual onlyOwner {
  60. require(newOwner != address(0), "Ownable: new owner is the zero address");
  61. _transferOwnership(newOwner);
  62. }
  63. /**
  64. * @dev Transfers ownership of the contract to a new account (`newOwner`).
  65. * Internal function without access restriction.
  66. */
  67. function _transferOwnership(address newOwner) internal virtual {
  68. address oldOwner = _owner;
  69. _owner = newOwner;
  70. emit OwnershipTransferred(oldOwner, newOwner);
  71. }
  72. }