diff --git a/contracts/interfaces/ILendingPool.sol b/contracts/interfaces/ILendingPool.sol index 64f726c0..1178d350 100644 --- a/contracts/interfaces/ILendingPool.sol +++ b/contracts/interfaces/ILendingPool.sol @@ -167,6 +167,16 @@ interface ILendingPool { uint256 variableBorrowIndex ); + /** + * @dev Emitted when the protocol treasury receives minted aTokens from the accrued interest. + * @param reserve the address of the reserve + * @param amountMinted the amount minted to the treasury + **/ + event TreasuryUpdated( + address indexed reserve, + uint256 amountMinted + ); + /** * @dev Deposits an `amount` of underlying asset into the reserve, receiving in return overlying aTokens. * - E.g. User deposits 100 USDC and gets in return 100 aUSDC diff --git a/contracts/protocol/lendingpool/LendingPool.sol b/contracts/protocol/lendingpool/LendingPool.sol index 531f007b..1425b0ff 100644 --- a/contracts/protocol/lendingpool/LendingPool.sol +++ b/contracts/protocol/lendingpool/LendingPool.sol @@ -479,6 +479,26 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage } } + /** + * @dev Mints the assets accrued through the reserve factor to the treasury in the form of aTokens + **/ + function mintToTreasury() public { + for (uint256 i = 0; i < _reservesCount; i++) { + address reserveAddress = _reservesList[i]; + DataTypes.ReserveData storage reserve = _reserves[reserveAddress]; + uint256 accruedToTreasury = reserve.accruedToTreasury; + + if (accruedToTreasury != 0) { + uint256 normalizedIncome = reserve.getNormalizedIncome(); + uint256 amountToMint = accruedToTreasury.rayMul(normalizedIncome); + IAToken(reserve.aTokenAddress).mintToTreasury(amountToMint, normalizedIncome); + + reserve.accruedToTreasury = 0; + emit TreasuryUpdated(reserveAddress, amountToMint); + } + } + } + /** * @dev Returns the state and configuration of the reserve * @param asset The address of the underlying asset of the reserve diff --git a/contracts/protocol/libraries/logic/ReserveLogic.sol b/contracts/protocol/libraries/logic/ReserveLogic.sol index 2b5b2cf4..485ad0d3 100644 --- a/contracts/protocol/libraries/logic/ReserveLogic.sol +++ b/contracts/protocol/libraries/logic/ReserveLogic.sol @@ -320,7 +320,7 @@ library ReserveLogic { vars.amountToMint = vars.totalDebtAccrued.percentMul(vars.reserveFactor); if (vars.amountToMint != 0) { - IAToken(reserve.aTokenAddress).mintToTreasury(vars.amountToMint, newLiquidityIndex); + reserve.accruedToTreasury = reserve.accruedToTreasury.add(vars.amountToMint.rayDiv(newLiquidityIndex)); } } diff --git a/contracts/protocol/libraries/types/DataTypes.sol b/contracts/protocol/libraries/types/DataTypes.sol index a19e5efc..ffec1d20 100644 --- a/contracts/protocol/libraries/types/DataTypes.sol +++ b/contracts/protocol/libraries/types/DataTypes.sol @@ -25,6 +25,8 @@ library DataTypes { address interestRateStrategyAddress; //the id of the reserve. Represents the position in the list of the active reserves uint8 id; + //the current treasury balance, scaled + uint256 accruedToTreasury; } struct ReserveConfigurationMap {