Skip to content

Update xtokens docs. #438

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

Merged
merged 3 commits into from
Apr 7, 2021
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
38 changes: 29 additions & 9 deletions tokens/src/imbalances.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions xtokens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 2 additions & 2 deletions xtokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down