Skip to content
Snippets Groups Projects
Commit 0e6481d0 authored by Andrew Jones's avatar Andrew Jones Committed by GitHub
Browse files

Migrate `pallet-indices` to `pallet!` (#8465)

* tmp add upgrade file

* Migrate pallet-indices to `pallet!`

* Delete temp upgrade file

* Fix some migration errors

* Fix some warnings

* Add serde bound, explicit balance type

* Module -> Pallet
parent d294e512
No related merge requests found
......@@ -24,7 +24,7 @@ use frame_system::RawOrigin;
use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite};
use sp_runtime::traits::Bounded;
use crate::Module as Indices;
use crate::Pallet as Indices;
const SEED: u32 = 0;
......
......@@ -29,86 +29,51 @@ use sp_std::prelude::*;
use codec::Codec;
use sp_runtime::MultiAddress;
use sp_runtime::traits::{
StaticLookup, Member, LookupError, Zero, Saturating, AtLeast32Bit
StaticLookup, LookupError, Zero, Saturating, AtLeast32Bit
};
use frame_support::{Parameter, decl_module, decl_error, decl_event, decl_storage, ensure};
use frame_support::dispatch::DispatchResult;
use frame_support::traits::{Currency, ReservableCurrency, Get, BalanceStatus::Reserved};
use frame_system::{ensure_signed, ensure_root};
use frame_support::traits::{Currency, ReservableCurrency, BalanceStatus::Reserved};
pub use weights::WeightInfo;
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
/// The module's config trait.
pub trait Config: frame_system::Config {
/// Type used for storing an account's index; implies the maximum number of accounts the system
/// can hold.
type AccountIndex: Parameter + Member + Codec + Default + AtLeast32Bit + Copy;
pub use pallet::*;
/// The currency trait.
type Currency: ReservableCurrency<Self::AccountId>;
#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use super::*;
/// The deposit needed for reserving an index.
type Deposit: Get<BalanceOf<Self>>;
/// The module's config trait.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Type used for storing an account's index; implies the maximum number of accounts the system
/// can hold.
type AccountIndex: Parameter + Member + MaybeSerializeDeserialize + Codec + Default + AtLeast32Bit + Copy;
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
/// The currency trait.
type Currency: ReservableCurrency<Self::AccountId>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
decl_storage! {
trait Store for Module<T: Config> as Indices {
/// The lookup from index to account.
pub Accounts build(|config: &GenesisConfig<T>|
config.indices.iter()
.cloned()
.map(|(a, b)| (a, (b, Zero::zero(), false)))
.collect::<Vec<_>>()
): map hasher(blake2_128_concat) T::AccountIndex => Option<(T::AccountId, BalanceOf<T>, bool)>;
}
add_extra_genesis {
config(indices): Vec<(T::AccountIndex, T::AccountId)>;
}
}
/// The deposit needed for reserving an index.
#[pallet::constant]
type Deposit: Get<BalanceOf<Self>>;
decl_event!(
pub enum Event<T> where
<T as frame_system::Config>::AccountId,
<T as Config>::AccountIndex
{
/// A account index was assigned. \[index, who\]
IndexAssigned(AccountId, AccountIndex),
/// A account index has been freed up (unassigned). \[index\]
IndexFreed(AccountIndex),
/// A account index has been frozen to its current account ID. \[index, who\]
IndexFrozen(AccountIndex, AccountId),
}
);
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
decl_error! {
pub enum Error for Module<T: Config> {
/// The index was not already assigned.
NotAssigned,
/// The index is assigned to another account.
NotOwner,
/// The index was not available.
InUse,
/// The source and destination accounts are identical.
NotTransfer,
/// The index is permanent and may not be freed/changed.
Permanent,
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
}
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin, system = frame_system {
/// The deposit needed for reserving an index.
const Deposit: BalanceOf<T> = T::Deposit::get();
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
fn deposit_event() = default;
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Assign an previously unassigned index.
///
/// Payment: `Deposit` is reserved from the sender account.
......@@ -127,8 +92,8 @@ decl_module! {
/// -------------------
/// - DB Weight: 1 Read/Write (Accounts)
/// # </weight>
#[weight = T::WeightInfo::claim()]
fn claim(origin, index: T::AccountIndex) {
#[pallet::weight(T::WeightInfo::claim())]
pub(crate) fn claim(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
let who = ensure_signed(origin)?;
Accounts::<T>::try_mutate(index, |maybe_value| {
......@@ -136,7 +101,8 @@ decl_module! {
*maybe_value = Some((who.clone(), T::Deposit::get(), false));
T::Currency::reserve(&who, T::Deposit::get())
})?;
Self::deposit_event(RawEvent::IndexAssigned(who, index));
Self::deposit_event(Event::IndexAssigned(who, index));
Ok(())
}
/// Assign an index already owned by the sender to another account. The balance reservation
......@@ -159,8 +125,12 @@ decl_module! {
/// - Reads: Indices Accounts, System Account (recipient)
/// - Writes: Indices Accounts, System Account (recipient)
/// # </weight>
#[weight = T::WeightInfo::transfer()]
fn transfer(origin, new: T::AccountId, index: T::AccountIndex) {
#[pallet::weight(T::WeightInfo::transfer())]
pub(crate) fn transfer(
origin: OriginFor<T>,
new: T::AccountId,
index: T::AccountIndex,
) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(who != new, Error::<T>::NotTransfer);
......@@ -172,7 +142,8 @@ decl_module! {
*maybe_value = Some((new.clone(), amount.saturating_sub(lost), false));
Ok(())
})?;
Self::deposit_event(RawEvent::IndexAssigned(new, index));
Self::deposit_event(Event::IndexAssigned(new, index));
Ok(())
}
/// Free up an index owned by the sender.
......@@ -193,8 +164,8 @@ decl_module! {
/// -------------------
/// - DB Weight: 1 Read/Write (Accounts)
/// # </weight>
#[weight = T::WeightInfo::free()]
fn free(origin, index: T::AccountIndex) {
#[pallet::weight(T::WeightInfo::free())]
pub(crate) fn free(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
let who = ensure_signed(origin)?;
Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
......@@ -204,7 +175,8 @@ decl_module! {
T::Currency::unreserve(&who, amount);
Ok(())
})?;
Self::deposit_event(RawEvent::IndexFreed(index));
Self::deposit_event(Event::IndexFreed(index));
Ok(())
}
/// Force an index to an account. This doesn't require a deposit. If the index is already
......@@ -228,8 +200,13 @@ decl_module! {
/// - Reads: Indices Accounts, System Account (original owner)
/// - Writes: Indices Accounts, System Account (original owner)
/// # </weight>
#[weight = T::WeightInfo::force_transfer()]
fn force_transfer(origin, new: T::AccountId, index: T::AccountIndex, freeze: bool) {
#[pallet::weight(T::WeightInfo::force_transfer())]
pub(crate) fn force_transfer(
origin: OriginFor<T>,
new: T::AccountId,
index: T::AccountIndex,
freeze: bool,
) -> DispatchResult {
ensure_root(origin)?;
Accounts::<T>::mutate(index, |maybe_value| {
......@@ -238,7 +215,8 @@ decl_module! {
}
*maybe_value = Some((new.clone(), Zero::zero(), freeze));
});
Self::deposit_event(RawEvent::IndexAssigned(new, index));
Self::deposit_event(Event::IndexAssigned(new, index));
Ok(())
}
/// Freeze an index so it will always point to the sender account. This consumes the deposit.
......@@ -258,8 +236,8 @@ decl_module! {
/// -------------------
/// - DB Weight: 1 Read/Write (Accounts)
/// # </weight>
#[weight = T::WeightInfo::freeze()]
fn freeze(origin, index: T::AccountIndex) {
#[pallet::weight(T::WeightInfo::freeze())]
pub(crate) fn freeze(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
let who = ensure_signed(origin)?;
Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
......@@ -270,12 +248,74 @@ decl_module! {
*maybe_value = Some((account, Zero::zero(), true));
Ok(())
})?;
Self::deposit_event(RawEvent::IndexFrozen(index, who));
Self::deposit_event(Event::IndexFrozen(index, who));
Ok(())
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
#[pallet::metadata(T::AccountId = "AccountId", T::AccountIndex = "AccountIndex")]
pub enum Event<T: Config> {
/// A account index was assigned. \[index, who\]
IndexAssigned(T::AccountId, T::AccountIndex),
/// A account index has been freed up (unassigned). \[index\]
IndexFreed(T::AccountIndex),
/// A account index has been frozen to its current account ID. \[index, who\]
IndexFrozen(T::AccountIndex, T::AccountId),
}
/// Old name generated by `decl_event`.
#[deprecated(note="use `Event` instead")]
pub type RawEvent<T> = Event<T>;
#[pallet::error]
pub enum Error<T> {
/// The index was not already assigned.
NotAssigned,
/// The index is assigned to another account.
NotOwner,
/// The index was not available.
InUse,
/// The source and destination accounts are identical.
NotTransfer,
/// The index is permanent and may not be freed/changed.
Permanent,
}
/// The lookup from index to account.
#[pallet::storage]
pub type Accounts<T: Config> = StorageMap<
_, Blake2_128Concat,
T::AccountIndex,
(T::AccountId, BalanceOf<T>, bool)
>;
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub indices: Vec<(T::AccountIndex, T::AccountId)>,
}
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self {
indices: Default::default(),
}
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
for (a, b) in &self.indices {
<Accounts<T>>::insert(a, (b, <BalanceOf<T>>::zero(), false))
}
}
}
}
impl<T: Config> Module<T> {
impl<T: Config> Pallet<T> {
// PUBLIC IMMUTABLES
/// Lookup an T::AccountIndex to get an Id, if there's one there.
......@@ -295,7 +335,7 @@ impl<T: Config> Module<T> {
}
}
impl<T: Config> StaticLookup for Module<T> {
impl<T: Config> StaticLookup for Pallet<T> {
type Source = MultiAddress<T::AccountId, T::AccountIndex>;
type Target = T::AccountId;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment