Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Add testCancelTransferOwnership Unit Test #274

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 104 additions & 103 deletions .gas-snapshot

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/forge-std
2 changes: 1 addition & 1 deletion lib/solady
36 changes: 18 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions src/snekmate/auth/ownable_2step.vy
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ def transfer_ownership(new_owner: address):
"""
@dev Starts the ownership transfer of the contract
to a new account `new_owner`.
@notice Note that this function can only be
called by the current `owner`. Also, there is
no security risk in setting `new_owner` to the
zero address as the default value of `pending_owner`
is in fact already the zero address and the zero
address cannot call `accept_ownership`. Eventually,
the function replaces the pending transfer if
there is one.
@notice Note that this function can only be called by
the current `owner`. Importantly, there is no
security risk in assigning `new_owner` to the
zero address, as the default value of `pending_owner`
is already set to the zero address, and the zero
address cannot invoke `accept_ownership`. In fact,
this can serve as a method to cancel an ongoing
ownership transfer. Eventually, the function
replaces the pending transfer if there is one.
@param new_owner The 20-byte address of the new owner.
"""
ownable._check_owner()
Expand Down
38 changes: 38 additions & 0 deletions test/auth/Ownable2Step.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,44 @@ contract Ownable2StepTest is Test {
vm.stopPrank();
}

function testCancelTransferOwnership() public {
address oldOwner = deployer;
address newOwner1 = makeAddr("newOwner1");
address newOwner2 = makeAddr("newOwner2");
vm.startPrank(oldOwner);
assertEq(ownable2Step.pending_owner(), zeroAddress);
vm.expectEmit(true, true, false, false);
emit IOwnable2Step.OwnershipTransferStarted(oldOwner, newOwner1);
ownable2Step.transfer_ownership(newOwner1);
assertEq(ownable2Step.owner(), oldOwner);
assertEq(ownable2Step.pending_owner(), newOwner1);

vm.expectEmit(true, true, false, false);
emit IOwnable2Step.OwnershipTransferStarted(oldOwner, newOwner2);
ownable2Step.transfer_ownership(newOwner2);
assertEq(ownable2Step.owner(), oldOwner);
assertEq(ownable2Step.pending_owner(), newOwner2);
vm.stopPrank();

vm.startPrank(newOwner1);
vm.expectRevert(bytes("ownable_2step: caller is not the new owner"));
ownable2Step.accept_ownership();
vm.stopPrank();

vm.startPrank(oldOwner);
vm.expectEmit(true, true, false, false);
emit IOwnable2Step.OwnershipTransferStarted(oldOwner, zeroAddress);
ownable2Step.transfer_ownership(zeroAddress);
assertEq(ownable2Step.owner(), oldOwner);
assertEq(ownable2Step.pending_owner(), zeroAddress);
vm.stopPrank();

vm.startPrank(newOwner2);
vm.expectRevert(bytes("ownable_2step: caller is not the new owner"));
ownable2Step.accept_ownership();
vm.stopPrank();
}

function testRenounceOwnershipSuccess() public {
address oldOwner = deployer;
address newOwner = zeroAddress;
Expand Down