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