-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
LilFlashloan.t.sol
194 lines (140 loc) · 5.44 KB
/
LilFlashloan.t.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.10;
import { stdError } from 'forge-std/stdlib.sol';
import { Vm } from 'forge-std/Vm.sol';
import { DSTest } from 'ds-test/test.sol';
import { ERC20 } from 'solmate/tokens/ERC20.sol';
import { LilFlashloan, FlashBorrower } from '../LilFlashloan.sol';
contract User {}
contract TestToken is ERC20('Test Token', 'TEST', 18) {
function mintTo(address to, uint256 amount) public payable {
_mint(to, amount);
}
}
contract TestReceiver is FlashBorrower, DSTest {
bytes32 internal testData;
bool internal shouldRepay = true;
bool internal shouldPayFees = true;
function setTestData(bytes calldata data) public payable {
testData = bytes32(data);
}
function setRepay(bool _shouldRepay) public payable {
shouldRepay = _shouldRepay;
}
function setRespectFees(bool _shouldPayFees) public payable {
shouldPayFees = _shouldPayFees;
}
function onFlashLoan(
ERC20 token,
uint256 amount,
bytes calldata data
) external {
assertEq(testData, bytes32(data));
if (!shouldRepay) return;
token.transfer(msg.sender, amount);
if (!shouldPayFees) return;
uint256 owedFees = LilFlashloan(msg.sender).getFee(token, amount);
TestToken(address(token)).mintTo(msg.sender, owedFees);
}
}
contract LilFlashloanTest is DSTest {
User internal user;
TestToken internal token;
TestReceiver internal receiver;
LilFlashloan internal lilFlashloan;
Vm internal hevm = Vm(HEVM_ADDRESS);
event FeeUpdated(ERC20 indexed token, uint256 fee);
event Withdrawn(ERC20 indexed token, uint256 amount);
event Transfer(address indexed from, address indexed to, uint256 amount);
event Flashloaned(FlashBorrower indexed receiver, ERC20 indexed token, uint256 amount);
function setUp() public {
user = new User();
token = new TestToken();
receiver = new TestReceiver();
lilFlashloan = new LilFlashloan();
}
function testCanFlashloan() public {
token.mintTo(address(lilFlashloan), 100 ether);
hevm.expectEmit(true, true, false, true);
emit Flashloaned(receiver, token, 100 ether);
hevm.expectEmit(true, true, false, true);
emit Transfer(address(lilFlashloan), address(receiver), 100 ether);
hevm.expectEmit(true, true, false, true);
emit Transfer(address(receiver), address(lilFlashloan), 100 ether);
lilFlashloan.execute(receiver, token, 100 ether, '');
assertEq(token.balanceOf(address(lilFlashloan)), 100 ether);
}
function testDataIsForwarded() public {
receiver.setTestData('forwarded data');
token.mintTo(address(lilFlashloan), 100 ether);
lilFlashloan.execute(receiver, token, 100 ether, 'forwarded data');
}
function testCanFlashloanWithFees() public {
token.mintTo(address(lilFlashloan), 100 ether);
// set 10% fee for token
lilFlashloan.setFees(token, 10_00);
lilFlashloan.execute(receiver, token, 100 ether, '');
assertEq(token.balanceOf(address(lilFlashloan)), 110 ether);
}
function testCannotFlasloanIfNotEnoughBalance() public {
token.mintTo(address(lilFlashloan), 1 ether);
hevm.expectRevert(stdError.arithmeticError); // error comes from ERC20 impl. (solmate in this test)
lilFlashloan.execute(receiver, token, 2 ether, '');
assertEq(token.balanceOf(address(lilFlashloan)), 1 ether);
}
function testFlashloanRevertsIfNotRepaid() public {
receiver.setRepay(false);
token.mintTo(address(lilFlashloan), 100 ether);
hevm.expectRevert(abi.encodeWithSignature('TokensNotReturned()'));
lilFlashloan.execute(receiver, token, 100 ether, '');
assertEq(token.balanceOf(address(lilFlashloan)), 100 ether);
}
function testFlashloanRevertsIfNotFeesNotPaid() public {
receiver.setRespectFees(false);
// set 10% fee for token
lilFlashloan.setFees(token, 10_00);
token.mintTo(address(lilFlashloan), 100 ether);
hevm.expectRevert(abi.encodeWithSignature('TokensNotReturned()'));
lilFlashloan.execute(receiver, token, 100 ether, '');
assertEq(token.balanceOf(address(lilFlashloan)), 100 ether);
}
function testManagerCanSetFees() public {
assertEq(lilFlashloan.fees(token), 0);
hevm.expectEmit(true, false, false, true);
emit FeeUpdated(token, 10_00);
// set 10% fee for token
lilFlashloan.setFees(token, 10_00);
assertEq(lilFlashloan.fees(token), 10_00);
}
function testCannotSetFeesHigherThan100Percent() public {
assertEq(lilFlashloan.fees(token), 0);
hevm.expectRevert(abi.encodeWithSignature('InvalidPercentage()'));
lilFlashloan.setFees(token, 101_00);
assertEq(lilFlashloan.fees(token), 0);
}
function testNonManagerCannotSetFees() public {
assertEq(lilFlashloan.fees(token), 0);
hevm.prank(address(user));
hevm.expectRevert(abi.encodeWithSignature('Unauthorized()'));
lilFlashloan.setFees(token, 10_00);
assertEq(lilFlashloan.fees(token), 0);
}
function testManagerCanWithdrawTokens() public {
token.mintTo(address(lilFlashloan), 10 ether);
assertEq(token.balanceOf(address(this)), 0);
hevm.expectEmit(true, false, false, true);
emit Withdrawn(token, 10 ether);
lilFlashloan.withdraw(token, 10 ether);
assertEq(token.balanceOf(address(this)), 10 ether);
assertEq(token.balanceOf(address(lilFlashloan)), 0);
}
function testNonManagerCannotWithdrawTokens() public {
token.mintTo(address(lilFlashloan), 10 ether);
assertEq(token.balanceOf(address(user)), 0);
hevm.prank(address(user));
hevm.expectRevert(abi.encodeWithSignature('Unauthorized()'));
lilFlashloan.withdraw(token, 10 ether);
assertEq(token.balanceOf(address(user)), 0);
assertEq(token.balanceOf(address(lilFlashloan)), 10 ether);
}
}