foo721.gno
2.02 Kb ยท 121 lines
1package foo721
2
3import (
4 "std"
5
6 "gno.land/p/demo/grc/grc721"
7 "gno.land/p/demo/ufmt"
8)
9
10var (
11 admin std.Address = "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"
12 foo = grc721.NewBasicNFT("FooNFT", "FNFT")
13)
14
15func init() {
16 mintNNFT(admin, 10) // @administrator (10)
17 mintNNFT("g1var589z07ppjsjd24ukm4uguzwdt0tw7g47cgm", 5) // @hariom (5)
18}
19
20func mintNNFT(owner std.Address, n uint64) {
21 count := foo.TokenCount()
22 for i := count; i < count+n; i++ {
23 tid := grc721.TokenID(ufmt.Sprintf("%d", i))
24 foo.Mint(owner, tid)
25 }
26}
27
28// Getters
29
30func BalanceOf(user std.Address) uint64 {
31 balance, err := foo.BalanceOf(user)
32 if err != nil {
33 panic(err)
34 }
35
36 return balance
37}
38
39func OwnerOf(tid grc721.TokenID) std.Address {
40 owner, err := foo.OwnerOf(tid)
41 if err != nil {
42 panic(err)
43 }
44
45 return owner
46}
47
48func IsApprovedForAll(owner, user std.Address) bool {
49 return foo.IsApprovedForAll(owner, user)
50}
51
52func GetApproved(tid grc721.TokenID) std.Address {
53 addr, err := foo.GetApproved(tid)
54 if err != nil {
55 panic(err)
56 }
57
58 return addr
59}
60
61// Setters
62
63func Approve(user std.Address, tid grc721.TokenID) {
64 err := foo.Approve(user, tid)
65 if err != nil {
66 panic(err)
67 }
68}
69
70func SetApprovalForAll(user std.Address, approved bool) {
71 err := foo.SetApprovalForAll(user, approved)
72 if err != nil {
73 panic(err)
74 }
75}
76
77func TransferFrom(from, to std.Address, tid grc721.TokenID) {
78 err := foo.TransferFrom(from, to, tid)
79 if err != nil {
80 panic(err)
81 }
82}
83
84// Admin
85
86func Mint(to std.Address, tid grc721.TokenID) {
87 caller := std.PreviousRealm().Address()
88 assertIsAdmin(caller)
89 err := foo.Mint(to, tid)
90 if err != nil {
91 panic(err)
92 }
93}
94
95func Burn(tid grc721.TokenID) {
96 caller := std.PreviousRealm().Address()
97 assertIsAdmin(caller)
98 err := foo.Burn(tid)
99 if err != nil {
100 panic(err)
101 }
102}
103
104// Render
105
106func Render(path string) string {
107 switch {
108 case path == "":
109 return foo.RenderHome()
110 default:
111 return "404\n"
112 }
113}
114
115// Util
116
117func assertIsAdmin(address std.Address) {
118 if address != admin {
119 panic("restricted access")
120 }
121}