Skip to content

Commit

Permalink
Add comment and tests for zero address behavior in Ownable2Step.trans…
Browse files Browse the repository at this point in the history
…ferOwnership() (#5226)

Co-authored-by: Hadrien Croubois <[email protected]>
  • Loading branch information
PurrProof and Amxx authored Sep 25, 2024
1 parent 2f0bc58 commit cc67e0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions contracts/access/Ownable2Step.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ abstract contract Ownable2Step is Ownable {
/**
* @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
* Can only be called by the current owner.
*
* Setting `newOwner` to the zero address is allowed; this can be used to cancel an initiated ownership transfer.
*/
function transferOwnership(address newOwner) public virtual override onlyOwner {
_pendingOwner = newOwner;
Expand Down
17 changes: 17 additions & 0 deletions test/access/Ownable2Step.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,22 @@ describe('Ownable2Step', function () {

expect(await this.ownable2Step.owner()).to.equal(this.accountA);
});

it('allows the owner to cancel an initiated ownership transfer by setting newOwner to zero address', async function () {
// initiate ownership transfer to accountA
await this.ownable2Step.connect(this.owner).transferOwnership(this.accountA);
expect(await this.ownable2Step.pendingOwner()).to.equal(this.accountA);

// cancel the ownership transfer by setting newOwner to zero address
await expect(this.ownable2Step.connect(this.owner).transferOwnership(ethers.ZeroAddress))
.to.emit(this.ownable2Step, 'OwnershipTransferStarted')
.withArgs(this.owner, ethers.ZeroAddress);
expect(await this.ownable2Step.pendingOwner()).to.equal(ethers.ZeroAddress);

// verify that accountA cannot accept ownership anymore
await expect(this.ownable2Step.connect(this.accountA).acceptOwnership())
.to.be.revertedWithCustomError(this.ownable2Step, 'OwnableUnauthorizedAccount')
.withArgs(this.accountA);
});
});
});

0 comments on commit cc67e0e

Please sign in to comment.