-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBudget.t.sol
More file actions
412 lines (330 loc) · 16.2 KB
/
Budget.t.sol
File metadata and controls
412 lines (330 loc) · 16.2 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.16;
import {FirmTest} from "../../common/test/lib/FirmTest.sol";
import {RolesStub} from "../../common/test/mocks/RolesStub.sol";
import {roleFlag} from "../../common/test/mocks/RolesAuthMock.sol";
import {AvatarStub} from "../../common/test/mocks/AvatarStub.sol";
import {UpgradeableModuleProxyFactory} from "../../factory/UpgradeableModuleProxyFactory.sol";
import {TimeShift, DateTimeLib} from "../../budget/TimeShiftLib.sol";
import {SafeAware} from "../../bases/SafeAware.sol";
import "../Budget.sol";
contract BudgetTest is FirmTest {
AvatarStub avatar;
RolesStub roles;
Budget budget;
address SPENDER = account("spender");
address RECEIVER = account("receiver");
address SOMEONE_ELSE = account("someone else");
function setUp() public {
avatar = new AvatarStub();
roles = new RolesStub();
budget = Budget(createProxy(new Budget(), abi.encodeCall(Budget.initialize, (avatar, roles, address(0)))));
}
function testInitialState() public {
assertEq(address(budget.avatar()), address(avatar));
assertEq(address(budget.target()), address(avatar));
assertEq(address(budget.roles()), address(roles));
}
function testCannotReinit() public {
vm.expectRevert(abi.encodeWithSelector(SafeAware.AlreadyInitialized.selector));
budget.initialize(avatar, roles, address(0));
}
function testCreateAllowance() public returns (uint256 allowanceId) {
vm.prank(address(avatar));
vm.warp(0);
allowanceId = createDailyAllowance(SPENDER, 1);
(
uint256 parentId,
uint256 amount,
uint256 spent,
address token,
uint40 nextResetTime,
address spender,
EncodedTimeShift recurrency,
bool isDisabled
) = budget.allowances(allowanceId);
assertEq(parentId, NO_PARENT_ID);
assertEq(amount, 10);
assertEq(spent, 0);
assertEq(token, address(0));
assertEq(nextResetTime, 1 days);
assertEq(spender, SPENDER);
assertEq(
bytes32(EncodedTimeShift.unwrap(recurrency)),
bytes32(EncodedTimeShift.unwrap(TimeShift(TimeShiftLib.TimeUnit.Daily, 0).encode()))
);
assertFalse(isDisabled);
}
function testUpdateAllowanceParams() public {
uint256 allowanceId = testCreateAllowance();
vm.startPrank(address(avatar));
budget.setAllowanceSpender(allowanceId, RECEIVER);
budget.setAllowanceAmount(allowanceId, 1);
budget.setAllowanceName(allowanceId, "new name");
(, uint256 amount,,, uint40 nextResetTime, address spender,,) = budget.allowances(allowanceId);
assertEq(amount, 1);
assertEq(nextResetTime, 1 days);
assertEq(spender, RECEIVER);
}
function testNotOwnerCannotCreateTopLevelAllowance() public {
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedNotAllowanceAdmin.selector, 0));
createDailyAllowance(SPENDER, 0);
}
function testBadTimeshiftsRevert() public {
vm.prank(address(avatar));
vm.expectRevert(abi.encodeWithSelector(TimeShiftLib.InvalidTimeShift.selector));
budget.createAllowance(
NO_PARENT_ID, SPENDER, address(0), 10, TimeShift(TimeShiftLib.TimeUnit.Inherit, 0).encode(), ""
);
}
function testInvalidSpenderReverts() public {
uint8 badRoleId = 101; // RolesStub returns false to roleExists when id > 100
vm.prank(address(avatar));
vm.expectRevert(abi.encodeWithSelector(RolesAuth.UnexistentRole.selector, badRoleId));
budget.createAllowance(
NO_PARENT_ID, roleFlag(badRoleId), address(0), 10, TimeShift(TimeShiftLib.TimeUnit.Daily, 0).encode(), ""
);
}
function testAllowanceIsKeptTrackOfOnSingle() public {
uint40 initialTime = uint40(DateTimeLib.timestampFromDateTime(2022, 1, 1, 0, 0, 0));
uint256 allowanceId = 1;
vm.prank(address(avatar));
vm.warp(initialTime);
createDailyAllowance(SPENDER, allowanceId);
assertExecutePayment(SPENDER, allowanceId, RECEIVER, 7, initialTime + 1 days);
vm.warp(initialTime + 1 days);
assertExecutePayment(SPENDER, allowanceId, RECEIVER, 7, initialTime + 2 days);
assertExecutePayment(SPENDER, allowanceId, RECEIVER, 2, initialTime + 2 days);
vm.prank(SPENDER);
vm.expectRevert(abi.encodeWithSelector(Budget.Overbudget.selector, allowanceId, 7, 1));
budget.executePayment(allowanceId, RECEIVER, 7, "");
}
function testAllowanceIsKeptTrackOfOnMulti() public {
uint40 initialTime = uint40(DateTimeLib.timestampFromDateTime(2022, 1, 1, 0, 0, 0));
uint256 allowanceId = 1;
vm.prank(address(avatar));
vm.warp(initialTime);
createDailyAllowance(SPENDER, allowanceId);
// max out allowance doing 5 payments of 2
(address[] memory tos, uint256[] memory amounts) = _generateMultiPaymentArrays(5, RECEIVER, 2);
vm.prank(SPENDER);
budget.executeMultiPayment(allowanceId, tos, amounts, "");
vm.prank(SPENDER);
vm.expectRevert(abi.encodeWithSelector(Budget.Overbudget.selector, allowanceId, 1, 0));
budget.executePayment(allowanceId, RECEIVER, 1, "");
vm.warp(initialTime + 1 days);
// almost max out allowance doing 4 payments of 2
(tos, amounts) = _generateMultiPaymentArrays(4, RECEIVER, 2);
vm.prank(SPENDER);
budget.executeMultiPayment(allowanceId, tos, amounts, "");
// max out on second payment
(tos, amounts) = _generateMultiPaymentArrays(2, RECEIVER, 2);
vm.prank(SPENDER);
vm.expectRevert(abi.encodeWithSelector(Budget.Overbudget.selector, allowanceId, 4, 2));
budget.executeMultiPayment(allowanceId, tos, amounts, "");
}
function testMultipleAllowances() public {
uint40 initialTime = uint40(DateTimeLib.timestampFromDateTime(2022, 1, 1, 0, 0, 0));
uint256 firstAllowanceId = 1;
uint256 secondAllowanceId = 2;
vm.warp(initialTime);
vm.startPrank(address(avatar));
createDailyAllowance(SPENDER, firstAllowanceId);
createDailyAllowance(SPENDER, secondAllowanceId);
vm.stopPrank();
assertExecutePayment(SPENDER, firstAllowanceId, RECEIVER, 7, initialTime + 1 days);
assertExecutePayment(SPENDER, secondAllowanceId, RECEIVER, 7, initialTime + 1 days);
vm.warp(initialTime + 1 days);
assertExecutePayment(SPENDER, firstAllowanceId, RECEIVER, 7, initialTime + 2 days);
assertExecutePayment(SPENDER, secondAllowanceId, RECEIVER, 7, initialTime + 2 days);
}
function testCreateSuballowance() public returns (uint256 topLevelAllowance, uint256 subAllowance) {
uint40 initialTime = uint40(DateTimeLib.timestampFromDateTime(2022, 1, 1, 0, 0, 0));
vm.warp(initialTime);
vm.prank(address(avatar));
topLevelAllowance = budget.createAllowance(
NO_PARENT_ID, SPENDER, address(0), 10, TimeShift(TimeShiftLib.TimeUnit.Monthly, 0).encode(), ""
);
vm.prank(SPENDER);
subAllowance = budget.createAllowance(
topLevelAllowance, SOMEONE_ELSE, address(0), 5, TimeShift(TimeShiftLib.TimeUnit.Daily, 0).encode(), ""
);
assertExecutePayment(SOMEONE_ELSE, subAllowance, RECEIVER, 5, initialTime + 1 days);
}
function testNonAdminCannotUpdateAllowanceParams() public {
(uint256 topLevelAllowanceId, uint256 subAllowanceId) = testCreateSuballowance();
vm.startPrank(SPENDER);
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedNotAllowanceAdmin.selector, NO_PARENT_ID));
budget.setAllowanceSpender(topLevelAllowanceId, RECEIVER);
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedNotAllowanceAdmin.selector, NO_PARENT_ID));
budget.setAllowanceAmount(topLevelAllowanceId, 1);
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedNotAllowanceAdmin.selector, NO_PARENT_ID));
budget.setAllowanceName(topLevelAllowanceId, "new name");
vm.stopPrank();
vm.startPrank(address(avatar));
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedNotAllowanceAdmin.selector, topLevelAllowanceId));
budget.setAllowanceSpender(subAllowanceId, RECEIVER);
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedNotAllowanceAdmin.selector, topLevelAllowanceId));
budget.setAllowanceAmount(subAllowanceId, 1);
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedNotAllowanceAdmin.selector, topLevelAllowanceId));
budget.setAllowanceName(subAllowanceId, "new name");
vm.stopPrank();
}
function testCreateSuballowanceWithInheritedRecurrency() public {
uint40 initialTime = uint40(DateTimeLib.timestampFromDateTime(2022, 1, 1, 0, 0, 0));
vm.warp(initialTime);
vm.prank(address(avatar));
uint256 topLevelAllowance = budget.createAllowance(
NO_PARENT_ID, SPENDER, address(0), 10, TimeShift(TimeShiftLib.TimeUnit.Daily, 0).encode(), ""
);
vm.prank(SPENDER);
uint256 subAllowance = budget.createAllowance(
topLevelAllowance, SOMEONE_ELSE, address(0), 5, TimeShift(TimeShiftLib.TimeUnit.Inherit, 0).encode(), ""
);
assertExecutePayment(SOMEONE_ELSE, subAllowance, RECEIVER, 5, initialTime + 1 days);
}
function testAllowanceChain() public {
uint40 initialTime = uint40(DateTimeLib.timestampFromDateTime(2022, 1, 1, 0, 0, 0));
vm.warp(initialTime);
vm.prank(address(avatar));
uint256 allowance1 = budget.createAllowance(
NO_PARENT_ID, SPENDER, address(0), 10, TimeShift(TimeShiftLib.TimeUnit.Monthly, 0).encode(), ""
);
vm.startPrank(SPENDER);
uint256 allowance2 = budget.createAllowance(
allowance1, SPENDER, address(0), 5, TimeShift(TimeShiftLib.TimeUnit.Inherit, 0).encode(), ""
);
uint256 allowance3 = budget.createAllowance(
allowance2, SPENDER, address(0), 2, TimeShift(TimeShiftLib.TimeUnit.Daily, 0).encode(), ""
);
uint256 allowance4 = budget.createAllowance(
allowance3, SPENDER, address(0), 1, TimeShift(TimeShiftLib.TimeUnit.Inherit, 0).encode(), ""
);
vm.stopPrank();
assertExecutePayment(SPENDER, allowance4, RECEIVER, 1, initialTime + 1 days);
vm.warp(initialTime + 1 days);
assertExecutePayment(SPENDER, allowance3, RECEIVER, 2, initialTime + 2 days);
(,, uint256 spent1,,,,,) = budget.allowances(allowance1);
(,, uint256 spent2,,,,,) = budget.allowances(allowance2);
(,, uint256 spent3,,,,,) = budget.allowances(allowance3);
(,, uint256 spent4,,,,,) = budget.allowances(allowance4);
assertEq(spent1, 3);
assertEq(spent2, 3);
assertEq(spent3, 2);
assertEq(spent4, 1); // It's one because the state doesn't get reset until a payment involving this allowance
vm.expectRevert(abi.encodeWithSelector(Budget.Overbudget.selector, allowance3, 1, 0));
vm.prank(SPENDER);
budget.executePayment(allowance4, SPENDER, 1, "");
}
function testDisablingAllowanceBreaksChain() public {
uint256 topLevelAllowanceId = 1;
uint256 childAllowanceId = 4;
testAllowanceChain(); // sets up the chain
vm.prank(address(avatar));
budget.setAllowanceState(topLevelAllowanceId, false);
vm.prank(SPENDER);
vm.expectRevert(abi.encodeWithSelector(Budget.DisabledAllowance.selector, topLevelAllowanceId));
budget.executePayment(childAllowanceId, RECEIVER, 1, "");
}
function testOnlyParentAdminCanDisableAllowance() public {
uint256 topLevelAllowanceId = 1;
uint256 childAllowanceId = 4;
testAllowanceChain(); // sets up the chain
vm.prank(SPENDER);
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedNotAllowanceAdmin.selector, 0));
budget.setAllowanceState(topLevelAllowanceId, false);
vm.prank(address(avatar));
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedNotAllowanceAdmin.selector, childAllowanceId - 1));
budget.setAllowanceState(childAllowanceId, false);
}
function testCantExecuteIfNotAuthorized() public {
vm.prank(address(avatar));
uint256 allowanceId = 1;
createDailyAllowance(SPENDER, allowanceId);
vm.prank(RECEIVER);
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedPaymentExecution.selector, allowanceId, RECEIVER));
budget.executePayment(allowanceId, RECEIVER, 7, "");
}
function testAllowanceSpenderWithRoleFlags() public {
uint256 allowanceId = 1;
uint8 roleId = 1;
vm.prank(address(avatar));
createDailyAllowance(roleFlag(roleId), allowanceId);
vm.startPrank(SPENDER);
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedPaymentExecution.selector, allowanceId, SPENDER));
budget.executePayment(allowanceId, RECEIVER, 7, ""); // execution fails since SPENDER doesn't have the required role yet
roles.setRole(SPENDER, roleId, true);
budget.executePayment(allowanceId, RECEIVER, 7, ""); // as soon as it gets the role, the payment executes
}
function testCantExecuteInexistentAllowance() public {
vm.prank(SPENDER);
vm.expectRevert(abi.encodeWithSelector(Budget.UnexistentAllowance.selector, 0));
budget.executePayment(0, RECEIVER, 7, "");
}
function testCantExecuteMultiIfNotAuthorized() public {
vm.prank(address(avatar));
uint256 allowanceId = 1;
createDailyAllowance(SPENDER, allowanceId);
vm.prank(RECEIVER);
vm.expectRevert(abi.encodeWithSelector(Budget.UnauthorizedPaymentExecution.selector, allowanceId, RECEIVER));
(address[] memory tos, uint256[] memory amounts) = _generateMultiPaymentArrays(2, RECEIVER, 7);
budget.executeMultiPayment(allowanceId, tos, amounts, "");
}
function testRevertOnBadInputToMultiPayment() public {
vm.prank(address(avatar));
uint256 allowanceId = 1;
createDailyAllowance(SPENDER, allowanceId);
vm.prank(SPENDER);
vm.expectRevert(abi.encodeWithSelector(Budget.BadInput.selector));
(address[] memory tos, uint256[] memory amounts) = _generateMultiPaymentArrays(2, RECEIVER, 7);
tos = new address[](1);
budget.executeMultiPayment(allowanceId, tos, amounts, "");
}
function createDailyAllowance(address spender, uint256 expectedId) public returns (uint256 allowanceId) {
allowanceId = budget.createAllowance(
NO_PARENT_ID, spender, address(0), 10, TimeShift(TimeShiftLib.TimeUnit.Daily, 0).encode(), ""
);
assertEq(allowanceId, expectedId);
}
function _generateMultiPaymentArrays(uint256 num, address to, uint256 amount)
internal
pure
returns (address[] memory tos, uint256[] memory amounts)
{
tos = new address[](num);
amounts = new uint256[](num);
for (uint256 i = 0; i < num; i++) {
tos[i] = to;
amounts[i] = amount;
}
}
event PaymentExecuted(
uint256 indexed allowanceId,
address indexed actor,
address token,
address indexed to,
uint256 amount,
uint40 nextResetTime,
string descrition
);
function assertExecutePayment(
address actor,
uint256 allowanceId,
address to,
uint256 amount,
uint40 expectedNextResetTime
) public {
(,, uint256 initialSpent, address token, uint40 initialNextReset,, EncodedTimeShift shift,) =
budget.allowances(allowanceId);
if (block.timestamp >= initialNextReset) {
initialSpent = 0;
}
vm.prank(actor);
vm.expectEmit(true, true, true, true);
emit PaymentExecuted(allowanceId, actor, token, to, amount, expectedNextResetTime, "");
budget.executePayment(allowanceId, to, amount, "");
(,, uint256 spent,, uint40 nextResetTime,,,) = budget.allowances(allowanceId);
assertEq(spent, initialSpent + amount);
assertEq(nextResetTime, shift.isInherited() ? 0 : expectedNextResetTime);
}
}