nft.gno
2.47 Kb · 75 lines
1package nsdata
2
3import (
4 "time"
5
6 "gno.land/r/nym-mnsrehearsal001/nsnft/v2"
7)
8
9/*
10The token reads, answered by the ledger.
11
12These used to be the vault's own grc721 state. They are now read-throughs
13to nsnft, kept here under the same names so nsview, nsskin, nsvote and
14the site can go on asking the registry — while an external marketplace
15asks nsnft directly and gets the identical answer, because there is only
16one ledger to ask.
17
18Token ids here are plain strings. The grc721.TokenID type was the vault's
19import; this realm no longer imports grc721 at all.
20*/
21
22func isTokenValid(tid string) bool {
23 label, domainLabel := splitTokenID(tid)
24 n, ok := getName(label, domainLabel)
25 return ok && isValidNow(n.expires, time.Now().Unix())
26}
27
28// BalanceOf counts every token an address holds — including free-tier
29// tokens, which are held rather than absent. That is a real change from
30// the previous generation, where a free name had no token and counted
31// for nothing; nsvote gates on this, so free-tier holders now have a vote.
32func BalanceOf(owner address) int64 { return nsnft.BalanceOf(owner) }
33
34// OwnerOf hides the owner of an expired token — the anti-scam pattern,
35// so our own pages never present a lapsed name as somebody's. nsnft
36// itself answers regardless; see MIGRATION.md, rule 6.
37func OwnerOf(tid string) address {
38 owner := nsnft.OwnerOf(tid)
39 if owner == "" {
40 panic("nsdata: no such token: " + tid)
41 }
42 if !isTokenValid(tid) {
43 panic("nsdata: token is expired, no current owner")
44 }
45 return owner
46}
47
48// GetApproved answers totally: the zero address means nobody is approved
49// or there is no such token. See the long note this replaced in git
50// history for why a panic here broke the marketplace once already.
51func GetApproved(tid string) address { return nsnft.GetApproved(tid) }
52
53// IsApprovedFor is the question a marketplace actually has: may this
54// operator move this token, by either route?
55func IsApprovedFor(tid string, operator address) bool {
56 owner := nsnft.OwnerOf(tid)
57 if owner == "" {
58 return false
59 }
60 if owner == operator || nsnft.IsApprovedForAll(owner, operator) {
61 return true
62 }
63 return nsnft.GetApproved(tid) == operator
64}
65
66func IsApprovedForAll(owner, operator address) bool {
67 return nsnft.IsApprovedForAll(owner, operator)
68}
69
70// TokenURI returns a pointer at whichever view realm renders the art.
71// nsnft has its own URI base for external marketplaces; both are set to
72// the same view realm at wiring time.
73func TokenURI(tid string) string {
74 return tokenURIBase + tid
75}