Skip to content

Commit

Permalink
fix: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmikko committed May 9, 2023
1 parent 0c5760f commit 85c4f29
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 118 deletions.
9 changes: 5 additions & 4 deletions contracts/credit/CreditConfiguratorV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ contract CreditConfigurator is ICreditConfigurator, ACLNonReentrantTrait {
(, uint128 maxCumulativeLossCurrent) = creditFacade().lossParams();

if (_maxCumulativeLoss != maxCumulativeLossCurrent) {
creditFacade().setMaxCumulativeLoss(_maxCumulativeLoss);
creditFacade().setCumulativeLossParams(_maxCumulativeLoss, false);
emit SetMaxCumulativeLoss(_maxCumulativeLoss);
}
}
Expand All @@ -690,7 +690,8 @@ contract CreditConfigurator is ICreditConfigurator, ACLNonReentrantTrait {
external
configuratorOnly // F: [CC-02]
{
creditFacade().resetCumulativeLoss();
(, uint128 maxCumulativeLossCurrent) = creditFacade().lossParams();
creditFacade().setCumulativeLossParams(maxCumulativeLossCurrent, true);
emit ResetCumulativeLoss();
}

Expand Down Expand Up @@ -795,7 +796,7 @@ contract CreditConfigurator is ICreditConfigurator, ACLNonReentrantTrait {
// Checks that the address is not already in the list,
// to avoid redundant events
if (!statusCurrent) {
creditFacade().addEmergencyLiquidator(liquidator); // F: [CC-38]
creditFacade().setEmergencyLiquidator(liquidator, AllowanceAction.ALLOW); // F: [CC-38]
emit AddEmergencyLiquidator(liquidator); // F: [CC-38]
}
}
Expand All @@ -816,7 +817,7 @@ contract CreditConfigurator is ICreditConfigurator, ACLNonReentrantTrait {
// Checks that the address is in the list
// to avoid redundant events
if (statusCurrent) {
creditFacade().removeEmergencyLiquidator(liquidator); // F: [CC-38]
creditFacade().setEmergencyLiquidator(liquidator, AllowanceAction.FORBID); // F: [CC-38]
emit RemoveEmergencyLiquidator(liquidator); // F: [CC-38]
}
}
Expand Down
103 changes: 45 additions & 58 deletions contracts/credit/CreditFacadeV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
_isAccountLiquidatable({creditAccount: creditAccount, isEmergency: paused()}); // F:[FA-14]

if (!collateralDebtData.isLiquidatable) revert CreditAccountNotLiquidatableException();

collateralDebtData.enabledTokensMask |=
_claimWithdrawals({action: claimAction, creditAccount: creditAccount, to: borrower});
}
Expand Down Expand Up @@ -521,7 +522,7 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
unchecked {
for (uint256 i = 0; i < len; ++i) {
MultiCall calldata mcall = calls[i]; // F:[FA-26]
//
//xw
// CREDIT FACADE
//
if (mcall.target == address(this)) {
Expand All @@ -530,9 +531,6 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {

bytes4 method = bytes4(mcall.callData);

/// TODO: Check not to copy and use [4:] directly
bytes memory callData = mcall.callData[4:];

//
// REVERT_IF_RECEIVED_LESS_THAN
//
Expand All @@ -546,21 +544,21 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
} // F:[FA-45A]

// Sets expected balances to currentBalance + delta
expectedBalances = CreditLogic.storeBalances(creditAccount, callData); // F:[FA-45]
expectedBalances = CreditLogic.storeBalances(creditAccount, mcall.callData[4:]); // F:[FA-45]
}
//
// SET FULL CHECK PARAMS
//
else if (method == ICreditFacadeMulticall.setFullCheckParams.selector) {
(fullCheckParams.collateralHints, fullCheckParams.minHealthFactor) =
abi.decode(callData, (uint256[], uint16));
abi.decode(mcall.callData[4:], (uint256[], uint16));
}
//
// ADD COLLATERAL
//
else if (method == ICreditFacadeMulticall.addCollateral.selector) {
_revertIfNoPermission(flags, ADD_COLLATERAL_PERMISSION);
enabledTokensMask |= _addCollateral(creditAccount, callData) & quotedTokenMaskInverted; // F:[FA-26, 27]
enabledTokensMask |= _addCollateral(creditAccount, mcall.callData[4:]) & quotedTokenMaskInverted; // F:[FA-26, 27]
}
//
// INCREASE DEBT
Expand All @@ -572,8 +570,9 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
// as that could be used to get free flash loans
flags &= ~DECREASE_DEBT_PERMISSION; // F:[FA-28]
flags |= INCREASE_DEBT_WAS_CALLED;
enabledTokensMask =
_manageDebt(creditAccount, callData, enabledTokensMask, ManageDebtAction.INCREASE_DEBT); // F:[FA-26]
enabledTokensMask = _manageDebt(
creditAccount, mcall.callData[4:], enabledTokensMask, ManageDebtAction.INCREASE_DEBT
); // F:[FA-26]
}
//
// DECREASE DEBT
Expand All @@ -583,16 +582,17 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
_revertIfNoPermission(flags, DECREASE_DEBT_PERMISSION);
// F:[FA-28]

enabledTokensMask =
_manageDebt(creditAccount, callData, enabledTokensMask, ManageDebtAction.DECREASE_DEBT); // F:[FA-27]
enabledTokensMask = _manageDebt(
creditAccount, mcall.callData[4:], enabledTokensMask, ManageDebtAction.DECREASE_DEBT
); // F:[FA-27]
}
//
// ENABLE TOKEN
//
else if (method == ICreditFacadeMulticall.enableToken.selector) {
_revertIfNoPermission(flags, ENABLE_TOKEN_PERMISSION);
// Parses token
address token = abi.decode(callData, (address)); // F: [FA-53]
address token = abi.decode(mcall.callData[4:], (address)); // F: [FA-53]
enabledTokensMask |= _getTokenMaskOrRevert(token) & quotedTokenMaskInverted;
}
//
Expand All @@ -601,23 +601,23 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
else if (method == ICreditFacadeMulticall.disableToken.selector) {
_revertIfNoPermission(flags, DISABLE_TOKEN_PERMISSION);
// Parses token
address token = abi.decode(callData, (address)); // F: [FA-53]
address token = abi.decode(mcall.callData[4:], (address)); // F: [FA-53]
/// IGNORE QUOTED TOKEN MASK
enabledTokensMask &= ~(_getTokenMaskOrRevert(token) & quotedTokenMaskInverted);
}
//
// UPDATE QUOTA
//
else if (method == ICreditFacadeMulticall.updateQuota.selector) {
_revertIfNoPermission(flags, UPDATE_QUOTAS_PERMISSION);
enabledTokensMask = _updateQuota(creditAccount, callData, enabledTokensMask);
_revertIfNoPermission(flags, UPDATE_QUOTA_PERMISSION);
enabledTokensMask = _updateQuota(creditAccount, mcall.callData[4:], enabledTokensMask);
}
//
// WITHDRAW
//
else if (method == ICreditFacadeMulticall.scheduleWithdrawal.selector) {
_revertIfNoPermission(flags, WITHDRAW_PERMISSION);
uint256 tokensToDisable = _scheduleWithdrawal(creditAccount, callData);
uint256 tokensToDisable = _scheduleWithdrawal(creditAccount, mcall.callData[4:]);
/// IGNORE QUOTED TOKEN MASK
enabledTokensMask = enabledTokensMask & (~(tokensToDisable & quotedTokenMaskInverted));
}
Expand All @@ -626,7 +626,7 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
//
else if (method == ICreditFacadeMulticall.revokeAdapterAllowances.selector) {
_revertIfNoPermission(flags, REVOKE_ALLOWANCES_PERMISSION);
_revokeAdapterAllowances(creditAccount, callData);
_revokeAdapterAllowances(creditAccount, mcall.callData[4:]);
}
//
// UNKNOWN METHOD
Expand All @@ -644,10 +644,9 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
// functionCall to it is strictly forbidden, even if
// the Configurator adds it as an adapter

