@@ -8,11 +8,20 @@ import {MockFunctions} from "./MockFunctions.sol";
88import {SigUtils} from "./SigUtils.t.sol " ;
99
1010contract GuardedMulticallerTest is Test {
11-
1211 GuardedMulticaller public gmc;
1312 MockFunctions public mock;
1413 SigUtils public sigUtils;
15-
14+
15+ event Multicalled (
16+ address indexed _multicallSigner ,
17+ bytes32 indexed _reference ,
18+ address [] _targets ,
19+ bytes [] _data ,
20+ uint256 _deadline
21+ );
22+
23+ event FunctionPermitted (address indexed _target , bytes4 _functionSelector , bool _permitted );
24+
1625 address public deployer;
1726 address public signer;
1827 uint256 public signerPk;
@@ -21,22 +30,21 @@ contract GuardedMulticallerTest is Test {
2130
2231 string public constant MULTICALLER_NAME = "Multicaller " ;
2332 string public constant MULTICALLER_VERSION = "v1 " ;
24-
33+
2534 bytes32 public ref;
2635 uint256 public deadline;
27-
36+
2837 function setUp () public {
2938 deployer = makeAddr ("deployer " );
3039 (signer, signerPk) = makeAddrAndKey ("signer " );
3140 (user, userPk) = makeAddrAndKey ("user " );
32-
41+
3342 vm.prank (deployer);
3443 gmc = new GuardedMulticaller (deployer, MULTICALLER_NAME, MULTICALLER_VERSION);
3544 vm.prank (deployer);
3645 gmc.grantMulticallSignerRole (signer);
37-
38- sigUtils = new SigUtils (MULTICALLER_NAME, MULTICALLER_VERSION, address (gmc));
3946
47+ sigUtils = new SigUtils (MULTICALLER_NAME, MULTICALLER_VERSION, address (gmc));
4048
4149 vm.prank (deployer);
4250 mock = new MockFunctions ();
@@ -55,161 +63,163 @@ contract GuardedMulticallerTest is Test {
5563 vm.prank (deployer);
5664 gmc.setFunctionPermits (functionPermits);
5765
58-
5966 deadline = block .timestamp + 30 minutes ;
6067 ref = keccak256 (abi.encodePacked ("test_ref " ));
6168 }
62-
69+
6370 function test_SuccessfulExecution () public {
6471 address [] memory targets = new address [](1 );
6572 targets[0 ] = address (mock);
6673
6774 bytes [] memory data = new bytes [](1 );
6875 data[0 ] = abi.encodeWithSelector (MockFunctions.succeed.selector );
69-
76+
7077 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
71-
78+
7279 vm.prank (user);
7380 vm.expectEmit (true , true , true , true );
74- emit GuardedMulticaller. Multicalled (signer, ref, targets, data, deadline);
81+ emit Multicalled (signer, ref, targets, data, deadline);
7582 gmc.execute (signer, ref, targets, data, deadline, signature);
7683 }
77-
84+
7885 function test_RevertWithCustomError () public {
7986 address [] memory targets = new address [](1 );
8087 targets[0 ] = address (mock);
81-
88+
8289 bytes [] memory data = new bytes [](1 );
8390 data[0 ] = abi.encodeWithSignature ("revertWithNoReason() " );
84-
91+
8592 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
86-
93+
8794 vm.prank (user);
8895 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.FailedCall.selector , targets[0 ], data[0 ]));
8996 gmc.execute (signer, ref, targets, data, deadline, signature);
9097 }
91-
98+
9299 function test_RevertIfDeadlinePassed () public {
93100 address [] memory targets = new address [](1 );
94101 targets[0 ] = address (mock);
95-
102+
96103 bytes [] memory data = new bytes [](1 );
97104 data[0 ] = abi.encodeWithSignature ("succeed() " );
98-
99105
100106 uint256 expiredDeadline = block .timestamp ;
101107 vm.warp (expiredDeadline + 30 minutes);
102108 bytes memory signature = signTypedData (signerPk, ref, targets, data, expiredDeadline);
103-
109+
104110 vm.prank (user);
105111 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.Expired.selector , expiredDeadline));
106112 gmc.execute (signer, ref, targets, data, expiredDeadline, signature);
107113 }
108-
114+
109115 function test_RevertIfReferenceReused () public {
110116 address [] memory targets = new address [](1 );
111117 targets[0 ] = address (mock);
112-
118+
113119 bytes [] memory data = new bytes [](1 );
114120 data[0 ] = abi.encodeWithSignature ("succeed() " );
115-
121+
116122 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
117-
123+
118124 vm.prank (user);
119125 gmc.execute (signer, ref, targets, data, deadline, signature);
120-
126+
121127 vm.prank (user);
122128 vm.expectRevert (abi.encodePacked (GuardedMulticaller.ReusedReference.selector , ref));
123129 gmc.execute (signer, ref, targets, data, deadline, signature);
124130 }
125-
131+
126132 function test_RevertIfInvalidReference () public {
127133 address [] memory targets = new address [](1 );
128134 targets[0 ] = address (mock);
129-
135+
130136 bytes [] memory data = new bytes [](1 );
131137 data[0 ] = abi.encodeWithSignature ("succeed() " );
132-
138+
133139 bytes32 invalidRef = bytes32 (0 );
134140 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
135-
141+
136142 vm.prank (user);
137143 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.InvalidReference.selector , invalidRef));
138144 gmc.execute (signer, invalidRef, targets, data, deadline, signature);
139145 }
140-
146+
141147 function test_RevertIfUnauthorizedSigner () public {
142148 address [] memory targets = new address [](1 );
143149 targets[0 ] = address (mock);
144-
150+
145151 bytes [] memory data = new bytes [](1 );
146152 data[0 ] = abi.encodeWithSignature ("succeed() " );
147-
153+
148154 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
149-
155+
150156 vm.prank (user);
151157 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.UnauthorizedSigner.selector , user));
152- // Note: execute called with user as signer.
158+ // Note: execute called with user as signer.
153159 gmc.execute (user, ref, targets, data, deadline, signature);
154160 }
155-
161+
156162 function test_RevertIfSignatureMismatch () public {
157163 address [] memory targets = new address [](1 );
158164 targets[0 ] = address (mock);
159-
165+
160166 bytes [] memory data = new bytes [](1 );
161167 data[0 ] = abi.encodeWithSignature ("succeed() " );
162-
168+
163169 bytes memory signature = signTypedData (userPk, ref, targets, data, deadline);
164-
170+
165171 vm.prank (user);
166172 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.UnauthorizedSignature.selector , signature));
167173 gmc.execute (signer, ref, targets, data, deadline, signature);
168174 }
169-
175+
170176 function test_RevertIfEmptyTargets () public {
171177 address [] memory targets = new address [](0 );
172178 bytes [] memory data = new bytes [](1 );
173179 data[0 ] = abi.encodeWithSignature ("succeed() " );
174-
180+
175181 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
176-
182+
177183 vm.prank (user);
178184 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.EmptyAddressArray.selector ));
179185 gmc.execute (signer, ref, targets, data, deadline, signature);
180186 }
181-
187+
182188 function test_RevertIfTargetsDataMismatch () public {
183189 address [] memory targets = new address [](2 );
184190 targets[0 ] = address (mock);
185191 targets[1 ] = address (mock);
186-
192+
187193 bytes [] memory data = new bytes [](1 );
188194 data[0 ] = abi.encodeWithSignature ("succeed() " );
189-
195+
190196 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
191-
197+
192198 vm.prank (user);
193- vm.expectRevert (abi.encodeWithSelector (
194- GuardedMulticaller.AddressDataArrayLengthsMismatch.selector ,
195- targets.length , data.length ));
199+ vm.expectRevert (
200+ abi.encodeWithSelector (
201+ GuardedMulticaller.AddressDataArrayLengthsMismatch.selector ,
202+ targets.length ,
203+ data.length
204+ )
205+ );
196206 gmc.execute (signer, ref, targets, data, deadline, signature);
197207 }
198208
199209 function test_RevertIfFunctionNotPermitted () public {
200210 address [] memory targets = new address [](1 );
201211 targets[0 ] = address (mock);
202-
212+
203213 bytes [] memory data = new bytes [](1 );
204214 data[0 ] = abi.encodeWithSignature ("notPermitted() " );
205-
215+
206216 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
207-
217+
208218 vm.prank (user);
209219 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.UnauthorizedFunction.selector , targets[0 ], data[0 ]));
210220 gmc.execute (signer, ref, targets, data, deadline, signature);
211221 }
212-
222+
213223 function test_RevertIfFunctionDisallowed () public {
214224 GuardedMulticaller.FunctionPermit[] memory functionPermits = new GuardedMulticaller.FunctionPermit [](1 );
215225 functionPermits[0 ] = GuardedMulticaller.FunctionPermit ({
@@ -219,35 +229,35 @@ contract GuardedMulticallerTest is Test {
219229 });
220230 vm.prank (deployer);
221231 gmc.setFunctionPermits (functionPermits);
222-
232+
223233 address [] memory targets = new address [](1 );
224234 targets[0 ] = address (mock);
225-
235+
226236 bytes [] memory data = new bytes [](1 );
227237 data[0 ] = abi.encodeWithSignature ("succeed() " );
228-
238+
229239 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
230-
240+
231241 vm.prank (user);
232242 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.UnauthorizedFunction.selector , targets[0 ], data[0 ]));
233243 gmc.execute (signer, ref, targets, data, deadline, signature);
234244 }
235-
245+
236246 function test_RevertIfInvalidSignature () public {
237247 address [] memory targets = new address [](1 );
238248 targets[0 ] = address (mock);
239-
249+
240250 bytes [] memory data = new bytes [](1 );
241251 data[0 ] = abi.encodeWithSignature ("succeed() " );
242-
252+
243253 bytes32 maliciousRef = keccak256 (abi.encodePacked ("malicious_ref " ));
244254 bytes memory signature = signTypedData (signerPk, maliciousRef, targets, data, deadline);
245-
255+
246256 vm.prank (user);
247257 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.UnauthorizedSignature.selector , signature));
248258 gmc.execute (signer, ref, targets, data, deadline, signature);
249259 }
250-
260+
251261 function test_EmitFunctionPermittedEvent () public {
252262 vm.startPrank (deployer);
253263 GuardedMulticaller.FunctionPermit[] memory functionPermits = new GuardedMulticaller.FunctionPermit [](1 );
@@ -257,26 +267,18 @@ contract GuardedMulticallerTest is Test {
257267 permitted: true
258268 });
259269 vm.expectEmit (true , true , true , true );
260- emit GuardedMulticaller.FunctionPermitted (
261- address (mock),
262- MockFunctions.succeed.selector ,
263- true
264- );
270+ emit FunctionPermitted (address (mock), MockFunctions.succeed.selector , true );
265271 gmc.setFunctionPermits (functionPermits);
266-
272+
267273 functionPermits[0 ] = GuardedMulticaller.FunctionPermit ({
268274 target: address (mock),
269275 functionSelector: MockFunctions.succeed.selector ,
270276 permitted: false
271277 });
272278 vm.expectEmit (true , true , true , true );
273- emit GuardedMulticaller.FunctionPermitted (
274- address (mock),
275- MockFunctions.succeed.selector ,
276- false
277- );
279+ emit FunctionPermitted (address (mock), MockFunctions.succeed.selector , false );
278280 gmc.setFunctionPermits (functionPermits);
279-
281+
280282 vm.stopPrank ();
281283 }
282284
@@ -299,7 +301,6 @@ contract GuardedMulticallerTest is Test {
299301 gmc.setFunctionPermits (functionPermits);
300302 }
301303
302-
303304 function test_RevertIfSetFunctionPermitsNonContract () public {
304305 GuardedMulticaller.FunctionPermit[] memory functionPermits = new GuardedMulticaller.FunctionPermit [](1 );
305306 functionPermits[0 ] = GuardedMulticaller.FunctionPermit ({
@@ -311,33 +312,33 @@ contract GuardedMulticallerTest is Test {
311312 vm.expectRevert (abi.encodeWithSelector (GuardedMulticaller.NonContractAddress.selector , deployer));
312313 gmc.setFunctionPermits (functionPermits);
313314 }
314-
315+
315316 function test_RevertIfGrantRevokeSignerRoleWithInvalidRole () public {
316317 vm.startPrank (user);
317-
318+
318319 vm.expectRevert ();
319320 gmc.grantMulticallSignerRole (user);
320-
321+
321322 vm.expectRevert ();
322323 gmc.revokeMulticallSignerRole (user);
323-
324+
324325 vm.stopPrank ();
325326 }
326-
327+
327328 function test_HasBeenExecuted () public {
328329 address [] memory targets = new address [](1 );
329330 targets[0 ] = address (mock);
330-
331+
331332 bytes [] memory data = new bytes [](1 );
332333 data[0 ] = abi.encodeWithSignature ("succeed() " );
333-
334+
334335 bytes memory signature = signTypedData (signerPk, ref, targets, data, deadline);
335-
336+
336337 vm.prank (user);
337338 gmc.execute (signer, ref, targets, data, deadline, signature);
338-
339+
339340 assertTrue (gmc.hasBeenExecuted (ref));
340-
341+
341342 bytes32 invalidRef = keccak256 (abi.encodePacked ("invalid_ref " ));
342343 assertFalse (gmc.hasBeenExecuted (invalidRef));
343344 }
@@ -366,5 +367,4 @@ contract GuardedMulticallerTest is Test {
366367 (uint8 v , bytes32 r , bytes32 s ) = vm.sign (_signerPk, digest);
367368 return abi.encodePacked (r, s, v);
368369 }
369-
370- }
370+ }
0 commit comments