Skip to content

Commit

Permalink
refactor(errors): aave + compound update
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinEgalite committed Nov 27, 2021
1 parent fe64e5c commit fe0d3e2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 41 deletions.
29 changes: 14 additions & 15 deletions contracts/aave/libraries/ErrorsForAave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ pragma solidity 0.8.7;
library Errors {
string public constant MM_MARKET_NOT_CREATED = "0";
string public constant MM_POSITIONS_MANAGER_SET = "1";
string public constant MM_NEW_NMAX_IS_0 = "2";
string public constant MM_MARKET_ALREADY_CREATED = "3";
string public constant MM_NEW_THRESHOLD_IS_0 = "4";
string public constant PM_MARKET_NOT_CREATED = "5";
string public constant PM_AMOUNT_NOT_ABOVE_THRESHOLD = "6";
string public constant PM_ONLY_MARKETS_MANAGER = "7";
string public constant PM_AMOUNT_IS_0 = "8";
string public constant PM_DEBT_VALUE_NOT_ABOVE_MAX = "9";
string public constant PM_AMOUNT_ABOVE_ALLOWED_TO_REPAY = "10";
string public constant PM_TO_SEIZE_ABOVE_COLLATERAL = "11";
string public constant PM_REMAINING_TO_UNMATCH_IS_NOT_0 = "12";
string public constant PM_DEBT_VALUE_ABOVE_MAX = "13";
string public constant PM_DELEGATECALL_BORROWER_UPDATE_NOT_SUCCESS = "14";
string public constant PM_DELEGATECALL_SUPPLIER_UPDATE_NOT_SUCCESS = "15";
string public constant PM_SUPPLY_ABOVE_CAP_VALUE = "16";
string public constant MM_MARKET_ALREADY_CREATED = "2";
string public constant MM_MARKET_CREATED_FAIL_ON_COMP = "3";
string public constant PM_MARKET_NOT_CREATED = "4";
string public constant PM_AMOUNT_NOT_ABOVE_THRESHOLD = "5";
string public constant PM_ONLY_MARKETS_MANAGER = "6";
string public constant PM_AMOUNT_IS_0 = "7";
string public constant PM_DEBT_VALUE_NOT_ABOVE_MAX = "8";
string public constant PM_AMOUNT_ABOVE_ALLOWED_TO_REPAY = "9";
string public constant PM_TO_SEIZE_ABOVE_COLLATERAL = "10";
string public constant PM_REMAINING_TO_UNMATCH_IS_NOT_0 = "11";
string public constant PM_DEBT_VALUE_ABOVE_MAX = "12";
string public constant PM_DELEGATECALL_BORROWER_UPDATE_NOT_SUCCESS = "13";
string public constant PM_DELEGATECALL_SUPPLIER_UPDATE_NOT_SUCCESS = "14";
string public constant PM_SUPPLY_ABOVE_CAP_VALUE = "15";
}
14 changes: 8 additions & 6 deletions contracts/compound/MarketsManagerForCompound.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "prb-math/contracts/PRBMathUD60x18.sol";
import "./libraries/CompoundMath.sol";
import "./libraries/ErrorsForCompound.sol";

