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)]
145
mod benchmarking;
146
mod impl_currency;
147
mod impl_fungible;
148
pub mod migration;
149
mod tests;
150
mod types;
151
pub mod weights;
152

            
153
extern crate alloc;
154

            
155
use alloc::vec::Vec;
156
use codec::{Codec, MaxEncodedLen};
157
use core::{cmp, fmt::Debug, mem, result};
158
use frame_support::{
159
	ensure,
160
	pallet_prelude::DispatchResult,
161
	traits::{
162
		tokens::{
163
			fungible, BalanceStatus as Status, DepositConsequence,
164
			Fortitude::{self, Force, Polite},
165
			IdAmount,
166
			Preservation::{Expendable, Preserve, Protect},
167
			WithdrawConsequence,
168
		},
169
		Currency, Defensive, Get, OnUnbalanced, ReservableCurrency, StoredMap,
170
	},
171
	BoundedSlice, WeakBoundedVec,
172
};
173
use frame_system as system;
174
pub use impl_currency::{NegativeImbalance, PositiveImbalance};
175
use scale_info::TypeInfo;
176
use sp_runtime::{
177
	traits::{
178
		AtLeast32BitUnsigned, Bounded, CheckedAdd, CheckedSub, MaybeSerializeDeserialize,
179
		Saturating, StaticLookup, Zero,
180
	},
181
	ArithmeticError, DispatchError, FixedPointOperand, Perbill, RuntimeDebug, TokenError,
182
};
183
pub use types::{
184
	AccountData, AdjustmentDirection, BalanceLock, DustCleaner, ExtraFlags, Reasons, ReserveData,
185
};
186
pub use weights::WeightInfo;
187

            
188
pub use pallet::*;
189

            
190
const LOG_TARGET: &str = "runtime::balances";
191

            
192
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
193

            
194
5498
#[frame_support::pallet]
195
pub mod pallet {
196
	use super::*;
197
	use frame_support::{
198
		pallet_prelude::*,
199
		traits::{fungible::Credit, tokens::Precision, VariantCount, VariantCountOf},
200
	};
201
	use frame_system::pallet_prelude::*;
202

            
203
	pub type CreditOf<T, I> = Credit<<T as frame_system::Config>::AccountId, Pallet<T, I>>;
204

            
205
	/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
206
	pub mod config_preludes {
207
		use super::*;
208
		use frame_support::{derive_impl, traits::ConstU64};
209

            
210
		pub struct TestDefaultConfig;
211

            
212
		#[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
213
		impl frame_system::DefaultConfig for TestDefaultConfig {}
214

            
215
		#[frame_support::register_default_impl(TestDefaultConfig)]
216
		impl DefaultConfig for TestDefaultConfig {
217
			#[inject_runtime_type]
218
			type RuntimeEvent = ();
219
			#[inject_runtime_type]
220
			type RuntimeHoldReason = ();
221
			#[inject_runtime_type]
222
			type RuntimeFreezeReason = ();
223

            
224
			type Balance = u64;
225
			type ExistentialDeposit = ConstU64<1>;
226

            
227
			type ReserveIdentifier = ();
228
			type FreezeIdentifier = Self::RuntimeFreezeReason;
229

            
230
			type DustRemoval = ();
231

            
232
			type MaxLocks = ConstU32<100>;
233
			type MaxReserves = ConstU32<100>;
234
			type MaxFreezes = VariantCountOf<Self::RuntimeFreezeReason>;
235

            
236
			type WeightInfo = ();
237
		}
238
	}
239

            
240
	#[pallet::config(with_default)]
241
	pub trait Config<I: 'static = ()>: frame_system::Config {
242
		/// The overarching event type.
243
		#[pallet::no_default_bounds]
244
		type RuntimeEvent: From<Event<Self, I>>
245
			+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
246

            
247
		/// The overarching hold reason.
248
		#[pallet::no_default_bounds]
249
		type RuntimeHoldReason: Parameter + Member + MaxEncodedLen + Copy + VariantCount;
250

            
251
		/// The overarching freeze reason.
252
		#[pallet::no_default_bounds]
253
		type RuntimeFreezeReason: VariantCount;
254

            
255
		/// Weight information for extrinsics in this pallet.
256
		type WeightInfo: WeightInfo;
257

            
258
		/// The balance of an account.
259
		type Balance: Parameter
260
			+ Member
261
			+ AtLeast32BitUnsigned
262
			+ Codec
263
			+ Default
264
			+ Copy
265
			+ MaybeSerializeDeserialize
266
			+ Debug
267
			+ MaxEncodedLen
268
			+ TypeInfo
269
			+ FixedPointOperand;
270

            
271
		/// Handler for the unbalanced reduction when removing a dust account.
272
		#[pallet::no_default_bounds]
273
		type DustRemoval: OnUnbalanced<CreditOf<Self, I>>;
274

            
275
		/// The minimum amount required to keep an account open. MUST BE GREATER THAN ZERO!
276
		///
277
		/// If you *really* need it to be zero, you can enable the feature `insecure_zero_ed` for
278
		/// this pallet. However, you do so at your own risk: this will open up a major DoS vector.
279
		/// In case you have multiple sources of provider references, you may also get unexpected
280
		/// behaviour if you set this to zero.
281
		///
282
		/// Bottom line: Do yourself a favour and make it at least one!
283
		#[pallet::constant]
284
		#[pallet::no_default_bounds]
285
		type ExistentialDeposit: Get<Self::Balance>;
286

            
287
		/// The means of storing the balances of an account.
288
		#[pallet::no_default]
289
		type AccountStore: StoredMap<Self::AccountId, AccountData<Self::Balance>>;
290

            
291
		/// The ID type for reserves.
292
		///
293
		/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
294
		type ReserveIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
295

            
296
		/// The ID type for freezes.
297
		type FreezeIdentifier: Parameter + Member + MaxEncodedLen + Copy;
298

            
299
		/// The maximum number of locks that should exist on an account.
300
		/// Not strictly enforced, but used for weight estimation.
301
		///
302
		/// Use of locks is deprecated in favour of freezes. See `https://github.com/paritytech/substrate/pull/12951/`
303
		#[pallet::constant]
304
		type MaxLocks: Get<u32>;
305

            
306
		/// The maximum number of named reserves that can exist on an account.
307
		///
308
		/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
309
		#[pallet::constant]
310
		type MaxReserves: Get<u32>;
311

            
312
		/// The maximum number of individual freeze locks that can exist on an account at any time.
313
		#[pallet::constant]
314
		type MaxFreezes: Get<u32>;
315
	}
