subxt-codegen 0.50.0

Generate an API for interacting with a substrate node from FRAME metadata
Documentation
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright 2019-2026 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

use std::collections::HashSet;

use heck::ToSnakeCase as _;
use heck::ToUpperCamelCase as _;

use scale_typegen::TypeGenerator;
use scale_typegen::typegen::ir::ToTokensWithSettings;
use subxt_metadata::{Metadata, RuntimeApiMetadata};

use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};

use crate::CodegenError;

/// Generate the runtime APIs.
pub fn generate_runtime_apis(
    metadata: &Metadata,
    type_gen: &TypeGenerator,
    types_mod_ident: &syn::Ident,
    crate_path: &syn::Path,
) -> Result<TokenStream2, CodegenError> {
    let runtime_fns: Vec<_> = metadata
        .runtime_api_traits()
        .map(|api| generate_runtime_api(api, type_gen, crate_path))
        .collect::<Result<_, _>>()?;

    let trait_defs = runtime_fns.iter().map(|(apis, _)| apis);
    let trait_getters = runtime_fns.iter().map(|(_, getters)| getters);

    Ok(quote! {
        pub mod runtime_apis {
            use super::root_mod;
            use super::#types_mod_ident;

            use #crate_path::ext::codec::Encode;

            pub struct RuntimeApi;

            impl RuntimeApi {
                #( #trait_getters )*
            }

            #( #trait_defs )*
        }
    })
}

