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