Search Apps Documentation Source Content File Folder Download Copy

foo20.gno

2.08 Kb ยท 88 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	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	checkErr(UserTeller.Transfer(to, amount))
40}
41
42func Approve(spender std.Address, amount uint64) {
43	checkErr(UserTeller.Approve(spender, amount))
44}
45
46func TransferFrom(from, to std.Address, amount uint64) {
47	checkErr(UserTeller.TransferFrom(from, to, amount))
48}
49
50// Faucet is distributing foo20 tokens without restriction (unsafe).
51// For a real token faucet, you should take care of setting limits are asking payment.
52func Faucet() {
53	caller := std.PrevRealm().Addr()
54	amount := uint64(1_000 * 10_000) // 1k
55	checkErr(privateLedger.Mint(caller, amount))
56}
57
58func Mint(to std.Address, amount uint64) {
59	Ownable.AssertCallerIsOwner()
60	checkErr(privateLedger.Mint(to, amount))
61}
62
63func Burn(from std.Address, amount uint64) {
64	Ownable.AssertCallerIsOwner()
65	checkErr(privateLedger.Burn(from, amount))
66}
67
68func Render(path string) string {
69	parts := strings.Split(path, "/")
70	c := len(parts)
71
72	switch {
73	case path == "":
74		return Token.RenderHome()
75	case c == 2 && parts[0] == "balance":
76		owner := std.Address(parts[1])
77		balance := UserTeller.BalanceOf(owner)
78		return ufmt.Sprintf("%d\n", balance)
79	default:
80		return "404\n"
81	}
82}
83
84func checkErr(err error) {
85	if err != nil {
86		panic(err)
87	}
88}