@@ -39,6 +39,8 @@ use crate::{
39
39
SolBytes ,
40
40
SolDecode ,
41
41
SolEncode ,
42
+ SolParamsDecode ,
43
+ SolParamsEncode ,
42
44
SolTypeDecode ,
43
45
SolTypeEncode ,
44
46
} ,
@@ -548,3 +550,74 @@ fn encode_refs_works() {
548
550
[ ]
549
551
) ;
550
552
}
553
+
554
+ macro_rules! test_case_params {
555
+ ( $ty: ty, $val: expr) => {
556
+ test_case_params!( $ty, $val, $ty, alloy_sol_types:: SolValue , $val, [ ] , [ ] )
557
+ } ;
558
+ ( $ty: ty, $val: expr, $sol_ty: ty, $sol_trait: ty) => {
559
+ test_case_params!( $ty, $val, $sol_ty, $sol_trait, $val, [ ] , [ ] )
560
+ } ;
561
+ ( $ty: ty, $val: expr, $sol_ty: ty, $sol_trait: ty, $sol_val: expr, [ $( $ty_cvt: tt) * ] , [ $( $sol_ty_cvt: tt) * ] ) => {
562
+ // `SolParamsEncode` test.
563
+ let encoded = <$ty as SolParamsEncode >:: encode( & $val) ;
564
+ let encoded_alloy = <$sol_ty as $sol_trait>:: abi_encode_params( & $sol_val) ;
565
+ assert_eq!( encoded, encoded_alloy) ;
566
+
567
+ // `SolParamsDecode` test.
568
+ let decoded = <$ty as SolParamsDecode >:: decode( & encoded) ;
569
+ let decoded_alloy = <$sol_ty as $sol_trait>:: abi_decode_params( & encoded) ;
570
+ assert_eq!( decoded$( $ty_cvt) * , decoded_alloy$( $sol_ty_cvt) * ) ;
571
+ } ;
572
+ }
573
+
574
+ #[ test]
575
+ fn params_works ( ) {
576
+ test_case_params ! ( ( ) , ( ) ) ;
577
+ test_case_params ! ( ( bool , ) , ( true , ) ) ;
578
+ // `SolValue` isn't implemented for `u8`.
579
+ test_case_params ! ( ( u8 , ) , ( 100u8 , ) , ( sol_data:: Uint <8 >, ) , AlloySolType ) ;
580
+ test_case_params ! (
581
+ ( bool , i8 , u32 , String ) ,
582
+ ( true , 100i8 , 1_000_000u32 , String :: from( "Hello, world!" ) )
583
+ ) ;
584
+
585
+ // simple sequences/collections.
586
+ test_case_params ! ( ( [ i8 ; 32 ] , ) , ( [ 100i8 ; 32 ] , ) ) ;
587
+ test_case_params ! ( ( Vec <i8 >, ) , ( Vec :: from( [ 100i8 ; 64 ] ) , ) ) ;
588
+ test_case_params ! ( ( [ i8 ; 32 ] , Vec <i8 >) , ( [ 100i8 ; 32 ] , Vec :: from( [ 100i8 ; 64 ] ) ) ) ;
589
+
590
+ // sequences of addresses.
591
+ test_case_params ! (
592
+ ( [ Address ; 4 ] , ) , ( [ Address :: from( [ 1 ; 20 ] ) ; 4 ] , ) ,
593
+ ( [ AlloyAddress ; 4 ] , ) , SolValue , ( [ AlloyAddress :: from( [ 1 ; 20 ] ) ; 4 ] , ) ,
594
+ [ . unwrap( ) . 0 . map( |val| val. 0 ) ] , [ . unwrap( ) . 0 . map( |val| val. 0 ) ]
595
+ ) ;
596
+ test_case_params ! (
597
+ ( Vec <Address >, ) , ( Vec :: from( [ Address :: from( [ 1 ; 20 ] ) ; 4 ] ) , ) ,
598
+ ( Vec <AlloyAddress >, ) , SolValue , ( Vec :: from( [ AlloyAddress :: from( [ 1 ; 20 ] ) ; 4 ] ) , ) ,
599
+ [ . unwrap( ) . 0 . into_iter( ) . map( |val| val. 0 ) . collect:: <Vec <_>>( ) ] , [ . unwrap( ) . 0 . into_iter( ) . map( |val| val. 0 ) . collect:: <Vec <_>>( ) ]
600
+ ) ;
601
+
602
+ // fixed-size byte arrays.
603
+ test_case_params ! (
604
+ ( SolBytes <[ u8 ; 32 ] >, ) ,
605
+ ( SolBytes ( [ 100u8 ; 32 ] ) , ) ,
606
+ ( AlloyFixedBytes <32 >, ) ,
607
+ SolValue ,
608
+ ( AlloyFixedBytes ( [ 100u8 ; 32 ] ) , ) ,
609
+ [ . unwrap( ) . 0.0 ] ,
610
+ [ . unwrap( ) . 0.0 ]
611
+ ) ;
612
+
613
+ // dynamic size byte arrays.
614
+ test_case_params ! (
615
+ ( SolBytes <Vec <u8 >>, ) ,
616
+ ( SolBytes ( Vec :: from( [ 100u8 ; 64 ] ) ) , ) ,
617
+ ( AlloyBytes , ) ,
618
+ SolValue ,
619
+ ( AlloyBytes :: from( [ 100u8 ; 64 ] ) , ) ,
620
+ [ . unwrap( ) . 0.0 ] ,
621
+ [ . unwrap( ) . 0.0 ]
622
+ ) ;
623
+ }
0 commit comments