316

            
317
	/// The in-code storage version.
318
	const STORAGE_VERSION: frame_support::traits::StorageVersion =
319
		frame_support::traits::StorageVersion::new(1);
320

            
321
1698
	#[pallet::pallet]
322
	#[pallet::storage_version(STORAGE_VERSION)]
323
	pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
324

            
325
	#[pallet::event]
326
33588
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
327
	pub enum Event<T: Config<I>, I: 'static = ()> {
328
		/// An account was created with some free balance.
329
		Endowed { account: T::AccountId, free_balance: T::Balance },
330
		/// An account was removed whose balance was non-zero but below ExistentialDeposit,
331
		/// resulting in an outright loss.
332
		DustLost { account: T::AccountId, amount: T::Balance },
333
		/// Transfer succeeded.
334
		Transfer { from: T::AccountId, to: T::AccountId, amount: T::Balance },
335
		/// A balance was set by root.
336
		BalanceSet { who: T::AccountId, free: T::Balance },
337
		/// Some balance was reserved (moved from free to reserved).
338
		Reserved { who: T::AccountId, amount: T::Balance },
339
		/// Some balance was unreserved (moved from reserved to free).
340
		Unreserved { who: T::AccountId, amount: T::Balance },
341
		/// Some balance was moved from the reserve of the first account to the second account.
342
		/// Final argument indicates the destination balance type.
343
		ReserveRepatriated {
344
			from: T::AccountId,
345
			to: T::AccountId,
346
			amount: T::Balance,
347
			destination_status: Status,
348
		},
349
		/// Some amount was deposited (e.g. for transaction fees).
350
		Deposit { who: T::AccountId, amount: T::Balance },
351
		/// Some amount was withdrawn from the account (e.g. for transaction fees).
352
		Withdraw { who: T::AccountId, amount: T::Balance },
353
		/// Some amount was removed from the account (e.g. for misbehavior).
354
		Slashed { who: T::AccountId, amount: T::Balance },
355
		/// Some amount was minted into an account.
356
		Minted { who: T::AccountId, amount: T::Balance },
357
		/// Some amount was burned from an account.
358
		Burned { who: T::AccountId, amount: T::Balance },
359
		/// Some amount was suspended from an account (it can be restored later).
360
		Suspended { who: T::AccountId, amount: T::Balance },
361
		/// Some amount was restored into an account.
362
		Restored { who: T::AccountId, amount: T::Balance },
363
		/// An account was upgraded.
364
		Upgraded { who: T::AccountId },
365
		/// Total issuance was increased by `amount`, creating a credit to be balanced.
366
		Issued { amount: T::Balance },
367
		/// Total issuance was decreased by `amount`, creating a debt to be balanced.
368
		Rescinded { amount: T::Balance },
369
		/// Some balance was locked.
370
		Locked { who: T::AccountId, amount: T::Balance },
371
		/// Some balance was unlocked.
372
		Unlocked { who: T::AccountId, amount: T::Balance },
373
		/// Some balance was frozen.
374
		Frozen { who: T::AccountId, amount: T::Balance },
375
		/// Some balance was thawed.
376
		Thawed { who: T::AccountId, amount: T::Balance },
377
		/// The `TotalIssuance` was forcefully changed.
378
		TotalIssuanceForced { old: T::Balance, new: T::Balance },
379
	}
380

            
381
3636
	#[pallet::error]
382
	pub enum Error<T, I = ()> {
383
		/// Vesting balance too high to send value.
384
		VestingBalance,
385
		/// Account liquidity restrictions prevent withdrawal.
386
		LiquidityRestrictions,
387
		/// Balance too low to send value.
388
		InsufficientBalance,
389
		/// Value too low to create account due to existential deposit.
390
		ExistentialDeposit,
391
		/// Transfer/payment would kill account.
392
		Expendability,
393
		/// A vesting schedule already exists for this account.
394
		ExistingVestingSchedule,
395
		/// Beneficiary account must pre-exist.
396
		DeadAccount,
397
		/// Number of named reserves exceed `MaxReserves`.
398
		TooManyReserves,
399
		/// Number of holds exceed `VariantCountOf<T::RuntimeHoldReason>`.
400
		TooManyHolds,
401
		/// Number of freezes exceed `MaxFreezes`.
402
		TooManyFreezes,
403
		/// The issuance cannot be modified since it is already deactivated.
404
		IssuanceDeactivated,
405
		/// The delta cannot be zero.
406
		DeltaZero,
407
	}
408

            
409
	/// The total units issued in the system.
410
365082
	#[pallet::storage]
411
	#[pallet::getter(fn total_issuance)]
412
	#[pallet::whitelist_storage]
