foo721.gno

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