11mod trigger;
2+ use bindings:: wavs:: worker:: layer_types:: { TriggerData , TriggerSource } ;
23use trigger:: { decode_trigger_event, encode_trigger_output} ;
34pub mod bindings;
45use crate :: bindings:: { export, Guest , TriggerAction } ;
@@ -14,8 +15,21 @@ export!(Component with_types_in bindings);
1415
1516impl Guest for Component {
1617 fn run ( trigger_action : TriggerAction ) -> std:: result:: Result < Vec < u8 > , String > {
17- let ( trigger_id, req) =
18- decode_trigger_event ( trigger_action. data ) . map_err ( |e| e. to_string ( ) ) ?;
18+ enum Destination {
19+ Ethereum ,
20+ CliOutput
21+ }
22+ let ( ( trigger_id, req) , dest) = match & trigger_action. data {
23+ // When the trigger comes from WAVS, it's from an ethereum contract event
24+ TriggerData :: EthContractEvent ( _) => {
25+ ( decode_trigger_event ( trigger_action. data ) . map_err ( |e| e. to_string ( ) ) ?, Destination :: Ethereum )
26+ } ,
27+ // When we test in a CLI exec command, it's just raw bytes (we can make-up the trigger_id)
28+ TriggerData :: Raw ( data) => ( ( 0 , data. clone ( ) ) , Destination :: CliOutput ) ,
29+ TriggerData :: CosmosContractEvent ( _) => {
30+ return Err ( "Unsupported trigger data type" . to_string ( ) ) ;
31+ } ,
32+ } ;
1933
2034 // Convert bytes to string and parse first char as u64
2135 let input = std:: str:: from_utf8 ( & req) . map_err ( |e| e. to_string ( ) ) ?;
@@ -38,7 +52,12 @@ impl Guest for Component {
3852 } ) ;
3953
4054 match res {
41- Ok ( data) => Ok ( encode_trigger_output ( trigger_id, & data) ) ,
55+ Ok ( data) => match dest {
56+ Destination :: Ethereum => {
57+ Ok ( encode_trigger_output ( trigger_id, & data) )
58+ } ,
59+ Destination :: CliOutput => Ok ( data) ,
60+ } ,
4261 Err ( e) => Err ( e) ,
4362 }
4463 }
0 commit comments