Ownable2Step.sol 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable2Step.sol)
  3. pragma solidity ^0.8.20;
  4. import {Ownable} from "./Ownable.sol";
  5. /**
  6. * @dev Contract module which provides 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 specified at deployment time in the constructor for `Ownable`. This
  11. * can later be changed with {transferOwnership} and {acceptOwnership}.
  12. *
  13. * This module is used through inheritance. It will make available all functions
  14. * from parent (Ownable).
  15. */
  16. abstract contract Ownable2Step is Ownable {
  17. address private _pendingOwner;
  18. event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
  19. /**
  20. * @dev Returns the address of the pending owner.
  21. */
  22. function pendingOwner() public view virtual returns (address) {
  23. return _pendingOwner;
  24. }
  25. /**
  26. * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
  27. * Can only be called by the current owner.
  28. */
  29. function transferOwnership(address newOwner) public virtual override onlyOwner {
  30. _pendingOwner = newOwner;
  31. emit OwnershipTransferStarted(owner(), newOwner);
  32. }
  33. /**
  34. * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
  35. * Internal function without access restriction.
  36. */
  37. function _transferOwnership(address newOwner) internal virtual override {
  38. delete _pendingOwner;
  39. super._transferOwnership(newOwner);
  40. }
  41. /**
  42. * @dev The new owner accepts the ownership transfer.
  43. */
  44. function acceptOwnership() public virtual {
  45. address sender = _msgSender();
  46. if (pendingOwner() != sender) {
  47. revert OwnableUnauthorizedAccount(sender);
  48. }
  49. _transferOwnership(sender);
  50. }
  51. }