-
Notifications
You must be signed in to change notification settings - Fork 8
/
lib.rs
74 lines (61 loc) · 1.95 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
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Encode, Decode};
use frame_support::{
decl_module, decl_storage, decl_event, decl_error, StorageValue, StorageDoubleMap,
traits::Randomness, RuntimeDebug,
};
use sp_io::hashing::blake2_128;
use frame_system::ensure_signed;
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
pub struct Kitty(pub [u8; 16]);
pub trait Trait: frame_system::Trait {
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
}
decl_storage! {
trait Store for Module<T: Trait> as Kitties {
/// Stores all the kitties, key is the kitty id
pub Kitties get(fn kitties): double_map hasher(blake2_128_concat) T::AccountId, hasher(blake2_128_concat) u32 => Option<Kitty>;
/// Stores the next kitty ID
pub NextKittyId get(fn next_kitty_id): u32;
}
}
decl_event! {
pub enum Event<T> where
<T as frame_system::Trait>::AccountId,
{
/// A kitty is created. \[owner, kitty_id, kitty\]
KittyCreated(AccountId, u32, Kitty),
}
}
decl_error! {
pub enum Error for Module<T: Trait> {
KittiesIdOverflow,
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
fn deposit_event() = default;
/// Create a new kitty
#[weight = 1000]
pub fn create(origin) {
let sender = ensure_signed(origin)?;
// TODO: ensure kitty id does not overflow
// return Err(Error::<T>::KittiesIdOverflow.into());
// Generate a random 128bit value
let payload = (
<pallet_randomness_collective_flip::Module<T> as Randomness<T::Hash>>::random_seed(),
&sender,
<frame_system::Module<T>>::extrinsic_index(),
);
let dna = payload.using_encoded(blake2_128);
// Create and store kitty
let kitty = Kitty(dna);
let kitty_id = Self::next_kitty_id();
Kitties::<T>::insert(&sender, kitty_id, kitty.clone());
NextKittyId::put(kitty_id + 1);
// Emit event
Self::deposit_event(RawEvent::KittyCreated(sender, kitty_id, kitty))
}
}
}