Skip to content

Commit

Permalink
chore(pallet-assets-freezer): remove last pieces of boilerplate code
Browse files Browse the repository at this point in the history
  • Loading branch information
pandres95 committed Apr 3, 2024
1 parent dbd9bd8 commit f2c9643
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 36 deletions.
7 changes: 2 additions & 5 deletions substrate/frame/assets-freezer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ This pallet provides the following functionality:

- Pallet hooks that implement custom logic to let `pallet-assets` know whether an balance is
frozen for an account on a given asset (see: [`pallet_assets::types::FrozenBalance`][docs:frozen_balance]).
- An implementation of fungibles [inspect][docs:inspect_freeze] and
[mutation][docs:mutate_freeze] APIs.
- Support for force freezing and thawing assets, given a Freezer ID
(see [`Config::RuntimeFreezeReason`][src:lib]).
- An implementation of the fungibles [inspect][docs:inspect_freeze] and the [mutation][docs:mutate_freeze]
APIs for freezes.

[docs:frozen_balance]: https://docs.rs/pallet-assets/latest/pallet_assets/trait.FrozenBalance.html
[docs:inspect_freeze]: https://docs.rs/frame-support/latest/frame_support/traits/tokens/fungibles/index.html#reexport.InspectFreeze
[docs:mutate_freeze]: https://docs.rs/frame-support/latest/frame_support/traits/tokens/fungibles/index.html#reexport.MutateFreeze
[src:lib]: ./src/lib.rs
32 changes: 12 additions & 20 deletions substrate/frame/assets-freezer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,14 @@
//!
//! - Pallet hooks that implement custom logic to let `pallet-assets` know whether an balance is
//! frozen for an account on a given asset (see: [`pallet_assets::FrozenBalance`]).
//! - An implementation of fungibles [inspect][docs:inspect_freeze] and
//! [mutation][docs:mutate_freeze] APIs.
//! - Support for force freezing and thawing assets, given a Freezer ID
//! (see [`Config::RuntimeFreezeReason`]).
//! - An implementation of the fungibles [inspect][docs:inspect_freeze] and the [mutation][docs:mutate_freeze]
//! APIs for freezes.
//!
//! [docs:inspect_freeze]: `frame_support::traits::fungibles::InspectFreeze`
//! [docs:mutate_freeze]: `frame_support::traits::fungibles::MutateFreeze`
#![cfg_attr(not(feature = "std"), no_std)]

use codec::Encode;
use frame_support::pallet_prelude::*;
use scale_info::prelude::fmt::Debug;
use sp_runtime::{
traits::{Saturating, Zero},
BoundedSlice,
};

// Re-export pallet items so that they can be accessed from the crate namespace.
pub use pallet::*;

#[cfg(test)]
Expand All @@ -71,9 +60,10 @@ pub use types::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;

use codec::FullCodec;
use frame_support::{traits::VariantCount, BoundedVec};
// use frame_system::pallet_prelude::*;
use core::fmt::Debug;
use frame_support::{pallet_prelude::*, traits::VariantCount, BoundedVec};

#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config + pallet_assets::Config<I> {
Expand All @@ -91,6 +81,7 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self, I>>
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// The maximum number of individual freeze locks that can exist on an account at any time.
#[pallet::constant]
type MaxFreezes: Get<u32>;
}
Expand All @@ -101,14 +92,9 @@ pub mod pallet {
TooManyFreezes,
}

// Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and
// method.
#[pallet::pallet]
pub struct Pallet<T, I = ()>(_);

#[pallet::call(weight(<T as Config>::WeightInfo))]
impl<T: Config<I>, I: 'static> Pallet<T, I> {}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config<I>, I: 'static = ()> {
Expand Down Expand Up @@ -142,6 +128,12 @@ pub mod pallet {
>;
}

use frame_support::pallet_prelude::DispatchResult;
use sp_runtime::{
traits::{Saturating, Zero},
BoundedSlice,
};

impl<T: Config<I>, I: 'static> Pallet<T, I> {
fn update_freezes(
asset: AssetIdOf<T, I>,
Expand Down
11 changes: 1 addition & 10 deletions substrate/frame/assets-freezer/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests mock for pallet-assets-freezer.
//! Tests mock for `pallet-assets-freezer`.
pub use crate::*;
use codec::{Compact, Decode, Encode, MaxEncodedLen};
Expand All @@ -25,21 +25,17 @@ use frame_support::{
};
use scale_info::TypeInfo;
use sp_core::{ConstU32, H256};
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
// Reexport crate as its pallet name for construct_runtime.
use crate as pallet_assets_freezer;

pub type AccountId = u64;
type Balance = u64;
pub type AssetId = u32;
type Block = frame_system::mocking::MockBlock<Test>;

// For testing the pallet, we construct a mock runtime.
frame_support::construct_runtime!(
pub enum Test
{
Expand Down Expand Up @@ -130,17 +126,12 @@ impl frame_support::traits::VariantCount for DummyFreezeReason {

impl Config for Test {
type RuntimeFreezeReason = DummyFreezeReason;
// type FreezeOrigin =
// AsEnsureOriginWithArg<EnsureRootWithSuccess<EnsureRoot<AccountId>, ConstU64<0>>>;
type RuntimeEvent = RuntimeEvent;
type MaxFreezes = ConstU32<2>;
}

// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
pub fn new_test_ext() -> sp_io::TestExternalities {
let t = RuntimeGenesisConfig {
// We use default for brevity, but you can configure as desired if needed.
assets: pallet_assets::GenesisConfig {
assets: vec![(1, 0, true, 1)],
metadata: vec![],
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/assets-freezer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// limitations under the License.

use frame_support::traits::fungibles::Inspect;
use super::*;
use frame_support::pallet_prelude::{Decode, Encode, MaxEncodedLen, RuntimeDebug, TypeInfo};

pub type AssetIdOf<T, I> = <pallet_assets::Pallet<T, I> as Inspect<AccountIdOf<T>>>::AssetId;
pub type AssetBalanceOf<T, I> = <pallet_assets::Pallet<T, I> as Inspect<AccountIdOf<T>>>::Balance;
Expand Down

0 comments on commit f2c9643

Please sign in to comment.