Skip to content

Commit

Permalink
change(pallet-assets-freezer): rename type aliases for direct type re…
Browse files Browse the repository at this point in the history
…ferences
  • Loading branch information
pandres95 committed Apr 11, 2024
1 parent 64a8de1 commit 90b8cd0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
43 changes: 18 additions & 25 deletions substrate/frame/assets-freezer/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,22 @@ use sp_runtime::traits::Zero;
/// Implements [`FrozenBalance`] from [`pallet-assets`], so it can understands how much of an
/// account balance is frozen, and is able to signal to this pallet when to clear the state of an
/// account.
impl<T: Config<I>, I: 'static> FrozenBalance<AssetIdOf<T, I>, AccountIdOf<T>, AssetBalanceOf<T, I>>
impl<T: Config<I>, I: 'static> FrozenBalance<T::AssetId, T::AccountId, T::Balance>
for Pallet<T, I>
{
fn frozen_balance(
asset: AssetIdOf<T, I>,
who: &AccountIdOf<T>,
) -> Option<AssetBalanceOf<T, I>> {
fn frozen_balance(asset: T::AssetId, who: &T::AccountId) -> Option<T::Balance> {
FrozenBalances::<T, I>::get(asset, who)
}

fn died(asset: AssetIdOf<T, I>, who: &AccountIdOf<T>) {
fn died(asset: T::AssetId, who: &T::AccountId) {
FrozenBalances::<T, I>::remove(asset.clone(), who);
Freezes::<T, I>::remove(asset, who);
}
}

impl<T: Config<I>, I: 'static> Inspect<AccountIdOf<T>> for Pallet<T, I> {
type AssetId = AssetIdOf<T, I>;
type Balance = AssetBalanceOf<T, I>;
impl<T: Config<I>, I: 'static> Inspect<T::AccountId> for Pallet<T, I> {
type AssetId = T::AssetId;
type Balance = T::Balance;

fn total_issuance(asset: Self::AssetId) -> Self::Balance {
pallet_assets::Pallet::<T, I>::total_issuance(asset)
Expand All @@ -55,17 +52,17 @@ impl<T: Config<I>, I: 'static> Inspect<AccountIdOf<T>> for Pallet<T, I> {
pallet_assets::Pallet::<T, I>::minimum_balance(asset)
}

fn total_balance(asset: Self::AssetId, who: &AccountIdOf<T>) -> Self::Balance {
fn total_balance(asset: Self::AssetId, who: &T::AccountId) -> Self::Balance {
pallet_assets::Pallet::<T, I>::total_balance(asset, who)
}

fn balance(asset: Self::AssetId, who: &AccountIdOf<T>) -> Self::Balance {
fn balance(asset: Self::AssetId, who: &T::AccountId) -> Self::Balance {
pallet_assets::Pallet::<T, I>::balance(asset, who)
}

fn reducible_balance(
asset: Self::AssetId,
who: &AccountIdOf<T>,
who: &T::AccountId,
preservation: Preservation,
force: Fortitude,
) -> Self::Balance {
Expand All @@ -74,7 +71,7 @@ impl<T: Config<I>, I: 'static> Inspect<AccountIdOf<T>> for Pallet<T, I> {

fn can_deposit(
asset: Self::AssetId,
who: &AccountIdOf<T>,
who: &T::AccountId,
amount: Self::Balance,
provenance: Provenance,
) -> DepositConsequence {
Expand All @@ -83,7 +80,7 @@ impl<T: Config<I>, I: 'static> Inspect<AccountIdOf<T>> for Pallet<T, I> {

fn can_withdraw(
asset: Self::AssetId,
who: &AccountIdOf<T>,
who: &T::AccountId,
amount: Self::Balance,
) -> WithdrawConsequence<Self::Balance> {
pallet_assets::Pallet::<T, I>::can_withdraw(asset, who, amount)
Expand All @@ -94,25 +91,25 @@ impl<T: Config<I>, I: 'static> Inspect<AccountIdOf<T>> for Pallet<T, I> {
}
}

impl<T: Config<I>, I: 'static> InspectFreeze<AccountIdOf<T>> for Pallet<T, I> {
impl<T: Config<I>, I: 'static> InspectFreeze<T::AccountId> for Pallet<T, I> {
type Id = T::FreezeIdentifier;

fn balance_frozen(asset: Self::AssetId, id: &Self::Id, who: &AccountIdOf<T>) -> Self::Balance {
fn balance_frozen(asset: Self::AssetId, id: &Self::Id, who: &T::AccountId) -> Self::Balance {
let freezes = Freezes::<T, I>::get(asset, who);
freezes.into_iter().find(|l| &l.id == id).map_or(Zero::zero(), |l| l.amount)
}

fn can_freeze(asset: Self::AssetId, id: &Self::Id, who: &AccountIdOf<T>) -> bool {
fn can_freeze(asset: Self::AssetId, id: &Self::Id, who: &T::AccountId) -> bool {
let freezes = Freezes::<T, I>::get(asset, who);
!freezes.is_full() || freezes.into_iter().any(|i| i.id == *id)
}
}

impl<T: Config<I>, I: 'static> MutateFreeze<AccountIdOf<T>> for Pallet<T, I> {
impl<T: Config<I>, I: 'static> MutateFreeze<T::AccountId> for Pallet<T, I> {
fn set_freeze(
asset: Self::AssetId,
id: &Self::Id,
who: &AccountIdOf<T>,
who: &T::AccountId,
amount: Self::Balance,
) -> sp_runtime::DispatchResult {
if amount.is_zero() {
Expand All @@ -132,7 +129,7 @@ impl<T: Config<I>, I: 'static> MutateFreeze<AccountIdOf<T>> for Pallet<T, I> {
fn extend_freeze(
asset: Self::AssetId,
id: &Self::Id,
who: &AccountIdOf<T>,
who: &T::AccountId,
amount: Self::Balance,
) -> sp_runtime::DispatchResult {
if amount.is_zero() {
Expand All @@ -149,11 +146,7 @@ impl<T: Config<I>, I: 'static> MutateFreeze<AccountIdOf<T>> for Pallet<T, I> {
Self::update_freezes(asset, who, freezes.as_bounded_slice())
}

fn thaw(
asset: Self::AssetId,
id: &Self::Id,
who: &AccountIdOf<T>,
) -> sp_runtime::DispatchResult {
fn thaw(asset: Self::AssetId, id: &Self::Id, who: &T::AccountId) -> sp_runtime::DispatchResult {
let mut freezes = Freezes::<T, I>::get(asset.clone(), who);
freezes.retain(|f| &f.id != id);
Self::update_freezes(asset, who, freezes.as_bounded_slice())
Expand Down
18 changes: 9 additions & 9 deletions substrate/frame/assets-freezer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ pub mod pallet {
pub(super) type Freezes<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
_,
Blake2_128Concat,
AssetIdOf<T, I>,
T::AssetId,
Blake2_128Concat,
AccountIdOf<T>,
BoundedVec<IdAmount<T::FreezeIdentifier, AssetBalanceOf<T, I>>, T::MaxFreezes>,
T::AccountId,
BoundedVec<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
ValueQuery,
>;

Expand All @@ -121,10 +121,10 @@ pub mod pallet {
pub(super) type FrozenBalances<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
_,
Blake2_128Concat,
AssetIdOf<T, I>,
T::AssetId,
Blake2_128Concat,
AccountIdOf<T>,
AssetBalanceOf<T, I>,
T::AccountId,
T::Balance,
>;

#[pallet::hooks]
Expand All @@ -146,9 +146,9 @@ pub mod pallet {

impl<T: Config<I>, I: 'static> Pallet<T, I> {
fn update_freezes(
asset: AssetIdOf<T, I>,
who: &AccountIdOf<T>,
freezes: BoundedSlice<IdAmount<T::FreezeIdentifier, AssetBalanceOf<T, I>>, T::MaxFreezes>,
asset: T::AssetId,
who: &T::AccountId,
freezes: BoundedSlice<IdAmount<T::FreezeIdentifier, T::Balance>, T::MaxFreezes>,
) -> DispatchResult {
let prev_frozen = FrozenBalances::<T, I>::get(asset.clone(), who).unwrap_or_default();
let after_frozen = freezes.into_iter().map(|f| f.amount).max().unwrap_or_else(Zero::zero);
Expand Down
6 changes: 3 additions & 3 deletions substrate/frame/assets/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ pub enum ConversionError {
}

// Type alias for `frame_system`'s account id.
pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
// This pallet's asset id and balance type.
pub type AssetIdOf<T, I> = <T as Config<I>>::AssetId;
pub type AssetBalanceOf<T, I> = <T as Config<I>>::Balance;
type AssetIdOf<T, I> = <T as Config<I>>::AssetId;
type AssetBalanceOf<T, I> = <T as Config<I>>::Balance;
// Generic fungible balance type.
type BalanceOf<F, T> = <F as fungible::Inspect<AccountIdOf<T>>>::Balance;

Expand Down

0 comments on commit 90b8cd0

Please sign in to comment.