foo20.gno
2.18 Kb ยท 100 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 "std"
7 "strings"
8
9 "gno.land/p/demo/grc/grc20"
10 "gno.land/p/demo/ownable"
11 "gno.land/p/demo/ufmt"
12 "gno.land/r/demo/grc20reg"
13)
14
15var (
16 Token, privateLedger = grc20.NewToken("Foo", "FOO", 4)
17 UserTeller = Token.CallerTeller()
18 Ownable = ownable.NewWithAddress("g1manfred47kzduec920z88wfr64ylksmdcedlf5") // @manfred
19)
20
21func init() {
22 privateLedger.Mint(Ownable.Owner(), 1_000_000*10_000) // @privateLedgeristrator (1M)
23 cross(grc20reg.Register)(Token.Getter(), "")
24}
25
26func TotalSupply() uint64 {
27 return UserTeller.TotalSupply()
28}
29
30func BalanceOf(owner std.Address) uint64 {
31 return UserTeller.BalanceOf(owner)
32}
33
34func Allowance(owner, spender std.Address) uint64 {
35 return UserTeller.Allowance(owner, spender)
36}
37
38func Transfer(to std.Address, amount uint64) {
39 crossing()
40
41 checkErr(UserTeller.Transfer(to, amount))
42}
43
44func Approve(spender std.Address, amount uint64) {
45 crossing()
46
47 checkErr(UserTeller.Approve(spender, amount))
48}
49
50func TransferFrom(from, to std.Address, amount uint64) {
51 crossing()
52
53 checkErr(UserTeller.TransferFrom(from, to, amount))
54}
55
56// Faucet is distributing foo20 tokens without restriction (unsafe).
57// For a real token faucet, you should take care of setting limits are asking payment.
58func Faucet() {
59 crossing()
60
61 caller := std.PreviousRealm().Address()
62 amount := uint64(1_000 * 10_000) // 1k
63 checkErr(privateLedger.Mint(caller, amount))
64}
65
66func Mint(to std.Address, amount uint64) {
67 crossing()
68
69 Ownable.AssertOwnedByPrevious()
70 checkErr(privateLedger.Mint(to, amount))
71}
72
73func Burn(from std.Address, amount uint64) {
74 crossing()
75
76 Ownable.AssertOwnedByPrevious()
77 checkErr(privateLedger.Burn(from, amount))
78}
79
80func Render(path string) string {
81 parts := strings.Split(path, "/")
82 c := len(parts)
83
84 switch {
85 case path == "":
86 return Token.RenderHome()
87 case c == 2 && parts[0] == "balance":
88 owner := std.Address(parts[1])
89 balance := UserTeller.BalanceOf(owner)
90 return ufmt.Sprintf("%d\n", balance)
91 default:
92 return "404\n"
93 }
94}
95
96func checkErr(err error) {
97 if err != nil {
98 panic(err)
99 }
100}