-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy path2.4-finished-code.rs
More file actions
96 lines (73 loc) · 3.12 KB
/
2.4-finished-code.rs
File metadata and controls
96 lines (73 loc) · 3.12 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
use parity_codec::Encode;
use support::{decl_storage, decl_module, StorageValue, StorageMap,
dispatch::Result, ensure, decl_event};
use system::ensure_signed;
use runtime_primitives::traits::{As, Hash};
use parity_codec_derive::{Encode, Decode};
#[derive(Encode, Decode, Default, Clone, PartialEq)]
pub struct Kitty<Hash, Balance> {
id: Hash,
dna: Hash,
price: Balance,
gen: u64,
}
pub trait Trait: balances::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
decl_event!(
pub enum Event<T>
where
<T as system::Trait>::AccountId,
<T as system::Trait>::Hash
{
Created(AccountId, Hash),
}
);
decl_storage! {
trait Store for Module<T: Trait> as KittyStorage {
Kitties get(kitty): map T::Hash => Kitty<T::Hash, T::Balance>;
KittyOwner get(owner_of): map T::Hash => Option<T::AccountId>;
AllKittiesArray get(kitty_by_index): map u64 => T::Hash;
AllKittiesCount get(all_kitties_count): u64;
AllKittiesIndex: map T::Hash => u64;
OwnedKittiesArray get(kitty_of_owner_by_index): map (T::AccountId, u64) => T::Hash;
OwnedKittiesCount get(owned_kitty_count): map T::AccountId => u64;
OwnedKittiesIndex: map T::Hash => u64;
Nonce: u64;
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default;
fn create_kitty(origin) -> Result {
let sender = ensure_signed(origin)?;
let owned_kitty_count = Self::owned_kitty_count(&sender);
let new_owned_kitty_count = owned_kitty_count.checked_add(1)
.ok_or("Overflow adding a new kitty to account balance")?;
let all_kitties_count = Self::all_kitties_count();
let new_all_kitties_count = all_kitties_count.checked_add(1)
.ok_or("Overflow adding a new kitty to total supply")?;
let nonce = <Nonce<T>>::get();
let random_hash = (<system::Module<T>>::random_seed(), &sender, nonce)
.using_encoded(<T as system::Trait>::Hashing::hash);
ensure!(!<KittyOwner<T>>::exists(random_hash), "Kitty already exists");
let new_kitty = Kitty {
id: random_hash,
dna: random_hash,
price: <T::Balance as As<u64>>::sa(0),
gen: 0,
};
<Kitties<T>>::insert(random_hash, new_kitty);
<KittyOwner<T>>::insert(random_hash, &sender);
<AllKittiesArray<T>>::insert(all_kitties_count, random_hash);
<AllKittiesCount<T>>::put(new_all_kitties_count);
<AllKittiesIndex<T>>::insert(random_hash, all_kitties_count);
<OwnedKittiesArray<T>>::insert((sender.clone(), owned_kitty_count), random_hash);
<OwnedKittiesCount<T>>::insert(&sender, new_owned_kitty_count);
<OwnedKittiesIndex<T>>::insert(random_hash, owned_kitty_count);
<Nonce<T>>::mutate(|n| *n += 1);
Self::deposit_event(RawEvent::Created(sender, random_hash));
Ok(())
}
}
}