Skip to content

Commit

Permalink
custom serde packet data with option
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Jan 21, 2024
1 parent 82165da commit f3806e5
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 166 deletions.
8 changes: 4 additions & 4 deletions ibc-apps/ics721-nft-transfer/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub fn refund_packet_nft_execute(
// mint vouchers back to sender
else {
for (i, token_id) in data.token_ids.0.iter().enumerate() {
let token_uri = data.token_uris.get(i);
let token_data = data.token_data.get(i);
let token_uri = data.token_uris.as_ref().and_then(|uris| uris.get(i));
let token_data = data.token_data.as_ref().and_then(|data| data.get(i));
ctx_a.mint_nft_execute(&sender, &data.class_id, token_id, token_uri, token_data)?;
}
Ok(())
Expand Down Expand Up @@ -76,8 +76,8 @@ pub fn refund_packet_nft_validate(
})
} else {
for (i, token_id) in data.token_ids.0.iter().enumerate() {
let token_uri = data.token_uris.get(i);
let token_data = data.token_data.get(i);
let token_uri = data.token_uris.as_ref().and_then(|uris| uris.get(i));
let token_data = data.token_data.as_ref().and_then(|data| data.get(i));
ctx_a.mint_nft_validate(&sender, &data.class_id, token_id, token_uri, token_data)?;
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions ibc-apps/ics721-nft-transfer/src/handler/on_recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ where
log: Vec::new(),
};
for (i, token_id) in data.token_ids.0.iter().enumerate() {
let token_uri = data.token_uris.get(i);
let token_data = data.token_data.get(i);
let token_uri = data.token_uris.as_ref().and_then(|uris| uris.get(i));
let token_data = data.token_data.as_ref().and_then(|data| data.get(i));

let trace_event = TokenTraceEvent {
trace_hash: ctx_b.token_hash_string(&class_id, token_id),
Expand Down
56 changes: 43 additions & 13 deletions ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ where
let class_id = &packet_data.class_id;
let token_ids = &packet_data.token_ids;
// overwrite even if they are set in MsgTransfer
packet_data.token_uris.clear();
packet_data.token_data.clear();
if let Some(uris) = &mut packet_data.token_uris {
uris.clear();
}
if let Some(data) = &mut packet_data.token_data {
data.clear();
}
for token_id in token_ids.as_ref() {
if is_sender_chain_source(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone(), class_id) {
transfer_ctx.escrow_nft_validate(
Expand All @@ -77,16 +81,27 @@ where
&msg.chan_id_on_a,
class_id,
token_id,
&packet_data.memo,
&packet_data.memo.clone().unwrap_or_default(),
)?;
} else {
transfer_ctx.burn_nft_validate(&sender, class_id, token_id, &packet_data.memo)?;
transfer_ctx.burn_nft_validate(
&sender,
class_id,
token_id,
&packet_data.memo.clone().unwrap_or_default(),
)?;
}
let nft = transfer_ctx.get_nft(class_id, token_id)?;
// Set the URI and the data if both exists
if let (Some(uri), Some(data)) = (nft.get_uri(), nft.get_data()) {
packet_data.token_uris.push(uri.clone());
packet_data.token_data.push(data.clone());
match &mut packet_data.token_uris {
Some(uris) => uris.push(uri.clone()),
None => packet_data.token_uris = Some(vec![uri.clone()]),
}
match &mut packet_data.token_data {
Some(token_data) => token_data.push(data.clone()),
None => packet_data.token_data = Some(vec![data.clone()]),
}
}
}

Expand Down Expand Up @@ -155,8 +170,12 @@ where
let class_id = &packet_data.class_id;
let token_ids = &packet_data.token_ids;
// overwrite even if they are set in MsgTransfer
packet_data.token_uris.clear();
packet_data.token_data.clear();
if let Some(uris) = &mut packet_data.token_uris {
uris.clear();
}
if let Some(data) = &mut packet_data.token_data {
data.clear();
}
for token_id in token_ids.as_ref() {
if is_sender_chain_source(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone(), class_id) {
transfer_ctx.escrow_nft_execute(
Expand All @@ -165,16 +184,27 @@ where
&msg.chan_id_on_a,
class_id,
token_id,
&packet_data.memo,
&packet_data.memo.clone().unwrap_or_default(),
)?;
} else {
transfer_ctx.burn_nft_execute(&sender, class_id, token_id, &packet_data.memo)?;
transfer_ctx.burn_nft_execute(
&sender,
class_id,
token_id,
&packet_data.memo.clone().unwrap_or_default(),
)?;
}
let nft = transfer_ctx.get_nft(class_id, token_id)?;
// Set the URI and the data if both exists
if let (Some(uri), Some(data)) = (nft.get_uri(), nft.get_data()) {
packet_data.token_uris.push(uri.clone());
packet_data.token_data.push(data.clone());
match &mut packet_data.token_uris {
Some(uris) => uris.push(uri.clone()),
None => packet_data.token_uris = Some(vec![uri.clone()]),
}
match &mut packet_data.token_data {
Some(token_data) => token_data.push(data.clone()),
None => packet_data.token_data = Some(vec![data.clone()]),
}
}
}

Expand Down Expand Up @@ -212,7 +242,7 @@ where
receiver: packet_data.receiver,
class: packet_data.class_id,
tokens: packet_data.token_ids,
memo: packet_data.memo,
memo: packet_data.memo.unwrap_or_default(),
};
send_packet_ctx_a.emit_ibc_event(ModuleEvent::from(transfer_event).into())?;

Expand Down
6 changes: 3 additions & 3 deletions ibc-apps/ics721-nft-transfer/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn on_recv_packet_execute(
receiver: data.receiver,
class: data.class_id,
tokens: data.token_ids,
memo: data.memo,
memo: data.memo.unwrap_or_default(),
success: ack.is_successful(),
};
extras.events.push(recv_event.into());
Expand Down Expand Up @@ -259,7 +259,7 @@ pub fn on_acknowledgement_packet_execute(
receiver: data.receiver,
class: data.class_id,
tokens: data.token_ids,
memo: data.memo,
memo: data.memo.unwrap_or_default(),
acknowledgement: acknowledgement.clone(),
};

Expand Down Expand Up @@ -307,7 +307,7 @@ pub fn on_timeout_packet_execute(
refund_receiver: data.sender,
refund_class: data.class_id,
refund_tokens: data.token_ids,
memo: data.memo,
memo: data.memo.unwrap_or_default(),
};

let extras = ModuleExtras {
Expand Down
26 changes: 25 additions & 1 deletion ibc-apps/ics721-nft-transfer/types/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use core::fmt::{self, Display, Formatter};
use core::str::FromStr;

use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use ibc_core::primitives::prelude::*;
use mime::Mime;

Expand All @@ -19,7 +21,6 @@ use crate::error::NftTransferError;
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, Default, PartialEq, Eq, derive_more::From)]
pub struct Data(String);
Expand All @@ -45,6 +46,29 @@ impl FromStr for Data {
}
}

