diff --git a/tokens/src/imbalances.rs b/tokens/src/imbalances.rs index 76faac8585f08874f3222fc43261a3402e7980f4..084a3b9a5e39724c5688a76b6a576f9473074320 100644 --- a/tokens/src/imbalances.rs +++ b/tokens/src/imbalances.rs @@ -1,7 +1,7 @@ // wrapping these imbalances in a private module is necessary to ensure absolute // privacy of the inner member. use crate::{Config, TotalIssuance}; -use frame_support::traits::{Get, Imbalance, TryDrop}; +use frame_support::traits::{Get, Imbalance, SameOrOther, TryDrop}; use sp_runtime::traits::{Saturating, Zero}; use sp_std::{marker, mem, result}; @@ -21,6 +21,12 @@ impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> PositiveImbalance<T, GetCurre } } +impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Default for PositiveImbalance<T, GetCurrencyId> { + fn default() -> Self { + Self::zero() + } +} + /// Opaque, move-only struct with private fields that serves as a token /// denoting that funds have been destroyed without any equal and opposite /// accounting. @@ -37,6 +43,12 @@ impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> NegativeImbalance<T, GetCurre } } +impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Default for NegativeImbalance<T, GetCurrencyId> { + fn default() -> Self { + Self::zero() + } +} + impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> TryDrop for PositiveImbalance<T, GetCurrencyId> { fn try_drop(self) -> result::Result<(), Self> { self.drop_zero() @@ -73,14 +85,18 @@ impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Imbalance<T::Balance> for Pos self.0 = self.0.saturating_add(other.0); mem::forget(other); } - fn offset(self, other: Self::Opposite) -> result::Result<Self, Self::Opposite> { + // allow to make the impl same with `pallet-balances` + #[allow(clippy::comparison_chain)] + fn offset(self, other: Self::Opposite) -> SameOrOther<Self, Self::Opposite> { let (a, b) = (self.0, other.0); mem::forget((self, other)); - if a >= b { - Ok(Self::new(a - b)) + if a > b { + SameOrOther::Same(Self::new(a - b)) + } else if b > a { + SameOrOther::Other(NegativeImbalance::new(b - a)) } else { - Err(NegativeImbalance::new(b - a)) + SameOrOther::None } } fn peek(&self) -> T::Balance { @@ -124,14 +140,18 @@ impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Imbalance<T::Balance> for Neg self.0 = self.0.saturating_add(other.0); mem::forget(other); } - fn offset(self, other: Self::Opposite) -> result::Result<Self, Self::Opposite> { + // allow to make the impl same with `pallet-balances` + #[allow(clippy::comparison_chain)] + fn offset(self, other: Self::Opposite) -> SameOrOther<Self, Self::Opposite> { let (a, b) = (self.0, other.0); mem::forget((self, other)); - if a >= b { - Ok(Self::new(a - b)) + if a > b { + SameOrOther::Same(Self::new(a - b)) + } else if b > a { + SameOrOther::Other(PositiveImbalance::new(b - a)) } else { - Err(PositiveImbalance::new(b - a)) + SameOrOther::None } } fn peek(&self) -> T::Balance { diff --git a/xtokens/README.md b/xtokens/README.md index 7032f29b8dfb44893528bb97b3834f49bd177e2b..4e12469b155e176a652eb9a2e84e84da41ead8d1 100644 --- a/xtokens/README.md +++ b/xtokens/README.md @@ -12,28 +12,20 @@ The xtokens module provides functions for ## Notes -#### Unit tests - -Unit tests could be added once Polkadot has XCM simulator. https://github.com/paritytech/polkadot/issues/2544 - #### Integration tests Integration tests could be done manually after integrating xtokens into runtime. To cover the full features, set up at least 4 relay chain validators and 3 collators of different parachains, and use dispatchable calls to include all these scenarios: - Transfer relay chain tokens to relay chain. - - Use dispatchable call `transfer_to_relay_chain`. - Transfer tokens issued by parachain A, from parachain A to parachain B. - - Use dispatchable call `transfer_to_parachain`. - Sending the tx from parachain A. - Set the destination as Parachain B. - Set the currency ID as parachain A token. - Transfer tokens issued by parachain B, from parachain A to parachain B. - - Use dispatchable call `transfer_to_parachain`. - Sending the tx from parachain A. - Set the destination as Parachain B. - Set the currency ID as parachain B token. - Transfer tokens issued by parachain C, from parachain A to parachain B. - - Use dispatchable call `transfer_to_parachain`. - Sending the tx from parachain A. - Set the destination as Parachain B. - Set the currency ID as parachain C token. diff --git a/xtokens/src/lib.rs b/xtokens/src/lib.rs index 801dad81929ae4386f63dffce45bdfc7e9315d0d..7df979629218b3e9f4ea55ebe1c12f5ce29c01d1 100644 --- a/xtokens/src/lib.rs +++ b/xtokens/src/lib.rs @@ -14,8 +14,8 @@ //! //! ### Dispatchable functions //! -//! - `transfer_to_relay_chain`: Transfer relay chain tokens to relay chain. -//! - `transfer_to_parachain`: Transfer tokens to a sibling parachain. +//! - `transfer`: Transfer local assets with given `CurrencyId` and `Amount`. +//! - `transfer_multiasset`: Transfer `MultiAsset` assets. #![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::from_over_into)]