2020-05-29 16:45:37 +00:00
|
|
|
// SPDX-License-Identifier: agpl-3.0
|
|
|
|
pragma solidity ^0.6.8;
|
|
|
|
|
2020-10-15 13:25:27 +00:00
|
|
|
import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
|
|
|
|
import {IERC20} from '../../dependencies/openzeppelin/contracts/IERC20.sol';
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-08-20 10:44:51 +00:00
|
|
|
import {FlashLoanReceiverBase} from '../../flashloan/base/FlashLoanReceiverBase.sol';
|
|
|
|
import {MintableERC20} from '../tokens/MintableERC20.sol';
|
2020-10-15 13:25:27 +00:00
|
|
|
import {SafeERC20} from '../../dependencies/openzeppelin/contracts/SafeERC20.sol';
|
2020-08-20 10:44:51 +00:00
|
|
|
import {ILendingPoolAddressesProvider} from '../../interfaces/ILendingPoolAddressesProvider.sol';
|
2020-05-29 16:45:37 +00:00
|
|
|
|
|
|
|
contract MockFlashLoanReceiver is FlashLoanReceiverBase {
|
2020-08-12 17:36:58 +00:00
|
|
|
using SafeERC20 for IERC20;
|
2020-07-13 08:54:08 +00:00
|
|
|
|
2020-08-23 16:38:34 +00:00
|
|
|
ILendingPoolAddressesProvider internal _provider;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
event ExecutedWithFail(address _reserve, uint256 _amount, uint256 _fee);
|
|
|
|
event ExecutedWithSuccess(address _reserve, uint256 _amount, uint256 _fee);
|
|
|
|
|
2020-08-25 12:50:07 +00:00
|
|
|
bool _failExecution;
|
|
|
|
uint256 _amountToApprove;
|
2020-10-07 14:20:32 +00:00
|
|
|
bool _simulateEOA;
|
2020-07-13 08:54:08 +00:00
|
|
|
|
2020-08-23 16:38:34 +00:00
|
|
|
constructor(ILendingPoolAddressesProvider provider) public FlashLoanReceiverBase(provider) {}
|
2020-07-13 08:54:08 +00:00
|
|
|
|
2020-08-25 12:50:07 +00:00
|
|
|
function setFailExecutionTransfer(bool fail) public {
|
|
|
|
_failExecution = fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setAmountToApprove(uint256 amountToApprove) public {
|
|
|
|
_amountToApprove = amountToApprove;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:20:32 +00:00
|
|
|
function setSimulateEOA(bool flag) public {
|
|
|
|
_simulateEOA = flag;
|
|
|
|
}
|
|
|
|
|
2020-08-25 12:50:07 +00:00
|
|
|
function amountToApprove() public view returns (uint256) {
|
|
|
|
return _amountToApprove;
|
2020-07-13 08:54:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 13:19:27 +00:00
|
|
|
function simulateEOA() public view returns (bool) {
|
2020-10-07 14:20:32 +00:00
|
|
|
return _simulateEOA;
|
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
function executeOperation(
|
2020-08-23 16:38:34 +00:00
|
|
|
address reserve,
|
|
|
|
uint256 amount,
|
|
|
|
uint256 fee,
|
|
|
|
bytes memory params
|
2020-10-12 13:19:27 +00:00
|
|
|
) public override returns (bool) {
|
2020-10-07 14:20:32 +00:00
|
|
|
params;
|
2020-07-13 08:54:08 +00:00
|
|
|
//mint to this contract the specific amount
|
2020-08-23 16:38:34 +00:00
|
|
|
MintableERC20 token = MintableERC20(reserve);
|
2020-07-13 08:54:08 +00:00
|
|
|
|
|
|
|
//check the contract has the specified balance
|
2020-08-25 12:50:07 +00:00
|
|
|
require(amount <= IERC20(reserve).balanceOf(address(this)), 'Invalid balance for the contract');
|
|
|
|
|
|
|
|
uint256 amountToReturn = (_amountToApprove != 0) ? _amountToApprove : amount.add(fee);
|
2020-07-13 08:54:08 +00:00
|
|
|
|
2020-08-25 12:50:07 +00:00
|
|
|
if (_failExecution) {
|
2020-08-23 16:38:34 +00:00
|
|
|
emit ExecutedWithFail(reserve, amount, fee);
|
2020-10-12 10:00:49 +00:00
|
|
|
return !_simulateEOA;
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
//execution does not fail - mint tokens and return them to the _destination
|
|
|
|
//note: if the reserve is eth, the mock contract must receive at least _fee ETH before calling executeOperation
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-08-23 16:38:34 +00:00
|
|
|
token.mint(fee);
|
2020-07-13 13:19:47 +00:00
|
|
|
|
2020-08-25 12:50:07 +00:00
|
|
|
IERC20(reserve).approve(_addressesProvider.getLendingPool(), amountToReturn);
|
2020-07-13 13:19:47 +00:00
|
|
|
|
2020-08-23 16:38:34 +00:00
|
|
|
emit ExecutedWithSuccess(reserve, amount, fee);
|
2020-10-07 14:20:32 +00:00
|
|
|
|
2020-10-12 10:00:49 +00:00
|
|
|
return true;
|
2020-07-13 08:54:08 +00:00
|
|
|
}
|
2020-06-02 14:16:22 +00:00
|
|
|
}
|