Ownable2Step.sol 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)
  3. pragma solidity ^0.8.0;
  4. import "./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. * By default, the owner account will be the one that deploys the contract. 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() external {
  45. address sender = _msgSender();
  46. require(pendingOwner() == sender, "Ownable2Step: caller is not the new owner");
  47. _transferOwnership(sender);
  48. }
  49. }