413
	pub type TotalIssuance<T: Config<I>, I: 'static = ()> = StorageValue<_, T::Balance, ValueQuery>;
414

            
415
	/// The total units of outstanding deactivated balance in the system.
416
1992
	#[pallet::storage]
417
	#[pallet::getter(fn inactive_issuance)]
418
	#[pallet::whitelist_storage]
419
	pub type InactiveIssuance<T: Config<I>, I: 'static = ()> =
420
		StorageValue<_, T::Balance, ValueQuery>;
421

            
422
	/// The Balances pallet example of storing the balance of an account.
423
	///
424
	/// # Example
425
	///
426
	/// ```nocompile
427
	///  impl pallet_balances::Config for Runtime {
428
	///    type AccountStore = StorageMapShim<Self::Account<Runtime>, frame_system::Provider<Runtime>, AccountId, Self::AccountData<Balance>>
429
	///  }
430
	/// ```
431
	///
432
	/// You can also store the balance of an account in the `System` pallet.
433
	///
434
	/// # Example
435
	///
436
	/// ```nocompile
437
	///  impl pallet_balances::Config for Runtime {
438
	///   type AccountStore = System
439
	///  }
440
	/// ```
441
	///
442
	/// But this comes with tradeoffs, storing account balances in the system pallet stores
443
	/// `frame_system` data alongside the account data contrary to storing account balances in the
444
	/// `Balances` pallet, which uses a `StorageMap` to store balances data only.
445
	/// NOTE: This is only used in the case that this pallet is used to store balances.
446
	#[pallet::storage]
447
	pub type Account<T: Config<I>, I: 'static = ()> =
448
		StorageMap<_, Blake2_128Concat, T::AccountId, AccountData<T::Balance>, ValueQuery>;
449

            
450
	/// Any liquidity locks on some account balances.
451
	/// NOTE: Should only be accessed when setting, changing and freeing a lock.
452
	///
453
	/// Use of locks is deprecated in favour of freezes. See `https://github.com/paritytech/substrate/pull/12951/`
454
967581
	#[pallet::storage]
455
	#[pallet::getter(fn locks)]
456
	pub type Locks<T: Config<I>, I: 'static = ()> = StorageMap<
457
		_,
458
		Blake2_128Concat,
459
		T::AccountId,
460
		WeakBoundedVec<BalanceLock<T::Balance>, T::MaxLocks>,
461
		ValueQuery,
462
	>;
463

            
464
	/// Named reserves on some account balances.
465
	///
466
	/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
467
	#[pallet::storage]
468
	#[pallet::getter(fn reserves)]
469
	pub type Reserves<T: Config<I>, I: 'static = ()> = StorageMap<
470
		_,
471
		Blake2_128Concat,
472
		T::AccountId,
473
		BoundedVec<ReserveData<T::ReserveIdentifier, T::Balance>, T::MaxReserves>,
474
		ValueQuery,
475
	>;
476

            
477
	/// Holds on account balances.
478
604173
	#[pallet::storage]
479
	pub type Holds<T: Config<I>, I: 'static = ()> = StorageMap<
480
		_,
481
		Blake2_128Concat,
482
		T::AccountId,
483
		BoundedVec<
484
			IdAmount<T::RuntimeHoldReason, T::Balance>,
485
			VariantCountOf<T::RuntimeHoldReason>,
486
		>,
487
		ValueQuery,
488
	>;
489

            
490
	/// Freeze locks on account balances.
491
107637
	#[pallet::storage]
492
	pub type Freezes<T: Config<I>, I: 'static = ()> = StorageMap<
493
		_,
494
		Blake2_128Concat,
495
		T::AccountId,
496
		BoundedVec<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
497
		ValueQuery,
498
	>;
499

            
500
	#[pallet::genesis_config]
501
	pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
502
		pub balances: Vec<(T::AccountId, T::Balance)>,
503
	}
504

            
505
	impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
506
		fn default() -> Self {
507
			Self { balances: Default::default() }
508
		}
509
	}
510

            
511
	#[pallet::genesis_build]
512
	impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
513
3
		fn build(&self) {
514
24
			let total = self.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n);
515
3

            
516
3
			<TotalIssuance<T, I>>::put(total);
517

            
518
27
			for (_, balance) in &self.balances {
519
24
				assert!(
520
24
					*balance >= <T as Config<I>>::ExistentialDeposit::get(),
521
					"the balance of any account should always be at least the existential deposit.",
522
				)
523
			}
524

            
525
			// ensure no duplicates exist.
526
3
			let endowed_accounts = self
527
3
				.balances
528
3
				.iter()
529
25
				.map(|(x, _)| x)
530
3
				.cloned()
531
3
				.collect::<alloc::collections::btree_set::BTreeSet<_>>();
532
3

            
533
3
			assert!(
534
3
				endowed_accounts.len() == self.balances.len(),
535
				"duplicate balances in genesis."
536
			);
537

            
538
24
			for &(ref who, free) in self.balances.iter() {
539
24
				frame_system::Pallet::<T>::inc_providers(who);
540
24
				assert!(T::AccountStore::insert(who, AccountData { free, ..Default::default() })
541
24
					.is_ok());
542
			}
543
3
		}
544
	}
545

            
546
554367
	#[pallet::hooks]
547
	impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
