Smart Contract
GoatedGoatsTraitPack
A.2068315349bdfce5.GoatedGoatsTraitPack
1/*
2 A NFT contract for the Goated Goats trait packs.
3
4 Key Callouts:
5 * Unlimited supply of packs
6 * Contains an id which represents which drop this pack was from == packID
7 * Main id for a pack is auto-increment
8 * Redeemable by public function that accepts in a TraitPacksVoucher
9 * Takes in pack, burns it, and emits a new event.
10 * Have an on/off switch for redeeming packs in case back-end is facing problems and needs to be temporarily turned off
11 * Collection-level metadata
12 * Edition-level metadata
13*/
14
15import NonFungibleToken from 0x1d7e57aa55817448
16import FungibleToken from 0xf233dcee88fe0abe
17import MetadataViews from 0x1d7e57aa55817448
18
19pub contract GoatedGoatsTraitPack: NonFungibleToken {
20 // -----------------------------------------------------------------------
21 // NonFungibleToken Standard Events
22 // -----------------------------------------------------------------------
23
24 pub event ContractInitialized()
25 pub event Withdraw(id: UInt64, from: Address?)
26 pub event Deposit(id: UInt64, to: Address?)
27
28 // -----------------------------------------------------------------------
29 // GoatedGoatsTraitPack Events
30 // -----------------------------------------------------------------------
31
32 pub event Mint(id: UInt64)
33 pub event Burn(id: UInt64)
34
35 // -----------------------------------------------------------------------
36 // Named Paths
37 // -----------------------------------------------------------------------
38
39 pub let CollectionStoragePath: StoragePath
40 pub let CollectionPublicPath: PublicPath
41
42 // -----------------------------------------------------------------------
43 // NonFungibleToken Standard Fields
44 // -----------------------------------------------------------------------
45
46 pub var totalSupply: UInt64
47
48 // -----------------------------------------------------------------------
49 // GoatedGoatsTraitPack Fields
50 // -----------------------------------------------------------------------
51
52 pub var name: String
53
54 access(self) var collectionMetadata: { String: String }
55 access(self) let idToTraitPackMetadata: { UInt64: TraitPackMetadata }
56
57 pub struct TraitPackMetadata {
58 pub let metadata: { String: String }
59
60 init(metadata: { String: String }) {
61 self.metadata = metadata
62 }
63 }
64
65 pub resource NFT : NonFungibleToken.INFT, MetadataViews.Resolver {
66 pub let id: UInt64
67 pub let packID: UInt64
68 pub let packEditionID: UInt64
69
70 pub fun getViews(): [Type] {
71 return [
72 Type<MetadataViews.Display>()
73 ]
74 }
75
76 pub fun resolveView(_ view: Type): AnyStruct? {
77 switch view {
78 case Type<MetadataViews.Display>():
79 var ipfsImage = MetadataViews.IPFSFile(cid: "No thumbnail cid set", path: "No thumbnail pat set")
80 if (self.getMetadata().containsKey("thumbnailCID")) {
81 ipfsImage = MetadataViews.IPFSFile(cid: self.getMetadata()["thumbnailCID"]!, path: self.getMetadata()["thumbnailPath"])
82 }
83 return MetadataViews.Display(
84 name: self.getMetadata()["name"] ?? "Goated Goat Trait Pack ".concat(self.id.toString()),
85 description: self.getMetadata()["description"] ?? "No description set",
86 thumbnail: ipfsImage
87 )
88 }
89
90 return nil
91 }
92
93 pub fun getMetadata(): {String: String} {
94 if (GoatedGoatsTraitPack.idToTraitPackMetadata[self.id] != nil) {
95 return GoatedGoatsTraitPack.idToTraitPackMetadata[self.id]!.metadata
96 } else {
97 return {}
98 }
99 }
100
101 init(id: UInt64, packID: UInt64, packEditionID: UInt64) {
102 self.id = id
103 self.packID = packID
104 self.packEditionID = packEditionID
105 emit Mint(id: self.id)
106 }
107
108 destroy() {
109 emit Burn(id: self.id)
110 }
111 }
112
113 pub resource interface TraitPackCollectionPublic {
114 pub fun deposit(token: @NonFungibleToken.NFT)
115 pub fun getIDs(): [UInt64]
116 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT
117 pub fun borrowTraitPack(id: UInt64): &GoatedGoatsTraitPack.NFT? {
118 post {
119 (result == nil) || (result?.id == id):
120 "Cannot borrow GoatedGoatsTraitPack reference: The ID of the returned reference is incorrect"
121 }
122 }
123 }
124
125 pub resource Collection: TraitPackCollectionPublic, NonFungibleToken.Provider, NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection {
126 pub var ownedNFTs: @{UInt64: NonFungibleToken.NFT}
127
128 init () {
129 self.ownedNFTs <- {}
130 }
131
132 pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
133 let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")
134 emit Withdraw(id: token.id, from: self.owner?.address)
135 return <-token
136 }
137
138 pub fun deposit(token: @NonFungibleToken.NFT) {
139 let token <- token as! @GoatedGoatsTraitPack.NFT
140 let id: UInt64 = token.id
141 let oldToken <- self.ownedNFTs[id] <- token
142 emit Deposit(id: id, to: self.owner?.address)
143 destroy oldToken
144 }
145
146 pub fun getIDs(): [UInt64] {
147 return self.ownedNFTs.keys
148 }
149
150 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
151 return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
152 }
153
154 pub fun borrowTraitPack(id: UInt64): &GoatedGoatsTraitPack.NFT? {
155 if self.ownedNFTs[id] != nil {
156 let ref = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
157 return ref as! &GoatedGoatsTraitPack.NFT
158 } else {
159 return nil
160 }
161 }
162
163 pub fun borrowViewResolver(id: UInt64): &AnyResource{MetadataViews.Resolver} {
164 let nft = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
165 let traitPack = nft as! &GoatedGoatsTraitPack.NFT
166 return traitPack as &AnyResource{MetadataViews.Resolver}
167 }
168
169 destroy() {
170 destroy self.ownedNFTs
171 }
172 }
173
174 // -----------------------------------------------------------------------
175 // Admin Functions
176 // -----------------------------------------------------------------------
177 access(account) fun setEditionMetadata(editionNumber: UInt64, metadata: {String: String}) {
178 self.idToTraitPackMetadata[editionNumber] = TraitPackMetadata(metadata: metadata)
179 }
180
181 access(account) fun setCollectionMetadata(metadata: {String: String}) {
182 self.collectionMetadata = metadata
183 }
184
185 access(account) fun mint(nftID: UInt64, packID: UInt64, packEditionID: UInt64) : @NonFungibleToken.NFT {
186 self.totalSupply = self.totalSupply + 1
187 return <-create NFT(id: nftID, packID: packID, packEditionID: packEditionID)
188 }
189
190 // -----------------------------------------------------------------------
191 // Public Functions
192 // -----------------------------------------------------------------------
193 pub fun getTotalSupply(): UInt64 {
194 return self.totalSupply
195 }
196
197 pub fun getName(): String {
198 return self.name
199 }
200
201 pub fun getCollectionMetadata(): {String: String} {
202 return self.collectionMetadata
203 }
204
205 pub fun getEditionMetadata(_ edition: UInt64): {String: String} {
206 if (self.idToTraitPackMetadata[edition] != nil) {
207 return self.idToTraitPackMetadata[edition]!.metadata
208 } else {
209 return {}
210 }
211 }
212
213 // -----------------------------------------------------------------------
214 // NonFungibleToken Standard Functions
215 // -----------------------------------------------------------------------
216 pub fun createEmptyCollection(): @NonFungibleToken.Collection {
217 return <- create Collection()
218 }
219
220 init() {
221 self.name = "Goated Goats Trait Pack"
222 self.totalSupply = 0
223
224 self.collectionMetadata = {}
225 self.idToTraitPackMetadata = {}
226
227 self.CollectionStoragePath = /storage/GoatTraitPackCollection
228 self.CollectionPublicPath = /public/GoatTraitPackCollection
229
230 emit ContractInitialized()
231 }
232}
233
234