From 56ac28bba74085bc12452a55d05d65753edb6724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Andr=C3=A9s=20Dorado=20Su=C3=A1rez?= Date: Thu, 4 Apr 2024 12:07:16 -0500 Subject: [PATCH] change(pallet-assets-freezer): apply requested changes --- substrate/frame/assets-freezer/src/impls.rs | 22 +- substrate/frame/assets-freezer/src/lib.rs | 63 ++-- substrate/frame/assets-freezer/src/mock.rs | 11 +- substrate/frame/assets-freezer/src/tests.rs | 275 ++++++++++-------- substrate/frame/assets-freezer/src/types.rs | 36 --- substrate/frame/assets/src/types.rs | 10 +- substrate/frame/balances/src/lib.rs | 14 +- substrate/frame/balances/src/types.rs | 11 +- substrate/frame/support/src/traits/tokens.rs | 6 +- .../frame/support/src/traits/tokens/misc.rs | 10 + 10 files changed, 233 insertions(+), 225 deletions(-) delete mode 100644 substrate/frame/assets-freezer/src/types.rs diff --git a/substrate/frame/assets-freezer/src/impls.rs b/substrate/frame/assets-freezer/src/impls.rs index 00e96e57ee845..1e24fd194b3f0 100644 --- a/substrate/frame/assets-freezer/src/impls.rs +++ b/substrate/frame/assets-freezer/src/impls.rs @@ -19,7 +19,7 @@ use super::*; use frame_support::traits::fungibles::{Inspect, InspectFreeze, MutateFreeze}; use pallet_assets::FrozenBalance; -use sp_runtime::traits::{Get, Zero}; +use sp_runtime::traits::Zero; impl, I: 'static> FrozenBalance, AccountIdOf, AssetBalanceOf> for Pallet @@ -37,10 +37,10 @@ impl, I: 'static> FrozenBalance, AccountIdOf, As } } -/// Implement [`frame_support::traits::fungibles::Inspect`] as it is bound by -/// [`frame_support::traits::fungibles::InspectFreeze`] and -/// [`frame_support::traits::fungibles::MutateFreeze`]. To do so, we'll re-export all of -/// `pallet-assets` implementation of the same trait. +/// Implement [`fungibles::Inspect`](frame_support::traits::fungibles::Inspect) as it is bound by +/// [`fungibles::InspectFreeze`](frame_support::traits::fungibles::InspectFreeze) and +/// [`fungibles::MutateFreeze`](frame_support::traits::fungibles::MutateFreeze). To do so, we'll +/// re-export all of `pallet-assets` implementation of the same trait. impl, I: 'static> Inspect> for Pallet { type AssetId = AssetIdOf; type Balance = AssetBalanceOf; @@ -93,7 +93,7 @@ impl, I: 'static> Inspect> for Pallet { } impl, I: 'static> InspectFreeze> for Pallet { - type Id = T::RuntimeFreezeReason; + type Id = T::FreezeIdentifier; fn balance_frozen(asset: Self::AssetId, id: &Self::Id, who: &AccountIdOf) -> Self::Balance { if let Some(i) = Freezes::::get(asset, who).iter().find(|i| i.id == *id) { @@ -105,11 +105,7 @@ impl, I: 'static> InspectFreeze> for Pallet { fn can_freeze(asset: Self::AssetId, id: &Self::Id, who: &AccountIdOf) -> bool { let freezes = Freezes::::get(asset, who); - freezes.len() < - T::MaxFreezes::get() - .try_into() - .expect("MaxFreezes is the same type as S within Freezes; qed") || - freezes.into_iter().any(|i| i.id == *id) + !freezes.is_full() || freezes.into_iter().any(|i| i.id == *id) } } @@ -128,7 +124,7 @@ impl, I: 'static> MutateFreeze> for Pallet { i.amount = amount; } else { freezes - .try_push(IdAmount { id: id.clone(), amount }) + .try_push(IdAmount { id: *id, amount }) .map_err(|_| Error::::TooManyFreezes)?; } Self::update_freezes(asset, who, freezes.as_bounded_slice()) @@ -148,7 +144,7 @@ impl, I: 'static> MutateFreeze> for Pallet { i.amount = i.amount.max(amount); } else { freezes - .try_push(IdAmount { id: id.clone(), amount }) + .try_push(IdAmount { id: *id, amount }) .map_err(|_| Error::::TooManyFreezes)?; } Self::update_freezes(asset, who, freezes.as_bounded_slice()) diff --git a/substrate/frame/assets-freezer/src/lib.rs b/substrate/frame/assets-freezer/src/lib.rs index d56e608154ac4..f1e3e0f233a53 100644 --- a/substrate/frame/assets-freezer/src/lib.rs +++ b/substrate/frame/assets-freezer/src/lib.rs @@ -44,8 +44,13 @@ #![cfg_attr(not(feature = "std"), no_std)] -use frame_support::pallet_prelude::DispatchResult; +use frame_support::{ + pallet_prelude::*, + traits::{tokens::IdAmount, VariantCount}, + BoundedVec, +}; use frame_system::pallet_prelude::BlockNumberFor; +use pallet_assets::{AccountIdOf, AssetBalanceOf, AssetIdOf}; use sp_runtime::{ traits::{Saturating, Zero}, BoundedSlice, @@ -59,35 +64,26 @@ mod mock; mod tests; mod impls; -mod types; -pub use types::*; #[frame_support::pallet] pub mod pallet { use super::*; - use codec::FullCodec; - use core::fmt::Debug; - use frame_support::{pallet_prelude::*, traits::VariantCount, BoundedVec}; - - #[pallet::config] + #[pallet::config(with_default)] pub trait Config: frame_system::Config + pallet_assets::Config { + /// The ID type for freezes. + type FreezeIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy; + /// The overarching freeze reason. - type RuntimeFreezeReason: VariantCount - + FullCodec - + TypeInfo - + PartialEq - + Ord - + MaxEncodedLen - + Clone - + Debug - + 'static; + #[pallet::no_default_bounds] + type RuntimeFreezeReason: VariantCount; /// The overarching event type. + #[pallet::no_default_bounds] type RuntimeEvent: From> + IsType<::RuntimeEvent>; - /// The maximum number of individual freeze locks that can exist on an account at any time. + /// The maximum number of individual freezes that can exist on an account at any time. #[pallet::constant] type MaxFreezes: Get; } @@ -105,12 +101,12 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event, I: 'static = ()> { // A reducible balance has been increased due to a freeze action. - AssetFrozen { who: AccountIdOf, asset_id: AssetIdOf, amount: AssetBalanceOf }, + Frozen { who: AccountIdOf, asset_id: AssetIdOf, amount: AssetBalanceOf }, // A reducible balance has been reduced due to a thaw action. - AssetThawed { who: AccountIdOf, asset_id: AssetIdOf, amount: AssetBalanceOf }, + Thawed { who: AccountIdOf, asset_id: AssetIdOf, amount: AssetBalanceOf }, } - /// A map that stores all the current freezes applied on an account for a given AssetId. + /// A map that stores freezes applied on an account for a given AssetId. #[pallet::storage] pub(super) type Freezes, I: 'static = ()> = StorageDoubleMap< _, @@ -118,11 +114,11 @@ pub mod pallet { AssetIdOf, Blake2_128Concat, AccountIdOf, - BoundedVec>, T::MaxFreezes>, + BoundedVec>, T::MaxFreezes>, ValueQuery, >; - /// A map that stores the current reducible balance for every account on a given AssetId. + /// A map that stores the current frozen balance for every account on a given AssetId. #[pallet::storage] pub(super) type FrozenBalances, I: 'static = ()> = StorageDoubleMap< _, @@ -139,6 +135,14 @@ pub mod pallet { fn try_state(_: BlockNumberFor) -> Result<(), sp_runtime::TryRuntimeError> { Self::do_try_state() } + + fn integrity_test() { + assert!( + T::MaxFreezes::get() >= ::VARIANT_COUNT, + "MaxFreezes should be greater than or equal to the number of freeze reasons: {} < {}", + T::MaxFreezes::get(), ::VARIANT_COUNT, + ); + } } } @@ -146,10 +150,7 @@ impl, I: 'static> Pallet { fn update_freezes( asset: AssetIdOf, who: &AccountIdOf, - freezes: BoundedSlice< - IdAmount>, - T::MaxFreezes, - >, + freezes: BoundedSlice>, T::MaxFreezes>, ) -> DispatchResult { let prev_frozen = FrozenBalances::::get(asset.clone(), who).unwrap_or_default(); let mut after_frozen: AssetBalanceOf = Zero::zero(); @@ -165,10 +166,10 @@ impl, I: 'static> Pallet { } if prev_frozen > after_frozen { let amount = prev_frozen.saturating_sub(after_frozen); - Self::deposit_event(Event::AssetThawed { asset_id: asset, who: who.clone(), amount }); + Self::deposit_event(Event::Thawed { asset_id: asset, who: who.clone(), amount }); } else if after_frozen > prev_frozen { let amount = after_frozen.saturating_sub(prev_frozen); - Self::deposit_event(Event::AssetFrozen { asset_id: asset, who: who.clone(), amount }); + Self::deposit_event(Event::Frozen { asset_id: asset, who: who.clone(), amount }); } Ok(()) } @@ -177,8 +178,8 @@ impl, I: 'static> Pallet { fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> { for (asset, who, _) in FrozenBalances::::iter() { let max_frozen_amount = Freezes::::get(asset.clone(), who.clone()) - .into_iter() - .reduce(IdAmount::>::max) + .iter() + .reduce(|max, i| if i.amount >= max.amount { i } else { max }) .map(|l| l.amount); frame_support::ensure!( diff --git a/substrate/frame/assets-freezer/src/mock.rs b/substrate/frame/assets-freezer/src/mock.rs index 03cab98109e8b..d78a90b7e5375 100644 --- a/substrate/frame/assets-freezer/src/mock.rs +++ b/substrate/frame/assets-freezer/src/mock.rs @@ -112,19 +112,18 @@ impl pallet_assets::Config for Test { type BenchmarkHelper = (); } -#[derive(Decode, Encode, MaxEncodedLen, PartialEq, Eq, Ord, PartialOrd, TypeInfo, Debug, Clone)] +#[derive( + Decode, Encode, MaxEncodedLen, PartialEq, Eq, Ord, PartialOrd, TypeInfo, Debug, Clone, Copy, +)] pub enum DummyFreezeReason { Governance, Staking, Other, } -impl frame_support::traits::VariantCount for DummyFreezeReason { - const VARIANT_COUNT: u32 = 2; -} - impl Config for Test { - type RuntimeFreezeReason = DummyFreezeReason; + type FreezeIdentifier = DummyFreezeReason; + type RuntimeFreezeReason = RuntimeFreezeReason; type RuntimeEvent = RuntimeEvent; type MaxFreezes = ConstU32<2>; } diff --git a/substrate/frame/assets-freezer/src/tests.rs b/substrate/frame/assets-freezer/src/tests.rs index 9b16cb034aaa9..949ccf882f8d8 100644 --- a/substrate/frame/assets-freezer/src/tests.rs +++ b/substrate/frame/assets-freezer/src/tests.rs @@ -42,136 +42,183 @@ fn test_set_freeze(id: DummyFreezeReason, amount: Balance) { assert_ok!(AssetsFreezer::update_freezes(ASSET_ID, &WHO, freezes.as_bounded_slice())); } -#[test] -fn it_works_returning_balance_frozen() { - new_test_ext(|| { - test_set_freeze(DummyFreezeReason::Governance, 1); - assert_eq!( - AssetsFreezer::balance_frozen(ASSET_ID, &DummyFreezeReason::Governance, &WHO), - 1u64 - ); - }); -} +mod impls_frozen_balance { + use super::*; + + #[test] + fn it_works_returning_frozen_balance() { + new_test_ext(|| { + test_set_freeze(DummyFreezeReason::Governance, 1); + assert_eq!(AssetsFreezer::frozen_balance(ASSET_ID, &WHO), Some(1u64)); + test_set_freeze(DummyFreezeReason::Staking, 3); + FrozenBalances::::insert(ASSET_ID, WHO, 3); + assert_eq!(AssetsFreezer::frozen_balance(ASSET_ID, &WHO), Some(3u64)); + }); + } -#[test] -fn it_works_returning_frozen_balances() { - new_test_ext(|| { - test_set_freeze(DummyFreezeReason::Governance, 1); - assert_eq!(AssetsFreezer::frozen_balance(ASSET_ID, &WHO), Some(1u64)); - test_set_freeze(DummyFreezeReason::Staking, 3); - FrozenBalances::::insert(ASSET_ID, WHO, 3); - assert_eq!(AssetsFreezer::frozen_balance(ASSET_ID, &WHO), Some(3u64)); - }); + #[test] + fn calling_died_works() { + new_test_ext(|| { + test_set_freeze(DummyFreezeReason::Governance, 1); + AssetsFreezer::died(ASSET_ID, &WHO); + assert!(FrozenBalances::::get(ASSET_ID, WHO).is_none()); + assert!(Freezes::::get(ASSET_ID, WHO).is_empty()); + }); + } } -#[test] -fn it_works_returning_can_freeze() { - new_test_ext(|| { - test_set_freeze(DummyFreezeReason::Governance, 1); - assert!(AssetsFreezer::can_freeze(ASSET_ID, &DummyFreezeReason::Staking, &WHO)); - test_set_freeze(DummyFreezeReason::Staking, 1); - assert!(!AssetsFreezer::can_freeze(ASSET_ID, &DummyFreezeReason::Other, &WHO)); - }); +mod impl_inspect_freeze { + use super::*; + + #[test] + fn it_works_returning_balance_frozen() { + new_test_ext(|| { + test_set_freeze(DummyFreezeReason::Governance, 1); + assert_eq!( + AssetsFreezer::balance_frozen(ASSET_ID, &DummyFreezeReason::Governance, &WHO), + 1u64 + ); + }); + } + + #[test] + fn it_works_returning_can_freeze() { + new_test_ext(|| { + test_set_freeze(DummyFreezeReason::Governance, 1); + assert!(AssetsFreezer::can_freeze(ASSET_ID, &DummyFreezeReason::Staking, &WHO)); + test_set_freeze(DummyFreezeReason::Staking, 1); + assert!(!AssetsFreezer::can_freeze(ASSET_ID, &DummyFreezeReason::Other, &WHO)); + }); + } } -#[test] -fn set_freeze_works() { - new_test_ext(|| { - assert_ok!(AssetsFreezer::set_freeze(ASSET_ID, &DummyFreezeReason::Governance, &WHO, 10)); - assert_eq!( - AssetsFreezer::reducible_balance( +mod impl_mutate_freeze { + use super::*; + + #[test] + fn set_freeze_works() { + new_test_ext(|| { + assert_ok!(AssetsFreezer::set_freeze( ASSET_ID, + &DummyFreezeReason::Governance, &WHO, - frame_support::traits::tokens::Preservation::Preserve, - frame_support::traits::tokens::Fortitude::Polite, - ), - 89 - ); - System::assert_last_event( - Event::::AssetFrozen { asset_id: ASSET_ID, who: WHO, amount: 10 }.into(), - ); - assert_ok!(AssetsFreezer::set_freeze(ASSET_ID, &DummyFreezeReason::Governance, &WHO, 8)); - assert_eq!( - AssetsFreezer::reducible_balance( + 10 + )); + assert_eq!( + AssetsFreezer::reducible_balance( + ASSET_ID, + &WHO, + frame_support::traits::tokens::Preservation::Preserve, + frame_support::traits::tokens::Fortitude::Polite, + ), + 89 + ); + System::assert_last_event( + Event::::Frozen { asset_id: ASSET_ID, who: WHO, amount: 10 }.into(), + ); + assert_ok!(AssetsFreezer::set_freeze( ASSET_ID, + &DummyFreezeReason::Governance, &WHO, - frame_support::traits::tokens::Preservation::Preserve, - frame_support::traits::tokens::Fortitude::Polite, - ), - 91 - ); - System::assert_last_event( - Event::::AssetThawed { asset_id: ASSET_ID, who: WHO, amount: 2 }.into(), - ); - }); -} + 8 + )); + assert_eq!( + AssetsFreezer::reducible_balance( + ASSET_ID, + &WHO, + frame_support::traits::tokens::Preservation::Preserve, + frame_support::traits::tokens::Fortitude::Polite, + ), + 91 + ); + System::assert_last_event( + Event::::Thawed { asset_id: ASSET_ID, who: WHO, amount: 2 }.into(), + ); + }); + } -#[test] -fn extend_freeze_works() { - new_test_ext(|| { - assert_ok!(AssetsFreezer::set_freeze(ASSET_ID, &DummyFreezeReason::Governance, &WHO, 10)); - assert_ok!(AssetsFreezer::extend_freeze(ASSET_ID, &DummyFreezeReason::Governance, &WHO, 8)); - System::assert_last_event( - Event::::AssetFrozen { asset_id: ASSET_ID, who: WHO, amount: 10 }.into(), - ); - assert_eq!( - AssetsFreezer::reducible_balance( + #[test] + fn extend_freeze_works() { + new_test_ext(|| { + assert_ok!(AssetsFreezer::set_freeze( ASSET_ID, + &DummyFreezeReason::Governance, &WHO, - frame_support::traits::tokens::Preservation::Preserve, - frame_support::traits::tokens::Fortitude::Polite, - ), - 89 - ); - assert_ok!(AssetsFreezer::extend_freeze( - ASSET_ID, - &DummyFreezeReason::Governance, - &WHO, - 11 - )); - System::assert_last_event( - Event::::AssetFrozen { asset_id: ASSET_ID, who: WHO, amount: 1 }.into(), - ); - assert_eq!( - AssetsFreezer::reducible_balance( + 10 + )); + assert_ok!(AssetsFreezer::extend_freeze( ASSET_ID, + &DummyFreezeReason::Governance, &WHO, - frame_support::traits::tokens::Preservation::Preserve, - frame_support::traits::tokens::Fortitude::Polite, - ), - 88 - ); - }); -} - -#[test] -fn thaw_works() { - new_test_ext(|| { - assert_ok!(AssetsFreezer::set_freeze(ASSET_ID, &DummyFreezeReason::Governance, &WHO, 10)); - System::assert_has_event( - Event::::AssetFrozen { asset_id: ASSET_ID, who: WHO, amount: 10 }.into(), - ); - assert_eq!( - AssetsFreezer::reducible_balance( + 8 + )); + System::assert_last_event( + Event::::Frozen { asset_id: ASSET_ID, who: WHO, amount: 10 }.into(), + ); + assert_eq!( + AssetsFreezer::reducible_balance( + ASSET_ID, + &WHO, + frame_support::traits::tokens::Preservation::Preserve, + frame_support::traits::tokens::Fortitude::Polite, + ), + 89 + ); + assert_ok!(AssetsFreezer::extend_freeze( ASSET_ID, + &DummyFreezeReason::Governance, &WHO, - frame_support::traits::tokens::Preservation::Preserve, - frame_support::traits::tokens::Fortitude::Polite, - ), - 89 - ); - assert_ok!(AssetsFreezer::thaw(ASSET_ID, &DummyFreezeReason::Governance, &WHO)); - System::assert_has_event( - Event::::AssetThawed { asset_id: ASSET_ID, who: WHO, amount: 10 }.into(), - ); - assert_eq!( - AssetsFreezer::reducible_balance( + 11 + )); + System::assert_last_event( + Event::::Frozen { asset_id: ASSET_ID, who: WHO, amount: 1 }.into(), + ); + assert_eq!( + AssetsFreezer::reducible_balance( + ASSET_ID, + &WHO, + frame_support::traits::tokens::Preservation::Preserve, + frame_support::traits::tokens::Fortitude::Polite, + ), + 88 + ); + }); + } + + #[test] + fn thaw_works() { + new_test_ext(|| { + assert_ok!(AssetsFreezer::set_freeze( ASSET_ID, + &DummyFreezeReason::Governance, &WHO, - frame_support::traits::tokens::Preservation::Preserve, - frame_support::traits::tokens::Fortitude::Polite, - ), - 99 - ); - }); + 10 + )); + System::assert_has_event( + Event::::Frozen { asset_id: ASSET_ID, who: WHO, amount: 10 }.into(), + ); + assert_eq!( + AssetsFreezer::reducible_balance( + ASSET_ID, + &WHO, + frame_support::traits::tokens::Preservation::Preserve, + frame_support::traits::tokens::Fortitude::Polite, + ), + 89 + ); + assert_ok!(AssetsFreezer::thaw(ASSET_ID, &DummyFreezeReason::Governance, &WHO)); + System::assert_has_event( + Event::::Thawed { asset_id: ASSET_ID, who: WHO, amount: 10 }.into(), + ); + assert_eq!( + AssetsFreezer::reducible_balance( + ASSET_ID, + &WHO, + frame_support::traits::tokens::Preservation::Preserve, + frame_support::traits::tokens::Fortitude::Polite, + ), + 99 + ); + }); + } } diff --git a/substrate/frame/assets-freezer/src/types.rs b/substrate/frame/assets-freezer/src/types.rs deleted file mode 100644 index f9895a2e951b3..0000000000000 --- a/substrate/frame/assets-freezer/src/types.rs +++ /dev/null @@ -1,36 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: Apache-2.0 - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use frame_support::{ - pallet_prelude::{Decode, Encode, MaxEncodedLen, RuntimeDebug, TypeInfo}, - traits::fungibles::Inspect, -}; - -pub type AssetIdOf = as Inspect>>::AssetId; -pub type AssetBalanceOf = as Inspect>>::Balance; -pub type AccountIdOf = ::AccountId; - -/// An identifier and balance. -#[derive( - Encode, Decode, Clone, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug, MaxEncodedLen, TypeInfo, -)] -pub struct IdAmount { - /// An identifier for this item. - pub id: Id, - /// Some amount for this item. - pub amount: Balance, -} diff --git a/substrate/frame/assets/src/types.rs b/substrate/frame/assets/src/types.rs index 11edc7d3fcb58..e34320771f25a 100644 --- a/substrate/frame/assets/src/types.rs +++ b/substrate/frame/assets/src/types.rs @@ -120,7 +120,7 @@ where { pub(crate) fn take_deposit(&mut self) -> Option { if !matches!(self, ExistenceReason::DepositHeld(_)) { - return None + return None; } if let ExistenceReason::DepositHeld(deposit) = core::mem::replace(self, ExistenceReason::DepositRefunded) @@ -133,7 +133,7 @@ where pub(crate) fn take_deposit_from(&mut self) -> Option<(AccountId, Balance)> { if !matches!(self, ExistenceReason::DepositFrom(..)) { - return None + return None; } if let ExistenceReason::DepositFrom(depositor, deposit) = core::mem::replace(self, ExistenceReason::DepositRefunded) @@ -276,10 +276,10 @@ pub enum ConversionError { } // Type alias for `frame_system`'s account id. -type AccountIdOf = ::AccountId; +pub type AccountIdOf = ::AccountId; // This pallet's asset id and balance type. -type AssetIdOf = >::AssetId; -type AssetBalanceOf = >::Balance; +pub type AssetIdOf = >::AssetId; +pub type AssetBalanceOf = >::Balance; // Generic fungible balance type. type BalanceOf = >>::Balance; diff --git a/substrate/frame/balances/src/lib.rs b/substrate/frame/balances/src/lib.rs index 685b12499ac0f..df9538b8afa8f 100644 --- a/substrate/frame/balances/src/lib.rs +++ b/substrate/frame/balances/src/lib.rs @@ -158,6 +158,7 @@ use frame_support::{ tokens::{ fungible, BalanceStatus as Status, DepositConsequence, Fortitude::{self, Force, Polite}, + IdAmount, Preservation::{Expendable, Preserve, Protect}, WithdrawConsequence, }, @@ -177,8 +178,7 @@ use sp_runtime::{ }; use sp_std::{cmp, fmt::Debug, mem, prelude::*, result}; pub use types::{ - AccountData, AdjustmentDirection, BalanceLock, DustCleaner, ExtraFlags, IdAmount, Reasons, - ReserveData, + AccountData, AdjustmentDirection, BalanceLock, DustCleaner, ExtraFlags, Reasons, ReserveData, }; pub use weights::WeightInfo; @@ -681,7 +681,7 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { ensure_signed(origin)?; if who.is_empty() { - return Ok(Pays::Yes.into()) + return Ok(Pays::Yes.into()); } let mut upgrade_count = 0; for i in &who { @@ -779,7 +779,7 @@ pub mod pallet { pub fn ensure_upgraded(who: &T::AccountId) -> bool { let mut a = T::AccountStore::get(who); if a.flags.is_new_logic() { - return false + return false; } a.flags.set_new_logic(); if !a.reserved.is_zero() && a.frozen.is_zero() { @@ -803,7 +803,7 @@ pub mod pallet { Ok(()) }); Self::deposit_event(Event::Upgraded { who: who.clone() }); - return true + return true; } /// Get the free balance of an account. @@ -1109,7 +1109,7 @@ pub mod pallet { status: Status, ) -> Result { if value.is_zero() { - return Ok(Zero::zero()) + return Ok(Zero::zero()); } let max = >::reducible_total_balance_on_hold( @@ -1124,7 +1124,7 @@ pub mod pallet { 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( diff --git a/substrate/frame/balances/src/types.rs b/substrate/frame/balances/src/types.rs index 69d33bb023f39..274812acd81ea 100644 --- a/substrate/frame/balances/src/types.rs +++ b/substrate/frame/balances/src/types.rs @@ -51,7 +51,7 @@ impl BitOr for Reasons { type Output = Reasons; fn bitor(self, other: Reasons) -> Reasons { if self == other { - return self + return self; } Reasons::All } @@ -78,15 +78,6 @@ pub struct ReserveData { pub amount: Balance, } -/// An identifier and balance. -#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)] -pub struct IdAmount { - /// An identifier for this item. - pub id: Id, - /// Some amount for this item. - pub amount: Balance, -} - /// All balance information for an account. #[derive(Encode, Decode, Clone, PartialEq, Eq, Default, RuntimeDebug, MaxEncodedLen, TypeInfo)] pub struct AccountData { diff --git a/substrate/frame/support/src/traits/tokens.rs b/substrate/frame/support/src/traits/tokens.rs index 3635311e64357..0a6e8878c4918 100644 --- a/substrate/frame/support/src/traits/tokens.rs +++ b/substrate/frame/support/src/traits/tokens.rs @@ -30,8 +30,8 @@ pub use imbalance::Imbalance; pub mod pay; pub use misc::{ AssetId, Balance, BalanceStatus, ConversionFromAssetBalance, ConversionToAssetBalance, - ConvertRank, DepositConsequence, ExistenceRequirement, Fortitude, GetSalary, Locker, Precision, - Preservation, Provenance, Restriction, UnityAssetBalanceConversion, WithdrawConsequence, - WithdrawReasons, + ConvertRank, DepositConsequence, ExistenceRequirement, Fortitude, GetSalary, IdAmount, Locker, + Precision, Preservation, Provenance, Restriction, UnityAssetBalanceConversion, + WithdrawConsequence, WithdrawReasons, }; pub use pay::{Pay, PayFromAccount, PaymentStatus}; diff --git a/substrate/frame/support/src/traits/tokens/misc.rs b/substrate/frame/support/src/traits/tokens/misc.rs index a4dd5e4914283..58ac575f71515 100644 --- a/substrate/frame/support/src/traits/tokens/misc.rs +++ b/substrate/frame/support/src/traits/tokens/misc.rs @@ -17,6 +17,7 @@ //! Miscellaneous types. +use crate::TypeInfo; use codec::{Decode, Encode, FullCodec, MaxEncodedLen}; use sp_arithmetic::traits::{AtLeast32BitUnsigned, Zero}; use sp_core::RuntimeDebug; @@ -329,3 +330,12 @@ impl> GetSalary for ConvertRank { C::convert(rank) } } + +/// An identifier and balance. +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)] +pub struct IdAmount { + /// An identifier for this item. + pub id: Id, + /// Some amount for this item. + pub amount: Balance, +}