Smart Contract
DriverzExclusive
A.f887ece39166906e.DriverzExclusive
1
2// Mainnet
3import NonFungibleToken from 0x1d7e57aa55817448
4import MetadataViews from 0x1d7e57aa55817448
5
6//Testnet
7// import NonFungibleToken from 0x631e88ae7f1d7c20
8// import MetadataViews from 0x631e88ae7f1d7c20
9
10pub contract DriverzExclusive: NonFungibleToken {
11
12 //Define Events
13 pub event ContractInitialized()
14 pub event Withdraw(id: UInt64, from: Address?)
15 pub event Deposit(id: UInt64, to: Address?)
16 pub event ExclusiveMinted(id: UInt64, name: String, description: String, image: String, traits: {String:String})
17
18 //Define Paths
19 pub let CollectionStoragePath: StoragePath
20 pub let CollectionPublicPath: PublicPath
21 pub let CollectionPrivatePath: PrivatePath
22 pub let AdminStoragePath: StoragePath
23
24 //Difine Total Supply
25 pub var totalSupply: UInt64
26
27 pub struct driverzExclusiveMetadata {
28 pub let id: UInt64
29 pub let name: String
30 pub let description: String
31 pub let image: String
32 pub let traits: {String:String}
33
34 init(_id: UInt64, _name: String, _description: String, _image: String, _traits:{String:String}) {
35 self.id = _id
36 self.name = _name
37 self.description = _description
38 self.image = _image
39 self.traits = _traits
40 }
41 }
42
43 pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
44
45 pub let id: UInt64
46 pub let name: String
47 pub let description: String
48 pub var image: String
49 pub let traits: {String: String}
50
51 init( _id: UInt64, _name: String, _description: String, _image: String, _traits: {String:String}) {
52
53 self.id = _id
54 self.name = _name
55 self.description = _description
56 self.image = _image
57 self.traits = _traits
58 }
59
60 pub fun revealThumbnail() {
61 let urlBase = self.image.slice(from: 0, upTo: 47)
62 let newImage = urlBase.concat(self.id.toString()).concat(".png")
63 self.image = newImage
64 }
65
66 pub fun getViews(): [Type] {
67 return [
68 Type<MetadataViews.NFTView>(),
69 Type<MetadataViews.Display>(),
70 Type<MetadataViews.ExternalURL>(),
71 Type<MetadataViews.NFTCollectionData>(),
72 Type<MetadataViews.NFTCollectionDisplay>(),
73 Type<DriverzExclusive.driverzExclusiveMetadata>(),
74 Type<MetadataViews.Royalties>(),
75 Type<MetadataViews.Traits>()
76 ]
77 }
78
79 pub fun resolveView(_ view: Type): AnyStruct? {
80 switch view {
81
82 case Type<MetadataViews.Display>():
83 return MetadataViews.Display(
84 name: self.name,
85 description: self.description,
86 thumbnail: MetadataViews.IPFSFile(
87 cid: self.image,
88 path: nil
89 )
90 )
91
92 case Type<MetadataViews.ExternalURL>():
93 return MetadataViews.ExternalURL("https://driverz.world")
94
95 case Type<MetadataViews.NFTCollectionData>():
96 return MetadataViews.NFTCollectionData(
97 storagePath: DriverzExclusive.CollectionStoragePath,
98 publicPath: DriverzExclusive.CollectionPublicPath,
99 providerPath: DriverzExclusive.CollectionPrivatePath,
100 publicCollection: Type<&Collection{NonFungibleToken.CollectionPublic}>(),
101 publicLinkedType: Type<&Collection{DriverzExclusive.CollectionPublic, NonFungibleToken.CollectionPublic, NonFungibleToken.Receiver, MetadataViews.ResolverCollection}>(),
102 providerLinkedType: Type<&Collection{DriverzExclusive.CollectionPublic, NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection, NonFungibleToken.Provider}>(),
103 createEmptyCollectionFunction: (fun (): @NonFungibleToken.Collection {
104 return <- DriverzExclusive.createEmptyCollection()
105 })
106 )
107
108 case Type<MetadataViews.NFTCollectionDisplay>():
109 let squareMedia = MetadataViews.Media(
110 file: MetadataViews.HTTPFile(
111 url: "https://driverzinc.io/DriverzNFT-logo.png"
112 ),
113 mediaType: "image"
114 )
115 let bannerMedia = MetadataViews.Media(
116 file: MetadataViews.HTTPFile(
117 url: "https://driverzinc.io/DriverzNFT-logo.png"
118 ),
119 mediaType: "image"
120 )
121 return MetadataViews.NFTCollectionDisplay(
122 name: "Driverz Exclusive",
123 description: "Driverz Exclusive Collection",
124 externalURL: MetadataViews.ExternalURL("https://driverz.world"),
125 squareImage: squareMedia,
126 bannerImage: bannerMedia,
127 socials: {
128 "twitter": MetadataViews.ExternalURL("https://twitter.com/DriverzWorld/"),
129 "discord": MetadataViews.ExternalURL("https://discord.gg/driverz"),
130 "instagram": MetadataViews.ExternalURL("https://www.instagram.com/driverzworld/")
131 }
132 )
133
134 case Type<DriverzExclusive.driverzExclusiveMetadata>():
135 return DriverzExclusive.driverzExclusiveMetadata(
136 id: self.id,
137 name: self.name,
138 description: self.description,
139 image: self.image,
140 traits: self.traits
141 )
142
143 case Type<MetadataViews.NFTView>():
144 let viewResolver = &self as &{MetadataViews.Resolver}
145 return MetadataViews.NFTView(
146 id: self.id,
147 uuid: self.uuid,
148 display: MetadataViews.getDisplay(viewResolver),
149 externalURL: MetadataViews.getExternalURL(viewResolver),
150 collectionData: MetadataViews.getNFTCollectionData(viewResolver),
151 collectionDisplay: MetadataViews.getNFTCollectionDisplay(viewResolver),
152 royalties: MetadataViews.getRoyalties(viewResolver),
153 traits: MetadataViews.getTraits(viewResolver)
154 )
155
156 case Type<MetadataViews.Royalties>():
157 return MetadataViews.Royalties([])
158
159 case Type<MetadataViews.Traits>():
160 let traits: [MetadataViews.Trait] = []
161 for trait in self.traits.keys {
162 traits.append(MetadataViews.Trait(
163 trait: trait,
164 value: self.traits[trait]!,
165 displayType: nil,
166 rarity: nil
167 ))
168 }
169 return MetadataViews.Traits(traits: traits)
170
171 }
172 return nil
173 }
174
175
176 }
177
178 pub resource interface CollectionPublic {
179 pub fun deposit(token: @NonFungibleToken.NFT)
180 pub fun getIDs(): [UInt64]
181 pub fun borrowViewResolver(id: UInt64): &{MetadataViews.Resolver}
182 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT
183 pub fun borrowDriverzExclusive(id: UInt64): &DriverzExclusive.NFT? {
184 post {
185 (result == nil) || (result?.id == id):
186 "Cannot borrow DriverzExclusive reference: The ID of the returned reference is incorrect."
187 }
188 }
189 }
190
191 pub resource Collection: CollectionPublic, NonFungibleToken.Provider, NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection {
192 // dictionary of NFT conforming tokens
193 // NFT is a resource type with an 'UInt64' ID field
194 pub var ownedNFTs: @{UInt64: NonFungibleToken.NFT}
195
196 // withdraw removes an NFT from the collection and moves it to the caller
197 pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
198 let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")
199
200 emit Withdraw(id: token.id, from: self.owner?.address)
201
202 return <-token
203 }
204
205 // deposit takes a NFT and adds it to the collections dictionary
206 // and adds the ID to the id array
207 pub fun deposit(token: @NonFungibleToken.NFT) {
208 let token <- token as! @DriverzExclusive.NFT
209
210 let id: UInt64 = token.id
211
212 let oldToken <- self.ownedNFTs[id] <- token
213
214 emit Deposit(id: id, to: self.owner?.address)
215
216 destroy oldToken
217
218 }
219
220 // getIDs returns an array of the IDs that are in the collection
221 pub fun getIDs(): [UInt64] {
222 return self.ownedNFTs.keys
223 }
224
225 pub fun borrowViewResolver(id: UInt64): &{MetadataViews.Resolver} {
226 let nft = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
227 let mainNFT = nft as! &DriverzExclusive.NFT
228 return mainNFT
229 }
230
231
232 // borrowNFT gets a reference to an NFT in the collection
233 // so that the caller can read its metadata and call its methods
234 pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
235 return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
236 }
237
238 pub fun borrowDriverzExclusive(id: UInt64): &DriverzExclusive.NFT? {
239 if self.ownedNFTs[id] != nil {
240 let ref = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
241 return ref as! &DriverzExclusive.NFT
242 } else {
243 return nil
244 }
245 }
246
247 init () {
248 self.ownedNFTs <- {}
249 }
250
251 destroy() {
252 destroy self.ownedNFTs
253 }
254 }
255
256 pub fun createEmptyCollection(): @NonFungibleToken.Collection {
257 return <- create Collection()
258 }
259
260 pub resource Admin {
261 pub fun mintNFT(
262 recipient: &{NonFungibleToken.CollectionPublic},
263 name: String,
264 description: String,
265 image: String,
266 traits: {String:String}
267 ) {
268 emit ExclusiveMinted(id: DriverzExclusive.totalSupply, name: name, description: description, image: image, traits: traits)
269 DriverzExclusive.totalSupply = DriverzExclusive.totalSupply + (1 as UInt64)
270
271 recipient.deposit(token: <- create DriverzExclusive.NFT(
272 initID: DriverzExclusive.totalSupply,
273 name: name,
274 description: description,
275 image: image,
276 traits: traits
277 )
278 )
279 }
280 }
281
282 init() {
283
284 self.CollectionStoragePath = /storage/DriverzExclusiveCollection
285 self.CollectionPublicPath = /public/DriverzExclusiveCollection
286 self.CollectionPrivatePath = /private/DriverzExclusiveCollection
287 self.AdminStoragePath = /storage/DriverzExclusiveMinter
288
289 self.totalSupply = 0
290
291 let minter <- create Admin()
292 self.account.save(<-minter, to: self.AdminStoragePath)
293
294 let collection <- DriverzExclusive.createEmptyCollection()
295 self.account.save(<- collection, to: self.CollectionStoragePath)
296
297 // create a public capability for the collection
298 self.account.link<&DriverzExclusive.Collection{NonFungibleToken.CollectionPublic, DriverzExclusive.CollectionPublic}>(
299 self.CollectionPublicPath,
300 target: self.CollectionStoragePath
301 )
302
303 emit ContractInitialized()
304 }
305}
306