-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathValidationLogic.sol
More file actions
670 lines (611 loc) · 24.8 KB
/
ValidationLogic.sol
File metadata and controls
670 lines (611 loc) · 24.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;
import {IERC20} from 'openzeppelin-contracts/contracts/token/ERC20/IERC20.sol';
import {Address} from '../../../dependencies/openzeppelin/contracts/Address.sol';
import {GPv2SafeERC20} from '../../../dependencies/gnosis/contracts/GPv2SafeERC20.sol';
import {IAToken} from '../../../interfaces/IAToken.sol';
import {IPriceOracleSentinel} from '../../../interfaces/IPriceOracleSentinel.sol';
import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
import {UserConfiguration} from '../configuration/UserConfiguration.sol';
import {EModeConfiguration} from '../configuration/EModeConfiguration.sol';
import {Errors} from '../helpers/Errors.sol';
import {TokenMath} from '../helpers/TokenMath.sol';
import {PercentageMath} from '../math/PercentageMath.sol';
import {DataTypes} from '../types/DataTypes.sol';
import {ReserveLogic} from './ReserveLogic.sol';
import {GenericLogic} from './GenericLogic.sol';
import {SafeCast} from 'openzeppelin-contracts/contracts/utils/math/SafeCast.sol';
/**
* @title ValidationLogic library
* @author Aave
* @notice Implements functions to validate the different actions of the protocol
*/
library ValidationLogic {
using ReserveLogic for DataTypes.ReserveData;
using TokenMath for uint256;
using PercentageMath for uint256;
using SafeCast for uint256;
using GPv2SafeERC20 for IERC20;
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
using UserConfiguration for DataTypes.UserConfigurationMap;
using Address for address;
// Factor to apply to "only-variable-debt" liquidity rate to get threshold for rebalancing, expressed in bps
// A value of 0.9e4 results in 90%
uint256 public constant REBALANCE_UP_LIQUIDITY_RATE_THRESHOLD = 0.9e4;
// Minimum health factor allowed under any circumstance
// A value of 0.95e18 results in 0.95
uint256 public constant MINIMUM_HEALTH_FACTOR_LIQUIDATION_THRESHOLD = 0.95e18;
/**
* @dev Minimum health factor to consider a user position healthy
* A value of 1e18 results in 1
*/
uint256 public constant HEALTH_FACTOR_LIQUIDATION_THRESHOLD = 1e18;
/**
* @notice Validates a supply action.
* @param reserveCache The cached data of the reserve
* @param scaledAmount The scaledAmount to be supplied
*/
function validateSupply(
DataTypes.ReserveCache memory reserveCache,
DataTypes.ReserveData storage reserve,
uint256 scaledAmount,
address onBehalfOf
) internal view {
require(scaledAmount != 0, Errors.InvalidAmount());
(bool isActive, bool isFrozen, , bool isPaused) = reserveCache.reserveConfiguration.getFlags();
require(isActive, Errors.ReserveInactive());
require(!isPaused, Errors.ReservePaused());
require(!isFrozen, Errors.ReserveFrozen());
require(onBehalfOf != reserveCache.aTokenAddress, Errors.SupplyToAToken());
uint256 supplyCap = reserveCache.reserveConfiguration.getSupplyCap();
require(
supplyCap == 0 ||
(
(IAToken(reserveCache.aTokenAddress).scaledTotalSupply() +
scaledAmount +
uint256(reserve.accruedToTreasury)).getATokenBalance(reserveCache.nextLiquidityIndex)
) <=
supplyCap * (10 ** reserveCache.reserveConfiguration.getDecimals()),
Errors.SupplyCapExceeded()
);
}
/**
* @notice Validates a withdraw action.
* @param reserveCache The cached data of the reserve
* @param scaledAmount The scaled amount to be withdrawn
* @param scaledUserBalance The scaled balance of the user
*/
function validateWithdraw(
DataTypes.ReserveCache memory reserveCache,
uint256 scaledAmount,
uint256 scaledUserBalance
) internal pure {
require(scaledAmount != 0, Errors.InvalidAmount());
require(scaledAmount <= scaledUserBalance, Errors.NotEnoughAvailableUserBalance());
(bool isActive, , , bool isPaused) = reserveCache.reserveConfiguration.getFlags();
require(isActive, Errors.ReserveInactive());
require(!isPaused, Errors.ReservePaused());
}
struct ValidateBorrowLocalVars {
uint256 amount;
uint256 userDebtInBaseCurrency;
uint256 availableLiquidity;
uint256 totalDebt;
uint256 reserveDecimals;
uint256 borrowCap;
uint256 amountInBaseCurrency;
uint256 assetUnit;
address siloedBorrowingAddress;
bool isActive;
bool isFrozen;
bool isPaused;
bool borrowingEnabled;
bool siloedBorrowingEnabled;
}
/**
* @notice Validates a borrow action.
* @param reservesData The state of all the reserves
* @param reservesList The addresses of all the active reserves
* @param eModeCategories The configuration of all the efficiency mode categories
* @param params Additional params needed for the validation
*/
function validateBorrow(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
mapping(uint8 => DataTypes.EModeCategory) storage eModeCategories,
DataTypes.ValidateBorrowParams memory params
) internal view {
require(params.amountScaled != 0, Errors.InvalidAmount());
ValidateBorrowLocalVars memory vars;
vars.amount = params.amountScaled.getVTokenBalance(params.reserveCache.nextVariableBorrowIndex);
(vars.isActive, vars.isFrozen, vars.borrowingEnabled, vars.isPaused) = params
.reserveCache
.reserveConfiguration
.getFlags();
require(vars.isActive, Errors.ReserveInactive());
require(!vars.isPaused, Errors.ReservePaused());
require(!vars.isFrozen, Errors.ReserveFrozen());
if (params.userEModeCategory != 0) {
require(
EModeConfiguration.isReserveEnabledOnBitmap(
eModeCategories[params.userEModeCategory].borrowableBitmap,
reservesData[params.asset].id
),
Errors.NotBorrowableInEMode()
);
} else {
require(vars.borrowingEnabled, Errors.BorrowingNotEnabled());
}
require(
IERC20(params.reserveCache.aTokenAddress).totalSupply() >= vars.amount,
Errors.InvalidAmount()
);
require(
params.priceOracleSentinel == address(0) ||
IPriceOracleSentinel(params.priceOracleSentinel).isBorrowAllowed(),
Errors.PriceOracleSentinelCheckFailed()
);
//validate interest rate mode
require(
params.interestRateMode == DataTypes.InterestRateMode.VARIABLE,
Errors.InvalidInterestRateModeSelected()
);
vars.reserveDecimals = params.reserveCache.reserveConfiguration.getDecimals();
vars.borrowCap = params.reserveCache.reserveConfiguration.getBorrowCap();
unchecked {
vars.assetUnit = 10 ** vars.reserveDecimals;
}
if (vars.borrowCap != 0) {
vars.totalDebt = (params.reserveCache.currScaledVariableDebt + params.amountScaled)
.getVTokenBalance(params.reserveCache.nextVariableBorrowIndex);
unchecked {
require(vars.totalDebt <= vars.borrowCap * vars.assetUnit, Errors.BorrowCapExceeded());
}
}
if (params.userConfig.isBorrowingAny()) {
(vars.siloedBorrowingEnabled, vars.siloedBorrowingAddress) = params
.userConfig
.getSiloedBorrowingState(reservesData, reservesList);
if (vars.siloedBorrowingEnabled) {
require(vars.siloedBorrowingAddress == params.asset, Errors.SiloedBorrowingViolation());
} else {
require(
!params.reserveCache.reserveConfiguration.getSiloedBorrowing(),
Errors.SiloedBorrowingViolation()
);
}
}
}
/**
* @notice Validates a repay action.
* @param user The user initiating the repayment
* @param reserveCache The cached data of the reserve
* @param amountSent The amount sent for the repayment. Can be an actual value or type(uint256).max
* @param onBehalfOf The address of the user sender is repaying for
* @param debtScaled The borrow scaled balance of the user
*/
function validateRepay(
address user,
DataTypes.ReserveCache memory reserveCache,
uint256 amountSent,
DataTypes.InterestRateMode interestRateMode,
address onBehalfOf,
uint256 debtScaled
) internal pure {
require(amountSent != 0, Errors.InvalidAmount());
require(
interestRateMode == DataTypes.InterestRateMode.VARIABLE,
Errors.InvalidInterestRateModeSelected()
);
require(
amountSent != type(uint256).max || user == onBehalfOf,
Errors.NoExplicitAmountToRepayOnBehalf()
);
(bool isActive, , , bool isPaused) = reserveCache.reserveConfiguration.getFlags();
require(isActive, Errors.ReserveInactive());
require(!isPaused, Errors.ReservePaused());
require(debtScaled != 0, Errors.NoDebtOfSelectedType());
}
/**
* @notice Validates the action of setting an asset as collateral.
* @param reserveConfig The config of the reserve
*/
function validateSetUseReserveAsCollateral(
DataTypes.ReserveConfigurationMap memory reserveConfig
) internal pure {
(bool isActive, , , bool isPaused) = reserveConfig.getFlags();
require(isActive, Errors.ReserveInactive());
require(!isPaused, Errors.ReservePaused());
}
/**
* @notice Validates a flashloan action.
* @param reservesData The state of all the reserves
* @param assets The assets being flash-borrowed
* @param amounts The amounts for each asset being borrowed
*/
function validateFlashloan(
mapping(address => DataTypes.ReserveData) storage reservesData,
address[] memory assets,
uint256[] memory amounts
) internal view {
require(assets.length == amounts.length, Errors.InconsistentFlashloanParams());
for (uint256 i = 0; i < assets.length; i++) {
for (uint256 j = i + 1; j < assets.length; j++) {
require(assets[i] != assets[j], Errors.InconsistentFlashloanParams());
}
validateFlashloanSimple(reservesData[assets[i]], amounts[i]);
}
}
/**
* @notice Validates a flashloan action.
* @param reserve The state of the reserve
*/
function validateFlashloanSimple(
DataTypes.ReserveData storage reserve,
uint256 amount
) internal view {
DataTypes.ReserveConfigurationMap memory configuration = reserve.configuration;
require(!configuration.getPaused(), Errors.ReservePaused());
require(configuration.getActive(), Errors.ReserveInactive());
require(configuration.getFlashLoanEnabled(), Errors.FlashloanDisabled());
require(IERC20(reserve.aTokenAddress).totalSupply() >= amount, Errors.InvalidAmount());
}
struct ValidateLiquidationCallLocalVars {
bool collateralReserveActive;
bool collateralReservePaused;
bool principalReserveActive;
bool principalReservePaused;
}
/**
* @notice Validates the liquidation action.
* @param borrowerConfig The user configuration mapping
* @param collateralReserve The reserve data of the collateral
* @param debtReserve The reserve data of the debt
* @param params Additional parameters needed for the validation
*/
function validateLiquidationCall(
DataTypes.UserConfigurationMap storage borrowerConfig,
DataTypes.ReserveData storage collateralReserve,
DataTypes.ReserveData storage debtReserve,
DataTypes.ValidateLiquidationCallParams memory params
) internal view {
ValidateLiquidationCallLocalVars memory vars;
require(params.borrower != params.liquidator, Errors.SelfLiquidation());
(vars.collateralReserveActive, , , vars.collateralReservePaused) = collateralReserve
.configuration
.getFlags();
(vars.principalReserveActive, , , vars.principalReservePaused) = params
.debtReserveCache
.reserveConfiguration
.getFlags();
require(vars.collateralReserveActive && vars.principalReserveActive, Errors.ReserveInactive());
require(!vars.collateralReservePaused && !vars.principalReservePaused, Errors.ReservePaused());
require(
params.priceOracleSentinel == address(0) ||
params.healthFactor < MINIMUM_HEALTH_FACTOR_LIQUIDATION_THRESHOLD ||
IPriceOracleSentinel(params.priceOracleSentinel).isLiquidationAllowed(),
Errors.PriceOracleSentinelCheckFailed()
);
require(
collateralReserve.liquidationGracePeriodUntil < uint40(block.timestamp) &&
debtReserve.liquidationGracePeriodUntil < uint40(block.timestamp),
Errors.LiquidationGraceSentinelCheckFailed()
);
require(
params.healthFactor < HEALTH_FACTOR_LIQUIDATION_THRESHOLD,
Errors.HealthFactorNotBelowThreshold()
);
//if collateral isn't enabled as collateral by user, it cannot be liquidated
require(
borrowerConfig.isUsingAsCollateral(collateralReserve.id),
Errors.CollateralCannotBeLiquidated()
);
require(params.totalDebt != 0, Errors.SpecifiedCurrencyNotBorrowedByUser());
}
/**
* @notice Validates the health factor of a user.
* @param reservesData The state of all the reserves
* @param reservesList The addresses of all the active reserves
* @param eModeCategories The configuration of all the efficiency mode categories
* @param userConfig The state of the user for the specific reserve
* @param user The user to validate health factor of
* @param userEModeCategory The users active efficiency mode category
* @param oracle The price oracle
*/
function validateHealthFactor(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
mapping(uint8 => DataTypes.EModeCategory) storage eModeCategories,
DataTypes.UserConfigurationMap memory userConfig,
address user,
uint8 userEModeCategory,
address oracle
) internal view returns (uint256, bool) {
(, , , , uint256 healthFactor, bool hasZeroLtvCollateral) = GenericLogic
.calculateUserAccountData(
reservesData,
reservesList,
eModeCategories,
DataTypes.CalculateUserAccountDataParams({
userConfig: userConfig,
user: user,
oracle: oracle,
userEModeCategory: userEModeCategory
})
);
require(
healthFactor >= HEALTH_FACTOR_LIQUIDATION_THRESHOLD,
Errors.HealthFactorLowerThanLiquidationThreshold()
);
return (healthFactor, hasZeroLtvCollateral);
}
/**
* @notice Validates the health factor of a user and the ltv of the asset being borrowed.
* The ltv validation is a measure to prevent accidental borrowing close to liquidations.
* Sophisticated users can work around this validation in various ways.
* @param reservesData The state of all the reserves
* @param reservesList The addresses of all the active reserves
* @param eModeCategories The configuration of all the efficiency mode categories
* @param userConfig The state of the user for the specific reserve
* @param user The user from which the aTokens are being transferred
* @param userEModeCategory The users active efficiency mode category
* @param oracle The price oracle
*/
function validateHFAndLtv(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
mapping(uint8 => DataTypes.EModeCategory) storage eModeCategories,
DataTypes.UserConfigurationMap memory userConfig,
address user,
uint8 userEModeCategory,
address oracle
) internal view {
(
uint256 userCollateralInBaseCurrency,
uint256 userDebtInBaseCurrency,
uint256 currentLtv,
,
uint256 healthFactor,
) = GenericLogic.calculateUserAccountData(
reservesData,
reservesList,
eModeCategories,
DataTypes.CalculateUserAccountDataParams({
userConfig: userConfig,
user: user,
oracle: oracle,
userEModeCategory: userEModeCategory
})
);
require(currentLtv != 0, Errors.LtvValidationFailed());
require(
healthFactor >= HEALTH_FACTOR_LIQUIDATION_THRESHOLD,
Errors.HealthFactorLowerThanLiquidationThreshold()
);
require(
userCollateralInBaseCurrency >= userDebtInBaseCurrency.percentDivCeil(currentLtv),
Errors.CollateralCannotCoverNewBorrow()
);
}
/**
* @notice Validates the health factor of a user and the ltvzero configuration for the asset being withdrawn/transferred or disabled as collateral.
* @param reservesData The state of all the reserves
* @param reservesList The addresses of all the active reserves
* @param eModeCategories The configuration of all the efficiency mode categories
* @param userConfig The state of the user for the specific reserve
* @param asset The asset for which the ltv will be validated
* @param from The user from which the aTokens are being transferred
* @param oracle The price oracle
* @param userEModeCategory The users active efficiency mode category
*/
function validateHFAndLtvzero(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
mapping(uint8 => DataTypes.EModeCategory) storage eModeCategories,
DataTypes.UserConfigurationMap memory userConfig,
address asset,
address from,
address oracle,
uint8 userEModeCategory
) internal view {
(, bool hasZeroLtvCollateral) = validateHealthFactor(
reservesData,
reservesList,
eModeCategories,
userConfig,
from,
userEModeCategory,
oracle
);
// If the user has an ltvzero asset, the selected asset must be the ltv0 asset.
// This mechanism ensures that a multi-collateral position needs to withdraw/transfer the ltv0 asset first.
if (hasZeroLtvCollateral) {
require(
getUserReserveLtv(
reservesData[asset],
eModeCategories[userEModeCategory],
userEModeCategory
) == 0,
Errors.LtvValidationFailed()
);
}
}
/**
* @notice Validates a transfer action.
* @param reserve The reserve object
*/
function validateTransfer(DataTypes.ReserveData storage reserve) internal view {
require(!reserve.configuration.getPaused(), Errors.ReservePaused());
}
/**
* @notice Validates a drop reserve action.
* @param reservesList The addresses of all the active reserves
* @param reserve The reserve object
* @param asset The address of the reserve's underlying asset
*/
function validateDropReserve(
mapping(uint256 => address) storage reservesList,
DataTypes.ReserveData storage reserve,
address asset
) internal view {
require(asset != address(0), Errors.ZeroAddressNotValid());
require(reserve.id != 0 || reservesList[0] == asset, Errors.AssetNotListed());
require(
IERC20(reserve.variableDebtTokenAddress).totalSupply() == 0,
Errors.VariableDebtSupplyNotZero()
);
require(
IERC20(reserve.aTokenAddress).totalSupply() == 0 && reserve.accruedToTreasury == 0,
Errors.UnderlyingClaimableRightsNotZero()
);
}
/**
* @notice Validates the action of setting efficiency mode.
* @param reservesData The state of all the reserves
* @param reservesList The addresses of all the active reserves
* @param eModeCategories a mapping storing configurations for all efficiency mode categories
* @param userConfig the user configuration
* @param categoryId The id of the users eMode category
*/
function validateSetUserEMode(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
mapping(uint8 => DataTypes.EModeCategory) storage eModeCategories,
DataTypes.UserConfigurationMap memory userConfig,
uint8 categoryId
) internal view {
DataTypes.EModeCategory storage eModeCategory = eModeCategories[categoryId];
// category is invalid if the liq threshold is not set
require(
categoryId == 0 || eModeCategory.liquidationThreshold != 0,
Errors.InconsistentEModeCategory()
);
// eMode can always be enabled if the user hasn't supplied anything
if (userConfig.isEmpty()) {
return;
}
uint256 i = 0;
bool isBorrowed = false;
bool isEnabledAsCollateral = false;
// the cache is muted inside the iteration and should not be used for other operations
uint256 unsafe_cachedUserConfig = userConfig.data;
// ensure that in the target eMode (even if it's eMode 0), the assets can still be borrowed and be used as collateral
unchecked {
while (unsafe_cachedUserConfig != 0) {
(unsafe_cachedUserConfig, isBorrowed, isEnabledAsCollateral) = UserConfiguration
.getNextFlags(unsafe_cachedUserConfig);
// ensure a user can only enter or exit an eMode if all his borrowed assets can be borrowed in the target state
if (isBorrowed) {
require(
categoryId != 0
? EModeConfiguration.isReserveEnabledOnBitmap(eModeCategory.borrowableBitmap, i)
: reservesData[reservesList[i]].configuration.getBorrowingEnabled(),
Errors.InvalidDebtInEmode(reservesList[i], categoryId)
);
}
// the asset must either be collateral inside or outside of eMode
if (isEnabledAsCollateral) {
require(
getUserReserveLtv(reservesData[reservesList[i]], eModeCategory, categoryId) != 0,
Errors.InvalidCollateralInEmode(reservesList[i], categoryId)
);
}
++i;
}
}
}
/**
* @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 eModeCategories a mapping storing configurations for all efficiency mode categories
* @param userConfig the user configuration
* @param reserveConfig The reserve configuration
* @param asset Address of the reserve to be enabled as collateral
* @param categoryId The id of the users eMode category
* @return True if the asset can be activated as collateral, false otherwise
*/
function validateUseAsCollateral(
mapping(address => DataTypes.ReserveData) storage reservesData,
mapping(uint256 => address) storage reservesList,
mapping(uint8 => DataTypes.EModeCategory) storage eModeCategories,
DataTypes.UserConfigurationMap storage userConfig,
DataTypes.ReserveConfigurationMap memory reserveConfig,
address asset,
uint8 categoryId
) internal view returns (bool) {
// asset must have a non zero ltv to be activated as collateral
if (getUserReserveLtv(reservesData[asset], eModeCategories[categoryId], categoryId) == 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.
* @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 eModeCategories a mapping storing configurations for all efficiency mode categories
* @param userConfig the user configuration
* @param reserveConfig The reserve configuration
* @param asset Address of the reserve to be enabled as collateral
* @param categoryId The id of the users eMode category
* @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,
mapping(uint8 => DataTypes.EModeCategory) storage eModeCategories,
DataTypes.UserConfigurationMap storage userConfig,
DataTypes.ReserveConfigurationMap memory reserveConfig,
address asset,
uint8 categoryId
) internal view returns (bool) {
if (reserveConfig.getDebtCeiling() != 0) {
return false;
}
return
validateUseAsCollateral(
reservesData,
reservesList,
eModeCategories,
userConfig,
reserveConfig,
asset,
categoryId
);
}
/**
* @notice Returns the ltv of the user in the particular reserve
* @param reserveData The reserve configuration
* @param eModeCategoryData The users eMode category configuration
* @param categoryId The id of the users eMode category
**/
function getUserReserveLtv(
DataTypes.ReserveData storage reserveData,
DataTypes.EModeCategory storage eModeCategoryData,
uint8 categoryId
) internal view returns (uint256) {
if (
categoryId != 0 &&
EModeConfiguration.isReserveEnabledOnBitmap(
eModeCategoryData.collateralBitmap,
reserveData.id
)
) {
if (
EModeConfiguration.isReserveEnabledOnBitmap(eModeCategoryData.ltvzeroBitmap, reserveData.id)
) {
return 0;
} else {
return eModeCategoryData.ltv;
}
}
return reserveData.configuration.getLtv();
}
}