Skip to content

Commit

Permalink
scale encoding for transfer (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda authored Jul 10, 2023
1 parent d3ec8ec commit 5f1f1fc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Scale encoding for ICS-20 transfer message
([#745](https://github.com/cosmos/ibc-rs/issues/745))
24 changes: 23 additions & 1 deletion crates/ibc/src/applications/transfer/amount.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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> {
self.0.checked_add(rhs.0).map(Self)
Expand Down
4 changes: 4 additions & 0 deletions crates/ibc/src/applications/transfer/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub type RawCoin = Coin<String>;

/// 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<D> {
/// Denomination
Expand Down
5 changes: 5 additions & 0 deletions crates/ibc/src/applications/transfer/msgs/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions crates/ibc/src/applications/transfer/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5f1f1fc

Please sign in to comment.