Skip to content

Commit

Permalink
Merge pull request #589 from Badger-Finance/fix-rm-pause
Browse files Browse the repository at this point in the history
Fix rm pause
  • Loading branch information
dapp-whisperer authored Aug 29, 2023
2 parents 73f744e + 03bac6a commit b4f4682
Show file tree
Hide file tree
Showing 24 changed files with 2,014 additions and 146 deletions.
10 changes: 5 additions & 5 deletions packages/contracts/contracts/ActivePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ contract ActivePool is IActivePool, ERC3156FlashLender, ReentrancyGuard, BaseMat
uint256 _FeeRecipientColl = FeeRecipientColl;
require(_FeeRecipientColl >= _shares, "ActivePool: Insufficient fee recipient coll");

ICdpManagerData(cdpManagerAddress).applyPendingGlobalState();
ICdpManagerData(cdpManagerAddress).syncPendingGlobalState();

unchecked {
_FeeRecipientColl -= _shares;
Expand All @@ -368,7 +368,7 @@ contract ActivePool is IActivePool, ERC3156FlashLender, ReentrancyGuard, BaseMat
uint256 balance = IERC20(token).balanceOf(address(this));
require(amount <= balance, "ActivePool: Attempt to sweep more than balance");

ICdpManagerData(cdpManagerAddress).applyPendingGlobalState();
ICdpManagerData(cdpManagerAddress).syncPendingGlobalState();

address cachedFeeRecipientAddress = feeRecipientAddress; // Saves an SLOAD

Expand All @@ -383,7 +383,7 @@ contract ActivePool is IActivePool, ERC3156FlashLender, ReentrancyGuard, BaseMat
"ActivePool: Cannot set fee recipient to zero address"
);

ICdpManagerData(cdpManagerAddress).applyPendingGlobalState();
ICdpManagerData(cdpManagerAddress).syncPendingGlobalState();

feeRecipientAddress = _feeRecipientAddress;
emit FeeRecipientAddressChanged(_feeRecipientAddress);
Expand All @@ -392,7 +392,7 @@ contract ActivePool is IActivePool, ERC3156FlashLender, ReentrancyGuard, BaseMat
function setFeeBps(uint _newFee) external requiresAuth {
require(_newFee <= MAX_FEE_BPS, "ERC3156FlashLender: _newFee should <= MAX_FEE_BPS");

ICdpManagerData(cdpManagerAddress).applyPendingGlobalState();
ICdpManagerData(cdpManagerAddress).syncPendingGlobalState();

// set new flash fee
uint _oldFee = feeBps;
Expand All @@ -401,7 +401,7 @@ contract ActivePool is IActivePool, ERC3156FlashLender, ReentrancyGuard, BaseMat
}

function setFlashLoansPaused(bool _paused) external requiresAuth {
ICdpManagerData(cdpManagerAddress).applyPendingGlobalState();
ICdpManagerData(cdpManagerAddress).syncPendingGlobalState();

flashLoansPaused = _paused;
emit FlashLoansPaused(msg.sender, _paused);
Expand Down
56 changes: 47 additions & 9 deletions packages/contracts/contracts/BorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,28 @@ contract BorrowerOperations is
In normal mode, ICR must be greater thatn MCR
Additionally, the new system TCR after the CDPs addition must be >CCR
*/
uint newTCR = _getNewTCRFromCdpChange(vars.netColl, true, vars.debt, true, vars.price);
if (isRecoveryMode) {
_requireICRisAboveCCR(vars.ICR);

// == Grace Period == //
// We are in RM, Edge case is Depositing Coll could exit RM
// We check with newTCR
if (newTCR < CCR) {
// Notify RM
cdpManager.notifyStartGracePeriod(newTCR);
} else {
// Notify Back to Normal Mode
cdpManager.notifyEndGracePeriod(newTCR);
}
} else {
_requireICRisAboveMCR(vars.ICR);
uint newTCR = _getNewTCRFromCdpChange(vars.netColl, true, vars.debt, true, vars.price); // bools: coll increase, debt increase
_requireNewTCRisAboveCCR(newTCR);

// == Grace Period == //
// We are not in RM, no edge case, we always stay above RM
// Always Notify Back to Normal Mode
cdpManager.notifyEndGracePeriod(newTCR);
}

// Set the cdp struct's properties
Expand Down Expand Up @@ -458,6 +474,10 @@ contract BorrowerOperations is
);
_requireNewTCRisAboveCCR(newTCR);

// == Grace Period == //
// By definition we are not in RM, notify CDPManager to ensure "Glass is on"
cdpManager.notifyEndGracePeriod(newTCR);

cdpManager.removeStake(_cdpId);

// We already verified msg.sender is the borrower
Expand Down Expand Up @@ -602,7 +622,7 @@ contract BorrowerOperations is
uint _collWithdrawal,
bool _isDebtIncrease,
LocalVariables_adjustCdp memory _vars
) internal view {
) internal {
/*
*In Recovery Mode, only allow:
*
Expand All @@ -617,23 +637,41 @@ contract BorrowerOperations is
* - The new ICR is above MCR
* - The adjustment won't pull the TCR below CCR
*/

_vars.newTCR = _getNewTCRFromCdpChange(
collateral.getPooledEthByShares(_vars.collChange),
_vars.isCollIncrease,
_vars.netDebtChange,
_isDebtIncrease,
_vars.price
);

if (_isRecoveryMode) {
_requireNoCollWithdrawal(_collWithdrawal);
if (_isDebtIncrease) {
_requireICRisAboveCCR(_vars.newICR);
_requireNewICRisAboveOldICR(_vars.newICR, _vars.oldICR);
}

// == Grace Period == //
// We are in RM, Edge case is Depositing Coll could exit RM
// We check with newTCR
if (_vars.newTCR < CCR) {
// Notify RM
cdpManager.notifyStartGracePeriod(_vars.newTCR);
} else {
// Notify Back to Normal Mode
cdpManager.notifyEndGracePeriod(_vars.newTCR);
}
} else {
// if Normal Mode
_requireICRisAboveMCR(_vars.newICR);
_vars.newTCR = _getNewTCRFromCdpChange(
collateral.getPooledEthByShares(_vars.collChange),
_vars.isCollIncrease,
_vars.netDebtChange,
_isDebtIncrease,
_vars.price
);
_requireNewTCRisAboveCCR(_vars.newTCR);

// == Grace Period == //
// We are not in RM, no edge case, we always stay above RM
// Always Notify Back to Normal Mode
cdpManager.notifyEndGracePeriod(_vars.newTCR);
}
}

Expand Down
Loading

0 comments on commit b4f4682

Please sign in to comment.