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 pusers "gno.land/p/demo/users"
13 "gno.land/r/demo/grc20reg"
14 "gno.land/r/demo/users"
15)
16
17var (
18 Token, privateLedger = grc20.NewToken("Foo", "FOO", 4)
19 UserTeller = Token.CallerTeller()
20 Ownable = ownable.NewWithAddress("g1manfred47kzduec920z88wfr64ylksmdcedlf5") // @manfred
21)
22
23func init() {
24 privateLedger.Mint(Ownable.Owner(), 1_000_000*10_000) // @privateLedgeristrator (1M)
25 getter := func() *grc20.Token { return Token }
26 grc20reg.Register(getter, "")
27}
28
29func TotalSupply() uint64 {
30 return UserTeller.TotalSupply()
31}
32
33func BalanceOf(owner pusers.AddressOrName) uint64 {
34 ownerAddr := users.Resolve(owner)
35 return UserTeller.BalanceOf(ownerAddr)
36}
37
38func Allowance(owner, spender pusers.AddressOrName) uint64 {
39 ownerAddr := users.Resolve(owner)
40 spenderAddr := users.Resolve(spender)
41 return UserTeller.Allowance(ownerAddr, spenderAddr)
42}
43
44func Transfer(to pusers.AddressOrName, amount uint64) {
45 toAddr := users.Resolve(to)
46 checkErr(UserTeller.Transfer(toAddr, amount))
47}
48
49func Approve(spender pusers.AddressOrName, amount uint64) {
50 spenderAddr := users.Resolve(spender)
51 checkErr(UserTeller.Approve(spenderAddr, amount))
52}
53
54func TransferFrom(from, to pusers.AddressOrName, amount uint64) {
55 fromAddr := users.Resolve(from)
56 toAddr := users.Resolve(to)
57 checkErr(UserTeller.TransferFrom(fromAddr, toAddr, amount))
58}
59
60// Faucet is distributing foo20 tokens without restriction (unsafe).
61// For a real token faucet, you should take care of setting limits are asking payment.
62func Faucet() {
63 caller := std.PrevRealm().Addr()
64 amount := uint64(1_000 * 10_000) // 1k
65 checkErr(privateLedger.Mint(caller, amount))
66}
67
68func Mint(to pusers.AddressOrName, amount uint64) {
69 Ownable.AssertCallerIsOwner()
70 toAddr := users.Resolve(to)
71 checkErr(privateLedger.Mint(toAddr, amount))
72}
73
74func Burn(from pusers.AddressOrName, amount uint64) {
75 Ownable.AssertCallerIsOwner()
76 fromAddr := users.Resolve(from)
77 checkErr(privateLedger.Burn(fromAddr, 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 := pusers.AddressOrName(parts[1])
89 ownerAddr := users.Resolve(owner)
90 balance := UserTeller.BalanceOf(ownerAddr)
91 return ufmt.Sprintf("%d\n", balance)
92 default:
93 return "404\n"
94 }
95}
96
97func checkErr(err error) {
98 if err != nil {
99 panic(err)
100 }
101}
foo20.gno
2.62 Kb ยท 101 lines