Skip to content

Commit cc67e0e

Browse files
PurrProofAmxx
andauthored
Add comment and tests for zero address behavior in Ownable2Step.transferOwnership() (#5226)
Co-authored-by: Hadrien Croubois <[email protected]>
1 parent 2f0bc58 commit cc67e0e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

contracts/access/Ownable2Step.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ abstract contract Ownable2Step is Ownable {
3737
/**
3838
* @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
3939
* Can only be called by the current owner.
40+
*
41+
* Setting `newOwner` to the zero address is allowed; this can be used to cancel an initiated ownership transfer.
4042
*/
4143
function transferOwnership(address newOwner) public virtual override onlyOwner {
4244
_pendingOwner = newOwner;

test/access/Ownable2Step.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,22 @@ describe('Ownable2Step', function () {
8181

8282
expect(await this.ownable2Step.owner()).to.equal(this.accountA);
8383
});
84+
85+
it('allows the owner to cancel an initiated ownership transfer by setting newOwner to zero address', async function () {
86+
// initiate ownership transfer to accountA
87+
await this.ownable2Step.connect(this.owner).transferOwnership(this.accountA);
88+
expect(await this.ownable2Step.pendingOwner()).to.equal(this.accountA);
89+
90+
// cancel the ownership transfer by setting newOwner to zero address
91+
await expect(this.ownable2Step.connect(this.owner).transferOwnership(ethers.ZeroAddress))
92+
.to.emit(this.ownable2Step, 'OwnershipTransferStarted')
93+
.withArgs(this.owner, ethers.ZeroAddress);
94+
expect(await this.ownable2Step.pendingOwner()).to.equal(ethers.ZeroAddress);
95+
96+
// verify that accountA cannot accept ownership anymore
97+
await expect(this.ownable2Step.connect(this.accountA).acceptOwnership())
98+
.to.be.revertedWithCustomError(this.ownable2Step, 'OwnableUnauthorizedAccount')
99+
.withArgs(this.accountA);
100+
});
84101
});
85102
});

0 commit comments

Comments
 (0)