Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

reads.gno

7.18 Kb · 236 lines
  1package core
  2
  3import (
  4	"chain"
  5	"strings"
  6
  7	"gno.land/p/nt/ufmt/v0"
  8
  9	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/safe/v2"
 10	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/types/v2"
 11)
 12
 13// Every function in this file is read-only (I-12: no exported non-crossing
 14// mutator). Lists are paginated with limit <= MaxPageSize (RA L-4).
 15
 16// SelfAddress is core's package address, for the deploy-time cross-check
 17// against scripts/pkgaddr.py (RA H-5).
 18func SelfAddress() address { return chain.PackageAddress(selfPath) }
 19
 20func CollectionCount() int64 { return collCount }
 21
 22// MintedTotal counts every token ever minted, burned ones included (OA B4).
 23func MintedTotal(cid string) int64 { return mustCollection(cid).minted() }
 24
 25// MintedBy is addr's lifetime count of sale mints in cid (OA B3, I-9),
 26// reserves excluded. Exact until the collection is Final, -1 afterwards
 27// (derived from the locked grc721 balance; see mintedBy).
 28func MintedBy(cid string, addr address) int64 { return mintedBy(mustCollection(cid), addr) }
 29
 30// Info is the typed collection view for other realms.
 31func Info(cid string) types.CollectionInfo {
 32	c := mustCollection(cid)
 33	info := types.CollectionInfo{
 34		ID:          c.id,
 35		Name:        c.name,
 36		Symbol:      c.symbol,
 37		Description: c.description,
 38		Creator:     c.creator,
 39		Minter:      c.minter,
 40		EngineID:    c.engineID,
 41		Params:      c.params,
 42		State:       stateAt(c, height()),
 43		MaxSupply:   c.maxSupply,
 44		Minted:      c.minted(),
 45		Reserved:    c.reserved,
 46		StartHeight: c.startHeight,
 47		EndHeight:   c.endHeight,
 48		RoyaltyBps:  c.royaltyBps,
 49		Soulbound:   c.soulbound,
 50	}
 51	info.TraitTypes = append([]string{}, traitTypesOf(c)...)
 52	if c.prov != nil {
 53		info.RevealDeadline = revealDeadline(c)
 54		info.Badge = c.prov.badge
 55		info.Mode = c.mode
 56		if c.state == types.StateFinal {
 57			info.Salt = c.prov.salt
 58		}
 59	}
 60	return info
 61}
 62
 63// CollectionTraitTypes is the collection's trait list as JSON ["…"].
 64func CollectionTraitTypes(cid string) string {
 65	w := &safe.JSONWriter{}
 66	writeStrings(w, traitTypesOf(mustCollection(cid)))
 67	return w.String()
 68}
 69
 70// PrimaryPayees returns a copy of the frozen primary-sale split.
 71func PrimaryPayees(cid string) []types.Payee {
 72	return append([]types.Payee{}, mustCollection(cid).primaryPayees...)
 73}
 74
 75// FeeConfig is the typed fee view minters and markets settle with.
 76func FeeConfig() types.Fees { return fees }
 77
 78// ListingDeposit is the deposit return (ugnot) market/v1 pins into a new
 79// listing (CV-L2); 0..ListingDepositCap, admin op listing.deposit.
 80func ListingDeposit() int64 { return listingDeposit }
 81
 82// PrimaryFeeBps is the protocol fee on cid's primary sale: the fee at
 83// Launch, or the current one if the admin lowered it since (SA-I5: a fee
 84// cannot rise during a sale).
 85func PrimaryFeeBps(cid string) int64 {
 86	c := mustCollection(cid)
 87	if c.prov != nil && c.primaryBps < fees.PrimaryBps {
 88		return c.primaryBps
 89	}
 90	return fees.PrimaryBps
 91}
 92
 93func Beacon() address { return beacon }
 94
 95func Admin() address { return admin }
 96
 97func IsMinter(path string) bool {
 98	v := minters.Get(path)
 99	return v != nil && !v.(*minterRecord).deprecated
100}
101
102// Collection is the collection record as JSON.
103func Collection(cid string) string {
104	w := &safe.JSONWriter{}
105	writeCollection(w, mustCollection(cid))
106	return w.String()
107}
108
109func Collections(offset, limit int64) string {
110	w := &safe.JSONWriter{}
111	w.BeginArray()
112	collections.IterateByOffset(int(offset), int(clampLimit(limit)), func(_ string, v any) bool {
113		writeCollection(w, v.(*collection))
114		return false
115	})
116	w.EndArray()
117	return w.String()
118}
119
120func CollectionsByCreator(creator address, offset, limit int64) string {
121	w := &safe.JSONWriter{}
122	w.BeginArray()
123	prefix := creator.String() + ":"
124	skipped, written, want := int64(0), int64(0), clampLimit(limit)
125	byCreator.Iterate(prefix, prefix+"\xff", func(_ string, v any) bool {
126		if skipped < offset {
127			skipped++
128			return false
129		}
130		writeCollection(w, mustCollection(v.(string)))
131		written++
132		return written >= want
133	})
134	w.EndArray()
135	return w.String()
136}
137
138// Fees is the fee configuration and its hard caps as JSON.
139func Fees() string {
140	w := &safe.JSONWriter{}
141	w.BeginObject()
142	w.IntField("createFee", fees.CreateFee)
143	w.IntField("primaryBps", fees.PrimaryBps)
144	w.IntField("secondaryBps", fees.SecondaryBps)
145	w.StrField("treasury", fees.Treasury.String())
146	w.IntField("createFeeCap", CreateFeeCap)
147	w.IntField("protocolFeeCapBps", ProtocolFeeCapBps)
148	w.IntField("royaltyCapBps", RoyaltyCapBps)
149	w.IntField("engineCapBps", fees.EngineCapBps)
150	w.IntField("engineFeeCapBps", EngineFeeCapBps)
151	w.IntField("listingDeposit", listingDeposit)
152	w.IntField("listingDepositCap", ListingDepositCap)
153	w.EndObject()
154	return w.String()
155}
156
157// PendingOps pages the timelock queue as JSON (M-25):
158// [{id, op, arg, proposed, earliest}].
159func PendingOps(offset, limit int64) string {
160	w := &safe.JSONWriter{}
161	w.BeginArray()
162	ops.IterateByOffset(int(offset), int(clampLimit(limit)), func(_ string, v any) bool {
163		p := v.(*pendingOp)
164		w.BeginObject()
165		w.StrField("id", itoa(p.id))
166		w.StrField("op", p.op)
167		w.StrField("arg", p.arg)
168		w.IntField("proposed", p.proposed)
169		w.IntField("earliest", earliestExecution(p))
170		w.EndObject()
171		return false
172	})
173	w.EndArray()
174	return w.String()
175}
176
177func writeCollection(w *safe.JSONWriter, c *collection) {
178	h := height()
179	w.BeginObject()
180	w.StrField("cid", c.id)
181	w.StrField("name", c.name)
182	w.StrField("symbol", c.symbol)
183	w.StrField("description", c.description)
184	w.StrField("creator", c.creator.String())
185	w.StrField("engine", c.engineID)
186	w.StrField("params", c.params)
187	w.StrField("minter", c.minter)
188	w.StrField("state", stateAt(c, h).String())
189	w.IntField("maxSupply", c.maxSupply)
190	w.IntField("minted", c.minted())
191	w.IntField("reserved", c.reserved)
192	w.IntField("royaltyBps", c.royaltyBps)
193	w.StrField("royaltyAddress", royaltyAddress(c.id).String())
194	w.StrField("primaryPayees", safe.FormatPayees(c.primaryPayees))
195	w.StrField("royaltyPayees", safe.FormatPayees(c.royaltyPayees))
196	w.IntField("startHeight", c.startHeight)
197	w.IntField("endHeight", c.endHeight)
198	w.IntField("createdHeight", c.createdHeight)
199	w.StrField("token", c.token.ID())
200	author, bps := collectionEngineFee(c)
201	w.StrField("engineAuthor", author.String())
202	w.IntField("engineFeeBps", bps)
203	w.Key("traitTypes")
204	writeStrings(w, traitTypesOf(c))
205	w.StrField("access", c.access)
206	w.BoolField("soulbound", c.soulbound)
207	if c.prov != nil {
208		w.StrField("mode", modeName(c.mode))
209		w.IntField("revealDeadline", revealDeadline(c))
210		w.BoolField("useBeacon", c.useBeacon)
211		w.StrField("beaconSource", c.beaconSource)
212		w.StrField("badge", c.prov.badge)
213		w.BoolField("fair", FairLaunch(c.id))
214		salt := ""
215		if c.state == types.StateFinal {
216			salt = c.prov.salt
217		}
218		w.StrField("salt", salt)
219	}
220	w.EndObject()
221}
222
223func clampLimit(limit int64) int64 {
224	if limit <= 0 || limit > MaxPageSize {
225		return MaxPageSize
226	}
227	return limit
228}
229
230// Render stays minimal in the immutable realm; the browsable gallery is the
231// private, re-deployable gallery realm (design §2.5.7).
232func Render(path string) string {
233	gallery := strings.TrimPrefix(strings.TrimSuffix(selfPath, "/core")+"/gallery", "gno.land")
234	return ufmt.Sprintf("# Collections ledger\n\n%d collections, %d engines. Browse them in the [gallery](%s).\n",
235		collCount, engineCount, gallery)
236}