1#![cfg_attr(not(feature = "std"), no_std)]
22
23mod benchmarking;
24mod mock;
25mod tests;
26pub mod weights;
27
28extern crate alloc;
29
30use alloc::vec::Vec;
31use codec::Codec;
32use frame_support::traits::{BalanceStatus::Reserved, Currency, ReservableCurrency};
33use sp_runtime::{
34 traits::{AtLeast32Bit, LookupError, Saturating, StaticLookup, Zero},
35 MultiAddress,
36};
37pub use weights::WeightInfo;
38
39type BalanceOf<T> =
40 <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
41type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
42
43pub use pallet::*;
44
45#[frame_support::pallet]
46pub mod pallet {
47 use super::*;
48 use frame_support::pallet_prelude::*;
49 use frame_system::pallet_prelude::*;
50
51 #[pallet::config]
53 pub trait Config: frame_system::Config {
54 type AccountIndex: Parameter
57 + Member
58 + MaybeSerializeDeserialize
59 + Codec
60 + Default
61 + AtLeast32Bit
62 + Copy
63 + MaxEncodedLen;
64
65 type Currency: ReservableCurrency<Self::AccountId>;
67
68 #[pallet::constant]
70 type Deposit: Get<BalanceOf<Self>>;
71
72 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
74
75 type WeightInfo: WeightInfo;
77 }
78
79 #[pallet::pallet]
80 pub struct Pallet<T>(_);
81
82 #[pallet::call]
83 impl<T: Config> Pallet<T> {
84 #[pallet::call_index(0)]
97 #[pallet::weight(T::WeightInfo::claim())]
98 pub fn claim(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
99 let who = ensure_signed(origin)?;
100
101 Accounts::<T>::try_mutate(index, |maybe_value| {
102 ensure!(maybe_value.is_none(), Error::<T>::InUse);
103 *maybe_value = Some((who.clone(), T::Deposit::get(), false));
104 T::Currency::reserve(&who, T::Deposit::get())
105 })?;
106 Self::deposit_event(Event::IndexAssigned { who, index });
107 Ok(())
108 }
109
110 #[pallet::call_index(1)]
123 #[pallet::weight(T::WeightInfo::transfer())]
124 pub fn transfer(
125 origin: OriginFor<T>,
126 new: AccountIdLookupOf<T>,
127 index: T::AccountIndex,
128 ) -> DispatchResult {
129 let who = ensure_signed(origin)?;
130 let new = T::Lookup::lookup(new)?;
131 ensure!(who != new, Error::<T>::NotTransfer);
132
133 Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
134 let (account, amount, perm) = maybe_value.take().ok_or(Error::<T>::NotAssigned)?;
135 ensure!(!perm, Error::<T>::Permanent);
136 ensure!(account == who, Error::<T>::NotOwner);
137 let lost = T::Currency::repatriate_reserved(&who, &new, amount, Reserved)?;
138 *maybe_value = Some((new.clone(), amount.saturating_sub(lost), false));
139 Ok(())
140 })?;
141 Self::deposit_event(Event::IndexAssigned { who: new, index });
142 Ok(())
143 }
144
145 #[pallet::call_index(2)]
158 #[pallet::weight(T::WeightInfo::free())]
159 pub fn free(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
160 let who = ensure_signed(origin)?;
161
162 Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
163 let (account, amount, perm) = maybe_value.take().ok_or(Error::<T>::NotAssigned)?;
164 ensure!(!perm, Error::<T>::Permanent);
165 ensure!(account == who, Error::<T>::NotOwner);
166 T::Currency::unreserve(&who, amount);
167 Ok(())
168 })?;
169 Self::deposit_event(Event::IndexFreed { index });
170 Ok(())
171 }
172
173 #[pallet::call_index(3)]
187 #[pallet::weight(T::WeightInfo::force_transfer())]
188 pub fn force_transfer(
189 origin: OriginFor<T>,
190 new: AccountIdLookupOf<T>,
191 index: T::AccountIndex,
192 freeze: bool,
193 ) -> DispatchResult {
194 ensure_root(origin)?;
195 let new = T::Lookup::lookup(new)?;
196
197 Accounts::<T>::mutate(index, |maybe_value| {
198 if let Some((account, amount, _)) = maybe_value.take() {
199 T::Currency::unreserve(&account, amount);
200 }
201 *maybe_value = Some((new.clone(), Zero::zero(), freeze));
202 });
203 Self::deposit_event(Event::IndexAssigned { who: new, index });
204 Ok(())
205 }
206
207 #[pallet::call_index(4)]
220 #[pallet::weight(T::WeightInfo::freeze())]
221 pub fn freeze(origin: OriginFor<T>, index: T::AccountIndex) -> DispatchResult {
222 let who = ensure_signed(origin)?;
223
224 Accounts::<T>::try_mutate(index, |maybe_value| -> DispatchResult {
225 let (account, amount, perm) = maybe_value.take().ok_or(Error::<T>::NotAssigned)?;
226 ensure!(!perm, Error::<T>::Permanent);
227 ensure!(account == who, Error::<T>::NotOwner);
228 let _ = T::Currency::slash_reserved(&who, amount);
229 *maybe_value = Some((account, Zero::zero(), true));
230 Ok(())
231 })?;
232 Self::deposit_event(Event::IndexFrozen { index, who });
233 Ok(())
234 }
235 }
236
237 #[pallet::event]
238 #[pallet::generate_deposit(pub(super) fn deposit_event)]
239 pub enum Event<T: Config> {
240 IndexAssigned { who: T::AccountId, index: T::AccountIndex },
242 IndexFreed { index: T::AccountIndex },
244 IndexFrozen { index: T::AccountIndex, who: T::AccountId },
246 }
247
248 #[pallet::error]
249 pub enum Error<T> {
250 NotAssigned,
252 NotOwner,
254 InUse,
256 NotTransfer,
258 Permanent,
260 }
261
262 #[pallet::storage]
264 pub type Accounts<T: Config> =
265 StorageMap<_, Blake2_128Concat, T::AccountIndex, (T::AccountId, BalanceOf<T>, bool)>;
266
267 #[pallet::genesis_config]
268 #[derive(frame_support::DefaultNoBound)]
269 pub struct GenesisConfig<T: Config> {
270 pub indices: Vec<(T::AccountIndex, T::AccountId)>,
271 }
272
273 #[pallet::genesis_build]
274 impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
275 fn build(&self) {
276 for (a, b) in &self.indices {
277 <Accounts<T>>::insert(a, (b, <BalanceOf<T>>::zero(), false))
278 }
279 }
280 }
281}
282
283impl<T: Config> Pallet<T> {
284 pub fn lookup_index(index: T::AccountIndex) -> Option<T::AccountId> {
288 Accounts::<T>::get(index).map(|x| x.0)
289 }
290
291 pub fn lookup_address(a: MultiAddress<T::AccountId, T::AccountIndex>) -> Option<T::AccountId> {
293 match a {
294 MultiAddress::Id(i) => Some(i),
295 MultiAddress::Index(i) => Self::lookup_index(i),
296 _ => None,
297 }
298 }
299}
300
301impl<T: Config> StaticLookup for Pallet<T> {
302 type Source = MultiAddress<T::AccountId, T::AccountIndex>;
303 type Target = T::AccountId;
304
305 fn lookup(a: Self::Source) -> Result<Self::Target, LookupError> {
306 Self::lookup_address(a).ok_or(LookupError)
307 }
308
309 fn unlookup(a: Self::Target) -> Self::Source {
310 MultiAddress::Id(a)
311 }
312}