Ownable2Step.sol 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. * This extension of the {Ownable} contract includes a two-step mechanism to transfer
  11. * ownership, where the new owner must call {acceptOwnership} in order to replace the
  12. * old one. This can help prevent common mistakes, such as transfers of ownership to
  13. * incorrect accounts, or to contracts that are unable to interact with the
  14. * permission system.
  15. *
  16. * The initial owner is specified at deployment time in the constructor for `Ownable`. This
  17. * can later be changed with {transferOwnership} and {acceptOwnership}.
  18. *
  19. * This module is used through inheritance. It will make available all functions
  20. * from parent (Ownable).
  21. */
  22. abstract contract Ownable2Step is Ownable {
  23. address private _pendingOwner;
  24. event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
  25. /**
  26. * @dev Returns the address of the pending owner.
  27. */
  28. function pendingOwner() public view virtual returns (address) {
  29. return _pendingOwner;
  30. }
  31. /**
  32. * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
  33. * Can only be called by the current owner.
  34. */
  35. function transferOwnership(address newOwner) public virtual override onlyOwner {
  36. _pendingOwner = newOwner;
  37. emit OwnershipTransferStarted(owner(), newOwner);
  38. }
  39. /**
  40. * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
  41. * Internal function without access restriction.
  42. */
  43. function _transferOwnership(address newOwner) internal virtual override {
  44. delete _pendingOwner;
  45. super._transferOwnership(newOwner);
  46. }
  47. /**
  48. * @dev The new owner accepts the ownership transfer.
  49. */
  50. function acceptOwnership() public virtual {
  51. address sender = _msgSender();
  52. if (pendingOwner() != sender) {
  53. revert OwnableUnauthorizedAccount(sender);
  54. }
  55. _transferOwnership(sender);
  56. }
  57. }