Skip to content

Commit

Permalink
Implement and test restriction on admin being zero address
Browse files Browse the repository at this point in the history
  • Loading branch information
apbendi committed Jan 10, 2024
1 parent 31e7aed commit d2488b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/V3FactoryOwner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ contract V3FactoryOwner {
event AdminUpdated(address indexed oldAmin, address indexed newAdmin);

error V3FactoryOwner__Unauthorized();
error V3FactoryOwner__InvalidAddress();

constructor(address _admin) {
if (_admin == address(0)) revert V3FactoryOwner__InvalidAddress();
admin = _admin;
}

function setAdmin(address _newAdmin) external {
_revertIfNotAdmin();
if (_newAdmin == address(0)) revert V3FactoryOwner__InvalidAddress();
emit AdminUpdated(admin, _newAdmin);
admin = _newAdmin;
}
Expand Down
17 changes: 17 additions & 0 deletions test/V3FactoryOwner.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,30 @@ contract Constructor is V3FactoryOwnerTest {
}

function testFuzz_SetTheAdminToAnArbitraryAddress(address _admin) public {
vm.assume(_admin != address(0));
V3FactoryOwner _factoryOwner = new V3FactoryOwner(_admin);
assertEq(_factoryOwner.admin(), _admin);
}

function test_RevertIf_TheAdminIsAddressZero() public {
vm.expectRevert(V3FactoryOwner.V3FactoryOwner__InvalidAddress.selector);
new V3FactoryOwner(address(0));
}
}

contract SetAdmin is V3FactoryOwnerTest {
function testFuzz_UpdatesTheAdminWhenCalledByTheCurrentAdmin(address _newAdmin) public {
vm.assume(_newAdmin != address(0));

vm.prank(admin);
factoryOwner.setAdmin(_newAdmin);

assertEq(factoryOwner.admin(), _newAdmin);
}

function testFuzz_EmitsAnEventWhenUpdatingTheAdmin(address _newAdmin) public {
vm.assume(_newAdmin != address(0));

vm.expectEmit(true, true, true, true);
vm.prank(admin);
emit AdminUpdated(admin, _newAdmin);
Expand All @@ -51,4 +61,11 @@ contract SetAdmin is V3FactoryOwnerTest {
vm.prank(_notAdmin);
factoryOwner.setAdmin(_newAdmin);
}

function test_RevertIf_TheNewAdminIsAddressZero() public {
vm.prank(admin);

vm.expectRevert(V3FactoryOwner.V3FactoryOwner__InvalidAddress.selector);
factoryOwner.setAdmin(address(0));
}
}

0 comments on commit d2488b7

Please sign in to comment.