Skip to content

Commit

Permalink
Factor out extracting tokens from IBC receive packet message into sep…
Browse files Browse the repository at this point in the history
…arate function.
  • Loading branch information
murisi committed Feb 10, 2025
1 parent af8d276 commit b0607df
Showing 1 changed file with 61 additions and 63 deletions.
124 changes: 61 additions & 63 deletions crates/ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,66 @@ where
})
}

// Extract the tokens used in a receive packet message
fn msg_recv_packet_tokens(
msg: &IbcMsgRecvPacket,
) -> Result<BTreeSet<Address>, Error> {
match msg.packet.port_id_on_b.as_str() {
PORT_ID_STR => {
// Record the token credited/debited in this
// transfer
let packet_data =
serde_json::from_slice::<PacketData>(&msg.packet.data)
.map_err(StorageError::new)
.map_err(Error::Storage)?;
let ibc_denom = packet_data.token.denom.to_string();
// Get the received token
let token = received_ibc_token(
ibc_denom,
&msg.packet.port_id_on_a,
&msg.packet.chan_id_on_a,
&msg.packet.port_id_on_b,
&msg.packet.chan_id_on_b,
)
.into_storage_result()
.map_err(Error::Storage)?;
Ok([token].into())
}
NFT_PORT_ID_STR => {
// Record the tokenS credited/debited in this NFT
// transfer
let packet_data =
serde_json::from_slice::<NftPacketData>(&msg.packet.data)
.map_err(StorageError::new)
.map_err(Error::Storage)?;
let ibc_traces: Vec<_> = packet_data
.token_ids
.0
.iter()
.map(|token_id| {
ibc_trace_for_nft(&packet_data.class_id, token_id)
})
.collect();
let mut tokens = BTreeSet::new();
for ibc_trace in ibc_traces {
// Get the received token
let token = received_ibc_token(
ibc_trace,
&msg.packet.port_id_on_a,
&msg.packet.chan_id_on_a,
&msg.packet.port_id_on_b,
&msg.packet.chan_id_on_b,
)
.into_storage_result()
.map_err(Error::Storage)?;
tokens.insert(token);
}
Ok(tokens)
}
_ => Ok(BTreeSet::new()),
}
}

// Apply the given write acknowledge to the changed balances structure
fn apply_recv_msg<S>(
storage: &S,
Expand Down Expand Up @@ -746,69 +806,7 @@ where
.is_receiving_success(msg)?
.is_some_and(|ack_succ| ack_succ) =>
{
let tokens = if msg.packet.port_id_on_b.as_str()
== PORT_ID_STR
{
// Record the token credited/debited in this
// transfer
let packet_data =
serde_json::from_slice::<PacketData>(
&msg.packet.data,
)
.map_err(StorageError::new)
.map_err(Error::Storage)?;
let ibc_denom = packet_data.token.denom.to_string();
// Get the received token
let token = received_ibc_token(
ibc_denom,
&msg.packet.port_id_on_a,
&msg.packet.chan_id_on_a,
&msg.packet.port_id_on_b,
&msg.packet.chan_id_on_b,
)
.into_storage_result()
.map_err(Error::Storage)?;
[token].into()
} else if msg.packet.port_id_on_b.as_str()
== NFT_PORT_ID_STR
{
// Record the tokenS credited/debited in this NFT
// transfer
let packet_data =
serde_json::from_slice::<NftPacketData>(
&msg.packet.data,
)
.map_err(StorageError::new)
.map_err(Error::Storage)?;
let ibc_traces: Vec<_> = packet_data
.token_ids
.0
.iter()
.map(|token_id| {
ibc_trace_for_nft(
&packet_data.class_id,
token_id,
)
})
.collect();
let mut tokens = BTreeSet::new();
for ibc_trace in ibc_traces {
// Get the received token
let token = received_ibc_token(
ibc_trace,
&msg.packet.port_id_on_a,
&msg.packet.chan_id_on_a,
&msg.packet.port_id_on_b,
&msg.packet.chan_id_on_b,
)
.into_storage_result()
.map_err(Error::Storage)?;
tokens.insert(token);
}
tokens
} else {
BTreeSet::new()
};
let tokens = msg_recv_packet_tokens(msg)?;
(extract_masp_tx_from_packet(&msg.packet), tokens)
}
#[cfg(is_apple_silicon)]
Expand Down

0 comments on commit b0607df

Please sign in to comment.