Smart Contract
AFLUpgradeRegistry
A.8f9231920da9af6d.AFLUpgradeRegistry
1import AFLNFT from 0x8f9231920da9af6d
2
3access(all) contract AFLUpgradeRegistry {
4
5 access(account) let registeredMintsByTemplateId: {UInt64: [Address]}
6
7 // function to register a mint
8 access(account) fun registerMint(templateId: UInt64, userAddress: Address) {
9 pre {
10 userAddress != nil: "User address must be provided for mint registration."
11 }
12
13 if self.registeredMintsByTemplateId[templateId] == nil {
14 self.registeredMintsByTemplateId[templateId] = []
15 }
16
17 self.registeredMintsByTemplateId[templateId]!.append(userAddress)
18 }
19
20 access(all) fun getCurrentMint(templateId: UInt64): [Address]? {
21 return self.registeredMintsByTemplateId[templateId]
22 }
23
24 access(all) fun getCurrentMintCount(templateId: UInt64): UInt64 {
25 return UInt64(self.registeredMintsByTemplateId[templateId]?.length ?? 0)
26 }
27
28 access(all) fun getAll(): {UInt64: [Address]} {
29 return self.registeredMintsByTemplateId
30 }
31
32 // Initiate Packing
33 access(account) fun createPacks(templateId: UInt64): AnyStruct {
34 // Each user gets a pack with as many mints as they have registered
35 // The mint numbers are randomized and the maxSerial the total number of mints
36
37 let mints: [Address] = self.registeredMintsByTemplateId[templateId]!
38
39 let maxSerial: UInt64 = UInt64(mints.length)
40 let usedSerials: [UInt64] = []
41
42 let packMetadata: {String: AnyStruct} = {
43 "templateId": templateId.toString(),
44 "mintCount": maxSerial.toString(),
45 "moments": []
46 }
47
48 var momentsByAddress: {Address: [{String: AnyStruct}]} = {}
49 for mint in mints {
50 var serial: UInt64 = UInt64(revertibleRandom(modulo: maxSerial))
51 // Ensure the serial is unique
52 while usedSerials.contains(serial) {
53 serial = UInt64(revertibleRandom(modulo: maxSerial))
54 }
55
56 // Add metadata for the tempalteId and serial number to the moment
57 let momentMetadata: {String: AnyStruct} = {
58 "templateId": templateId.toString(),
59 "serial": serial.toString()
60 }
61
62 if momentsByAddress[mint] == nil {
63 momentsByAddress[mint] = []
64 }
65 momentsByAddress[mint]!.append(momentMetadata)
66 }
67
68 return momentsByAddress
69 }
70
71 init() {
72 self.registeredMintsByTemplateId = {}
73 }
74}