foo20.gno

2.45 Kb ยท 99 lines
 1// foo20 is a GRC20 token contract where all the GRC20 methods are proxified
 2// 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/users"
14)
15
16var (
17	banker *grc20.Banker
18	admin  *ownable.Ownable
19	token  grc20.Token
20)
21
22func init() {
23	admin = ownable.NewWithAddress("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq") // @manfred
24	banker = grc20.NewBanker("Foo", "FOO", 4)
25	banker.Mint(admin.Owner(), 1000000*10000) // @administrator (1M)
26	token = banker.Token()
27}
28
29func TotalSupply() uint64 { return token.TotalSupply() }
30
31func BalanceOf(owner pusers.AddressOrName) uint64 {
32	ownerAddr := users.Resolve(owner)
33	return token.BalanceOf(ownerAddr)
34}
35
36func Allowance(owner, spender pusers.AddressOrName) uint64 {
37	ownerAddr := users.Resolve(owner)
38	spenderAddr := users.Resolve(spender)
39	return token.Allowance(ownerAddr, spenderAddr)
40}
41
42func Transfer(to pusers.AddressOrName, amount uint64) {
43	toAddr := users.Resolve(to)
44	checkErr(token.Transfer(toAddr, amount))
45}
46
47func Approve(spender pusers.AddressOrName, amount uint64) {
48	spenderAddr := users.Resolve(spender)
49	checkErr(token.Approve(spenderAddr, amount))
50}
51
52func TransferFrom(from, to pusers.AddressOrName, amount uint64) {
53	fromAddr := users.Resolve(from)
54	toAddr := users.Resolve(to)
55	checkErr(token.TransferFrom(fromAddr, toAddr, amount))
56}
57
58// Faucet is distributing foo20 tokens without restriction (unsafe).
59// For a real token faucet, you should take care of setting limits are asking payment.
60func Faucet() {
61	caller := std.PrevRealm().Addr()
62	amount := uint64(1_000 * 10_000) // 1k
63	checkErr(banker.Mint(caller, amount))
64}
65
66func Mint(to pusers.AddressOrName, amount uint64) {
67	admin.AssertCallerIsOwner()
68	toAddr := users.Resolve(to)
69	checkErr(banker.Mint(toAddr, amount))
70}
71
72func Burn(from pusers.AddressOrName, amount uint64) {
73	admin.AssertCallerIsOwner()
74	fromAddr := users.Resolve(from)
75	checkErr(banker.Burn(fromAddr, amount))
76}
77
78func Render(path string) string {
79	parts := strings.Split(path, "/")
80	c := len(parts)
81
82	switch {
83	case path == "":
84		return banker.RenderHome()
85	case c == 2 && parts[0] == "balance":
86		owner := pusers.AddressOrName(parts[1])
87		ownerAddr := users.Resolve(owner)
88		balance := banker.BalanceOf(ownerAddr)
89		return ufmt.Sprintf("%d\n", balance)
90	default:
91		return "404\n"
92	}
93}
94
95func checkErr(err error) {
96	if err != nil {
97		panic(err)
98	}
99}