From e125999f798a3f6068f4440f447a3df92528061e Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Mon, 10 Jul 2023 18:50:39 +0100 Subject: [PATCH] scale encoding for transfer (#745) --- .../745-scale-encoding-for-transfer-packet.md | 2 ++ .../ibc/src/applications/transfer/amount.rs | 24 ++++++++++++++++++- crates/ibc/src/applications/transfer/coin.rs | 4 ++++ .../applications/transfer/msgs/transfer.rs | 5 ++++ .../ibc/src/applications/transfer/packet.rs | 4 ++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 .changelog/unreleased/improvement/745-scale-encoding-for-transfer-packet.md diff --git a/.changelog/unreleased/improvement/745-scale-encoding-for-transfer-packet.md b/.changelog/unreleased/improvement/745-scale-encoding-for-transfer-packet.md new file mode 100644 index 000000000..9c00f98ec --- /dev/null +++ b/.changelog/unreleased/improvement/745-scale-encoding-for-transfer-packet.md @@ -0,0 +1,2 @@ +- Scale encoding for ICS-20 transfer message + ([#745](https://github.com/cosmos/ibc-rs/issues/745)) \ No newline at end of file diff --git a/crates/ibc/src/applications/transfer/amount.rs b/crates/ibc/src/applications/transfer/amount.rs index 50ba5fe9f..2b266a5ca 100644 --- a/crates/ibc/src/applications/transfer/amount.rs +++ b/crates/ibc/src/applications/transfer/amount.rs @@ -1,6 +1,6 @@ //! Contains the `Amount` type, which represents amounts of tokens transferred. -use core::str::FromStr; +use core::{ops::Deref, str::FromStr}; use derive_more::{Display, From, Into}; use super::error::TokenTransferError; @@ -11,6 +11,28 @@ use primitive_types::U256; #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Display, From, Into)] pub struct Amount(U256); +#[cfg(feature = "parity-scale-codec")] +impl parity_scale_codec::WrapperTypeDecode for Amount { + type Wrapped = [u64; 4]; +} + +#[cfg(feature = "parity-scale-codec")] +impl parity_scale_codec::WrapperTypeEncode for Amount {} + +impl Deref for Amount { + type Target = [u64; 4]; + + fn deref(&self) -> &Self::Target { + &self.0 .0 + } +} + +impl From<[u64; 4]> for Amount { + fn from(value: [u64; 4]) -> Self { + Self(U256(value)) + } +} + impl Amount { pub fn checked_add(self, rhs: Self) -> Option { self.0.checked_add(rhs.0).map(Self) diff --git a/crates/ibc/src/applications/transfer/coin.rs b/crates/ibc/src/applications/transfer/coin.rs index abffa03cb..f95988093 100644 --- a/crates/ibc/src/applications/transfer/coin.rs +++ b/crates/ibc/src/applications/transfer/coin.rs @@ -23,6 +23,10 @@ pub type RawCoin = Coin; /// Coin defines a token with a denomination and an amount. #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr( + feature = "parity-scale-codec", + derive(parity_scale_codec::Encode, parity_scale_codec::Decode,) +)] #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] pub struct Coin { /// Denomination diff --git a/crates/ibc/src/applications/transfer/msgs/transfer.rs b/crates/ibc/src/applications/transfer/msgs/transfer.rs index 9039c88ab..746386f26 100644 --- a/crates/ibc/src/applications/transfer/msgs/transfer.rs +++ b/crates/ibc/src/applications/transfer/msgs/transfer.rs @@ -24,6 +24,11 @@ pub(crate) const TYPE_URL: &str = "/ibc.applications.transfer.v1.MsgTransfer"; /// have to specify the information related to the transfer of the token, and /// let the library figure out how to build the packet properly. #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr( + feature = "parity-scale-codec", + derive(parity_scale_codec::Encode, parity_scale_codec::Decode,) +)] pub struct MsgTransfer { /// the port on which the packet will be sent pub port_id_on_a: PortId, diff --git a/crates/ibc/src/applications/transfer/packet.rs b/crates/ibc/src/applications/transfer/packet.rs index 8bb5e59fa..89f1d5b4e 100644 --- a/crates/ibc/src/applications/transfer/packet.rs +++ b/crates/ibc/src/applications/transfer/packet.rs @@ -16,6 +16,10 @@ use crate::signer::Signer; feature = "serde", serde(try_from = "RawPacketData", into = "RawPacketData") )] +#[cfg_attr( + feature = "parity-scale-codec", + derive(parity_scale_codec::Encode, parity_scale_codec::Decode,) +)] #[derive(Clone, Debug, PartialEq, Eq)] pub struct PacketData { pub token: PrefixedCoin,