/// Generates runtime functions for the given API metadata.
fn generate_runtime_api(
    api: RuntimeApiMetadata,
    type_gen: &TypeGenerator,
    crate_path: &syn::Path,
) -> Result<(TokenStream2, TokenStream2), CodegenError> {
    let types_mod_ident = type_gen.types_mod_ident();
    // Trait name must remain as is (upper case) to identify the runtime call.
    let trait_name_str = api.name();
    // The snake case for the trait name.
    let trait_name_snake = format_ident!("{}", api.name().to_snake_case());

    let docs = api.docs();
    let docs: TokenStream2 = type_gen
        .settings()
        .should_gen_docs
        .then_some(quote! { #( #[doc = #docs ] )* })
        .unwrap_or_default();

    let types_and_methods = api
        .methods()
        .map(|method| {
            let method_name = format_ident!("{}", method.name());
            let method_name_str = method.name();
            let validation_hash = method.hash();

            let docs = method.docs();
            let docs: TokenStream2 = type_gen
                .settings()
                .should_gen_docs
                .then_some(quote! { #( #[doc = #docs ] )* })
                .unwrap_or_default();

            struct Input {
                name: syn::Ident,
                type_alias: syn::Ident,
                type_path: TokenStream2,
            }

            let runtime_api_inputs: Vec<Input> = {
                let mut unique_names = HashSet::new();
                let mut unique_aliases = HashSet::new();

                method
                    .inputs()
                    .enumerate()
                    .map(|(idx, input)| {
                        // The method argument name is either the input name or the
                        // index (eg _1, _2 etc) if one isn't provided.
                        // if we get unlucky we'll end up with param_param1 etc.
                        let mut name = input.name.trim_start_matches('_').to_string();
                        if name.is_empty() {
                            name = format!("_{idx}");
                        }
                        while !unique_names.insert(name.clone()) {
                            name = format!("{name}_param{idx}");
                        }

                        // The alias is either InputName if provided, or Param1, Param2 etc if not.
                        // If we get unlucky we may even end up with ParamParam1 etc.
                        let mut alias = name.trim_start_matches('_').to_upper_camel_case();
                        // Note: name is not empty.
                        if alias.as_bytes()[0].is_ascii_digit() {
                            alias = format!("Param{alias}");
                        }
                        while !unique_aliases.insert(alias.clone()) {
                            alias = format!("{alias}Param{idx}");
                        }

                        // Generate alias for runtime type.
                        let type_path = type_gen
                            .resolve_type_path(input.id)
                            .expect("runtime api input type is in metadata; qed")
                            .to_token_stream(type_gen.settings());

                        Input {
                            name: format_ident!("{name}"),
                            type_alias: format_ident!("{alias}"),
                            type_path,
                        }
                    })
                    .collect()
            };

            let input_tuple_types = runtime_api_inputs
                .iter()
                .map(|i| {
                    let ty = &i.type_alias;
                    quote!(#method_name::#ty)
                })
                .collect::<Vec<_>>();

            let input_args = runtime_api_inputs
                .iter()
                .map(|i| {
                    let arg = &i.name;
                    let ty = &i.type_alias;
                    quote!(#arg: #method_name::#ty)
                })
                .collect::<Vec<_>>();

            let input_param_names = runtime_api_inputs.iter().map(|i| &i.name);

            let input_type_aliases = runtime_api_inputs.iter().map(|i| {
                let ty = &i.type_alias;
                let path = &i.type_path;
                quote!(pub type #ty = #path;)
            });

            let output_type_path = type_gen
                .resolve_type_path(method.output_ty())?
                .to_token_stream(type_gen.settings());

            // Define the input and output type bits for the method.
            let runtime_api_types = quote! {
                pub mod #method_name {
                    use super::root_mod;
                    use super::#types_mod_ident;

                    #(#input_type_aliases)*

                    pub mod output {
                        use super::#types_mod_ident;
                        pub type Output = #output_type_path;
                    }
                }
            };

            // Define the getter method that will live on the `ViewFunctionApi` type.
            let runtime_api_method = quote!(
                #docs
                pub fn #method_name(
                    &self,
                    #(#input_args),*
                ) -> #crate_path::runtime_apis::StaticPayload<
                    (#(#input_tuple_types,)*),
                    #method_name::output::Output
                > {
                    #crate_path::runtime_apis::StaticPayload::new_static(
                        #trait_name_str,
                        #method_name_str,
                        (#(#input_param_names,)*),
                        [#(#validation_hash,)*],
                    )
                }
            );

            Ok((runtime_api_types, runtime_api_method))
        })
        .collect::<Result<Vec<_>, CodegenError>>()?;

    let trait_name = format_ident!("{}", trait_name_str);
    let types = types_and_methods.iter().map(|(types, _)| types);
    let methods = types_and_methods.iter().map(|(_, methods)| methods);

    // The runtime API definition and types.
    let trait_defs = quote!(
        pub mod #trait_name_snake {
            use super::root_mod;
            use super::#types_mod_ident;

            #docs
            pub struct #trait_name;

            impl #trait_name {
                #( #methods )*
            }

            #( #types )*
        }
    );

    // A getter for the `RuntimeApi` to get the trait structure.
    let trait_getter = quote!(
        pub fn #trait_name_snake(&self) -> #trait_name_snake::#trait_name {
            #trait_name_snake::#trait_name
        }
    );

    Ok((trait_defs, trait_getter))
}

#[cfg(test)]
mod tests {
    use crate::RuntimeGenerator;
    use frame_metadata::v15::{
        self, RuntimeApiMetadata, RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata,
    };
    use quote::quote;
    use scale_info::meta_type;
    use subxt_metadata::Metadata;

    fn metadata_with_runtime_apis(runtime_apis: Vec<RuntimeApiMetadata>) -> Metadata {
        let extrinsic_metadata = v15::ExtrinsicMetadata {
            version: 0,
            signed_extensions: vec![],
            address_ty: meta_type::<()>(),
            call_ty: meta_type::<()>(),
            signature_ty: meta_type::<()>(),
            extra_ty: meta_type::<()>(),
        };

        let metadata: Metadata = v15::RuntimeMetadataV15::new(
            vec![],
            extrinsic_metadata,
            meta_type::<()>(),
            runtime_apis,
            v15::OuterEnums {
                call_enum_ty: meta_type::<()>(),
                event_enum_ty: meta_type::<()>(),
                error_enum_ty: meta_type::<()>(),
            },
            v15::CustomMetadata {
                map: Default::default(),
            },
        )
        .try_into()
        .expect("can build valid metadata");
        metadata
    }

    fn generate_code(runtime_apis: Vec<RuntimeApiMetadata>) -> String {
        let metadata = metadata_with_runtime_apis(runtime_apis);
        let item_mod = syn::parse_quote!(
            pub mod api {}
        );
        let generator = RuntimeGenerator::new(metadata);
        let generated = generator
            .generate_runtime(
                item_mod,
                Default::default(),
                Default::default(),
                syn::parse_str("::subxt_path").unwrap(),
                false,
            )
            .expect("should be able to generate runtime");
        generated.to_string()
    }

    #[test]
    fn unique_param_names() {
        let runtime_apis = vec![RuntimeApiMetadata {
            name: "Test",
            methods: vec![RuntimeApiMethodMetadata {
                name: "test",
                inputs: vec![
                    RuntimeApiMethodParamMetadata {
                        name: "foo",
                        ty: meta_type::<bool>(),
                    },
                    RuntimeApiMethodParamMetadata {
                        name: "bar",
                        ty: meta_type::<bool>(),
                    },
                ],
                output: meta_type::<bool>(),
                docs: vec![],
            }],

            docs: vec![],
        }];

        let code = generate_code(runtime_apis);

        let expected_alias = quote!(
            pub mod test {
                use super::root_mod;
                use super::runtime_types;
                pub type Foo = ::core::primitive::bool;
                pub type Bar = ::core::primitive::bool;
                pub mod output {
                    use super::runtime_types;
                    pub type Output = ::core::primitive::bool;
                }
            }
        );

        assert!(code.contains(&expected_alias.to_string()));
    }

    #[test]
    fn duplicate_param_names() {
        let runtime_apis = vec![RuntimeApiMetadata {
            name: "Test",
            methods: vec![RuntimeApiMethodMetadata {
                name: "test",
                inputs: vec![
                    RuntimeApiMethodParamMetadata {
                        name: "_a",
                        ty: meta_type::<bool>(),
                    },
                    RuntimeApiMethodParamMetadata {
                        name: "a",
                        ty: meta_type::<bool>(),
                    },
                    RuntimeApiMethodParamMetadata {
                        name: "__a",
                        ty: meta_type::<bool>(),
                    },
                ],
                output: meta_type::<bool>(),
                docs: vec![],
            }],

            docs: vec![],
        }];

        let code = generate_code(runtime_apis);

        let expected_alias = quote!(
            pub mod test {
                use super::root_mod;
                use super::runtime_types;
                pub type A = ::core::primitive::bool;
                pub type AParam1 = ::core::primitive::bool;
                pub type AParam2 = ::core::primitive::bool;
                pub mod output {
                    use super::runtime_types;
                    pub type Output = ::core::primitive::bool;
                }
            }
        );

        assert!(code.contains(&expected_alias.to_string()));
    }

    #[test]
    fn duplicate_param_and_alias_names() {
        let runtime_apis = vec![RuntimeApiMetadata {
            name: "Test",
            methods: vec![RuntimeApiMethodMetadata {
                name: "test",
                inputs: vec![
                    RuntimeApiMethodParamMetadata {
                        name: "_",
                        ty: meta_type::<bool>(),
                    },
                    RuntimeApiMethodParamMetadata {
                        name: "_a",
                        ty: meta_type::<bool>(),
                    },
                    RuntimeApiMethodParamMetadata {
                        name: "_param_0",
                        ty: meta_type::<bool>(),
                    },
                    RuntimeApiMethodParamMetadata {
                        name: "__",
                        ty: meta_type::<bool>(),
                    },
                    RuntimeApiMethodParamMetadata {
                        name: "___param_0_param_2",
                        ty: meta_type::<bool>(),
                    },
                ],
                output: meta_type::<bool>(),
                docs: vec![],
            }],

            docs: vec![],
        }];

        let code = generate_code(runtime_apis);

        let expected_alias = quote!(
            pub mod test {
                use super::root_mod;
                use super::runtime_types;
                pub type Param0 = ::core::primitive::bool;
                pub type A = ::core::primitive::bool;
                pub type Param0Param2 = ::core::primitive::bool;
                pub type Param3 = ::core::primitive::bool;
                pub type Param0Param2Param4 = ::core::primitive::bool;
                pub mod output {
                    use super::runtime_types;
                    pub type Output = ::core::primitive::bool;
                }
            }
        );

        assert!(code.contains(&expected_alias.to_string()));
    }
}