Updated require message errors with constant string numbers to reduce gas

This commit is contained in:
pol 2020-08-25 15:32:22 +02:00
parent cd09d04d30
commit abe967c707

View File

@ -44,6 +44,14 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25; uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25;
uint256 public constant FLASHLOAN_FEE_TOTAL = 9; uint256 public constant FLASHLOAN_FEE_TOTAL = 9;
//require error messages
string private constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '1'; // 'User does not have any stable rate loan for this reserve'
string private constant INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '2'; // 'Interest rate rebalance conditions were not met'
string private constant LIQUIDATION_CALL_FAILED = '3'; // 'Liquidation call failed'
string private constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '4'; // 'There is not enough liquidity available to borrow'
string private constant REQUESTED_AMOUNT_TO_SMALL = '5'; // 'The requested amount is too small for a FlashLoan.'
string private constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '6'; // 'The actual balance of the protocol is inconsistent'
ILendingPoolAddressesProvider internal _addressesProvider; ILendingPoolAddressesProvider internal _addressesProvider;
mapping(address => ReserveLogic.ReserveData) internal _reserves; mapping(address => ReserveLogic.ReserveData) internal _reserves;
@ -195,8 +203,14 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
reserve.updateCumulativeIndexesAndTimestamp(); reserve.updateCumulativeIndexesAndTimestamp();
if (ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE) { if (
IStableDebtToken(reserve.stableDebtTokenAddress).mint(msg.sender, amount, userStableRate); ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE
) {
IStableDebtToken(reserve.stableDebtTokenAddress).mint(
msg.sender,
amount,
userStableRate
);
} else { } else {
IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, amount); IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, amount);
} }
@ -240,7 +254,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
) external override nonReentrant { ) external override nonReentrant {
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(onBehalfOf, reserve); (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(
onBehalfOf,
reserve
);
ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode);
@ -291,7 +308,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
function swapBorrowRateMode(address asset, uint256 rateMode) external override nonReentrant { function swapBorrowRateMode(address asset, uint256 rateMode) external override nonReentrant {
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve); (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(
msg.sender,
reserve
);
ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode);
@ -344,7 +364,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user); uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user);
// user must be borrowing on asset at a stable rate // user must be borrowing on asset at a stable rate
require(stableBorrowBalance > 0, 'User does not have any stable rate loan for this reserve'); require(stableBorrowBalance > 0, NOT_ENOUGH_STABLE_BORROW_BALANCE);
uint256 rebalanceDownRateThreshold = reserve.currentStableBorrowRate.rayMul( uint256 rebalanceDownRateThreshold = reserve.currentStableBorrowRate.rayMul(
WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA) WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA)
@ -358,8 +378,9 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
uint256 userStableRate = stableDebtToken.getUserStableRate(user); uint256 userStableRate = stableDebtToken.getUserStableRate(user);
require( require(
userStableRate < reserve.currentLiquidityRate || userStableRate > rebalanceDownRateThreshold, userStableRate < reserve.currentLiquidityRate ||
'Interest rate rebalance conditions were not met' userStableRate > rebalanceDownRateThreshold,
INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET
); );
//burn old debt tokens, mint new ones //burn old debt tokens, mint new ones
@ -435,7 +456,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
receiveAToken receiveAToken
) )
); );
require(success, 'Liquidation call failed'); require(success, LIQUIDATION_CALL_FAILED);
(uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string)); (uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string));
@ -469,11 +490,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
//calculate amount fee //calculate amount fee
uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000); uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000);
require( require(availableLiquidityBefore >= amount, NOT_ENOUGH_LIQUIDITY_TO_BORROW);
availableLiquidityBefore >= amount, require(amountFee > 0, REQUESTED_AMOUNT_TO_SMALL);
'There is not enough liquidity available to borrow'
);
require(amountFee > 0, 'The requested amount is too small for a FlashLoan.');
//get the FlashLoanReceiver instance //get the FlashLoanReceiver instance
IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress); IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress);
@ -489,7 +507,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
require( require(
availableLiquidityAfter == availableLiquidityBefore.add(amountFee), availableLiquidityAfter == availableLiquidityBefore.add(amountFee),
'The actual balance of the protocol is inconsistent' INCONSISTENT_PROTOCOL_ACTUAL_BALANCE
); );
//compounding the cumulated interest //compounding the cumulated interest
@ -659,7 +677,9 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool {
user user
); );
usageAsCollateralEnabled = _usersConfig[user].isUsingAsCollateral(reserve.index); usageAsCollateralEnabled = _usersConfig[user].isUsingAsCollateral(reserve.index);
variableBorrowIndex = IVariableDebtToken(reserve.variableDebtTokenAddress).getUserIndex(user); variableBorrowIndex = IVariableDebtToken(reserve.variableDebtTokenAddress).getUserIndex(
user
);
} }
function getReserves() external override view returns (address[] memory) { function getReserves() external override view returns (address[] memory) {