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