Merge branch 'master' of gitlab.com:aave-tech/protocol-v2 into fix/75-improve-migration-scripts

This commit is contained in:
David Racero 2020-10-29 14:11:00 +01:00
commit c187440f84
4 changed files with 112 additions and 25 deletions

View File

@ -1,21 +1,23 @@
// SPDX-License-Identifier: agpl-3.0 // SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8; pragma solidity ^0.6.8;
import {Errors} from '../helpers/Errors.sol';
/** /**
* @title ReserveConfiguration library * @title ReserveConfiguration library
* @author Aave * @author Aave
* @notice Implements the bitmap logic to handle the reserve configuration * @notice Implements the bitmap logic to handle the reserve configuration
*/ */
library ReserveConfiguration { library ReserveConfiguration {
uint256 constant LTV_MASK = 0xFFFFFFFFFFFFFFFF0000; uint256 constant LTV_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000; // prettier-ignore
uint256 constant LIQUIDATION_THRESHOLD_MASK = 0xFFFFFFFFFFFF0000FFFF; uint256 constant LIQUIDATION_THRESHOLD_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000FFFF; // prettier-ignore
uint256 constant LIQUIDATION_BONUS_MASK = 0xFFFFFFFF0000FFFFFFFF; uint256 constant LIQUIDATION_BONUS_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000FFFFFFFF; // prettier-ignore
uint256 constant DECIMALS_MASK = 0xFFFFFF00FFFFFFFFFFFF; uint256 constant DECIMALS_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FFFFFFFFFFFF; // prettier-ignore
uint256 constant ACTIVE_MASK = 0xFFFFFEFFFFFFFFFFFFFF; uint256 constant ACTIVE_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFF; // prettier-ignore
uint256 constant FROZEN_MASK = 0xFFFFFDFFFFFFFFFFFFFF; uint256 constant FROZEN_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDFFFFFFFFFFFFFF; // prettier-ignore
uint256 constant BORROWING_MASK = 0xFFFFFBFFFFFFFFFFFFFF; uint256 constant BORROWING_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFFFFFFFFFFF; // prettier-ignore
uint256 constant STABLE_BORROWING_MASK = 0xFFFFF7FFFFFFFFFFFFFF; uint256 constant STABLE_BORROWING_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFFFFF; // prettier-ignore
uint256 constant RESERVE_FACTOR_MASK = 0xFFFFFFFFFFFFFFFF; uint256 constant RESERVE_FACTOR_MASK = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFFFFFF; // prettier-ignore
/// @dev For the LTV, the start bit is 0 (up to 15), but we don't declare it as for 0 no bit movement is needed /// @dev For the LTV, the start bit is 0 (up to 15), but we don't declare it as for 0 no bit movement is needed
uint256 constant LIQUIDATION_THRESHOLD_START_BIT_POSITION = 16; uint256 constant LIQUIDATION_THRESHOLD_START_BIT_POSITION = 16;
@ -27,6 +29,12 @@ library ReserveConfiguration {
uint256 constant STABLE_BORROWING_ENABLED_START_BIT_POSITION = 59; uint256 constant STABLE_BORROWING_ENABLED_START_BIT_POSITION = 59;
uint256 constant RESERVE_FACTOR_START_BIT_POSITION = 64; uint256 constant RESERVE_FACTOR_START_BIT_POSITION = 64;
uint256 constant MAX_VALID_LTV = 65535;
uint256 constant MAX_VALID_LIQUIDATION_THRESHOLD = 65535;
uint256 constant MAX_VALID_LIQUIDATION_BONUS = 65535;
uint256 constant MAX_VALID_DECIMALS = 255;
uint256 constant MAX_VALID_RESERVE_FACTOR = 65535;
struct Map { struct Map {
//bit 0-15: LTV //bit 0-15: LTV
//bit 16-31: Liq. threshold //bit 16-31: Liq. threshold
@ -47,6 +55,8 @@ library ReserveConfiguration {
* @param ltv the new ltv * @param ltv the new ltv
**/ **/
function setLtv(ReserveConfiguration.Map memory self, uint256 ltv) internal pure { function setLtv(ReserveConfiguration.Map memory self, uint256 ltv) internal pure {
require(ltv <= MAX_VALID_LTV, Errors.INVALID_LTV);
self.data = (self.data & LTV_MASK) | ltv; self.data = (self.data & LTV_MASK) | ltv;
} }
@ -68,6 +78,8 @@ library ReserveConfiguration {
internal internal
pure pure
{ {
require(threshold <= MAX_VALID_LIQUIDATION_THRESHOLD, Errors.INVALID_LIQ_THRESHOLD);
self.data = self.data =
(self.data & LIQUIDATION_THRESHOLD_MASK) | (self.data & LIQUIDATION_THRESHOLD_MASK) |
(threshold << LIQUIDATION_THRESHOLD_START_BIT_POSITION); (threshold << LIQUIDATION_THRESHOLD_START_BIT_POSITION);
@ -92,6 +104,8 @@ library ReserveConfiguration {
* @param bonus the new liquidation bonus * @param bonus the new liquidation bonus
**/ **/
function setLiquidationBonus(ReserveConfiguration.Map memory self, uint256 bonus) internal pure { function setLiquidationBonus(ReserveConfiguration.Map memory self, uint256 bonus) internal pure {
require(bonus <= MAX_VALID_LIQUIDATION_BONUS, Errors.INVALID_LIQ_BONUS);
self.data = self.data =
(self.data & LIQUIDATION_BONUS_MASK) | (self.data & LIQUIDATION_BONUS_MASK) |
(bonus << LIQUIDATION_BONUS_START_BIT_POSITION); (bonus << LIQUIDATION_BONUS_START_BIT_POSITION);
@ -116,6 +130,8 @@ library ReserveConfiguration {
* @param decimals the decimals * @param decimals the decimals
**/ **/
function setDecimals(ReserveConfiguration.Map memory self, uint256 decimals) internal pure { function setDecimals(ReserveConfiguration.Map memory self, uint256 decimals) internal pure {
require(decimals <= MAX_VALID_DECIMALS, Errors.INVALID_DECIMALS);
self.data = (self.data & DECIMALS_MASK) | (decimals << RESERVE_DECIMALS_START_BIT_POSITION); self.data = (self.data & DECIMALS_MASK) | (decimals << RESERVE_DECIMALS_START_BIT_POSITION);
} }
@ -145,7 +161,7 @@ library ReserveConfiguration {
* @return the active state * @return the active state
**/ **/
function getActive(ReserveConfiguration.Map storage self) internal view returns (bool) { function getActive(ReserveConfiguration.Map storage self) internal view returns (bool) {
return ((self.data & ~ACTIVE_MASK) >> IS_ACTIVE_START_BIT_POSITION) != 0; return (self.data & ~ACTIVE_MASK) != 0;
} }
/** /**
@ -165,7 +181,7 @@ library ReserveConfiguration {
* @return the frozen state * @return the frozen state
**/ **/
function getFrozen(ReserveConfiguration.Map storage self) internal view returns (bool) { function getFrozen(ReserveConfiguration.Map storage self) internal view returns (bool) {
return ((self.data & ~FROZEN_MASK) >> IS_FROZEN_START_BIT_POSITION) != 0; return (self.data & ~FROZEN_MASK) != 0;
} }
/** /**
@ -185,7 +201,7 @@ library ReserveConfiguration {
* @return the borrowing state * @return the borrowing state
**/ **/
function getBorrowingEnabled(ReserveConfiguration.Map storage self) internal view returns (bool) { function getBorrowingEnabled(ReserveConfiguration.Map storage self) internal view returns (bool) {
return ((self.data & ~BORROWING_MASK) >> BORROWING_ENABLED_START_BIT_POSITION) != 0; return (self.data & ~BORROWING_MASK) != 0;
} }
/** /**
@ -212,8 +228,7 @@ library ReserveConfiguration {
view view
returns (bool) returns (bool)
{ {
return return (self.data & ~STABLE_BORROWING_MASK) != 0;
((self.data & ~STABLE_BORROWING_MASK) >> STABLE_BORROWING_ENABLED_START_BIT_POSITION) != 0;
} }
/** /**
@ -225,6 +240,8 @@ library ReserveConfiguration {
internal internal
pure pure
{ {
require(reserveFactor <= MAX_VALID_RESERVE_FACTOR, Errors.INVALID_RESERVE_FACTOR);
self.data = self.data =
(self.data & RESERVE_FACTOR_MASK) | (self.data & RESERVE_FACTOR_MASK) |
(reserveFactor << RESERVE_FACTOR_START_BIT_POSITION); (reserveFactor << RESERVE_FACTOR_START_BIT_POSITION);
@ -257,10 +274,10 @@ library ReserveConfiguration {
uint256 dataLocal = self.data; uint256 dataLocal = self.data;
return ( return (
(dataLocal & ~ACTIVE_MASK) >> IS_ACTIVE_START_BIT_POSITION != 0, (dataLocal & ~ACTIVE_MASK) != 0,
(dataLocal & ~FROZEN_MASK) >> IS_FROZEN_START_BIT_POSITION != 0, (dataLocal & ~FROZEN_MASK) != 0,
(dataLocal & ~BORROWING_MASK) >> BORROWING_ENABLED_START_BIT_POSITION != 0, (dataLocal & ~BORROWING_MASK) != 0,
(dataLocal & ~STABLE_BORROWING_MASK) >> STABLE_BORROWING_ENABLED_START_BIT_POSITION != 0 (dataLocal & ~STABLE_BORROWING_MASK) != 0
); );
} }
@ -316,8 +333,7 @@ library ReserveConfiguration {
); );
} }
/**
/**
* @dev gets the configuration flags of the reserve from a memory object * @dev gets the configuration flags of the reserve from a memory object
* @param self the reserve configuration * @param self the reserve configuration
* @return the state flags representing active, freezed, borrowing enabled, stableRateBorrowing enabled * @return the state flags representing active, freezed, borrowing enabled, stableRateBorrowing enabled
@ -333,10 +349,10 @@ library ReserveConfiguration {
) )
{ {
return ( return (
(self.data & ~ACTIVE_MASK) >> IS_ACTIVE_START_BIT_POSITION != 0, (self.data & ~ACTIVE_MASK) != 0,
(self.data & ~FROZEN_MASK) >> IS_FROZEN_START_BIT_POSITION != 0, (self.data & ~FROZEN_MASK) != 0,
(self.data & ~BORROWING_MASK) >> BORROWING_ENABLED_START_BIT_POSITION != 0, (self.data & ~BORROWING_MASK) != 0,
(self.data & ~STABLE_BORROWING_MASK) >> STABLE_BORROWING_ENABLED_START_BIT_POSITION != 0 (self.data & ~STABLE_BORROWING_MASK) != 0
); );
} }
} }

View File

@ -89,6 +89,15 @@ library Errors {
// pausable error message // pausable error message
string public constant IS_PAUSED = '58'; // 'Pool is paused' string public constant IS_PAUSED = '58'; // 'Pool is paused'
// Reserve configuration
string public constant INVALID_LTV = '70';
string public constant INVALID_LIQ_THRESHOLD = '71';
string public constant INVALID_LIQ_BONUS = '72';
string public constant INVALID_DECIMALS = '73';
string public constant INVALID_RESERVE_FACTOR = '74';
enum CollateralManagerErrors { enum CollateralManagerErrors {
NO_ERROR, NO_ERROR,
NO_COLLATERAL_AVAILABLE, NO_COLLATERAL_AVAILABLE,

View File

@ -119,6 +119,12 @@ export enum ProtocolErrors {
IS_PAUSED = '58', // Pool is paused IS_PAUSED = '58', // Pool is paused
INVALID_LTV = '70',
INVALID_LIQ_THRESHOLD = '71',
INVALID_LIQ_BONUS = '72',
INVALID_DECIMALS = '73',
INVALID_RESERVE_FACTOR = '74',
// old // old
INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer',

View File

@ -10,7 +10,63 @@ const APPROVAL_AMOUNT_LENDING_POOL =
const {expect} = require('chai'); const {expect} = require('chai');
makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const {CALLER_NOT_AAVE_ADMIN, RESERVE_LIQUIDITY_NOT_0} = ProtocolErrors; const {
CALLER_NOT_AAVE_ADMIN,
RESERVE_LIQUIDITY_NOT_0,
INVALID_LTV,
INVALID_LIQ_THRESHOLD,
INVALID_LIQ_BONUS,
INVALID_DECIMALS,
INVALID_RESERVE_FACTOR,
} = ProtocolErrors;
it('Reverts trying to set an invalid LTV', async () => {
const {configurator, weth} = testEnv;
const invalidLtv = 65536;
await expect(configurator.setLtv(weth.address, invalidLtv)).to.be.revertedWith(INVALID_LTV);
});
it('Reverts trying to set an invalid liquidation threshold', async () => {
const {configurator, weth} = testEnv;
const invalidLiqThreshold = 65536;
await expect(
configurator.setLiquidationThreshold(weth.address, invalidLiqThreshold)
).to.be.revertedWith(INVALID_LIQ_THRESHOLD);
});
it('Reverts trying to set an invalid liquidation bonus', async () => {
const {configurator, weth} = testEnv;
const invalidLiqBonus = 65536;
await expect(
configurator.setLiquidationBonus(weth.address, invalidLiqBonus)
).to.be.revertedWith(INVALID_LIQ_BONUS);
});
it('Reverts trying to set an invalid reserve decimals', async () => {
const {configurator, weth} = testEnv;
const invalidDecimals = 256;
await expect(configurator.setReserveDecimals(weth.address, invalidDecimals)).to.be.revertedWith(
INVALID_DECIMALS
);
});
it('Reverts trying to set an invalid reserve factor', async () => {
const {configurator, weth} = testEnv;
const invalidReserveFactor = 65536;
await expect(
configurator.setReserveFactor(weth.address, invalidReserveFactor)
).to.be.revertedWith(INVALID_RESERVE_FACTOR);
});
it('Deactivates the ETH reserve', async () => { it('Deactivates the ETH reserve', async () => {
const {configurator, weth, helpersContract} = testEnv; const {configurator, weth, helpersContract} = testEnv;