Skip to content
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
23 changes: 22 additions & 1 deletion substrate/frame/balances/src/impl_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use frame_support::{
ensure,
pallet_prelude::DispatchResult,
traits::{
tokens::{fungible, BalanceStatus as Status, Fortitude::Polite, Precision::BestEffort},
tokens::{fungible, BalanceStatus as Status, Fortitude::Polite, Precision::BestEffort, Preservation::{self, Preserve, Protect}},
Currency, DefensiveSaturating, ExistenceRequirement,
ExistenceRequirement::AllowDeath,
Get, Imbalance, LockIdentifier, LockableCurrency, NamedReservableCurrency,
Expand Down Expand Up @@ -487,6 +487,27 @@ where
)
.unwrap_or_else(|_| SignedImbalance::Positive(Self::PositiveImbalance::zero()))
}

fn transferrable_balance(
who: &T::AccountId,
preservation: Preservation,
) -> Self::Balance {
let a = Self::account(who);
let mut untouchable = a.frozen;
// If we want to keep our provider ref..
if preservation == Preserve
// ..or we don't want the account to die and our provider ref is needed for it to live..
|| preservation == Protect && !a.free.is_zero() &&
frame_system::Pallet::<T>::providers(who) == 1
// ..or we don't care about the account dying but our provider ref is required..
|| preservation == Expendable && !a.free.is_zero() &&
!frame_system::Pallet::<T>::can_dec_provider(who)
{
// ..then the ED needed..
untouchable = untouchable.max(T::ExistentialDeposit::get());
}
a.free.saturating_sub(untouchable)
}
}

impl<T: Config<I>, I: 'static> ReservableCurrency<T::AccountId> for Pallet<T, I>
Expand Down
16 changes: 15 additions & 1 deletion substrate/frame/support/src/traits/tokens/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use super::{
imbalance::{Imbalance, SignedImbalance},
misc::{Balance, ExistenceRequirement, WithdrawReasons},
misc::{Balance, ExistenceRequirement, WithdrawReasons}, Preservation,
};
use crate::{dispatch::DispatchResult, traits::Get};
use sp_runtime::{traits::MaybeSerializeDeserialize, DispatchError};
Expand Down Expand Up @@ -207,6 +207,17 @@ pub trait Currency<AccountId> {
who: &AccountId,
balance: Self::Balance,
) -> SignedImbalance<Self::Balance, Self::PositiveImbalance>;

/// Get the maximum amount that `who` can withdraw/transfer successfully based on whether the
/// account should be kept alive (`preservation`) or whether we are willing to force the
/// reduction and potentially go below user-level restrictions on the minimum amount of the
/// account.
///
/// Always less than or equal to `balance()`.
fn transferrable_balance(
who: &AccountId,
preservation: Preservation,
) -> Self::Balance;
}

/// A non-const `Get` implementation parameterised by a `Currency` impl which provides the result
Expand Down Expand Up @@ -313,4 +324,7 @@ impl<AccountId> Currency<AccountId> for () {
) -> SignedImbalance<Self::Balance, Self::PositiveImbalance> {
SignedImbalance::Positive(())
}
fn transferrable_balance(_: &AccountId, _: Preservation) -> u32 {
0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use sp_core::Get;
use super::{super::misc::BalanceStatus, Currency};
use crate::{
dispatch::DispatchResult,
traits::{ExistenceRequirement, SignedImbalance, WithdrawReasons},
traits::{ExistenceRequirement, SignedImbalance, WithdrawReasons, tokens::Preservation},
};
use sp_runtime::DispatchError;

Expand Down Expand Up @@ -338,6 +338,12 @@ impl<
) -> SignedImbalance<Self::Balance, Self::PositiveImbalance> {
NamedReservable::make_free_balance_be(who, balance)
}
fn transferrable_balance(
who: &AccountId,
preservation: Preservation,
) -> Self::Balance {
NamedReservable::transferrable_balance(who, preservation)
}
}
impl<
NamedReservable: NamedReservableCurrency<AccountId>,
Expand Down