Branch data Line data Source code
1 : : // SPDX-License-Identifier: BUSL-1.1
2 : : pragma solidity ^0.8.10;
3 : :
4 : : import {DataTypes} from '../types/DataTypes.sol';
5 : : import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
6 : : import {UserConfiguration} from '../configuration/UserConfiguration.sol';
7 : : import {SafeCast} from '../../../dependencies/openzeppelin/contracts/SafeCast.sol';
8 : :
9 : : /**
10 : : * @title IsolationModeLogic library
11 : : * @author Aave
12 : : * @notice Implements the base logic for handling repayments for assets borrowed in isolation mode
13 : : */
14 : : library IsolationModeLogic {
15 : : using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
16 : : using UserConfiguration for DataTypes.UserConfigurationMap;
17 : : using SafeCast for uint256;
18 : :
19 : : // See `IPool` for descriptions
20 : : event IsolationModeTotalDebtUpdated(address indexed asset, uint256 totalDebt);
21 : :
22 : : /**
23 : : * @notice updated the isolated debt whenever a position collateralized by an isolated asset is repaid or liquidated
24 : : * @param reservesData The state of all the reserves
25 : : * @param reservesList The addresses of all the active reserves
26 : : * @param userConfig The user configuration mapping
27 : : * @param reserveCache The cached data of the reserve
28 : : * @param repayAmount The amount being repaid
29 : : */
30 : : function updateIsolatedDebtIfIsolated(
31 : : mapping(address => DataTypes.ReserveData) storage reservesData,
32 : : mapping(uint256 => address) storage reservesList,
33 : : DataTypes.UserConfigurationMap storage userConfig,
34 : : DataTypes.ReserveCache memory reserveCache,
35 : : uint256 repayAmount
36 : : ) internal {
37 : 13035 : (bool isolationModeActive, address isolationModeCollateralAddress, ) = userConfig
38 : : .getIsolationModeState(reservesData, reservesList);
39 : :
40 : 13035 : if (isolationModeActive) {
41 : 2 : uint128 isolationModeTotalDebt = reservesData[isolationModeCollateralAddress]
42 : : .isolationModeTotalDebt;
43 : :
44 : 2 : uint128 isolatedDebtRepaid = (repayAmount /
45 : : 10 **
46 : : (reserveCache.reserveConfiguration.getDecimals() -
47 : : ReserveConfiguration.DEBT_CEILING_DECIMALS)).toUint128();
48 : :
49 : : // since the debt ceiling does not take into account the interest accrued, it might happen that amount
50 : : // repaid > debt in isolation mode
51 : 2 : if (isolationModeTotalDebt <= isolatedDebtRepaid) {
52 : 1 : reservesData[isolationModeCollateralAddress].isolationModeTotalDebt = 0;
53 : 1 : emit IsolationModeTotalDebtUpdated(isolationModeCollateralAddress, 0);
54 : : } else {
55 : 1 : uint256 nextIsolationModeTotalDebt = reservesData[isolationModeCollateralAddress]
56 : : .isolationModeTotalDebt = isolationModeTotalDebt - isolatedDebtRepaid;
57 : 1 : emit IsolationModeTotalDebtUpdated(
58 : : isolationModeCollateralAddress,
59 : : nextIsolationModeTotalDebt
60 : : );
61 : : }
62 : : }
63 : : }
64 : : }
|