Ownable.sol 2.2 KB

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