548
53637
		fn integrity_test() {
549
53637
			#[cfg(not(feature = "insecure_zero_ed"))]
550
53637
			assert!(
551
53637
				!<T as Config<I>>::ExistentialDeposit::get().is_zero(),
552
				"The existential deposit must be greater than zero!"
553
			);
554

            
555
53637
			assert!(
556
53637
				T::MaxFreezes::get() >= <T::RuntimeFreezeReason as VariantCount>::VARIANT_COUNT,
557
				"MaxFreezes should be greater than or equal to the number of freeze reasons: {} < {}",
558
				T::MaxFreezes::get(), <T::RuntimeFreezeReason as VariantCount>::VARIANT_COUNT,
559
			);
560
53637
		}
561

            
562
		#[cfg(feature = "try-runtime")]
563
53637
		fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
564
53637
			Holds::<T, I>::iter_keys().try_for_each(|k| {
565
867
				if Holds::<T, I>::decode_len(k).unwrap_or(0) >
566
867
					T::RuntimeHoldReason::VARIANT_COUNT as usize
567
				{
568
					Err("Found `Hold` with too many elements")
569
				} else {
570
867
					Ok(())
571
				}
572
53637
			})?;
573

            
574
53637
			Freezes::<T, I>::iter_keys().try_for_each(|k| {
575
				if Freezes::<T, I>::decode_len(k).unwrap_or(0) > T::MaxFreezes::get() as usize {
576
					Err("Found `Freeze` with too many elements")
577
				} else {
578
					Ok(())
579
				}
580
53637
			})?;
581

            
582
53637
			Ok(())
583
53637
		}
584
	}
585

            
586
28560
	#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
587
	impl<T: Config<I>, I: 'static> Pallet<T, I> {
588
		/// Transfer some liquid free balance to another account.
589
		///
590
		/// `transfer_allow_death` will set the `FreeBalance` of the sender and receiver.
591
		/// If the sender's account is below the existential deposit as a result
592
		/// of the transfer, the account will be reaped.
593
		///
594
		/// The dispatch origin for this call must be `Signed` by the transactor.
595
		#[pallet::call_index(0)]
596
		pub fn transfer_allow_death(
597
			origin: OriginFor<T>,
598
			dest: AccountIdLookupOf<T>,
599
			#[pallet::compact] value: T::Balance,
600
3441
		) -> DispatchResult {
601
3441
			let source = ensure_signed(origin)?;
602
3441
			let dest = T::Lookup::lookup(dest)?;
603
2880
			<Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Expendable)?;
604
2418
			Ok(())
605
		}
606

            
607
		/// Exactly as `transfer_allow_death`, except the origin must be root and the source account
608
		/// may be specified.
609
		#[pallet::call_index(2)]
610
		pub fn force_transfer(
611
			origin: OriginFor<T>,
612
			source: AccountIdLookupOf<T>,
613
			dest: AccountIdLookupOf<T>,
614
			#[pallet::compact] value: T::Balance,
615
330
		) -> DispatchResult {
616
330
			ensure_root(origin)?;
617
			let source = T::Lookup::lookup(source)?;
618
			let dest = T::Lookup::lookup(dest)?;
619
			<Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Expendable)?;
620
			Ok(())
621
		}
622

            
623
		/// Same as the [`transfer_allow_death`] call, but with a check that the transfer will not
624
		/// kill the origin account.
625
		///
626
		/// 99% of the time you want [`transfer_allow_death`] instead.
627
		///
628
		/// [`transfer_allow_death`]: struct.Pallet.html#method.transfer
629
		#[pallet::call_index(3)]
630
		pub fn transfer_keep_alive(
631
			origin: OriginFor<T>,
632
			dest: AccountIdLookupOf<T>,
633
			#[pallet::compact] value: T::Balance,
634
339
		) -> DispatchResult {
635
339
			let source = ensure_signed(origin)?;
636
339
			let dest = T::Lookup::lookup(dest)?;
637
108
			<Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Preserve)?;
638
87
			Ok(())
639
		}
640

            
641
		/// Transfer the entire transferable balance from the caller account.
642
		///
643
		/// NOTE: This function only attempts to transfer _transferable_ balances. This means that
644
		/// any locked, reserved, or existential deposits (when `keep_alive` is `true`), will not be
645
		/// transferred by this function. To ensure that this function results in a killed account,
646
		/// you might need to prepare the account by removing any reference counters, storage
647
		/// deposits, etc...
648
		///
649
		/// The dispatch origin of this call must be Signed.
650
		///
651
		/// - `dest`: The recipient of the transfer.
652
		/// - `keep_alive`: A boolean to determine if the `transfer_all` operation should send all
653
		///   of the funds the account has, causing the sender account to be killed (false), or
654
		///   transfer everything except at least the existential deposit, which will guarantee to
655
		///   keep the sender account alive (true).
656
		#[pallet::call_index(4)]
657
		pub fn transfer_all(
658
			origin: OriginFor<T>,
659
			dest: AccountIdLookupOf<T>,
660
			keep_alive: bool,
661
753
		) -> DispatchResult {
662
753
			let transactor = ensure_signed(origin)?;
663
753
			let keep_alive = if keep_alive { Preserve } else { Expendable };
664
753
			let reducible_balance = <Self as fungible::Inspect<_>>::reducible_balance(
665
753
				&transactor,
666
753
				keep_alive,
667
753
				Fortitude::Polite,
668
753
			);
669
753
			let dest = T::Lookup::lookup(dest)?;
670
531
			<Self as fungible::Mutate<_>>::transfer(
671
531
				&transactor,
672
531
				&dest,
673
531
				reducible_balance,
674
531
				keep_alive,
675
531
			)?;
676
531
			Ok(())
677
		}
678

            
679
		/// Unreserve some balance from a user by force.
680
		///
681
		/// Can only be called by ROOT.
682
		#[pallet::call_index(5)]
