-
Notifications
You must be signed in to change notification settings - Fork 1
/
contractExample.sol
54 lines (47 loc) · 1.7 KB
/
contractExample.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
interface IERC20 {
function approve(address spender, uint256 amount) external returns (bool);
}
interface IERC3156FlashBorrower {
/**
* @dev Receive a flash loan.
* @param initiator The initiator of the loan.
* @param token The loan currency.
* @param amount The amount of tokens lent.
* @param fee The additional amount of tokens to repay.
* @param data Arbitrary data structure, intended to contain user-defined parameters.
* @return The keccak256 hash of "ERC3156FlashBorrower.onFlashLoan"
*/
function onFlashLoan(
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external returns (bytes32);
}
/*
* FlashBorrowerExample is a simple smart contract that enables
* to borrow and returns a flash loan.
*/
contract FlashBorrowerExample is IERC3156FlashBorrower {
uint256 MAX_INT = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
// @dev ERC-3156 Flash loan callback
function onFlashLoan(
// If compiler error, ignore it
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external override returns (bytes32) {
// Set the allowance to payback the flash loan
IERC20(token).approve(msg.sender, MAX_INT);
// Build your trading business logic here
// e.g., sell on uniswapv2
// e.g., buy on uniswapv3
// Return success to the lender, he will transfer get the funds back if allowance is set accordingly
return keccak256('ERC3156FlashBorrower.onFlashLoan');
}
}