Search Apps Documentation Source Content File Folder Download Copy Actions Download

viewjson.gno

3.68 Kb · 142 lines
  1package nftmarket
  2
  3// JSON read helpers for front-ends. These return compact JSON strings so a web
  4// client can fetch a whole view in a single vm/qeval call instead of parsing
  5// Gno tuple output. They are read-only.
  6
  7import (
  8	"strconv"
  9	"strings"
 10
 11	"gno.land/p/g18wk4a80cr7dqa25vfka2yug5n3pd50udled6y3/grc721"
 12	"gno.land/p/nt/ufmt/v0"
 13)
 14
 15// CollectionsJSON returns every collection as a JSON array.
 16func CollectionsJSON() string {
 17	var b strings.Builder
 18	b.WriteString("[")
 19	for i, id := range collOrder {
 20		v, ok := collections.Get(id)
 21		if !ok {
 22			continue
 23		}
 24		c := v.(*Collection)
 25		if i > 0 {
 26			b.WriteString(",")
 27		}
 28		b.WriteString(ufmt.Sprintf(
 29			`{"id":%s,"name":%s,"symbol":%s,"creator":%s,"baseURI":%s,"mintPrice":%d,"maxSupply":%d,"minted":%d,"burned":%d,"royaltyBps":%d,"sealed":%s}`,
 30			js(c.id), js(c.name), js(c.symbol), js(c.creator.String()), js(c.baseURI),
 31			c.mintPrice, c.maxSupply, c.minted, c.burned, c.royaltyBps, jb(c.sealed)))
 32	}
 33	b.WriteString("]")
 34	return b.String()
 35}
 36
 37// TokensJSON returns every minted token of a collection with its market state.
 38func TokensJSON(collID string) string {
 39	coll := mustGetCollection(collID)
 40	var b strings.Builder
 41	b.WriteString("[")
 42	first := true
 43	for i := int64(1); i <= coll.minted; i++ {
 44		tokenID := strconv.FormatInt(i, 10)
 45		owner, err := coll.nft.OwnerOf(grc721.TokenID(tokenID))
 46		if err != nil {
 47			continue
 48		}
 49		if !first {
 50			b.WriteString(",")
 51		}
 52		first = false
 53
 54		listed, price, seller := false, int64(0), ""
 55		if l, ok := getListing(collID, tokenID); ok {
 56			listed, price, seller = true, l.price, l.seller.String()
 57		}
 58		auction, minBid, highBid, highBidder, endUnix := false, int64(0), int64(0), "", int64(0)
 59		if a, ok := getAuction(collID, tokenID); ok {
 60			auction, minBid, highBid, highBidder, endUnix = true, a.minBid, a.highestBid, a.highestBidder.String(), a.endTime.Unix()
 61		}
 62
 63		b.WriteString(ufmt.Sprintf(
 64			`{"id":%s,"owner":%s,"listed":%s,"price":%d,"seller":%s,"auction":%s,"minBid":%d,"highBid":%d,"highBidder":%s,"endUnix":%d}`,
 65			js(tokenID), js(owner.String()), jb(listed), price, js(seller),
 66			jb(auction), minBid, highBid, js(highBidder), endUnix))
 67	}
 68	b.WriteString("]")
 69	return b.String()
 70}
 71
 72// OffersJSON returns the standing offers on a token.
 73func OffersJSON(collID, tokenID string) string {
 74	prefix := collID + "/" + tokenID + "/"
 75	var b strings.Builder
 76	b.WriteString("[")
 77	first := true
 78	offers.Iterate("", "", func(key string, value any) bool {
 79		if !strings.HasPrefix(key, prefix) {
 80			return false
 81		}
 82		o := value.(*Offer)
 83		if !first {
 84			b.WriteString(",")
 85		}
 86		first = false
 87		b.WriteString(ufmt.Sprintf(`{"buyer":%s,"amount":%d}`, js(o.buyer.String()), o.amount))
 88		return false
 89	})
 90	b.WriteString("]")
 91	return b.String()
 92}
 93
 94// AllOffersJSON returns every standing offer across all tokens (for "top offers").
 95func AllOffersJSON() string {
 96	var b strings.Builder
 97	b.WriteString("[")
 98	first := true
 99	offers.Iterate("", "", func(key string, value any) bool {
100		o := value.(*Offer)
101		if !first {
102			b.WriteString(",")
103		}
104		first = false
105		b.WriteString(ufmt.Sprintf(`{"collID":%s,"tokenID":%s,"buyer":%s,"amount":%d}`,
106			js(o.collID), js(o.tokenID), js(o.buyer.String()), o.amount))
107		return false
108	})
109	b.WriteString("]")
110	return b.String()
111}
112
113// js encodes a string as a JSON string literal.
114func js(s string) string {
115	var b strings.Builder
116	b.WriteString(`"`)
117	for _, r := range s {
118		switch r {
119		case '"':
120			b.WriteString(`\"`)
121		case '\\':
122			b.WriteString(`\\`)
123		case '\n':
124			b.WriteString(`\n`)
125		case '\r':
126			b.WriteString(`\r`)
127		case '\t':
128			b.WriteString(`\t`)
129		default:
130			b.WriteString(string(r))
131		}
132	}
133	b.WriteString(`"`)
134	return b.String()
135}
136
137func jb(v bool) string {
138	if v {
139		return "true"
140	}
141	return "false"
142}