683
		pub fn force_unreserve(
684
			origin: OriginFor<T>,
685
			who: AccountIdLookupOf<T>,
686
			amount: T::Balance,
687
60
		) -> DispatchResult {
688
60
			ensure_root(origin)?;
689
			let who = T::Lookup::lookup(who)?;
690
			let _leftover = <Self as ReservableCurrency<_>>::unreserve(&who, amount);
691
			Ok(())
692
		}
693

            
694
		/// Upgrade a specified account.
695
		///
696
		/// - `origin`: Must be `Signed`.
697
		/// - `who`: The account to be upgraded.
698
		///
699
		/// This will waive the transaction fee if at least all but 10% of the accounts needed to
700
		/// be upgraded. (We let some not have to be upgraded just in order to allow for the
701
		/// possibility of churn).
702
		#[pallet::call_index(6)]
703
		#[pallet::weight(T::WeightInfo::upgrade_accounts(who.len() as u32))]
704
		pub fn upgrade_accounts(
705
			origin: OriginFor<T>,
706
			who: Vec<T::AccountId>,
707
309
		) -> DispatchResultWithPostInfo {
708
309
			ensure_signed(origin)?;
709
309
			if who.is_empty() {
710
273
				return Ok(Pays::Yes.into())
711
36
			}
712
36
			let mut upgrade_count = 0;
713
435
			for i in &who {
714
399
				let upgraded = Self::ensure_upgraded(i);
715
399
				if upgraded {
716
					upgrade_count.saturating_inc();
717
399
				}
718
			}
719
36
			let proportion_upgraded = Perbill::from_rational(upgrade_count, who.len() as u32);
720
36
			if proportion_upgraded >= Perbill::from_percent(90) {
721
				Ok(Pays::No.into())
722
			} else {
723
36
				Ok(Pays::Yes.into())
724
			}
725
		}
726

            
727
		/// Set the regular balance of a given account.
728
		///
729
		/// The dispatch origin for this call is `root`.
730
		#[pallet::call_index(8)]
731
		#[pallet::weight(
732
			T::WeightInfo::force_set_balance_creating() // Creates a new account.
733
				.max(T::WeightInfo::force_set_balance_killing()) // Kills an existing account.
734
		)]
735
		pub fn force_set_balance(
736
			origin: OriginFor<T>,
737
			who: AccountIdLookupOf<T>,
738
			#[pallet::compact] new_free: T::Balance,
739
177
		) -> DispatchResult {
740
177
			ensure_root(origin)?;
741
			let who = T::Lookup::lookup(who)?;
742
			let existential_deposit = Self::ed();
743

            
744
			let wipeout = new_free < existential_deposit;
745
			let new_free = if wipeout { Zero::zero() } else { new_free };
746

            
747
			// First we try to modify the account's balance to the forced balance.
748
			let old_free = Self::mutate_account_handling_dust(&who, |account| {
749
				let old_free = account.free;
750
				account.free = new_free;
751
				old_free
752
			})?;
753

            
754
			// This will adjust the total issuance, which was not done by the `mutate_account`
755
			// above.
756
			if new_free > old_free {
757
				mem::drop(PositiveImbalance::<T, I>::new(new_free - old_free));
758
			} else if new_free < old_free {
759
				mem::drop(NegativeImbalance::<T, I>::new(old_free - new_free));
760
			}
761

            
762
			Self::deposit_event(Event::BalanceSet { who, free: new_free });
763
			Ok(())
764
		}
765

            
766
		/// Adjust the total issuance in a saturating way.
767
		///
768
		/// Can only be called by root and always needs a positive `delta`.
769
		///
770
		/// # Example
771
		#[doc = docify::embed!("./src/tests/dispatchable_tests.rs", force_adjust_total_issuance_example)]
772
		#[pallet::call_index(9)]
773
		#[pallet::weight(T::WeightInfo::force_adjust_total_issuance())]
774
		pub fn force_adjust_total_issuance(
775
			origin: OriginFor<T>,
776
			direction: AdjustmentDirection,
777
			#[pallet::compact] delta: T::Balance,
778
18
		) -> DispatchResult {
779
18
			ensure_root(origin)?;
780

            
781
			ensure!(delta > Zero::zero(), Error::<T, I>::DeltaZero);
782

            
783
			let old = TotalIssuance::<T, I>::get();
784
			let new = match direction {
785
				AdjustmentDirection::Increase => old.saturating_add(delta),
786
				AdjustmentDirection::Decrease => old.saturating_sub(delta),
787
			};
788

            
789
			ensure!(InactiveIssuance::<T, I>::get() <= new, Error::<T, I>::IssuanceDeactivated);
790
			TotalIssuance::<T, I>::set(new);
791

            
792
			Self::deposit_event(Event::<T, I>::TotalIssuanceForced { old, new });
793

            
794
			Ok(())
795
		}
796

            
797
		/// Burn the specified liquid free balance from the origin account.
798
		///
799
		/// If the origin's account ends up below the existential deposit as a result
800
		/// of the burn and `keep_alive` is false, the account will be reaped.
801
		///
802
		/// Unlike sending funds to a _burn_ address, which merely makes the funds inaccessible,
803
		/// this `burn` operation will reduce total issuance by the amount _burned_.
804
		#[pallet::call_index(10)]
805
		#[pallet::weight(if *keep_alive {T::WeightInfo::burn_allow_death() } else {T::WeightInfo::burn_keep_alive()})]
