subxt_codegen/api/
events.rs

1// Copyright 2019-2025 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5use super::CodegenError;
6use proc_macro2::TokenStream as TokenStream2;
7use quote::quote;
8use scale_typegen::TypeGenerator;
9use scale_typegen::typegen::ir::ToTokensWithSettings;
10use subxt_metadata::PalletMetadata;
11
12/// Generate events from the provided pallet metadata.
13///
14/// The function creates a new module named `events` under the pallet's module.
15///
16/// ```rust,ignore
17/// pub mod PalletName {
18///     pub mod events {
19///     ...
20///     }
21/// }
22/// ```
23///
24/// The function generates the events as rust structs that implement the `subxt::event::StaticEvent` trait
25/// to uniquely identify the event's identity when creating the extrinsic.
26///
27/// ```rust,ignore
28/// pub struct EventName {
29///      pub event_param: type,
30/// }
31/// impl ::subxt::events::StaticEvent for EventName {
32/// ...
33/// }
34/// ```
35///
36/// # Arguments
37///
38/// - `type_gen` - [`scale_typegen::TypeGenerator`] that contains settings and all types from the runtime metadata.
39/// - `pallet` - Pallet metadata from which the events are generated.
40/// - `crate_path` - The crate path under which the `subxt-core` crate is located, e.g. `::subxt::ext::subxt_core` when using subxt as a dependency.
41pub fn generate_events(
42    type_gen: &TypeGenerator,
43    pallet: &PalletMetadata,
44    crate_path: &syn::Path,
45) -> Result<TokenStream2, CodegenError> {
46    // Early return if the pallet has no events.
47    let Some(event_ty) = pallet.event_ty_id() else {
48        return Ok(quote!());
49    };
50
51    let variant_names_and_struct_defs =
52        super::generate_structs_from_variants(type_gen, event_ty, |name| name.into(), "Event")?;
53
54    let event_structs = variant_names_and_struct_defs.into_iter().map(|var| {
55        let pallet_name = pallet.name();
56        let event_struct_name = &var.composite.name;
57        let event_name = var.variant_name;
58        let alias_mod = var.type_alias_mod;
59        let struct_def = type_gen
60            .upcast_composite(&var.composite)
61            .to_token_stream(type_gen.settings());
62        quote! {
63            #struct_def
64            #alias_mod
65
66            impl #crate_path::events::StaticEvent for #event_struct_name {
67                const PALLET: &'static str = #pallet_name;
68                const EVENT: &'static str = #event_name;
69            }
70        }
71    });
72
73    let event_type = type_gen
74        .resolve_type_path(event_ty)?
75        .to_token_stream(type_gen.settings());
76    let event_ty = type_gen.resolve_type(event_ty)?;
77    let docs = &event_ty.docs;
78    let docs = type_gen
79        .settings()
80        .should_gen_docs
81        .then_some(quote! { #( #[doc = #docs ] )* })
82        .unwrap_or_default();
83    let types_mod_ident = type_gen.types_mod_ident();
84
85    Ok(quote! {
86        #docs
87        pub type Event = #event_type;
88        pub mod events {
89            use super::#types_mod_ident;
90            #( #event_structs )*
91        }
92    })
93}