-
Notifications
You must be signed in to change notification settings - Fork 352
/
pallet-dont-panic.rs
68 lines (56 loc) · 1.8 KB
/
pallet-dont-panic.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
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
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>;
}
#[pallet::pallet]
#[pallet::without_storage_info]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
/// Storage item for holding an ImportantValue
#[pallet::storage]
#[pallet::getter(fn get_val)]
pub(super) type ImportantValue<T: Config> = StorageValue<_, u64, ValueQuery>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Emit after val is found
FoundVal(T::AccountId, u64),
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::error]
pub enum Error<T> {
NoImportantValueFound,
}
#[pallet::call]
impl<T:Config> Pallet<T> {
/// Find important value
///
/// Parameters:
/// - `useful_amounts`: A vector of u64 values in which there is a important value.
///
/// Emits `FoundVal` event when successful.
#[pallet::weight(10_000)]
pub fn find_important_value(
origin: OriginFor<T>,
useful_amounts: Vec<u64>,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
ensure!(useful_amounts[0] > 1_000, <Error<T>>::NoImportantValueFound);
// Found the important value
ImportantValue::<T>::put(&useful_amounts[0]);
// Emit event
Self::deposit_event(Event::FoundVal(sender, useful_amounts[0]));
Ok(().into())
}
}
}