Ownable.sol 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)
  3. pragma solidity ^0.8.20;
  4. import {Context} from "../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. * The initial owner is set to the address provided by the deployer. This can
  11. * 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. /**
  20. * @dev The caller account is not authorized to perform an operation.
  21. */
  22. error OwnableUnauthorizedAccount(address account);
  23. /**
  24. * @dev The owner is not a valid owner account. (eg. `address(0)`)
  25. */
  26. error OwnableInvalidOwner(address owner);
  27. event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
  28. /**
  29. * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
  30. */
  31. constructor(address initialOwner) {
  32. _transferOwnership(initialOwner);
  33. }
  34. /**
  35. * @dev Throws if called by any account other than the owner.
  36. */
  37. modifier onlyOwner() {
  38. _checkOwner();
  39. _;
  40. }
  41. /**
  42. * @dev Returns the address of the current owner.
  43. */
  44. function owner() public view virtual returns (address) {
  45. return _owner;
  46. }
  47. /**
  48. * @dev Throws if the sender is not the owner.
  49. */
  50. function _checkOwner() internal view virtual {
  51. if (owner() != _msgSender()) {
  52. revert OwnableUnauthorizedAccount(_msgSender());
  53. }
  54. }
  55. /**
  56. * @dev Leaves the contract without owner. It will not be possible to call
  57. * `onlyOwner` functions. Can only be called by the current owner.
  58. *
  59. * NOTE: Renouncing ownership will leave the contract without an owner,
  60. * thereby disabling any functionality that is only available to the owner.
  61. */
  62. function renounceOwnership() public virtual onlyOwner {
  63. _transferOwnership(address(0));
  64. }
  65. /**
  66. * @dev Transfers ownership of the contract to a new account (`newOwner`).
  67. * Can only be called by the current owner.
  68. */
  69. function transferOwnership(address newOwner) public virtual onlyOwner {
  70. if (newOwner == address(0)) {
  71. revert OwnableInvalidOwner(address(0));
  72. }
  73. _transferOwnership(newOwner);
  74. }
  75. /**
  76. * @dev Transfers ownership of the contract to a new account (`newOwner`).
  77. * Internal function without access restriction.
  78. */
  79. function _transferOwnership(address newOwner) internal virtual {
  80. address oldOwner = _owner;
  81. _owner = newOwner;
  82. emit OwnershipTransferred(oldOwner, newOwner);
  83. }
  84. }