2020-05-29 16:45:37 +00:00
|
|
|
// SPDX-License-Identifier: agpl-3.0
|
|
|
|
pragma solidity ^0.6.8;
|
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
import '@openzeppelin/contracts/math/SafeMath.sol';
|
|
|
|
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
import '../../flashloan/base/FlashLoanReceiverBase.sol';
|
|
|
|
import '../tokens/MintableERC20.sol';
|
2020-08-12 17:36:58 +00:00
|
|
|
import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol';
|
2020-05-29 16:45:37 +00:00
|
|
|
|
|
|
|
contract MockFlashLoanReceiver is FlashLoanReceiverBase {
|
2020-07-13 08:54:08 +00:00
|
|
|
using SafeMath for uint256;
|
2020-08-12 17:36:58 +00:00
|
|
|
using SafeERC20 for IERC20;
|
2020-07-13 08:54:08 +00:00
|
|
|
|
|
|
|
event ExecutedWithFail(address _reserve, uint256 _amount, uint256 _fee);
|
|
|
|
event ExecutedWithSuccess(address _reserve, uint256 _amount, uint256 _fee);
|
|
|
|
|
|
|
|
bool failExecution = false;
|
|
|
|
|
|
|
|
constructor(ILendingPoolAddressesProvider _provider) public FlashLoanReceiverBase(_provider) {}
|
|
|
|
|
|
|
|
function setFailExecutionTransfer(bool _fail) public {
|
|
|
|
failExecution = _fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
function executeOperation(
|
|
|
|
address _reserve,
|
2020-07-13 10:24:36 +00:00
|
|
|
address _destination,
|
2020-07-13 08:54:08 +00:00
|
|
|
uint256 _amount,
|
|
|
|
uint256 _fee,
|
|
|
|
bytes memory _params
|
|
|
|
) public override {
|
|
|
|
//mint to this contract the specific amount
|
|
|
|
MintableERC20 token = MintableERC20(_reserve);
|
|
|
|
|
|
|
|
//check the contract has the specified balance
|
|
|
|
require(
|
2020-08-12 17:36:58 +00:00
|
|
|
_amount <= IERC20(_reserve).balanceOf(address(this)),
|
2020-07-13 08:54:08 +00:00
|
|
|
'Invalid balance for the contract'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (failExecution) {
|
|
|
|
emit ExecutedWithFail(_reserve, _amount, _fee);
|
|
|
|
return;
|
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-12 17:36:58 +00:00
|
|
|
token.mint(_fee);
|
2020-07-13 13:19:47 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
//returning amount + fee to the destination
|
2020-07-13 10:24:36 +00:00
|
|
|
transferFundsBackInternal(_reserve, _destination, _amount.add(_fee));
|
2020-07-13 13:19:47 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
emit ExecutedWithSuccess(_reserve, _amount, _fee);
|
|
|
|
}
|
2020-06-02 14:16:22 +00:00
|
|
|
}
|