Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: zero ltv transfer #820

Merged
merged 31 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
eb15937
fix: patch zero ltv transfer
sakulstra Feb 27, 2023
4a7bf3c
fix: revert undesired changes
sakulstra Feb 27, 2023
03e8bab
fix: change implementation
sakulstra Feb 27, 2023
acca03e
fix: add docs
sakulstra Feb 27, 2023
921d330
fix: rename error
sakulstra Feb 27, 2023
ca8aac8
fix: improved isolation mode enter mechanic
sakulstra Mar 1, 2023
df8d4f6
fix: improve tests
sakulstra Mar 1, 2023
fd6cc12
fix: remove check from bridge as anotehr role is used already
sakulstra Mar 2, 2023
cbef611
fix: use acl manager
sakulstra Mar 3, 2023
24604e5
fix: flashloan receiver.executeOperation params
sakulstra Mar 3, 2023
dc5c5b2
fix: fix comment
sakulstra Mar 3, 2023
4ff1cf9
fix: move role to constant
sakulstra Mar 3, 2023
543609f
fix: commit broken test
sakulstra Mar 3, 2023
38af96f
fix: add additional test
sakulstra Mar 3, 2023
2407171
fix: add flashloan test
sakulstra Mar 6, 2023
b569141
fix: add additional test
sakulstra Mar 7, 2023
08559ea
fix: fix tests
sakulstra Mar 7, 2023
229d071
fix: prune fl fix
sakulstra Mar 7, 2023
557b94b
Update package.json
sakulstra Mar 14, 2023
fed0b3d
Update contracts/protocol/libraries/logic/ValidationLogic.sol
sakulstra Mar 14, 2023
4b894c5
Update contracts/protocol/libraries/logic/ValidationLogic.sol
sakulstra Mar 14, 2023
baac938
Update contracts/protocol/libraries/logic/ValidationLogic.sol
sakulstra Mar 14, 2023
ad1140b
fix: resolve conflicts
sakulstra Mar 14, 2023
5ba3a53
fix: add comments
sakulstra Mar 14, 2023
2c33310
fix: also add automatic isolation logic to mintUnbacked
sakulstra Mar 15, 2023
60ceb14
fix: update test
sakulstra Mar 15, 2023
4e5baa9
Update contracts/protocol/libraries/logic/ValidationLogic.sol
sakulstra Mar 15, 2023
51d876e
fix: add some more ltv0 tests
sakulstra Mar 15, 2023
b252276
feat: fix tests & add another one
sakulstra Mar 22, 2023
27d2e6e
fix: Fetch addressesProvider from AToken
miguelmtzinf Mar 22, 2023
58de25e
Merge pull request #6 from aave/fix/zero-ltv-transfer-alternative
sakulstra Mar 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/protocol/libraries/helpers/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ library Errors {
string public constant PRICE_ORACLE_SENTINEL_CHECK_FAILED = '59'; // 'Price oracle sentinel validation failed'
string public constant ASSET_NOT_BORROWABLE_IN_ISOLATION = '60'; // 'Asset is not borrowable in isolation mode'
string public constant RESERVE_ALREADY_INITIALIZED = '61'; // 'Reserve has already been initialized'
string public constant USER_IN_ISOLATION_MODE = '62'; // 'User is in isolation mode'
string public constant USER_IN_ISOLATION_MODE_OR_LTV_ZERO = '62'; // 'User is in isolation mode or ltv is zero'
string public constant INVALID_LTV = '63'; // 'Invalid ltv parameter for the reserve'
string public constant INVALID_LIQ_THRESHOLD = '64'; // 'Invalid liquidity threshold parameter for the reserve'
string public constant INVALID_LIQ_BONUS = '65'; // 'Invalid liquidity bonus parameter for the reserve'
Expand Down
5 changes: 3 additions & 2 deletions contracts/protocol/libraries/logic/BridgeLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ library BridgeLogic {

if (isFirstSupply) {
if (
ValidationLogic.validateUseAsCollateral(
ValidationLogic.validateAutomaticUseAsCollateral(
reservesData,
reservesList,
userConfig,
reserveCache.reserveConfiguration
reserveCache.reserveConfiguration,
reserveCache.aTokenAddress
)
) {
userConfig.setUsingAsCollateral(reserve.id, true);
Expand Down
5 changes: 3 additions & 2 deletions contracts/protocol/libraries/logic/LiquidationLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,12 @@ library LiquidationLogic {
if (liquidatorPreviousATokenBalance == 0) {
DataTypes.UserConfigurationMap storage liquidatorConfig = usersConfig[msg.sender];
if (
ValidationLogic.validateUseAsCollateral(
ValidationLogic.validateAutomaticUseAsCollateral(
reservesData,
reservesList,
liquidatorConfig,
collateralReserve.configuration
collateralReserve.configuration,
collateralReserve.aTokenAddress
)
) {
liquidatorConfig.setUsingAsCollateral(collateralReserve.id, true);
Expand Down
12 changes: 7 additions & 5 deletions contracts/protocol/libraries/logic/SupplyLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ library SupplyLogic {

if (isFirstSupply) {
if (
ValidationLogic.validateUseAsCollateral(
ValidationLogic.validateAutomaticUseAsCollateral(
reservesData,
reservesList,
userConfig,
reserveCache.reserveConfiguration
reserveCache.reserveConfiguration,
reserveCache.aTokenAddress
)
) {
userConfig.setUsingAsCollateral(reserve.id, true);
Expand Down Expand Up @@ -212,11 +213,12 @@ library SupplyLogic {
if (params.balanceToBefore == 0) {
DataTypes.UserConfigurationMap storage toConfig = usersConfig[params.to];
if (
ValidationLogic.validateUseAsCollateral(
ValidationLogic.validateAutomaticUseAsCollateral(
reservesData,
reservesList,
toConfig,
reserve.configuration
reserve.configuration,
reserve.aTokenAddress
)
) {
toConfig.setUsingAsCollateral(reserveId, true);
Expand Down Expand Up @@ -270,7 +272,7 @@ library SupplyLogic {
userConfig,
reserveCache.reserveConfiguration
),
Errors.USER_IN_ISOLATION_MODE
Errors.USER_IN_ISOLATION_MODE_OR_LTV_ZERO
);

userConfig.setUsingAsCollateral(reserve.id, true);
Expand Down
52 changes: 47 additions & 5 deletions contracts/protocol/libraries/logic/ValidationLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {IScaledBalanceToken} from '../../../interfaces/IScaledBalanceToken.sol';
import {IPriceOracleGetter} from '../../../interfaces/IPriceOracleGetter.sol';
import {IAToken} from '../../../interfaces/IAToken.sol';
import {IPriceOracleSentinel} from '../../../interfaces/IPriceOracleSentinel.sol';
import {IPoolAddressesProvider} from '../../../interfaces/IPoolAddressesProvider.sol';
import {IAccessControl} from '../../../dependencies/openzeppelin/contracts/IAccessControl.sol';
import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
import {UserConfiguration} from '../configuration/UserConfiguration.sol';
import {Errors} from '../helpers/Errors.sol';
Expand All @@ -19,6 +21,7 @@ import {DataTypes} from '../types/DataTypes.sol';
import {ReserveLogic} from './ReserveLogic.sol';
import {GenericLogic} from './GenericLogic.sol';
import {SafeCast} from '../../../dependencies/openzeppelin/contracts/SafeCast.sol';
import {IncentivizedERC20} from '../../tokenization/base/IncentivizedERC20.sol';

/**
* @title ReserveLogic library
Expand Down Expand Up @@ -49,6 +52,12 @@ library ValidationLogic {
*/
uint256 public constant HEALTH_FACTOR_LIQUIDATION_THRESHOLD = 1e18;

/**
* @dev Role identifier for the role allowed to supply isolated reserves as collateral
*/
bytes32 public constant ISOLATED_COLLATERAL_SUPPLIER_ROLE =
keccak256('ISOLATED_COLLATERAL_SUPPLIER');

/**
* @notice Validates a supply action.
* @param reserveCache The cached data of the reserve
Expand Down Expand Up @@ -663,7 +672,7 @@ library ValidationLogic {
Errors.INCONSISTENT_EMODE_CATEGORY
);

//eMode can always be enabled if the user hasn't supplied anything
// eMode can always be enabled if the user hasn't supplied anything
if (userConfig.isEmpty()) {
return;
}
Expand All @@ -687,10 +696,8 @@ library ValidationLogic {
}

/**
* @notice Validates if an asset can be activated as collateral in the following actions: supply, transfer,
* set as collateral, mint unbacked, and liquidate
* @dev This is used to ensure that the constraints for isolated assets are respected by all the actions that
* generate transfers of aTokens
* @notice Validates the action of activating the asset as collateral.
* @dev Only possible if the asset has non-zero LTV and the user is not in isolation mode
* @param reservesData The state of all the reserves
* @param reservesList The addresses of all the active reserves
* @param userConfig the user configuration
Expand All @@ -703,11 +710,46 @@ library ValidationLogic {
DataTypes.UserConfigurationMap storage userConfig,
DataTypes.ReserveConfigurationMap memory reserveConfig
) internal view returns (bool) {
if (reserveConfig.getLtv() == 0) {
return false;
}
if (!userConfig.isUsingAsCollateralAny()) {
return true;
}
(bool isolationModeActive, , ) = userConfig.getIsolationModeState(reservesData, reservesList);

return (!isolationModeActive && reserveConfig.getDebtCeiling() == 0);
}

/**
* @notice Validates if an asset should be automatically activated as collateral in the following actions: supply,
* transfer, mint unbacked, and liquidate
* @dev This is used to ensure that isolated assets are not enabled as collateral automatically
* @param reservesData The state of all the reserves
* @param reservesList The addresses of all the active reserves
* @param userConfig the user configuration
* @param reserveConfig The reserve configuration
* @return True if the asset can be activated as collateral, false otherwise
*/
function validateAutomaticUseAsCollateral(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
DataTypes.UserConfigurationMap storage userConfig,
DataTypes.ReserveConfigurationMap memory reserveConfig,
address aTokenAddress
) internal view returns (bool) {
if (reserveConfig.getDebtCeiling() != 0) {
// ensures only the ISOLATED_COLLATERAL_SUPPLIER_ROLE can enable collateral as side-effect of an action
IPoolAddressesProvider addressesProvider = IncentivizedERC20(aTokenAddress)
.POOL()
.ADDRESSES_PROVIDER();
if (
!IAccessControl(addressesProvider.getACLManager()).hasRole(
ISOLATED_COLLATERAL_SUPPLIER_ROLE,
msg.sender
)
) return false;
}
return validateUseAsCollateral(reservesData, reservesList, userConfig, reserveConfig);
}
}
6 changes: 1 addition & 5 deletions contracts/protocol/libraries/types/DataTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ library DataTypes {
string label;
}

enum InterestRateMode {
NONE,
STABLE,
VARIABLE
}
enum InterestRateMode {NONE, STABLE, VARIABLE}

struct ReserveCache {
uint256 currScaledVariableDebt;
Expand Down
4 changes: 2 additions & 2 deletions helpers/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BigNumber} from '@ethersproject/bignumber';
import { BigNumber } from '@ethersproject/bignumber';

export interface SymbolMap<T> {
[symbol: string]: T;
Expand Down Expand Up @@ -131,7 +131,7 @@ export enum ProtocolErrors {
PRICE_ORACLE_SENTINEL_CHECK_FAILED = '59', // 'Price oracle sentinel validation failed'
ASSET_NOT_BORROWABLE_IN_ISOLATION = '60', // 'Asset is not borrowable in isolation mode'
RESERVE_ALREADY_INITIALIZED = '61', // 'Reserve has already been initialized'
USER_IN_ISOLATION_MODE = '62', // 'User is in isolation mode'
USER_IN_ISOLATION_MODE_OR_LTV_ZERO = '62', // 'User is in isolation mode or ltv is zero'
INVALID_LTV = '63', // 'Invalid ltv parameter for the reserve'
INVALID_LIQ_THRESHOLD = '64', // 'Invalid liquidity threshold parameter for the reserve'
INVALID_LIQ_BONUS = '65', // 'Invalid liquidity bonus parameter for the reserve'
Expand Down
Loading