-
Notifications
You must be signed in to change notification settings - Fork 36
/
lib.rs
84 lines (68 loc) · 1.9 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#![cfg_attr(not(feature = "std"), no_std)]
/// A FRAME pallet for genesis config
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use frame_support::{pallet_prelude::*, Blake2_128Concat};
use frame_system::pallet_prelude::*;
use sp_std::vec::Vec;
#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
// genesis storage with config()
#[pallet::storage]
#[pallet::getter(fn something)]
pub type Something<T> = StorageValue<_, u32>;
#[pallet::storage]
#[pallet::getter(fn some_map)]
pub type SomeMap<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, u32>;
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub something: Option<u32>,
pub some_map: Vec<(T::AccountId, u32)>,
}
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self {
something: None,
some_map: vec![],
}
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
if let Some(ref something) = self.something {
Something::<T>::put(something);
}
for (who, value) in self.some_map.iter() {
SomeMap::<T>::insert(who, value);
}
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
SomethingStored(u32, T::AccountId),
}
#[pallet::error]
pub enum Error<T> {
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(0)]
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
let who = ensure_signed(origin)?;
Something::<T>::put(something);
Self::deposit_event(Event::SomethingStored(something, who));
Ok(())
}
}
}