@@ -137,18 +137,16 @@ describe('AddressUtils', async () => {
137
137
[
138
138
'$functionCall(address,bytes)'
139
139
] ( await targetContract . getAddress ( ) , '0x' ) ,
140
- ) . to . be . revertedWith ( 'AddressUtils: failed low-level call ') ;
140
+ ) . to . be . revertedWithCustomError ( instance , 'AddressUtils__FailedCall ') ;
141
141
} ) ;
142
142
} ) ;
143
143
} ) ;
144
144
145
- describe ( '#functionCall(address,bytes,string )' , ( ) => {
145
+ describe ( '#functionCall(address,bytes,bytes4 )' , ( ) => {
146
146
it ( 'returns the bytes representation of the return value of the target function' , async ( ) => {
147
147
const mock = await deployMockContract ( deployer , [
148
148
'function fn () external payable returns (bool)' ,
149
149
] ) ;
150
- const revertReason = 'REVERT_REASON' ;
151
-
152
150
await mock . mock . fn . returns ( true ) ;
153
151
154
152
const target = mock . address ;
@@ -160,18 +158,18 @@ describe('AddressUtils', async () => {
160
158
await instance
161
159
. connect ( deployer )
162
160
[
163
- '$functionCall(address,bytes,string )'
164
- ] . staticCall ( target , data , revertReason ) ,
161
+ '$functionCall(address,bytes,bytes4 )'
162
+ ] . staticCall ( target , data , ethers . randomBytes ( 4 ) ) ,
165
163
) . to . equal ( ethers . zeroPadValue ( '0x01' , 32 ) ) ;
166
164
} ) ;
167
165
168
166
describe ( 'reverts if' , ( ) => {
169
167
it ( 'target is not a contract' , async ( ) => {
170
168
await expect (
171
- instance [ '$functionCall(address,bytes,string )' ] (
169
+ instance [ '$functionCall(address,bytes,bytes4 )' ] (
172
170
ethers . ZeroAddress ,
173
171
'0x' ,
174
- '' ,
172
+ ethers . randomBytes ( 4 ) ,
175
173
) ,
176
174
) . to . be . revertedWithCustomError (
177
175
instance ,
@@ -196,12 +194,17 @@ describe('AddressUtils', async () => {
196
194
await expect (
197
195
instance
198
196
. connect ( deployer )
199
- [ '$functionCall(address,bytes,string)' ] ( target , data , '' ) ,
197
+ [
198
+ '$functionCall(address,bytes,bytes4)'
199
+ ] ( target , data , ethers . randomBytes ( 4 ) ) ,
200
200
) . to . be . revertedWith ( revertReason ) ;
201
201
} ) ;
202
202
203
- it ( 'target contract reverts, with provided error message' , async ( ) => {
204
- const revertReason = 'REVERT_REASON' ;
203
+ it ( 'target contract reverts, with provided custom error' , async ( ) => {
204
+ // unrelated custom error, but it must exist on the contract due to limitiations with revertedWithCustomError matcher
205
+ const customError = 'AddressUtils__InsufficientBalance' ;
206
+ const revertReason =
207
+ instance . interface . getError ( customError ) ?. selector ! ;
205
208
206
209
const targetContract = await new AddressUtils__factory (
207
210
deployer ,
@@ -211,9 +214,9 @@ describe('AddressUtils', async () => {
211
214
instance
212
215
. connect ( deployer )
213
216
[
214
- '$functionCall(address,bytes,string )'
217
+ '$functionCall(address,bytes,bytes4 )'
215
218
] ( await targetContract . getAddress ( ) , '0x' , revertReason ) ,
216
- ) . to . be . revertedWith ( revertReason ) ;
219
+ ) . to . be . revertedWithCustomError ( instance , customError ) ;
217
220
} ) ;
218
221
} ) ;
219
222
} ) ;
@@ -278,7 +281,7 @@ describe('AddressUtils', async () => {
278
281
) ;
279
282
} ) ;
280
283
281
- it ( 'contract balance is insufficient for the call ' , async ( ) => {
284
+ it ( 'contract balance is insufficient' , async ( ) => {
282
285
await expect (
283
286
instance
284
287
. connect ( deployer )
@@ -313,8 +316,9 @@ describe('AddressUtils', async () => {
313
316
[
314
317
'$functionCallWithValue(address,bytes,uint256)'
315
318
] ( await targetContract . getAddress ( ) , data , value ) ,
316
- ) . to . be . revertedWith (
317
- 'AddressUtils: failed low-level call with value' ,
319
+ ) . to . be . revertedWithCustomError (
320
+ instance ,
321
+ 'AddressUtils__FailedCallWithValue' ,
318
322
) ;
319
323
} ) ;
320
324
@@ -352,14 +356,15 @@ describe('AddressUtils', async () => {
352
356
[
353
357
'$functionCallWithValue(address,bytes,uint256)'
354
358
] ( await targetContract . getAddress ( ) , '0x' , 0 ) ,
355
- ) . to . be . revertedWith (
356
- 'AddressUtils: failed low-level call with value' ,
359
+ ) . to . be . revertedWithCustomError (
360
+ instance ,
361
+ 'AddressUtils__FailedCallWithValue' ,
357
362
) ;
358
363
} ) ;
359
364
} ) ;
360
365
} ) ;
361
366
362
- describe ( '#functionCallWithValue(address,bytes,uint256,string )' , ( ) => {
367
+ describe ( '#functionCallWithValue(address,bytes,uint256,bytes4 )' , ( ) => {
363
368
it ( 'returns the bytes representation of the return value of the target function' , async ( ) => {
364
369
const mock = await deployMockContract ( deployer , [
365
370
'function fn () external payable returns (bool)' ,
@@ -376,8 +381,8 @@ describe('AddressUtils', async () => {
376
381
await instance
377
382
. connect ( deployer )
378
383
[
379
- '$functionCallWithValue(address,bytes,uint256,string )'
380
- ] . staticCall ( target , data , 0 , '' ) ,
384
+ '$functionCallWithValue(address,bytes,uint256,bytes4 )'
385
+ ] . staticCall ( target , data , 0 , ethers . randomBytes ( 4 ) ) ,
381
386
) . to . equal ( ethers . zeroPadValue ( '0x01' , 32 ) ) ;
382
387
} ) ;
383
388
@@ -401,42 +406,45 @@ describe('AddressUtils', async () => {
401
406
instance
402
407
. connect ( deployer )
403
408
[
404
- '$functionCallWithValue(address,bytes,uint256,string )'
405
- ] ( target , data , value , '' ) ,
409
+ '$functionCallWithValue(address,bytes,uint256,bytes4 )'
410
+ ] ( target , data , value , ethers . randomBytes ( 4 ) ) ,
406
411
) . to . changeEtherBalances ( [ instance , mock ] , [ - value , value ] ) ;
407
412
} ) ;
408
413
409
414
describe ( 'reverts if' , ( ) => {
410
415
it ( 'target is not a contract' , async ( ) => {
411
416
await expect (
412
- instance [ '$functionCallWithValue(address,bytes,uint256,string )' ] (
417
+ instance [ '$functionCallWithValue(address,bytes,uint256,bytes4 )' ] (
413
418
ethers . ZeroAddress ,
414
419
'0x' ,
415
420
0 ,
416
- '' ,
421
+ ethers . randomBytes ( 4 ) ,
417
422
) ,
418
423
) . to . be . revertedWithCustomError (
419
424
instance ,
420
425
'AddressUtils__NotContract' ,
421
426
) ;
422
427
} ) ;
423
428
424
- it ( 'contract balance is insufficient for the call ' , async ( ) => {
429
+ it ( 'contract balance is insufficient' , async ( ) => {
425
430
await expect (
426
431
instance
427
432
. connect ( deployer )
428
433
[
429
- '$functionCallWithValue(address,bytes,uint256,string )'
430
- ] ( await instance . getAddress ( ) , '0x' , 1 , '' ) ,
434
+ '$functionCallWithValue(address,bytes,uint256,bytes4 )'
435
+ ] ( await instance . getAddress ( ) , '0x' , 1 , ethers . randomBytes ( 4 ) ) ,
431
436
) . to . be . revertedWithCustomError (
432
437
instance ,
433
438
'AddressUtils__InsufficientBalance' ,
434
439
) ;
435
440
} ) ;
436
441
437
- it ( 'target function is not payable and value is included' , async ( ) => {
442
+ it ( 'target function is not payable and value is included, with provided custom error ' , async ( ) => {
438
443
const value = 2n ;
439
- const revertReason = 'REVERT_REASON' ;
444
+ // unrelated custom error, but it must exist on the contract due to limitiations with revertedWithCustomError matcher
445
+ const customError = 'AddressUtils__InsufficientBalance' ;
446
+ const revertReason =
447
+ instance . interface . getError ( customError ) ?. selector ! ;
440
448
441
449
await setBalance ( await instance . getAddress ( ) , value ) ;
442
450
@@ -455,9 +463,9 @@ describe('AddressUtils', async () => {
455
463
instance
456
464
. connect ( deployer )
457
465
[
458
- '$functionCallWithValue(address,bytes,uint256,string )'
466
+ '$functionCallWithValue(address,bytes,uint256,bytes4 )'
459
467
] ( await targetContract . getAddress ( ) , data , value , revertReason ) ,
460
- ) . to . be . revertedWith ( revertReason ) ;
468
+ ) . to . be . revertedWithCustomError ( instance , customError ) ;
461
469
} ) ;
462
470
463
471
it ( 'target contract reverts, with target contract error message' , async ( ) => {
@@ -478,13 +486,16 @@ describe('AddressUtils', async () => {
478
486
instance
479
487
. connect ( deployer )
480
488
[
481
- '$functionCallWithValue(address,bytes,uint256,string )'
482
- ] ( target , data , 0 , '' ) ,
489
+ '$functionCallWithValue(address,bytes,uint256,bytes4 )'
490
+ ] ( target , data , 0 , ethers . randomBytes ( 4 ) ) ,
483
491
) . to . be . revertedWith ( revertReason ) ;
484
492
} ) ;
485
493
486
- it ( 'target contract reverts, with provided error message' , async ( ) => {
487
- const revertReason = 'REVERT_REASON' ;
494
+ it ( 'target contract reverts, with provided custom error' , async ( ) => {
495
+ // unrelated custom error, but it must exist on the contract due to limitiations with revertedWithCustomError matcher
496
+ const customError = 'AddressUtils__InsufficientBalance' ;
497
+ const revertReason =
498
+ instance . interface . getError ( customError ) ?. selector ! ;
488
499
489
500
const targetContract = await new AddressUtils__factory (
490
501
deployer ,
@@ -494,9 +505,9 @@ describe('AddressUtils', async () => {
494
505
instance
495
506
. connect ( deployer )
496
507
[
497
- '$functionCallWithValue(address,bytes,uint256,string )'
508
+ '$functionCallWithValue(address,bytes,uint256,bytes4 )'
498
509
] ( await targetContract . getAddress ( ) , '0x' , 0 , revertReason ) ,
499
- ) . to . be . revertedWith ( revertReason ) ;
510
+ ) . to . be . revertedWithCustomError ( instance , customError ) ;
500
511
} ) ;
501
512
} ) ;
502
513
} ) ;
0 commit comments