pallet_balances/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # Balances Pallet
19//!
20//! The Balances pallet provides functionality for handling accounts and balances for a single
21//! token.
22//!
23//! It makes heavy use of concepts such as Holds and Freezes from the
24//! [`frame_support::traits::fungible`] traits, therefore you should read and understand those docs
25//! as a prerequisite to understanding this pallet.
26//!
27//! Also see the [`frame_tokens`] reference docs for higher level information regarding the
28//! place of this palet in FRAME.
29//!
30//! ## Overview
31//!
32//! The Balances pallet provides functions for:
33//!
34//! - Getting and setting free balances.
35//! - Retrieving total, reserved and unreserved balances.
36//! - Repatriating a reserved balance to a beneficiary account that exists.
37//! - Transferring a balance between accounts (when not reserved).
38//! - Slashing an account balance.
39//! - Account creation and removal.
40//! - Managing total issuance.
41//! - Setting and managing locks.
42//!
43//! ### Terminology
44//!
45//! - **Reaping an account:** The act of removing an account by resetting its nonce. Happens after
46//!   its total balance has become less than the Existential Deposit.
47//!
48//! ### Implementations
49//!
50//! The Balances pallet provides implementations for the following [`fungible`] traits. If these
51//! traits provide the functionality that you need, then you should avoid tight coupling with the
52//! Balances pallet.
53//!
54//! - [`fungible::Inspect`]
55//! - [`fungible::Mutate`]
56//! - [`fungible::Unbalanced`]
57//! - [`fungible::Balanced`]
58//! - [`fungible::BalancedHold`]
59//! - [`fungible::InspectHold`]
60//! - [`fungible::MutateHold`]
61//! - [`fungible::InspectFreeze`]
62//! - [`fungible::MutateFreeze`]
63//! - [`fungible::Imbalance`]
64//!
65//! It also implements the following [`Currency`] related traits, however they are deprecated and
66//! will eventually be removed.
67//!
68//! - [`Currency`]: Functions for dealing with a fungible assets system.
69//! - [`ReservableCurrency`]
70//! - [`NamedReservableCurrency`](frame_support::traits::NamedReservableCurrency):
71//! Functions for dealing with assets that can be reserved from an account.
72//! - [`LockableCurrency`](frame_support::traits::LockableCurrency): Functions for
73//! dealing with accounts that allow liquidity restrictions.
74//! - [`Imbalance`](frame_support::traits::Imbalance): Functions for handling
75//! imbalances between total issuance in the system and account balances. Must be used when a
76//! function creates new funds (e.g. a reward) or destroys some funds (e.g. a system fee).
77//!
78//! ## Usage
79//!
80//! The following examples show how to use the Balances pallet in your custom pallet.
81//!
82//! ### Examples from the FRAME
83//!
84//! The Contract pallet uses the `Currency` trait to handle gas payment, and its types inherit from
85//! `Currency`:
86//!
87//! ```
88//! use frame_support::traits::Currency;
89//! # pub trait Config: frame_system::Config {
90//! #   type Currency: Currency<Self::AccountId>;
91//! # }
92//!
93//! pub type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
94//! pub type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;
95//!
96//! # fn main() {}
97//! ```
98//!
99//! The Staking pallet uses the `LockableCurrency` trait to lock a stash account's funds:
100//!
101//! ```
102//! use frame_support::traits::{WithdrawReasons, LockableCurrency};
103//! use sp_runtime::traits::Bounded;
104//! pub trait Config: frame_system::Config {
105//!     type Currency: LockableCurrency<Self::AccountId, Moment=frame_system::pallet_prelude::BlockNumberFor<Self>>;
106//! }
107//! # struct StakingLedger<T: Config> {
108//! #   stash: <T as frame_system::Config>::AccountId,
109//! #   total: <<T as Config>::Currency as frame_support::traits::Currency<<T as frame_system::Config>::AccountId>>::Balance,
110//! #   phantom: std::marker::PhantomData<T>,
111//! # }
112//! # const STAKING_ID: [u8; 8] = *b"staking ";
113//!
114//! fn update_ledger<T: Config>(
115//!     controller: &T::AccountId,
116//!     ledger: &StakingLedger<T>
117//! ) {
118//!     T::Currency::set_lock(
119//!         STAKING_ID,
120//!         &ledger.stash,
121//!         ledger.total,
122//!         WithdrawReasons::all()
123//!     );
124//!     // <Ledger<T>>::insert(controller, ledger); // Commented out as we don't have access to Staking's storage here.
125//! }
126//! # fn main() {}
127//! ```
128//!
129//! ## Genesis config
130//!
131//! The Balances pallet depends on the [`GenesisConfig`].
132//!
133//! ## Assumptions
134//!
135//! * Total issued balanced of all accounts should be less than `Config::Balance::max_value()`.
136//! * Existential Deposit is set to a value greater than zero.
137//!
138//! Note, you may find the Balances pallet still functions with an ED of zero when the
139//! `insecure_zero_ed` cargo feature is enabled. However this is not a configuration which is
140//! generally supported, nor will it be.
141//!
142//! [`frame_tokens`]: ../polkadot_sdk_docs/reference_docs/frame_tokens/index.html
143
144#![cfg_attr(not(feature = "std"), no_std)]
145mod benchmarking;
146mod impl_currency;
147mod impl_fungible;
148pub mod migration;
149mod tests;
150mod types;
151pub mod weights;
152
153extern crate alloc;
154
155use alloc::vec::Vec;
156use codec::{Codec, MaxEncodedLen};
157use core::{cmp, fmt::Debug, mem, result};
158use frame_support::{
159	ensure,
160	pallet_prelude::DispatchResult,
161	traits::{
162		tokens::{
163			fungible, BalanceStatus as Status, DepositConsequence,
164			Fortitude::{self, Force, Polite},
165			IdAmount,
166			Preservation::{Expendable, Preserve, Protect},
167			WithdrawConsequence,
168		},
169		Currency, Defensive, Get, OnUnbalanced, ReservableCurrency, StoredMap,
170	},
171	BoundedSlice, WeakBoundedVec,
172};
173use frame_system as system;
174pub use impl_currency::{NegativeImbalance, PositiveImbalance};
175use scale_info::TypeInfo;
176use sp_runtime::{
177	traits::{
178		AtLeast32BitUnsigned, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Saturating,
179		StaticLookup, Zero,
180	},
181	ArithmeticError, DispatchError, FixedPointOperand, Perbill, RuntimeDebug, TokenError,
182};
183pub use types::{
184	AccountData, AdjustmentDirection, BalanceLock, DustCleaner, ExtraFlags, Reasons, ReserveData,
185};
186pub use weights::WeightInfo;
187
188pub use pallet::*;
189
190const LOG_TARGET: &str = "runtime::balances";
191
192type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
193
194#[frame_support::pallet]
195pub mod pallet {
196	use super::*;
197	use frame_support::{
198		pallet_prelude::*,
199		traits::{fungible::Credit, tokens::Precision, VariantCount, VariantCountOf},
200	};
201	use frame_system::pallet_prelude::*;
202
203	pub type CreditOf<T, I> = Credit<<T as frame_system::Config>::AccountId, Pallet<T, I>>;
204
205	/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
206	pub mod config_preludes {
207		use super::*;
208		use frame_support::{derive_impl, traits::ConstU64};
209
210		pub struct TestDefaultConfig;
211
212		#[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
213		impl frame_system::DefaultConfig for TestDefaultConfig {}
214
215		#[frame_support::register_default_impl(TestDefaultConfig)]
216		impl DefaultConfig for TestDefaultConfig {
217			#[inject_runtime_type]
218			type RuntimeEvent = ();
219			#[inject_runtime_type]
220			type RuntimeHoldReason = ();
221			#[inject_runtime_type]
222			type RuntimeFreezeReason = ();
223
224			type Balance = u64;
225			type ExistentialDeposit = ConstU64<1>;
226
227			type ReserveIdentifier = ();
228			type FreezeIdentifier = Self::RuntimeFreezeReason;
229
230			type DustRemoval = ();
231
232			type MaxLocks = ConstU32<100>;
233			type MaxReserves = ConstU32<100>;
234			type MaxFreezes = VariantCountOf<Self::RuntimeFreezeReason>;
235
236			type WeightInfo = ();
237			type DoneSlashHandler = ();
238		}
239	}
240
241	#[pallet::config(with_default)]
242	pub trait Config<I: 'static = ()>: frame_system::Config {
243		/// The overarching event type.
244		#[pallet::no_default_bounds]
245		type RuntimeEvent: From<Event<Self, I>>
246			+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
247
248		/// The overarching hold reason.
249		#[pallet::no_default_bounds]
250		type RuntimeHoldReason: Parameter + Member + MaxEncodedLen + Copy + VariantCount;
251
252		/// The overarching freeze reason.
253		#[pallet::no_default_bounds]
254		type RuntimeFreezeReason: VariantCount;
255
256		/// Weight information for extrinsics in this pallet.
257		type WeightInfo: WeightInfo;
258
259		/// The balance of an account.
260		type Balance: Parameter
261			+ Member
262			+ AtLeast32BitUnsigned
263			+ Codec
264			+ Default
265			+ Copy
266			+ MaybeSerializeDeserialize
267			+ Debug
268			+ MaxEncodedLen
269			+ TypeInfo
270			+ FixedPointOperand;
271
272		/// Handler for the unbalanced reduction when removing a dust account.
273		#[pallet::no_default_bounds]
274		type DustRemoval: OnUnbalanced<CreditOf<Self, I>>;
275
276		/// The minimum amount required to keep an account open. MUST BE GREATER THAN ZERO!
277		///
278		/// If you *really* need it to be zero, you can enable the feature `insecure_zero_ed` for
279		/// this pallet. However, you do so at your own risk: this will open up a major DoS vector.
280		/// In case you have multiple sources of provider references, you may also get unexpected
281		/// behaviour if you set this to zero.
282		///
283		/// Bottom line: Do yourself a favour and make it at least one!
284		#[pallet::constant]
285		#[pallet::no_default_bounds]
286		type ExistentialDeposit: Get<Self::Balance>;
287
288		/// The means of storing the balances of an account.
289		#[pallet::no_default]
290		type AccountStore: StoredMap<Self::AccountId, AccountData<Self::Balance>>;
291
292		/// The ID type for reserves.
293		///
294		/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
295		type ReserveIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
296
297		/// The ID type for freezes.
298		type FreezeIdentifier: Parameter + Member + MaxEncodedLen + Copy;
299
300		/// The maximum number of locks that should exist on an account.
301		/// Not strictly enforced, but used for weight estimation.
302		///
303		/// Use of locks is deprecated in favour of freezes. See `https://github.com/paritytech/substrate/pull/12951/`
304		#[pallet::constant]
305		type MaxLocks: Get<u32>;
306
307		/// The maximum number of named reserves that can exist on an account.
308		///
309		/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
310		#[pallet::constant]
311		type MaxReserves: Get<u32>;
312
313		/// The maximum number of individual freeze locks that can exist on an account at any time.
314		#[pallet::constant]
315		type MaxFreezes: Get<u32>;
316
317		/// Allows callbacks to other pallets so they can update their bookkeeping when a slash
318		/// occurs.
319		type DoneSlashHandler: fungible::hold::DoneSlash<
320			Self::RuntimeHoldReason,
321			Self::AccountId,
322			Self::Balance,
323		>;
324	}
325
326	/// The in-code storage version.
327	const STORAGE_VERSION: frame_support::traits::StorageVersion =
328		frame_support::traits::StorageVersion::new(1);
329
330	#[pallet::pallet]
331	#[pallet::storage_version(STORAGE_VERSION)]
332	pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
333
334	#[pallet::event]
335	#[pallet::generate_deposit(pub(super) fn deposit_event)]
336	pub enum Event<T: Config<I>, I: 'static = ()> {
337		/// An account was created with some free balance.
338		Endowed { account: T::AccountId, free_balance: T::Balance },
339		/// An account was removed whose balance was non-zero but below ExistentialDeposit,
340		/// resulting in an outright loss.
341		DustLost { account: T::AccountId, amount: T::Balance },
342		/// Transfer succeeded.
343		Transfer { from: T::AccountId, to: T::AccountId, amount: T::Balance },
344		/// A balance was set by root.
345		BalanceSet { who: T::AccountId, free: T::Balance },
346		/// Some balance was reserved (moved from free to reserved).
347		Reserved { who: T::AccountId, amount: T::Balance },
348		/// Some balance was unreserved (moved from reserved to free).
349		Unreserved { who: T::AccountId, amount: T::Balance },
350		/// Some balance was moved from the reserve of the first account to the second account.
351		/// Final argument indicates the destination balance type.
352		ReserveRepatriated {
353			from: T::AccountId,
354			to: T::AccountId,
355			amount: T::Balance,
356			destination_status: Status,
357		},
358		/// Some amount was deposited (e.g. for transaction fees).
359		Deposit { who: T::AccountId, amount: T::Balance },
360		/// Some amount was withdrawn from the account (e.g. for transaction fees).
361		Withdraw { who: T::AccountId, amount: T::Balance },
362		/// Some amount was removed from the account (e.g. for misbehavior).
363		Slashed { who: T::AccountId, amount: T::Balance },
364		/// Some amount was minted into an account.
365		Minted { who: T::AccountId, amount: T::Balance },
366		/// Some amount was burned from an account.
367		Burned { who: T::AccountId, amount: T::Balance },
368		/// Some amount was suspended from an account (it can be restored later).
369		Suspended { who: T::AccountId, amount: T::Balance },
370		/// Some amount was restored into an account.
371		Restored { who: T::AccountId, amount: T::Balance },
372		/// An account was upgraded.
373		Upgraded { who: T::AccountId },
374		/// Total issuance was increased by `amount`, creating a credit to be balanced.
375		Issued { amount: T::Balance },
376		/// Total issuance was decreased by `amount`, creating a debt to be balanced.
377		Rescinded { amount: T::Balance },
378		/// Some balance was locked.
379		Locked { who: T::AccountId, amount: T::Balance },
380		/// Some balance was unlocked.
381		Unlocked { who: T::AccountId, amount: T::Balance },
382		/// Some balance was frozen.
383		Frozen { who: T::AccountId, amount: T::Balance },
384		/// Some balance was thawed.
385		Thawed { who: T::AccountId, amount: T::Balance },
386		/// The `TotalIssuance` was forcefully changed.
387		TotalIssuanceForced { old: T::Balance, new: T::Balance },
388	}
389
390	#[pallet::error]
391	pub enum Error<T, I = ()> {
392		/// Vesting balance too high to send value.
393		VestingBalance,
394		/// Account liquidity restrictions prevent withdrawal.
395		LiquidityRestrictions,
396		/// Balance too low to send value.
397		InsufficientBalance,
398		/// Value too low to create account due to existential deposit.
399		ExistentialDeposit,
400		/// Transfer/payment would kill account.
401		Expendability,
402		/// A vesting schedule already exists for this account.
403		ExistingVestingSchedule,
404		/// Beneficiary account must pre-exist.
405		DeadAccount,
406		/// Number of named reserves exceed `MaxReserves`.
407		TooManyReserves,
408		/// Number of holds exceed `VariantCountOf<T::RuntimeHoldReason>`.
409		TooManyHolds,
410		/// Number of freezes exceed `MaxFreezes`.
411		TooManyFreezes,
412		/// The issuance cannot be modified since it is already deactivated.
413		IssuanceDeactivated,
414		/// The delta cannot be zero.
415		DeltaZero,
416	}
417
418	/// The total units issued in the system.
419	#[pallet::storage]
420	#[pallet::whitelist_storage]
421	pub type TotalIssuance<T: Config<I>, I: 'static = ()> = StorageValue<_, T::Balance, ValueQuery>;
422
423	/// The total units of outstanding deactivated balance in the system.
424	#[pallet::storage]
425	#[pallet::whitelist_storage]
426	pub type InactiveIssuance<T: Config<I>, I: 'static = ()> =
427		StorageValue<_, T::Balance, ValueQuery>;
428
429	/// The Balances pallet example of storing the balance of an account.
430	///
431	/// # Example
432	///
433	/// ```nocompile
434	///  impl pallet_balances::Config for Runtime {
435	///    type AccountStore = StorageMapShim<Self::Account<Runtime>, frame_system::Provider<Runtime>, AccountId, Self::AccountData<Balance>>
436	///  }
437	/// ```
438	///
439	/// You can also store the balance of an account in the `System` pallet.
440	///
441	/// # Example
442	///
443	/// ```nocompile
444	///  impl pallet_balances::Config for Runtime {
445	///   type AccountStore = System
446	///  }
447	/// ```
448	///
449	/// But this comes with tradeoffs, storing account balances in the system pallet stores
450	/// `frame_system` data alongside the account data contrary to storing account balances in the
451	/// `Balances` pallet, which uses a `StorageMap` to store balances data only.
452	/// NOTE: This is only used in the case that this pallet is used to store balances.
453	#[pallet::storage]
454	pub type Account<T: Config<I>, I: 'static = ()> =
455		StorageMap<_, Blake2_128Concat, T::AccountId, AccountData<T::Balance>, ValueQuery>;
456
457	/// Any liquidity locks on some account balances.
458	/// NOTE: Should only be accessed when setting, changing and freeing a lock.
459	///
460	/// Use of locks is deprecated in favour of freezes. See `https://github.com/paritytech/substrate/pull/12951/`
461	#[pallet::storage]
462	pub type Locks<T: Config<I>, I: 'static = ()> = StorageMap<
463		_,
464		Blake2_128Concat,
465		T::AccountId,
466		WeakBoundedVec<BalanceLock<T::Balance>, T::MaxLocks>,
467		ValueQuery,
468	>;
469
470	/// Named reserves on some account balances.
471	///
472	/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
473	#[pallet::storage]
474	pub type Reserves<T: Config<I>, I: 'static = ()> = StorageMap<
475		_,
476		Blake2_128Concat,
477		T::AccountId,
478		BoundedVec<ReserveData<T::ReserveIdentifier, T::Balance>, T::MaxReserves>,
479		ValueQuery,
480	>;
481
482	/// Holds on account balances.
483	#[pallet::storage]
484	pub type Holds<T: Config<I>, I: 'static = ()> = StorageMap<
485		_,
486		Blake2_128Concat,
487		T::AccountId,
488		BoundedVec<
489			IdAmount<T::RuntimeHoldReason, T::Balance>,
490			VariantCountOf<T::RuntimeHoldReason>,
491		>,
492		ValueQuery,
493	>;
494
495	/// Freeze locks on account balances.
496	#[pallet::storage]
497	pub type Freezes<T: Config<I>, I: 'static = ()> = StorageMap<
498		_,
499		Blake2_128Concat,
500		T::AccountId,
501		BoundedVec<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
502		ValueQuery,
503	>;
504
505	#[pallet::genesis_config]
506	pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
507		pub balances: Vec<(T::AccountId, T::Balance)>,
508	}
509
510	impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
511		fn default() -> Self {
512			Self { balances: Default::default() }
513		}
514	}
515
516	#[pallet::genesis_build]
517	impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
518		fn build(&self) {
519			let total = self.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n);
520
521			<TotalIssuance<T, I>>::put(total);
522
523			for (_, balance) in &self.balances {
524				assert!(
525					*balance >= <T as Config<I>>::ExistentialDeposit::get(),
526					"the balance of any account should always be at least the existential deposit.",
527				)
528			}
529
530			// ensure no duplicates exist.
531			let endowed_accounts = self
532				.balances
533				.iter()
534				.map(|(x, _)| x)
535				.cloned()
536				.collect::<alloc::collections::btree_set::BTreeSet<_>>();
537
538			assert!(
539				endowed_accounts.len() == self.balances.len(),
540				"duplicate balances in genesis."
541			);
542
543			for &(ref who, free) in self.balances.iter() {
544				frame_system::Pallet::<T>::inc_providers(who);
545				assert!(T::AccountStore::insert(who, AccountData { free, ..Default::default() })
546					.is_ok());
547			}
548		}
549	}
550
551	#[pallet::hooks]
552	impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
553		fn integrity_test() {
554			#[cfg(not(feature = "insecure_zero_ed"))]
555			assert!(
556				!<T as Config<I>>::ExistentialDeposit::get().is_zero(),
557				"The existential deposit must be greater than zero!"
558			);
559
560			assert!(
561				T::MaxFreezes::get() >= <T::RuntimeFreezeReason as VariantCount>::VARIANT_COUNT,
562				"MaxFreezes should be greater than or equal to the number of freeze reasons: {} < {}",
563				T::MaxFreezes::get(), <T::RuntimeFreezeReason as VariantCount>::VARIANT_COUNT,
564			);
565		}
566
567		#[cfg(feature = "try-runtime")]
568		fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
569			Holds::<T, I>::iter_keys().try_for_each(|k| {
570				if Holds::<T, I>::decode_len(k).unwrap_or(0) >
571					T::RuntimeHoldReason::VARIANT_COUNT as usize
572				{
573					Err("Found `Hold` with too many elements")
574				} else {
575					Ok(())
576				}
577			})?;
578
579			Freezes::<T, I>::iter_keys().try_for_each(|k| {
580				if Freezes::<T, I>::decode_len(k).unwrap_or(0) > T::MaxFreezes::get() as usize {
581					Err("Found `Freeze` with too many elements")
582				} else {
583					Ok(())
584				}
585			})?;
586
587			Ok(())
588		}
589	}
590
591	#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
592	impl<T: Config<I>, I: 'static> Pallet<T, I> {
593		/// Transfer some liquid free balance to another account.
594		///
595		/// `transfer_allow_death` will set the `FreeBalance` of the sender and receiver.
596		/// If the sender's account is below the existential deposit as a result
597		/// of the transfer, the account will be reaped.
598		///
599		/// The dispatch origin for this call must be `Signed` by the transactor.
600		#[pallet::call_index(0)]
601		pub fn transfer_allow_death(
602			origin: OriginFor<T>,
603			dest: AccountIdLookupOf<T>,
604			#[pallet::compact] value: T::Balance,
605		) -> DispatchResult {
606			let source = ensure_signed(origin)?;
607			let dest = T::Lookup::lookup(dest)?;
608			<Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Expendable)?;
609			Ok(())
610		}
611
612		/// Exactly as `transfer_allow_death`, except the origin must be root and the source account
613		/// may be specified.
614		#[pallet::call_index(2)]
615		pub fn force_transfer(
616			origin: OriginFor<T>,
617			source: AccountIdLookupOf<T>,
618			dest: AccountIdLookupOf<T>,
619			#[pallet::compact] value: T::Balance,
620		) -> DispatchResult {
621			ensure_root(origin)?;
622			let source = T::Lookup::lookup(source)?;
623			let dest = T::Lookup::lookup(dest)?;
624			<Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Expendable)?;
625			Ok(())
626		}
627
628		/// Same as the [`transfer_allow_death`] call, but with a check that the transfer will not
629		/// kill the origin account.
630		///
631		/// 99% of the time you want [`transfer_allow_death`] instead.
632		///
633		/// [`transfer_allow_death`]: struct.Pallet.html#method.transfer
634		#[pallet::call_index(3)]
635		pub fn transfer_keep_alive(
636			origin: OriginFor<T>,
637			dest: AccountIdLookupOf<T>,
638			#[pallet::compact] value: T::Balance,
639		) -> DispatchResult {
640			let source = ensure_signed(origin)?;
641			let dest = T::Lookup::lookup(dest)?;
642			<Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Preserve)?;
643			Ok(())
644		}
645
646		/// Transfer the entire transferable balance from the caller account.
647		///
648		/// NOTE: This function only attempts to transfer _transferable_ balances. This means that
649		/// any locked, reserved, or existential deposits (when `keep_alive` is `true`), will not be
650		/// transferred by this function. To ensure that this function results in a killed account,
651		/// you might need to prepare the account by removing any reference counters, storage
652		/// deposits, etc...
653		///
654		/// The dispatch origin of this call must be Signed.
655		///
656		/// - `dest`: The recipient of the transfer.
657		/// - `keep_alive`: A boolean to determine if the `transfer_all` operation should send all
658		///   of the funds the account has, causing the sender account to be killed (false), or
659		///   transfer everything except at least the existential deposit, which will guarantee to
660		///   keep the sender account alive (true).
661		#[pallet::call_index(4)]
662		pub fn transfer_all(
663			origin: OriginFor<T>,
664			dest: AccountIdLookupOf<T>,
665			keep_alive: bool,
666		) -> DispatchResult {
667			let transactor = ensure_signed(origin)?;
668			let keep_alive = if keep_alive { Preserve } else { Expendable };
669			let reducible_balance = <Self as fungible::Inspect<_>>::reducible_balance(
670				&transactor,
671				keep_alive,
672				Fortitude::Polite,
673			);
674			let dest = T::Lookup::lookup(dest)?;
675			<Self as fungible::Mutate<_>>::transfer(
676				&transactor,
677				&dest,
678				reducible_balance,
679				keep_alive,
680			)?;
681			Ok(())
682		}
683
684		/// Unreserve some balance from a user by force.
685		///
686		/// Can only be called by ROOT.
687		#[pallet::call_index(5)]
688		pub fn force_unreserve(
689			origin: OriginFor<T>,
690			who: AccountIdLookupOf<T>,
691			amount: T::Balance,
692		) -> DispatchResult {
693			ensure_root(origin)?;
694			let who = T::Lookup::lookup(who)?;
695			let _leftover = <Self as ReservableCurrency<_>>::unreserve(&who, amount);
696			Ok(())
697		}
698
699		/// Upgrade a specified account.
700		///
701		/// - `origin`: Must be `Signed`.
702		/// - `who`: The account to be upgraded.
703		///
704		/// This will waive the transaction fee if at least all but 10% of the accounts needed to
705		/// be upgraded. (We let some not have to be upgraded just in order to allow for the
706		/// possibility of churn).
707		#[pallet::call_index(6)]
708		#[pallet::weight(T::WeightInfo::upgrade_accounts(who.len() as u32))]
709		pub fn upgrade_accounts(
710			origin: OriginFor<T>,
711			who: Vec<T::AccountId>,
712		) -> DispatchResultWithPostInfo {
713			ensure_signed(origin)?;
714			if who.is_empty() {
715				return Ok(Pays::Yes.into())
716			}
717			let mut upgrade_count = 0;
718			for i in &who {
719				let upgraded = Self::ensure_upgraded(i);
720				if upgraded {
721					upgrade_count.saturating_inc();
722				}
723			}
724			let proportion_upgraded = Perbill::from_rational(upgrade_count, who.len() as u32);
725			if proportion_upgraded >= Perbill::from_percent(90) {
726				Ok(Pays::No.into())
727			} else {
728				Ok(Pays::Yes.into())
729			}
730		}
731
732		/// Set the regular balance of a given account.
733		///
734		/// The dispatch origin for this call is `root`.
735		#[pallet::call_index(8)]
736		#[pallet::weight(
737			T::WeightInfo::force_set_balance_creating() // Creates a new account.
738				.max(T::WeightInfo::force_set_balance_killing()) // Kills an existing account.
739		)]
740		pub fn force_set_balance(
741			origin: OriginFor<T>,
742			who: AccountIdLookupOf<T>,
743			#[pallet::compact] new_free: T::Balance,
744		) -> DispatchResult {
745			ensure_root(origin)?;
746			let who = T::Lookup::lookup(who)?;
747			let existential_deposit = Self::ed();
748
749			let wipeout = new_free < existential_deposit;
750			let new_free = if wipeout { Zero::zero() } else { new_free };
751
752			// First we try to modify the account's balance to the forced balance.
753			let old_free = Self::mutate_account_handling_dust(&who, |account| {
754				let old_free = account.free;
755				account.free = new_free;
756				old_free
757			})?;
758
759			// This will adjust the total issuance, which was not done by the `mutate_account`
760			// above.
761			if new_free > old_free {
762				mem::drop(PositiveImbalance::<T, I>::new(new_free - old_free));
763			} else if new_free < old_free {
764				mem::drop(NegativeImbalance::<T, I>::new(old_free - new_free));
765			}
766
767			Self::deposit_event(Event::BalanceSet { who, free: new_free });
768			Ok(())
769		}
770
771		/// Adjust the total issuance in a saturating way.
772		///
773		/// Can only be called by root and always needs a positive `delta`.
774		///
775		/// # Example
776		#[doc = docify::embed!("./src/tests/dispatchable_tests.rs", force_adjust_total_issuance_example)]
777		#[pallet::call_index(9)]
778		#[pallet::weight(T::WeightInfo::force_adjust_total_issuance())]
779		pub fn force_adjust_total_issuance(
780			origin: OriginFor<T>,
781			direction: AdjustmentDirection,
782			#[pallet::compact] delta: T::Balance,
783		) -> DispatchResult {
784			ensure_root(origin)?;
785
786			ensure!(delta > Zero::zero(), Error::<T, I>::DeltaZero);
787
788			let old = TotalIssuance::<T, I>::get();
789			let new = match direction {
790				AdjustmentDirection::Increase => old.saturating_add(delta),
791				AdjustmentDirection::Decrease => old.saturating_sub(delta),
792			};
793
794			ensure!(InactiveIssuance::<T, I>::get() <= new, Error::<T, I>::IssuanceDeactivated);
795			TotalIssuance::<T, I>::set(new);
796
797			Self::deposit_event(Event::<T, I>::TotalIssuanceForced { old, new });
798
799			Ok(())
800		}
801
802		/// Burn the specified liquid free balance from the origin account.
803		///
804		/// If the origin's account ends up below the existential deposit as a result
805		/// of the burn and `keep_alive` is false, the account will be reaped.
806		///
807		/// Unlike sending funds to a _burn_ address, which merely makes the funds inaccessible,
808		/// this `burn` operation will reduce total issuance by the amount _burned_.
809		#[pallet::call_index(10)]
810		#[pallet::weight(if *keep_alive {T::WeightInfo::burn_allow_death() } else {T::WeightInfo::burn_keep_alive()})]
811		pub fn burn(
812			origin: OriginFor<T>,
813			#[pallet::compact] value: T::Balance,
814			keep_alive: bool,
815		) -> DispatchResult {
816			let source = ensure_signed(origin)?;
817			let preservation = if keep_alive { Preserve } else { Expendable };
818			<Self as fungible::Mutate<_>>::burn_from(
819				&source,
820				value,
821				preservation,
822				Precision::Exact,
823				Polite,
824			)?;
825			Ok(())
826		}
827	}
828
829	impl<T: Config<I>, I: 'static> Pallet<T, I> {
830		/// Public function to get the total issuance.
831		pub fn total_issuance() -> T::Balance {
832			TotalIssuance::<T, I>::get()
833		}
834
835		/// Public function to get the inactive issuance.
836		pub fn inactive_issuance() -> T::Balance {
837			InactiveIssuance::<T, I>::get()
838		}
839
840		/// Public function to access the Locks storage.
841		pub fn locks(who: &T::AccountId) -> WeakBoundedVec<BalanceLock<T::Balance>, T::MaxLocks> {
842			Locks::<T, I>::get(who)
843		}
844
845		/// Public function to access the reserves storage.
846		pub fn reserves(
847			who: &T::AccountId,
848		) -> BoundedVec<ReserveData<T::ReserveIdentifier, T::Balance>, T::MaxReserves> {
849			Reserves::<T, I>::get(who)
850		}
851
852		fn ed() -> T::Balance {
853			T::ExistentialDeposit::get()
854		}
855		/// Ensure the account `who` is using the new logic.
856		///
857		/// Returns `true` if the account did get upgraded, `false` if it didn't need upgrading.
858		pub fn ensure_upgraded(who: &T::AccountId) -> bool {
859			let mut a = T::AccountStore::get(who);
860			if a.flags.is_new_logic() {
861				return false
862			}
863			a.flags.set_new_logic();
864			if !a.reserved.is_zero() && a.frozen.is_zero() {
865				if system::Pallet::<T>::providers(who) == 0 {
866					// Gah!! We have no provider refs :(
867					// This shouldn't practically happen, but we need a failsafe anyway: let's give
868					// them enough for an ED.
869					log::warn!(
870						target: LOG_TARGET,
871						"account with a non-zero reserve balance has no provider refs, account_id: '{:?}'.",
872						who
873					);
874					a.free = a.free.max(Self::ed());
875					system::Pallet::<T>::inc_providers(who);
876				}
877				let _ = system::Pallet::<T>::inc_consumers_without_limit(who).defensive();
878			}
879			// Should never fail - we're only setting a bit.
880			let _ = T::AccountStore::try_mutate_exists(who, |account| -> DispatchResult {
881				*account = Some(a);
882				Ok(())
883			});
884			Self::deposit_event(Event::Upgraded { who: who.clone() });
885			return true
886		}
887
888		/// Get the free balance of an account.
889		pub fn free_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
890			Self::account(who.borrow()).free
891		}
892
893		/// Get the balance of an account that can be used for transfers, reservations, or any other
894		/// non-locking, non-transaction-fee activity. Will be at most `free_balance`.
895		pub fn usable_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
896			<Self as fungible::Inspect<_>>::reducible_balance(who.borrow(), Expendable, Polite)
897		}
898
899		/// Get the balance of an account that can be used for paying transaction fees (not tipping,
900		/// or any other kind of fees, though). Will be at most `free_balance`.
901		///
902		/// This requires that the account stays alive.
903		pub fn usable_balance_for_fees(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
904			<Self as fungible::Inspect<_>>::reducible_balance(who.borrow(), Protect, Polite)
905		}
906
907		/// Get the reserved balance of an account.
908		pub fn reserved_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
909			Self::account(who.borrow()).reserved
910		}
911
912		/// Get both the free and reserved balances of an account.
913		pub(crate) fn account(who: &T::AccountId) -> AccountData<T::Balance> {
914			T::AccountStore::get(who)
915		}
916
917		/// Mutate an account to some new value, or delete it entirely with `None`. Will enforce
918		/// `ExistentialDeposit` law, annulling the account as needed.
919		///
920		/// It returns the result from the closure. Any dust is handled through the low-level
921		/// `fungible::Unbalanced` trap-door for legacy dust management.
922		///
923		/// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used
924		/// when it is known that the account already exists.
925		///
926		/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
927		/// the caller will do this.
928		pub(crate) fn mutate_account_handling_dust<R>(
929			who: &T::AccountId,
930			f: impl FnOnce(&mut AccountData<T::Balance>) -> R,
931		) -> Result<R, DispatchError> {
932			let (r, maybe_dust) = Self::mutate_account(who, f)?;
933			if let Some(dust) = maybe_dust {
934				<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
935			}
936			Ok(r)
937		}
938
939		/// Mutate an account to some new value, or delete it entirely with `None`. Will enforce
940		/// `ExistentialDeposit` law, annulling the account as needed.
941		///
942		/// It returns the result from the closure. Any dust is handled through the low-level
943		/// `fungible::Unbalanced` trap-door for legacy dust management.
944		///
945		/// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used
946		/// when it is known that the account already exists.
947		///
948		/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
949		/// the caller will do this.
950		pub(crate) fn try_mutate_account_handling_dust<R, E: From<DispatchError>>(
951			who: &T::AccountId,
952			f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
953		) -> Result<R, E> {
954			let (r, maybe_dust) = Self::try_mutate_account(who, f)?;
955			if let Some(dust) = maybe_dust {
956				<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
957			}
958			Ok(r)
959		}
960
961		/// Mutate an account to some new value, or delete it entirely with `None`. Will enforce
962		/// `ExistentialDeposit` law, annulling the account as needed.
963		///
964		/// It returns both the result from the closure, and an optional amount of dust
965		/// which should be handled once it is known that all nested mutates that could affect
966		/// storage items what the dust handler touches have completed.
967		///
968		/// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used
969		/// when it is known that the account already exists.
970		///
971		/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
972		/// the caller will do this.
973		pub(crate) fn mutate_account<R>(
974			who: &T::AccountId,
975			f: impl FnOnce(&mut AccountData<T::Balance>) -> R,
976		) -> Result<(R, Option<T::Balance>), DispatchError> {
977			Self::try_mutate_account(who, |a, _| -> Result<R, DispatchError> { Ok(f(a)) })
978		}
979
980		/// Returns `true` when `who` has some providers or `insecure_zero_ed` feature is disabled.
981		/// Returns `false` otherwise.
982		#[cfg(not(feature = "insecure_zero_ed"))]
983		fn have_providers_or_no_zero_ed(_: &T::AccountId) -> bool {
984			true
985		}
986
987		/// Returns `true` when `who` has some providers or `insecure_zero_ed` feature is disabled.
988		/// Returns `false` otherwise.
989		#[cfg(feature = "insecure_zero_ed")]
990		fn have_providers_or_no_zero_ed(who: &T::AccountId) -> bool {
991			frame_system::Pallet::<T>::providers(who) > 0
992		}
993
994		/// Mutate an account to some new value, or delete it entirely with `None`. Will enforce
995		/// `ExistentialDeposit` law, annulling the account as needed. This will do nothing if the
996		/// result of `f` is an `Err`.
997		///
998		/// It returns both the result from the closure, and an optional amount of dust
999		/// which should be handled once it is known that all nested mutates that could affect
1000		/// storage items what the dust handler touches have completed.
1001		///
1002		/// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used
1003		/// when it is known that the account already exists.
1004		///
1005		/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
1006		/// the caller will do this.
1007		pub(crate) fn try_mutate_account<R, E: From<DispatchError>>(
1008			who: &T::AccountId,
1009			f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
1010		) -> Result<(R, Option<T::Balance>), E> {
1011			Self::ensure_upgraded(who);
1012			let result = T::AccountStore::try_mutate_exists(who, |maybe_account| {
1013				let is_new = maybe_account.is_none();
1014				let mut account = maybe_account.take().unwrap_or_default();
1015				let did_provide =
1016					account.free >= Self::ed() && Self::have_providers_or_no_zero_ed(who);
1017				let did_consume =
1018					!is_new && (!account.reserved.is_zero() || !account.frozen.is_zero());
1019
1020				let result = f(&mut account, is_new)?;
1021
1022				let does_provide = account.free >= Self::ed();
1023				let does_consume = !account.reserved.is_zero() || !account.frozen.is_zero();
1024
1025				if !did_provide && does_provide {
1026					frame_system::Pallet::<T>::inc_providers(who);
1027				}
1028				if did_consume && !does_consume {
1029					frame_system::Pallet::<T>::dec_consumers(who);
1030				}
1031				if !did_consume && does_consume {
1032					frame_system::Pallet::<T>::inc_consumers(who)?;
1033				}
1034				if does_consume && frame_system::Pallet::<T>::consumers(who) == 0 {
1035					// NOTE: This is a failsafe and should not happen for normal accounts. A normal
1036					// account should have gotten a consumer ref in `!did_consume && does_consume`
1037					// at some point.
1038					log::error!(target: LOG_TARGET, "Defensively bumping a consumer ref.");
1039					frame_system::Pallet::<T>::inc_consumers(who)?;
1040				}
1041				if did_provide && !does_provide {
1042					// This could reap the account so must go last.
1043					frame_system::Pallet::<T>::dec_providers(who).inspect_err(|_| {
1044						// best-effort revert consumer change.
1045						if did_consume && !does_consume {
1046							let _ = frame_system::Pallet::<T>::inc_consumers(who).defensive();
1047						}
1048						if !did_consume && does_consume {
1049							let _ = frame_system::Pallet::<T>::dec_consumers(who);
1050						}
1051					})?;
1052				}
1053
1054				let maybe_endowed = if is_new { Some(account.free) } else { None };
1055
1056				// Handle any steps needed after mutating an account.
1057				//
1058				// This includes DustRemoval unbalancing, in the case than the `new` account's total
1059				// balance is non-zero but below ED.
1060				//
1061				// Updates `maybe_account` to `Some` iff the account has sufficient balance.
1062				// Evaluates `maybe_dust`, which is `Some` containing the dust to be dropped, iff
1063				// some dust should be dropped.
1064				//
1065				// We should never be dropping if reserved is non-zero. Reserved being non-zero
1066				// should imply that we have a consumer ref, so this is economically safe.
1067				let ed = Self::ed();
1068				let maybe_dust = if account.free < ed && account.reserved.is_zero() {
1069					if account.free.is_zero() {
1070						None
1071					} else {
1072						Some(account.free)
1073					}
1074				} else {
1075					assert!(
1076						account.free.is_zero() || account.free >= ed || !account.reserved.is_zero()
1077					);
1078					*maybe_account = Some(account);
1079					None
1080				};
1081				Ok((maybe_endowed, maybe_dust, result))
1082			});
1083			result.map(|(maybe_endowed, maybe_dust, result)| {
1084				if let Some(endowed) = maybe_endowed {
1085					Self::deposit_event(Event::Endowed {
1086						account: who.clone(),
1087						free_balance: endowed,
1088					});
1089				}
1090				if let Some(amount) = maybe_dust {
1091					Pallet::<T, I>::deposit_event(Event::DustLost { account: who.clone(), amount });
1092				}
1093				(result, maybe_dust)
1094			})
1095		}
1096
1097		/// Update the account entry for `who`, given the locks.
1098		pub(crate) fn update_locks(who: &T::AccountId, locks: &[BalanceLock<T::Balance>]) {
1099			let bounded_locks = WeakBoundedVec::<_, T::MaxLocks>::force_from(
1100				locks.to_vec(),
1101				Some("Balances Update Locks"),
1102			);
1103
1104			if locks.len() as u32 > T::MaxLocks::get() {
1105				log::warn!(
1106					target: LOG_TARGET,
1107					"Warning: A user has more currency locks than expected. \
1108					A runtime configuration adjustment may be needed."
1109				);
1110			}
1111			let freezes = Freezes::<T, I>::get(who);
1112			let mut prev_frozen = Zero::zero();
1113			let mut after_frozen = Zero::zero();
1114			// No way this can fail since we do not alter the existential balances.
1115			// TODO: Revisit this assumption.
1116			let res = Self::mutate_account(who, |b| {
1117				prev_frozen = b.frozen;
1118				b.frozen = Zero::zero();
1119				for l in locks.iter() {
1120					b.frozen = b.frozen.max(l.amount);
1121				}
1122				for l in freezes.iter() {
1123					b.frozen = b.frozen.max(l.amount);
1124				}
1125				after_frozen = b.frozen;
1126			});
1127			debug_assert!(res.is_ok());
1128			if let Ok((_, maybe_dust)) = res {
1129				debug_assert!(maybe_dust.is_none(), "Not altering main balance; qed");
1130			}
1131
1132			match locks.is_empty() {
1133				true => Locks::<T, I>::remove(who),
1134				false => Locks::<T, I>::insert(who, bounded_locks),
1135			}
1136
1137			if prev_frozen > after_frozen {
1138				let amount = prev_frozen.saturating_sub(after_frozen);
1139				Self::deposit_event(Event::Unlocked { who: who.clone(), amount });
1140			} else if after_frozen > prev_frozen {
1141				let amount = after_frozen.saturating_sub(prev_frozen);
1142				Self::deposit_event(Event::Locked { who: who.clone(), amount });
1143			}
1144		}
1145
1146		/// Update the account entry for `who`, given the locks.
1147		pub(crate) fn update_freezes(
1148			who: &T::AccountId,
1149			freezes: BoundedSlice<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
1150		) -> DispatchResult {
1151			let mut prev_frozen = Zero::zero();
1152			let mut after_frozen = Zero::zero();
1153			let (_, maybe_dust) = Self::mutate_account(who, |b| {
1154				prev_frozen = b.frozen;
1155				b.frozen = Zero::zero();
1156				for l in Locks::<T, I>::get(who).iter() {
1157					b.frozen = b.frozen.max(l.amount);
1158				}
1159				for l in freezes.iter() {
1160					b.frozen = b.frozen.max(l.amount);
1161				}
1162				after_frozen = b.frozen;
1163			})?;
1164			debug_assert!(maybe_dust.is_none(), "Not altering main balance; qed");
1165			if freezes.is_empty() {
1166				Freezes::<T, I>::remove(who);
1167			} else {
1168				Freezes::<T, I>::insert(who, freezes);
1169			}
1170			if prev_frozen > after_frozen {
1171				let amount = prev_frozen.saturating_sub(after_frozen);
1172				Self::deposit_event(Event::Thawed { who: who.clone(), amount });
1173			} else if after_frozen > prev_frozen {
1174				let amount = after_frozen.saturating_sub(prev_frozen);
1175				Self::deposit_event(Event::Frozen { who: who.clone(), amount });
1176			}
1177			Ok(())
1178		}
1179
1180		/// Move the reserved balance of one account into the balance of another, according to
1181		/// `status`. This will respect freezes/locks only if `fortitude` is `Polite`.
1182		///
1183		/// Is a no-op if the value to be moved is zero.
1184		///
1185		/// NOTE: returns actual amount of transferred value in `Ok` case.
1186		pub(crate) fn do_transfer_reserved(
1187			slashed: &T::AccountId,
1188			beneficiary: &T::AccountId,
1189			value: T::Balance,
1190			precision: Precision,
1191			fortitude: Fortitude,
1192			status: Status,
1193		) -> Result<T::Balance, DispatchError> {
1194			if value.is_zero() {
1195				return Ok(Zero::zero())
1196			}
1197
1198			let max = <Self as fungible::InspectHold<_>>::reducible_total_balance_on_hold(
1199				slashed, fortitude,
1200			);
1201			let actual = match precision {
1202				Precision::BestEffort => value.min(max),
1203				Precision::Exact => value,
1204			};
1205			ensure!(actual <= max, TokenError::FundsUnavailable);
1206			if slashed == beneficiary {
1207				return match status {
1208					Status::Free => Ok(actual.saturating_sub(Self::unreserve(slashed, actual))),
1209					Status::Reserved => Ok(actual),
1210				}
1211			}
1212
1213			let ((_, maybe_dust_1), maybe_dust_2) = Self::try_mutate_account(
1214				beneficiary,
1215				|to_account, is_new| -> Result<((), Option<T::Balance>), DispatchError> {
1216					ensure!(!is_new, Error::<T, I>::DeadAccount);
1217					Self::try_mutate_account(slashed, |from_account, _| -> DispatchResult {
1218						match status {
1219							Status::Free =>
1220								to_account.free = to_account
1221									.free
1222									.checked_add(&actual)
1223									.ok_or(ArithmeticError::Overflow)?,
1224							Status::Reserved =>
1225								to_account.reserved = to_account
1226									.reserved
1227									.checked_add(&actual)
1228									.ok_or(ArithmeticError::Overflow)?,
1229						}
1230						from_account.reserved.saturating_reduce(actual);
1231						Ok(())
1232					})
1233				},
1234			)?;
1235
1236			if let Some(dust) = maybe_dust_1 {
1237				<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
1238			}
1239			if let Some(dust) = maybe_dust_2 {
1240				<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
1241			}
1242
1243			Self::deposit_event(Event::ReserveRepatriated {
1244				from: slashed.clone(),
1245				to: beneficiary.clone(),
1246				amount: actual,
1247				destination_status: status,
1248			});
1249			Ok(actual)
1250		}
1251	}
1252}