806
		pub fn burn(
807
			origin: OriginFor<T>,
808
			#[pallet::compact] value: T::Balance,
809
			keep_alive: bool,
810
33
		) -> DispatchResult {
811
33
			let source = ensure_signed(origin)?;
812
33
			let preservation = if keep_alive { Preserve } else { Expendable };
813
33
			<Self as fungible::Mutate<_>>::burn_from(
814
33
				&source,
815
33
				value,
816
33
				preservation,
817
33
				Precision::Exact,
818
33
				Polite,
819
33
			)?;
820
33
			Ok(())
821
		}
822
	}
823

            
824
	impl<T: Config<I>, I: 'static> Pallet<T, I> {
825
58635
		fn ed() -> T::Balance {
826
58635
			T::ExistentialDeposit::get()
827
58635
		}
828
		/// Ensure the account `who` is using the new logic.
829
		///
830
		/// Returns `true` if the account did get upgraded, `false` if it didn't need upgrading.
831
21156
		pub fn ensure_upgraded(who: &T::AccountId) -> bool {
832
21156
			let mut a = T::AccountStore::get(who);
833
21156
			if a.flags.is_new_logic() {
834
21156
				return false
835
			}
836
			a.flags.set_new_logic();
837
			if !a.reserved.is_zero() && a.frozen.is_zero() {
838
				if system::Pallet::<T>::providers(who) == 0 {
839
					// Gah!! We have no provider refs :(
840
					// This shouldn't practically happen, but we need a failsafe anyway: let's give
841
					// them enough for an ED.
842
					log::warn!(
843
						target: LOG_TARGET,
844
						"account with a non-zero reserve balance has no provider refs, account_id: '{:?}'.",
845
						who
846
					);
847
					a.free = a.free.max(Self::ed());
848
					system::Pallet::<T>::inc_providers(who);
849
				}
850
				let _ = system::Pallet::<T>::inc_consumers_without_limit(who).defensive();
851
			}
852
			// Should never fail - we're only setting a bit.
853
			let _ = T::AccountStore::try_mutate_exists(who, |account| -> DispatchResult {
854
				*account = Some(a);
855
				Ok(())
856
			});
857
			Self::deposit_event(Event::Upgraded { who: who.clone() });
858
			return true
859
21156
		}
860

            
861
		/// Get the free balance of an account.
862
		pub fn free_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
863
			Self::account(who.borrow()).free
864
		}
865

            
866
		/// Get the balance of an account that can be used for transfers, reservations, or any other
867
		/// non-locking, non-transaction-fee activity. Will be at most `free_balance`.
868
		pub fn usable_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
869
			<Self as fungible::Inspect<_>>::reducible_balance(who.borrow(), Expendable, Polite)
870
		}
871

            
872
		/// Get the balance of an account that can be used for paying transaction fees (not tipping,
873
		/// or any other kind of fees, though). Will be at most `free_balance`.
874
		///
875
		/// This requires that the account stays alive.
876
		pub fn usable_balance_for_fees(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
877
			<Self as fungible::Inspect<_>>::reducible_balance(who.borrow(), Protect, Polite)
878
		}
879

            
880
		/// Get the reserved balance of an account.
881
		pub fn reserved_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
882
			Self::account(who.borrow()).reserved
883
		}
884

            
885
		/// Get both the free and reserved balances of an account.
886
240726
		pub(crate) fn account(who: &T::AccountId) -> AccountData<T::Balance> {
887
240726
			T::AccountStore::get(who)
888
240726
		}
889

            
890
		/// Mutate an account to some new value, or delete it entirely with `None`. Will enforce
891
		/// `ExistentialDeposit` law, annulling the account as needed.
892
		///
893
		/// It returns the result from the closure. Any dust is handled through the low-level
894
		/// `fungible::Unbalanced` trap-door for legacy dust management.
895
		///
896
		/// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used
897
		/// when it is known that the account already exists.
898
		///
899
		/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
900
		/// the caller will do this.
901
246
		pub(crate) fn mutate_account_handling_dust<R>(
902
246
			who: &T::AccountId,
903
246
			f: impl FnOnce(&mut AccountData<T::Balance>) -> R,
904
246
		) -> Result<R, DispatchError> {
905
246
			let (r, maybe_dust) = Self::mutate_account(who, f)?;
906
246
			if let Some(dust) = maybe_dust {
907
				<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
908
246
			}
909
246
			Ok(r)
910
246
		}
911

            
912
		/// Mutate an account to some new value, or delete it entirely with `None`. Will enforce
913
		/// `ExistentialDeposit` law, annulling the account as needed.
914
		///
915
		/// It returns the result from the closure. Any dust is handled through the low-level
916
		/// `fungible::Unbalanced` trap-door for legacy dust management.
917
		///
918
		/// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used
919
		/// when it is known that the account already exists.
920
		///
921
		/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
922
		/// the caller will do this.
923
8754
		pub(crate) fn try_mutate_account_handling_dust<R, E: From<DispatchError>>(
924
8754
			who: &T::AccountId,
925
8754
			f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
926
8754
		) -> Result<R, E> {
927
8754
			let (r, maybe_dust) = Self::try_mutate_account(who, f)?;
928
6936
			if let Some(dust) = maybe_dust {
929
3
				<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
930
6933
			}
931
6936
			Ok(r)
932
8754
		}
933

            
934
		/// Mutate an account to some new value, or delete it entirely with `None`. Will enforce
935
		/// `ExistentialDeposit` law, annulling the account as needed.
936
		///
937
		/// It returns both the result from the closure, and an optional amount of dust
938
		/// which should be handled once it is known that all nested mutates that could affect
939
		/// storage items what the dust handler touches have completed.
940
		///
941
		/// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used
942
		/// when it is known that the account already exists.
943
		///
944
		/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
945
		/// the caller will do this.
