forked from neptune-mutual-blue/protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Role-Based Access Control and Contract Size Optimization
- Updated Open Zeppelin version - Added a new library `AccessControlLibV1` for role based access control. This library infers the roles defined in the the main protocol contract and enables the calling contracts to check if a given user has permission to execute certain transactions - Added a new base contract for the protocol - Added numerous roles: Admin, Pause Agent, Unpause Agent, Governance Agent, Cover Manager, and Liquidity Manager. - The protocol contract has updated features related to `pause`, `unpause`, and `access control`. The rest of the smart contracts now infer the state of the protocol instead of re-implementing the above features and duplicating logic. - Refactored the protocol base to use `Role Based Access Control` instead of ownership module - Refactored `Recoverable` to drop the ownership dependency. Moved logic of Recoverable to a new library `BaseLibV1` resulting in a much smaller smart contracts implementing this contract. - Updated `finalize` feature on the governance contract to be only accessible by governance agent. - Updated `increaseProvision` feature on the cover provision contract to be only accessible to liquidity manager. - Refactored vault contract and moved the logic to `VaultLibV1` - Refactored `setPolicyRatesByKey` on policy admin contract to only allow access by cover manager. - Added contracts: FakeRecoverable and IPausable - Refactored the vault interface to and merged events for better clarity. The event `LiquidityAdded` is now merged to `PodsIssued` whereas the event `LiquidityRemoved` is merged to `PodsRedeemed` - Updated the documentation - Refactored and fixed unit tests and stories
- Loading branch information
Showing
129 changed files
with
4,033 additions
and
953 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Neptune Mutual Protocol (https://neptunemutual.com) | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity >=0.4.22 <0.9.0; | ||
import "openzeppelin-solidity/contracts/access/AccessControl.sol"; | ||
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; | ||
import "openzeppelin-solidity/contracts/security/Pausable.sol"; | ||
import "../libraries/ProtoUtilV1.sol"; | ||
import "./Recoverable.sol"; | ||
|
||
abstract contract ProtoBase is AccessControl, Pausable, Recoverable { | ||
using ProtoUtilV1 for IStore; | ||
|
||
constructor(IStore store) Recoverable(store) { | ||
_setAccessPolicy(); | ||
} | ||
|
||
function _setAccessPolicy() private { | ||
_setRoleAdmin(AccessControlLibV1.NS_ROLES_ADMIN, AccessControlLibV1.NS_ROLES_ADMIN); | ||
_setRoleAdmin(AccessControlLibV1.NS_ROLES_COVER_MANAGER, AccessControlLibV1.NS_ROLES_ADMIN); | ||
_setRoleAdmin(AccessControlLibV1.NS_ROLES_LIQUIDITY_MANAGER, AccessControlLibV1.NS_ROLES_ADMIN); | ||
_setRoleAdmin(AccessControlLibV1.NS_ROLES_GOVERNANCE_AGENT, AccessControlLibV1.NS_ROLES_ADMIN); | ||
_setRoleAdmin(AccessControlLibV1.NS_ROLES_UPGRADE_AGENT, AccessControlLibV1.NS_ROLES_ADMIN); | ||
_setRoleAdmin(AccessControlLibV1.NS_ROLES_RECOVERY_AGENT, AccessControlLibV1.NS_ROLES_ADMIN); | ||
_setRoleAdmin(AccessControlLibV1.NS_ROLES_PAUSE_AGENT, AccessControlLibV1.NS_ROLES_ADMIN); | ||
_setRoleAdmin(AccessControlLibV1.NS_ROLES_UNPAUSE_AGENT, AccessControlLibV1.NS_ROLES_ADMIN); | ||
|
||
_setupRole(AccessControlLibV1.NS_ROLES_ADMIN, msg.sender); | ||
} | ||
|
||
function setupRole( | ||
bytes32 role, | ||
bytes32 adminRole, | ||
address account | ||
) external { | ||
AccessControlLibV1.mustBeAdmin(s); | ||
|
||
_setRoleAdmin(role, adminRole); | ||
|
||
if (account != address(0)) { | ||
_setupRole(role, account); | ||
} | ||
} | ||
|
||
/** | ||
* @dev Pauses this contract. | ||
* Can only be called by "Pause Agents". | ||
*/ | ||
function pause() external { | ||
AccessControlLibV1.mustBePauseAgent(s); | ||
super._pause(); | ||
} | ||
|
||
/** | ||
* @dev Unpauses this contract. | ||
* Can only be called by "Unpause Agents". | ||
*/ | ||
function unpause() external whenPaused { | ||
AccessControlLibV1.mustBeUnpauseAgent(s); | ||
super._unpause(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,34 @@ | ||
// Neptune Mutual Protocol (https://neptunemutual.com) | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity >=0.4.22 <0.9.0; | ||
import "../libraries/ProtoUtilV1.sol"; | ||
import "openzeppelin-solidity/contracts/access/Ownable.sol"; | ||
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; | ||
import "openzeppelin-solidity/contracts/security/ReentrancyGuard.sol"; | ||
import "openzeppelin-solidity/contracts/security/Pausable.sol"; | ||
import "../libraries/BaseLibV1.sol"; | ||
import "../libraries/ValidationLibV1.sol"; | ||
|
||
abstract contract Recoverable is Ownable, ReentrancyGuard, Pausable { | ||
using ProtoUtilV1 for IStore; | ||
abstract contract Recoverable is ReentrancyGuard { | ||
IStore public s; | ||
|
||
constructor(IStore store) { | ||
require(address(store) != address(0), "Invalid Store"); | ||
|
||
s = store; | ||
} | ||
|
||
/** | ||
* @dev Recover all Ether held by the contract. | ||
* On success, no event is emitted because the recovery feature does | ||
* not have any significance in the SDK or the UI. | ||
*/ | ||
function recoverEther(address sendTo) external { | ||
_mustBeOwnerOrProtoOwner(); | ||
|
||
// slither-disable-next-line arbitrary-send | ||
payable(sendTo).transfer(address(this).balance); | ||
BaseLibV1.recoverEther(s, sendTo); | ||
} | ||
|
||
/** | ||
* @dev Recover all BEP-20 compatible tokens sent to this address. | ||
* @param token BEP-20 The address of the token contract | ||
* @dev Recover all IERC-20 compatible tokens sent to this address. | ||
* On success, no event is emitted because the recovery feature does | ||
* not have any significance in the SDK or the UI. | ||
* @param token IERC-20 The address of the token contract | ||
*/ | ||
function recoverToken(address token, address sendTo) external { | ||
_mustBeOwnerOrProtoOwner(); | ||
|
||
IERC20 bep20 = IERC20(token); | ||
|
||
uint256 balance = bep20.balanceOf(address(this)); | ||
require(bep20.transfer(sendTo, balance), "Transfer failed"); | ||
} | ||
|
||
function pause() external { | ||
_mustBeUnpaused(); | ||
_mustBeOwnerOrProtoOwner(); | ||
|
||
super._pause(); | ||
} | ||
|
||
function unpause() external whenPaused { | ||
_mustBeOwnerOrProtoOwner(); | ||
|
||
super._unpause(); | ||
} | ||
|
||
/** | ||
* @dev Reverts if the sender is not the contract owner or a protocol member. | ||
*/ | ||
function _mustBeOwnerOrProtoMember() internal view { | ||
bool isProtocol = s.isProtocolMember(super._msgSender()); | ||
|
||
if (isProtocol == false) { | ||
require(super._msgSender() == super.owner(), "Forbidden"); | ||
} | ||
} | ||
|
||
/** | ||
* @dev Reverts if the sender is not the contract owner or protocol owner. | ||
*/ | ||
function _mustBeOwnerOrProtoOwner() internal view { | ||
IProtocol protocol = ProtoUtilV1.getProtocol(s); | ||
|
||
if (address(protocol) == address(0)) { | ||
require(super._msgSender() == owner(), "Forbidden"); | ||
return; | ||
} | ||
|
||
address protocolOwner = Ownable(address(protocol)).owner(); | ||
require(super._msgSender() == owner() || super._msgSender() == protocolOwner, "Forbidden"); | ||
} | ||
|
||
function _mustBeUnpaused() internal view { | ||
require(super.paused() == false, "Contract is paused"); | ||
|
||
address protocol = ProtoUtilV1.getProtocolAddress(s); | ||
require(Pausable(protocol).paused() == false, "Protocol is paused"); | ||
BaseLibV1.recoverToken(s, token, sendTo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.