-
Notifications
You must be signed in to change notification settings - Fork 352
/
pallet-bad-origin.rs
61 lines (49 loc) · 1.69 KB
/
pallet-bad-origin.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
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::{*, ValueQuery}};
use frame_system::pallet_prelude::*;
use sp_std::prelude::*;
/// Pallet configuration
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
// Specifies the account that can perform some action
type ForceOrigin: EnsureOrigin<Self::Origin>;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
/// Storage item for important value that should be editable only by root
#[pallet::storage]
#[pallet::getter(fn get_important_val)]
pub(super) type ImportantVal<T: Config> = StorageValue<_, u64, ValueQuery>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Emit when new important val is set.
ImportantValSet(T::AccountId, u64),
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T:Config> Pallet<T> {
/// Set the important val
/// Should be only callable by ForceOrigin
#[pallet::weight(10_000)]
pub fn set_important_val(
origin: OriginFor<T>,
new_val: u64
) -> DispatchResultWithPostInfo {
T::ForceOrigin::ensure_origin(origin.clone())?;
let sender = ensure_signed(origin)?;
// Change to new value
<ImportantVal<T>>::put(new_val);
// Emit event
Self::deposit_event(Event::ImportantValSet(sender, new_val));
Ok(().into())
}
}
}