nft.gno
5.21 Kb · 165 lines
1package rooms
2
3import (
4 "chain"
5 "strconv"
6 "strings"
7
8 "gno.land/p/nt/avl/v0"
9 "gno.land/p/nt/grc721/enumerable/v0"
10 "gno.land/p/nt/grc721/v0"
11)
12
13// NFTs: one GRC721 token is one asset on a side, held whole. gno.land has no
14// GRC721 registry yet, so rooms keeps its own, on the trust model of
15// gno.land/r/nt/grc20reg/v0: a collection registers itself, from its own
16// realm, under the key "<realm path>.<symbol>". An NFT asset's denom is that
17// key, "#" and the token id; a key never holds "#".
18
19// MaxTokenIDLen bounds what a room stores per NFT.
20const MaxTokenIDLen = 64
21
22type collection struct {
23 token *grc721.Token
24 // list is the collection's enumerable view, when it has one: it lets
25 // OwnedJSON show a wallet's NFTs of this collection.
26 list *enumerable.Enumerable
27}
28
29var collections avl.Tree // "<realm path>.<symbol>" -> *collection
30
31// RegisterCollection makes a GRC721 collection tradable in rooms and returns
32// its key. Only the collection's own realm can register it, once; list is
33// its enumerable view, or nil. A collection's realm also lets owners
34// approve this realm (see Address) for one token id, as its own Approve
35// does; DepositNFT spends that approval.
36//
37// key := rooms.RegisterCollection(cross(cur), Token, List)
38func RegisterCollection(cur realm, token *grc721.Token, list *enumerable.Enumerable) string {
39 if !cur.IsCurrent() {
40 panic("unauthorized: realm token is not current")
41 }
42 refuseCoins(cur)
43 if token == nil {
44 panic("collection is nil")
45 }
46 path := cur.Previous().PkgPath()
47 if !strings.HasPrefix(path, "gno.land/r/") || strings.Contains(path, "#") {
48 panic("register a collection from its own realm")
49 }
50 key := path + "." + token.GetSymbol()
51 // Token.ID() is "<creating realm path>.<symbol>.<id>" and cannot be
52 // forged: the token was created by the realm that registers it.
53 if !strings.HasPrefix(token.ID(), key+".") {
54 panic("register a collection from its own realm")
55 }
56 if list != nil && list.TokenID() != token.ID() {
57 panic("the list belongs to another collection")
58 }
59 if collections.Has(key) {
60 panic("collection already registered")
61 }
62 collections.Set(key, &collection{token: token, list: list})
63 chain.Emit("CollectionRegistered",
64 "key", key,
65 "name", token.GetName(),
66 "symbol", token.GetSymbol(),
67 )
68 return key
69}
70
71// DepositNFT moves one NFT of a registered collection from the caller to
72// this realm and adds it to the caller's side. The caller must first
73// approve this realm's address (see Address) for that token id, in the
74// collection's realm.
75func DepositNFT(cur realm, roomID uint64, collectionKey string, tokenID string) {
76 who := callerOf(cur)
77 refuseCoins(cur)
78 assertNotPaused()
79 col := mustCollection(collectionKey)
80 assertTokenID(tokenID)
81
82 r := mustRoom(roomID)
83 r.assertLive()
84 mine, _ := r.sideOf(who)
85 r.assertAccepts(KindNFT, collectionKey)
86 denom := collectionKey + "#" + tokenID
87 tid := grc721.TokenID(tokenID)
88
89 // Say plainly what is missing before anything changes.
90 if owner, err := col.token.OwnerOf(tid); err != nil || owner != who {
91 panic("you do not own that NFT")
92 }
93 if to, err := col.token.GetApproved(tid); (err != nil || to != cur.Address()) && !col.token.IsApprovedForAll(who, cur.Address()) {
94 panic("approve this realm for that NFT first")
95 }
96
97 // Record first, move second: a GRC721 transfer runs the issuer's
98 // extension hooks, and anything they call back into must already see
99 // the room as it will be. A failed transfer aborts the whole call.
100 mine.addNFT(denom)
101 r.bump()
102
103 // This realm is the spender: the owner's approval to it is what moves.
104 if err := col.token.RealmTeller(0, cur).TransferFrom(0, cur, who, cur.Address(), tid); err != nil {
105 panic(err.Error())
106 }
107
108 chain.Emit("Deposited",
109 "id", idString(r.id),
110 "party", who.String(),
111 "nft", collectionKey,
112 "tokenId", tokenID,
113 "version", idString(r.version),
114 )
115}
116
117// WithdrawNFT returns one NFT from the caller's side to the caller. Same
118// rules as WithdrawCoins: it works while the room is open, also after it
119// has expired or while the realm is paused.
120func WithdrawNFT(cur realm, roomID uint64, collectionKey string, tokenID string) {
121 withdraw(cur, roomID, KindNFT, collectionKey+"#"+tokenID, 1)
122}
123
124// sendNFT pays one NFT out of this realm's custody.
125func sendNFT(cur realm, to address, denom string) {
126 key, tid, ok := strings.Cut(denom, "#")
127 if !ok {
128 panic("unknown NFT")
129 }
130 col := mustCollection(key)
131 if err := col.token.RealmTeller(0, cur).TransferFrom(0, cur, cur.Address(), to, grc721.TokenID(tid)); err != nil {
132 panic(err.Error())
133 }
134}
135
136func (s *side) addNFT(denom string) {
137 for _, a := range s.assets {
138 if a.kind == KindNFT && a.denom == denom {
139 panic("that NFT is already in the room")
140 }
141 }
142 if len(s.assets) >= MaxAssetsPerSide {
143 panic("a side can hold at most 8 different assets")
144 }
145 s.assets = append(s.assets, asset{kind: KindNFT, denom: denom, amount: 1})
146}
147
148func mustCollection(key string) *collection {
149 v := collections.Get(key)
150 if v == nil {
151 panic("collection is not registered with rooms")
152 }
153 return v.(*collection)
154}
155
156func assertTokenID(tid string) {
157 if tid == "" || len(tid) > MaxTokenIDLen {
158 panic("token id must be 1 to " + strconv.Itoa(MaxTokenIDLen) + " bytes")
159 }
160 for _, r := range tid {
161 if r < 0x20 || r == 0x7f {
162 panic("token id has a control character")
163 }
164 }
165}