From 3789dab3ae0ab667a16190fcf32b40b5a33dd0e8 Mon Sep 17 00:00:00 2001 From: Michael Zhai Date: Mon, 22 Feb 2021 14:08:53 +1300 Subject: [PATCH 1/2] Add `transfer_all` to Currencies --- currencies/src/default_weight.rs | 5 +++++ currencies/src/lib.rs | 19 +++++++++++++++++++ currencies/src/tests.rs | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/currencies/src/default_weight.rs b/currencies/src/default_weight.rs index 079c8ced4..31a7658e2 100644 --- a/currencies/src/default_weight.rs +++ b/currencies/src/default_weight.rs @@ -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) } diff --git a/currencies/src/lib.rs b/currencies/src/lib.rs index adf09dfd2..95eb9ba93 100644 --- a/currencies/src/lib.rs +++ b/currencies/src/lib.rs @@ -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; @@ -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, + dest: ::Source, + currency_id: CurrencyIdOf, + ) -> DispatchResultWithPostInfo { + let from = ensure_signed(origin)?; + let to = T::Lookup::lookup(dest)?; + let balance = >::free_balance(currency_id, &from); + >::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 diff --git a/currencies/src/tests.rs b/currencies/src/tests.rs index 30bee086a..140a2bdae 100644 --- a/currencies/src/tests.rs +++ b/currencies/src/tests.rs @@ -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() From 07240b81beea998b45b72204c6d0c4b08d9f8749 Mon Sep 17 00:00:00 2001 From: Michael Zhai Date: Mon, 22 Feb 2021 17:33:32 +1300 Subject: [PATCH 2/2] Add frozen deduction for transfer all --- currencies/src/lib.rs | 3 ++- tokens/src/lib.rs | 5 ++++- tokens/src/tests.rs | 7 ++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/currencies/src/lib.rs b/currencies/src/lib.rs index 95eb9ba93..4c2d426ac 100644 --- a/currencies/src/lib.rs +++ b/currencies/src/lib.rs @@ -157,7 +157,8 @@ pub mod module { Ok(().into()) } - /// Transfer all remaining balance to the given account under `currency_id`. + /// Transfer all remaining balance to the given account under + /// `currency_id`. /// /// The dispatch origin for this call must be `Signed` by the /// transactor. diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index c993cf95e..2f020b3b8 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -336,7 +336,10 @@ pub mod module { ) -> DispatchResultWithPostInfo { let from = ensure_signed(origin)?; let to = T::Lookup::lookup(dest)?; - let balance = >::free_balance(currency_id, &from); + + let frozen = Self::accounts(&from, currency_id).frozen(); + let balance = + >::free_balance(currency_id, &from).saturating_sub(frozen); >::transfer(currency_id, &from, &to, balance)?; Self::deposit_event(Event::Transferred(currency_id, from, to, balance)); diff --git a/tokens/src/tests.rs b/tokens/src/tests.rs index 1b2f04021..d60058389 100644 --- a/tokens/src/tests.rs +++ b/tokens/src/tests.rs @@ -324,11 +324,12 @@ fn transfer_all_should_work() { .execute_with(|| { System::set_block_number(1); + assert_ok!(Tokens::set_lock(ID_1, DOT, &ALICE, 10)); assert_ok!(Tokens::transfer_all(Some(ALICE).into(), BOB, DOT)); - assert_eq!(Tokens::free_balance(DOT, &ALICE), 0); - assert_eq!(Tokens::free_balance(DOT, &BOB), 200); + assert_eq!(Tokens::free_balance(DOT, &ALICE), 10); + assert_eq!(Tokens::free_balance(DOT, &BOB), 190); - let transferred_event = Event::tokens(crate::Event::Transferred(DOT, ALICE, BOB, 100)); + let transferred_event = Event::tokens(crate::Event::Transferred(DOT, ALICE, BOB, 90)); assert!(System::events().iter().any(|record| record.event == transferred_event)); }); }