Skip to content
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
10 changes: 10 additions & 0 deletions substrate/frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,16 @@ pub mod pallet {
pub(crate) fn try_mutate_account<R, E: From<DispatchError>>(
who: &T::AccountId,
f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
) -> Result<(R, Option<T::Balance>), E> {
frame_support::storage::transactional::with_storage_layer(|| {
Self::try_mutate_account_inner(who, f)
})
}

/// Implementation of [`Self::try_mutate_account`].
fn try_mutate_account_inner<R, E: From<DispatchError>>(
who: &T::AccountId,
f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
) -> Result<(R, Option<T::Balance>), E> {
Self::ensure_upgraded(who);
let result = T::AccountStore::try_mutate_exists(who, |maybe_account| {
Expand Down
20 changes: 20 additions & 0 deletions substrate/frame/balances/src/tests/dispatchable_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,23 @@ fn upgrade_accounts_should_work() {
assert_eq!(System::consumers(&7), 0);
});
}

#[test]
fn withdraw_error_does_not_modify_issuance() {
use frame_support::traits::{Currency, ExistenceRequirement::AllowDeath, WithdrawReasons};

ExtBuilder::default().existential_deposit(10).build_and_execute_with(|| {
let _ = Balances::deposit_creating(&1, 100);
System::inc_consumers(&1).unwrap();

assert_noop!(
<Balances as frame_support::traits::Currency<u64>>::withdraw(
&1,
95,
WithdrawReasons::TRANSFER,
AllowDeath
),
DispatchError::ConsumerRemaining,
);
});
}