if (
creditManager.adapterToContract(mcall.target) == address(0)
|| mcall.target == address(creditManager)
) revert TargetContractNotAllowedException(); // F:[FA-24]
if (creditManager.adapterToContract(mcall.target) == address(0)) {
revert TargetContractNotAllowedException();
} // F:[FA-24]

if (flags & EXTERNAL_CONTRACT_WAS_CALLED == 0) {
flags |= EXTERNAL_CONTRACT_WAS_CALLED;
Expand Down Expand Up @@ -720,7 +719,7 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
}
}

function _updateQuota(address creditAccount, bytes memory callData, uint256 enabledTokensMask)
function _updateQuota(address creditAccount, bytes calldata callData, uint256 enabledTokensMask)
internal
returns (uint256)
{
Expand All @@ -729,7 +728,7 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
return (enabledTokensMask | tokensToEnable) & (~tokensToDisable);
}

function _revokeAdapterAllowances(address creditAccount, bytes memory callData) internal {
function _revokeAdapterAllowances(address creditAccount, bytes calldata callData) internal {
(RevocationPair[] memory revocations) = abi.decode(callData, (RevocationPair[]));
creditManager.revokeAdapterAllowances(creditAccount, revocations);
}
Expand All @@ -741,7 +740,7 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
/// @param callData Bytes calldata for parsing
function _manageDebt(
address creditAccount,
bytes memory callData,
bytes calldata callData,
uint256 _enabledTokensMask,
ManageDebtAction action
) internal returns (uint256 enabledTokensMask) {
Expand All @@ -767,7 +766,7 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
}
}

