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 owner #43

Merged
merged 8 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 5 additions & 1 deletion src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {IOracle} from "src/interfaces/IOracle.sol";
import {MathLib} from "src/libraries/MathLib.sol";
import {SafeTransferLib} from "src/libraries/SafeTransferLib.sol";

import {Ownable} from "src/Ownable.sol";

uint constant WAD = 1e18;

uint constant alpha = 0.5e18;
Expand All @@ -29,7 +31,7 @@ function irm(uint utilization) pure returns (uint) {
return utilization / 365 days;
}

contract Blue {
contract Blue is Ownable {
using MathLib for uint;
using SafeTransferLib for IERC20;

Expand All @@ -52,6 +54,8 @@ contract Blue {
// Interests last update (used to check if a market has been created).
mapping(Id => uint) public lastUpdate;

constructor(address owner) Ownable(owner) {}

// Markets management.

function createMarket(Market calldata market) external {
Expand Down
39 changes: 39 additions & 0 deletions src/Ownable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @title Ownable
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @dev Greatly inspired by Solmate and OZ implementations.
abstract contract Ownable {
/* EVENTS */

event OwnershipTransferred(address indexed oldOwner, address indexed newOwner);

/* STORAGE */

address public owner;

/* MODIFIERS */

modifier onlyOwner() virtual {
require(msg.sender == owner, "not owner");
_;
}

/* CONSTRUCTOR */

constructor(address newOwner) {
owner = newOwner;

emit OwnershipTransferred(address(0), newOwner);
}

/* PUBLIC */

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

emit OwnershipTransferred(msg.sender, newOwner);
}
}
26 changes: 25 additions & 1 deletion test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract BlueTest is Test {

function setUp() public {
// Create Blue.
blue = new Blue();
blue = new Blue(msg.sender);

// List a market.
borrowableAsset = new ERC20("borrowable", "B", 18);
Expand Down Expand Up @@ -91,6 +91,30 @@ contract BlueTest is Test {

// Tests

function testOwner(address owner) public {
Blue blue2 = new Blue(owner);

assertEq(blue2.owner(), owner, "owner");
}

function testTransferOwnership(address oldOwner, address newOwner) public {
Blue blue2 = new Blue(oldOwner);

vm.prank(oldOwner);
blue2.transferOwnership(newOwner);
assertEq(blue2.owner(), newOwner, "owner");
}

function testTransferOwnershipWhenNotOwner(address attacker, address newOwner) public {
vm.assume(attacker != address(0xdead));

Blue blue2 = new Blue(address(0xdead));

vm.prank(attacker);
vm.expectRevert("not owner");
blue2.transferOwnership(newOwner);
}

function testSupply(uint amount) public {
amount = bound(amount, 1, 2 ** 64);

Expand Down