#![cfg_attr(not(feature = "std"), no_std)]
mod benchmarking;
mod impl_currency;
mod impl_fungible;
pub mod migration;
mod tests;
mod types;
pub mod weights;
extern crate alloc;
use alloc::{
format,
string::{String, ToString},
vec::Vec,
};
use codec::{Codec, MaxEncodedLen};
use core::{cmp, fmt::Debug, mem, result};
pub use impl_currency::{NegativeImbalance, PositiveImbalance};
use pezframe_support::{
ensure,
pezpallet_prelude::DispatchResult,
traits::{
tokens::{
fungible, BalanceStatus as Status, DepositConsequence,
Fortitude::{self, Force, Polite},
IdAmount,
Preservation::{Expendable, Preserve, Protect},
WithdrawConsequence,
},
Currency, Defensive, Get, OnUnbalanced, ReservableCurrency, StoredMap,
},
BoundedSlice, WeakBoundedVec,
};
use pezframe_system as system;
use pezsp_core::{sr25519::Pair as SrPair, Pair};
use pezsp_runtime::{
traits::{
AtLeast32BitUnsigned, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Saturating,
StaticLookup, Zero,
},
ArithmeticError, DispatchError, FixedPointOperand, Perbill, RuntimeDebug, TokenError,
};
use scale_info::TypeInfo;
pub use types::{
AccountData, AdjustmentDirection, BalanceLock, DustCleaner, ExtraFlags, Reasons, ReserveData,
};
pub use weights::WeightInfo;
pub use pezpallet::*;
const LOG_TARGET: &str = "runtime::balances";
const DEFAULT_ADDRESS_URI: &str = "//Sender//{}";
type AccountIdLookupOf<T> = <<T as pezframe_system::Config>::Lookup as StaticLookup>::Source;
#[pezframe_support::pezpallet]
pub mod pezpallet {
use super::*;
use codec::HasCompact;
use pezframe_support::{
pezpallet_prelude::*,
traits::{fungible::Credit, tokens::Precision, VariantCount, VariantCountOf},
};
use pezframe_system::pezpallet_prelude::*;
pub type CreditOf<T, I> = Credit<<T as pezframe_system::Config>::AccountId, Pezpallet<T, I>>;
pub mod config_preludes {
use super::*;
use pezframe_support::derive_impl;
pub struct TestDefaultConfig;
#[derive_impl(pezframe_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
impl pezframe_system::DefaultConfig for TestDefaultConfig {}
#[pezframe_support::register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
#[inject_runtime_type]
type RuntimeEvent = ();
#[inject_runtime_type]
type RuntimeHoldReason = ();
#[inject_runtime_type]
type RuntimeFreezeReason = ();
type Balance = u64;
type ExistentialDeposit = ConstUint<1>;
type ReserveIdentifier = ();
type FreezeIdentifier = Self::RuntimeFreezeReason;
type DustRemoval = ();
type MaxLocks = ConstU32<100>;
type MaxReserves = ConstU32<100>;
type MaxFreezes = VariantCountOf<Self::RuntimeFreezeReason>;
type WeightInfo = ();
type DoneSlashHandler = ();
}
}
#[pezpallet::config(with_default)]
pub trait Config<I: 'static = ()>: pezframe_system::Config {
#[pezpallet::no_default_bounds]
#[allow(deprecated)]
type RuntimeEvent: From<Event<Self, I>>
+ IsType<<Self as pezframe_system::Config>::RuntimeEvent>;
#[pezpallet::no_default_bounds]
type RuntimeHoldReason: Parameter + Member + MaxEncodedLen + Copy + VariantCount;
#[pezpallet::no_default_bounds]
type RuntimeFreezeReason: VariantCount;
type WeightInfo: WeightInfo;
type Balance: Parameter
+ Member
+ AtLeast32BitUnsigned
+ Codec
+ HasCompact<Type: DecodeWithMemTracking>
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Debug
+ MaxEncodedLen
+ TypeInfo
+ FixedPointOperand;
#[pezpallet::no_default_bounds]
type DustRemoval: OnUnbalanced<CreditOf<Self, I>>;
#[pezpallet::constant]
#[pezpallet::no_default_bounds]
type ExistentialDeposit: Get<Self::Balance>;
#[pezpallet::no_default]
type AccountStore: StoredMap<Self::AccountId, AccountData<Self::Balance>>;
type ReserveIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
type FreezeIdentifier: Parameter + Member + MaxEncodedLen + Copy;
#[pezpallet::constant]
type MaxLocks: Get<u32>;
#[pezpallet::constant]
type MaxReserves: Get<u32>;
#[pezpallet::constant]
type MaxFreezes: Get<u32>;
type DoneSlashHandler: fungible::hold::DoneSlash<
Self::RuntimeHoldReason,
Self::AccountId,
Self::Balance,
>;
}
const STORAGE_VERSION: pezframe_support::traits::StorageVersion =
pezframe_support::traits::StorageVersion::new(1);
#[pezpallet::pezpallet]
#[pezpallet::storage_version(STORAGE_VERSION)]
pub struct Pezpallet<T, I = ()>(PhantomData<(T, I)>);
#[pezpallet::event]
#[pezpallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config<I>, I: 'static = ()> {
Endowed { account: T::AccountId, free_balance: T::Balance },
DustLost { account: T::AccountId, amount: T::Balance },
Transfer { from: T::AccountId, to: T::AccountId, amount: T::Balance },
BalanceSet { who: T::AccountId, free: T::Balance },
Reserved { who: T::AccountId, amount: T::Balance },
Unreserved { who: T::AccountId, amount: T::Balance },
ReserveRepatriated {
from: T::AccountId,
to: T::AccountId,
amount: T::Balance,
destination_status: Status,
},
Deposit { who: T::AccountId, amount: T::Balance },
Withdraw { who: T::AccountId, amount: T::Balance },
Slashed { who: T::AccountId, amount: T::Balance },
Minted { who: T::AccountId, amount: T::Balance },
MintedCredit { amount: T::Balance },
Burned { who: T::AccountId, amount: T::Balance },
BurnedDebt { amount: T::Balance },
Suspended { who: T::AccountId, amount: T::Balance },
Restored { who: T::AccountId, amount: T::Balance },
Upgraded { who: T::AccountId },
Issued { amount: T::Balance },
Rescinded { amount: T::Balance },
Locked { who: T::AccountId, amount: T::Balance },
Unlocked { who: T::AccountId, amount: T::Balance },
Frozen { who: T::AccountId, amount: T::Balance },
Thawed { who: T::AccountId, amount: T::Balance },
TotalIssuanceForced { old: T::Balance, new: T::Balance },
Held { reason: T::RuntimeHoldReason, who: T::AccountId, amount: T::Balance },
BurnedHeld { reason: T::RuntimeHoldReason, who: T::AccountId, amount: T::Balance },
TransferOnHold {
reason: T::RuntimeHoldReason,
source: T::AccountId,
dest: T::AccountId,
amount: T::Balance,
},
TransferAndHold {
reason: T::RuntimeHoldReason,
source: T::AccountId,
dest: T::AccountId,
transferred: T::Balance,
},
Released { reason: T::RuntimeHoldReason, who: T::AccountId, amount: T::Balance },
Unexpected(UnexpectedKind),
}
#[derive(Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, TypeInfo, RuntimeDebug)]
pub enum UnexpectedKind {
BalanceUpdated,
FailedToMutateAccount,
}
#[pezpallet::error]
pub enum Error<T, I = ()> {
VestingBalance,
LiquidityRestrictions,
InsufficientBalance,
ExistentialDeposit,
Expendability,
ExistingVestingSchedule,
DeadAccount,
TooManyReserves,
TooManyHolds,
TooManyFreezes,
IssuanceDeactivated,
DeltaZero,
}
#[pezpallet::storage]
#[pezpallet::whitelist_storage]
pub type TotalIssuance<T: Config<I>, I: 'static = ()> = StorageValue<_, T::Balance, ValueQuery>;
#[pezpallet::storage]
#[pezpallet::whitelist_storage]
pub type InactiveIssuance<T: Config<I>, I: 'static = ()> =
StorageValue<_, T::Balance, ValueQuery>;
#[pezpallet::storage]
pub type Account<T: Config<I>, I: 'static = ()> =
StorageMap<_, Blake2_128Concat, T::AccountId, AccountData<T::Balance>, ValueQuery>;
#[pezpallet::storage]
pub type Locks<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Blake2_128Concat,
T::AccountId,
WeakBoundedVec<BalanceLock<T::Balance>, T::MaxLocks>,
ValueQuery,
>;
#[pezpallet::storage]
pub type Reserves<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Blake2_128Concat,
T::AccountId,
BoundedVec<ReserveData<T::ReserveIdentifier, T::Balance>, T::MaxReserves>,
ValueQuery,
>;
#[pezpallet::storage]
pub type Holds<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Blake2_128Concat,
T::AccountId,
BoundedVec<
IdAmount<T::RuntimeHoldReason, T::Balance>,
VariantCountOf<T::RuntimeHoldReason>,
>,
ValueQuery,
>;
#[pezpallet::storage]
pub type Freezes<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Blake2_128Concat,
T::AccountId,
BoundedVec<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
ValueQuery,
>;
#[pezpallet::genesis_config]
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
pub balances: Vec<(T::AccountId, T::Balance)>,
pub dev_accounts: Option<(u32, T::Balance, Option<String>)>,
}
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
fn default() -> Self {
Self { balances: Default::default(), dev_accounts: None }
}
}
#[pezpallet::genesis_build]
impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
fn build(&self) {
let total = self.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n);
<TotalIssuance<T, I>>::put(total);
for (_, balance) in &self.balances {
assert!(
*balance >= <T as Config<I>>::ExistentialDeposit::get(),
"the balance of any account should always be at least the existential deposit.",
)
}
let endowed_accounts = self
.balances
.iter()
.map(|(x, _)| x)
.cloned()
.collect::<alloc::collections::btree_set::BTreeSet<_>>();
assert!(
endowed_accounts.len() == self.balances.len(),
"duplicate balances in genesis."
);
if let Some((num_accounts, balance, ref derivation)) = self.dev_accounts {
Pezpallet::<T, I>::derive_dev_account(
num_accounts,
balance,
derivation.as_deref().unwrap_or(DEFAULT_ADDRESS_URI),
);
}
for &(ref who, free) in self.balances.iter() {
pezframe_system::Pezpallet::<T>::inc_providers(who);
assert!(T::AccountStore::insert(who, AccountData { free, ..Default::default() })
.is_ok());
}
}
}
#[pezpallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pezpallet<T, I> {
fn integrity_test() {
#[cfg(not(feature = "insecure_zero_ed"))]
assert!(
!<T as Config<I>>::ExistentialDeposit::get().is_zero(),
"The existential deposit must be greater than zero!"
);
assert!(
T::MaxFreezes::get() >= <T::RuntimeFreezeReason as VariantCount>::VARIANT_COUNT,
"MaxFreezes should be greater than or equal to the number of freeze reasons: {} < {}",
T::MaxFreezes::get(), <T::RuntimeFreezeReason as VariantCount>::VARIANT_COUNT,
);
}
#[cfg(feature = "try-runtime")]
fn try_state(n: BlockNumberFor<T>) -> Result<(), pezsp_runtime::TryRuntimeError> {
Self::do_try_state(n)
}
}
#[pezpallet::call(weight(<T as Config<I>>::WeightInfo))]
impl<T: Config<I>, I: 'static> Pezpallet<T, I> {
#[pezpallet::call_index(0)]
pub fn transfer_allow_death(
origin: OriginFor<T>,
dest: AccountIdLookupOf<T>,
#[pezpallet::compact] value: T::Balance,
) -> DispatchResult {
let source = ensure_signed(origin)?;
let dest = T::Lookup::lookup(dest)?;
<Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Expendable)?;
Ok(())
}
#[pezpallet::call_index(2)]
pub fn force_transfer(
origin: OriginFor<T>,
source: AccountIdLookupOf<T>,
dest: AccountIdLookupOf<T>,
#[pezpallet::compact] value: T::Balance,
) -> DispatchResult {
ensure_root(origin)?;
let source = T::Lookup::lookup(source)?;
let dest = T::Lookup::lookup(dest)?;
<Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Expendable)?;
Ok(())
}
#[pezpallet::call_index(3)]
pub fn transfer_keep_alive(
origin: OriginFor<T>,
dest: AccountIdLookupOf<T>,
#[pezpallet::compact] value: T::Balance,
) -> DispatchResult {
let source = ensure_signed(origin)?;
let dest = T::Lookup::lookup(dest)?;
<Self as fungible::Mutate<_>>::transfer(&source, &dest, value, Preserve)?;
Ok(())
}
#[pezpallet::call_index(4)]
pub fn transfer_all(
origin: OriginFor<T>,
dest: AccountIdLookupOf<T>,
keep_alive: bool,
) -> DispatchResult {
let transactor = ensure_signed(origin)?;
let keep_alive = if keep_alive { Preserve } else { Expendable };
let reducible_balance = <Self as fungible::Inspect<_>>::reducible_balance(
&transactor,
keep_alive,
Fortitude::Polite,
);
let dest = T::Lookup::lookup(dest)?;
<Self as fungible::Mutate<_>>::transfer(
&transactor,
&dest,
reducible_balance,
keep_alive,
)?;
Ok(())
}
#[pezpallet::call_index(5)]
pub fn force_unreserve(
origin: OriginFor<T>,
who: AccountIdLookupOf<T>,
amount: T::Balance,
) -> DispatchResult {
ensure_root(origin)?;
let who = T::Lookup::lookup(who)?;
let _leftover = <Self as ReservableCurrency<_>>::unreserve(&who, amount);
Ok(())
}
#[pezpallet::call_index(6)]
#[pezpallet::weight(T::WeightInfo::upgrade_accounts(who.len() as u32))]
pub fn upgrade_accounts(
origin: OriginFor<T>,
who: Vec<T::AccountId>,
) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
if who.is_empty() {
return Ok(Pays::Yes.into());
}
let mut upgrade_count = 0;
for i in &who {
let upgraded = Self::ensure_upgraded(i);
if upgraded {
upgrade_count.saturating_inc();
}
}
let proportion_upgraded = Perbill::from_rational(upgrade_count, who.len() as u32);
if proportion_upgraded >= Perbill::from_percent(90) {
Ok(Pays::No.into())
} else {
Ok(Pays::Yes.into())
}
}
#[pezpallet::call_index(8)]
#[pezpallet::weight(
T::WeightInfo::force_set_balance_creating() // Creates a new account.
.max(T::WeightInfo::force_set_balance_killing()) // Kills an existing account.
)]
pub fn force_set_balance(
origin: OriginFor<T>,
who: AccountIdLookupOf<T>,
#[pezpallet::compact] new_free: T::Balance,
) -> DispatchResult {
ensure_root(origin)?;
let who = T::Lookup::lookup(who)?;
let existential_deposit = Self::ed();
let wipeout = new_free < existential_deposit;
let new_free = if wipeout { Zero::zero() } else { new_free };
let old_free = Self::mutate_account_handling_dust(&who, false, |account| {
let old_free = account.free;
account.free = new_free;
old_free
})?;
if new_free > old_free {
mem::drop(PositiveImbalance::<T, I>::new(new_free - old_free));
} else if new_free < old_free {
mem::drop(NegativeImbalance::<T, I>::new(old_free - new_free));
}
Self::deposit_event(Event::BalanceSet { who, free: new_free });
Ok(())
}
#[doc = docify::embed!("./src/tests/dispatchable_tests.rs", force_adjust_total_issuance_example)]
#[pezpallet::call_index(9)]
#[pezpallet::weight(T::WeightInfo::force_adjust_total_issuance())]
pub fn force_adjust_total_issuance(
origin: OriginFor<T>,
direction: AdjustmentDirection,
#[pezpallet::compact] delta: T::Balance,
) -> DispatchResult {
ensure_root(origin)?;
ensure!(delta > Zero::zero(), Error::<T, I>::DeltaZero);
let old = TotalIssuance::<T, I>::get();
let new = match direction {
AdjustmentDirection::Increase => old.saturating_add(delta),
AdjustmentDirection::Decrease => old.saturating_sub(delta),
};
ensure!(InactiveIssuance::<T, I>::get() <= new, Error::<T, I>::IssuanceDeactivated);
TotalIssuance::<T, I>::set(new);
Self::deposit_event(Event::<T, I>::TotalIssuanceForced { old, new });
Ok(())
}
#[pezpallet::call_index(10)]
#[pezpallet::weight(if *keep_alive {T::WeightInfo::burn_allow_death() } else {T::WeightInfo::burn_keep_alive()})]
pub fn burn(
origin: OriginFor<T>,
#[pezpallet::compact] value: T::Balance,
keep_alive: bool,
) -> DispatchResult {
let source = ensure_signed(origin)?;
let preservation = if keep_alive { Preserve } else { Expendable };
<Self as fungible::Mutate<_>>::burn_from(
&source,
value,
preservation,
Precision::Exact,
Polite,
)?;
Ok(())
}
}
impl<T: Config<I>, I: 'static> Pezpallet<T, I> {
pub fn total_issuance() -> T::Balance {
TotalIssuance::<T, I>::get()
}
pub fn inactive_issuance() -> T::Balance {
InactiveIssuance::<T, I>::get()
}
pub fn locks(who: &T::AccountId) -> WeakBoundedVec<BalanceLock<T::Balance>, T::MaxLocks> {
Locks::<T, I>::get(who)
}
pub fn reserves(
who: &T::AccountId,
) -> BoundedVec<ReserveData<T::ReserveIdentifier, T::Balance>, T::MaxReserves> {
Reserves::<T, I>::get(who)
}
fn ed() -> T::Balance {
T::ExistentialDeposit::get()
}
pub fn ensure_upgraded(who: &T::AccountId) -> bool {
let mut a = T::AccountStore::get(who);
if a.flags.is_new_logic() {
return false;
}
a.flags.set_new_logic();
if !a.reserved.is_zero() && a.frozen.is_zero() {
if system::Pezpallet::<T>::providers(who) == 0 {
log::warn!(
target: LOG_TARGET,
"account with a non-zero reserve balance has no provider refs, account_id: '{:?}'.",
who
);
a.free = a.free.max(Self::ed());
system::Pezpallet::<T>::inc_providers(who);
}
let _ = system::Pezpallet::<T>::inc_consumers_without_limit(who).defensive();
}
let _ = T::AccountStore::try_mutate_exists(who, |account| -> DispatchResult {
*account = Some(a);
Ok(())
});
Self::deposit_event(Event::Upgraded { who: who.clone() });
return true;
}
pub fn free_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
Self::account(who.borrow()).free
}
pub fn usable_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
<Self as fungible::Inspect<_>>::reducible_balance(who.borrow(), Expendable, Polite)
}
pub fn usable_balance_for_fees(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
<Self as fungible::Inspect<_>>::reducible_balance(who.borrow(), Protect, Polite)
}
pub fn reserved_balance(who: impl core::borrow::Borrow<T::AccountId>) -> T::Balance {
Self::account(who.borrow()).reserved
}
pub(crate) fn account(who: &T::AccountId) -> AccountData<T::Balance> {
T::AccountStore::get(who)
}
pub(crate) fn mutate_account_handling_dust<R>(
who: &T::AccountId,
force_consumer_bump: bool,
f: impl FnOnce(&mut AccountData<T::Balance>) -> R,
) -> Result<R, DispatchError> {
let (r, maybe_dust) = Self::mutate_account(who, force_consumer_bump, f)?;
if let Some(dust) = maybe_dust {
<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
}
Ok(r)
}
pub(crate) fn try_mutate_account_handling_dust<R, E: From<DispatchError>>(
who: &T::AccountId,
force_consumer_bump: bool,
f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
) -> Result<R, E> {
let (r, maybe_dust) = Self::try_mutate_account(who, force_consumer_bump, f)?;
if let Some(dust) = maybe_dust {
<Self as fungible::Unbalanced<_>>::handle_raw_dust(dust);
}
Ok(r)
}
pub(crate) fn mutate_account<R>(
who: &T::AccountId,
force_consumer_bump: bool,
f: impl FnOnce(&mut AccountData<T::Balance>) -> R,
) -> Result<(R, Option<T::Balance>), DispatchError> {
Self::try_mutate_account(who, force_consumer_bump, |a, _| -> Result<R, DispatchError> {
Ok(f(a))
})
}
#[cfg(not(feature = "insecure_zero_ed"))]
fn have_providers_or_no_zero_ed(_: &T::AccountId) -> bool {
true
}
#[cfg(feature = "insecure_zero_ed")]
fn have_providers_or_no_zero_ed(who: &T::AccountId) -> bool {
pezframe_system::Pezpallet::<T>::providers(who) > 0
}
pub(crate) fn try_mutate_account<R, E: From<DispatchError>>(
who: &T::AccountId,
force_consumer_bump: bool,
f: impl FnOnce(&mut AccountData<T::Balance>, bool) -> Result<R, E>,
) -> Result<(R, Option<T::Balance>), E> {
Self::ensure_upgraded(who);
let result = T::AccountStore::try_mutate_exists(who, |maybe_account| {
let is_new = maybe_account.is_none();
let mut account = maybe_account.take().unwrap_or_default();
let did_provide =
account.free >= Self::ed() && Self::have_providers_or_no_zero_ed(who);
let did_consume =
!is_new && (!account.reserved.is_zero() || !account.frozen.is_zero());
let result = f(&mut account, is_new)?;
let does_provide = account.free >= Self::ed();
let does_consume = !account.reserved.is_zero() || !account.frozen.is_zero();
if !did_provide && does_provide {
pezframe_system::Pezpallet::<T>::inc_providers(who);
}
if did_consume && !does_consume {
pezframe_system::Pezpallet::<T>::dec_consumers(who);
}
if !did_consume && does_consume {
if force_consumer_bump {
pezframe_system::Pezpallet::<T>::inc_consumers_without_limit(who)?;
} else {
pezframe_system::Pezpallet::<T>::inc_consumers(who)?;
}
}
if does_consume && pezframe_system::Pezpallet::<T>::consumers(who) == 0 {
log::error!(target: LOG_TARGET, "Defensively bumping a consumer ref.");
pezframe_system::Pezpallet::<T>::inc_consumers(who)?;
}
if did_provide && !does_provide {
pezframe_system::Pezpallet::<T>::dec_providers(who).inspect_err(|_| {
if did_consume && !does_consume {
let _ = pezframe_system::Pezpallet::<T>::inc_consumers(who).defensive();
}
if !did_consume && does_consume {
let _ = pezframe_system::Pezpallet::<T>::dec_consumers(who);
}
})?;
}
let maybe_endowed = if is_new { Some(account.free) } else { None };
let ed = Self::ed();
let maybe_dust = if account.free < ed && account.reserved.is_zero() {
if account.free.is_zero() {
None
} else {
Some(account.free)
}
} else {
assert!(
account.free.is_zero() || account.free >= ed || !account.reserved.is_zero()
);
*maybe_account = Some(account);
None
};
Ok((maybe_endowed, maybe_dust, result))
});
result.map(|(maybe_endowed, maybe_dust, result)| {
if let Some(endowed) = maybe_endowed {
Self::deposit_event(Event::Endowed {
account: who.clone(),
free_balance: endowed,
});
}
if let Some(amount) = maybe_dust {
Pezpallet::<T, I>::deposit_event(Event::DustLost {
account: who.clone(),
amount,
});
}
(result, maybe_dust)
})
}
pub(crate) fn update_locks(who: &T::AccountId, locks: &[BalanceLock<T::Balance>]) {
let bounded_locks = WeakBoundedVec::<_, T::MaxLocks>::force_from(
locks.to_vec(),
Some("Balances Update Locks"),
);
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."
);
}
let freezes = Freezes::<T, I>::get(who);
let mut prev_frozen = Zero::zero();
let mut after_frozen = Zero::zero();
let res = Self::mutate_account(who, true, |b| {
prev_frozen = b.frozen;
b.frozen = Zero::zero();
for l in locks.iter() {
b.frozen = b.frozen.max(l.amount);
}
for l in freezes.iter() {
b.frozen = b.frozen.max(l.amount);
}
after_frozen = b.frozen;
});
match res {
Ok((_, None)) => {
},
Ok((_, Some(_dust))) => {
Self::deposit_event(Event::Unexpected(UnexpectedKind::BalanceUpdated));
defensive!("caused unexpected dusting/balance update.");
},
_ => {
Self::deposit_event(Event::Unexpected(UnexpectedKind::FailedToMutateAccount));
defensive!("errored in mutate_account");
},
}
match locks.is_empty() {
true => Locks::<T, I>::remove(who),
false => Locks::<T, I>::insert(who, bounded_locks),
}
if prev_frozen > after_frozen {
let amount = prev_frozen.saturating_sub(after_frozen);
Self::deposit_event(Event::Unlocked { who: who.clone(), amount });
} else if after_frozen > prev_frozen {
let amount = after_frozen.saturating_sub(prev_frozen);
Self::deposit_event(Event::Locked { who: who.clone(), amount });
}
}
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, false, |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;
})?;
if maybe_dust.is_some() {
Self::deposit_event(Event::Unexpected(UnexpectedKind::BalanceUpdated));
defensive!("caused unexpected dusting/balance update.");
}
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(())
}
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,
false,
|to_account, is_new| -> Result<((), Option<T::Balance>), DispatchError> {
ensure!(!is_new, Error::<T, I>::DeadAccount);
Self::try_mutate_account(slashed, false, |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)
}
pub fn derive_dev_account(num_accounts: u32, balance: T::Balance, derivation: &str) {
assert!(num_accounts > 0, "num_accounts must be greater than zero");
assert!(
balance >= <T as Config<I>>::ExistentialDeposit::get(),
"the balance of any account should always be at least the existential deposit.",
);
assert!(
derivation.contains("{}"),
"Invalid derivation, expected `{{}}` as part of the derivation"
);
for index in 0..num_accounts {
let derivation_string = derivation.replace("{}", &index.to_string());
let pair: SrPair = Pair::from_string(&derivation_string, None)
.expect(&format!("Failed to parse derivation string: {derivation_string}"));
let who = T::AccountId::decode(&mut &pair.public().encode()[..])
.expect(&format!("Failed to decode public key from pair: {:?}", pair.public()));
Self::mutate_account_handling_dust(&who, false, |account| {
account.free = balance;
})
.expect(&format!("Failed to add account to keystore: {:?}", who));
}
}
}
#[cfg(any(test, feature = "try-runtime"))]
impl<T: Config<I>, I: 'static> Pezpallet<T, I> {
pub(crate) fn do_try_state(
_n: BlockNumberFor<T>,
) -> Result<(), pezsp_runtime::TryRuntimeError> {
Self::hold_and_freeze_count()?;
Self::account_frozen_greater_than_locks()?;
Self::account_frozen_greater_than_freezes()?;
Ok(())
}
fn hold_and_freeze_count() -> Result<(), pezsp_runtime::TryRuntimeError> {
Holds::<T, I>::iter_keys().try_for_each(|k| {
if Holds::<T, I>::decode_len(k).unwrap_or(0)
> T::RuntimeHoldReason::VARIANT_COUNT as usize
{
Err("Found `Hold` with too many elements")
} else {
Ok(())
}
})?;
Freezes::<T, I>::iter_keys().try_for_each(|k| {
if Freezes::<T, I>::decode_len(k).unwrap_or(0) > T::MaxFreezes::get() as usize {
Err("Found `Freeze` with too many elements")
} else {
Ok(())
}
})?;
Ok(())
}
fn account_frozen_greater_than_locks() -> Result<(), pezsp_runtime::TryRuntimeError> {
Locks::<T, I>::iter().try_for_each(|(who, locks)| {
let max_locks = locks.iter().map(|l| l.amount).max().unwrap_or_default();
let frozen = T::AccountStore::get(&who).frozen;
if max_locks > frozen {
log::warn!(
target: crate::LOG_TARGET,
"Maximum lock of {:?} ({:?}) is greater than the frozen balance {:?}",
who,
max_locks,
frozen
);
Err("bad locks".into())
} else {
Ok(())
}
})
}
fn account_frozen_greater_than_freezes() -> Result<(), pezsp_runtime::TryRuntimeError> {
Freezes::<T, I>::iter().try_for_each(|(who, freezes)| {
let max_locks = freezes.iter().map(|l| l.amount).max().unwrap_or_default();
let frozen = T::AccountStore::get(&who).frozen;
if max_locks > frozen {
log::warn!(
target: crate::LOG_TARGET,
"Maximum freeze of {:?} ({:?}) is greater than the frozen balance {:?}",
who,
max_locks,
frozen
);
Err("bad freezes".into())
} else {
Ok(())
}
})
}
}
}