function _addCollateral(address creditAccount, bytes memory callData) internal returns (uint256 tokenMaskAfter) {
function _addCollateral(address creditAccount, bytes calldata callData) internal returns (uint256 tokenMaskAfter) {
(address token, uint256 amount) = abi.decode(callData, (address, uint256)); // F:[FA-26, 27]
// Requests Credit Manager to transfer collateral to the Credit Account
tokenMaskAfter = creditManager.addCollateral(msg.sender, creditAccount, token, amount); // F:[FA-21]
Expand All @@ -776,6 +775,14 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
emit AddCollateral(creditAccount, token, amount); // F:[FA-21]
}

function _scheduleWithdrawal(address creditAccount, bytes calldata callData)
internal
returns (uint256 tokensToDisable)
{
(address token, uint256 amount) = abi.decode(callData, (address, uint256));
tokensToDisable = creditManager.scheduleWithdrawal(creditAccount, token, amount);
}

/// @dev Transfers credit account to another user
/// By default, this action is forbidden, and the user has to approve transfers from sender to itself
/// by calling approveAccountTransfer.
Expand Down Expand Up @@ -861,16 +868,16 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {

/// @dev Approves account transfer from another user to msg.sender
/// @param from Address for which account transfers are allowed/forbidden
/// @param state True is transfer is allowed, false if forbidden
function approveAccountTransfer(address from, bool state) external override nonReentrant {
/// @param allowTransfer True is transfer is allowed, false if forbidden
function approveAccountTransfer(address from, bool allowTransfer) external override nonReentrant {
// In whitelisted mode only select addresses can have Credit Accounts
// So this action is prohibited
if (whitelisted) revert AccountTransferNotAllowedException(); // F:[FA-32]

transfersAllowed[from][msg.sender] = state; // F:[FA-38]
transfersAllowed[from][msg.sender] = allowTransfer; // F:[FA-38]

// Emits event
emit AllowAccountTransfer(from, msg.sender, state); // F:[FA-38]
emit SetAccountTransferAllowance(from, msg.sender, allowTransfer); // F:[FA-38]
}

//
Expand Down Expand Up @@ -985,18 +992,6 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
return creditManager.getTokenByMask(mask);
}

function _enabledTokensMask(address creditAccount) internal view returns (uint256) {
return creditManager.enabledTokensMaskOf(creditAccount);
}

function _scheduleWithdrawal(address creditAccount, bytes memory callData)
internal
returns (uint256 tokensToDisable)
{
(address token, uint256 amount) = abi.decode(callData, (address, uint256));
tokensToDisable = creditManager.scheduleWithdrawal(creditAccount, token, amount);
}

function _claimWithdrawals(address creditAccount, address to, ClaimAction action)
internal
returns (uint256 tokensToEnable)
Expand Down Expand Up @@ -1044,13 +1039,14 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {
}

/// @dev Sets the max cumulative loss that can be accrued before pausing the Credit Manager
function setMaxCumulativeLoss(uint128 _maxCumulativeLoss) external creditConfiguratorOnly {
function setCumulativeLossParams(uint128 _maxCumulativeLoss, bool resetCumulativeLoss)
external
creditConfiguratorOnly
{
lossParams.maxCumulativeLoss = _maxCumulativeLoss;
}

/// @dev Resets the current cumulative loss value
function resetCumulativeLoss() external creditConfiguratorOnly {
lossParams.currentCumulativeLoss = 0;
if (resetCumulativeLoss) {
lossParams.currentCumulativeLoss = 0;
}
}

/// @dev Adds forbidden token
Expand All @@ -1068,19 +1064,10 @@ contract CreditFacadeV3 is ICreditFacade, ACLNonReentrantTrait {

/// @dev Adds an address to the list of emergency liquidators
/// @param liquidator Address to add to the list
function addEmergencyLiquidator(address liquidator)
function setEmergencyLiquidator(address liquidator, AllowanceAction allowanceAction)
external
creditConfiguratorOnly // F:[CM-4]
{
canLiquidateWhilePaused[liquidator] = true;
}

/// @dev Removes an address from the list of emergency liquidators
/// @param liquidator Address to remove from the list
function removeEmergencyLiquidator(address liquidator)
external
creditConfiguratorOnly // F: [CM-4]
{
canLiquidateWhilePaused[liquidator] = false;
canLiquidateWhilePaused[liquidator] = allowanceAction == AllowanceAction.ALLOW;
}
}
Loading

0 comments on commit 85c4f29

Please sign in to comment.