referrerpolicy=no-referrer-when-downgrade

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