946
9051
		pub(crate) fn mutate_account<R>(
947
9051
			who: &T::AccountId,
948
9051
			f: impl FnOnce(&mut AccountData<T::Balance>) -> R,
949
9051
		) -> Result<(R, Option<T::Balance>), DispatchError> {
950
12068
			Self::try_mutate_account(who, |a, _| -> Result<R, DispatchError> { Ok(f(a)) })
951
9051
		}
952

            
953
		/// Returns `true` when `who` has some providers or `insecure_zero_ed` feature is disabled.
954
		/// Returns `false` otherwise.
955
		#[cfg(not(feature = "insecure_zero_ed"))]
956
17820
		fn have_providers_or_no_zero_ed(_: &T::AccountId) -> bool {
957
17820
			true
958
17820
		}
959

            
960
		/// Returns `true` when `who` has some providers or `insecure_zero_ed` feature is disabled.
961
		/// Returns `false` otherwise.
962
		#[cfg(feature = "insecure_zero_ed")]
963
		fn have_providers_or_no_zero_ed(who: &T::AccountId) -> bool {
964
			frame_system::Pallet::<T>::providers(who) > 0
965
		}
966

            
967
		/// Mutate an account to some new value, or delete it entirely with `None`. Will enforce
968
		/// `ExistentialDeposit` law, annulling the account as needed. This will do nothing if the
969
		/// result of `f` is an `Err`.
970
		///
971
		/// It returns both the result from the closure, and an optional amount of dust
972
		/// which should be handled once it is known that all nested mutates that could affect
973
		/// storage items what the dust handler touches have completed.
974
		///
975
		/// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used
976
		/// when it is known that the account already exists.
977
		///
978
		/// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that
979
		/// the caller will do this.
980
20757
		pub(crate) fn try_mutate_account<R, E: From<DispatchError>>(
981
20757
			who: &T::AccountId,
982
20757
			f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
983
20757
		) -> Result<(R, Option<T::Balance>), E> {
984
20757
			Self::ensure_upgraded(who);
985
27676
			let result = T::AccountStore::try_mutate_exists(who, |maybe_account| {
986
20757
				let is_new = maybe_account.is_none();
987
20757
				let mut account = maybe_account.take().unwrap_or_default();
988
20757
				let did_provide =
989
20757
					account.free >= Self::ed() && Self::have_providers_or_no_zero_ed(who);
990
20757
				let did_consume =
991
20757
					!is_new && (!account.reserved.is_zero() || !account.frozen.is_zero());
992

            
993
20757
				let result = f(&mut account, is_new)?;
994

            
995
18939
				let does_provide = account.free >= Self::ed();
996
18939
				let does_consume = !account.reserved.is_zero() || !account.frozen.is_zero();
997

            
998
18939
				if !did_provide && does_provide {
999
318
					frame_system::Pallet::<T>::inc_providers(who);
18621
				}
18939
				if did_consume && !does_consume {
105
					frame_system::Pallet::<T>::dec_consumers(who);
18834
				}
18939
				if !did_consume && does_consume {
3552
					frame_system::Pallet::<T>::inc_consumers(who)?;
15387
				}
18939
				if does_consume && frame_system::Pallet::<T>::consumers(who) == 0 {
					// NOTE: This is a failsafe and should not happen for normal accounts. A normal
					// account should have gotten a consumer ref in `!did_consume && does_consume`
					// at some point.
					log::error!(target: LOG_TARGET, "Defensively bumping a consumer ref.");
					frame_system::Pallet::<T>::inc_consumers(who)?;
18939
				}
18939
				if did_provide && !does_provide {
					// This could reap the account so must go last.
159
					frame_system::Pallet::<T>::dec_providers(who).map_err(|r| {
						// best-effort revert consumer change.
						if did_consume && !does_consume {
							let _ = frame_system::Pallet::<T>::inc_consumers(who).defensive();
						}
						if !did_consume && does_consume {
							let _ = frame_system::Pallet::<T>::dec_consumers(who);
						}
						r
159
					})?;
18780
				}
18939
				let maybe_endowed = if is_new { Some(account.free) } else { None };
				// Handle any steps needed after mutating an account.
				//
				// This includes DustRemoval unbalancing, in the case than the `new` account's total
				// balance is non-zero but below ED.
				//
				// Updates `maybe_account` to `Some` iff the account has sufficient balance.
				// Evaluates `maybe_dust`, which is `Some` containing the dust to be dropped, iff
				// some dust should be dropped.
				//
				// We should never be dropping if reserved is non-zero. Reserved being non-zero
				// should imply that we have a consumer ref, so this is economically safe.
18939
				let ed = Self::ed();
18939
				let maybe_dust = if account.free < ed && account.reserved.is_zero() {
960
					if account.free.is_zero() {
957
						None
					} else {
3
						Some(account.free)
					}
				} else {
17979
					assert!(
17979
						account.free.is_zero() || account.free >= ed || !account.reserved.is_zero()
					);
17979
					*maybe_account = Some(account);
17979
					None
				};
18939
				Ok((maybe_endowed, maybe_dust, result))
27676
			});
20757
			result.map(|(maybe_endowed, maybe_dust, result)| {
18939
				if let Some(endowed) = maybe_endowed {
1119
					Self::deposit_event(Event::Endowed {
1119
						account: who.clone(),
1119
						free_balance: endowed,
1119
					});
17820
				}
18939
				if let Some(amount) = maybe_dust {
3
					Pallet::<T, I>::deposit_event(Event::DustLost { account: who.clone(), amount });
18936
				}
18939
				(result, maybe_dust)
20757
			})
20757
		}
		/// Update the account entry for `who`, given the locks.
