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

Pauser Roles #49

Open
wants to merge 8 commits into
base: merge-train-r2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 12 additions & 6 deletions packages/contracts/contracts/CollateralManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

// Libraries
import "@openzeppelin/contracts-upgradeable/utils/structs/EnumerableSetUpgradeable.sol";

import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
// Interfaces
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/IERC721Upgradeable.sol";
Expand Down Expand Up @@ -70,14 +70,19 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
_;
}

modifier onlyProtocolOwner() {
modifier onlyProtocolOwner() {

address protocolOwner = OwnableUpgradeable(address(tellerV2)).owner();

require(_msgSender() == protocolOwner, "Sender not authorized");
_;
}

modifier whenProtocolNotPaused() {
require( PausableUpgradeable(address(tellerV2)).paused() == false , "Protocol is paused");
_;
}

/* External Functions */

/**
Expand Down Expand Up @@ -262,7 +267,7 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
* @notice Withdraws deposited collateral from the created escrow of a bid that has been successfully repaid.
* @param _bidId The id of the bid to withdraw collateral for.
*/
function withdraw(uint256 _bidId) external {
function withdraw(uint256 _bidId) external whenProtocolNotPaused {
BidState bidState = tellerV2.getBidState(_bidId);

require(bidState == BidState.PAID, "collateral cannot be withdrawn");
Expand All @@ -277,7 +282,7 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
address _tokenAddress,
uint256 _amount,
address _recipientAddress
) external onlyProtocolOwner {
) external onlyProtocolOwner whenProtocolNotPaused {

ICollateralEscrowV1(_escrows[_bidId]).withdrawDustTokens(
_tokenAddress,
Expand All @@ -290,7 +295,7 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
* @notice Withdraws deposited collateral from the created escrow of a bid that has been CLOSED after being defaulted.
* @param _bidId The id of the bid to withdraw collateral for.
*/
function lenderClaimCollateral(uint256 _bidId) external onlyTellerV2 {
function lenderClaimCollateral(uint256 _bidId) external onlyTellerV2 whenProtocolNotPaused {
if (isBidCollateralBacked(_bidId)) {
BidState bidState = tellerV2.getBidState(_bidId);

Expand All @@ -308,7 +313,7 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
* @notice Withdraws deposited collateral from the created escrow of a bid that has been CLOSED after being defaulted.
* @param _bidId The id of the bid to withdraw collateral for.
*/
function lenderClaimCollateralWithRecipient(uint256 _bidId, address _collateralRecipient) external onlyTellerV2 {
function lenderClaimCollateralWithRecipient(uint256 _bidId, address _collateralRecipient) external onlyTellerV2 whenProtocolNotPaused {
if (isBidCollateralBacked(_bidId)) {
BidState bidState = tellerV2.getBidState(_bidId);

Expand All @@ -331,6 +336,7 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
function liquidateCollateral(uint256 _bidId, address _liquidatorAddress)
external
onlyTellerV2
whenProtocolNotPaused
{
if (isBidCollateralBacked(_bidId)) {
BidState bidState = tellerV2.getBidState(_bidId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import "../interfaces/ILenderCommitmentForwarder.sol";
import "../interfaces/ISmartCommitmentForwarder.sol";
import "./LenderCommitmentForwarder_G1.sol";

import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";

import { CommitmentCollateralType, ISmartCommitment } from "../interfaces/ISmartCommitment.sol";


contract SmartCommitmentForwarder is
ExtensionsContextUpgradeable, //this should always be first for upgradeability
ExtensionsContextUpgradeable, //this should always be first for upgradeability
TellerV2MarketForwarder_G3,
PausableUpgradeable, //this does add some storage but AFTER all other storage
ISmartCommitmentForwarder
{
event ExercisedSmartCommitment(
Expand All @@ -24,9 +27,23 @@ contract SmartCommitmentForwarder is

error InsufficientBorrowerCollateral(uint256 required, uint256 actual);



modifier onlyProtocolPauser() {
require( ITellerV2( _tellerV2 ).isPauser(_msgSender()) , "Sender not authorized");
_;
}



constructor(address _protocolAddress, address _marketRegistry)
TellerV2MarketForwarder_G3(_protocolAddress, _marketRegistry)
{}
{ }

function initialize() public initializer {
__Pausable_init();
}


/**
* @notice Accept the commitment to submitBid and acceptBid using the funds
Expand All @@ -50,7 +67,7 @@ contract SmartCommitmentForwarder is
address _recipient,
uint16 _interestRate,
uint32 _loanDuration
) public returns (uint256 bidId) {
) public whenNotPaused returns (uint256 bidId) {
require(
ISmartCommitment(_smartCommitmentAddress)
.getCollateralTokenType() <=
Expand Down Expand Up @@ -162,6 +179,23 @@ contract SmartCommitmentForwarder is



/**
* @notice Lets the DAO/owner of the protocol implement an emergency stop mechanism.
*/
function pause() public virtual onlyProtocolPauser whenNotPaused {
_pause();
}

/**
* @notice Lets the DAO/owner of the protocol undo a previously implemented emergency stop.
*/
function unpause() public virtual onlyProtocolPauser whenPaused {
_unpause();
}


// -----

//Overrides
function _msgSender()
internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ contract LenderCommitmentGroup_Smart is
_;
}

modifier whenForwarderNotPaused() {
require( PausableUpgradeable(address(SMART_COMMITMENT_FORWARDER)).paused() == false , "Protocol is paused");
_;
}



/// @custom:oz-upgrades-unsafe-allow constructor
constructor(
address _tellerV2,
Expand Down Expand Up @@ -374,7 +381,7 @@ contract LenderCommitmentGroup_Smart is
uint256 _amount,
address _sharesRecipient,
uint256 _minSharesAmountOut
) external returns (uint256 sharesAmount_) {
) external whenForwarderNotPaused returns (uint256 sharesAmount_) {
//transfers the primary principal token from msg.sender into this contract escrow


Expand Down Expand Up @@ -445,7 +452,7 @@ contract LenderCommitmentGroup_Smart is
uint256 _collateralTokenId,
uint32 _loanDuration,
uint16 _interestRate
) external onlySmartCommitmentForwarder whenNotPaused {
) external onlySmartCommitmentForwarder whenForwarderNotPaused {

require(
_collateralTokenAddress == address(collateralToken),
Expand Down Expand Up @@ -505,7 +512,7 @@ contract LenderCommitmentGroup_Smart is

function prepareSharesForWithdraw(
uint256 _amountPoolSharesTokens
) external returns (bool) {
) external whenForwarderNotPaused returns (bool) {
require( poolSharesToken.balanceOf(msg.sender) >= _amountPoolSharesTokens );

poolSharesPreparedToWithdrawForLender[msg.sender] = _amountPoolSharesTokens;
Expand All @@ -523,7 +530,7 @@ contract LenderCommitmentGroup_Smart is
uint256 _amountPoolSharesTokens,
address _recipient,
uint256 _minAmountOut
) external returns (uint256) {
) external whenForwarderNotPaused returns (uint256) {

require(poolSharesPreparedToWithdrawForLender[msg.sender] >= _amountPoolSharesTokens,"Shares not prepared for withdraw");
require(poolSharesPreparedTimestamp[msg.sender] <= block.timestamp - WITHDRAW_DELAY_TIME_SECONDS,"Shares not prepared for withdraw");
Expand Down Expand Up @@ -566,7 +573,7 @@ contract LenderCommitmentGroup_Smart is
function liquidateDefaultedLoanWithIncentive(
uint256 _bidId,
int256 _tokenAmountDifference
) public bidIsActiveForGroup(_bidId) {
) public whenForwarderNotPaused bidIsActiveForGroup(_bidId) {

//use original principal amount as amountDue

Expand Down Expand Up @@ -863,7 +870,7 @@ contract LenderCommitmentGroup_Smart is
address repayer,
uint256 principalAmount,
uint256 interestAmount
) external onlyTellerV2 {
) external onlyTellerV2 whenForwarderNotPaused {
//can use principal amt to increment amt paid back!! nice for math .
totalPrincipalTokensRepaid += principalAmount;
totalInterestCollected += interestAmount;
Expand All @@ -883,7 +890,7 @@ contract LenderCommitmentGroup_Smart is
If principaltokens get stuck in the escrow vault for any reason, anyone may
call this function to move them from that vault in to this contract
*/
function withdrawFromEscrowVault ( uint256 _amount ) public {
function withdrawFromEscrowVault ( uint256 _amount ) public whenForwarderNotPaused {


address _escrowVault = ITellerV2(TELLER_V2).getEscrowVault();
Expand Down
69 changes: 60 additions & 9 deletions packages/contracts/contracts/TellerV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ contract TellerV2 is
_;
}


modifier onlyPauser() {

require( pauserRoleBearer[_msgSender()] || owner() == _msgSender(), "Requires role: Pauser");


_;
}


modifier whenLiquidationsNotPaused() {
require(!liquidationsPaused, "Liquidations are paused");

_;
}



/** Constant Variables **/

uint8 public constant CURRENT_CODE_VERSION = 10;
Expand Down Expand Up @@ -708,21 +726,53 @@ contract TellerV2 is
}

/**
* @notice Lets the DAO/owner of the protocol implement an emergency stop mechanism.
* @notice Lets a pauser of the protocol implement an emergency stop mechanism.
*/
function pauseProtocol() public virtual onlyOwner whenNotPaused {
function pauseProtocol() public virtual onlyPauser whenNotPaused {
_pause();
}

/**
* @notice Lets the DAO/owner of the protocol undo a previously implemented emergency stop.
* @notice Lets a pauser of the protocol undo a previously implemented emergency stop.
*/
function unpauseProtocol() public virtual onlyOwner whenPaused {
function unpauseProtocol() public virtual onlyPauser whenPaused {
_unpause();
}


/**
* @notice Lets a pauser of the protocol implement an emergency stop mechanism.
*/
function pauseLiquidations() public virtual onlyPauser {
liquidationsPaused = true;
}

/**
* @notice Lets a pauser of the protocol undo a previously implemented emergency stop.
*/
function unpauseLiquidations() public virtual onlyPauser {
liquidationsPaused = false;
}



function addPauser(address _pauser) public virtual onlyOwner {
pauserRoleBearer[_pauser] = true;
}


function removePauser(address _pauser) public virtual onlyOwner {
pauserRoleBearer[_pauser] = false;
}


function isPauser(address _account) public view returns(bool){
return pauserRoleBearer[_account] ;
}


function lenderCloseLoan(uint256 _bidId)
external
external whenNotPaused whenLiquidationsNotPaused
acceptedLoan(_bidId, "lenderClaimCollateral")
{
Bid storage bid = bids[_bidId];
Expand All @@ -738,7 +788,7 @@ contract TellerV2 is
function lenderCloseLoanWithRecipient(
uint256 _bidId,
address _collateralRecipient
) external {
) external whenNotPaused whenLiquidationsNotPaused {
_lenderCloseLoanWithRecipient(_bidId, _collateralRecipient);
}

Expand All @@ -765,7 +815,7 @@ contract TellerV2 is
* @param _bidId The id of the loan to make the payment towards.
*/
function liquidateLoanFull(uint256 _bidId)
external
external whenNotPaused whenLiquidationsNotPaused
acceptedLoan(_bidId, "liquidateLoan")
{
Bid storage bid = bids[_bidId];
Expand All @@ -777,7 +827,7 @@ contract TellerV2 is
}

function liquidateLoanFullWithRecipient(uint256 _bidId, address _recipient)
external
external whenNotPaused whenLiquidationsNotPaused
acceptedLoan(_bidId, "liquidateLoan")
{
_liquidateLoanFull(_bidId, _recipient);
Expand Down Expand Up @@ -861,7 +911,8 @@ contract TellerV2 is
_borrowerBidsActive[bid.borrower].remove(_bidId);

// If loan is is being liquidated and backed by collateral, withdraw and send to borrower
if (_shouldWithdrawCollateral) {
if (_shouldWithdrawCollateral) {

// _getCollateralManagerForBid(_bidId).withdraw(_bidId);
collateralManager.withdraw(_bidId);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/contracts/contracts/TellerV2Storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ abstract contract TellerV2Storage_G6 is TellerV2Storage_G5 {
mapping(uint256 => address) public repaymentListenerForBid;
}

abstract contract TellerV2Storage is TellerV2Storage_G6 {}
abstract contract TellerV2Storage_G7 is TellerV2Storage_G6 {
mapping(address => bool) public pauserRoleBearer;
bool public liquidationsPaused;
}

abstract contract TellerV2Storage is TellerV2Storage_G7 {}
3 changes: 3 additions & 0 deletions packages/contracts/contracts/interfaces/ITellerV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,7 @@ interface ITellerV2 {


function getEscrowVault() external view returns(address);


function isPauser(address _account) external view returns(bool);
}
10 changes: 10 additions & 0 deletions packages/contracts/contracts/mock/TellerV2SolMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ contract TellerV2SolMock is ITellerV2, IProtocolFee, TellerV2Storage , ILoanRepa
uint256 public amountOwedMockPrincipal;
uint256 public amountOwedMockInterest;
address public approvedForwarder;
bool public isPausedMock;


PaymentCycleType globalBidPaymentCycleType = PaymentCycleType.Seconds;
Expand All @@ -45,6 +46,15 @@ contract TellerV2SolMock is ITellerV2, IProtocolFee, TellerV2Storage , ILoanRepa
}


function paused() external view returns(bool){
return isPausedMock;
}


function isPauser(address _account) public view returns(bool){
return false; //for now
}

function approveMarketForwarder(uint256 _marketId, address _forwarder)
external
{
Expand Down
Loading
Loading