-
Notifications
You must be signed in to change notification settings - Fork 0
/
cps.gno
109 lines (88 loc) · 2.38 KB
/
cps.gno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package cps
import (
"std"
"strings"
"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ufmt"
"gno.land/r/demo/users"
)
var (
cps *grc20.AdminToken
admin std.Address = "g1àààààààààààààààyouraddressààààààààààààà0000000" // TODO: helper to change admin
)
func init() {
cps = grc20.NewAdminToken("Cryptopunkstar", "CPS", 4)
cps.Mint(admin, 1000000*100000) // @administrator (1M)
cps.Mint("g1àààààààààààààààyouraddressààààààààààààà0000000", 100000*100000) // @cryptopunkstar (100k)
}
// method proxies as public functions.
// getters.
func TotalSupply() uint64 {
return cps.TotalSupply()
}
func BalanceOf(owner users.AddressOrName) uint64 {
balance, err := cps.BalanceOf(owner.Resolve())
if err != nil {
panic(err)
}
return balance
}
func Allowance(owner, spender users.AddressOrName) uint64 {
allowance, err := cps.Allowance(owner.Resolve(), spender.Resolve())
if err != nil {
panic(err)
}
return allowance
}
// setters.
func Transfer(to users.AddressOrName, amount uint64) {
caller := std.GetOrigCaller()
cps.Transfer(caller, to.Resolve(), amount)
}
func Approve(spender users.AddressOrName, amount uint64) {
caller := std.GetOrigCaller()
cps.Approve(caller, spender.Resolve(), amount)
}
func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.GetOrigCaller()
cps.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
}
// faucet.
func Faucet() {
// FIXME: add limits?
// FIXME: add payment in gnot?
caller := std.GetOrigCaller()
cps.Mint(caller, 1000*10000) // 1k
}
// administration.
func Mint(address users.AddressOrName, amount uint64) {
caller := std.GetOrigCaller()
assertIsAdmin(caller)
cps.Mint(address.Resolve(), amount)
}
func Burn(address users.AddressOrName, amount uint64) {
caller := std.GetOrigCaller()
assertIsAdmin(caller)
cps.Burn(address.Resolve(), amount)
}
// render le rendu
func Render(path string) string {
parts := strings.Split(path, "/")
c := len(parts)
switch {
case path == "":
return cps.RenderHome()
case c == 2 && parts[0] == "balance":
owner := users.AddressOrName(parts[1])
balance, _ := cps.BalanceOf(owner.Resolve())
return ufmt.Sprintf("%d\n", balance)
default:
return "404\n"
}
}
// secure access for admin :)
func assertIsAdmin(address std.Address) {
if address != admin {
panic("restricted access")
}
}