From 5b3927580f4f6be8f006c5299129fad7377c5d25 Mon Sep 17 00:00:00 2001 From: Xavier Lau <x@acg.box> Date: Sat, 18 Jan 2025 16:30:34 +0800 Subject: [PATCH] Resolve comments --- substrate/frame/nis/src/benchmarking.rs | 2 +- substrate/frame/nis/src/lib.rs | 49 +++++++++---------------- substrate/frame/nis/src/tests.rs | 7 +--- substrate/frame/src/lib.rs | 28 ++++++++++++-- substrate/frame/support/src/lib.rs | 2 +- 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/substrate/frame/nis/src/benchmarking.rs b/substrate/frame/nis/src/benchmarking.rs index 26024e1a625..a92212d8177 100644 --- a/substrate/frame/nis/src/benchmarking.rs +++ b/substrate/frame/nis/src/benchmarking.rs @@ -26,7 +26,7 @@ use crate::*; const SEED: u32 = 0; type BalanceOf<T> = - <<T as Config>::Currency as FunInspect<<T as frame_system::Config>::AccountId>>::Balance; + <<T as Config>::Currency as FungibleInspect<<T as frame_system::Config>::AccountId>>::Balance; fn fill_queues<T: Config>() -> Result<(), DispatchError> { // filling queues involves filling the first queue entirely and placing a single item in all diff --git a/substrate/frame/nis/src/lib.rs b/substrate/frame/nis/src/lib.rs index 6c53c0f3674..813739b2bae 100644 --- a/substrate/frame/nis/src/lib.rs +++ b/substrate/frame/nis/src/lib.rs @@ -90,25 +90,11 @@ pub use pallet::*; pub use weights::WeightInfo; use alloc::{vec, vec::Vec}; -use frame::{ - prelude::*, - traits::{ - fungible::{ - self, Balanced as FunBalanced, Inspect as FunInspect, Mutate as FunMutate, - MutateHold as FunMutateHold, - }, - nonfungible::{Inspect as NftInspect, Transfer as NftTransfer}, - tokens::{ - Balance, DepositConsequence, - Fortitude::{self, *}, - Precision::*, - Preservation::{self, *}, - Provenance, - Restriction::*, - WithdrawConsequence, - }, - }, -}; +use frame::prelude::*; +use Fortitude::*; +use Precision::*; +use Preservation::*; +use Restriction::*; pub struct WithMaximumOf<A: TypedGet>(core::marker::PhantomData<A>); impl<A: TypedGet> Convert<Perquintill, A::Type> for WithMaximumOf<A> @@ -132,7 +118,7 @@ where } pub struct NoCounterpart<T>(core::marker::PhantomData<T>); -impl<T> FunInspect<T> for NoCounterpart<T> { +impl<T> FungibleInspect<T> for NoCounterpart<T> { type Balance = u32; fn total_issuance() -> u32 { 0 @@ -163,7 +149,7 @@ impl<T> fungible::Unbalanced<T> for NoCounterpart<T> { } fn set_total_issuance(_: Self::Balance) {} } -impl<T: Eq> FunMutate<T> for NoCounterpart<T> {} +impl<T: Eq> FungibleMutate<T> for NoCounterpart<T> {} impl<T> Convert<Perquintill, u32> for NoCounterpart<T> { fn convert(_: Perquintill) -> u32 { 0 @@ -186,8 +172,9 @@ impl BenchmarkSetup for () { pub mod pallet { use super::*; - type BalanceOf<T> = - <<T as Config>::Currency as FunInspect<<T as frame_system::Config>::AccountId>>::Balance; + type BalanceOf<T> = <<T as Config>::Currency as FungibleInspect< + <T as frame_system::Config>::AccountId, + >>::Balance; type DebtOf<T> = fungible::Debt<<T as frame_system::Config>::AccountId, <T as Config>::Currency>; type ReceiptRecordOf<T> = @@ -210,10 +197,10 @@ pub mod pallet { type PalletId: Get<PalletId>; /// Currency type that this works on. - type Currency: FunInspect<Self::AccountId, Balance = Self::CurrencyBalance> - + FunMutate<Self::AccountId> - + FunBalanced<Self::AccountId> - + FunMutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>; + type Currency: FungibleInspect<Self::AccountId, Balance = Self::CurrencyBalance> + + FungibleMutate<Self::AccountId> + + FungibleBalanced<Self::AccountId> + + FungibleMutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>; /// Overarching hold reason. type RuntimeHoldReason: From<HoldReason>; @@ -230,7 +217,7 @@ pub mod pallet { type IgnoredIssuance: Get<BalanceOf<Self>>; /// The accounting system for the fungible counterpart tokens. - type Counterpart: FunMutate<Self::AccountId>; + type Counterpart: FungibleMutate<Self::AccountId>; /// The system to convert an overall proportion of issuance into a number of fungible /// counterpart tokens. @@ -238,7 +225,7 @@ pub mod pallet { /// In general it's best to use `WithMaximumOf`. type CounterpartAmount: ConvertBack< Perquintill, - <Self::Counterpart as FunInspect<Self::AccountId>>::Balance, + <Self::Counterpart as FungibleInspect<Self::AccountId>>::Balance, >; /// Unbalanced handler to account for funds created (in case of a higher total issuance over @@ -930,7 +917,7 @@ pub mod pallet { pub required: Balance, } - impl<T: Config> NftInspect<T::AccountId> for Pallet<T> { + impl<T: Config> NonFungibleInspect<T::AccountId> for Pallet<T> { type ItemId = ReceiptIndex; fn owner(item: &ReceiptIndex) -> Option<T::AccountId> { @@ -949,7 +936,7 @@ pub mod pallet { } } - impl<T: Config> NftTransfer<T::AccountId> for Pallet<T> { + impl<T: Config> NonFungibleTransfer<T::AccountId> for Pallet<T> { fn transfer(index: &ReceiptIndex, dest: &T::AccountId) -> DispatchResult { let mut item = Receipts::<T>::get(index).ok_or(TokenError::UnknownAsset)?; let (owner, on_hold) = item.owner.take().ok_or(Error::<T>::AlreadyCommunal)?; diff --git a/substrate/frame/nis/src/tests.rs b/substrate/frame/nis/src/tests.rs index 2329f3e8626..1bd9fc467f1 100644 --- a/substrate/frame/nis/src/tests.rs +++ b/substrate/frame/nis/src/tests.rs @@ -17,10 +17,7 @@ //! Tests for NIS pallet. -use frame::{ - runtime::prelude::TokenError::FundsUnavailable, testing_prelude::*, - traits::fungible::InspectHold, -}; +use frame::{runtime::prelude::*, testing_prelude::*}; use crate::{ mock::{Balance, *}, @@ -72,7 +69,7 @@ fn place_bid_works() { new_test_ext().execute_with(|| { System::run_to_block::<AllPalletsWithSystem>(1); assert_noop!(Nis::place_bid(signed(1), 1, 2), Error::<Test>::AmountTooSmall); - assert_noop!(Nis::place_bid(signed(1), 101, 2), FundsUnavailable); + assert_noop!(Nis::place_bid(signed(1), 101, 2), TokenError::FundsUnavailable); assert_noop!(Nis::place_bid(signed(1), 10, 4), Error::<Test>::DurationTooBig); assert_ok!(Nis::place_bid(signed(1), 10, 2)); assert_eq!(Balances::reserved_balance(1), 10); diff --git a/substrate/frame/src/lib.rs b/substrate/frame/src/lib.rs index 60b500f8a38..f23d9cbffa9 100644 --- a/substrate/frame/src/lib.rs +++ b/substrate/frame/src/lib.rs @@ -206,9 +206,11 @@ pub mod prelude { pub use frame_support::{ defensive, defensive_assert, traits::{ - Contains, EitherOf, EstimateNextSessionRotation, IsSubType, MapSuccess, NoOpPoll, - OnRuntimeUpgrade, OneSessionHandler, RankedMembers, RankedMembersSwapHandler, + Contains, Defensive, DefensiveSaturating, EitherOf, EstimateNextSessionRotation, + IsSubType, MapSuccess, NoOpPoll, OnRuntimeUpgrade, OneSessionHandler, RankedMembers, + RankedMembersSwapHandler, }, + PalletId, }; /// Pallet prelude of `frame-system`. @@ -229,11 +231,15 @@ pub mod prelude { /// All arithmetic types and traits used for safe math. pub use super::arithmetic::*; + /// All token related types and traits. + pub use super::token::*; + /// Runtime traits #[doc(no_inline)] pub use sp_runtime::traits::{ - BlockNumberProvider, Bounded, Convert, DispatchInfoOf, Dispatchable, ReduceBy, - ReplaceWithDefault, SaturatedConversion, Saturating, StaticLookup, TrailingZeroInput, + AccountIdConversion, BlockNumberProvider, Bounded, Convert, ConvertBack, DispatchInfoOf, + Dispatchable, ReduceBy, ReplaceWithDefault, SaturatedConversion, Saturating, StaticLookup, + TrailingZeroInput, }; /// Other error/result types for runtime #[doc(no_inline)] @@ -533,6 +539,20 @@ pub mod arithmetic { pub use sp_arithmetic::{traits::*, *}; } +/// All token related types and traits. +pub mod token { + pub use frame_support::traits::{ + fungible::{ + Balanced as FungibleBalanced, Inspect as FungibleInspect, + InspectHold as FungibleInspectHold, Mutate as FungibleMutate, + MutateHold as FungibleMutateHold, + }, + nonfungible::{Inspect as NonFungibleInspect, Transfer as NonFungibleTransfer}, + tokens::*, + OnUnbalanced, + }; +} + /// All derive macros used in frame. /// /// This is already part of the [`prelude`]. diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 9bfbc5f7dde..a6969260e6a 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -910,7 +910,7 @@ pub mod pallet_prelude { Task, TypedGet, }, Blake2_128, Blake2_128Concat, Blake2_256, CloneNoBound, DebugNoBound, EqNoBound, Identity, - PalletId, PartialEqNoBound, RuntimeDebugNoBound, Twox128, Twox256, Twox64Concat, + PartialEqNoBound, RuntimeDebugNoBound, Twox128, Twox256, Twox64Concat, }; pub use codec::{Decode, Encode, MaxEncodedLen}; pub use core::marker::PhantomData; -- GitLab