-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMySimpleFlashloanV3.sol
85 lines (71 loc) · 2.66 KB
/
MySimpleFlashloanV3.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.10;
import {
IPoolAddressesProvider
} from "https://github.com/aave/aave-v3-core/contracts/interfaces/IPoolAddressesProvider.sol";
import { IPool } from "https://github.com/aave/aave-v3-core/contracts/interfaces/IPool.sol";
import { IFlashLoanSimpleReceiver } from "https://github.com/aave/aave-v3-core/contracts/flashloan/interfaces/IFlashLoanSimpleReceiver.sol";
import { IERC20 } from "https://github.com/aave/aave-v3-core/contracts/dependencies/openzeppelin/contracts/IERC20.sol";
import { IFaucet } from "https://github.com/aave/aave-v3-periphery/contracts/mocks/testnet-helpers/IFaucet.sol";
abstract contract FlashLoanSimpleReceiverBase is IFlashLoanSimpleReceiver {
IPoolAddressesProvider public immutable override ADDRESSES_PROVIDER;
IPool public immutable override POOL;
IFaucet public immutable FAUCET;
constructor(IPoolAddressesProvider provider, IFaucet faucet) {
ADDRESSES_PROVIDER = provider;
POOL = IPool(provider.getPool());
FAUCET = faucet;
}
}
contract MySimpleFlashLoanV3 is FlashLoanSimpleReceiverBase {
constructor(IPoolAddressesProvider _poolAddressesProvider, IFaucet _faucet) FlashLoanSimpleReceiverBase(_poolAddressesProvider, _faucet) {}
// Modifier to restrict access to the Pool
modifier onlyPool() {
require(msg.sender == address(POOL), "Caller is not the Pool");
_;
}
/**
This function is called after your contract has received the borrowed amount
*/
function executeOperation(
address asset,
uint256 amount,
uint256 premium,
address initiator,
bytes calldata params
)
external
override
onlyPool
returns (bool)
{
//
// This contract now has the funds requested.
// Your logic goes here.
//
// At the end of your logic above, this contract owes
// the flashloaned amounts + premiums.
// Therefore ensure your contract has enough to repay
// these amounts.
// Approve the Pool contract allowance to *pull* the owed amount
uint amountOwed = amount + premium;
FAUCET.mint(asset, address(this), premium);
IERC20(asset).approve(address(POOL), amountOwed);
return true;
}
function executeFlashLoan(
address underlyingToken,
uint256 amount
) public {
address receiverAddress = address(this);
bytes memory params = "";
uint16 referralCode = 0;
POOL.flashLoanSimple(
receiverAddress,
underlyingToken,
amount,
params,
referralCode
);
}
}