From 58488158cd69cdda3d2806ab68ff649ae5142409 Mon Sep 17 00:00:00 2001 From: The3D <frangellaemilio@gmail.com> Date: Fri, 4 Sep 2020 12:48:29 +0200 Subject: [PATCH] Refactored error messages --- contracts/libraries/helpers/Errors.sol | 12 +++++++++- contracts/libraries/logic/ReserveLogic.sol | 6 ++--- contracts/libraries/math/PercentageMath.sol | 13 +++++++---- contracts/libraries/math/WadRayMath.sol | 26 +++++++++++---------- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/contracts/libraries/helpers/Errors.sol b/contracts/libraries/helpers/Errors.sol index a01caa85..974aa5e1 100644 --- a/contracts/libraries/helpers/Errors.sol +++ b/contracts/libraries/helpers/Errors.sol @@ -37,6 +37,7 @@ library Errors { string public constant REQUESTED_AMOUNT_TOO_SMALL = '25'; // 'The requested amount is too small for a FlashLoan.' string public constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '26'; // 'The actual balance of the protocol is inconsistent' string public constant CALLER_NOT_LENDING_POOL_CONFIGURATOR = '27'; // 'The actual balance of the protocol is inconsistent' + string public constant INVALID_FLASHLOAN_MODE = '43'; //Invalid flashloan mode selected // require error messages - aToken string public constant CALLER_MUST_BE_LENDING_POOL = '28'; // 'The caller of this function must be a lending pool' @@ -48,6 +49,11 @@ library Errors { // require error messages - ReserveLogic string public constant RESERVE_ALREADY_INITIALIZED = '34'; // 'Reserve has already been initialized' + string public constant LIQUIDITY_INDEX_OVERFLOW = '47'; // Liquidity index overflows uint128 + string public constant VARIABLE_BORROW_INDEX_OVERFLOW = '48'; // Variable borrow index overflows uint128 + string public constant LIQUIDITY_RATE_OVERFLOW = '49'; // Liquidity rate overflows uint128 + string public constant VARIABLE_BORROW_RATE_OVERFLOW = '50'; // Variable borrow rate overflows uint128 + string public constant STABLE_BORROW_RATE_OVERFLOW = '51'; // Stable borrow rate overflows uint128 //require error messages - LendingPoolConfiguration string public constant CALLER_NOT_LENDING_POOL_MANAGER = '35'; // 'The caller must be a lending pool manager' @@ -62,5 +68,9 @@ library Errors { string public constant SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '40'; // 'User did not borrow the specified currency' string public constant NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '41'; // "There isn't enough liquidity available to liquidate" string public constant NO_ERRORS = '42'; // 'No errors' - string public constant INVALID_FLASHLOAN_MODE = '43'; //Invalid flashloan mode selected + + //require error messages - Math libraries + string public constant MULTIPLICATION_OVERFLOW = '44'; + string public constant ADDITION_OVERFLOW = '45'; + string public constant DIVISION_BY_ZERO = '46'; } diff --git a/contracts/libraries/logic/ReserveLogic.sol b/contracts/libraries/logic/ReserveLogic.sol index 0d92191b..607c8c63 100644 --- a/contracts/libraries/logic/ReserveLogic.sol +++ b/contracts/libraries/logic/ReserveLogic.sol @@ -133,7 +133,7 @@ library ReserveLogic { lastUpdateTimestamp ); uint256 index = cumulatedLiquidityInterest.rayMul(reserve.lastLiquidityIndex); - require(index < (1 << 128), "ReserveLogic: Liquidity index overflow"); + require(index < (1 << 128), Errors.LIQUIDITY_INDEX_OVERFLOW); reserve.lastLiquidityIndex = uint128(index); @@ -147,7 +147,7 @@ library ReserveLogic { index = cumulatedVariableBorrowInterest.rayMul( reserve.lastVariableBorrowIndex ); - require(index < (1 << 128), "ReserveLogic: Variable borrow index overflow"); + require(index < (1 << 128), Errors.VARIABLE_BORROW_INDEX_OVERFLOW); reserve.lastVariableBorrowIndex = uint128(index); } } @@ -175,7 +175,7 @@ library ReserveLogic { result = result.rayMul( reserve.lastLiquidityIndex ); - require(result < (1 << 128), "ReserveLogic: Liquidity index overflow"); + require(result < (1 << 128), Errors.LIQUIDITY_INDEX_OVERFLOW); reserve.lastLiquidityIndex = uint128(result); } diff --git a/contracts/libraries/math/PercentageMath.sol b/contracts/libraries/math/PercentageMath.sol index b7bbe05b..4d14107e 100644 --- a/contracts/libraries/math/PercentageMath.sol +++ b/contracts/libraries/math/PercentageMath.sol @@ -1,6 +1,9 @@ // SPDX-License-Identifier: agpl-3.0 pragma solidity ^0.6.8; + +import {Errors} from '../helpers/Errors.sol'; + /** * @title PercentageMath library * @author Aave @@ -27,11 +30,11 @@ library PercentageMath { uint256 result = value*percentage; - require(result/value == percentage, "PercentageMath: Multiplication overflow"); + require(result/value == percentage, Errors.MULTIPLICATION_OVERFLOW); result+=HALF_PERCENT; - require(result >= HALF_PERCENT, "PercentageMath: Addition overflow"); + require(result >= HALF_PERCENT, Errors.ADDITION_OVERFLOW); return result/PERCENTAGE_FACTOR; } @@ -43,16 +46,16 @@ library PercentageMath { * @return the value divided the percentage **/ function percentDiv(uint256 value, uint256 percentage) internal pure returns (uint256) { - require(percentage != 0, "PercentageMath: Division by 0"); + require(percentage != 0, Errors.DIVISION_BY_ZERO); uint256 halfPercentage = percentage / 2; uint256 result = value*PERCENTAGE_FACTOR; - require(result/PERCENTAGE_FACTOR == value, "PercentageMath: Multiplication overflow"); + require(result/PERCENTAGE_FACTOR == value, Errors.MULTIPLICATION_OVERFLOW); result += halfPercentage; - require(result >= halfPercentage, "PercentageMath: Addition overflow"); + require(result >= halfPercentage, Errors.ADDITION_OVERFLOW); return result/percentage; } diff --git a/contracts/libraries/math/WadRayMath.sol b/contracts/libraries/math/WadRayMath.sol index 58147191..1a1bdabd 100644 --- a/contracts/libraries/math/WadRayMath.sol +++ b/contracts/libraries/math/WadRayMath.sol @@ -1,6 +1,8 @@ // SPDX-License-Identifier: agpl-3.0 pragma solidity ^0.6.8; +import {Errors} from '../helpers/Errors.sol'; + /** * @title WadRayMath library * @author Aave @@ -60,11 +62,11 @@ library WadRayMath { uint256 result = a*b; - require(result/a == b, "WadRayMath: Multiplication overflow"); + require(result/a == b, Errors.MULTIPLICATION_OVERFLOW); result+=halfWAD; - require(result >= halfWAD, "WadRayMath: Addition overflow"); + require(result >= halfWAD, Errors.ADDITION_OVERFLOW); return result/WAD; } @@ -76,17 +78,17 @@ library WadRayMath { * @return the result of a/b, in wad **/ function wadDiv(uint256 a, uint256 b) internal pure returns (uint256) { - require(b != 0, "WadRayMath: Division by 0"); + require(b != 0, Errors.DIVISION_BY_ZERO); uint256 halfB = b / 2; uint256 result = a*WAD; - require(result/WAD == a, "WadRayMath: Multiplication overflow"); + require(result/WAD == a, Errors.MULTIPLICATION_OVERFLOW); result += halfB; - require(result >= halfB, "WadRayMath: Addition overflow"); + require(result >= halfB, Errors.ADDITION_OVERFLOW); return result/b; } @@ -104,11 +106,11 @@ library WadRayMath { uint256 result = a*b; - require(result/a == b, "WadRayMath: Multiplication overflow"); + require(result/a == b, Errors.MULTIPLICATION_OVERFLOW); result+=halfRAY; - require(result >= halfRAY, "WadRayMath: Addition overflow"); + require(result >= halfRAY, Errors.ADDITION_OVERFLOW); return result/RAY; } @@ -120,17 +122,17 @@ library WadRayMath { * @return the result of a/b, in ray **/ function rayDiv(uint256 a, uint256 b) internal pure returns (uint256) { - require(b != 0, "WadRayMath: Division by 0"); + require(b != 0, Errors.DIVISION_BY_ZERO); uint256 halfB = b / 2; uint256 result = a*RAY; - require(result/RAY == a, "WadRayMath: Multiplication overflow"); + require(result/RAY == a, Errors.MULTIPLICATION_OVERFLOW); result += halfB; - require(result >= halfB, "WadRayMath: Addition overflow"); + require(result >= halfB, Errors.ADDITION_OVERFLOW); return result/b; @@ -144,7 +146,7 @@ library WadRayMath { function rayToWad(uint256 a) internal pure returns (uint256) { uint256 halfRatio = WAD_RAY_RATIO / 2; uint256 result = halfRatio+a; - require(result >= halfRatio, "WadRayMath: Addition overflow"); + require(result >= halfRatio, Errors.ADDITION_OVERFLOW); return result/WAD_RAY_RATIO; } @@ -156,7 +158,7 @@ library WadRayMath { **/ function wadToRay(uint256 a) internal pure returns (uint256) { uint256 result = a*WAD_RAY_RATIO; - require(result/WAD_RAY_RATIO == a, "WadRayMath: Multiplication overflow"); + require(result/WAD_RAY_RATIO == a, Errors.MULTIPLICATION_OVERFLOW); return result; } }