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

Commit

Permalink
Add keep_alive parameter to can_hold
Browse files Browse the repository at this point in the history
  • Loading branch information
KiChjang committed Oct 7, 2022
1 parent 13d2261 commit 7a6db84
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
25 changes: 15 additions & 10 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,19 +1169,24 @@ impl<T: Config<I>, I: 'static> fungible::InspectHold<T::AccountId> for Pallet<T,
fn balance_on_hold(who: &T::AccountId) -> T::Balance {
Self::account(who).reserved
}
fn can_hold(who: &T::AccountId, amount: T::Balance) -> bool {
fn can_hold(who: &T::AccountId, amount: T::Balance, keep_alive: bool) -> bool {
let a = Self::account(who);
let min_balance = T::ExistentialDeposit::get().max(a.frozen(Reasons::All));
if a.reserved.checked_add(&amount).is_none() {
return false
}
// We require it to be min_balance + amount to ensure that the full reserved funds may be
// slashed without compromising locked funds or destroying the account.
let required_free = match min_balance.checked_add(&amount) {
Some(x) => x,
None => return false,
};
a.free >= required_free

if keep_alive {
let min_balance = T::ExistentialDeposit::get().max(a.frozen(Reasons::All));
// We require it to be min_balance + amount to ensure that the full reserved funds may
// be slashed without compromising locked funds or destroying the account.
let required_free = match min_balance.checked_add(&amount) {
Some(x) => x,
None => return false,
};
a.free >= required_free
} else {
a.free >= amount
}
}
}
impl<T: Config<I>, I: 'static> fungible::MutateHold<T::AccountId> for Pallet<T, I> {
Expand All @@ -1190,7 +1195,7 @@ impl<T: Config<I>, I: 'static> fungible::MutateHold<T::AccountId> for Pallet<T,
return Ok(())
}
ensure!(
<Self as fungible::InspectHold<T::AccountId>>::can_hold(who, amount),
<Self as fungible::InspectHold<T::AccountId>>::can_hold(who, amount, false),
Error::<T, I>::InsufficientBalance,
);
Self::try_mutate_account(who, |a, _| -> DispatchResult {
Expand Down
6 changes: 3 additions & 3 deletions frame/support/src/traits/tokens/fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub trait InspectHold<AccountId>: Inspect<AccountId> {
fn balance_on_hold(who: &AccountId) -> Self::Balance;

/// Check to see if some `amount` of funds of `who` may be placed on hold.
fn can_hold(who: &AccountId, amount: Self::Balance) -> bool;
fn can_hold(who: &AccountId, amount: Self::Balance, keep_alive: bool) -> bool;
}

/// Trait for mutating a fungible asset which can be reserved.
Expand Down Expand Up @@ -269,8 +269,8 @@ impl<
fn balance_on_hold(who: &AccountId) -> Self::Balance {
<F as fungibles::InspectHold<AccountId>>::balance_on_hold(A::get(), who)
}
fn can_hold(who: &AccountId, amount: Self::Balance) -> bool {
<F as fungibles::InspectHold<AccountId>>::can_hold(A::get(), who, amount)
fn can_hold(who: &AccountId, amount: Self::Balance, keep_alive: bool) -> bool {
<F as fungibles::InspectHold<AccountId>>::can_hold(A::get(), who, amount, keep_alive)
}
}

Expand Down
10 changes: 9 additions & 1 deletion frame/support/src/traits/tokens/fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,15 @@ pub trait InspectHold<AccountId>: Inspect<AccountId> {
fn balance_on_hold(asset: Self::AssetId, who: &AccountId) -> Self::Balance;

/// Check to see if some `amount` of `asset` may be held on the account of `who`.
fn can_hold(asset: Self::AssetId, who: &AccountId, amount: Self::Balance) -> bool;
///
/// If `keep_alive` is set to `true`, then it also checks so that the free balance of `asset`
/// does not go below its existential deposit after holding `amount`.
fn can_hold(
asset: Self::AssetId,
who: &AccountId,
amount: Self::Balance,
keep_alive: bool,
) -> bool;
}

/// Trait for mutating a set of named fungible assets which can be placed on hold.
Expand Down

0 comments on commit 7a6db84

Please sign in to comment.