-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmigration_example.rs
207 lines (187 loc) · 7.05 KB
/
migration_example.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use super::*;
fn post_upgrade_example() {
match ic_cdk::storage::stable_restore::<(LegacyTokenLevelMetadata, LegacyLedger)>() {
Ok((legacy_metadata_store, legacy_ledger_store)) => {
METADATA.with(|metadata| {
*metadata.borrow_mut() = Metadata {
name: Some(legacy_metadata_store.name),
logo: None, // manual via setLogo
symbol: Some(legacy_metadata_store.symbol),
custodians: legacy_metadata_store
.owner
.map(|custodian| HashSet::from([custodian]))
.unwrap_or_else(HashSet::new),
created_at: time(),
upgraded_at: time(),
};
metadata.borrow_mut().upgraded_at = time();
});
LEDGER.with(|ledger| {
let tokens = legacy_ledger_store
.tokens
.into_iter()
.map(|(k, v)| {
(
Nat::from(k),
TokenMetadata {
token_identifier: Nat::from(k),
owner: Some(v.principal),
operator: None, // for safety
is_burned: false,
properties: v
.metadata_desc
.into_iter()
.flat_map(|desc| desc.key_val_data)
.collect::<Vec<MetadataKeyVal>>()
.into_iter()
.map(|pair| {
(
pair.key,
match pair.val {
MetadataVal::TextContent(v) => {
GenericValue::TextContent(v)
}
MetadataVal::BlobContent(v) => {
GenericValue::BlobContent(v)
}
MetadataVal::NatContent(v) => {
GenericValue::NatContent(v)
}
MetadataVal::Nat8Content(v) => {
GenericValue::Nat8Content(v)
}
MetadataVal::Nat16Content(v) => {
GenericValue::Nat16Content(v)
}
MetadataVal::Nat32Content(v) => {
GenericValue::Nat32Content(v)
}
MetadataVal::Nat64Content(v) => {
GenericValue::Nat64Content(v)
}
},
)
})
.collect::<Vec<(String, GenericValue)>>(),
minted_at: time(),
minted_by: v.principal,
transferred_at: None,
transferred_by: None,
burned_at: None,
burned_by: None,
},
)
})
.collect::<HashMap<TokenIdentifier, TokenMetadata>>();
let mut owners = HashMap::new();
for (_, v) in tokens.iter() {
owners
.entry(v.owner.unwrap()) // note on BurnedNFT
.or_insert_with(HashSet::new)
.insert(v.token_identifier.clone());
}
*ledger.borrow_mut() = Ledger {
tokens,
owners,
// reset all operators for safeness
operators: Default::default(),
// consider import from cap or continue counting forward
// or even continue using from old cap root bucket
tx_records: vec![],
};
});
}
Err(err) => {
trap(&format!(
"An error occurred when loading from stable memory (post_upgrade): {:?}",
err
));
}
}
}
#[derive(CandidType, Deserialize)]
struct LegacyTokenLevelMetadata {
owner: Option<Principal>,
symbol: String,
name: String,
history: Option<Principal>,
}
#[derive(CandidType, Deserialize)]
struct LegacyLedger {
tokens: HashMap<TokenIndex, LegacyTokenMetadata>,
user_tokens: HashMap<User, Vec<TokenIndex>>,
token_approvals: HashMap<TokenIndex, User>,
operator_approvals: HashMap<User, Approvals>,
}
#[derive(CandidType, Deserialize)]
struct LegacyTokenMetadata {
account_identifier: AccountIdentifier,
metadata: LegacyMetadata,
token_identifier: LegacyTokenIdentifier,
principal: Principal,
metadata_desc: MetadataDesc,
}
type LegacyTokenIdentifier = String;
type TokenIndex = u64;
type Approvals = Vec<User>;
#[derive(CandidType, Debug, Deserialize, Eq, Hash, PartialEq)]
enum User {
address(AccountIdentifier),
principal(Principal),
}
type AccountIdentifier = String;
#[derive(CandidType, Deserialize)]
enum LegacyMetadata {
fungible(FungibleMetadata),
nonfungible(Option<MetadataContainer>),
}
#[derive(CandidType, Deserialize)]
struct FungibleMetadata {
name: String,
symbol: String,
decimals: u8,
metadata: Option<MetadataContainer>,
}
type Blob = Vec<u8>;
#[derive(CandidType, Deserialize)]
enum MetadataContainer {
data(Vec<MetadataValue>),
blob(Blob),
json(String),
}
#[derive(CandidType, Deserialize)]
struct MetadataValue(String, Value);
#[derive(Clone, CandidType, Deserialize)]
enum Value {
text(String),
blob(Blob),
nat(Nat),
nat8(u8),
}
type MetadataDesc = Vec<MetadataPart>;
#[derive(CandidType, Deserialize)]
struct MetadataPart {
purpose: MetadataPurpose,
key_val_data: Vec<MetadataKeyVal>,
data: Vec<u8>,
}
#[derive(CandidType, Deserialize)]
enum MetadataPurpose {
Preview,
Rendered,
}
#[derive(CandidType, Deserialize)]
struct MetadataKeyVal {
key: String,
val: MetadataVal,
}
#[derive(CandidType, Deserialize)]
enum MetadataVal {
TextContent(String),
BlobContent(Vec<u8>),
NatContent(Nat),
Nat8Content(u8),
Nat16Content(u16),
Nat32Content(u32),
Nat64Content(u64),
}