-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathpallet-bad-lottery.rs
82 lines (69 loc) · 2.24 KB
/
pallet-bad-lottery.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
#![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 frame_support::traits::Randomness;
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>;
/// Create a randomness type for this pallet
type MyRandomness: Randomness<Self::Hash, Self::BlockNumber>;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
/// Storage item for nonce
#[pallet::storage]
#[pallet::getter(fn get_nonce)]
pub(super) type Nonce<T: Config> = StorageValue<_, u64, ValueQuery>;
/// Storage item for current winner
#[pallet::storage]
#[pallet::getter(fn get_winner)]
pub(super) type Winner<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Emit when new winner is found
NewWinner(T::AccountId),
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::error]
pub enum Error<T> {
/// Guessed the wrong number.
IncorrectGuess,
}
#[pallet::call]
impl<T:Config> Pallet<T> {
/// Guess the random value
/// If you guess correctly, you become the winner
#[pallet::weight(10_000)]
pub fn guess(
origin: OriginFor<T>,
guess: T::Hash
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
// Random value.
let nonce = Self::get_and_increment_nonce();
let (random_value, _) = T::MyRandomness::random(&nonce);
// Check if guess is correct
ensure!(guess == random_value, <Error<T>>::IncorrectGuess);
<Winner<T>>::put(&sender);
Self::deposit_event(Event::NewWinner(sender));
Ok(().into())
}
}
impl<T:Config> Pallet<T> {
/// Increment the nonce each time guess() is called
pub fn get_and_increment_nonce() -> Vec<u8> {
let nonce = Nonce::<T>::get();
Nonce::<T>::put(nonce.wrapping_add(1));
nonce.encode()
}
}
}