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