Search Apps Documentation Source Content File Folder Download Copy

userbook.gno

1.00 Kb ยท 54 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	SignUp() // Sign up the deployer
29}
30
31func SignUp() string {
32	// Get transaction caller
33	caller := std.PrevRealm().Addr()
34
35	// Check if the user is already signed up
36	if _, exists := tracker.Get(caller.String()); exists {
37		panic(caller.String() + " is already signed up!")
38	}
39
40	now := time.Now()
41
42	// Sign up the user
43	signupsTree.Set(idCounter.Next().String(), &Signup{
44		caller,
45		signupsTree.Size(),
46		now,
47	})
48
49	tracker.Set(caller.String(), struct{}{})
50
51	std.Emit(signUpEvent, "account", caller.String())
52
53	return ufmt.Sprintf("%s added to userbook! Timestamp: %s", caller.String(), now.Format(time.RFC822Z))
54}