From 6e0091a66886d878dc7580bcccdf76ae5354a8d0 Mon Sep 17 00:00:00 2001 From: David Racero Date: Mon, 14 Sep 2020 15:24:30 +0200 Subject: [PATCH] Added PausablePool contract to support errors by number. --- contracts/lendingpool/LendingPool.sol | 8 +-- contracts/libraries/helpers/Errors.sol | 8 ++- contracts/misc/PausablePool.sol | 88 ++++++++++++++++++++++++++ helpers/types.ts | 3 +- 4 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 contracts/misc/PausablePool.sol diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 668e534b..48502b31 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -4,7 +4,6 @@ pragma experimental ABIEncoderV2; import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; -import {Pausable} from '@openzeppelin/contracts/utils/Pausable.sol'; import { VersionedInitializable } from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; @@ -25,6 +24,7 @@ import {LendingPoolLiquidationManager} from './LendingPoolLiquidationManager.sol import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import {ILendingPool} from '../interfaces/ILendingPool.sol'; +import {PausablePool} from '../misc/PausablePool.sol'; /** * @title LendingPool contract @@ -32,7 +32,7 @@ import {ILendingPool} from '../interfaces/ILendingPool.sol'; * @author Aave **/ -contract LendingPool is VersionedInitializable, Pausable, ILendingPool { +contract LendingPool is VersionedInitializable, PausablePool, ILendingPool { using SafeMath for uint256; using WadRayMath for uint256; using ReserveLogic for ReserveLogic.ReserveData; @@ -929,9 +929,9 @@ contract LendingPool is VersionedInitializable, Pausable, ILendingPool { } /** - * @dev Returns true if the contract is paused, and false otherwise. + * @dev retrieve pause status */ function isPaused() public override view returns (bool) { - return Pausable.paused(); + return PausablePool.paused(); } } diff --git a/contracts/libraries/helpers/Errors.sol b/contracts/libraries/helpers/Errors.sol index ee9feac9..42497bb7 100644 --- a/contracts/libraries/helpers/Errors.sol +++ b/contracts/libraries/helpers/Errors.sol @@ -46,8 +46,8 @@ library Errors { string public constant CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30'; // 'User cannot give allowance to himself' string public constant TRANSFER_AMOUNT_NOT_GT_0 = '31'; // 'Transferred amount needs to be greater than zero' string public constant INVALID_ATOKEN_BALANCE = '52'; // balance on burning is invalid - - // require error messages - ReserveLogic + + // 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 @@ -73,4 +73,8 @@ library Errors { string public constant MULTIPLICATION_OVERFLOW = '44'; string public constant ADDITION_OVERFLOW = '45'; string public constant DIVISION_BY_ZERO = '46'; + + // pausable error message + string public constant IS_PAUSED = '54'; // 'Pool is paused' + string public constant IS_ACTIVE = '55'; // 'Pool is active' } diff --git a/contracts/misc/PausablePool.sol b/contracts/misc/PausablePool.sol new file mode 100644 index 00000000..d3be59b3 --- /dev/null +++ b/contracts/misc/PausablePool.sol @@ -0,0 +1,88 @@ +pragma solidity ^0.6.0; + +import {Context} from './Context.sol'; + +/** + * @dev Contract module which allows children to implement an emergency stop + * mechanism that can be triggered by an authorized account. + * + * This module is used through inheritance. It will make available the + * modifiers `whenNotPaused` and `whenPaused`, which can be applied to + * the functions of your contract. Note that they will not be pausable by + * simply including this module, only once the modifiers are put in place. + */ +contract PausablePool is Context { + /** + * @dev Emitted when the pause is triggered by `account`. + */ + event Paused(address account); + + /** + * @dev Emitted when the pause is lifted by `account`. + */ + event Unpaused(address account); + + bool private _paused; + + /** + * @dev Initializes the contract in unpaused state. + */ + constructor() internal { + _paused = false; + } + + /** + * @dev Returns true if the contract is paused, and false otherwise. + */ + function paused() public virtual view returns (bool) { + return _paused; + } + + /** + * @dev Modifier to make a function callable only when the contract is not paused. + * + * Requirements: + * + * - The contract must not be paused. + */ + modifier whenNotPaused() { + require(!_paused, '54'); + _; + } + + /** + * @dev Modifier to make a function callable only when the contract is paused. + * + * Requirements: + * + * - The contract must be paused. + */ + modifier whenPaused() { + require(_paused, '55'); + _; + } + + /** + * @dev Triggers stopped state. + * + * Requirements: + * + * - The contract must not be paused. + */ + function _pause() internal virtual whenNotPaused { + _paused = true; + emit Paused(_msgSender()); + } + + /** + * @dev Returns to normal state. + * + * Requirements: + * + * - The contract must be paused. + */ + function _unpause() internal virtual whenPaused { + _paused = false; + emit Unpaused(_msgSender()); + } +} diff --git a/helpers/types.ts b/helpers/types.ts index 07e7a48f..53eb80f0 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -99,7 +99,8 @@ export enum ProtocolErrors { NO_ERRORS = '42', // 'No errors' INVALID_FLASHLOAN_MODE = '43', //Invalid flashloan mode - IS_PAUSED = 'Pausable: paused', + IS_PAUSED = '54', // Pool is paused + IS_ACTIVE = '55', // Pool is active // old