Smart Contract
FooBar
A.f7913d7a57ba6555.FooBar
1import NonFungibleToken from 0x1d7e57aa55817448 //Mainnet address: 0x1d7e57aa55817448
2import MetadataViews from 0x1d7e57aa55817448 //Mainnet address: 0x1d7e57aa55817448
3
4access(all) contract FooBar {
5
6 /// Standard Paths
7 access(all) let CollectionStoragePath: StoragePath
8 access(all) let CollectionPublicPath: PublicPath
9
10 /// Path where the minter should be stored
11 access(all) let MinterStoragePath: StoragePath
12
13 access(all) resource NFT: NonFungibleToken.NFT {
14 access(all) let id: UInt64
15
16 init() {
17 self.id = self.uuid
18 }
19
20 access(all) fun createEmptyCollection(): @{NonFungibleToken.Collection} {
21 return <-FooBar.createEmptyCollection(nftType: Type<@FooBar.NFT>())
22 }
23
24 /// Gets a list of views specific to the individual NFT
25 access(all) view fun getViews(): [Type] {
26 return [
27 Type<MetadataViews.Display>(),
28 Type<MetadataViews.Editions>(),
29 Type<MetadataViews.NFTCollectionData>(),
30 Type<MetadataViews.NFTCollectionDisplay>(),
31 Type<MetadataViews.Serial>()
32 ]
33 }
34
35 /// Resolves a view for this specific NFT
36 access(all) fun resolveView(_ view: Type): AnyStruct? {
37 switch view {
38 case Type<MetadataViews.Display>():
39 return MetadataViews.Display(
40 name: "FooBar Example Token",
41 description: "An Example NFT Contract from the Flow NFT Guide",
42 thumbnail: MetadataViews.HTTPFile(
43 url: "Fill this in with a URL to a thumbnail of the NFT"
44 )
45 )
46 case Type<MetadataViews.Editions>():
47 // There is no max number of NFTs that can be minted from this contract
48 // so the max edition field value is set to nil
49 let editionInfo = MetadataViews.Edition(name: "FooBar Edition", number: self.id, max: nil)
50 let editionList: [MetadataViews.Edition] = [editionInfo]
51 return MetadataViews.Editions(
52 editionList
53 )
54 case Type<MetadataViews.Serial>():
55 return MetadataViews.Serial(
56 self.id
57 )
58 case Type<MetadataViews.NFTCollectionData>():
59 return FooBar.resolveContractView(resourceType: Type<@FooBar.NFT>(), viewType: Type<MetadataViews.NFTCollectionData>())
60 case Type<MetadataViews.NFTCollectionDisplay>():
61 return FooBar.resolveContractView(resourceType: Type<@FooBar.NFT>(), viewType: Type<MetadataViews.NFTCollectionDisplay>())
62 }
63 return nil
64 }
65 }
66
67 access(all) resource Collection: NonFungibleToken.Collection {
68
69 access(all) var ownedNFTs: @{UInt64: {NonFungibleToken.NFT}}
70
71 init() {
72 self.ownedNFTs <- {}
73 }
74
75 /// deposit takes a NFT and adds it to the collections dictionary
76 /// and adds the ID to the id array
77 access(all) fun deposit(token: @{NonFungibleToken.NFT}) {
78 let token <- token as! @FooBar.NFT
79 let id = token.id
80
81 // add the new token to the dictionary which removes the old one
82 let oldToken <- self.ownedNFTs[token.id] <- token
83
84 destroy oldToken
85 }
86
87 /// withdraw removes an NFT from the collection and moves it to the caller
88 access(NonFungibleToken.Withdraw) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
89 let token <- self.ownedNFTs.remove(key: withdrawID)
90 ?? panic("Could not withdraw an NFT with the provided ID from the collection")
91
92 return <-token
93 }
94
95 /// getIDs returns an array of the IDs that are in the collection
96 access(all) view fun getIDs(): [UInt64] {
97 return self.ownedNFTs.keys
98 }
99
100 /// getSupportedNFTTypes returns a list of NFT types that this receiver accepts
101 access(all) view fun getSupportedNFTTypes(): {Type: Bool} {
102 let supportedTypes: {Type: Bool} = {}
103 supportedTypes[Type<@FooBar.NFT>()] = true
104 return supportedTypes
105 }
106
107 /// Returns whether or not the given type is accepted by the collection
108 /// A collection that can accept any type should just return true by default
109 access(all) view fun isSupportedNFTType(type: Type): Bool {
110 return type == Type<@FooBar.NFT>()
111 }
112
113 /// Allows a caller to borrow a reference to a specific NFT
114 /// so that they can get the metadata views for the specific NFT
115 access(all) view fun borrowNFT(_ id: UInt64): &{NonFungibleToken.NFT}? {
116 return (&self.ownedNFTs[id] as &{NonFungibleToken.NFT}?)
117 }
118
119 /// createEmptyCollection creates an empty Collection of the same type
120 /// and returns it to the caller
121 /// @return A an empty collection of the same type
122 access(all) fun createEmptyCollection(): @{NonFungibleToken.Collection} {
123 return <-FooBar.createEmptyCollection(nftType: Type<@FooBar.NFT>())
124 }
125
126 }
127
128 /// createEmptyCollection creates an empty Collection for the specified NFT type
129 /// and returns it to the caller so that they can own NFTs
130 access(all) fun createEmptyCollection(nftType: Type): @{NonFungibleToken.Collection} {
131 return <- create Collection()
132 }
133
134 /// Gets a list of views for all the NFTs defined by this contract
135 access(all) view fun getContractViews(resourceType: Type?): [Type] {
136 return [
137 Type<MetadataViews.NFTCollectionData>(),
138 Type<MetadataViews.NFTCollectionDisplay>()
139 ]
140 }
141
142 /// Resolves a view that applies to all the NFTs defined by this contract
143 access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? {
144 switch viewType {
145 case Type<MetadataViews.NFTCollectionData>():
146 let collectionData = MetadataViews.NFTCollectionData(
147 storagePath: self.CollectionStoragePath,
148 publicPath: self.CollectionPublicPath,
149 publicCollection: Type<&FooBar.Collection>(),
150 publicLinkedType: Type<&FooBar.Collection>(),
151 createEmptyCollectionFunction: (fun(): @{NonFungibleToken.Collection} {
152 return <-FooBar.createEmptyCollection(nftType: Type<@FooBar.NFT>())
153 })
154 )
155 return collectionData
156 case Type<MetadataViews.NFTCollectionDisplay>():
157 let media = MetadataViews.Media(
158 file: MetadataViews.HTTPFile(
159 url: "Add your own SVG+XML link here"
160 ),
161 mediaType: "image/svg+xml"
162 )
163 return MetadataViews.NFTCollectionDisplay(
164 name: "The FooBar Example Collection",
165 description: "This collection is used as an example to help you develop your next Flow NFT.",
166 externalURL: MetadataViews.ExternalURL("Add your own link here"),
167 squareImage: media,
168 bannerImage: media,
169 socials: {
170 "twitter": MetadataViews.ExternalURL("Add a link to your project's twitter")
171 }
172 )
173 }
174 return nil
175 }
176
177 access(all) resource NFTMinter {
178 access(all) fun createNFT(): @NFT {
179 return <-create NFT()
180 }
181
182 init() {}
183 }
184
185 init() {
186 // Set the named paths
187 self.CollectionStoragePath = /storage/fooBarNFTCollection
188 self.CollectionPublicPath = /public/fooBarNFTCollection
189 self.MinterStoragePath = /storage/fooBarNFTMinter
190 self.account.storage.save(<- create NFTMinter(), to: /storage/fooBarNFTMinter)
191 }
192}