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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use crate::pallet::{expand::merge_where_clauses, Def};
use frame_support_procedural_tools::get_doc_literals;
pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
let frame_support = &def.frame_support;
let frame_system = &def.frame_system;
let type_impl_gen = &def.type_impl_generics(def.pallet_struct.attr_span);
let type_use_gen = &def.type_use_generics(def.pallet_struct.attr_span);
let type_decl_gen = &def.type_decl_generics(def.pallet_struct.attr_span);
let pallet_ident = &def.pallet_struct.pallet;
let config_where_clause = &def.config.where_clause;
let mut storages_where_clauses = vec![&def.config.where_clause];
storages_where_clauses.extend(def.storages.iter().map(|storage| &storage.where_clause));
let storages_where_clauses = merge_where_clauses(&storages_where_clauses);
let pallet_item = {
let pallet_module_items = &mut def.item.content.as_mut().expect("Checked by def").1;
let item = &mut pallet_module_items[def.pallet_struct.index];
if let syn::Item::Struct(item) = item {
item
} else {
unreachable!("Checked by pallet struct parser")
}
};
if let Some(field) = pallet_item.fields.iter_mut().next() {
if field.ty == syn::parse_quote!(_) {
field.ty = syn::parse_quote!(
#frame_support::sp_std::marker::PhantomData<(#type_use_gen)>
);
}
}
if get_doc_literals(&pallet_item.attrs).is_empty() {
pallet_item.attrs.push(syn::parse_quote!(
#[doc = r"
The [pallet](https://docs.substrate.io/v3/runtime/frame#pallets) implementing
the on-chain logic.
"]
));
}
pallet_item.attrs.push(syn::parse_quote!(
#[derive(
#frame_support::CloneNoBound,
#frame_support::EqNoBound,
#frame_support::PartialEqNoBound,
#frame_support::RuntimeDebugNoBound,
)]
));
let pallet_error_metadata = if let Some(error_def) = &def.error {
let error_ident = &error_def.error;
quote::quote_spanned!(def.pallet_struct.attr_span =>
impl<#type_impl_gen> #pallet_ident<#type_use_gen> #config_where_clause {
pub fn error_metadata() -> Option<#frame_support::metadata::PalletErrorMetadata> {
Some(#frame_support::metadata::PalletErrorMetadata {
ty: #frame_support::scale_info::meta_type::<#error_ident<#type_use_gen>>()
})
}
}
)
} else {
quote::quote_spanned!(def.pallet_struct.attr_span =>
impl<#type_impl_gen> #pallet_ident<#type_use_gen> #config_where_clause {
pub fn error_metadata() -> Option<#frame_support::metadata::PalletErrorMetadata> {
None
}
}
)
};
let storage_info_span =
def.pallet_struct.generate_storage_info.unwrap_or(def.pallet_struct.attr_span);
let storage_names = &def.storages.iter().map(|storage| &storage.ident).collect::<Vec<_>>();
let storage_cfg_attrs =
&def.storages.iter().map(|storage| &storage.cfg_attrs).collect::<Vec<_>>();
let storage_info_traits = &def
.storages
.iter()
.map(|storage| {
if storage.unbounded || def.pallet_struct.generate_storage_info.is_none() {
quote::quote_spanned!(storage_info_span => PartialStorageInfoTrait)
} else {
quote::quote_spanned!(storage_info_span => StorageInfoTrait)
}
})
.collect::<Vec<_>>();
let storage_info_methods = &def
.storages
.iter()
.map(|storage| {
if storage.unbounded || def.pallet_struct.generate_storage_info.is_none() {
quote::quote_spanned!(storage_info_span => partial_storage_info)
} else {
quote::quote_spanned!(storage_info_span => storage_info)
}
})
.collect::<Vec<_>>();
let storage_info = quote::quote_spanned!(storage_info_span =>
impl<#type_impl_gen> #frame_support::traits::StorageInfoTrait
for #pallet_ident<#type_use_gen>
#storages_where_clauses
{
fn storage_info()
-> #frame_support::sp_std::vec::Vec<#frame_support::traits::StorageInfo>
{
#[allow(unused_mut)]
let mut res = #frame_support::sp_std::vec![];
#(
#(#storage_cfg_attrs)*
{
let mut storage_info = <
#storage_names<#type_use_gen>
as #frame_support::traits::#storage_info_traits
>::#storage_info_methods();
res.append(&mut storage_info);
}
)*
res
}
}
);
let storage_version = if let Some(v) = def.pallet_struct.storage_version.as_ref() {
quote::quote! { #v }
} else {
quote::quote! { #frame_support::traits::StorageVersion::default() }
};
quote::quote_spanned!(def.pallet_struct.attr_span =>
#pallet_error_metadata
#[deprecated(note = "use `Pallet` instead")]
#[allow(dead_code)]
pub type Module<#type_decl_gen> = #pallet_ident<#type_use_gen>;
impl<#type_impl_gen> #frame_support::traits::GetStorageVersion
for #pallet_ident<#type_use_gen>
#config_where_clause
{
fn current_storage_version() -> #frame_support::traits::StorageVersion {
#storage_version
}
fn on_chain_storage_version() -> #frame_support::traits::StorageVersion {
#frame_support::traits::StorageVersion::get::<Self>()
}
}
impl<#type_impl_gen> #frame_support::traits::OnGenesis
for #pallet_ident<#type_use_gen>
#config_where_clause
{
fn on_genesis() {
let storage_version = #storage_version;
storage_version.put::<Self>();
}
}
impl<#type_impl_gen> #frame_support::traits::PalletInfoAccess
for #pallet_ident<#type_use_gen>
#config_where_clause
{
fn index() -> usize {
<
<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo
>::index::<Self>()
.expect("Pallet is part of the runtime because pallet `Config` trait is \
implemented by the runtime")
}
fn name() -> &'static str {
<
<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo
>::name::<Self>()
.expect("Pallet is part of the runtime because pallet `Config` trait is \
implemented by the runtime")
}
fn module_name() -> &'static str {
<
<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo
>::module_name::<Self>()
.expect("Pallet is part of the runtime because pallet `Config` trait is \
implemented by the runtime")
}
fn crate_version() -> #frame_support::traits::CrateVersion {
#frame_support::crate_to_crate_version!()
}
}
impl<#type_impl_gen> #frame_support::traits::PalletsInfoAccess
for #pallet_ident<#type_use_gen>
#config_where_clause
{
fn count() -> usize { 1 }
fn accumulate(
acc: &mut #frame_support::sp_std::vec::Vec<#frame_support::traits::PalletInfoData>
) {
use #frame_support::traits::PalletInfoAccess;
let item = #frame_support::traits::PalletInfoData {
index: Self::index(),
name: Self::name(),
module_name: Self::module_name(),
crate_version: Self::crate_version(),
};
acc.push(item);
}
}
#storage_info
)
}