1+ use std:: collections:: BTreeMap ;
2+
13use anyhow:: Context ;
24use namada_ibc:: apps:: nft_transfer:: types:: PORT_ID_STR as NFT_PORT_ID_STR ;
35use namada_ibc:: apps:: transfer:: types:: packet:: PacketData as FtPacketData ;
@@ -10,7 +12,7 @@ use namada_ibc::core::channel::types::msgs::PacketMsg;
1012use namada_ibc:: core:: channel:: types:: packet:: Packet ;
1113use namada_ibc:: core:: handler:: types:: msgs:: MsgEnvelope ;
1214use namada_ibc:: core:: host:: types:: identifiers:: { ChannelId , PortId } ;
13- use namada_sdk:: address:: Address ;
15+ use namada_sdk:: address:: { Address , IBC } ;
1416use namada_sdk:: token:: Transfer ;
1517
1618use crate :: id:: Id ;
@@ -275,34 +277,57 @@ pub fn transfer_to_ibc_tx_kind(
275277 & transfer. message . packet_data . token . denom ,
276278 ) ?;
277279
278- let transfer_data = TransferData {
279- sources : crate :: ser:: AccountsMap (
280- [ (
281- convert_account (
282- & transfer. message . packet_data ,
283- token. clone ( ) ,
284- true ,
285- )
286- . expect ( "Should be able to convert sender" ) ,
287- denominated_amount,
288- ) ]
289- . into ( ) ,
290- ) ,
291- targets : crate :: ser:: AccountsMap (
292- [ (
293- ChainAddress :: ExternalAccount (
294- transfer. message . packet_data . receiver . to_string ( ) ,
295- token,
296- ) ,
297- denominated_amount,
298- ) ]
299- . into ( ) ,
280+ // Extract the sources, targets and shielded section hash from the
281+ // transfer data. Remove the occurences of the IBC address since we
282+ // will replace them with more accurate data
283+ let ( mut sources, mut targets, shielded_section_hash) = transfer
284+ . transfer
285+ . clone ( )
286+ . map ( |t| {
287+ (
288+ t. sources
289+ . into_iter ( )
290+ . filter_map ( |( source, amount) | {
291+ ( source. owner != IBC ) . then_some ( (
292+ ChainAddress :: ChainAccount ( source) ,
293+ amount,
294+ ) )
295+ } )
296+ . collect :: < BTreeMap < _ , _ > > ( ) ,
297+ t. targets
298+ . into_iter ( )
299+ . filter_map ( |( target, amount) | {
300+ ( target. owner != IBC ) . then_some ( (
301+ ChainAddress :: ChainAccount ( target) ,
302+ amount,
303+ ) )
304+ } )
305+ . collect :: < BTreeMap < _ , _ > > ( ) ,
306+ t. shielded_section_hash ,
307+ )
308+ } )
309+ . unwrap_or_default ( ) ;
310+ // Extend sources and targets with the actual IBC addresses
311+ sources. insert (
312+ convert_account (
313+ & transfer. message . packet_data ,
314+ token. clone ( ) ,
315+ true ,
316+ )
317+ . expect ( "Should be able to convert sender" ) ,
318+ denominated_amount,
319+ ) ;
320+ targets. insert (
321+ ChainAddress :: ExternalAccount (
322+ transfer. message . packet_data . receiver . to_string ( ) ,
323+ token,
300324 ) ,
301- shielded_section_hash : transfer
302- . transfer
303- . clone ( )
304- . map ( |t| t. shielded_section_hash )
305- . unwrap_or_default ( ) ,
325+ denominated_amount,
326+ ) ;
327+ let transfer_data = TransferData {
328+ sources : crate :: ser:: AccountsMap ( sources) ,
329+ targets : crate :: ser:: AccountsMap ( targets) ,
330+ shielded_section_hash,
306331 } ;
307332
308333 if transfer. transfer . is_some ( ) {
0 commit comments