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

rename MergeAccount to TransferAll #472

Merged
merged 2 commits into from
May 2, 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
13 changes: 5 additions & 8 deletions currencies/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ use frame_support::{
};
use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};
use orml_traits::{
account::MergeAccount,
arithmetic::{Signed, SimpleArithmetic},
currency::TransferAll,
BalanceStatus, BasicCurrency, BasicCurrencyExtended, BasicLockableCurrency, BasicReservableCurrency,
LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency, MultiReservableCurrency,
};
Expand Down Expand Up @@ -94,7 +94,7 @@ pub mod module {
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

type MultiCurrency: MergeAccount<Self::AccountId>
type MultiCurrency: TransferAll<Self::AccountId>
+ MultiCurrencyExtended<Self::AccountId>
+ MultiLockableCurrency<Self::AccountId>
+ MultiReservableCurrency<Self::AccountId>;
Expand Down Expand Up @@ -685,14 +685,11 @@ where
}
}

impl<T: Config> MergeAccount<T::AccountId> for Pallet<T> {
fn merge_account(source: &T::AccountId, dest: &T::AccountId) -> DispatchResult {
impl<T: Config> TransferAll<T::AccountId> for Pallet<T> {
fn transfer_all(source: &T::AccountId, dest: &T::AccountId) -> DispatchResult {
with_transaction_result(|| {
// transfer non-native free to dest
T::MultiCurrency::merge_account(source, dest)?;

// unreserve all reserved currency
T::NativeCurrency::unreserve(source, T::NativeCurrency::reserved_balance(source));
T::MultiCurrency::transfer_all(source, dest)?;

// transfer all free to dest
T::NativeCurrency::transfer(source, dest, T::NativeCurrency::free_balance(source))
Expand Down
15 changes: 4 additions & 11 deletions tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ use frame_support::{
};
use frame_system::{ensure_signed, pallet_prelude::*};
use orml_traits::{
account::MergeAccount,
arithmetic::{self, Signed},
currency::TransferAll,
BalanceStatus, GetByKey, LockIdentifier, MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency,
MultiReservableCurrency, OnDust,
};
Expand Down Expand Up @@ -198,8 +198,6 @@ pub mod module {
AmountIntoBalanceFailed,
/// Failed because liquidity restrictions due to locking
LiquidityRestrictions,
/// Account still has active reserved
StillHasActiveReserved,
}

#[pallet::event]
Expand Down Expand Up @@ -1042,16 +1040,11 @@ where
}
}

impl<T: Config> MergeAccount<T::AccountId> for Pallet<T> {
impl<T: Config> TransferAll<T::AccountId> for Pallet<T> {
#[transactional]
fn merge_account(source: &T::AccountId, dest: &T::AccountId) -> DispatchResult {
fn transfer_all(source: &T::AccountId, dest: &T::AccountId) -> DispatchResult {
Accounts::<T>::iter_prefix(source).try_for_each(|(currency_id, account_data)| -> DispatchResult {
// ensure the account has no active reserved of non-native token
ensure!(account_data.reserved.is_zero(), Error::<T>::StillHasActiveReserved);

// transfer all free to recipient
<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, source, dest, account_data.free)?;
Ok(())
<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, source, dest, account_data.free)
})
}
}
18 changes: 9 additions & 9 deletions tokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ fn no_op_if_amount_is_zero() {
}

#[test]
fn merge_account_should_work() {
fn transfer_all_trait_should_work() {
ExtBuilder::default()
.balances(vec![(ALICE, DOT, 100), (ALICE, BTC, 200)])
.build()
Expand All @@ -440,18 +440,18 @@ fn merge_account_should_work() {
assert_eq!(Tokens::free_balance(BTC, &ALICE), 200);
assert_eq!(Tokens::free_balance(DOT, &BOB), 0);

assert_ok!(Tokens::reserve(DOT, &ALICE, 1));
assert_noop!(
Tokens::merge_account(&ALICE, &BOB),
Error::<Runtime>::StillHasActiveReserved
);
Tokens::unreserve(DOT, &ALICE, 1);

assert_ok!(Tokens::merge_account(&ALICE, &BOB));
assert_ok!(<Tokens as TransferAll<AccountId>>::transfer_all(&ALICE, &BOB));
assert_eq!(Tokens::free_balance(DOT, &ALICE), 0);
assert_eq!(Tokens::free_balance(BTC, &ALICE), 0);
assert_eq!(Tokens::free_balance(DOT, &BOB), 100);
assert_eq!(Tokens::free_balance(BTC, &BOB), 200);

assert_ok!(Tokens::reserve(DOT, &BOB, 1));
assert_ok!(<Tokens as TransferAll<AccountId>>::transfer_all(&BOB, &ALICE));
assert_eq!(Tokens::free_balance(DOT, &ALICE), 99);
assert_eq!(Tokens::free_balance(BTC, &ALICE), 200);
assert_eq!(Tokens::free_balance(DOT, &BOB), 0);
assert_eq!(Tokens::free_balance(BTC, &BOB), 0);
});
}

Expand Down
19 changes: 0 additions & 19 deletions traits/src/account.rs

This file was deleted.

20 changes: 19 additions & 1 deletion traits/src/currency.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::arithmetic;
use codec::{Codec, FullCodec};
pub use frame_support::traits::{BalanceStatus, LockIdentifier};
pub use frame_support::{
traits::{BalanceStatus, LockIdentifier},
transactional,
};
use sp_runtime::{
traits::{AtLeast32BitUnsigned, MaybeSerializeDeserialize},
DispatchError, DispatchResult,
Expand Down Expand Up @@ -341,3 +344,18 @@ pub trait OnDust<AccountId, CurrencyId, Balance> {
impl<AccountId, CurrencyId, Balance> OnDust<AccountId, CurrencyId, Balance> for () {
fn on_dust(_: &AccountId, _: CurrencyId, _: Balance) {}
}

pub trait TransferAll<AccountId> {
fn transfer_all(source: &AccountId, dest: &AccountId) -> DispatchResult;
}

#[impl_trait_for_tuples::impl_for_tuples(5)]
impl<AccountId> TransferAll<AccountId> for Tuple {
#[transactional]
fn transfer_all(source: &AccountId, dest: &AccountId) -> DispatchResult {
for_tuples!( #( {
Tuple::transfer_all(source, dest)?;
} )* );
Ok(())
}
}
1 change: 0 additions & 1 deletion traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub use nft::NFT;
pub use price::{DefaultPriceProvider, PriceProvider};
pub use rewards::RewardHandler;

pub mod account;
pub mod arithmetic;
pub mod auction;
pub mod currency;
Expand Down