qux.gno
1.83 Kb ยท 84 lines
1package qux
2
3import (
4 "strings"
5
6 "gno.land/p/demo/tokens/grc20"
7 "gno.land/p/nt/ownable"
8 "gno.land/p/nt/ufmt"
9
10 "gno.land/r/demo/defi/grc20reg"
11)
12
13var (
14 token, privateLedger = grc20.NewToken("Qux", "QUX", 6)
15 owner = ownable.NewWithAddress("g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d") // ADMIN
16)
17
18func init() {
19 privateLedger.Mint(owner.Owner(), 100_000_000_000_000)
20 grc20reg.Register(cross, token, "")
21}
22
23func TotalSupply() int64 {
24 userTeller := token.CallerTeller()
25 return userTeller.TotalSupply()
26}
27
28func BalanceOf(owner address) int64 {
29 userTeller := token.CallerTeller()
30 return userTeller.BalanceOf(owner)
31}
32
33func Allowance(owner, spender address) int64 {
34 userTeller := token.CallerTeller()
35 return userTeller.Allowance(owner, spender)
36}
37
38func Transfer(cur realm, to address, amount int64) {
39 userTeller := token.CallerTeller()
40 checkErr(userTeller.Transfer(to, amount))
41}
42
43func Approve(cur realm, spender address, amount int64) {
44 userTeller := token.CallerTeller()
45 checkErr(userTeller.Approve(spender, amount))
46}
47
48func TransferFrom(cur realm, from, to address, amount int64) {
49 userTeller := token.CallerTeller()
50 checkErr(userTeller.TransferFrom(from, to, amount))
51}
52
53func Mint(cur realm, to address, amount int64) {
54 owner.AssertOwnedByPrevious()
55 checkErr(privateLedger.Mint(to, amount))
56}
57
58func Burn(cur realm, from address, amount int64) {
59 owner.AssertOwnedByPrevious()
60 checkErr(privateLedger.Burn(from, amount))
61}
62
63func Render(path string) string {
64 parts := strings.Split(path, "/")
65 c := len(parts)
66
67 switch {
68 case path == "":
69 return token.RenderHome()
70 case c == 2 && parts[0] == "balance":
71 owner := address(parts[1])
72 userTeller := token.CallerTeller()
73 balance := userTeller.BalanceOf(owner)
74 return ufmt.Sprintf("%d\n", balance)
75 default:
76 return "404\n"
77 }
78}
79
80func checkErr(err error) {
81 if err != nil {
82 panic(err)
83 }
84}