@@ -6,7 +6,7 @@ use std::str::FromStr;
66use bigdecimal:: BigDecimal ;
77use namada_core:: masp:: MaspTxId ;
88use namada_core:: token:: Amount as NamadaAmount ;
9- use namada_events:: extend:: ReadFromEventAttributes ;
9+ use namada_events:: extend:: { InnerTxHash , ReadFromEventAttributes } ;
1010use namada_ibc:: IbcTxDataHash ;
1111use namada_ibc:: apps:: transfer:: types:: packet:: PacketData as Ics20PacketData ;
1212use namada_tx:: IndexedTx ;
@@ -60,6 +60,7 @@ pub struct BlockResult {
6060#[ derive( Debug , Clone ) ]
6161pub struct Event {
6262 pub kind : EventKind ,
63+ pub inner_tx_hash : Option < Id > ,
6364 pub attributes : Option < TxAttributesType > ,
6465}
6566
@@ -197,6 +198,34 @@ pub struct IbcPacket {
197198 pub data : String ,
198199}
199200
201+ impl IbcPacket {
202+ pub fn as_fungible_token_packet ( & self ) -> Option < FungibleTokenPacket > {
203+ let packet_data: Ics20PacketData =
204+ serde_json:: from_str ( & self . data ) . ok ( ) ?;
205+ let ibc_amount: NamadaAmount =
206+ packet_data. token . amount . try_into ( ) . ok ( ) ?;
207+
208+ Some ( FungibleTokenPacket {
209+ memo : packet_data. memo . to_string ( ) ,
210+ sender : packet_data. sender . to_string ( ) ,
211+ receiver : packet_data. receiver . to_string ( ) ,
212+ denom : packet_data. token . denom . to_string ( ) ,
213+ amount : Amount :: from ( ibc_amount) . into ( ) ,
214+ } )
215+ }
216+
217+ pub fn id ( & self ) -> String {
218+ format ! (
219+ "{}/{}/{}/{}/{}" ,
220+ self . dest_port,
221+ self . dest_channel,
222+ self . source_port,
223+ self . source_channel,
224+ self . sequence
225+ )
226+ }
227+ }
228+
200229#[ derive( Debug , Clone , Default ) ]
201230pub struct FungibleTokenPacket {
202231 pub sender : String ,
@@ -393,20 +422,11 @@ impl TxAttributesType {
393422 _ => return None ,
394423 } ;
395424
396- let packet_data: Ics20PacketData =
397- serde_json:: from_str ( & packet. data ) . ok ( ) ?;
398- let ibc_amount: NamadaAmount =
399- packet_data. token . amount . try_into ( ) . ok ( ) ?;
400-
401- let ics20_packet = FungibleTokenPacket {
402- memo : packet_data. memo . to_string ( ) ,
403- sender : packet_data. sender . to_string ( ) ,
404- receiver : packet_data. receiver . to_string ( ) ,
405- denom : packet_data. token . denom . to_string ( ) ,
406- amount : Amount :: from ( ibc_amount) . into ( ) ,
407- } ;
408-
409- Some ( ( action, Some ( packet) , Cow :: Owned ( ics20_packet) ) )
425+ Some ( (
426+ action,
427+ Some ( packet) ,
428+ Cow :: Owned ( packet. as_fungible_token_packet ( ) ?) ,
429+ ) )
410430 }
411431}
412432
@@ -430,7 +450,12 @@ impl From<TendermintBlockResultResponse> for BlockResult {
430450 ) ;
431451 let attributes =
432452 TxAttributesType :: deserialize ( & kind, & raw_attributes) ;
433- Event { kind, attributes }
453+ let inner_tx_hash = try_parse_inner_tx_hash ( & raw_attributes) ;
454+ Event {
455+ kind,
456+ attributes,
457+ inner_tx_hash,
458+ }
434459 } )
435460 . collect :: < Vec < Event > > ( ) ;
436461 let end_events = value
@@ -451,7 +476,12 @@ impl From<TendermintBlockResultResponse> for BlockResult {
451476 ) ;
452477 let attributes =
453478 TxAttributesType :: deserialize ( & kind, & raw_attributes) ;
454- Event { kind, attributes }
479+ let inner_tx_hash = try_parse_inner_tx_hash ( & raw_attributes) ;
480+ Event {
481+ kind,
482+ attributes,
483+ inner_tx_hash,
484+ }
455485 } )
456486 . collect :: < Vec < Event > > ( ) ;
457487 Self {
@@ -561,6 +591,14 @@ impl BlockResult {
561591 }
562592}
563593
594+ fn try_parse_inner_tx_hash (
595+ raw_attributes : & BTreeMap < String , String > ,
596+ ) -> Option < Id > {
597+ InnerTxHash :: read_opt_from_event_attributes ( raw_attributes)
598+ . expect ( "parsing the inner tx hash shouldn't fail" )
599+ . map ( |hash| Id :: Hash ( hash. to_string ( ) . to_lowercase ( ) ) )
600+ }
601+
564602#[ cfg( test) ]
565603mod tests {
566604 use super :: * ;
@@ -594,6 +632,7 @@ mod tests {
594632
595633 Some ( Event {
596634 kind,
635+ inner_tx_hash : None ,
597636 attributes : parsed_attributes,
598637 } )
599638 } )
@@ -604,6 +643,7 @@ mod tests {
604643 events. remove( 0 ) ,
605644 Event {
606645 kind: EventKind :: FungibleTokenPacket ,
646+ inner_tx_hash: None ,
607647 attributes: Some (
608648 TxAttributesType :: FungibleTokenPacket {
609649 is_ack: true ,
0 commit comments