1#![cfg_attr(not(feature = "std"), no_std)]
145mod benchmarking;
146mod impl_currency;
147mod impl_fungible;
148pub mod migration;
149mod tests;
150mod types;
151pub mod weights;
152
153extern crate alloc;
154
155use alloc::{
156 format,
157 string::{String, ToString},
158 vec::Vec,
159};
160use codec::{Codec, MaxEncodedLen};
161use core::{cmp, fmt::Debug, mem, result};
162use frame_support::{
163 ensure,
164 pallet_prelude::DispatchResult,
165 traits::{
166 tokens::{
167 fungible, BalanceStatus as Status, DepositConsequence,
168 Fortitude::{self, Force, Polite},
169 IdAmount,
170 Preservation::{Expendable, Preserve, Protect},
171 WithdrawConsequence,
172 },
173 Currency, Defensive, Get, OnUnbalanced, ReservableCurrency, StoredMap,
174 },
175 BoundedSlice, WeakBoundedVec,
176};
177use frame_system as system;
178pub use impl_currency::{NegativeImbalance, PositiveImbalance};
179use scale_info::TypeInfo;
180use sp_core::{sr25519::Pair as SrPair, Pair};
181use sp_runtime::{
182 traits::{
183 AtLeast32BitUnsigned, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Saturating,
184 StaticLookup, Zero,
185 },
186 ArithmeticError, DispatchError, FixedPointOperand, Perbill, RuntimeDebug, TokenError,
187};
188
189pub use types::{
190 AccountData, AdjustmentDirection, BalanceLock, DustCleaner, ExtraFlags, Reasons, ReserveData,
191};
192pub use weights::WeightInfo;
193
194pub use pallet::*;
195
196const LOG_TARGET: &str = "runtime::balances";
197
198const 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 pub mod config_preludes {
217 use super::*;
218 use frame_support::derive_impl;
219
220 pub struct TestDefaultConfig;
221
222 #[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
223 impl frame_system::DefaultConfig for TestDefaultConfig {}
224
225 #[frame_support::register_default_impl(TestDefaultConfig)]
226 impl DefaultConfig for TestDefaultConfig {
227 #[inject_runtime_type]
228 type RuntimeEvent = ();
229 #[inject_runtime_type]
230 type RuntimeHoldReason = ();
231 #[inject_runtime_type]
232 type RuntimeFreezeReason = ();
233
234 type Balance = u64;
235 type ExistentialDeposit = ConstUint<1>;
236
237 type ReserveIdentifier = ();
238 type FreezeIdentifier = Self::RuntimeFreezeReason;
239
240 type DustRemoval = ();
241
242 type MaxLocks = ConstU32<100>;
243 type MaxReserves = ConstU32<100>;
244 type MaxFreezes = VariantCountOf<Self::RuntimeFreezeReason>;
245
246 type WeightInfo = ();
247 type DoneSlashHandler = ();
248 }
249 }
250
251 #[pallet::config(with_default)]
252 pub trait Config<I: 'static = ()>: frame_system::Config {
253 #[pallet::no_default_bounds]
255 #[allow(deprecated)]
256 type RuntimeEvent: From<Event<Self, I>>
257 + IsType<<Self as frame_system::Config>::RuntimeEvent>;
258
259 #[pallet::no_default_bounds]
261 type RuntimeHoldReason: Parameter + Member + MaxEncodedLen + Copy + VariantCount;
262
263 #[pallet::no_default_bounds]
265 type RuntimeFreezeReason: VariantCount;
266
267 type WeightInfo: WeightInfo;
269
270 type Balance: Parameter
272 + Member
273 + AtLeast32BitUnsigned
274 + Codec
275 + HasCompact<Type: DecodeWithMemTracking>
276 + Default
277 + Copy
278 + MaybeSerializeDeserialize
279 + Debug
280 + MaxEncodedLen
281 + TypeInfo
282 + FixedPointOperand;
283
284 #[pallet::no_default_bounds]
286 type DustRemoval: OnUnbalanced<CreditOf<Self, I>>;
287
288 #[pallet::constant]
297 #[pallet::no_default_bounds]
298 type ExistentialDeposit: Get<Self::Balance>;
299
300 #[pallet::no_default]
302 type AccountStore: StoredMap<Self::AccountId, AccountData<Self::Balance>>;
303
304 type ReserveIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
308
309 type FreezeIdentifier: Parameter + Member + MaxEncodedLen + Copy;
311
312 #[pallet::constant]
317 type MaxLocks: Get<u32>;
318
319 #[pallet::constant]
323 type MaxReserves: Get<u32>;
324
325 #[pallet::constant]
327 type MaxFreezes: Get<u32>;
328
329 type DoneSlashHandler: fungible::hold::DoneSlash<
332 Self::RuntimeHoldReason,
333 Self::AccountId,
334 Self::Balance,
335 >;
336 }
337
338 const STORAGE_VERSION: frame_support::traits::StorageVersion =
340 frame_support::traits::StorageVersion::new(1);
341
342 #[pallet::pallet]
343 #[pallet::storage_version(STORAGE_VERSION)]
344 pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
345
346 #[pallet::event]
347 #[pallet::generate_deposit(pub(super) fn deposit_event)]
348 pub enum Event<T: Config<I>, I: 'static = ()> {
349 Endowed { account: T::AccountId, free_balance: T::Balance },
351 DustLost { account: T::AccountId, amount: T::Balance },
354 Transfer { from: T::AccountId, to: T::AccountId, amount: T::Balance },
356 BalanceSet { who: T::AccountId, free: T::Balance },
358 Reserved { who: T::AccountId, amount: T::Balance },
360 Unreserved { who: T::AccountId, amount: T::Balance },
362 ReserveRepatriated {
365 from: T::AccountId,
366 to: T::AccountId,
367 amount: T::Balance,
368 destination_status: Status,
369 },
370 Deposit { who: T::AccountId, amount: T::Balance },
372 Withdraw { who: T::AccountId, amount: T::Balance },
374 Slashed { who: T::AccountId, amount: T::Balance },
376 Minted { who: T::AccountId, amount: T::Balance },
378 Burned { who: T::AccountId, amount: T::Balance },
380 Suspended { who: T::AccountId, amount: T::Balance },
382 Restored { who: T::AccountId, amount: T::Balance },
384 Upgraded { who: T::AccountId },
386 Issued { amount: T::Balance },
388 Rescinded { amount: T::Balance },
390 Locked { who: T::AccountId, amount: T::Balance },
392 Unlocked { who: T::AccountId, amount: T::Balance },
394 Frozen { who: T::AccountId, amount: T::Balance },
396 Thawed { who: T::AccountId, amount: T::Balance },
398 TotalIssuanceForced { old: T::Balance, new: T::Balance },
400 Unexpected(UnexpectedKind),
402 }
403
404 #[derive(Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, TypeInfo, RuntimeDebug)]
408 pub enum UnexpectedKind {
409 BalanceUpdated,
411 FailedToMutateAccount,
414 }
415
416 #[pallet::error]
417 pub enum Error<T, I = ()> {
418 VestingBalance,
420 LiquidityRestrictions,
422 InsufficientBalance,
424 ExistentialDeposit,
426 Expendability,
428 ExistingVestingSchedule,
430 DeadAccount,
432 TooManyReserves,
434 TooManyHolds,
436 TooManyFreezes,
438 IssuanceDeactivated,
440 DeltaZero,
442 }
443
444 #[pallet::storage]
446 #[pallet::whitelist_storage]
447 pub type TotalIssuance<T: Config<I>, I: 'static = ()> = StorageValue<_, T::Balance, ValueQuery>;
448
449 #[pallet::storage]
451 #[pallet::whitelist_storage]
452 pub type InactiveIssuance<T: Config<I>, I: 'static = ()> =
453 StorageValue<_, T::Balance, ValueQuery>;
454
455 #[pallet::storage]
480 pub type Account<T: Config<I>, I: 'static = ()> =
481 StorageMap<_, Blake2_128Concat, T::AccountId, AccountData<T::Balance>, ValueQuery>;
482
483 #[pallet::storage]
488 pub type Locks<T: Config<I>, I: 'static = ()> = StorageMap<
489 _,
490 Blake2_128Concat,
491 T::AccountId,
492 WeakBoundedVec<BalanceLock<T::Balance>, T::MaxLocks>,
493 ValueQuery,
494 >;
495
496 #[pallet::storage]
500 pub type Reserves<T: Config<I>, I: 'static = ()> = StorageMap<
501 _,
502 Blake2_128Concat,
503 T::AccountId,
504 BoundedVec<ReserveData<T::ReserveIdentifier, T::Balance>, T::MaxReserves>,
505 ValueQuery,
506 >;
507
508 #[pallet::storage]
510 pub type Holds<T: Config<I>, I: 'static = ()> = StorageMap<
511 _,
512 Blake2_128Concat,
513 T::AccountId,
514 BoundedVec<
515 IdAmount<T::RuntimeHoldReason, T::Balance>,
516 VariantCountOf<T::RuntimeHoldReason>,
517 >,
518 ValueQuery,
519 >;
520
521 #[pallet::storage]
523 pub type Freezes<T: Config<I>, I: 'static = ()> = StorageMap<
524 _,
525 Blake2_128Concat,
526 T::AccountId,
527 BoundedVec<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
528 ValueQuery,
529 >;
530
531 #[pallet::genesis_config]
532 pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
533 pub balances: Vec<(T::AccountId, T::Balance)>,
534 pub dev_accounts: Option<(u32, T::Balance, Option<String>)>,
541 }
542
543 impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
544 fn default() -> Self {
545 Self { balances: Default::default(), dev_accounts: None }
546 }
547 }
548
549 #[pallet::genesis_build]
550 impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
551 fn build(&self) {
552 let total = self.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n);
553
554 <TotalIssuance<T, I>>::put(total);
555
556 for (_, balance) in &self.balances {
557 assert!(
558 *balance >= <T as Config<I>>::ExistentialDeposit::get(),
559 "the balance of any account should always be at least the existential deposit.",
560 )
561 }
562
563 let endowed_accounts = self
565 .balances
566 .iter()
567 .map(|(x, _)| x)
568 .cloned()
569 .collect::<alloc::collections::btree_set::BTreeSet<_>>();
570
571 assert!(
572 endowed_accounts.len() == self.balances.len(),
573 "duplicate balances in genesis."
574 );
575
576 if let Some((num_accounts, balance, ref derivation)) = self.dev_accounts {
578 Pallet::<T, I>::derive_dev_account(
580 num_accounts,
581 balance,
582 derivation.as_deref().unwrap_or(DEFAULT_ADDRESS_URI),
583 );
584 }
585 for &(ref who, free) in self.balances.iter() {
586 frame_system::Pallet::<T>::inc_providers(who);
587 assert!(T::AccountStore::insert(who, AccountData { free, ..Default::default() })
588 .is_ok());
589 }
590 }
591 }
592
593 #[pallet::hooks]
594 impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
595 fn integrity_test() {
596 #[cfg(not(feature = "insecure_zero_ed"))]
597 assert!(
598 !<T as Config<I>>::ExistentialDeposit::get().is_zero(),
599 "The existential deposit must be greater than zero!"
600 );
601
602 assert!(
603 T::MaxFreezes::get() >= <T::RuntimeFreezeReason as VariantCount>::VARIANT_COUNT,
604 "MaxFreezes should be greater than or equal to the number of freeze reasons: {} < {}",
605 T::MaxFreezes::get(), <T::RuntimeFreezeReason as VariantCount>::VARIANT_COUNT,
606 );
607 }
608
609 #[cfg(feature = "try-runtime")]
610 fn try_state(n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
611 Self::do_try_state(n)
612 }
613 }
614
615 #[pallet::call(weight(<T as Config<I>>::WeightInfo))]
616 impl<T: Config<I>, I: 'static> Pallet<T, I> {
617 #[pallet::call_index(0)]
625 pub fn transfer_allow_death(
626 origin: OriginFor<T>,
627 dest: AccountIdLookupOf<T>,
628 #[pallet::compact] value: T::Balance,
629 ) -> DispatchResult {
630 let source = ensure_signed(origin)?;
631 let dest = T::Lookup::lookup(dest)?;
632 <Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Expendable)?;
633 Ok(())
634 }
635
636 #[pallet::call_index(2)]
639 pub fn force_transfer(
640 origin: OriginFor<T>,
641 source: AccountIdLookupOf<T>,
642 dest: AccountIdLookupOf<T>,
643 #[pallet::compact] value: T::Balance,
644 ) -> DispatchResult {
645 ensure_root(origin)?;
646 let source = T::Lookup::lookup(source)?;
647 let dest = T::Lookup::lookup(dest)?;
648 <Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Expendable)?;
649 Ok(())
650 }
651
652 #[pallet::call_index(3)]
659 pub fn transfer_keep_alive(
660 origin: OriginFor<T>,
661 dest: AccountIdLookupOf<T>,
662 #[pallet::compact] value: T::Balance,
663 ) -> DispatchResult {
664 let source = ensure_signed(origin)?;
665 let dest = T::Lookup::lookup(dest)?;
666 <Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Preserve)?;
667 Ok(())
668 }
669
670 #[pallet::call_index(4)]
686 pub fn transfer_all(
687 origin: OriginFor<T>,
688 dest: AccountIdLookupOf<T>,
689 keep_alive: bool,
690 ) -> DispatchResult {
691 let transactor = ensure_signed(origin)?;
692 let keep_alive = if keep_alive { Preserve } else { Expendable };
693 let reducible_balance = <Self as fungible::Inspect<_>>::reducible_balance(
694 &transactor,
695 keep_alive,
696 Fortitude::Polite,
697 );
698 let dest = T::Lookup::lookup(dest)?;
699 <Self as fungible::Mutate<_>>::transfer(
700 &transactor,
701 &dest,
702 reducible_balance,
703 keep_alive,
704 )?;
705 Ok(())
706 }
707
708 #[pallet::call_index(5)]
712 pub fn force_unreserve(
713 origin: OriginFor<T>,
714 who: AccountIdLookupOf<T>,
715 amount: T::Balance,
716 ) -> DispatchResult {
717 ensure_root(origin)?;
718 let who = T::Lookup::lookup(who)?;
719 let _leftover = <Self as ReservableCurrency<_>>::unreserve(&who, amount);
720 Ok(())
721 }
722
723 #[pallet::call_index(6)]
732 #[pallet::weight(T::WeightInfo::upgrade_accounts(who.len() as u32))]
733 pub fn upgrade_accounts(
734 origin: OriginFor<T>,
735 who: Vec<T::AccountId>,
736 ) -> DispatchResultWithPostInfo {
737 ensure_signed(origin)?;
738 if who.is_empty() {
739 return Ok(Pays::Yes.into())
740 }
741 let mut upgrade_count = 0;
742 for i in &who {
743 let upgraded = Self::ensure_upgraded(i);
744 if upgraded {
745 upgrade_count.saturating_inc();
746 }
747 }
748 let proportion_upgraded = Perbill::from_rational(upgrade_count, who.len() as u32);
749 if proportion_upgraded >= Perbill::from_percent(90) {
750 Ok(Pays::No.into())
751 } else {
752 Ok(Pays::Yes.into())
753 }
754 }
755
756 #[pallet::call_index(8)]
760 #[pallet::weight(
761 T::WeightInfo::force_set_balance_creating() .max(T::WeightInfo::force_set_balance_killing()) )]
764 pub fn force_set_balance(
765 origin: OriginFor<T>,
766 who: AccountIdLookupOf<T>,
767 #[pallet::compact] new_free: T::Balance,
768 ) -> DispatchResult {
769 ensure_root(origin)?;
770 let who = T::Lookup::lookup(who)?;
771 let existential_deposit = Self::ed();
772
773 let wipeout = new_free < existential_deposit;
774 let new_free = if wipeout { Zero::zero() } else { new_free };
775
776 let old_free = Self::mutate_account_handling_dust(&who, false, |account| {
778 let old_free = account.free;
779 account.free = new_free;
780 old_free
781 })?;
782
783 if new_free > old_free {
786 mem::drop(PositiveImbalance::<T, I>::new(new_free - old_free));
787 } else if new_free < old_free {
788 mem::drop(NegativeImbalance::<T, I>::new(old_free - new_free));
789 }
790
791 Self::deposit_event(Event::BalanceSet { who, free: new_free });
792 Ok(())
793 }
794
795 #[doc = docify::embed!("./src/tests/dispatchable_tests.rs", force_adjust_total_issuance_example)]
801 #[pallet::call_index(9)]
802 #[pallet::weight(T::WeightInfo::force_adjust_total_issuance())]
803 pub fn force_adjust_total_issuance(
804 origin: OriginFor<T>,
805 direction: AdjustmentDirection,
806 #[pallet::compact] delta: T::Balance,
807 ) -> DispatchResult {
808 ensure_root(origin)?;
809
810 ensure!(delta > Zero::zero(), Error::<T, I>::DeltaZero);
811
812 let old = TotalIssuance::<T, I>::get();
813 let new = match direction {
814 AdjustmentDirection::Increase => old.saturating_add(delta),
815 AdjustmentDirection::Decrease => old.saturating_sub(delta),
816 };
817
818 ensure!(InactiveIssuance::<T, I>::get() <= new, Error::<T, I>::IssuanceDeactivated);
819 TotalIssuance::<T, I>::set(new);
820
821 Self::deposit_event(Event::<T, I>::TotalIssuanceForced { old, new });
822
823 Ok(())
824 }
825
826 #[pallet::call_index(10)]
834 #[pallet::weight(if *keep_alive {T::WeightInfo::burn_allow_death() } else {T::WeightInfo::burn_keep_alive()})]
835 pub fn burn(
836 origin: OriginFor<T>,
837 #[pallet::compact] value: T::Balance,
838 keep_alive: bool,
839 ) -> DispatchResult {
840 let source = ensure_signed(origin)?;
841 let preservation = if keep_alive { Preserve } else { Expendable };
842 <Self as fungible::Mutate<_>>::burn_from(
843 &source,
844 value,
845 preservation,
846 Precision::Exact,
847 Polite,
848 )?;
849 Ok(())
850 }
851 }
852
853 impl<T: Config<I>, I: 'static> Pallet<T, I> {
854 pub fn total_issuance() -> T::Balance {
856 TotalIssuance::<T, I>::get()
857 }
858
859 pub fn inactive_issuance() -> T::Balance {
861 InactiveIssuance::<T, I>::get()
862 }
863
864 pub fn locks(who: &T::AccountId) -> WeakBoundedVec<BalanceLock<T::Balance>, T::MaxLocks> {
866 Locks::<T, I>::get(who)
867 }
868
869 pub fn reserves(
871 who: &T::AccountId,
872 ) -> BoundedVec<ReserveData<T::ReserveIdentifier, T::Balance>, T::MaxReserves> {
873 Reserves::<T, I>::get(who)
874 }
875
876 fn ed() -> T::Balance {
877 T::ExistentialDeposit::get()
878 }
879 pub fn ensure_upgraded(who: &T::AccountId) -> bool {
883 let mut a = T::AccountStore::get(who);
884 if a.flags.is_new_logic() {
885 return false
886 }
887 a.flags.set_new_logic();
888 if !a.reserved.is_zero() && a.frozen.is_zero() {
889 if system::Pallet::<T>::providers(who) == 0 {
890 log::warn!(
894 target: LOG_TARGET,
895 "account with a non-zero reserve balance has no provider refs, account_id: '{:?}'.",
896 who
897 );
898 a.free = a.free.max(Self::ed());
899 system::Pallet::<T>::inc_providers(who);
900 }
901 let _ = system::Pallet::<T>::inc_consumers_without_limit(who).defensive();
902 }
903 let _ = T::AccountStore::try_mutate_exists(who, |account| -> DispatchResult {
905 *account = Some(a);
906 Ok(())
907 });
908 Self::deposit_event(Event::Upgraded { who: who.clone() });
909 return true
910 }
911
912 pub fn free_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
914 Self::account(who.borrow()).free
915 }
916
917 pub fn usable_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
920 <Self as fungible::Inspect<_>>::reducible_balance(who.borrow(), Expendable, Polite)
921 }
922
923 pub fn usable_balance_for_fees(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
928 <Self as fungible::Inspect<_>>::reducible_balance(who.borrow(), Protect, Polite)
929 }
930
931 pub fn reserved_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
933 Self::account(who.borrow()).reserved
934 }
935
936 pub(crate) fn account(who: &T::AccountId) -> AccountData<T::Balance> {
938 T::AccountStore::get(who)
939 }
940
941 pub(crate) fn mutate_account_handling_dust<R>(
953 who: &T::AccountId,
954 force_consumer_bump: bool,
955 f: impl FnOnce(&mut AccountData<T::Balance>) -> R,
956 ) -> Result<R, DispatchError> {
957 let (r, maybe_dust) = Self::mutate_account(who, force_consumer_bump, f)?;
958 if let Some(dust) = maybe_dust {
959 <Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
960 }
961 Ok(r)
962 }
963
964 pub(crate) fn try_mutate_account_handling_dust<R, E: From<DispatchError>>(
976 who: &T::AccountId,
977 force_consumer_bump: bool,
978 f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
979 ) -> Result<R, E> {
980 let (r, maybe_dust) = Self::try_mutate_account(who, force_consumer_bump, f)?;
981 if let Some(dust) = maybe_dust {
982 <Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
983 }
984 Ok(r)
985 }
986
987 pub(crate) fn mutate_account<R>(
1004 who: &T::AccountId,
1005 force_consumer_bump: bool,
1006 f: impl FnOnce(&mut AccountData<T::Balance>) -> R,
1007 ) -> Result<(R, Option<T::Balance>), DispatchError> {
1008 Self::try_mutate_account(who, force_consumer_bump, |a, _| -> Result<R, DispatchError> {
1009 Ok(f(a))
1010 })
1011 }
1012
1013 #[cfg(not(feature = "insecure_zero_ed"))]
1016 fn have_providers_or_no_zero_ed(_: &T::AccountId) -> bool {
1017 true
1018 }
1019
1020 #[cfg(feature = "insecure_zero_ed")]
1023 fn have_providers_or_no_zero_ed(who: &T::AccountId) -> bool {
1024 frame_system::Pallet::<T>::providers(who) > 0
1025 }
1026
1027 pub(crate) fn try_mutate_account<R, E: From<DispatchError>>(
1041 who: &T::AccountId,
1042 force_consumer_bump: bool,
1043 f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
1044 ) -> Result<(R, Option<T::Balance>), E> {
1045 Self::ensure_upgraded(who);
1046 let result = T::AccountStore::try_mutate_exists(who, |maybe_account| {
1047 let is_new = maybe_account.is_none();
1048 let mut account = maybe_account.take().unwrap_or_default();
1049 let did_provide =
1050 account.free >= Self::ed() && Self::have_providers_or_no_zero_ed(who);
1051 let did_consume =
1052 !is_new && (!account.reserved.is_zero() || !account.frozen.is_zero());
1053
1054 let result = f(&mut account, is_new)?;
1055
1056 let does_provide = account.free >= Self::ed();
1057 let does_consume = !account.reserved.is_zero() || !account.frozen.is_zero();
1058
1059 if !did_provide && does_provide {
1060 frame_system::Pallet::<T>::inc_providers(who);
1061 }
1062 if did_consume && !does_consume {
1063 frame_system::Pallet::<T>::dec_consumers(who);
1064 }
1065 if !did_consume && does_consume {
1066 if force_consumer_bump {
1067 frame_system::Pallet::<T>::inc_consumers_without_limit(who)?;
1069 } else {
1070 frame_system::Pallet::<T>::inc_consumers(who)?;
1071 }
1072 }
1073 if does_consume && frame_system::Pallet::<T>::consumers(who) == 0 {
1074 log::error!(target: LOG_TARGET, "Defensively bumping a consumer ref.");
1078 frame_system::Pallet::<T>::inc_consumers(who)?;
1079 }
1080 if did_provide && !does_provide {
1081 frame_system::Pallet::<T>::dec_providers(who).inspect_err(|_| {
1083 if did_consume && !does_consume {
1085 let _ = frame_system::Pallet::<T>::inc_consumers(who).defensive();
1086 }
1087 if !did_consume && does_consume {
1088 let _ = frame_system::Pallet::<T>::dec_consumers(who);
1089 }
1090 })?;
1091 }
1092
1093 let maybe_endowed = if is_new { Some(account.free) } else { None };
1094
1095 let ed = Self::ed();
1107 let maybe_dust = if account.free < ed && account.reserved.is_zero() {
1108 if account.free.is_zero() {
1109 None
1110 } else {
1111 Some(account.free)
1112 }
1113 } else {
1114 assert!(
1115 account.free.is_zero() || account.free >= ed || !account.reserved.is_zero()
1116 );
1117 *maybe_account = Some(account);
1118 None
1119 };
1120 Ok((maybe_endowed, maybe_dust, result))
1121 });
1122 result.map(|(maybe_endowed, maybe_dust, result)| {
1123 if let Some(endowed) = maybe_endowed {
1124 Self::deposit_event(Event::Endowed {
1125 account: who.clone(),
1126 free_balance: endowed,
1127 });
1128 }
1129 if let Some(amount) = maybe_dust {
1130 Pallet::<T, I>::deposit_event(Event::DustLost { account: who.clone(), amount });
1131 }
1132 (result, maybe_dust)
1133 })
1134 }
1135
1136 pub(crate) fn update_locks(who: &T::AccountId, locks: &[BalanceLock<T::Balance>]) {
1138 let bounded_locks = WeakBoundedVec::<_, T::MaxLocks>::force_from(
1139 locks.to_vec(),
1140 Some("Balances Update Locks"),
1141 );
1142
1143 if locks.len() as u32 > T::MaxLocks::get() {
1144 log::warn!(
1145 target: LOG_TARGET,
1146 "Warning: A user has more currency locks than expected. \
1147 A runtime configuration adjustment may be needed."
1148 );
1149 }
1150 let freezes = Freezes::<T, I>::get(who);
1151 let mut prev_frozen = Zero::zero();
1152 let mut after_frozen = Zero::zero();
1153 let res = Self::mutate_account(who, true, |b| {
1156 prev_frozen = b.frozen;
1157 b.frozen = Zero::zero();
1158 for l in locks.iter() {
1159 b.frozen = b.frozen.max(l.amount);
1160 }
1161 for l in freezes.iter() {
1162 b.frozen = b.frozen.max(l.amount);
1163 }
1164 after_frozen = b.frozen;
1165 });
1166 match res {
1167 Ok((_, None)) => {
1168 },
1170 Ok((_, Some(_dust))) => {
1171 Self::deposit_event(Event::Unexpected(UnexpectedKind::BalanceUpdated));
1172 defensive!("caused unexpected dusting/balance update.");
1173 },
1174 _ => {
1175 Self::deposit_event(Event::Unexpected(UnexpectedKind::FailedToMutateAccount));
1176 defensive!("errored in mutate_account");
1177 },
1178 }
1179
1180 match locks.is_empty() {
1181 true => Locks::<T, I>::remove(who),
1182 false => Locks::<T, I>::insert(who, bounded_locks),
1183 }
1184
1185 if prev_frozen > after_frozen {
1186 let amount = prev_frozen.saturating_sub(after_frozen);
1187 Self::deposit_event(Event::Unlocked { who: who.clone(), amount });
1188 } else if after_frozen > prev_frozen {
1189 let amount = after_frozen.saturating_sub(prev_frozen);
1190 Self::deposit_event(Event::Locked { who: who.clone(), amount });
1191 }
1192 }
1193
1194 pub(crate) fn update_freezes(
1196 who: &T::AccountId,
1197 freezes: BoundedSlice<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
1198 ) -> DispatchResult {
1199 let mut prev_frozen = Zero::zero();
1200 let mut after_frozen = Zero::zero();
1201 let (_, maybe_dust) = Self::mutate_account(who, false, |b| {
1202 prev_frozen = b.frozen;
1203 b.frozen = Zero::zero();
1204 for l in Locks::<T, I>::get(who).iter() {
1205 b.frozen = b.frozen.max(l.amount);
1206 }
1207 for l in freezes.iter() {
1208 b.frozen = b.frozen.max(l.amount);
1209 }
1210 after_frozen = b.frozen;
1211 })?;
1212 if maybe_dust.is_some() {
1213 Self::deposit_event(Event::Unexpected(UnexpectedKind::BalanceUpdated));
1214 defensive!("caused unexpected dusting/balance update.");
1215 }
1216 if freezes.is_empty() {
1217 Freezes::<T, I>::remove(who);
1218 } else {
1219 Freezes::<T, I>::insert(who, freezes);
1220 }
1221 if prev_frozen > after_frozen {
1222 let amount = prev_frozen.saturating_sub(after_frozen);
1223 Self::deposit_event(Event::Thawed { who: who.clone(), amount });
1224 } else if after_frozen > prev_frozen {
1225 let amount = after_frozen.saturating_sub(prev_frozen);
1226 Self::deposit_event(Event::Frozen { who: who.clone(), amount });
1227 }
1228 Ok(())
1229 }
1230
1231 pub(crate) fn do_transfer_reserved(
1238 slashed: &T::AccountId,
1239 beneficiary: &T::AccountId,
1240 value: T::Balance,
1241 precision: Precision,
1242 fortitude: Fortitude,
1243 status: Status,
1244 ) -> Result<T::Balance, DispatchError> {
1245 if value.is_zero() {
1246 return Ok(Zero::zero())
1247 }
1248
1249 let max = <Self as fungible::InspectHold<_>>::reducible_total_balance_on_hold(
1250 slashed, fortitude,
1251 );
1252 let actual = match precision {
1253 Precision::BestEffort => value.min(max),
1254 Precision::Exact => value,
1255 };
1256 ensure!(actual <= max, TokenError::FundsUnavailable);
1257 if slashed == beneficiary {
1258 return match status {
1259 Status::Free => Ok(actual.saturating_sub(Self::unreserve(slashed, actual))),
1260 Status::Reserved => Ok(actual),
1261 }
1262 }
1263
1264 let ((_, maybe_dust_1), maybe_dust_2) = Self::try_mutate_account(
1265 beneficiary,
1266 false,
1267 |to_account, is_new| -> Result<((), Option<T::Balance>), DispatchError> {
1268 ensure!(!is_new, Error::<T, I>::DeadAccount);
1269 Self::try_mutate_account(slashed, false, |from_account, _| -> DispatchResult {
1270 match status {
1271 Status::Free =>
1272 to_account.free = to_account
1273 .free
1274 .checked_add(&actual)
1275 .ok_or(ArithmeticError::Overflow)?,
1276 Status::Reserved =>
1277 to_account.reserved = to_account
1278 .reserved
1279 .checked_add(&actual)
1280 .ok_or(ArithmeticError::Overflow)?,
1281 }
1282 from_account.reserved.saturating_reduce(actual);
1283 Ok(())
1284 })
1285 },
1286 )?;
1287
1288 if let Some(dust) = maybe_dust_1 {
1289 <Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
1290 }
1291 if let Some(dust) = maybe_dust_2 {
1292 <Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
1293 }
1294
1295 Self::deposit_event(Event::ReserveRepatriated {
1296 from: slashed.clone(),
1297 to: beneficiary.clone(),
1298 amount: actual,
1299 destination_status: status,
1300 });
1301 Ok(actual)
1302 }
1303
1304 pub fn derive_dev_account(num_accounts: u32, balance: T::Balance, derivation: &str) {
1306 assert!(num_accounts > 0, "num_accounts must be greater than zero");
1308
1309 assert!(
1310 balance >= <T as Config<I>>::ExistentialDeposit::get(),
1311 "the balance of any account should always be at least the existential deposit.",
1312 );
1313
1314 assert!(
1315 derivation.contains("{}"),
1316 "Invalid derivation, expected `{{}}` as part of the derivation"
1317 );
1318
1319 for index in 0..num_accounts {
1320 let derivation_string = derivation.replace("{}", &index.to_string());
1322
1323 let pair: SrPair = Pair::from_string(&derivation_string, None)
1325 .expect(&format!("Failed to parse derivation string: {derivation_string}"));
1326
1327 let who = T::AccountId::decode(&mut &pair.public().encode()[..])
1329 .expect(&format!("Failed to decode public key from pair: {:?}", pair.public()));
1330
1331 Self::mutate_account_handling_dust(&who, false, |account| {
1333 account.free = balance;
1334 })
1335 .expect(&format!("Failed to add account to keystore: {:?}", who));
1336 }
1337 }
1338 }
1339
1340 #[cfg(any(test, feature = "try-runtime"))]
1341 impl<T: Config<I>, I: 'static> Pallet<T, I> {
1342 pub(crate) fn do_try_state(
1343 _n: BlockNumberFor<T>,
1344 ) -> Result<(), sp_runtime::TryRuntimeError> {
1345 Self::hold_and_freeze_count()?;
1346 Self::account_frozen_greater_than_locks()?;
1347 Self::account_frozen_greater_than_freezes()?;
1348 Ok(())
1349 }
1350
1351 fn hold_and_freeze_count() -> Result<(), sp_runtime::TryRuntimeError> {
1352 Holds::<T, I>::iter_keys().try_for_each(|k| {
1353 if Holds::<T, I>::decode_len(k).unwrap_or(0) >
1354 T::RuntimeHoldReason::VARIANT_COUNT as usize
1355 {
1356 Err("Found `Hold` with too many elements")
1357 } else {
1358 Ok(())
1359 }
1360 })?;
1361
1362 Freezes::<T, I>::iter_keys().try_for_each(|k| {
1363 if Freezes::<T, I>::decode_len(k).unwrap_or(0) > T::MaxFreezes::get() as usize {
1364 Err("Found `Freeze` with too many elements")
1365 } else {
1366 Ok(())
1367 }
1368 })?;
1369
1370 Ok(())
1371 }
1372
1373 fn account_frozen_greater_than_locks() -> Result<(), sp_runtime::TryRuntimeError> {
1374 Locks::<T, I>::iter().try_for_each(|(who, locks)| {
1375 let max_locks = locks.iter().map(|l| l.amount).max().unwrap_or_default();
1376 let frozen = T::AccountStore::get(&who).frozen;
1377 if max_locks > frozen {
1378 log::warn!(
1379 target: crate::LOG_TARGET,
1380 "Maximum lock of {:?} ({:?}) is greater than the frozen balance {:?}",
1381 who,
1382 max_locks,
1383 frozen
1384 );
1385 Err("bad locks".into())
1386 } else {
1387 Ok(())
1388 }
1389 })
1390 }
1391
1392 fn account_frozen_greater_than_freezes() -> Result<(), sp_runtime::TryRuntimeError> {
1393 Freezes::<T, I>::iter().try_for_each(|(who, freezes)| {
1394 let max_locks = freezes.iter().map(|l| l.amount).max().unwrap_or_default();
1395 let frozen = T::AccountStore::get(&who).frozen;
1396 if max_locks > frozen {
1397 log::warn!(
1398 target: crate::LOG_TARGET,
1399 "Maximum freeze of {:?} ({:?}) is greater than the frozen balance {:?}",
1400 who,
1401 max_locks,
1402 frozen
1403 );
1404 Err("bad freezes".into())
1405 } else {
1406 Ok(())
1407 }
1408 })
1409 }
1410 }
1411}