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

Add transfer_all to Currencies #379

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions currencies/src/default_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ impl crate::WeightInfo for () {
.saturating_add(DbWeight::get().reads(5 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn transfer_all_non_native_currency() -> Weight {
(172_011_000 as Weight)
.saturating_add(DbWeight::get().reads(5 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn transfer_native_currency() -> Weight {
(43_023_000 as Weight)
}
Expand Down
19 changes: 19 additions & 0 deletions currencies/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub mod module {

pub trait WeightInfo {
fn transfer_non_native_currency() -> Weight;
fn transfer_all_non_native_currency() -> Weight;
fn transfer_native_currency() -> Weight;
fn update_balance_non_native_currency() -> Weight;
fn update_balance_native_currency_creating() -> Weight;
Expand Down Expand Up @@ -156,6 +157,24 @@ pub mod module {
Ok(().into())
}

/// Transfer all remaining balance to the given account under `currency_id`.
///
/// The dispatch origin for this call must be `Signed` by the
/// transactor.
#[pallet::weight(T::WeightInfo::transfer_all_non_native_currency())]
pub fn transfer_all(
origin: OriginFor<T>,
dest: <T::Lookup as StaticLookup>::Source,
currency_id: CurrencyIdOf<T>,
) -> DispatchResultWithPostInfo {
let from = ensure_signed(origin)?;
let to = T::Lookup::lookup(dest)?;
let balance = <Self as MultiCurrency<T::AccountId>>::free_balance(currency_id, &from);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be free - frozen

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean apply this for transfer_all in token module as we only define it in orml-tokens

<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, &from, &to, balance)?;

Ok(().into())
}

/// Transfer some native currency to another account.
///
/// The dispatch origin for this call must be `Signed` by the
Expand Down
12 changes: 12 additions & 0 deletions currencies/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ fn multi_currency_should_work() {
});
}

#[test]
fn transfer_all_should_work() {
ExtBuilder::default()
.one_hundred_for_alice_n_bob()
.build()
.execute_with(|| {
assert_ok!(Currencies::transfer_all(Some(ALICE).into(), BOB, X_TOKEN_ID));
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 0);
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &BOB), 200);
});
}

#[test]
fn multi_currency_extended_should_work() {
ExtBuilder::default()
Expand Down