Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Implement transfer_all in Balances Pallet #9018

Merged
11 commits merged into from
Jun 11, 2021
14 changes: 8 additions & 6 deletions frame/balances/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ benchmarks_instance_pallet! {
let existential_deposit = T::ExistentialDeposit::get();
let caller = whitelisted_caller();

// Give some multiple of the existential deposit + creation fee + transfer fee
// Give some multiple of the existential deposit
let balance = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&caller, balance);

Expand Down Expand Up @@ -130,7 +130,7 @@ benchmarks_instance_pallet! {
let source: T::AccountId = account("source", 0, SEED);
let source_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(source.clone());

// Give some multiple of the existential deposit + creation fee + transfer fee
// Give some multiple of the existential deposit
let balance = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&source, balance);

Expand All @@ -154,7 +154,7 @@ benchmarks_instance_pallet! {
let existential_deposit = T::ExistentialDeposit::get();
let caller = whitelisted_caller();

// Give some multiple of the existential deposit + creation fee + transfer fee
// Give some multiple of the existential deposit
let balance = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&caller, balance);

Expand Down Expand Up @@ -185,12 +185,14 @@ benchmarks_instance_pallet! {
let recipient: T::AccountId = account("recipient", 0, SEED);
let recipient_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(recipient.clone());

// Give the sender account max funds, thus a transfer will not kill account.
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value());
// Give some multiple of the existential deposit
let existential_deposit = T::ExistentialDeposit::get();
let balance = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&caller, balance);
}: _(RawOrigin::Signed(caller.clone()), recipient_lookup, false)
verify {
assert!(Balances::<T, I>::free_balance(&caller).is_zero());
assert_eq!(Balances::<T, I>::free_balance(&recipient), T::Balance::max_value());
assert_eq!(Balances::<T, I>::free_balance(&recipient), balance);
}
}

Expand Down
19 changes: 16 additions & 3 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,22 @@ pub mod pallet {

/// Transfer the entire transferable balance from the caller account.
///
/// # <weight>
/// NOTE: This function is only attempt to transfer _transferable_ balances. This means that
/// any locked, reserved, or existential deposits (when `keep_alive` is `true`), will not be
/// transferred by this function. To ensure that this function results in a killed account,
/// you might need to prepare the account by removing any reference counters, storage
/// deposits, etc...
apopiak marked this conversation as resolved.
Show resolved Hide resolved
///
/// The dispatch origin of this call must be Signed.
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
///
/// - `dest`: The recipient of the transfer.
/// - `keep_alive`: A boolean to determine if the `transfer_all` operation should send all
/// of the funds the account has, causing the sender account to be killed (false), or
/// transfer everything except at least the existential deposit, which will guarantee to
/// keep the sender account alive (true).
/// # <weight>
/// - O(1). Just like transfer, but reading the user's transferable balance first.
/// #</weight>
/// #</weight>
#[pallet::weight(T::WeightInfo::transfer_all())]
pub fn transfer_all(
origin: OriginFor<T>,
Expand Down Expand Up @@ -1716,7 +1729,7 @@ impl<T: Config<I>, I: 'static> NamedReservableCurrency<T::AccountId> for Pallet<
/// Is a no-op if value to be reserved is zero.
fn reserve_named(id: &Self::ReserveIdentifier, who: &T::AccountId, value: Self::Balance) -> DispatchResult {
if value.is_zero() { return Ok(()) }

Reserves::<T, I>::try_mutate(who, |reserves| -> DispatchResult {
match reserves.binary_search_by_key(id, |data| data.id) {
Ok(index) => {
Expand Down