-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathFlowScheduler.t.sol
More file actions
373 lines (344 loc) · 13.3 KB
/
FlowScheduler.t.sol
File metadata and controls
373 lines (344 loc) · 13.3 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import { ISuperToken } from "@superfluid-finance/ethereum-contracts/contracts/interfaces/superfluid/ISuperToken.sol";
import { FlowOperatorDefinitions } from "@superfluid-finance/ethereum-contracts/contracts/interfaces/superfluid/ISuperfluid.sol";
import { IFlowScheduler } from "./../contracts/interface/IFlowScheduler.sol";
import { FlowScheduler } from "./../contracts/FlowScheduler.sol";
import { FoundrySuperfluidTester } from "@superfluid-finance/ethereum-contracts/test/foundry/FoundrySuperfluidTester.t.sol";
import { SuperToken } from "@superfluid-finance/ethereum-contracts/contracts/superfluid/SuperToken.sol";
import { SuperTokenV1Library } from "@superfluid-finance/ethereum-contracts/contracts/apps/SuperTokenV1Library.sol";
/// @title FlowSchedulerTests
/// @notice Look at me , I am the captain now - Elvijs
contract FlowSchedulerTest is FoundrySuperfluidTester {
event FlowScheduleCreated(
ISuperToken indexed superToken,
address indexed sender,
address indexed receiver,
uint32 startDate,
uint32 startMaxDelay,
int96 flowRate,
uint32 endDate,
uint256 startAmount,
bytes userData
);
event FlowScheduleDeleted(
ISuperToken indexed superToken,
address indexed sender,
address indexed receiver
);
event CreateFlowExecuted(
ISuperToken indexed superToken,
address indexed sender,
address indexed receiver,
uint32 startDate,
uint32 startMaxDelay,
int96 flowRate,
uint256 startAmount,
bytes userData
);
event DeleteFlowExecuted(
ISuperToken indexed superToken,
address indexed sender,
address indexed receiver,
uint32 endDate,
bytes userData
);
/// @dev This is required by solidity for using the SuperTokenV1Library in the tester
using SuperTokenV1Library for SuperToken;
constructor() FoundrySuperfluidTester(3) {}
FlowScheduler internal flowScheduler;
function setUp() override public virtual {
super.setUp();
flowScheduler = new FlowScheduler(sf.host);
}
function getHashID(
address _superToken,
address sender,
address receiver
)
public pure returns (bytes32)
{
return keccak256(abi.encodePacked(
_superToken,
sender,
receiver
));
}
/// @dev Constants for Testing
uint32 internal defaultStartDate = uint32(block.timestamp + 1);
uint32 testNumber;
int96 defaultFlowRate = int96(1000);
uint32 defaultStartMaxDelay = uint32(60);
uint256 defaultStartAmount = 500;
function testCreateFlowScheduleWithExplicitTimeWindow() public {
vm.prank(alice);
vm.expectEmit(true, true, true, true);
uint32 defaultEndDate = defaultStartDate + uint32(3600);
emit FlowScheduleCreated(
superToken,
alice,
bob,
defaultStartDate,
defaultStartMaxDelay,
defaultFlowRate,
defaultEndDate,
defaultStartAmount,
""
);
vm.expectCall(
address(flowScheduler),
abi.encodeCall(
flowScheduler.createFlowSchedule,
(
superToken,
bob,
defaultStartDate,
defaultStartMaxDelay,
defaultFlowRate,
defaultStartAmount,
defaultEndDate,
"",
""
)
)
);
flowScheduler.createFlowSchedule(
superToken,
bob,
defaultStartDate,
defaultStartMaxDelay,
defaultFlowRate,
defaultStartAmount,
defaultEndDate,
"",
""
);
(uint32 startDate,,uint32 endDate,,,) = flowScheduler.flowSchedules(getHashID(address(superToken),alice,bob));
assertTrue(startDate != 0 || endDate != 0, "Flow schedule not created");
}
function testCreateFlowScheduleWithZeroTimes() public {
vm.prank(alice);
vm.expectEmit(true, true, true, true);
uint32 startMaxDelay = 0;
uint32 defaultEndDate = 3600;
emit FlowScheduleCreated(
superToken,
alice,
bob,
0,
startMaxDelay,
defaultFlowRate,
defaultEndDate,
defaultStartAmount,
""
);
vm.expectCall(
address(flowScheduler),
abi.encodeCall(
flowScheduler.createFlowSchedule,
(
superToken,
bob,
0,
startMaxDelay,
defaultFlowRate,
defaultStartAmount,
defaultEndDate,
"",
""
)
)
);
flowScheduler.createFlowSchedule(
superToken,
bob,
0,
startMaxDelay,
defaultFlowRate,
defaultStartAmount,
defaultEndDate,
"",
""
);
(uint32 startDate,,uint32 endDate,,,) = flowScheduler.flowSchedules(getHashID(address(superToken),alice,bob));
assertTrue(startDate != 0 || endDate != 0, "Flow schedule not created");
}
function testCannotCreateFlowScheduleWhenSenderSameAsReceiver() public {
// Expect revert on receiver same as sender.
vm.prank(alice);
vm.expectRevert(IFlowScheduler.AccountInvalid.selector);
flowScheduler.createFlowSchedule(
superToken,
alice,
defaultStartDate,
uint32(60),
defaultFlowRate,
defaultStartAmount,
defaultStartDate + uint32(3600),
"",
""
);
}
function testCannotCreateFlowScheduleWhenTimeWindowInvalid() public {
vm.expectRevert(IFlowScheduler.TimeWindowInvalid.selector);
flowScheduler.createFlowSchedule(
superToken,
bob,
uint32(0),
uint32(60),
int96(1000),
defaultStartAmount,
uint32(0),
"",
""
);
}
function testCannotExecuteCreateFlowWhenScheduleDNE() public {
vm.prank(admin);
// Expect revert on when schedule does not exist.
vm.expectRevert(IFlowScheduler.TimeWindowInvalid.selector);
vm.warp(100);
flowScheduler.executeCreateFlow(superToken,alice,bob,"");
}
function testCannotExecuteDeleteFlowWhenScheduleDNE() public {
vm.prank(admin);
// Expect revert on when schedule does not exist.
vm.expectRevert(IFlowScheduler.TimeWindowInvalid.selector);
flowScheduler.executeDeleteFlow(superToken,alice,bob,"");
}
function testCannotExecuteCreateFlowWithInvalidPermissions() public {
vm.prank(bob);
superToken.increaseAllowance(address(flowScheduler), type(uint256).max);
vm.prank(bob);
flowScheduler.createFlowSchedule(
superToken, alice, defaultStartDate, uint32(1000), int96(1000), defaultStartAmount, defaultStartDate + uint32(3600), "", ""
);
vm.expectRevert(0xa3eab6ac); // error CFA_ACL_OPERATOR_NO_CREATE_PERMISSIONS() -> 0xa3eab6ac
vm.warp(defaultStartDate + 1000);
vm.prank(admin);
flowScheduler.executeCreateFlow(superToken, bob,alice,"");
}
function testCannotExecuteCreateFlowWhenTimeWindowInvalid() public {
// Expect revert on when start and end are both 0.
vm.expectRevert(IFlowScheduler.TimeWindowInvalid.selector);
vm.startPrank(admin);
flowScheduler.executeCreateFlow(superToken,alice,bob,"");
// Expect revert on when start time is exactly block.timestamp.
vm.expectRevert(IFlowScheduler.TimeWindowInvalid.selector);
flowScheduler.executeCreateFlow(superToken,alice,bob,"");
// Expect revert on when start time is after end time.
vm.expectRevert(IFlowScheduler.TimeWindowInvalid.selector);
flowScheduler.executeCreateFlow(superToken,alice,bob,"");
// Expect revert on when start time is empty and duration is not.
vm.expectRevert(IFlowScheduler.TimeWindowInvalid.selector);
flowScheduler.executeCreateFlow(superToken,alice,bob,"");
vm.stopPrank();
}
function testExecuteCreateFlow() public {
vm.startPrank(alice);
flowScheduler.createFlowSchedule(
superToken, bob, defaultStartDate, uint32(100), int96(1000), defaultStartAmount, defaultStartDate + uint32(3600), "", ""
);
sf.host.callAgreement(
sf.cfa,
abi.encodeCall(
sf.cfa.updateFlowOperatorPermissions,
(
superToken,
address(flowScheduler),
FlowOperatorDefinitions.AUTHORIZE_FLOW_OPERATOR_CREATE,
1000,
new bytes(0)
)
),
new bytes(0)
);
// increase allowance so flowScheduler can transfer tokens from alice to bob
// when vesting starts
superToken.increaseAllowance(address(flowScheduler), type(uint256).max);
vm.stopPrank();
vm.startPrank(admin);
vm.expectEmit(true, true, true, true);
emit CreateFlowExecuted(
superToken, alice, bob, defaultStartDate, uint32(100), int96(1000), defaultStartAmount, ""
);
vm.warp(100);
vm.expectCall(
address(flowScheduler),
abi.encodeCall(
flowScheduler.executeCreateFlow,
(superToken, alice, bob, "")
)
);
FlowScheduler.FlowSchedule memory schedule = flowScheduler.getFlowSchedule(address(superToken), alice, bob);
assertEq(schedule.startAmount, defaultStartAmount);
uint256 bobBalanceBefore = superToken.balanceOf(bob);
uint256 aliceBalanceBefore = superToken.balanceOf(alice);
flowScheduler.executeCreateFlow(superToken, alice, bob, "");
schedule = flowScheduler.getFlowSchedule(address(superToken), alice, bob);
assertEq(schedule.startAmount, 0);
// Bob's balance is slightly more than or equal to his previous balance plus defaultStartAmount
// this is due to the logic which determines the startAmount
assertTrue(superToken.balanceOf(bob) >= bobBalanceBefore + defaultStartAmount);
// Alice's balance is less than or equal to previous balance sub defaultStartAmount
// it is actually slightly less because of the deposit paid for stream creation
assertTrue(superToken.balanceOf(alice) <= aliceBalanceBefore - defaultStartAmount);
}
function testExecute2xCreateFlow() public {
vm.startPrank(alice);
flowScheduler.createFlowSchedule(
superToken, bob, defaultStartDate, uint32(100), int96(1000), 0, defaultStartDate + uint32(3600), "", ""
);
sf.host.callAgreement(
sf.cfa,
abi.encodeCall(
sf.cfa.updateFlowOperatorPermissions,
(
superToken,
address(flowScheduler),
FlowOperatorDefinitions.AUTHORIZE_FLOW_OPERATOR_CREATE,
1000,
new bytes(0)
)
),
new bytes(0)
);
// Remove any allowance
superToken.approve(address(flowScheduler), 0);
vm.stopPrank();
vm.startPrank(admin);
vm.expectEmit(true, true, true, true);
emit CreateFlowExecuted(
superToken, alice, bob, defaultStartDate, uint32(100), int96(1000), 0, ""
);
vm.warp(100);
vm.expectCall(
address(flowScheduler),
abi.encodeCall(
flowScheduler.executeCreateFlow,
(superToken, alice, bob, "")
)
);
FlowScheduler.FlowSchedule memory schedule = flowScheduler.getFlowSchedule(alice, bob, address(superToken));
assertEq(schedule.startAmount, 0);
flowScheduler.executeCreateFlow(superToken, alice, bob, "");
schedule = flowScheduler.getFlowSchedule(address(superToken), alice, bob);
assertEq(schedule.startAmount, 0);
// try to execute again
vm.expectRevert(IFlowScheduler.TimeWindowInvalid.selector);
flowScheduler.executeCreateFlow(superToken,alice,bob,"");
}
function testCreateScheduleAndDeleteSchedule() public {
vm.startPrank(alice);
flowScheduler.createFlowSchedule(
superToken, bob, defaultStartDate, 60, int96(1000), defaultStartAmount, defaultStartDate + uint32(3600), "", ""
);
FlowScheduler.FlowSchedule memory schedule1 = flowScheduler.getFlowSchedule(address(superToken), alice, bob);
assertEq(schedule1.startDate, defaultStartDate);
vm.expectEmit(true, true, true, true);
emit FlowScheduleDeleted(superToken, alice, bob);
flowScheduler.deleteFlowSchedule(superToken, bob, "");
FlowScheduler.FlowSchedule memory schedule2 = flowScheduler.getFlowSchedule(address(superToken), alice, bob);
assertEq(schedule2.startDate, 0);
}
}