Skip to content

Commit

Permalink
feature: add transferrable balance to currency trait
Browse files Browse the repository at this point in the history
  • Loading branch information
dnjscksdn98 committed Oct 26, 2023
1 parent bdf1868 commit 2bd73be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
25 changes: 24 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,12 @@ 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 +492,24 @@ 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
11 changes: 10 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, Preservation, WithdrawReasons},
};
use crate::{dispatch::DispatchResult, traits::Get};
use sp_runtime::{traits::MaybeSerializeDeserialize, DispatchError};
Expand Down Expand Up @@ -207,6 +207,12 @@ 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.
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 +319,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::{tokens::Preservation, ExistenceRequirement, SignedImbalance, WithdrawReasons},
};
use sp_runtime::DispatchError;

Expand Down Expand Up @@ -338,6 +338,9 @@ 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

0 comments on commit 2bd73be

Please sign in to comment.