sol.gno
1.80 Kb · 83 lines
1package sol
2
3import (
4 "strings"
5
6 "gno.land/p/demo/tokens/grc20"
7 ownable "gno.land/p/nt/ownable/v0"
8 ufmt "gno.land/p/nt/ufmt/v0"
9
10 "gno.land/r/demo/defi/grc20reg"
11)
12
13var (
14 token *grc20.Token
15 privateLedger *grc20.PrivateLedger
16 userTeller grc20.Teller
17
18 owner = ownable.NewWithAddress("g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d") // ADMIN
19)
20
21func init(cur realm) {
22 token, privateLedger = grc20.NewToken(0, cur, "Solana", "SOL", 9)
23 userTeller = token.CallerTeller()
24
25 privateLedger.Mint(owner.Owner(), 600_000_000_000_000_000)
26 grc20reg.Register(cross(cur), token, "")
27}
28
29func TotalSupply() int64 {
30 return userTeller.TotalSupply()
31}
32
33func BalanceOf(owner address) int64 {
34 return userTeller.BalanceOf(owner)
35}
36
37func Allowance(owner, spender address) int64 {
38 return userTeller.Allowance(owner, spender)
39}
40
41func Transfer(cur realm, to address, amount int64) {
42 checkErr(userTeller.Transfer(0, cur, to, amount))
43}
44
45func Approve(cur realm, spender address, amount int64) {
46 checkErr(userTeller.Approve(0, cur, spender, amount))
47}
48
49func TransferFrom(cur realm, from, to address, amount int64) {
50 checkErr(userTeller.TransferFrom(0, cur, from, to, amount))
51}
52
53func Mint(cur realm, to address, amount int64) {
54 owner.AssertOwnedBy(cur.Previous().Address())
55 checkErr(privateLedger.Mint(to, amount))
56}
57
58func Burn(cur realm, from address, amount int64) {
59 owner.AssertOwnedBy(cur.Previous().Address())
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 balance := userTeller.BalanceOf(owner)
73 return ufmt.Sprintf("%d\n", balance)
74 default:
75 return "404\n"
76 }
77}
78
79func checkErr(err error) {
80 if err != nil {
81 panic(err)
82 }
83}