Ownable.sol 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.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. if (initialOwner == address(0)) {
  33. revert OwnableInvalidOwner(address(0));
  34. }
  35. _transferOwnership(initialOwner);
  36. }
  37. /**
  38. * @dev Throws if called by any account other than the owner.
  39. */
  40. modifier onlyOwner() {
  41. _checkOwner();
  42. _;
  43. }
  44. /**
  45. * @dev Returns the address of the current owner.
  46. */
  47. function owner() public view virtual returns (address) {
  48. return _owner;
  49. }
  50. /**
  51. * @dev Throws if the sender is not the owner.
  52. */
  53. function _checkOwner() internal view virtual {
  54. if (owner() != _msgSender()) {
  55. revert OwnableUnauthorizedAccount(_msgSender());
  56. }
  57. }
  58. /**
  59. * @dev Leaves the contract without owner. It will not be possible to call
  60. * `onlyOwner` functions. Can only be called by the current owner.
  61. *
  62. * NOTE: Renouncing ownership will leave the contract without an owner,
  63. * thereby disabling any functionality that is only available to the owner.
  64. */
  65. function renounceOwnership() public virtual onlyOwner {
  66. _transferOwnership(address(0));
  67. }
  68. /**
  69. * @dev Transfers ownership of the contract to a new account (`newOwner`).
  70. * Can only be called by the current owner.
  71. */
  72. function transferOwnership(address newOwner) public virtual onlyOwner {
  73. if (newOwner == address(0)) {
  74. revert OwnableInvalidOwner(address(0));
  75. }
  76. _transferOwnership(newOwner);
  77. }
  78. /**
  79. * @dev Transfers ownership of the contract to a new account (`newOwner`).
  80. * Internal function without access restriction.
  81. */
  82. function _transferOwnership(address newOwner) internal virtual {
  83. address oldOwner = _owner;
  84. _owner = newOwner;
  85. emit OwnershipTransferred(oldOwner, newOwner);
  86. }
  87. }