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

Commit

Permalink
Use Free Balance for Check
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Apr 21, 2022
1 parent 8351b2d commit 858fd78
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 54 deletions.
23 changes: 4 additions & 19 deletions frame/balances/src/lib.rs
Expand Up @@ -996,12 +996,13 @@ pub mod pallet {
frozen_balance
}

// TODO: rename to non_lockable_balance
impl_rpc! {
fn usable_balance_rpc(who: impl Borrow<T::AccountId>) -> RuntimeDispatchInfo<T::Balance> {
let account = Self::account(who.borrow());

RuntimeDispatchInfo {
// TODO: balances
usable_balance: Zero::zero(),
// usable_balance: Self::usable_balance(who.borrow()),
usable_balance: account.usable(Reasons::Misc, Self::frozen_balance(who)),
}
}
}
Expand Down Expand Up @@ -1937,22 +1938,6 @@ pub mod pallet {

Self::update_locks(who, &locks);
}

// /// Get the balance of an account that can be used for transfers, reservations, or any other
// /// non-locking, non-transaction-fee activity. Will be at most `free_balance`.
// fn usable_balance(who: &T::AccountId) -> Self::Balance {
// let account = Self::account(who);

// account.usable(Reasons::Misc, Self::frozen_balance(who))
// }

// /// Get the balance of an account that can be used for paying transaction fees (not tipping,
// /// or any other kind of fees, though). Will be at most `free_balance`.
// fn usable_balance_for_fees(who: &T::AccountId) -> Self::Balance {
// let account = Self::account(who);

// account.usable(Reasons::Fee, Self::frozen_balance(who))
// }
}

impl<T: Config<I>, I: 'static> NamedReservableCurrency<T::AccountId> for Pallet<T, I>
Expand Down
9 changes: 4 additions & 5 deletions frame/bridge/relay-authorities/src/lib.rs
Expand Up @@ -303,11 +303,10 @@ decl_module! {
find_authority_position::<T, I>(&<Authorities<T, I>>::get(), &account_id).is_none(),
<Error<T, I>>::AuthorityAE
);
// TODO: balances
// ensure!(
// <RingCurrency<T, I>>::usable_balance(&account_id) > stake,
// <Error<T, I>>::StakeIns
// );
ensure!(
<RingCurrency<T, I>>::free_balance(&account_id) > stake,
<Error<T, I>>::StakeIns
);

