Skip to content

Commit

Permalink
✨ Owned (#193)
Browse files Browse the repository at this point in the history
Co-authored-by: t11s <[email protected]>
  • Loading branch information
z0r0z and transmissions11 authored May 14, 2022
1 parent b6ae78e commit c8f454a
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ MultiRolesAuthorityTest:testSetPublicCapabilities() (gas: 27762)
MultiRolesAuthorityTest:testSetRoleCapabilities() (gas: 28985)
MultiRolesAuthorityTest:testSetRoles() (gas: 29006)
MultiRolesAuthorityTest:testSetTargetCustomAuthority() (gas: 27976)
OwnedTest:testCallFunctionAsNonOwner() (gas: 11311)
OwnedTest:testCallFunctionAsOwner() (gas: 10479)
OwnedTest:testSetOwner() (gas: 13035)
ReentrancyGuardTest:testFailUnprotectedCall() (gas: 45665)
ReentrancyGuardTest:testNoReentrancy() (gas: 7515)
ReentrancyGuardTest:testProtectedCall() (gas: 32949)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

```ml
auth
├─ Owned — "Simple single owner authorization"
├─ Auth — "Flexible and updatable auth pattern"
├─ authorities
│ ├─ RolesAuthority — "Role based Authority that supports up to 256 roles"
Expand Down
2 changes: 1 addition & 1 deletion lib/ds-test
Submodule ds-test updated 1 files
+4 −4 src/test.sol
44 changes: 44 additions & 0 deletions src/auth/Owned.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @notice Simple single owner authorization mixin.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/auth/Owned.sol)
abstract contract Owned {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/

event OwnerUpdated(address indexed user, address indexed newOwner);

/*//////////////////////////////////////////////////////////////
OWNERSHIP STORAGE
//////////////////////////////////////////////////////////////*/

address public owner;

modifier onlyOwner() virtual {
require(msg.sender == owner, "UNAUTHORIZED");

_;
}

/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/

constructor(address _owner) {
owner = _owner;

emit OwnerUpdated(address(0), _owner);
}

/*//////////////////////////////////////////////////////////////
OWNERSHIP LOGIC
//////////////////////////////////////////////////////////////*/

function setOwner(address newOwner) public virtual onlyOwner {
owner = newOwner;

emit OwnerUpdated(msg.sender, newOwner);
}
}
40 changes: 40 additions & 0 deletions src/test/Owned.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.10;

import {DSTestPlus} from "./utils/DSTestPlus.sol";
import {MockOwned} from "./utils/mocks/MockOwned.sol";

contract OwnedTest is DSTestPlus {
MockOwned mockOwned;

function setUp() public {
mockOwned = new MockOwned();
}

function testSetOwner() public {
testSetOwner(address(0xBEEF));
}

function testCallFunctionAsNonOwner() public {
testCallFunctionAsNonOwner(address(0));
}

function testCallFunctionAsOwner() public {
mockOwned.updateFlag();
}

function testSetOwner(address newOwner) public {
mockOwned.setOwner(newOwner);

assertEq(mockOwned.owner(), newOwner);
}

function testCallFunctionAsNonOwner(address owner) public {
hevm.assume(owner != address(this));

mockOwned.setOwner(owner);

hevm.expectRevert("UNAUTHORIZED");
mockOwned.updateFlag();
}
}
12 changes: 12 additions & 0 deletions src/test/utils/mocks/MockOwned.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

import {Owned} from "../../../auth/Owned.sol";

contract MockOwned is Owned(msg.sender) {
bool public flag;

function updateFlag() public virtual onlyOwner {
flag = true;
}
}
2 changes: 1 addition & 1 deletion src/utils/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pragma solidity >=0.8.0;
abstract contract ReentrancyGuard {
uint256 private locked = 1;

modifier nonReentrant() {
modifier nonReentrant() virtual {
require(locked == 1, "REENTRANCY");

locked = 2;
Expand Down

0 comments on commit c8f454a

Please sign in to comment.