userbook.gno
1.03 Kb ยท 56 lines
1// Package userbook demonstrates a small userbook system working with gnoweb
2package userbook
3
4import (
5 "std"
6 "time"
7
8 "gno.land/p/demo/avl"
9 "gno.land/p/demo/seqid"
10 "gno.land/p/demo/ufmt"
11)
12
13type Signup struct {
14 address std.Address
15 ordinal int
16 timestamp time.Time
17}
18
19var (
20 signupsTree = avl.NewTree()
21 tracker = avl.NewTree()
22 idCounter seqid.ID
23)
24
25const signUpEvent = "SignUp"
26
27func init() {
28 cross(SignUp)() // Sign up the deployer
29}
30
31func SignUp() string {
32 crossing()
33
34 // Get transaction caller
35 caller := std.PreviousRealm().Address()
36
37 // Check if the user is already signed up
38 if _, exists := tracker.Get(caller.String()); exists {
39 panic(caller.String() + " is already signed up!")
40 }
41
42 now := time.Now()
43
44 // Sign up the user
45 signupsTree.Set(idCounter.Next().String(), &Signup{
46 caller,
47 signupsTree.Size(),
48 now,
49 })
50
51 tracker.Set(caller.String(), struct{}{})
52
53 std.Emit(signUpEvent, "account", caller.String())
54
55 return ufmt.Sprintf("%s added to userbook! Timestamp: %s", caller.String(), now.Format(time.RFC822Z))
56}