faucet.gno

2.26 Kb ยท 91 lines
 1package faucet
 2
 3import (
 4	"errors"
 5	"std"
 6
 7	"gno.land/p/demo/avl"
 8	"gno.land/p/demo/ufmt"
 9)
10
11var (
12	// configurable by admin.
13	gAdminAddr          std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
14	gControllers                    = avl.NewTree()
15	gControllersMaxSize             = 10 // limit it to 10
16	gInPause                        = false
17	gMessage                        = "# Community Faucet.\n\n"
18
19	// internal vars, for stats.
20	gTotalTransferred std.Coins
21	gTotalTransfers   = uint(0)
22
23	// per request limit, 350 gnot
24	gLimit std.Coin = std.NewCoin("ugnot", 350000000)
25)
26
27func Transfer(to std.Address, send int64) string {
28	if err := assertIsController(); err != nil {
29		return err.Error()
30	}
31
32	if gInPause {
33		return errors.New("faucet in pause").Error()
34	}
35
36	// limit the per request
37	if send > gLimit.Amount {
38		return errors.New("Per request limit " + gLimit.String() + " exceed").Error()
39	}
40	sendCoins := std.Coins{std.NewCoin("ugnot", send)}
41
42	gTotalTransferred = gTotalTransferred.Add(sendCoins)
43	gTotalTransfers++
44
45	banker := std.GetBanker(std.BankerTypeRealmSend)
46	pkgaddr := std.CurrentRealm().Addr()
47	banker.SendCoins(pkgaddr, to, sendCoins)
48	return ""
49}
50
51func GetPerTransferLimit() int64 {
52	return gLimit.Amount
53}
54
55func Render(_ string) string {
56	banker := std.GetBanker(std.BankerTypeRealmSend)
57	balance := banker.GetCoins(std.CurrentRealm().Addr())
58
59	output := gMessage
60	if gInPause {
61		output += "Status: inactive.\n"
62	} else {
63		output += "Status: active.\n"
64	}
65	output += ufmt.Sprintf("Balance: %s.\n", balance.String())
66	output += ufmt.Sprintf("Total transfers: %s (in %d times).\n\n", gTotalTransferred.String(), gTotalTransfers)
67
68	output += "Package address: " + std.CurrentRealm().Addr().String() + "\n\n"
69	output += ufmt.Sprintf("Admin: %s\n\n ", gAdminAddr.String())
70	output += ufmt.Sprintf("Controllers:\n\n ")
71
72	for i := 0; i < gControllers.Size(); i++ {
73		_, v := gControllers.GetByIndex(i)
74		output += ufmt.Sprintf("%s  ", v.(std.Address))
75	}
76
77	output += "\n\n"
78	output += ufmt.Sprintf("Per request limit: %s\n\n", gLimit.String())
79
80	return output
81}
82
83func assertIsController() error {
84	caller := std.GetOrigCaller()
85
86	ok := gControllers.Has(caller.String())
87	if !ok {
88		return errors.New(caller.String() + " is not on the controller list")
89	}
90	return nil
91}