Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit bf200cc

Browse files
authored
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
1 parent 70f1b61 commit bf200cc

File tree

2 files changed

+123
-83
lines changed

2 files changed

+123
-83
lines changed

frame/indices/src/benchmarking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use frame_system::RawOrigin;
2424
use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite};
2525
use sp_runtime::traits::Bounded;
2626

27-
use crate::Module as Indices;
27+
use crate::Pallet as Indices;
2828

2929
const SEED: u32 = 0;
3030

frame/indices/src/lib.rs

Lines changed: 122 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -29,86 +29,51 @@ use sp_std::prelude::*;
2929
use codec::Codec;
3030
use sp_runtime::MultiAddress;
3131
use sp_runtime::traits::{
32-
StaticLookup, Member, LookupError, Zero, Saturating, AtLeast32Bit
32+
StaticLookup, LookupError, Zero, Saturating, AtLeast32Bit
3333
};
34-
use frame_support::{Parameter, decl_module, decl_error, decl_event, decl_storage, ensure};
35-
use frame_support::dispatch::DispatchResult;
36-
use frame_support::traits::{Currency, ReservableCurrency, Get, BalanceStatus::Reserved};
37-
use frame_system::{ensure_signed, ensure_root};
34+
use frame_support::traits::{Currency, ReservableCurrency, BalanceStatus::Reserved};
3835
pub use weights::WeightInfo;
3936

4037
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
4138

