mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 08:41:02 +00:00
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
This commit is contained in:
@@ -24,7 +24,7 @@ use frame_system::RawOrigin;
|
|||||||
use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite};
|
use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite};
|
||||||
use sp_runtime::traits::Bounded;
|
use sp_runtime::traits::Bounded;
|
||||||
|
|
||||||
use crate::Module as Indices;
|
use crate::Pallet as Indices;
|
||||||
|
|
||||||
const SEED: u32 = 0;
|
const SEED: u32 = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -29,86 +29,51 @@ use sp_std::prelude::*;
|
|||||||
use codec::Codec;
|
use codec::Codec;
|
||||||
use sp_runtime::MultiAddress;
|
use sp_runtime::MultiAddress;
|
||||||
use sp_runtime::traits::{
|
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::traits::{Currency, ReservableCurrency, BalanceStatus::Reserved};
|
||||||
use frame_support::dispatch::DispatchResult;
|
|
||||||
use frame_support::traits::{Currency, ReservableCurrency, Get, BalanceStatus::Reserved};
|
|
||||||
use frame_system::{ensure_signed, ensure_root};
|
|
||||||
pub use weights::WeightInfo;
|
pub use weights::WeightInfo;
|
||||||
|
|
||||||
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
|
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
|
||||||
|
|
||||||
|
pub use pallet::*;
|
||||||
|
|
||||||
|
#[frame_support::pallet]
|
||||||
|
pub mod pallet {
|
||||||
|
use frame_support::pallet_prelude::*;
|
||||||
|
use frame_system::pallet_prelude::*;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
/// The module's config trait.
|
/// The module's config trait.
|
||||||
|
#[pallet::config]
|
||||||
pub trait Config: frame_system::Config {
|
pub trait Config: frame_system::Config {
|
||||||
/// Type used for storing an account's index; implies the maximum number of accounts the system
|
/// Type used for storing an account's index; implies the maximum number of accounts the system
|
||||||
/// can hold.
|
/// can hold.
|
||||||
type AccountIndex: Parameter + Member + Codec + Default + AtLeast32Bit + Copy;
|
type AccountIndex: Parameter + Member + MaybeSerializeDeserialize + Codec + Default + AtLeast32Bit + Copy;
|
||||||
|
|
||||||
/// The currency trait.
|
/// The currency trait.
|
||||||
type Currency: ReservableCurrency<Self::AccountId>;
|
type Currency: ReservableCurrency<Self::AccountId>;
|
||||||
|
|
||||||
/// The deposit needed for reserving an index.
|
/// The deposit needed for reserving an index.
|
||||||
|
#[pallet::constant]
|
||||||
type Deposit: Get<BalanceOf<Self>>;
|
type Deposit: Get<BalanceOf<Self>>;
|
||||||
|
|
||||||
/// The overarching event type.
|
/// The overarching event type.
|
||||||
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
|
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
|
||||||
|
|
||||||
/// Weight information for extrinsics in this pallet.
|
/// Weight information for extrinsics in this pallet.
|
||||||
type WeightInfo: WeightInfo;
|
type WeightInfo: WeightInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
decl_storage! {
|
#[pallet::pallet]
|
||||||
trait Store for Module<T: Config> as Indices {
|
#[pallet::generate_store(pub(super) trait Store)]
|
||||||
/// The lookup from index to account.
|
pub struct Pallet<T>(PhantomData<T>);
|
||||||
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)>;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
decl_event!(
|
#[pallet::hooks]
|
||||||
pub enum Event<T> where
|
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
|
||||||
<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),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
fn deposit_event() = default;
|
|
||||||
|
|
||||||
|
#[pallet::call]
|
||||||
|
impl<T: Config> Pallet<T> {
|
||||||
/// Assign an previously unassigned index.
|
/// Assign an previously unassigned index.
|
||||||
///
|
///
|
||||||
/// Payment: `Deposit` is reserved from the sender account.
|
/// Payment: `Deposit` is reserved from the sender account.
|
||||||
@@ -127,8 +92,8 @@ decl_module! {
|
|||||||
/// -------------------
|
/// -------------------
|
||||||
/// - DB Weight: 1 Read/Write (Accounts)
|
/// - DB Weight: 1 Read/Write (Accounts)
|
||||||
/// # </weight>
|
/// # </weight>
|
||||||
#[weight = T::WeightInfo::claim()]
|
#[pallet::weight(T::WeightInfo::claim())]
|
||||||
fn claim(origin, index: T::AccountIndex) {
|
pub(crate) fn claim(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
|
||||||
let who = ensure_signed(origin)?;
|
let who = ensure_signed(origin)?;
|
||||||
|
|
||||||
Accounts::<T>::try_mutate(index, |maybe_value| {
|
Accounts::<T>::try_mutate(index, |maybe_value| {
|
||||||
@@ -136,7 +101,8 @@ decl_module! {
|
|||||||
*maybe_value = Some((who.clone(), T::Deposit::get(), false));
|
*maybe_value = Some((who.clone(), T::Deposit::get(), false));
|
||||||
T::Currency::reserve(&who, T::Deposit::get())
|
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
|
/// 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)
|
/// - Reads: Indices Accounts, System Account (recipient)
|
||||||
/// - Writes: Indices Accounts, System Account (recipient)
|
/// - Writes: Indices Accounts, System Account (recipient)
|
||||||
/// # </weight>
|
/// # </weight>
|
||||||
#[weight = T::WeightInfo::transfer()]
|
#[pallet::weight(T::WeightInfo::transfer())]
|
||||||
fn transfer(origin, new: T::AccountId, index: T::AccountIndex) {
|
pub(crate) fn transfer(
|
||||||
|
origin: OriginFor<T>,
|
||||||
|
new: T::AccountId,
|
||||||
|
index: T::AccountIndex,
|
||||||
|
) -> DispatchResult {
|
||||||
let who = ensure_signed(origin)?;
|
let who = ensure_signed(origin)?;
|
||||||
ensure!(who != new, Error::<T>::NotTransfer);
|
ensure!(who != new, Error::<T>::NotTransfer);
|
||||||
|
|
||||||
@@ -172,7 +142,8 @@ decl_module! {
|
|||||||
*maybe_value = Some((new.clone(), amount.saturating_sub(lost), false));
|
*maybe_value = Some((new.clone(), amount.saturating_sub(lost), false));
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
Self::deposit_event(RawEvent::IndexAssigned(new, index));
|
Self::deposit_event(Event::IndexAssigned(new, index));
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Free up an index owned by the sender.
|
/// Free up an index owned by the sender.
|
||||||
@@ -193,8 +164,8 @@ decl_module! {
|
|||||||
/// -------------------
|
/// -------------------
|
||||||
/// - DB Weight: 1 Read/Write (Accounts)
|
/// - DB Weight: 1 Read/Write (Accounts)
|
||||||
/// # </weight>
|
/// # </weight>
|
||||||
#[weight = T::WeightInfo::free()]
|
#[pallet::weight(T::WeightInfo::free())]
|
||||||
fn free(origin, index: T::AccountIndex) {
|
pub(crate) fn free(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
|
||||||
let who = ensure_signed(origin)?;
|
let who = ensure_signed(origin)?;
|
||||||
|
|
||||||
Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
|
Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
|
||||||
@@ -204,7 +175,8 @@ decl_module! {
|
|||||||
T::Currency::unreserve(&who, amount);
|
T::Currency::unreserve(&who, amount);
|
||||||
Ok(())
|
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
|
/// 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)
|
/// - Reads: Indices Accounts, System Account (original owner)
|
||||||
/// - Writes: Indices Accounts, System Account (original owner)
|
/// - Writes: Indices Accounts, System Account (original owner)
|
||||||
/// # </weight>
|
/// # </weight>
|
||||||
#[weight = T::WeightInfo::force_transfer()]
|
#[pallet::weight(T::WeightInfo::force_transfer())]
|
||||||
fn force_transfer(origin, new: T::AccountId, index: T::AccountIndex, freeze: bool) {
|
pub(crate) fn force_transfer(
|
||||||
|
origin: OriginFor<T>,
|
||||||
|
new: T::AccountId,
|
||||||
|
index: T::AccountIndex,
|
||||||
|
freeze: bool,
|
||||||
|
) -> DispatchResult {
|
||||||
ensure_root(origin)?;
|
ensure_root(origin)?;
|
||||||
|
|
||||||
Accounts::<T>::mutate(index, |maybe_value| {
|
Accounts::<T>::mutate(index, |maybe_value| {
|
||||||
@@ -238,7 +215,8 @@ decl_module! {
|
|||||||
}
|
}
|
||||||
*maybe_value = Some((new.clone(), Zero::zero(), freeze));
|
*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.
|
/// 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)
|
/// - DB Weight: 1 Read/Write (Accounts)
|
||||||
/// # </weight>
|
/// # </weight>
|
||||||
#[weight = T::WeightInfo::freeze()]
|
#[pallet::weight(T::WeightInfo::freeze())]
|
||||||
fn freeze(origin, index: T::AccountIndex) {
|
pub(crate) fn freeze(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
|
||||||
let who = ensure_signed(origin)?;
|
let who = ensure_signed(origin)?;
|
||||||
|
|
||||||
Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
|
Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
|
||||||
@@ -270,12 +248,74 @@ decl_module! {
|
|||||||
*maybe_value = Some((account, Zero::zero(), true));
|
*maybe_value = Some((account, Zero::zero(), true));
|
||||||
Ok(())
|
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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Config> Module<T> {
|
#[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> Pallet<T> {
|
||||||
// PUBLIC IMMUTABLES
|
// PUBLIC IMMUTABLES
|
||||||
|
|
||||||
/// Lookup an T::AccountIndex to get an Id, if there's one there.
|
/// 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 Source = MultiAddress<T::AccountId, T::AccountIndex>;
|
||||||
type Target = T::AccountId;
|
type Target = T::AccountId;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user