Skip to content

Commit

Permalink
fix to get memo for NFT transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Jan 24, 2024
1 parent b335442 commit 3dbdb20
Show file tree
Hide file tree
Showing 12 changed files with 482 additions and 546 deletions.
164 changes: 91 additions & 73 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ flate2 = "1.0.22"
fs_extra = "1.2.0"
futures = "0.3"
git2 = "0.18.1"
ibc = {git = "https://github.com/cosmos/ibc-rs", rev = "5053973072f0f955e5d11a048293712c92c4a676", features = ["serde"]}
ibc-derive = "0.5.0"
ibc-testkit = {git = "https://github.com/cosmos/ibc-rs", rev = "5053973072f0f955e5d11a048293712c92c4a676", default-features = false}
ibc = {version = "0.50.0", features = ["serde"]}
ibc-derive = "0.6.0"
ibc-testkit = {version = "0.50.0", default-features = false}
ics23 = "0.11.0"
index-set = { git = "https://github.com/heliaxdev/index-set", tag = "v0.8.1", features = ["serialize-borsh", "serialize-serde"] }
itertools = "0.10.0"
Expand Down
60 changes: 20 additions & 40 deletions crates/core/src/types/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::ibc::apps::nft_transfer::context::{NftClassContext, NftContext};
use crate::ibc::apps::nft_transfer::types::error::NftTransferError;
use crate::ibc::apps::nft_transfer::types::msgs::transfer::MsgTransfer as IbcMsgNftTransfer;
use crate::ibc::apps::nft_transfer::types::{
ClassData, ClassId, ClassUri, PrefixedClassId, TokenData, TokenId,
TokenUri, TracePath as NftTracePath,
ClassData, ClassId, ClassUri, Memo as NftMemo, PrefixedClassId, TokenData,
TokenId, TokenUri, TracePath as NftTracePath,
};
use crate::ibc::apps::transfer::types::msgs::transfer::MsgTransfer as IbcMsgTransfer;
use crate::ibc::apps::transfer::types::{Memo, PrefixedDenom, TracePath};
Expand All @@ -25,27 +25,16 @@ use crate::ibc::core::handler::types::events::{
};
use crate::ibc::primitives::proto::Protobuf;
use crate::tendermint::abci::Event as AbciEvent;
use crate::types::masp::PaymentAddress;
use crate::types::token::Transfer;

/// The event type defined in ibc-rs for receiving a token
pub const EVENT_TYPE_PACKET: &str = "fungible_token_packet";
/// The event type defined in ibc-rs for IBC denom
pub const EVENT_TYPE_DENOM_TRACE: &str = "denomination_trace";
/// The event attribute key defined in ibc-rs for IBC denom
pub const EVENT_ATTRIBUTE_DENOM: &str = "denom";
/// The event attribute key defined in ibc-rs for a receiver
pub const EVENT_ATTRIBUTE_RECEIVER: &str = "receiver";
/// The event attribute key defined in ibc-rs for a trace hash
pub const EVENT_ATTRIBUTE_TRACE: &str = "trace_hash";
/// The event type defined in ibc-rs for receiving an NFT
pub const EVENT_TYPE_NFT_PACKET: &str = "non_fungible_token_packet";
/// The event type defined in ibc-rs for NFT trace
pub const EVENT_TYPE_TOKEN_TRACE: &str = "token_trace";
/// The event attribute key defined in ibc-rs for NFT class ID
pub const EVENT_ATTRIBUTE_CLASS: &str = "class";
/// The event attribute key defined in ibc-rs for NFT token ID
pub const EVENT_ATTRIBUTE_TOKEN: &str = "token";
/// The event attribute key defined in ibc-rs for receiving result
pub const EVENT_ATTRIBUTE_SUCCESS: &str = "success";
/// The event attribute value defined in ibc-rs for receiving success
pub const EVENT_VALUE_SUCCESS: &str = "true";
/// The escrow address for IBC transfer
pub const IBC_ESCROW_ADDRESS: Address = Address::Internal(InternalAddress::Ibc);

Expand Down Expand Up @@ -295,31 +284,22 @@ impl TryFrom<Memo> for IbcShieldedTransfer {
}
}

/// Get the shielded transfer from the memo
pub fn get_shielded_transfer(
event: &IbcEvent,
) -> Result<Option<IbcShieldedTransfer>> {
if event.event_type != EVENT_TYPE_PACKET {
// This event is not for receiving a token
return Ok(None);
}
let is_success =
event.attributes.get("success") == Some(&"true".to_string());
let receiver = event.attributes.get("receiver");
let is_shielded = if let Some(receiver) = receiver {
PaymentAddress::from_str(receiver).is_ok()
} else {
false
};
if !is_success || !is_shielded {
return Ok(None);
impl From<IbcShieldedTransfer> for NftMemo {
fn from(shielded: IbcShieldedTransfer) -> Self {
let bytes = shielded.serialize_to_vec();
HEXUPPER.encode(&bytes).into()
}
}

event
.attributes
.get("memo")
.map(|memo| IbcShieldedTransfer::try_from(Memo::from(memo.clone())))
.transpose()
impl TryFrom<NftMemo> for IbcShieldedTransfer {
type Error = Error;

fn try_from(memo: NftMemo) -> Result<Self> {
let bytes = HEXUPPER
.decode(memo.as_ref().as_bytes())
.map_err(Error::DecodingHex)?;
Self::try_from_slice(&bytes).map_err(Error::DecodingShieldedTransfer)
}
}

/// NFT class
Expand Down
1 change: 1 addition & 0 deletions crates/ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ masp_primitives.workspace = true
primitive-types.workspace = true
proptest = { workspace = true, optional = true }
prost.workspace = true
serde_json.workspace = true
sha2.workspace = true
thiserror.workspace = true
tracing.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/ibc/src/context/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! AnyClientState and AnyConsensusState for IBC context

use ibc_derive::ConsensusState;
use ibc_derive::IbcConsensusState;
#[cfg(feature = "testing")]
use ibc_testkit::testapp::ibc::clients::mock::client_state::MockClientContext;
#[cfg(feature = "testing")]
Expand Down Expand Up @@ -85,7 +85,7 @@ impl TryFrom<Any> for AnyClientState {
}

/// ConsensusState for light clients
#[derive(ConsensusState)]
#[derive(IbcConsensusState)]
pub enum AnyConsensusState {
/// Tendermint consensus state
Tendermint(TmConsensusState),
Expand Down
Loading

0 comments on commit 3dbdb20

Please sign in to comment.