Search Apps Documentation Source Content File Folder Download Copy Actions Download

views.gno

2.93 Kb · 124 lines
  1package gnogle_nft2
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	"gno.land/p/g18wk4a80cr7dqa25vfka2yug5n3pd50udled6y3/grc721"
  8	"gno.land/p/nt/ufmt/v0"
  9)
 10
 11// OwnerOf returns the owner of a token, or the zero address if it does not exist.
 12func OwnerOf(collID, tokenID string) address {
 13	coll, ok := getCollection(collID)
 14	if !ok {
 15		return zeroAddr
 16	}
 17	o, err := coll.nft.OwnerOf(grc721.TokenID(tokenID))
 18	if err != nil {
 19		return zeroAddr
 20	}
 21	return o
 22}
 23
 24// TokenURI returns baseURI + tokenID.
 25func TokenURI(collID, tokenID string) string {
 26	coll, ok := getCollection(collID)
 27	if !ok {
 28		return ""
 29	}
 30	return coll.baseURI + tokenID
 31}
 32
 33// CollectionRoyalty returns (creator, royaltyBps) — used by the market realm to
 34// split secondary-sale proceeds.
 35func CollectionRoyalty(collID string) (address, int64) {
 36	coll := mustGetCollection(collID)
 37	return coll.creator, coll.royaltyBps
 38}
 39
 40// CollectionMinted returns how many tokens have been minted (token ids 1..N).
 41func CollectionMinted(collID string) int64 {
 42	coll := mustGetCollection(collID)
 43	return coll.minted
 44}
 45
 46// Exists reports whether a token currently exists (not burned).
 47func Exists(collID, tokenID string) bool {
 48	return OwnerOf(collID, tokenID) != zeroAddr
 49}
 50
 51// CollectionsJSON returns every collection as JSON (for front-ends).
 52func CollectionsJSON() string {
 53	var b strings.Builder
 54	b.WriteString("[")
 55	for i, id := range collOrder {
 56		v, ok := collections.Get(id)
 57		if !ok {
 58			continue
 59		}
 60		c := v.(*Collection)
 61		if i > 0 {
 62			b.WriteString(",")
 63		}
 64		b.WriteString(ufmt.Sprintf(
 65			`{"id":%s,"name":%s,"symbol":%s,"creator":%s,"baseURI":%s,"mintPrice":%d,"maxSupply":%d,"minted":%d,"burned":%d,"royaltyBps":%d,"sealed":%s,"verified":%s}`,
 66			js(c.id), js(c.name), js(c.symbol), js(c.creator.String()), js(c.baseURI),
 67			c.mintPrice, c.maxSupply, c.minted, c.burned, c.royaltyBps, jb(c.sealed), jb(c.verified)))
 68	}
 69	b.WriteString("]")
 70	return b.String()
 71}
 72
 73// TokensJSON returns each minted token's id + owner. Market state (listings,
 74// auctions) lives in the market realm; the front-end joins the two.
 75func TokensJSON(collID string) string {
 76	coll := mustGetCollection(collID)
 77	var b strings.Builder
 78	b.WriteString("[")
 79	first := true
 80	for i := int64(1); i <= coll.minted; i++ {
 81		tokenID := strconv.FormatInt(i, 10)
 82		owner, err := coll.nft.OwnerOf(grc721.TokenID(tokenID))
 83		if err != nil {
 84			continue
 85		}
 86		if !first {
 87			b.WriteString(",")
 88		}
 89		first = false
 90		b.WriteString(ufmt.Sprintf(`{"id":%s,"owner":%s}`, js(tokenID), js(owner.String())))
 91	}
 92	b.WriteString("]")
 93	return b.String()
 94}
 95
 96func js(s string) string {
 97	var b strings.Builder
 98	b.WriteString(`"`)
 99	for _, r := range s {
100		switch r {
101		case '"':
102			b.WriteString(`\"`)
103		case '\\':
104			b.WriteString(`\\`)
105		case '\n':
106			b.WriteString(`\n`)
107		case '\r':
108			b.WriteString(`\r`)
109		case '\t':
110			b.WriteString(`\t`)
111		default:
112			b.WriteString(string(r))
113		}
114	}
115	b.WriteString(`"`)
116	return b.String()
117}
118
119func jb(v bool) string {
120	if v {
121		return "true"
122	}
123	return "false"
124}