Smart Contract
Tires
A.f887ece39166906e.Tires
1// import NonFungibleToken from "./NonFungibleToken.cdc"
2import NonFungibleToken from 0x1d7e57aa55817448
3import ViewResolver from 0x1d7e57aa55817448
4import MetadataViews from 0x1d7e57aa55817448
5
6access(all) contract Tires: NonFungibleToken {
7
8 access(all) let CollectionStoragePath: StoragePath
9 access(all) let CollectionPublicPath: PublicPath
10 access(all) let MinterStoragePath: StoragePath
11
12 access(all) var totalSupply: UInt64
13
14 access(all) resource interface Public {}
15
16 //you can extend these fields if you need
17 access(all) struct Metadata {
18 access(all) let name: String
19 access(all) let ipfsLink: String
20
21 init(name: String,ipfsLink: String) {
22 self.name=name
23 //Stored in the ipfs
24 self.ipfsLink=ipfsLink
25 }
26 }
27
28 access(all) resource NFT: NonFungibleToken.NFT, Public {
29 access(all) let id: UInt64
30 access(all) let metadata: Metadata
31 access(all) let type: UInt64
32
33 access(all) view fun getMetadata(): Metadata {
34 return self.metadata
35 }
36
37 init(initID: UInt64, metadata: Metadata, type: UInt64) {
38 self.id = initID
39 self.metadata=metadata
40 self.type=type
41 }
42
43 access(all) fun createEmptyCollection(): @{NonFungibleToken.Collection} {
44 return <- Tires.createEmptyCollection(nftType: Type<@NFT>())
45 }
46
47 access(all) view fun getViews(): [Type] {
48 return [
49 Type<MetadataViews.Display>(),
50 Type<MetadataViews.ExternalURL>(),
51 Type<MetadataViews.NFTCollectionData>(),
52 Type<MetadataViews.NFTCollectionDisplay>(),
53 Type<Tires.Metadata>(),
54 Type<MetadataViews.Royalties>()
55 ]
56 }
57
58 access(all) fun resolveView(_ view: Type): AnyStruct? {
59 switch view {
60 case Type<MetadataViews.Display>():
61 return MetadataViews.Display(
62 name: self.metadata.name,
63 description: "",
64 thumbnail: MetadataViews.IPFSFile(
65 cid: self.metadata.ipfsLink,
66 path: nil
67 )
68 )
69 case Type<MetadataViews.ExternalURL>():
70 return MetadataViews.ExternalURL("https://driverz.world/")
71 case Type<MetadataViews.NFTCollectionData>():
72 return Tires.resolveContractView(resourceType: Type<@NFT>(), viewType: Type<MetadataViews.NFTCollectionData>())
73 case Type<MetadataViews.NFTCollectionDisplay>():
74 return Tires.resolveContractView(resourceType: Type<@NFT>(), viewType: Type<MetadataViews.NFTCollectionDisplay>())
75 case Type<Tires.Metadata>():
76 return self.metadata
77 case Type<MetadataViews.Royalties>():
78 return MetadataViews.Royalties([])
79 }
80 return nil
81 }
82 }
83
84 access(all) resource interface TiresCollectionPublic {
85 access(all) fun deposit(token: @{NonFungibleToken.NFT})
86 access(all) view fun getIDs(): [UInt64]
87 access(all) fun borrowArt(id: UInt64): &Tires.NFT? {
88 // If the result isn't nil, the id of the returned reference
89 // should be the same as the argument to the function
90 post {
91 (result == nil) || (result?.id == id):
92 "Cannot borrow Moment reference: The ID of the returned reference is incorrect"
93 }
94 }
95 }
96
97 access(all) resource Collection: TiresCollectionPublic, NonFungibleToken.Collection {
98 access(all) var ownedNFTs: @{UInt64: {NonFungibleToken.NFT}}
99
100 access(NonFungibleToken.Withdraw) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
101 let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")
102 return <-token
103 }
104
105 access(all) fun deposit(token: @{NonFungibleToken.NFT}) {
106 let token <- token as! @Tires.NFT
107 let id: UInt64 = token.id
108 let oldToken <- self.ownedNFTs[id] <- token
109 destroy oldToken
110 }
111
112 access(all) view fun getSupportedNFTTypes(): {Type: Bool} {
113 let supportedTypes: {Type: Bool} = {}
114 supportedTypes[Type<@NFT>()] = true
115 return supportedTypes
116 }
117
118 access(all) view fun isSupportedNFTType(type: Type): Bool {
119 return type == Type<@NFT>()
120 }
121
122 access(all) view fun getIDs(): [UInt64] {
123 return self.ownedNFTs.keys
124 }
125
126 access(all) view fun borrowViewResolver(id: UInt64): &{ViewResolver.Resolver}? {
127 if let nft = &self.ownedNFTs[id] as &{NonFungibleToken.NFT}? {
128 return nft as &{ViewResolver.Resolver}
129 }
130 return nil
131 }
132
133 access(all) view fun borrowNFT(_ id: UInt64): &{NonFungibleToken.NFT}? {
134 return (&self.ownedNFTs[id] as &{NonFungibleToken.NFT}?)
135 }
136
137 access(all) fun borrowArt(id: UInt64): &Tires.NFT? {
138 if let nft = &self.ownedNFTs[id] as &{NonFungibleToken.NFT}? {
139 return nft as! &NFT
140 }
141 return nil
142 }
143
144 access(all) fun createEmptyCollection(): @{NonFungibleToken.Collection} {
145 return <- Tires.createEmptyCollection(nftType: Type<@NFT>())
146 }
147
148 init () {
149 self.ownedNFTs <- {}
150 }
151 }
152
153 access(all) fun createEmptyCollection(nftType: Type): @{NonFungibleToken.Collection} {
154 return <- create Collection()
155 }
156
157 access(all) struct NftData {
158 access(all) let metadata: Tires.Metadata
159 access(all) let id: UInt64
160 access(all) let type: UInt64
161 init(metadata: Tires.Metadata, id: UInt64, type: UInt64) {
162 self.metadata= metadata
163 self.id=id
164 self.type=type
165 }
166 }
167
168 access(all) fun getNft(address:Address) : [NftData] {
169 var artData: [NftData] = []
170 let account=getAccount(address)
171
172 if let artCollection= account.capabilities.borrow<&Tires.Collection>(self.CollectionPublicPath) {
173 for id in artCollection.getIDs() {
174 var art=artCollection.borrowArt(id: id)
175 artData.append(NftData(metadata: art!.getMetadata(), id: id, type: art!.type))
176 }
177 }
178 return artData
179 }
180
181 access(all) resource NFTMinter {
182 access(all) fun mintNFT(
183 recipient: &{NonFungibleToken.CollectionPublic},
184 name: String,
185 ipfsLink: String,
186 type: UInt64
187 ) {
188 recipient.deposit(token: <-create Tires.NFT(
189 initID: Tires.totalSupply,
190 metadata: Metadata(
191 name: name,
192 ipfsLink:ipfsLink,
193 ),
194 type: type))
195
196 Tires.totalSupply = Tires.totalSupply + 1
197 }
198 }
199
200 access(all) view fun getContractViews(resourceType: Type?): [Type] {
201 return [
202 Type<MetadataViews.NFTCollectionData>(),
203 Type<MetadataViews.NFTCollectionDisplay>()
204 ]
205 }
206
207 access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? {
208 switch viewType {
209 case Type<MetadataViews.NFTCollectionData>():
210 let collectionData = MetadataViews.NFTCollectionData(
211 storagePath: self.CollectionStoragePath,
212 publicPath: self.CollectionPublicPath,
213 publicCollection: Type<&Collection>(),
214 publicLinkedType: Type<&Collection>(),
215 createEmptyCollectionFunction: (fun(): @{NonFungibleToken.Collection} {
216 return <- Tires.createEmptyCollection(nftType: Type<@NFT>())
217 })
218 )
219 return collectionData
220 case Type<MetadataViews.NFTCollectionDisplay>():
221 let squareMedia = MetadataViews.Media(
222 file: MetadataViews.HTTPFile(
223 url: "https://driverz.world/DriverzNFT-logo.png"
224 ),
225 mediaType: "image"
226 )
227 let bannerMedia = MetadataViews.Media(
228 file: MetadataViews.HTTPFile(
229 url: "https://driverz.world/DriverzNFT-logo.png"
230 ),
231 mediaType: "image"
232 )
233 return MetadataViews.NFTCollectionDisplay(
234 name: "Driverz Utility Tires",
235 description: "Driverz Tire Collection",
236 externalURL: MetadataViews.ExternalURL("https://driverz.world/"),
237 squareImage: squareMedia,
238 bannerImage: bannerMedia,
239 socials: {
240 "twitter": MetadataViews.ExternalURL("https://twitter.com/DriverzWorld"),
241 "discord": MetadataViews.ExternalURL("https://discord.gg/driverz"),
242 "instagram": MetadataViews.ExternalURL("https://www.instagram.com/driverzworld")
243 }
244 )
245 }
246 return nil
247 }
248
249 init() {
250 self.CollectionStoragePath = /storage/TiresCollection
251 self.CollectionPublicPath = /public/TiresCollection
252 self.MinterStoragePath = /storage/TiresMinter
253
254 self.totalSupply = 0
255
256 let minter <- create NFTMinter()
257 self.account.storage.save(<-minter, to: self.MinterStoragePath)
258
259 let collection <- create Collection()
260 self.account.storage.save(<-collection, to: Tires.CollectionStoragePath)
261 let collectionCap = self.account.capabilities.storage.issue<&Collection>(self.CollectionStoragePath)
262 self.account.capabilities.publish(collectionCap, at: self.CollectionPublicPath)
263 }
264}