impl serde::Serialize for Data {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&BASE64_STANDARD.encode(&self.0))
}
}

impl<'de> serde::Deserialize<'de> for Data {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let encoded = String::deserialize(deserializer)?;
let decoded = BASE64_STANDARD
.decode(encoded)
.map_err(serde::de::Error::custom)?;
let decoded_str = String::from_utf8(decoded).map_err(serde::de::Error::custom)?;
Ok(Data(decoded_str))
}
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
Expand Down
2 changes: 1 addition & 1 deletion ibc-apps/ics721-nft-transfer/types/src/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use ibc_core::primitives::prelude::*;
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Memo(String);

impl AsRef<str> for Memo {
Expand Down
18 changes: 14 additions & 4 deletions ibc-apps/ics721-nft-transfer/types/src/msgs/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ impl TryFrom<RawMsgTransfer> for MsgTransfer {
return Err(ContextError::from(PacketError::MissingTimeout))?;
}

let memo = if raw_msg.memo.is_empty() {
None
} else {
Some(raw_msg.memo.into())
};

Ok(MsgTransfer {
port_id_on_a: raw_msg.source_port.parse()?,
chan_id_on_a: raw_msg.source_channel.parse()?,
Expand All @@ -74,11 +80,11 @@ impl TryFrom<RawMsgTransfer> for MsgTransfer {
class_uri: None,
class_data: None,
token_ids: raw_msg.token_ids.try_into()?,
token_uris: vec![],
token_data: vec![],
token_uris: None,
token_data: None,
sender: raw_msg.sender.into(),
receiver: raw_msg.receiver.into(),
memo: raw_msg.memo.into(),
memo,
},
timeout_height_on_b,
timeout_timestamp_on_b,
Expand All @@ -103,7 +109,11 @@ impl From<MsgTransfer> for RawMsgTransfer {
receiver: domain_msg.packet_data.receiver.to_string(),
timeout_height: domain_msg.timeout_height_on_b.into(),
timeout_timestamp: domain_msg.timeout_timestamp_on_b.nanoseconds(),
memo: domain_msg.packet_data.memo.to_string(),
memo: domain_msg
.packet_data
.memo
.map(|m| m.to_string())
.unwrap_or_default(),
}
}
}
Expand Down
Loading

0 comments on commit f3806e5

Please sign in to comment.