import {ICErc20, IComptroller} from "./interfaces/compound/ICompound.sol";
import "./interfaces/IPositionsManagerForCompound.sol";
Expand Down Expand Up @@ -66,7 +67,7 @@ contract MarketsManagerForCompound is Ownable {
/** @dev Prevents to update a market not created yet.
*/
modifier isMarketCreated(address _marketAddress) {
require(isCreated[_marketAddress], "0");
require(isCreated[_marketAddress], Errors.MM_MARKET_NOT_CREATED);
_;
}

Expand All @@ -76,7 +77,10 @@ contract MarketsManagerForCompound is Ownable {
* @param _positionsManagerForCompound The address of compound module.
*/
function setPositionsManager(address _positionsManagerForCompound) external onlyOwner {
require(address(positionsManagerForCompound) == address(0), "1");
require(
address(positionsManagerForCompound) == address(0),
Errors.MM_POSITIONS_MANAGER_SET
);
positionsManagerForCompound = IPositionsManagerForCompound(_positionsManagerForCompound);
emit PositionsManagerForCompoundSet(_positionsManagerForCompound);
}
Expand All @@ -85,7 +89,6 @@ contract MarketsManagerForCompound is Ownable {
* @param _newMaxNumber The maximum number of users to have in the tree.
*/
function setMaxNumberOfUsersInTree(uint16 _newMaxNumber) external onlyOwner {
require(_newMaxNumber > 1, "2");
positionsManagerForCompound.setMaxNumberOfUsersInTree(_newMaxNumber);
emit MaxNumberUpdated(_newMaxNumber);
}
Expand All @@ -95,9 +98,9 @@ contract MarketsManagerForCompound is Ownable {
* @param _threshold The threshold to set for the market.
*/
function createMarket(address _marketAddress, uint256 _threshold) external onlyOwner {
require(!isCreated[_marketAddress], "3");
require(!isCreated[_marketAddress], Errors.MM_MARKET_ALREADY_CREATED);
uint256[] memory results = positionsManagerForCompound.createMarket(_marketAddress);
require(results[0] == 0, "4");
require(results[0] == 0, Errors.MM_MARKET_CREATED_FAIL_ON_COMP);
positionsManagerForCompound.setThreshold(_marketAddress, _threshold);
lastUpdateBlockNumber[_marketAddress] = block.number;
p2pUnitExchangeRate[_marketAddress] = 1e18;
Expand All @@ -115,7 +118,6 @@ contract MarketsManagerForCompound is Ownable {
onlyOwner
isMarketCreated(_marketAddress)
{
require(_newThreshold > 0, "5");
positionsManagerForCompound.setThreshold(_marketAddress, _newThreshold);
emit ThresholdUpdated(_marketAddress, _newThreshold);
}
Expand Down
47 changes: 27 additions & 20 deletions contracts/compound/PositionsManagerForCompound.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";

import "./libraries/CompoundMath.sol";
import "./libraries/ErrorsForCompound.sol";
import {ICErc20, IComptroller, ICompoundOracle} from "./interfaces/compound/ICompound.sol";
import "./interfaces/IMarketsManagerForCompound.sol";
import "./interfaces/IUpdatePositions.sol";
Expand Down Expand Up @@ -142,7 +143,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
* @param _cTokenAddress The address of the market.
*/
modifier isMarketCreated(address _cTokenAddress) {
require(marketsManagerForCompound.isCreated(_cTokenAddress), "0");
require(marketsManagerForCompound.isCreated(_cTokenAddress), Errors.PM_MARKET_NOT_CREATED);
_;
}

Expand All @@ -151,14 +152,14 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
* @param _amount The amount in ERC20 tokens.
*/
modifier isAboveThreshold(address _cTokenAddress, uint256 _amount) {
require(_amount >= threshold[_cTokenAddress], "1");
require(_amount >= threshold[_cTokenAddress], Errors.PM_AMOUNT_NOT_ABOVE_THRESHOLD);
_;
}

/** @dev Prevents a user to call function only allowed for the markets manager.
*/
modifier onlyMarketsManager() {
require(msg.sender == address(marketsManagerForCompound), "2");
require(msg.sender == address(marketsManagerForCompound), Errors.PM_ONLY_MARKETS_MANAGER);
_;
}

Expand Down Expand Up @@ -333,7 +334,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage

/* If there aren't enough suppliers waiting on Comp to match all the tokens borrowed, the rest is borrowed from Comp */
if (remainingToBorrowOnPool > 0) {
require(cToken.borrow(remainingToBorrowOnPool) == 0, "3");
require(cToken.borrow(remainingToBorrowOnPool) == 0, Errors.PM_BORROW_ON_COMP_FAIL);
uint256 borrowIndex = cToken.borrowIndex();
borrowBalanceInOf[_cTokenAddress][msg.sender].onPool += remainingToBorrowOnPool.div(
borrowIndex
Expand Down Expand Up @@ -383,14 +384,14 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
address _borrower,
uint256 _amount
) external nonReentrant {
require(_amount > 0, "4");
require(_amount > 0, Errors.PM_AMOUNT_IS_0);
(uint256 debtValue, uint256 maxDebtValue, ) = _getUserHypotheticalBalanceStates(
_borrower,
address(0),
0,
0
);
require(debtValue > maxDebtValue, "5");
require(debtValue > maxDebtValue, Errors.PM_DEBT_VALUE_NOT_ABOVE_MAX);
LiquidateVars memory vars;
vars.borrowBalance =
borrowBalanceInOf[_cTokenBorrowedAddress][_borrower].onPool.mul(
Expand All @@ -399,15 +400,21 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
borrowBalanceInOf[_cTokenBorrowedAddress][_borrower].inP2P.mul(
marketsManagerForCompound.p2pUnitExchangeRate(_cTokenBorrowedAddress)
);
require(_amount <= vars.borrowBalance.mul(comptroller.closeFactorMantissa()), "6");
require(
_amount <= vars.borrowBalance.mul(comptroller.closeFactorMantissa()),
Errors.PM_AMOUNT_ABOVE_ALLOWED_TO_REPAY
);

_repay(_cTokenBorrowedAddress, _borrower, _amount);

// Calculate the amount of token to seize from collateral
ICompoundOracle compoundOracle = ICompoundOracle(comptroller.oracle());
vars.priceCollateralMantissa = compoundOracle.getUnderlyingPrice(_cTokenCollateralAddress);
vars.priceBorrowedMantissa = compoundOracle.getUnderlyingPrice(_cTokenBorrowedAddress);
require(vars.priceCollateralMantissa != 0 && vars.priceBorrowedMantissa != 0, "7");
require(
vars.priceCollateralMantissa != 0 && vars.priceBorrowedMantissa != 0,
Errors.PM_TO_SEIZE_ABOVE_COLLATERAL
);

/*
* Get the exchange rate and calculate the number of collateral tokens to seize:
Expand All @@ -430,7 +437,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
marketsManagerForCompound.updateP2pUnitExchangeRate(_cTokenCollateralAddress)
);

require(vars.amountToSeize <= totalCollateral, "8");
require(vars.amountToSeize <= totalCollateral, Errors.PM_TO_SEIZE_ABOVE_COLLATERAL);
emit Liquidated(
msg.sender,
_borrower,
Expand All @@ -456,7 +463,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
address _holder,
address _receiver
) internal isMarketCreated(_cTokenAddress) {
require(_amount > 0, "9");
require(_amount > 0, Errors.PM_AMOUNT_IS_0);
_checkAccountLiquidity(_holder, _cTokenAddress, _amount, 0);
emit Withdrawn(_holder, _cTokenAddress, _amount);
ICErc20 cToken = ICErc20(_cTokenAddress);
Expand Down Expand Up @@ -553,7 +560,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
remainingToWithdraw -= remaining;
require(
_unmatchBorrowers(_cTokenAddress, remainingToWithdraw) == 0, // We break some P2P credit lines the user had with borrowers and fallback on Comp.
"12"
Errors.PM_REMAINING_TO_UNMATCH_IS_NOT_0
);
}
}
Expand All @@ -573,7 +580,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
address _borrower,
uint256 _amount
) internal isMarketCreated(_cTokenAddress) {
require(_amount > 0, "13");
require(_amount > 0, Errors.PM_AMOUNT_IS_0);
ICErc20 cToken = ICErc20(_cTokenAddress);
IERC20 underlyingToken = IERC20(cToken.underlying());
underlyingToken.safeTransferFrom(msg.sender, address(this), _amount);
Expand Down Expand Up @@ -664,7 +671,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
remainingToRepay -= contractBorrowBalanceOnPool;
require(
_unmatchSuppliers(_cTokenAddress, remainingToRepay) == 0, // We break some P2P credit lines the user had with suppliers and fallback on Comp.
"14"
Errors.PM_REMAINING_TO_UNMATCH_IS_NOT_0
);
}
}
Expand All @@ -684,7 +691,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
ICErc20 cToken = ICErc20(_cTokenAddress);
IERC20 underlyingToken = IERC20(cToken.underlying());
underlyingToken.safeApprove(_cTokenAddress, _amount);
require(cToken.mint(_amount) == 0, "15");
require(cToken.mint(_amount) == 0, Errors.PM_MINT_ON_COMP_FAIL);
}

/** @dev Withdraws ERC20 tokens from Comp.
Expand All @@ -696,7 +703,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
isAboveCompoundThreshold(_cTokenAddress, _amount)
{
ICErc20 cToken = ICErc20(_cTokenAddress);
require(cToken.redeemUnderlying(_amount) == 0, "16");
require(cToken.redeemUnderlying(_amount) == 0, Errors.PM_REDEEM_ON_COMP_FAIL);
}

/** @dev Finds liquidity on Comp and matches it in P2P.
Expand Down Expand Up @@ -843,7 +850,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
cToken.borrowBalanceCurrent(address(this))
);
underlyingToken.safeApprove(_cTokenAddress, toRepay);
require(cToken.repayBorrow(toRepay) == 0, "17");
require(cToken.repayBorrow(toRepay) == 0, Errors.PM_REPAY_ON_COMP_FAIL);
}

/** @dev Finds borrowers in peer-to-peer that match the given `_amount` and move them to Comp.
Expand Down Expand Up @@ -915,7 +922,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
_withdrawnAmount,
_borrowedAmount
);
require(debtValue <= maxDebtValue, "18");
require(debtValue <= maxDebtValue, Errors.PM_DEBT_VALUE_ABOVE_MAX);
}

/** @dev Returns the debt value, max debt value and collateral value of a given user.
Expand Down Expand Up @@ -961,7 +968,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
supplyBalanceInOf[vars.cTokenEntered][_account].inP2P.mul(vars.p2pExchangeRate);
// Price recovery
vars.underlyingPrice = compoundOracle.getUnderlyingPrice(vars.cTokenEntered);
require(vars.underlyingPrice != 0, "19");
require(vars.underlyingPrice != 0, Errors.PM_ORACLE_FAIL);

if (_cTokenAddress == vars.cTokenEntered) {
vars.debtToAdd += _borrowedAmount;
Expand Down Expand Up @@ -991,7 +998,7 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
(bool success, ) = address(updatePositions).delegatecall(
abi.encodeWithSignature("updateBorrowerList(address,address)", _cTokenAddress, _account)
);
require(success, "20");
require(success, Errors.PM_DELEGATECALL_BORROWER_UPDATE_NOT_SUCCESS);
}

/** @dev Updates suppliers tree with the new balances of a given account.
Expand All @@ -1002,6 +1009,6 @@ contract PositionsManagerForCompound is ReentrancyGuard, PositionsManagerStorage
(bool success, ) = address(updatePositions).delegatecall(
abi.encodeWithSignature("updateSupplierList(address,address)", _cTokenAddress, _account)
);
require(success, "21");
require(success, Errors.PM_DELEGATECALL_SUPPLIER_UPDATE_NOT_SUCCESS);
}
}
25 changes: 25 additions & 0 deletions contracts/compound/libraries/ErrorsForCompound.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity 0.8.7;

library Errors {
string public constant MM_MARKET_NOT_CREATED = "0";
string public constant MM_POSITIONS_MANAGER_SET = "1";
string public constant MM_MARKET_ALREADY_CREATED = "2";
string public constant MM_MARKET_CREATED_FAIL_ON_COMP = "3";
string public constant PM_MARKET_NOT_CREATED = "4";
string public constant PM_AMOUNT_NOT_ABOVE_THRESHOLD = "5";
string public constant PM_ONLY_MARKETS_MANAGER = "6";
string public constant PM_AMOUNT_IS_0 = "7";
string public constant PM_DEBT_VALUE_NOT_ABOVE_MAX = "8";
string public constant PM_AMOUNT_ABOVE_ALLOWED_TO_REPAY = "9";
string public constant PM_TO_SEIZE_ABOVE_COLLATERAL = "10";
string public constant PM_REMAINING_TO_UNMATCH_IS_NOT_0 = "11";
string public constant PM_DEBT_VALUE_ABOVE_MAX = "12";
string public constant PM_DELEGATECALL_BORROWER_UPDATE_NOT_SUCCESS = "13";
string public constant PM_DELEGATECALL_SUPPLIER_UPDATE_NOT_SUCCESS = "14";
string public constant PM_BORROW_ON_COMP_FAIL = "15";
string public constant PM_MINT_ON_COMP_FAIL = "16";
string public constant PM_REDEEM_ON_COMP_FAIL = "17";
string public constant PM_REPAY_ON_COMP_FAIL = "18";
string public constant PM_ORACLE_FAIL = "19";
}

0 comments on commit fe0d3e2

Please sign in to comment.