<Candidates<T, I>>::try_mutate(|candidates| {
ensure!(
Expand Down
9 changes: 4 additions & 5 deletions frame/bridge/relayer-game/src/lib.rs
Expand Up @@ -249,11 +249,10 @@ impl<T: Config<I>, I: Instance> Module<T, I> {
) -> Result<RingBalance<T, I>, DispatchError> {
let stake = T::RelayerGameAdjustor::estimate_stake(round, affirmations_count);

// TODO: balances
// ensure!(
// T::RingCurrency::usable_balance(relayer) >= stake,
// <Error<T, I>>::StakeIns
// );
ensure!(
T::RingCurrency::free_balance(relayer) >= stake,
<Error<T, I>>::StakeIns
);

Ok(stake)
}
Expand Down
5 changes: 2 additions & 3 deletions frame/claims/src/lib.rs
Expand Up @@ -350,10 +350,9 @@ impl<T: Config> Module<T> {
}

fn pot<C: LockableCurrency<T::AccountId>>() -> C::Balance {
// TODO: balances
sp_runtime::traits::Zero::zero()
// Already lock minimal balance in the account, no need to worry about to be 0.
// C::usable_balance(&Self::account_id())
// There is not other lock on module account, so `free_balance == usable_balance`.
C::free_balance(&Self::account_id())
}

// Constructs the message that RPC's `personal_sign` and `sign` would sign.
Expand Down
15 changes: 8 additions & 7 deletions frame/staking/src/impls.rs
Expand Up @@ -268,11 +268,11 @@ impl<T: Config> Pallet<T> {

let module_account = Self::account_id();

// TODO: balances
// ensure!(
// T::RingCurrency::usable_balance(&module_account) >= validator_total_payout,
// <Error<T>>::PayoutIns
// );
// There is not lock on module account, so `free_balance == usable_balance`.
ensure!(
T::RingCurrency::free_balance(&module_account) >= validator_total_payout,
<Error<T>>::PayoutIns
);

let validator_prefs = Self::eras_validator_prefs(&era, &validator_stash);
// Validator first gets a cut off the top.
Expand Down Expand Up @@ -351,6 +351,7 @@ impl<T: Config> Pallet<T> {
/// DO NOT modify the locks' staking amount outside this function.
pub fn update_ledger(controller: &AccountId<T>, ledger: &StakingLedgerT<T>) {
let StakingLedger {
stash,
active,
active_kton,
ring_staking_lock,
Expand All @@ -360,13 +361,13 @@ impl<T: Config> Pallet<T> {

T::RingCurrency::set_lock(
STAKING_ID,
&ledger.stash,
&stash,
active.saturating_add(ring_staking_lock.locked_amount()),
WithdrawReasons::all(),
);
T::KtonCurrency::set_lock(
STAKING_ID,
&ledger.stash,
&stash,
active_kton.saturating_add(kton_staking_lock.locked_amount()),
WithdrawReasons::all(),
);
Expand Down
11 changes: 8 additions & 3 deletions frame/staking/src/lib.rs
Expand Up @@ -1574,9 +1574,14 @@ pub mod pallet {

// check total free balance and locked one
// strict on punishing in kton
// TODO: balances
if true {
// if T::KtonCurrency::usable_balance(stash) >= kton_slash {
if T::KtonCurrency::ensure_can_withdraw(
stash,
kton_slash,
Reasons::all(),
T::KtonCurrency::free_balance(stash).saturating_sub(kton_slash),
)
.is_ok()
{
*active_deposit_ring = active_deposit_ring.saturating_sub(item.value);

let imbalance = T::KtonCurrency::slash(stash, kton_slash).0;
Expand Down
8 changes: 5 additions & 3 deletions frame/support/src/structs.rs
Expand Up @@ -107,9 +107,11 @@ where
{
#[inline]
pub fn locked_amount(&self) -> Balance {
self.unbondings.iter().fold(Zero::zero(), |acc, unbonding| {
acc.saturating_add(unbonding.amount)
})
self.unbondings
.iter()
.fold(self.staking_amount, |acc, unbonding| {
acc.saturating_add(unbonding.amount)
})
}

#[inline]
Expand Down
15 changes: 6 additions & 9 deletions frame/wormhole/backing/ethereum/src/lib.rs
Expand Up @@ -430,10 +430,8 @@ pub mod pallet {

let fee_account = Self::fee_account_id();
let sync_reward = T::SyncReward::get().min(
// TODO: balances
Zero::zero()
// T::RingCurrency::usable_balance(&fee_account)
// .saturating_sub(T::RingCurrency::minimum_balance()),
T::RingCurrency::free_balance(&fee_account)
.saturating_sub(T::RingCurrency::minimum_balance()),
);

if !sync_reward.is_zero() {
Expand Down Expand Up @@ -554,11 +552,10 @@ pub mod pallet {
/// Return the amount of money in the pot.
// The existential deposit is not part of the pot so backing account never gets deleted.
pub fn pot<C: LockableCurrency<T::AccountId>>() -> C::Balance {
// TODO: balances
Zero::zero()
// C::usable_balance(&Self::account_id())
// // Must never be less than 0 but better be safe.
// .saturating_sub(C::minimum_balance())
// There is not lock on module account, so `free_balance == usable_balance`.
C::usable_balance(&Self::account_id())
// Must never be less than 0 but better be safe.
.saturating_sub(C::minimum_balance())
}

fn redeem_token(
Expand Down

0 comments on commit 858fd78

Please sign in to comment.