42-
/// The module's config trait.
43-
pub trait Config: frame_system::Config {
44-
/// Type used for storing an account's index; implies the maximum number of accounts the system
45-
/// can hold.
46-
type AccountIndex: Parameter + Member + Codec + Default + AtLeast32Bit + Copy;
39+
pub use pallet::*;
4740

48-
/// The currency trait.
49-
type Currency: ReservableCurrency<Self::AccountId>;
41+
#[frame_support::pallet]
42+
pub mod pallet {
43+
use frame_support::pallet_prelude::*;
44+
use frame_system::pallet_prelude::*;
45+
use super::*;
5046

51-
/// The deposit needed for reserving an index.
52-
type Deposit: Get<BalanceOf<Self>>;
47+
/// The module's config trait.
48+
#[pallet::config]
49+
pub trait Config: frame_system::Config {
50+
/// Type used for storing an account's index; implies the maximum number of accounts the system
51+
/// can hold.
52+
type AccountIndex: Parameter + Member + MaybeSerializeDeserialize + Codec + Default + AtLeast32Bit + Copy;
5353

54-
/// The overarching event type.
55-
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
54+
/// The currency trait.
55+
type Currency: ReservableCurrency<Self::AccountId>;
5656

57-
/// Weight information for extrinsics in this pallet.
58-
type WeightInfo: WeightInfo;
59-
}
60-
61-
decl_storage! {
62-
trait Store for Module<T: Config> as Indices {
63-
/// The lookup from index to account.
64-
pub Accounts build(|config: &GenesisConfig<T>|
65-
config.indices.iter()
66-
.cloned()
67-
.map(|(a, b)| (a, (b, Zero::zero(), false)))
68-
.collect::<Vec<_>>()
69-
): map hasher(blake2_128_concat) T::AccountIndex => Option<(T::AccountId, BalanceOf<T>, bool)>;
70-
}
71-
add_extra_genesis {
72-
config(indices): Vec<(T::AccountIndex, T::AccountId)>;
73-
}
74-
}
57+
/// The deposit needed for reserving an index.
58+
#[pallet::constant]
59+
type Deposit: Get<BalanceOf<Self>>;
7560

76-
decl_event!(
77-
pub enum Event<T> where
78-
<T as frame_system::Config>::AccountId,
79-
<T as Config>::AccountIndex
80-
{
81-
/// A account index was assigned. \[index, who\]
82-
IndexAssigned(AccountId, AccountIndex),
83-
/// A account index has been freed up (unassigned). \[index\]
84-
IndexFreed(AccountIndex),
85-
/// A account index has been frozen to its current account ID. \[index, who\]
86-
IndexFrozen(AccountIndex, AccountId),
87-
}
88-
);
61+
/// The overarching event type.
62+
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
8963

90-
decl_error! {
91-
pub enum Error for Module<T: Config> {
92-
/// The index was not already assigned.
93-
NotAssigned,
94-
/// The index is assigned to another account.
95-
NotOwner,
96-
/// The index was not available.
97-
InUse,
98-
/// The source and destination accounts are identical.
99-
NotTransfer,
100-
/// The index is permanent and may not be freed/changed.
101-
Permanent,
64+
/// Weight information for extrinsics in this pallet.
65+
type WeightInfo: WeightInfo;
10266
}
103-
}
10467

105-
decl_module! {
106-
pub struct Module<T: Config> for enum Call where origin: T::Origin, system = frame_system {
107-
/// The deposit needed for reserving an index.
108-
const Deposit: BalanceOf<T> = T::Deposit::get();
68+
#[pallet::pallet]
69+
#[pallet::generate_store(pub(super) trait Store)]
70+
pub struct Pallet<T>(PhantomData<T>);
10971

110-
fn deposit_event() = default;
72+
#[pallet::hooks]
73+
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
11174

75+
#[pallet::call]
76+
impl<T: Config> Pallet<T> {
11277
/// Assign an previously unassigned index.
11378
///
11479
/// Payment: `Deposit` is reserved from the sender account.
@@ -127,16 +92,17 @@ decl_module! {
12792
/// -------------------
12893
/// - DB Weight: 1 Read/Write (Accounts)
12994
/// # </weight>
130-
#[weight = T::WeightInfo::claim()]
131-
fn claim(origin, index: T::AccountIndex) {
95+
#[pallet::weight(T::WeightInfo::claim())]
96+
pub(crate) fn claim(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
13297
let who = ensure_signed(origin)?;
13398

13499
Accounts::<T>::try_mutate(index, |maybe_value| {
135100
ensure!(maybe_value.is_none(), Error::<T>::InUse);
136101
*maybe_value = Some((who.clone(), T::Deposit::get(), false));
137102
T::Currency::reserve(&who, T::Deposit::get())
138103
})?;
139-
Self::deposit_event(RawEvent::IndexAssigned(who, index));
104+
Self::deposit_event(Event::IndexAssigned(who, index));
105+
Ok(())
140106
}
141107

142108
/// Assign an index already owned by the sender to another account. The balance reservation
@@ -159,8 +125,12 @@ decl_module! {
159125
/// - Reads: Indices Accounts, System Account (recipient)
160126
/// - Writes: Indices Accounts, System Account (recipient)
161127
/// # </weight>
162-
#[weight = T::WeightInfo::transfer()]
163-
fn transfer(origin, new: T::AccountId, index: T::AccountIndex) {
128+
#[pallet::weight(T::WeightInfo::transfer())]
129+
pub(crate) fn transfer(
130+
origin: OriginFor<T>,
131+
new: T::AccountId,
132+
index: T::AccountIndex,
133+
) -> DispatchResult {
164134
let who = ensure_signed(origin)?;
165135
ensure!(who != new, Error::<T>::NotTransfer);
166136

@@ -172,7 +142,8 @@ decl_module! {
172142
*maybe_value = Some((new.clone(), amount.saturating_sub(lost), false));
173143
Ok(())
174144
})?;
175-
Self::deposit_event(RawEvent::IndexAssigned(new, index));
145+
Self::deposit_event(Event::IndexAssigned(new, index));
146+
Ok(())
176147
}
177148

178149
/// Free up an index owned by the sender.
@@ -193,8 +164,8 @@ decl_module! {
193164
/// -------------------
194165
/// - DB Weight: 1 Read/Write (Accounts)
195166
/// # </weight>
196-
#[weight = T::WeightInfo::free()]
197-
fn free(origin, index: T::AccountIndex) {
167+
#[pallet::weight(T::WeightInfo::free())]
168+
pub(crate) fn free(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
198169
let who = ensure_signed(origin)?;
199170

200171
Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
@@ -204,7 +175,8 @@ decl_module! {
204175
T::Currency::unreserve(&who, amount);
205176
Ok(())
206177
})?;
207-
Self::deposit_event(RawEvent::IndexFreed(index));
178+
Self::deposit_event(Event::IndexFreed(index));
179+
Ok(())
208180
}
209181

210182
/// Force an index to an account. This doesn't require a deposit. If the index is already
@@ -228,8 +200,13 @@ decl_module! {
228200
/// - Reads: Indices Accounts, System Account (original owner)
229201
/// - Writes: Indices Accounts, System Account (original owner)
230202
/// # </weight>
231-
#[weight = T::WeightInfo::force_transfer()]
232-
fn force_transfer(origin, new: T::AccountId, index: T::AccountIndex, freeze: bool) {
203+
#[pallet::weight(T::WeightInfo::force_transfer())]
204+
pub(crate) fn force_transfer(
205+
origin: OriginFor<T>,
206+
new: T::AccountId,
207+
index: T::AccountIndex,
208+
freeze: bool,
209+
) -> DispatchResult {
233210
ensure_root(origin)?;
234211

235212
Accounts::<T>::mutate(index, |maybe_value| {
@@ -238,7 +215,8 @@ decl_module! {
238215
}
239216
*maybe_value = Some((new.clone(), Zero::zero(), freeze));
240217
});
241-
Self::deposit_event(RawEvent::IndexAssigned(new, index));
218+
Self::deposit_event(Event::IndexAssigned(new, index));
219+
Ok(())
242220
}
243221

244222
/// Freeze an index so it will always point to the sender account. This consumes the deposit.
@@ -258,8 +236,8 @@ decl_module! {
258236
/// -------------------
259237
/// - DB Weight: 1 Read/Write (Accounts)
260238
/// # </weight>
261-
#[weight = T::WeightInfo::freeze()]
262-
fn freeze(origin, index: T::AccountIndex) {
239+
#[pallet::weight(T::WeightInfo::freeze())]
240+
pub(crate) fn freeze(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
263241
let who = ensure_signed(origin)?;
264242

265243
Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
@@ -270,12 +248,74 @@ decl_module! {
270248
*maybe_value = Some((account, Zero::zero(), true));
271249
Ok(())
272250
})?;
273-
Self::deposit_event(RawEvent::IndexFrozen(index, who));
251+
Self::deposit_event(Event::IndexFrozen(index, who));
252+
Ok(())
253+
}
254+
}
255+
256+
#[pallet::event]
257+
#[pallet::generate_deposit(pub(super) fn deposit_event)]
258+
#[pallet::metadata(T::AccountId = "AccountId", T::AccountIndex = "AccountIndex")]
259+
pub enum Event<T: Config> {
260+
/// A account index was assigned. \[index, who\]
261+
IndexAssigned(T::AccountId, T::AccountIndex),
262+
/// A account index has been freed up (unassigned). \[index\]
263+
IndexFreed(T::AccountIndex),
264+
/// A account index has been frozen to its current account ID. \[index, who\]
265+
IndexFrozen(T::AccountIndex, T::AccountId),
266+
}
267+
268+
/// Old name generated by `decl_event`.
269+
#[deprecated(note="use `Event` instead")]
270+
pub type RawEvent<T> = Event<T>;
271+
272+
#[pallet::error]
273+
pub enum Error<T> {
274+
/// The index was not already assigned.
275+
NotAssigned,
276+
/// The index is assigned to another account.
277+
NotOwner,
278+
/// The index was not available.
279+
InUse,
280+
/// The source and destination accounts are identical.
281+
NotTransfer,
282+
/// The index is permanent and may not be freed/changed.
283+
Permanent,
284+
}
285+
286+
/// The lookup from index to account.
287+
#[pallet::storage]
288+
pub type Accounts<T: Config> = StorageMap<
289+
_, Blake2_128Concat,
290+
T::AccountIndex,
291+
(T::AccountId, BalanceOf<T>, bool)
292+
>;
293+
294+
#[pallet::genesis_config]
295+
pub struct GenesisConfig<T: Config> {
296+
pub indices: Vec<(T::AccountIndex, T::AccountId)>,
297+
}
298+
299+
#[cfg(feature = "std")]
300+
impl<T: Config> Default for GenesisConfig<T> {
301+
fn default() -> Self {
302+
Self {
303+
indices: Default::default(),
304+
}
305+
}
306+
}
307+
308+
#[pallet::genesis_build]
309+
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
310+
fn build(&self) {
311+
for (a, b) in &self.indices {
312+
<Accounts<T>>::insert(a, (b, <BalanceOf<T>>::zero(), false))
313+
}
274314
}
275315
}
276316
}
277317

278-
impl<T: Config> Module<T> {
318+
impl<T: Config> Pallet<T> {
279319
// PUBLIC IMMUTABLES
280320

281321
/// Lookup an T::AccountIndex to get an Id, if there's one there.
@@ -295,7 +335,7 @@ impl<T: Config> Module<T> {
295335
}
296336
}
297337

298-
impl<T: Config> StaticLookup for Module<T> {
338+
impl<T: Config> StaticLookup for Pallet<T> {
299339
type Source = MultiAddress<T::AccountId, T::AccountIndex>;
300340
type Target = T::AccountId;
301341

0 commit comments

Comments
 (0)