foo20.gno
2.35 Kb · 91 lines
1// foo20 is a GRC20 token contract where all the grc20.Teller methods are
2// proxified with top-level functions. see also gno.land/r/demo/bar20.
3package foo20
4
5import (
6 "strings"
7
8 "gno.land/p/demo/tokens/grc20"
9 "gno.land/p/nt/ownable/v0"
10 "gno.land/p/nt/ufmt/v0"
11 "gno.land/r/demo/defi/grc20reg"
12)
13
14var (
15 Token *grc20.Token
16 privateLedger *grc20.PrivateLedger
17 userTeller grc20.Teller
18 Ownable = ownable.NewWithAddress("g1rp7cmetn27eqlpjpc4vuusf8kaj746tysc0qgh") // govdao t1 multisig
19)
20
21func init(cur realm) {
22 // foo20 only ever creates this one token, so id 0 can't collide.
23 Token, privateLedger = grc20.NewToken("Foo", "FOO", 4, 0, cur)
24 userTeller = Token.CallerTeller()
25 privateLedger.Mint(Ownable.Owner(), 1_000_000*10_000) // @privateLedgeristrator (1M)
26 grc20reg.Register(cross(cur), Token, "")
27}
28
29func TotalSupply() int64 {
30 return userTeller.TotalSupply()
31}
32
33func BalanceOf(owner address) int64 {
34 return userTeller.BalanceOf(owner)
35}
36
37func Allowance(owner, spender address) int64 {
38 return userTeller.Allowance(owner, spender)
39}
40
41func Transfer(cur realm, to address, amount int64) {
42 checkErr(userTeller.Transfer(0, cur, to, amount))
43}
44
45func Approve(cur realm, spender address, amount int64) {
46 checkErr(userTeller.Approve(0, cur, spender, amount))
47}
48
49func TransferFrom(cur realm, from, to address, amount int64) {
50 checkErr(userTeller.TransferFrom(0, cur, from, to, amount))
51}
52
53// Faucet is distributing foo20 tokens without restriction (unsafe).
54// For a real token faucet, you should take care of setting limits are asking payment.
55func Faucet(cur realm) {
56 caller := cur.Previous().Address()
57 amount := int64(1_000 * 10_000) // 1k
58 checkErr(privateLedger.Mint(caller, amount))
59}
60
61func Mint(cur realm, to address, amount int64) {
62 Ownable.AssertOwnedBy(cur.Previous().Address())
63 checkErr(privateLedger.Mint(to, amount))
64}
65
66func Burn(cur realm, from address, amount int64) {
67 Ownable.AssertOwnedBy(cur.Previous().Address())
68 checkErr(privateLedger.Burn(from, amount))
69}
70
71func Render(path string) string {
72 parts := strings.Split(path, "/")
73 c := len(parts)
74
75 switch {
76 case path == "":
77 return Token.RenderHome()
78 case c == 2 && parts[0] == "balance":
79 owner := address(parts[1])
80 balance := userTeller.BalanceOf(owner)
81 return ufmt.Sprintf("%d\n", balance)
82 default:
83 return "404\n"
84 }
85}
86
87func checkErr(err error) {
88 if err != nil {
89 panic(err.Error())
90 }
91}