Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Store type name of a field in event metadata #654

Merged
merged 4 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions subxt/src/events/events_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ impl EventDetails {
);

// Skip over the bytes belonging to this event.
for (_name, type_id) in event_metadata.fields() {
for field_metadata in event_metadata.fields() {
// Skip over the bytes for this field:
scale_decode::decode(
input,
*type_id,
field_metadata.type_id(),
&metadata.runtime_metadata().types,
scale_decode::visitor::IgnoreVisitor,
)?;
Expand Down Expand Up @@ -288,15 +288,15 @@ impl EventDetails {
let is_named = event_metadata
.fields()
.get(0)
.map(|(n, _)| n.is_some())
.map(|fm| fm.name().is_some())
.unwrap_or(false);

if !is_named {
let mut event_values = vec![];
for (_, type_id) in event_metadata.fields() {
for field_metadata in event_metadata.fields() {
let value = scale_value::scale::decode_as_type(
bytes,
*type_id,
field_metadata.type_id(),
&self.metadata.runtime_metadata().types,
)?;
event_values.push(value);
Expand All @@ -305,13 +305,14 @@ impl EventDetails {
Ok(scale_value::Composite::Unnamed(event_values))
} else {
let mut event_values = vec![];
for (name, type_id) in event_metadata.fields() {
for field_metadata in event_metadata.fields() {
let value = scale_value::scale::decode_as_type(
bytes,
*type_id,
field_metadata.type_id(),
&self.metadata.runtime_metadata().types,
)?;
event_values.push((name.clone().unwrap_or_default(), value));
event_values
.push((field_metadata.name().unwrap_or_default().to_string(), value));
}

Ok(scale_value::Composite::Named(event_values))
Expand Down
47 changes: 43 additions & 4 deletions subxt/src/metadata/metadata_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,47 @@ impl PalletMetadata {
}
}

/// Metadata for specific field.
#[derive(Clone, Debug)]
pub struct EventFieldMetadata {
name: Option<String>,
type_name: Option<String>,
type_id: u32,
}

impl EventFieldMetadata {
pub fn new(name: Option<String>, type_name: Option<String>, type_id: u32) -> Self {
EventFieldMetadata {
name,
type_name,
type_id,
}
}

/// Get the name of the field.
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}

// Get the type name of the field as it appears in the code
pub fn type_name(&self) -> Option<&str> {
self.type_name.as_deref()
}

/// Get the id of a type
pub fn type_id(&self) -> u32 {
self.type_id
}
}

/// Metadata for specific events.
#[derive(Clone, Debug)]
pub struct EventMetadata {
// The pallet name is shared across every event, so put it
// behind an Arc to avoid lots of needless clones of it existing.
pallet: Arc<str>,
event: String,
fields: Vec<(Option<String>, u32)>,
fields: Vec<EventFieldMetadata>,
docs: Vec<String>,
}

Expand All @@ -319,8 +352,8 @@ impl EventMetadata {
&self.event
}

/// The names and types of each field in the event.
pub fn fields(&self) -> &[(Option<String>, u32)] {
/// The names, type names & types of each field in the event.
pub fn fields(&self) -> &[EventFieldMetadata] {
&self.fields
}

Expand Down Expand Up @@ -457,7 +490,13 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
fields: variant
.fields()
.iter()
.map(|f| (f.name().map(|n| n.to_owned()), f.ty().id()))
.map(|f| {
EventFieldMetadata::new(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe we could use something like impl From<Field<PortableForm>> here

f.name().map(|n| n.to_owned()),
f.type_name().map(|n| n.to_owned()),
f.ty().id(),
)
})
.collect(),
docs: variant.docs().to_vec(),
},
Expand Down