363
		pub(crate) fn update_locks(who: &T::AccountId, locks: &[BalanceLock<T::Balance>]) {
363
			let bounded_locks = WeakBoundedVec::<_, T::MaxLocks>::force_from(
363
				locks.to_vec(),
363
				Some("Balances Update Locks"),
363
			);
363

            
363
			if locks.len() as u32 > T::MaxLocks::get() {
				log::warn!(
					target: LOG_TARGET,
					"Warning: A user has more currency locks than expected. \
					A runtime configuration adjustment may be needed."
				);
363
			}
363
			let freezes = Freezes::<T, I>::get(who);
363
			let mut prev_frozen = Zero::zero();
363
			let mut after_frozen = Zero::zero();
363
			// No way this can fail since we do not alter the existential balances.
363
			// TODO: Revisit this assumption.
484
			let res = Self::mutate_account(who, |b| {
363
				prev_frozen = b.frozen;
363
				b.frozen = Zero::zero();
363
				for l in locks.iter() {
48
					b.frozen = b.frozen.max(l.amount);
48
				}
363
				for l in freezes.iter() {
					b.frozen = b.frozen.max(l.amount);
				}
363
				after_frozen = b.frozen;
484
			});
363
			debug_assert!(res.is_ok());
363
			if let Ok((_, maybe_dust)) = res {
363
				debug_assert!(maybe_dust.is_none(), "Not altering main balance; qed");
			}
363
			match locks.is_empty() {
315
				true => Locks::<T, I>::remove(who),
48
				false => Locks::<T, I>::insert(who, bounded_locks),
			}
363
			if prev_frozen > after_frozen {
				let amount = prev_frozen.saturating_sub(after_frozen);
				Self::deposit_event(Event::Unlocked { who: who.clone(), amount });
363
			} else if after_frozen > prev_frozen {
48
				let amount = after_frozen.saturating_sub(prev_frozen);
48
				Self::deposit_event(Event::Locked { who: who.clone(), amount });
315
			}
363
		}
		/// Update the account entry for `who`, given the locks.
		pub(crate) fn update_freezes(
			who: &T::AccountId,
			freezes: BoundedSlice<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
		) -> DispatchResult {
			let mut prev_frozen = Zero::zero();
			let mut after_frozen = Zero::zero();
			let (_, maybe_dust) = Self::mutate_account(who, |b| {
				prev_frozen = b.frozen;
				b.frozen = Zero::zero();
				for l in Locks::<T, I>::get(who).iter() {
					b.frozen = b.frozen.max(l.amount);
				}
				for l in freezes.iter() {
					b.frozen = b.frozen.max(l.amount);
				}
				after_frozen = b.frozen;
			})?;
			debug_assert!(maybe_dust.is_none(), "Not altering main balance; qed");
			if freezes.is_empty() {
				Freezes::<T, I>::remove(who);
			} else {
				Freezes::<T, I>::insert(who, freezes);
			}
			if prev_frozen > after_frozen {
				let amount = prev_frozen.saturating_sub(after_frozen);
				Self::deposit_event(Event::Thawed { who: who.clone(), amount });
			} else if after_frozen > prev_frozen {
				let amount = after_frozen.saturating_sub(prev_frozen);
				Self::deposit_event(Event::Frozen { who: who.clone(), amount });
			}
			Ok(())
		}
		/// Move the reserved balance of one account into the balance of another, according to
		/// `status`. This will respect freezes/locks only if `fortitude` is `Polite`.
		///
		/// Is a no-op if the value to be moved is zero.
		///
		/// NOTE: returns actual amount of transferred value in `Ok` case.
		pub(crate) fn do_transfer_reserved(
			slashed: &T::AccountId,
			beneficiary: &T::AccountId,
			value: T::Balance,
			precision: Precision,
			fortitude: Fortitude,
			status: Status,
		) -> Result<T::Balance, DispatchError> {
			if value.is_zero() {
				return Ok(Zero::zero())
			}
			let max = <Self as fungible::InspectHold<_>>::reducible_total_balance_on_hold(
				slashed, fortitude,
			);
			let actual = match precision {
				Precision::BestEffort => value.min(max),
				Precision::Exact => value,
			};
			ensure!(actual <= max, TokenError::FundsUnavailable);
			if slashed == beneficiary {
				return match status {
					Status::Free => Ok(actual.saturating_sub(Self::unreserve(slashed, actual))),
					Status::Reserved => Ok(actual),
				}
			}
			let ((_, maybe_dust_1), maybe_dust_2) = Self::try_mutate_account(
				beneficiary,
				|to_account, is_new| -> Result<((), Option<T::Balance>), DispatchError> {
					ensure!(!is_new, Error::<T, I>::DeadAccount);
					Self::try_mutate_account(slashed, |from_account, _| -> DispatchResult {
						match status {
							Status::Free =>
								to_account.free = to_account
									.free
									.checked_add(&actual)
									.ok_or(ArithmeticError::Overflow)?,
							Status::Reserved =>
								to_account.reserved = to_account
									.reserved
									.checked_add(&actual)
									.ok_or(ArithmeticError::Overflow)?,
						}
						from_account.reserved.saturating_reduce(actual);
						Ok(())
					})
				},
			)?;
			if let Some(dust) = maybe_dust_1 {
				<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
			}
			if let Some(dust) = maybe_dust_2 {
				<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
			}
			Self::deposit_event(Event::ReserveRepatriated {
				from: slashed.clone(),
				to: beneficiary.clone(),
				amount: actual,
				destination_status: status,
			});
			Ok(actual)
		}
	}
}