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 transferrable balance to currency trait to fix balance inconsistency #2038

Closed
Closed
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
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 {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to factor this one function out into a LegacyCurrencyTransferrableBalance trait or something?
Than you can require that in pallet_evm like type Currency: Currency<Balance> + LegacyCurrencyTransferrableBalance<Balance>; or so.
It seems like a better solution than editing the existing trait. Do you think it could work?

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