Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

btc.gno

1.83 Kb · 85 lines
 1package test_btc
 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
13const tokenID = 0
14
15var (
16	token         *grc20.Token
17	privateLedger *grc20.PrivateLedger
18	userTeller    grc20.Teller
19
20	owner = ownable.NewWithAddress("g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d") // ADMIN
21)
22
23func init(cur realm) {
24	token, privateLedger = grc20.NewToken("Bitcoin", "BTC", 8, tokenID, cur)
25	userTeller = token.CallerTeller()
26
27	privateLedger.Mint(owner.Owner(), 2_100_000_000_000_000)
28	grc20reg.Register(cross(cur), token, "")
29}
30
31func TotalSupply() int64 {
32	return userTeller.TotalSupply()
33}
34
35func BalanceOf(owner address) int64 {
36	return userTeller.BalanceOf(owner)
37}
38
39func Allowance(owner, spender address) int64 {
40	return userTeller.Allowance(owner, spender)
41}
42
43func Transfer(cur realm, to address, amount int64) {
44	checkErr(userTeller.Transfer(0, cur, to, amount))
45}
46
47func Approve(cur realm, spender address, amount int64) {
48	checkErr(userTeller.Approve(0, cur, spender, amount))
49}
50
51func TransferFrom(cur realm, from, to address, amount int64) {
52	checkErr(userTeller.TransferFrom(0, cur, from, to, amount))
53}
54
55func Mint(cur realm, to address, amount int64) {
56	owner.AssertOwnedBy(cur.Previous().Address())
57	checkErr(privateLedger.Mint(to, amount))
58}
59
60func Burn(cur realm, from address, amount int64) {
61	owner.AssertOwnedBy(cur.Previous().Address())
62	checkErr(privateLedger.Burn(from, amount))
63}
64
65func Render(path string) string {
66	parts := strings.Split(path, "/")
67	c := len(parts)
68
69	switch {
70	case path == "":
71		return token.RenderHome()
72	case c == 2 && parts[0] == "balance":
73		owner := address(parts[1])
74		balance := userTeller.BalanceOf(owner)
75		return ufmt.Sprintf("%d\n", balance)
76	default:
77		return "404\n"
78	}
79}
80
81func checkErr(err error) {
82	if err != nil {
83		panic(err)
84	}
85}