Skip to content

Commit d1749f0

Browse files
authored
Update xtokens docs. (#438)
* Update xtokens docs. * Fix typo. * Update imbalances impl.
1 parent a3724dc commit d1749f0

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

tokens/src/imbalances.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// wrapping these imbalances in a private module is necessary to ensure absolute
22
// privacy of the inner member.
33
use crate::{Config, TotalIssuance};
4-
use frame_support::traits::{Get, Imbalance, TryDrop};
4+
use frame_support::traits::{Get, Imbalance, SameOrOther, TryDrop};
55
use sp_runtime::traits::{Saturating, Zero};
66
use sp_std::{marker, mem, result};
77

@@ -21,6 +21,12 @@ impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> PositiveImbalance<T, GetCurre
2121
}
2222
}
2323

24+
impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Default for PositiveImbalance<T, GetCurrencyId> {
25+
fn default() -> Self {
26+
Self::zero()
27+
}
28+
}
29+
2430
/// Opaque, move-only struct with private fields that serves as a token
2531
/// denoting that funds have been destroyed without any equal and opposite
2632
/// accounting.
@@ -37,6 +43,12 @@ impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> NegativeImbalance<T, GetCurre
3743
}
3844
}
3945

46+
impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Default for NegativeImbalance<T, GetCurrencyId> {
47+
fn default() -> Self {
48+
Self::zero()
49+
}
50+
}
51+
4052
impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> TryDrop for PositiveImbalance<T, GetCurrencyId> {
4153
fn try_drop(self) -> result::Result<(), Self> {
4254
self.drop_zero()
@@ -73,14 +85,18 @@ impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Imbalance<T::Balance> for Pos
7385
self.0 = self.0.saturating_add(other.0);
7486
mem::forget(other);
7587
}
76-
fn offset(self, other: Self::Opposite) -> result::Result<Self, Self::Opposite> {
88+
// allow to make the impl same with `pallet-balances`
89+
#[allow(clippy::comparison_chain)]
90+
fn offset(self, other: Self::Opposite) -> SameOrOther<Self, Self::Opposite> {
7791
let (a, b) = (self.0, other.0);
7892
mem::forget((self, other));
7993

80-
if a >= b {
81-
Ok(Self::new(a - b))
94+
if a > b {
95+
SameOrOther::Same(Self::new(a - b))
96+
} else if b > a {
97+
SameOrOther::Other(NegativeImbalance::new(b - a))
8298
} else {
83-
Err(NegativeImbalance::new(b - a))
99+
SameOrOther::None
84100
}
85101
}
86102
fn peek(&self) -> T::Balance {
@@ -124,14 +140,18 @@ impl<T: Config, GetCurrencyId: Get<T::CurrencyId>> Imbalance<T::Balance> for Neg
124140
self.0 = self.0.saturating_add(other.0);
125141
mem::forget(other);
126142
}
127-
fn offset(self, other: Self::Opposite) -> result::Result<Self, Self::Opposite> {
143+
// allow to make the impl same with `pallet-balances`
144+
#[allow(clippy::comparison_chain)]
145+
fn offset(self, other: Self::Opposite) -> SameOrOther<Self, Self::Opposite> {
128146
let (a, b) = (self.0, other.0);
129147
mem::forget((self, other));
130148

131-
if a >= b {
132-
Ok(Self::new(a - b))
149+
if a > b {
150+
SameOrOther::Same(Self::new(a - b))
151+
} else if b > a {
152+
SameOrOther::Other(PositiveImbalance::new(b - a))
133153
} else {
134-
Err(PositiveImbalance::new(b - a))
154+
SameOrOther::None
135155
}
136156
}
137157
fn peek(&self) -> T::Balance {

xtokens/README.md

-8
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,20 @@ The xtokens module provides functions for
1212

1313
## Notes
1414

15-
#### Unit tests
16-
17-
Unit tests could be added once Polkadot has XCM simulator. https://github.com/paritytech/polkadot/issues/2544
18-
1915
#### Integration tests
2016

2117
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:
2218

2319
- Transfer relay chain tokens to relay chain.
24-
- Use dispatchable call `transfer_to_relay_chain`.
2520
- Transfer tokens issued by parachain A, from parachain A to parachain B.
26-
- Use dispatchable call `transfer_to_parachain`.
2721
- Sending the tx from parachain A.
2822
- Set the destination as Parachain B.
2923
- Set the currency ID as parachain A token.
3024
- Transfer tokens issued by parachain B, from parachain A to parachain B.
31-
- Use dispatchable call `transfer_to_parachain`.
3225
- Sending the tx from parachain A.
3326
- Set the destination as Parachain B.
3427
- Set the currency ID as parachain B token.
3528
- Transfer tokens issued by parachain C, from parachain A to parachain B.
36-
- Use dispatchable call `transfer_to_parachain`.
3729
- Sending the tx from parachain A.
3830
- Set the destination as Parachain B.
3931
- Set the currency ID as parachain C token.

xtokens/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
//!
1515
//! ### Dispatchable functions
1616
//!
17-
//! - `transfer_to_relay_chain`: Transfer relay chain tokens to relay chain.
18-
//! - `transfer_to_parachain`: Transfer tokens to a sibling parachain.
17+
//! - `transfer`: Transfer local assets with given `CurrencyId` and `Amount`.
18+
//! - `transfer_multiasset`: Transfer `MultiAsset` assets.
1919
2020
#![cfg_attr(not(feature = "std"), no_std)]
2121
#![allow(clippy::from_over_into)]

0 commit comments

Comments
 (0)