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