// SPDX-License-Identifier: BUSL-1.1 pragma solidity ^0.8.10; import {GPv2SafeERC20} from '../../../dependencies/gnosis/contracts/GPv2SafeERC20.sol'; import {SafeCast} from 'openzeppelin-contracts/contracts/utils/math/SafeCast.sol'; import {IERC20} from 'openzeppelin-contracts/contracts/token/ERC20/IERC20.sol'; import {IAToken} from '../../../interfaces/IAToken.sol'; import {IPool} from '../../../interfaces/IPool.sol'; import {IFlashLoanReceiver} from '../../../misc/flashloan/interfaces/IFlashLoanReceiver.sol'; import {IFlashLoanSimpleReceiver} from '../../../misc/flashloan/interfaces/IFlashLoanSimpleReceiver.sol'; import {IPoolAddressesProvider} from '../../../interfaces/IPoolAddressesProvider.sol'; import {ReserveConfiguration} from '../configuration/ReserveConfiguration.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 {ValidationLogic} from './ValidationLogic.sol'; import {BorrowLogic} from './BorrowLogic.sol'; import {ReserveLogic} from './ReserveLogic.sol'; /** * @title FlashLoanLogic library * @author Aave * @notice Implements the logic for the flash loans */ library FlashLoanLogic { using ReserveLogic for DataTypes.ReserveCache; using ReserveLogic for DataTypes.ReserveData; using GPv2SafeERC20 for IERC20; using ReserveConfiguration for DataTypes.ReserveConfigurationMap; using TokenMath for uint256; using PercentageMath for uint256; using SafeCast for uint256; // Helper struct for internal variables used in the `executeFlashLoan` function struct FlashLoanLocalVars { IFlashLoanReceiver receiver; address currentAsset; uint256 currentAmount; uint256[] totalPremiums; uint256 flashloanPremium; } /** * @notice Implements the flashloan feature that allow users to access liquidity of the pool for one transaction * as long as the amount taken plus fee is returned or debt is opened. * @dev For authorized flashborrowers the fee is waived * @dev At the end of the transaction the pool will pull amount borrowed + fee from the receiver, * if the receiver have not approved the pool the transaction will revert. * @dev Emits the `FlashLoan()` event * @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 user configuration mapping that tracks the supplied/borrowed assets * @param params The additional parameters needed to execute the flashloan function */ function executeFlashLoan( mapping(address => DataTypes.ReserveData) storage reservesData, mapping(uint256 => address) storage reservesList, mapping(uint8 => DataTypes.EModeCategory) storage eModeCategories, DataTypes.UserConfigurationMap storage userConfig, DataTypes.FlashloanParams memory params ) external { // The usual action flow (cache -> updateState -> validation -> changeState -> updateRates) // is altered to (validation -> user payload -> cache -> updateState -> changeState -> updateRates) for flashloans. // This is done to protect against reentrance and rate manipulation within the user specified payload. ValidationLogic.validateFlashloan(reservesData, params.assets, params.amounts); FlashLoanLocalVars memory vars; vars.totalPremiums = new uint256[](params.assets.length); vars.receiver = IFlashLoanReceiver(params.receiverAddress); vars.flashloanPremium = params.isAuthorizedFlashBorrower ? 0 : params.flashLoanPremium; for (uint256 i = 0; i < params.assets.length; i++) { vars.currentAmount = params.amounts[i]; vars.totalPremiums[i] = DataTypes.InterestRateMode(params.interestRateModes[i]) == DataTypes.InterestRateMode.NONE ? vars.currentAmount.percentMulCeil(vars.flashloanPremium) : 0; reservesData[params.assets[i]].virtualUnderlyingBalance -= vars.currentAmount.toUint128(); IAToken(reservesData[params.assets[i]].aTokenAddress).transferUnderlyingTo( params.receiverAddress, vars.currentAmount ); } require( vars.receiver.executeOperation( params.assets, params.amounts, vars.totalPremiums, params.user, params.params ), Errors.InvalidFlashloanExecutorReturn() ); for (uint256 i = 0; i < params.assets.length; i++) { vars.currentAsset = params.assets[i]; vars.currentAmount = params.amounts[i]; if ( DataTypes.InterestRateMode(params.interestRateModes[i]) == DataTypes.InterestRateMode.NONE ) { _handleFlashLoanRepayment( reservesData[vars.currentAsset], DataTypes.FlashLoanRepaymentParams({ user: params.user, asset: vars.currentAsset, interestRateStrategyAddress: params.interestRateStrategyAddress, receiverAddress: params.receiverAddress, amount: vars.currentAmount, totalPremium: vars.totalPremiums[i], referralCode: params.referralCode }) ); } else { // If the user chose to not return the funds, the system checks if there is enough collateral and // eventually opens a debt position BorrowLogic.executeBorrow( reservesData, reservesList, eModeCategories, userConfig, DataTypes.ExecuteBorrowParams({ asset: vars.currentAsset, interestRateStrategyAddress: params.interestRateStrategyAddress, user: params.user, onBehalfOf: params.onBehalfOf, amount: vars.currentAmount, interestRateMode: DataTypes.InterestRateMode(params.interestRateModes[i]), referralCode: params.referralCode, releaseUnderlying: false, oracle: IPoolAddressesProvider(params.addressesProvider).getPriceOracle(), userEModeCategory: IPool(params.pool).getUserEMode(params.onBehalfOf).toUint8() }) ); // no premium is paid when taking on the flashloan as debt emit IPool.FlashLoan( params.receiverAddress, params.user, vars.currentAsset, vars.currentAmount, DataTypes.InterestRateMode(params.interestRateModes[i]), 0, params.referralCode ); } } } /** * @notice Implements the simple flashloan feature that allow users to access liquidity of ONE reserve for one * transaction as long as the amount taken plus fee is returned. * @dev Does not waive fee for approved flashborrowers nor allow taking on debt instead of repaying to save gas * @dev At the end of the transaction the pool will pull amount borrowed + fee from the receiver, * if the receiver have not approved the pool the transaction will revert. * @dev Emits the `FlashLoan()` event * @param reserve The state of the flashloaned reserve * @param params The additional parameters needed to execute the simple flashloan function */ function executeFlashLoanSimple( DataTypes.ReserveData storage reserve, DataTypes.FlashloanSimpleParams memory params ) external { // The usual action flow (cache -> updateState -> validation -> changeState -> updateRates) // is altered to (validation -> user payload -> cache -> updateState -> changeState -> updateRates) for flashloans. // This is done to protect against reentrance and rate manipulation within the user specified payload. ValidationLogic.validateFlashloanSimple(reserve, params.amount); IFlashLoanSimpleReceiver receiver = IFlashLoanSimpleReceiver(params.receiverAddress); uint256 totalPremium = params.amount.percentMulCeil(params.flashLoanPremium); reserve.virtualUnderlyingBalance -= params.amount.toUint128(); IAToken(reserve.aTokenAddress).transferUnderlyingTo(params.receiverAddress, params.amount); require( receiver.executeOperation( params.asset, params.amount, totalPremium, params.user, params.params ), Errors.InvalidFlashloanExecutorReturn() ); _handleFlashLoanRepayment( reserve, DataTypes.FlashLoanRepaymentParams({ user: params.user, asset: params.asset, interestRateStrategyAddress: params.interestRateStrategyAddress, receiverAddress: params.receiverAddress, amount: params.amount, totalPremium: totalPremium, referralCode: params.referralCode }) ); } /** * @notice Handles repayment of flashloaned assets + premium * @dev Will pull the amount + premium from the receiver, so must have approved pool * @param reserve The state of the flashloaned reserve * @param params The additional parameters needed to execute the repayment function */ function _handleFlashLoanRepayment( DataTypes.ReserveData storage reserve, DataTypes.FlashLoanRepaymentParams memory params ) internal { uint256 amountPlusPremium = params.amount + params.totalPremium; DataTypes.ReserveCache memory reserveCache = reserve.cache(); reserve.updateState(reserveCache); reserve.accruedToTreasury += params .totalPremium .getATokenMintScaledAmount(reserveCache.nextLiquidityIndex) .toUint128(); reserve.updateInterestRatesAndVirtualBalance( reserveCache, params.asset, amountPlusPremium, 0, params.interestRateStrategyAddress ); IERC20(params.asset).safeTransferFrom( params.receiverAddress, reserveCache.aTokenAddress, amountPlusPremium ); emit IPool.FlashLoan( params.receiverAddress, params.user, params.asset, params.amount, DataTypes.InterestRateMode.NONE, params.totalPremium, params.referralCode ); } }