11#![ allow( unused_imports, dead_code) ]
22
3+ use fe_abi:: event:: AbiEvent ;
4+ use fe_abi:: types:: { AbiTupleField , AbiType } ;
35pub use fe_codegen:: db:: { CodegenDb , Db } ;
46
57use fe_analyzer:: namespace:: items:: { ContractId , FunctionId , IngotId , IngotMode , ModuleId } ;
68use fe_common:: diagnostics:: Diagnostic ;
79use fe_common:: files:: FileKind ;
810use fe_common:: { db:: Upcast , utils:: files:: BuildFiles } ;
911use fe_parser:: ast:: SmolStr ;
12+ use fe_test_runner:: ethabi:: { Event , EventParam , ParamType } ;
1013use fe_test_runner:: TestSink ;
1114use indexmap:: { indexmap, IndexMap } ;
1215use serde_json:: Value ;
@@ -31,20 +34,70 @@ pub struct CompiledContract {
3134#[ derive( Debug , Clone , PartialEq , Eq ) ]
3235pub struct CompiledTest {
3336 pub name : SmolStr ,
37+ events : Vec < AbiEvent > ,
3438 bytecode : String ,
3539}
3640
3741#[ cfg( feature = "solc-backend" ) ]
3842impl CompiledTest {
39- pub fn new ( name : SmolStr , bytecode : String ) -> Self {
40- Self { name, bytecode }
43+ pub fn new ( name : SmolStr , events : Vec < AbiEvent > , bytecode : String ) -> Self {
44+ Self {
45+ name,
46+ events,
47+ bytecode,
48+ }
4149 }
4250
4351 pub fn execute ( & self , sink : & mut TestSink ) -> bool {
44- fe_test_runner:: execute ( & self . name , & self . bytecode , sink)
52+ let events = map_abi_events ( & self . events ) ;
53+ fe_test_runner:: execute ( & self . name , & events, & self . bytecode , sink)
54+ }
55+ }
56+
57+ fn map_abi_events ( events : & [ AbiEvent ] ) -> Vec < Event > {
58+ events. iter ( ) . map ( map_abi_event) . collect ( )
59+ }
60+
61+ fn map_abi_event ( event : & AbiEvent ) -> Event {
62+ let inputs = event
63+ . inputs
64+ . iter ( )
65+ . map ( |input| {
66+ let kind = map_abi_type ( & input. ty ) ;
67+ EventParam {
68+ name : input. name . to_owned ( ) ,
69+ kind,
70+ indexed : input. indexed ,
71+ }
72+ } )
73+ . collect ( ) ;
74+ Event {
75+ name : event. name . to_owned ( ) ,
76+ inputs,
77+ anonymous : event. anonymous ,
4578 }
4679}
4780
81+ fn map_abi_type ( typ : & AbiType ) -> ParamType {
82+ match typ {
83+ AbiType :: UInt ( value) => ParamType :: Uint ( * value) ,
84+ AbiType :: Int ( value) => ParamType :: Int ( * value) ,
85+ AbiType :: Address => ParamType :: Address ,
86+ AbiType :: Bool => ParamType :: Bool ,
87+ AbiType :: Function => panic ! ( "function cannot be mapped to an actual ABI value type" ) ,
88+ AbiType :: Array { elem_ty, len } => {
89+ ParamType :: FixedArray ( Box :: new ( map_abi_type ( elem_ty) ) , * len)
90+ }
91+ AbiType :: Tuple ( params) => ParamType :: Tuple ( map_abi_types ( params) ) ,
92+ AbiType :: Bytes => ParamType :: Bytes ,
93+ AbiType :: String => ParamType :: String ,
94+ }
95+ }
96+
97+ fn map_abi_types ( fields : & [ AbiTupleField ] ) -> Vec < ParamType > {
98+ fields. iter ( ) . map ( |field| map_abi_type ( & field. ty ) ) . collect ( )
99+ }
100+
48101#[ derive( Debug ) ]
49102pub struct CompileError ( pub Vec < Diagnostic > ) ;
50103
@@ -168,7 +221,8 @@ fn compile_test(db: &mut Db, test: FunctionId, optimize: bool) -> CompiledTest {
168221 . to_string ( )
169222 . replace ( '"' , "\\ \" " ) ;
170223 let bytecode = compile_to_evm ( "test" , & yul_test, optimize) ;
171- CompiledTest :: new ( test. name ( db) , bytecode)
224+ let events = db. codegen_abi_module_events ( test. module ( db) ) ;
225+ CompiledTest :: new ( test. name ( db) , events, bytecode)
172226}
173227
174228#[ cfg( feature = "solc-backend" ) ]
0 commit comments