Smart Contract
AccountV2Migration
A.e467b9dd11fa00df.AccountV2Migration
1access(all)
2contract AccountV2Migration {
3
4 access(all)
5 enum StorageFormat: UInt8 {
6 access(all)
7 case Unknown
8
9 access(all)
10 case V1
11
12 access(all)
13 case V2
14 }
15
16 access(all)
17 event Migrated(
18 addressStartIndex: UInt64,
19 count: UInt64
20 )
21
22 access(all)
23 resource Admin {
24 access(all)
25 fun setNextAddressStartIndex(_ nextAddressStartIndex: UInt64) {
26 AccountV2Migration.nextAddressStartIndex = nextAddressStartIndex
27 }
28
29 access(all)
30 fun setBatchSize(_ batchSize: UInt64) {
31 AccountV2Migration.batchSize = batchSize
32 }
33
34 access(all)
35 fun setMaxAddressIndex(_ maxAddressIndex: UInt64) {
36 AccountV2Migration.maxAddressIndex = maxAddressIndex
37 }
38
39 access(all)
40 fun migrateNextBatch() {
41 AccountV2Migration.migrateNextBatch()
42 }
43 }
44
45 access(all)
46 let adminStoragePath: StoragePath
47
48 access(all)
49 var nextAddressStartIndex: UInt64
50
51 access(all)
52 var maxAddressIndex: UInt64
53
54 access(all)
55 var batchSize: UInt64
56
57 init() {
58 self.adminStoragePath = /storage/accountV2MigrationAdmin
59 self.nextAddressStartIndex = 1
60 self.maxAddressIndex = 0
61 self.batchSize = 0
62
63 self.account.storage.save(
64 <-create Admin(),
65 to: self.adminStoragePath
66 )
67 }
68
69 access(account)
70 fun migrateNextBatch() {
71 var batchSize = self.batchSize
72 if batchSize <= 0 {
73 return
74 }
75
76 let startIndex = self.nextAddressStartIndex
77 if startIndex > self.maxAddressIndex {
78 return
79 }
80
81 let maxBatchSize = self.maxAddressIndex - startIndex + 1
82 if batchSize > maxBatchSize {
83 batchSize = maxBatchSize
84 }
85
86 if !scheduleAccountV2Migration(
87 addressStartIndex: startIndex,
88 count: batchSize
89 ) {
90 return
91 }
92
93 self.nextAddressStartIndex = startIndex + batchSize
94
95 emit Migrated(
96 addressStartIndex: startIndex,
97 count: batchSize
98 )
99 }
100
101 access(all)
102 fun getAccountStorageFormat(address: Address): StorageFormat? {
103 let rawStorageFormat = getAccountStorageFormat(address: address)
104 return StorageFormat(rawValue: rawStorageFormat)
105 }
106}
107