Skip to content

Commit

Permalink
Created a separate type that is either an Address or IBC receiver.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Jun 4, 2024
1 parent 6cbe813 commit 20d113b
Show file tree
Hide file tree
Showing 30 changed files with 338 additions and 161 deletions.
54 changes: 54 additions & 0 deletions crates/core/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,14 @@ impl TransferSource {
_ => None,
}
}

/// Get the contained MaybeIbcAddress, if any
pub fn maybe_ibc_address(&self) -> Option<MaybeIbcAddress> {
match self {
Self::Address(x) => Some(MaybeIbcAddress::Address(x.clone())),
_ => None,
}
}
}

impl Display for TransferSource {
Expand All @@ -410,6 +418,43 @@ impl Display for TransferSource {
}
}

/// Represents either a Namada address or some IBC address
#[derive(Debug, Clone, BorshDeserialize, BorshSerialize, BorshDeserializer)]
pub enum MaybeIbcAddress {
/// A transparent address
Address(Address),
/// An IBC address
Ibc(String),
}

impl MaybeIbcAddress {
/// Get the transparent address that this target would effectively go to
pub fn effective_address(&self) -> Address {
match self {
Self::Address(x) => x.clone(),
// An IBC signer address effectively means that assets are
// associated with the IBC internal address
Self::Ibc(_) => IBC,
}
}

/// Get the contained IBC receiver, if any
pub fn payment_address(&self) -> Option<String> {
match self {
Self::Ibc(address) => Some(address.clone()),
_ => None,
}
}

/// Get the contained Address, if any
pub fn address(&self) -> Option<Address> {
match self {
Self::Address(x) => Some(x.clone()),
_ => None,
}
}
}

/// Represents a target for the funds of a transfer
#[derive(Debug, Clone, BorshDeserialize, BorshSerialize, BorshDeserializer)]
pub enum TransferTarget {
Expand Down Expand Up @@ -450,6 +495,15 @@ impl TransferTarget {
_ => None,
}
}

/// Get the contained MaybeIbcAddress, if any
pub fn maybe_ibc_address(&self) -> Option<MaybeIbcAddress> {
match self {
Self::Address(x) => Some(MaybeIbcAddress::Address(x.clone())),
Self::Ibc(x) => Some(MaybeIbcAddress::Ibc(x.clone())),
_ => None,
}
}
}

impl Display for TransferTarget {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ impl From<Amount> for IbcAmount {

impl From<DenominatedAmount> for IbcAmount {
fn from(amount: DenominatedAmount) -> Self {
amount.canonical().amount.into()
amount.amount.into()
}
}

Expand Down
15 changes: 15 additions & 0 deletions crates/events/src/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,21 @@ impl EventAttributeEntry<'static> for Info {
}
}

/// Extend an [`Event`] with `packet_ack` data, indicating the success or
/// failure of processing a received packet.
pub struct PacketAck(pub Vec<u8>);

impl EventAttributeEntry<'static> for PacketAck {
type Value = Vec<u8>;
type ValueOwned = Self::Value;

const KEY: &'static str = "packet_ack";

fn into_value(self) -> Self::Value {
self.0
}
}

/// Extend an [`Event`] with `masp_tx_block_index` data, indicating that the tx
/// at the specified index in the block contains a valid masp transaction.
pub struct MaspTxBlockIndex(pub TxIndex);
Expand Down
11 changes: 1 addition & 10 deletions crates/ibc/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,9 @@ where
D: DB + for<'iter> DBIter<'iter> + 'static,
H: StorageHasher + 'static,
{
let denom = token::read_denom(state, token)?.ok_or_else(|| {
StorageError::new_alloc(format!("No denomination for {token}"))
})?;
let amount = DenominatedAmount::new(target.amount, denom).canonical();
if amount.denom().0 != 0 {
return Err(StorageError::new_alloc(format!(
"The amount for the IBC transfer should be an integer: {amount}"
)));
}
let token = PrefixedCoin {
denom: token.to_string().parse().expect("invalid token"),
amount: amount.amount().into(),
amount: target.amount.into(),
};
let packet_data = PacketData {
token,
Expand Down
5 changes: 1 addition & 4 deletions crates/ibc/src/context/token_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ where
};

// Convert IBC amount to Namada amount for the token
let denom = read_denom(&*self.inner.borrow(), &token)
.map_err(ContextError::from)?
.unwrap_or(Denomination(0));
let uint_amount = Uint(primitive_types::U256::from(coin.amount).0);
let amount = Amount::from_uint(uint_amount, denom).map_err(|e| {
let amount = Amount::from_uint(uint_amount, 0).map_err(|e| {
TokenTransferError::ContextError(
ChannelError::Other {
description: format!(
Expand Down
Loading

0 comments on commit 20d113b

Please sign in to comment.