Smart Contract
Wheel
A.f887ece39166906e.Wheel
1import NonFungibleToken from 0x1d7e57aa55817448
2import MetadataViews from 0x1d7e57aa55817448
3import ViewResolver from 0x1d7e57aa55817448
4
5access(all) contract Wheel: 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 WheelMetadata {
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 <- Wheel.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<Wheel.WheelMetadata>(),
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 Wheel.resolveContractView(resourceType: Type<@NFT>(), viewType: Type<MetadataViews.NFTCollectionData>())
70 case Type<MetadataViews.NFTCollectionDisplay>():
71 return Wheel.resolveContractView(resourceType: Type<@NFT>(), viewType: Type<MetadataViews.NFTCollectionDisplay>())
72 case Type<Wheel.WheelMetadata>():
73 return Wheel.WheelMetadata(
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 borrowArt(id: UInt64): &Wheel.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 access(all) fun deposit(token: @{NonFungibleToken.NFT})
96 access(all) view fun getIDs(): [UInt64]
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! @Wheel.NFT
109 let id: UInt64 = token.id
110 let oldToken <- self.ownedNFTs[id] <- token
111 destroy oldToken
112 }
113
114 access(all) view fun getSupportedNFTTypes(): {Type: Bool} {
115 let supportedTypes: {Type: Bool} = {}
116 supportedTypes[Type<@NFT>()] = true
117 return supportedTypes
118 }
119
120 access(all) view fun isSupportedNFTType(type: Type): Bool {
121 return type == Type<@NFT>()
122 }
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) fun borrowArt(id: UInt64): &Wheel.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 <- Wheel.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 Wheel.NFT(
167 initID: Wheel.totalSupply,
168 name: name,
169 description: description,
170 ipfsLink: ipfsLink
171 ))
172
173 Wheel.totalSupply = Wheel.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 <- Wheel.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 Wheels",
212 description: "Driverz Wheel 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/WheelCollection
228 self.CollectionPublicPath = /public/WheelCollection
229 self.AdminStoragePath = /storage/WheelMinter
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: Wheel.CollectionStoragePath)
238 let collectionCap = self.account.capabilities.storage.issue<&Collection>(self.CollectionStoragePath)
239 self.account.capabilities.publish(collectionCap, at: self.CollectionPublicPath)
240 }
241}