-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCryptoHamster.aes
45 lines (34 loc) · 1.41 KB
/
CryptoHamster.aes
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
@compiler >= 6
contract CryptoHamster =
record state =
{ index : int
, map_hamsters : map(string, hamster) }
record hamster =
{ id : int
, name : string
, dna : int }
stateful entrypoint init() =
{ index = 1,
map_hamsters = {} }
stateful entrypoint create_hamster(hamster_name: string) =
require(!name_exists(hamster_name), "Name is already taken")
let dna : int = generate_dna(hamster_name)
create_hamster_by_name_dna(hamster_name, dna)
entrypoint name_exists(name: string) : bool =
Map.member(name, state.map_hamsters)
entrypoint get_hamster_dna(name: string) : int =
require(name_exists(name), "There is no hamster with that name!")
state.map_hamsters[name].dna
stateful function create_hamster_by_name_dna(name: string, dna: int) =
let new_hamster : hamster =
{ id = state.index,
name = name,
dna = dna }
put(state{map_hamsters[name] = new_hamster})
put(state{index = (state.index + 1)})
function generate_dna(name: string) : int =
get_block_hash_bytes_as_int() - Chain.timestamp + state.index
function get_block_hash_bytes_as_int() : int =
switch(Chain.block_hash(Chain.block_height))
None => abort("blockhash not found")
Some(bytes) => Bytes.to_int(bytes)