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