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

Add events for balance reserve and unreserve functions #6330

Merged
merged 10 commits into from
Jun 12, 2020
10 changes: 10 additions & 0 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ decl_event!(
BalanceSet(AccountId, Balance, Balance),
/// Some amount was deposited (e.g. for transaction fees).
Deposit(AccountId, Balance),
/// Some balance was reserved (moved from free to reserved).
Reserved(AccountId, Balance),
/// Some balance was unreserved (moved from reserved to free).
Unreserved(AccountId, Balance),
/// Some balance was moved from the reserve of the first account to the second account.
/// Final argument indicates the destination balance type.
ReserveRepatriated(AccountId, AccountId, Balance, Status),
}
);

Expand Down Expand Up @@ -1150,6 +1157,7 @@ impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I>
Self::try_mutate_account(who, |account, _| -> DispatchResult {
account.free = account.free.checked_sub(&value).ok_or(Error::<T, I>::InsufficientBalance)?;
account.reserved = account.reserved.checked_add(&value).ok_or(Error::<T, I>::Overflow)?;
Self::deposit_event(RawEvent::Reserved(who.clone(), value.clone()));
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved
Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve.into(), account.free)
})
}
Expand All @@ -1166,6 +1174,7 @@ impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I>
// defensive only: this can never fail since total issuance which is at least free+reserved
// fits into the same data type.
account.free = account.free.saturating_add(actual);
Self::deposit_event(RawEvent::Unreserved(who.clone(), actual.clone()));
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved
value - actual
})
}
Expand Down Expand Up @@ -1217,6 +1226,7 @@ impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I>
Status::Reserved => to_account.reserved = to_account.reserved.checked_add(&actual).ok_or(Error::<T, I>::Overflow)?,
}
from_account.reserved -= actual;
Self::deposit_event(RawEvent::ReserveRepatriated(slashed.clone(), beneficiary.clone(), actual.clone(), status));
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved
Ok(value - actual)
})
})
Expand Down
4 changes: 2 additions & 2 deletions frame/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ macro_rules! decl_tests {
<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath),
Error::<$test, _>::LiquidityRestrictions
);
assert_noop!(
assert_err!(
joepetrowski marked this conversation as resolved.
Show resolved Hide resolved
<Balances as ReservableCurrency<_>>::reserve(&1, 1),
Error::<$test, _>::LiquidityRestrictions
Error::<$test, _>::LiquidityRestrictions,
);
assert!(<ChargeTransactionPayment<$test> as SignedExtension>::pre_dispatch(
ChargeTransactionPayment::from(1),
Expand Down
15 changes: 7 additions & 8 deletions frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use sp_runtime::{
};
use sp_staking::offence::OffenceDetails;
use frame_support::{
assert_ok, assert_noop, StorageMap,
assert_ok, assert_noop, assert_err, StorageMap,
traits::{Currency, ReservableCurrency, OnInitialize, OnFinalize},
};
use pallet_balances::Error as BalancesError;
Expand Down Expand Up @@ -337,8 +337,8 @@ fn staking_should_work() {
claimed_rewards: vec![0],
})
);
// e.g. it cannot spend more than 500 that it has free from the total 2000
assert_noop!(
// e.g. it cannot reserve more than 500 that it has free from the total 2000
assert_err!(
Balances::reserve(&3, 501),
BalancesError::<Test, _>::LiquidityRestrictions
);
Expand Down Expand Up @@ -783,11 +783,10 @@ fn cannot_reserve_staked_balance() {
assert_eq!(Balances::free_balance(11), 1000);
// Confirm account 11 (via controller 10) is totally staked
assert_eq!(Staking::eras_stakers(Staking::active_era().unwrap().index, 11).own, 1000);
// Confirm account 11 cannot transfer as a result
assert_noop!(
Balances::reserve(&11, 1),
BalancesError::<Test, _>::LiquidityRestrictions
);
// Confirm account 11 cannot reserve as a result
assert_eq!(Balances::reserved_balance(11), 0);
let _ = Balances::reserve(&11, 1);
assert_eq!(Balances::reserved_balance(11), 0);

// Give account 11 extra free balance
let _ = Balances::make_free_balance_be(&11, 10000);
Expand Down
1 change: 1 addition & 0 deletions frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ pub trait Currency<AccountId> {
}

/// Status of funds.
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, RuntimeDebug)]
pub enum BalanceStatus {
/// Funds are free, as corresponding to `free` item in Balances.
Free,
Expand Down