mirror of
				https://github.com/Instadapp/aave-protocol-v2.git
				synced 2024-07-29 21:47:30 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			86 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Solidity
		
	
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Solidity
		
	
	
	
	
	
// SPDX-License-Identifier: agpl-3.0
 | 
						|
pragma solidity 0.6.12;
 | 
						|
 | 
						|
import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
 | 
						|
import {IERC20} from '../../dependencies/openzeppelin/contracts/IERC20.sol';
 | 
						|
 | 
						|
import {FlashLoanReceiverBase} from '../../flashloan/base/FlashLoanReceiverBase.sol';
 | 
						|
import {MintableERC20} from '../tokens/MintableERC20.sol';
 | 
						|
import {SafeERC20} from '../../dependencies/openzeppelin/contracts/SafeERC20.sol';
 | 
						|
import {ILendingPoolAddressesProvider} from '../../interfaces/ILendingPoolAddressesProvider.sol';
 | 
						|
 | 
						|
contract MockFlashLoanReceiver is FlashLoanReceiverBase {
 | 
						|
  using SafeERC20 for IERC20;
 | 
						|
 | 
						|
  ILendingPoolAddressesProvider internal _provider;
 | 
						|
 | 
						|
  event ExecutedWithFail(address[] _assets, uint256[] _amounts, uint256[] _premiums);
 | 
						|
  event ExecutedWithSuccess(address[] _assets, uint256[] _amounts, uint256[] _premiums);
 | 
						|
 | 
						|
  bool _failExecution;
 | 
						|
  uint256 _amountToApprove;
 | 
						|
  bool _simulateEOA;
 | 
						|
 | 
						|
  constructor(ILendingPoolAddressesProvider provider) public FlashLoanReceiverBase(provider) {}
 | 
						|
 | 
						|
  function setFailExecutionTransfer(bool fail) public {
 | 
						|
    _failExecution = fail;
 | 
						|
  }
 | 
						|
 | 
						|
  function setAmountToApprove(uint256 amountToApprove) public {
 | 
						|
    _amountToApprove = amountToApprove;
 | 
						|
  }
 | 
						|
 | 
						|
  function setSimulateEOA(bool flag) public {
 | 
						|
    _simulateEOA = flag;
 | 
						|
  }
 | 
						|
 | 
						|
  function amountToApprove() public view returns (uint256) {
 | 
						|
    return _amountToApprove;
 | 
						|
  }
 | 
						|
 | 
						|
  function simulateEOA() public view returns (bool) {
 | 
						|
    return _simulateEOA;
 | 
						|
  }
 | 
						|
 | 
						|
  function executeOperation(
 | 
						|
    address[] memory assets,
 | 
						|
    uint256[] memory amounts,
 | 
						|
    uint256[] memory premiums,
 | 
						|
    address initiator,
 | 
						|
    bytes memory params
 | 
						|
  ) public override returns (bool) {
 | 
						|
    params;
 | 
						|
    initiator;
 | 
						|
 | 
						|
    if (_failExecution) {
 | 
						|
      emit ExecutedWithFail(assets, amounts, premiums);
 | 
						|
      return !_simulateEOA;
 | 
						|
    }
 | 
						|
 | 
						|
    for (uint256 i = 0; i < assets.length; i++) {
 | 
						|
      //mint to this contract the specific amount
 | 
						|
      MintableERC20 token = MintableERC20(assets[i]);
 | 
						|
 | 
						|
      //check the contract has the specified balance
 | 
						|
      require(
 | 
						|
        amounts[i] <= IERC20(assets[i]).balanceOf(address(this)),
 | 
						|
        'Invalid balance for the contract'
 | 
						|
      );
 | 
						|
 | 
						|
      uint256 amountToReturn = (_amountToApprove != 0)
 | 
						|
        ? _amountToApprove
 | 
						|
        : amounts[i].add(premiums[i]);
 | 
						|
      //execution does not fail - mint tokens and return them to the _destination
 | 
						|
 | 
						|
      token.mint(premiums[i]);
 | 
						|
 | 
						|
      IERC20(assets[i]).approve(address(LENDING_POOL), amountToReturn);
 | 
						|
    }
 | 
						|
 | 
						|
    emit ExecutedWithSuccess(assets, amounts, premiums);
 | 
						|
 | 
						|
    return true;
 | 
						|
  }
 | 
						|
}
 |