-
Notifications
You must be signed in to change notification settings - Fork 382
/
foo20.gno
129 lines (107 loc) · 2.51 KB
/
foo20.gno
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package foo20
import (
"std"
"strings"
"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ufmt"
"gno.land/r/demo/users"
)
var (
foo *grc20.AdminToken
admin std.Address = "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj" // TODO: helper to change admin
)
func init() {
foo = grc20.NewAdminToken("Foo", "FOO", 4)
foo.Mint(admin, 1000000*10000) // @administrator (1M)
foo.Mint("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq", 10000*10000) // @manfred (10k)
}
// method proxies as public functions.
//
// getters.
func TotalSupply() uint64 {
return foo.TotalSupply()
}
func BalanceOf(owner users.AddressOrName) uint64 {
balance, err := foo.BalanceOf(owner.Resolve())
if err != nil {
panic(err)
}
return balance
}
func Allowance(owner, spender users.AddressOrName) uint64 {
allowance, err := foo.Allowance(owner.Resolve(), spender.Resolve())
if err != nil {
panic(err)
}
return allowance
}
// setters.
func Transfer(to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := foo.Transfer(caller, to.Resolve(), amount)
if err != nil {
panic(err)
}
}
func Approve(spender users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := foo.Approve(caller, spender.Resolve(), amount)
if err != nil {
panic(err)
}
}
func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := foo.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
if err != nil {
panic(err)
}
}
// faucet.
func Faucet() {
// FIXME: add limits?
// FIXME: add payment in gnot?
caller := std.PrevRealm().Addr()
err := foo.Mint(caller, 1000*10000) // 1k
if err != nil {
panic(err)
}
}
// administration.
func Mint(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
err := foo.Mint(address.Resolve(), amount)
if err != nil {
panic(err)
}
}
func Burn(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
err := foo.Burn(address.Resolve(), amount)
if err != nil {
panic(err)
}
}
// render.
//
func Render(path string) string {
parts := strings.Split(path, "/")
c := len(parts)
switch {
case path == "":
return foo.RenderHome()
case c == 2 && parts[0] == "balance":
owner := users.AddressOrName(parts[1])
balance, _ := foo.BalanceOf(owner.Resolve())
return ufmt.Sprintf("%d\n", balance)
default:
return "404\n"
}
}
func assertIsAdmin(address std.Address) {
if address != admin {
panic("restricted access")
}
}