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

6.14 Kb · 206 lines
  1package core
  2
  3import (
  4	"chain"
  5	"strings"
  6
  7	"gno.land/p/nt/ufmt/v0"
  8
  9	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/safe/v1"
 10	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/types/v1"
 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	}
 50	if c.prov != nil {
 51		info.RevealDeadline = revealDeadline(c)
 52		info.Badge = c.prov.badge
 53	}
 54	return info
 55}
 56
 57// PrimaryPayees returns a copy of the frozen primary-sale split.
 58func PrimaryPayees(cid string) []types.Payee {
 59	return append([]types.Payee{}, mustCollection(cid).primaryPayees...)
 60}
 61
 62// FeeConfig is the typed fee view minters and markets settle with.
 63func FeeConfig() types.Fees { return fees }
 64
 65// PrimaryFeeBps is the protocol fee on cid's primary sale: the fee at
 66// Launch, or the current one if the admin lowered it since (SA-I5: a fee
 67// cannot rise during a sale).
 68func PrimaryFeeBps(cid string) int64 {
 69	c := mustCollection(cid)
 70	if c.prov != nil && c.primaryBps < fees.PrimaryBps {
 71		return c.primaryBps
 72	}
 73	return fees.PrimaryBps
 74}
 75
 76func Beacon() address { return beacon }
 77
 78func Admin() address { return admin }
 79
 80func IsMinter(path string) bool {
 81	v := minters.Get(path)
 82	return v != nil && !v.(*minterRecord).deprecated
 83}
 84
 85// Collection is the collection record as JSON.
 86func Collection(cid string) string {
 87	w := &safe.JSONWriter{}
 88	writeCollection(w, mustCollection(cid))
 89	return w.String()
 90}
 91
 92func Collections(offset, limit int64) string {
 93	w := &safe.JSONWriter{}
 94	w.BeginArray()
 95	collections.IterateByOffset(int(offset), int(clampLimit(limit)), func(_ string, v any) bool {
 96		writeCollection(w, v.(*collection))
 97		return false
 98	})
 99	w.EndArray()
100	return w.String()
101}
102
103func CollectionsByCreator(creator address, offset, limit int64) string {
104	w := &safe.JSONWriter{}
105	w.BeginArray()
106	prefix := creator.String() + ":"
107	skipped, written, want := int64(0), int64(0), clampLimit(limit)
108	byCreator.Iterate(prefix, prefix+"\xff", func(_ string, v any) bool {
109		if skipped < offset {
110			skipped++
111			return false
112		}
113		writeCollection(w, mustCollection(v.(string)))
114		written++
115		return written >= want
116	})
117	w.EndArray()
118	return w.String()
119}
120
121// Fees is the fee configuration and its hard caps as JSON.
122func Fees() string {
123	w := &safe.JSONWriter{}
124	w.BeginObject()
125	w.IntField("createFee", fees.CreateFee)
126	w.IntField("primaryBps", fees.PrimaryBps)
127	w.IntField("secondaryBps", fees.SecondaryBps)
128	w.StrField("treasury", fees.Treasury.String())
129	w.IntField("createFeeCap", CreateFeeCap)
130	w.IntField("protocolFeeCapBps", ProtocolFeeCapBps)
131	w.IntField("royaltyCapBps", RoyaltyCapBps)
132	w.IntField("engineCapBps", fees.EngineCapBps)
133	w.IntField("engineFeeCapBps", EngineFeeCapBps)
134	w.EndObject()
135	return w.String()
136}
137
138// PendingOps is the timelock queue as JSON.
139func PendingOps() string {
140	w := &safe.JSONWriter{}
141	w.BeginArray()
142	ops.Iterate("", "", func(_ string, v any) bool {
143		p := v.(*pendingOp)
144		w.BeginObject()
145		w.StrField("id", itoa(p.id))
146		w.StrField("op", p.op)
147		w.StrField("arg", p.arg)
148		w.IntField("proposed", p.proposed)
149		w.IntField("earliest", earliestExecution(p))
150		w.EndObject()
151		return false
152	})
153	w.EndArray()
154	return w.String()
155}
156
157func writeCollection(w *safe.JSONWriter, c *collection) {
158	h := height()
159	w.BeginObject()
160	w.StrField("cid", c.id)
161	w.StrField("name", c.name)
162	w.StrField("symbol", c.symbol)
163	w.StrField("description", c.description)
164	w.StrField("creator", c.creator.String())
165	w.StrField("engine", c.engineID)
166	w.StrField("params", c.params)
167	w.StrField("minter", c.minter)
168	w.StrField("state", stateAt(c, h).String())
169	w.IntField("maxSupply", c.maxSupply)
170	w.IntField("minted", c.minted())
171	w.IntField("reserved", c.reserved)
172	w.IntField("royaltyBps", c.royaltyBps)
173	w.StrField("royaltyAddress", royaltyAddress(c.id).String())
174	w.StrField("primaryPayees", safe.FormatPayees(c.primaryPayees))
175	w.StrField("royaltyPayees", safe.FormatPayees(c.royaltyPayees))
176	w.IntField("startHeight", c.startHeight)
177	w.IntField("endHeight", c.endHeight)
178	w.IntField("createdHeight", c.createdHeight)
179	w.StrField("token", c.token.ID())
180	author, bps := collectionEngineFee(c)
181	w.StrField("engineAuthor", author.String())
182	w.IntField("engineFeeBps", bps)
183	if c.prov != nil {
184		w.IntField("revealDeadline", revealDeadline(c))
185		w.BoolField("useBeacon", c.useBeacon)
186		w.StrField("beaconSource", c.beaconSource)
187		w.StrField("badge", c.prov.badge)
188		w.BoolField("fair", FairLaunch(c.id))
189	}
190	w.EndObject()
191}
192
193func clampLimit(limit int64) int64 {
194	if limit <= 0 || limit > MaxPageSize {
195		return MaxPageSize
196	}
197	return limit
198}
199
200// Render stays minimal in the immutable realm; the browsable gallery is the
201// private, re-deployable gallery realm (design §2.5.7).
202func Render(path string) string {
203	gallery := strings.TrimPrefix(strings.TrimSuffix(selfPath, "/core")+"/gallery", "gno.land")
204	return ufmt.Sprintf("# Collections ledger\n\n%d collections, %d engines. Browse them in the [gallery](%s).\n",
205		collCount, engineCount, gallery)
206}