Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

C4 mothership fixes #119

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e531e30
Add setter for margin burn rate
mikeyrf Dec 13, 2021
d902965
Fix collateral active bool flip on enable/disableCollateral
mikeyrf Dec 13, 2021
2aa7584
Fix require msgs for mothership
mikeyrf Dec 13, 2021
403e173
Add mothership conftest
mikeyrf Dec 13, 2021
9f55e90
Add mothership config tests
mikeyrf Dec 13, 2021
7b27e5c
Set ovl in mothership constructor
mikeyrf Dec 13, 2021
2975956
Make ovl token immutable in mothership
mikeyrf Dec 13, 2021
06fe6e4
Add events to mothership
mikeyrf Dec 13, 2021
c68de85
Remove guardian role from mothership
mikeyrf Dec 13, 2021
834d485
Add mothership global param bounds
mikeyrf Dec 13, 2021
6f700ff
Fix mothership test dir w init, clean up mothership adjust params
mikeyrf Dec 13, 2021
0d26079
Add mothership success tests for market
mikeyrf Dec 14, 2021
611f1e6
Fix mothership success tests, init revert tests
mikeyrf Dec 14, 2021
e395ca8
Finish mothership market sucess tests
mikeyrf Dec 14, 2021
66fa1cf
Init mothership collateral tests
mikeyrf Dec 14, 2021
f65c7bc
Add mothership collateral tests
mikeyrf Dec 16, 2021
9d5cc51
Finish mothership collateral tests
mikeyrf Dec 16, 2021
d39c1c3
Fix mothership global param setters and getter
mikeyrf Dec 17, 2021
78d5330
Add mothership global param tests
mikeyrf Dec 17, 2021
491ce1f
Add setter for margin burn rate
mikeyrf Dec 13, 2021
e213ad2
Fix collateral active bool flip on enable/disableCollateral
mikeyrf Dec 13, 2021
9b86527
Fix require msgs for mothership
mikeyrf Dec 13, 2021
69ee4f5
Add mothership conftest
mikeyrf Dec 13, 2021
bd78105
Add mothership config tests
mikeyrf Dec 13, 2021
ff13d94
Rebase main - set ovl mothership
mikeyrf Dec 20, 2021
48ac18b
Make ovl token immutable in mothership
mikeyrf Dec 13, 2021
55da9bc
Rebase main - add events to mothership
mikeyrf Dec 20, 2021
dbff53f
Remove guardian role from mothership
mikeyrf Dec 13, 2021
216dcba
Add mothership global param bounds
mikeyrf Dec 13, 2021
9c7332a
Fix mothership test dir w init, clean up mothership adjust params
mikeyrf Dec 13, 2021
7e4d760
Rebase main - add mothership success tests
mikeyrf Dec 20, 2021
513a198
Fix mothership success tests, init revert tests
mikeyrf Dec 14, 2021
886b8f0
Rebase main conflicts - Finish mothership market sucess tests
mikeyrf Dec 20, 2021
178c7ce
Init mothership collateral tests
mikeyrf Dec 14, 2021
44c955d
Add mothership collateral tests
mikeyrf Dec 16, 2021
74e22d6
Finish mothership collateral tests
mikeyrf Dec 16, 2021
6d7bd76
Rebase main conflicts - Fix mothership global param setters and getter
mikeyrf Dec 20, 2021
eb826de
Add mothership global param tests
mikeyrf Dec 17, 2021
1dc9544
Merge branch 'c4-mothership' of github.com:overlay-market/overlay-v1-…
mikeyrf Dec 20, 2021
36e88c4
Fix conftest tests for mothership changes
mikeyrf Dec 20, 2021
c51f56c
Fix comments in conftest
mikeyrf Dec 20, 2021
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
16 changes: 8 additions & 8 deletions contracts/collateral/OverlayV1OVLCollateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
/// @notice Disburses fees
function disburse () public {

( uint256 _marginBurnRate,
( address _feeTo,,
uint256 _feeBurnRate,
address _feeTo ) = mothership.getUpdateParams();
uint256 _marginBurnRate ) = mothership.getGlobalParams();

uint _feeForward = fees;
uint _feeBurn = _feeForward.mulUp(_feeBurnRate);
_feeForward = _feeForward - _feeBurn;
_feeForward -= _feeBurn;

uint _liqForward = liquidations;
uint _liqBurn = _liqForward.mulUp(_marginBurnRate);
Expand Down Expand Up @@ -236,7 +236,7 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

/**
@notice Build a position on Overlay with OVL collateral
@dev This interacts with an Overlay Market to register oi and hold
@dev This interacts with an Overlay Market to register oi and hold
positions on behalf of users.
@dev Build event emitted
@param _market The address of the desired market to interact with
Expand Down Expand Up @@ -355,16 +355,16 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
if (_userCost < _userValueAdjusted) {

ovl.transferMint(
msg.sender,
_userCost,
msg.sender,
_userCost,
_userValueAdjusted - _userCost
);

} else {

ovl.transferBurn(
msg.sender,
_userValueAdjusted,
msg.sender,
_userValueAdjusted,
_userCost - _userValueAdjusted
);

Expand Down
32 changes: 8 additions & 24 deletions contracts/interfaces/IOverlayV1Mothership.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface IOverlayV1Mothership {
bool
);

function allCollateral(
function allCollaterals(
uint collateralIndex
) external view returns (
address collateralAddress
Expand All @@ -51,40 +51,24 @@ interface IOverlayV1Mothership {
);

function getGlobalParams() external view returns (
uint16 fee_,
uint16 feeBurnRate_,
address feeTo_,
uint8 marginMaintenance_,
uint8 marginBurnRate_
);

function getUpdateParams() external view returns (
uint marginBurnRate_,
uint fee_,
uint feeBurnRate_,
address feeTo_
);

function getMarginParams() external view returns (
uint marginMaintenance_,
uint marginRewardRate_
uint marginBurnRate_
);

function fee() external view returns (uint256);

function updateMarket(
address _market,
address _rewardsTo
) external;

function massUpdateMarkets(
address _rewardsTo
) external;

function hasRole(
bytes32 _role,
address _account
) external view returns (
bool
);

function setFeeTo(address _feeTo) external;
function setFee(uint _fee) external;
function setFeeBurnRate(uint _feeBurnRate) external;
function setMarginBurnRate(uint _marginBurnRate) external;

}
179 changes: 100 additions & 79 deletions contracts/mothership/OverlayV1Mothership.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,51 @@ import "../OverlayToken.sol";

contract OverlayV1Mothership is AccessControlEnumerable {

uint16 public constant MIN_FEE = 1; // 0.01%
uint16 public constant MAX_FEE = 100; // 1.00%
uint public constant MIN_FEE = 1e14; // 0.01%
uint public constant MAX_FEE = 1e16; // 1.00%
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we see scenarios on long tail markets where we want more?


uint16 public constant MIN_MARGIN_MAINTENANCE = 100; // 1% maintenance
uint16 public constant MAX_MARGIN_MAINTENANCE = 6000; // 60% maintenance
uint public constant MAX_FEE_BURN = 1e18; // 100%
uint public constant MAX_MARGIN_BURN = 1e18; // 100%

bytes32 public constant ADMIN = 0x00;
bytes32 public constant GOVERNOR = keccak256("GOVERNOR");
bytes32 public constant GUARDIAN = keccak256("GUARDIAN");
bytes32 public constant MINTER = keccak256("MINTER");
bytes32 public constant BURNER = keccak256("BURNER");

// ovl erc20 token
address public ovl;

// portion of liquidations to burn on update
uint public marginBurnRate;
address public immutable ovl;

// global params adjustable by gov
// address to send fees to
address public feeTo;
// build/unwind trading fee
uint public fee;
// portion of build/unwind fee burnt
uint public feeBurnRate;
// address to send fees to
address public feeTo;
// portion of liquidations to burn on update
uint public marginBurnRate;

mapping(address => bool) public marketActive;
mapping(address => bool) public marketExists;
address[] public allMarkets;

mapping(address => bool) public collateralExists;
mapping(address => bool) public collateralActive;
address[] public allCollateral;
address[] public allCollaterals;

event UpdateCollateral(address collateral, bool active);
event UpdateMarket(address market, bool active);

event UpdateFeeTo(address feeTo);
event UpdateFee(uint fee);
event UpdateFeeBurnRate(uint feeBurnRate);
event UpdateMarginBurnRate(uint marginBurnRate);

modifier onlyGovernor () {
require(hasRole(GOVERNOR, msg.sender), "OVLV1:!gov");
_;
}

modifier onlyGuardian () {
require(hasRole(GUARDIAN, msg.sender), "OVLV1:!guard");
_;
}

constructor(
address _ovl,
address _feeTo,
uint _fee,
uint _feeBurnRate,
Expand All @@ -60,65 +60,59 @@ contract OverlayV1Mothership is AccessControlEnumerable {

_setupRole(ADMIN, msg.sender);
_setupRole(GOVERNOR, msg.sender);
_setupRole(GUARDIAN, msg.sender);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing the guardian role? I don't have any strong opinions yet about the types of roles we need but it is likely more than just admin and governor.

Governor would be something with a reasonable time lock for instance.

Guardian would be something along the lines of emergency shutdown.

Admin, that'd be to grant/remove various roles.

Not sure though. I'd like to look at other protocol's thought processes. Fei in particular. We are in the realm of somewhat more permissioned operation so maybe protocols like Aave and Compound should also be taken into account.

_setRoleAdmin(GOVERNOR, ADMIN);
_setRoleAdmin(GUARDIAN, ADMIN);

// global params
fee = _fee;
feeBurnRate = _feeBurnRate;
feeTo = _feeTo;
marginBurnRate = _marginBurnRate;

}

function setOVL (address _ovl) external onlyGovernor {

// immutable params
ovl = _ovl;

// global params
_setFeeTo(_feeTo);
_setFee(_fee);
_setFeeBurnRate(_feeBurnRate);
_setMarginBurnRate(_marginBurnRate);
}

function totalMarkets () external view returns (uint) {
return allMarkets.length;
}

function totalCollaterals () external view returns (uint) {
return allCollaterals.length;
}

/**
@notice Make this contract aware of a new market contract's existence
@dev Should be called after contract deployment in specific market factory.createMarket
@dev Only the Governor can initialize a market
@dev Appends new market address to `allMarkets` array to track them
@param market Overlay market contract address
@param _market Overlay market contract address
*/
function initializeMarket(address market) external onlyGovernor {

require(!marketExists[market], "OVLV1:!!initialized");

marketExists[market] = true;
marketActive[market] = true;
function initializeMarket(address _market) external onlyGovernor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really initializing a market, that seems to say we are instantiating some state on the market. We are just adding it to the manifest here in the mothership.

require(!marketExists[_market], "OVLV1: market exists");

allMarkets.push(market);
marketExists[_market] = true;
marketActive[_market] = true;
allMarkets.push(_market);

emit UpdateMarket(_market, true);
}

/// @notice Disables an existing market contract for a mirin market
function disableMarket(address market) external onlyGovernor {
function disableMarket(address _market) external onlyGovernor {
require(marketExists[_market], "OVLV1: market !exists");
require(marketActive[_market], "OVLV1: market !enabled");

require(marketActive[market], "OVLV1: !enabled");

marketActive[market] = false;
marketActive[_market] = false;

emit UpdateMarket(_market, false);
}

/// @notice Enables an existing market contract for a mirin market
function enableMarket(address market) external onlyGovernor {

require(marketExists[market], "OVLV1: !exists");
function enableMarket(address _market) external onlyGovernor {
require(marketExists[_market], "OVLV1: market !exists");
require(!marketActive[_market], "OVLV1: market !disabled");

require(!marketActive[market], "OVLV1: !disabled");

marketActive[market] = true;
marketActive[_market] = true;

emit UpdateMarket(_market, true);
}

/**
Expand All @@ -128,65 +122,92 @@ contract OverlayV1Mothership is AccessControlEnumerable {
@param _collateral Overlay OVL collateral contract address
*/
function initializeCollateral (address _collateral) external onlyGovernor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, not really initializing here, just adding to the manifest and activating


require(!collateralExists[_collateral], "OVLV1:!!iintialized");
require(!collateralExists[_collateral], "OVLV1: collateral exists");

collateralExists[_collateral] = true;
collateralActive[_collateral] = true;

allCollateral.push(_collateral);
allCollaterals.push(_collateral);

OverlayToken(ovl).grantRole(OverlayToken(ovl).MINTER_ROLE(), _collateral);

OverlayToken(ovl).grantRole(OverlayToken(ovl).BURNER_ROLE(), _collateral);

emit UpdateCollateral(_collateral, true);
}

function enableCollateral (address _collateral) external onlyGovernor {
require(collateralExists[_collateral], "OVLV1: collateral !exists");
require(!collateralActive[_collateral], "OVLV1: collateral !disabled");

require(collateralExists[_collateral], "OVLV1:!exists");

require(!collateralActive[_collateral], "OVLV1:!disabled");
collateralActive[_collateral] = true;

OverlayToken(ovl).grantRole(OverlayToken(ovl).MINTER_ROLE(), _collateral);

OverlayToken(ovl).grantRole(OverlayToken(ovl).BURNER_ROLE(), _collateral);

emit UpdateCollateral(_collateral, true);
}

function disableCollateral (address _collateral) external onlyGovernor {
require(collateralExists[_collateral], "OVLV1: collateral !exists");
require(collateralActive[_collateral], "OVLV1: collateral !enabled");

require(collateralActive[_collateral], "OVLV1:!enabled");
collateralActive[_collateral] = false;

OverlayToken(ovl).revokeRole(OverlayToken(ovl).MINTER_ROLE(), _collateral);

OverlayToken(ovl).revokeRole(OverlayToken(ovl).BURNER_ROLE(), _collateral);

emit UpdateCollateral(_collateral, false);
}

function setFeeTo(address _feeTo) external onlyGovernor {
_setFeeTo(_feeTo);
}

function setFee(uint _fee) external onlyGovernor {
_setFee(_fee);
}

function setFeeBurnRate(uint _feeBurnRate) external onlyGovernor {
_setFeeBurnRate(_feeBurnRate);
}

/// @notice Allows gov to adjust per market params
function setMarginBurnRate(uint _marginBurnRate) external onlyGovernor {
_setMarginBurnRate(_marginBurnRate);
}

function _setFeeTo(address _feeTo) internal {
require(_feeTo != address(0), "OVLV1: fees to the zero address");
feeTo = _feeTo;
emit UpdateFeeTo(_feeTo);
}

/// @notice Allows gov to adjust global params
function adjustGlobalParams(
uint16 _fee,
uint16 _feeBurnRate,
address _feeTo
) external onlyGovernor {
function _setFee(uint _fee) internal {
require(_fee >= MIN_FEE && _fee <= MAX_FEE, "OVLV1: fee rate out of bounds");
fee = _fee;
emit UpdateFee(_fee);
}

function _setFeeBurnRate(uint _feeBurnRate) internal {
require(_feeBurnRate <= MAX_FEE_BURN, "OVLV1: fee burn rate out of bounds");
feeBurnRate = _feeBurnRate;
feeTo = _feeTo;
emit UpdateFeeBurnRate(_feeBurnRate);
}

function _setMarginBurnRate(uint _marginBurnRate) internal {
require(_marginBurnRate <= MAX_MARGIN_BURN, "OVLV1: margin burn rate out of bounds");
marginBurnRate = _marginBurnRate;
emit UpdateMarginBurnRate(_marginBurnRate);
}

function getUpdateParams() external view returns (
uint,
uint,
address
function getGlobalParams() external view returns (
address feeTo_,
uint fee_,
uint feeBurnRate_,
uint marginBurnRate_
) {
return (
marginBurnRate,
feeBurnRate,
feeTo
);
feeTo_ = feeTo;
fee_ = fee;
feeBurnRate_ = feeBurnRate;
marginBurnRate_ = marginBurnRate;
}

}
Loading