Ownable.sol 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (access/Ownable.sol)
  3. pragma solidity ^0.8.0;
  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() {
  24. _transferOwnership(_msgSender());
  25. }
  26. /**
  27. * @dev Returns the address of the current owner.
  28. */
  29. function owner() public view virtual returns (address) {
  30. return _owner;
  31. }
  32. /**
  33. * @dev Throws if called by any account other than the owner.
  34. */
  35. modifier onlyOwner() {
  36. require(owner() == _msgSender(), "Ownable: caller is not the owner");
  37. _;
  38. }
  39. /**
  40. * @dev Leaves the contract without owner. It will not be possible to call
  41. * `onlyOwner` functions anymore. Can only be called by the current owner.
  42. *
  43. * NOTE: Renouncing ownership will leave the contract without an owner,
  44. * thereby removing any functionality that is only available to the owner.
  45. */
  46. function renounceOwnership() public virtual onlyOwner {
  47. _transferOwnership(address(0));
  48. }
  49. /**
  50. * @dev Transfers ownership of the contract to a new account (`newOwner`).
  51. * Can only be called by the current owner.
  52. */
  53. function transferOwnership(address newOwner) public virtual onlyOwner {
  54. require(newOwner != address(0), "Ownable: new owner is the zero address");
  55. _transferOwnership(newOwner);
  56. }
  57. /**
  58. * @dev Transfers ownership of the contract to a new account (`newOwner`).
  59. * Internal function without access restriction.
  60. */
  61. function _transferOwnership(address newOwner) internal virtual {
  62. address oldOwner = _owner;
  63. _owner = newOwner;
  64. emit OwnershipTransferred(oldOwner, newOwner);
  65. }
  66. }