Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scale encoding for transfer #745

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading