diff --git a/examples/examples/events.rs b/examples/examples/events.rs index 01499c1acb..297670b927 100644 --- a/examples/examples/events.rs +++ b/examples/examples/events.rs @@ -35,19 +35,6 @@ async fn main() -> Result<(), Box> { } } - // Or we can attempt to decode them into a specific arbitraty pallet enum - // (We could also set the output type to Value to dynamically decode, here): - println!("Event details for Balances pallet:"); - for event in events.iter() { - let event = event?; - - if let Ok(ev) = event.as_pallet_event::() { - println!("{ev:?}"); - } else { - continue; - } - } - // Or we can look for specific events which match our statically defined ones: let transfer_event = events.find_first::()?; if let Some(ev) = transfer_event { diff --git a/subxt/src/blocks/extrinsic_types.rs b/subxt/src/blocks/extrinsic_types.rs index ac090c7092..1783ca3a3e 100644 --- a/subxt/src/blocks/extrinsic_types.rs +++ b/subxt/src/blocks/extrinsic_types.rs @@ -8,7 +8,7 @@ use crate::{ config::{Config, Hasher}, error::{BlockError, Error}, events, - metadata::{DecodeWithMetadata, ExtrinsicMetadata}, + metadata::ExtrinsicMetadata, rpc::types::ChainBlockExtrinsic, Metadata, }; @@ -373,8 +373,7 @@ where } /// Decode and provide the extrinsic fields back in the form of a [`scale_value::Composite`] - /// type which represents the named or unnamed fields that were - /// present in the extrinsic. + /// type which represents the named or unnamed fields that were present in the extrinsic. pub fn field_values( &self, ) -> Result, Error> { @@ -390,10 +389,8 @@ where Ok(decoded) } - /// Attempt to statically decode these [`ExtrinsicDetails`] into a type representing the extrinsic - /// fields. This leans directly on [`codec::Decode`]. You can also attempt to decode the entirety - /// of the extrinsic using [`Self::as_root_extrinsic()`], which is more lenient because it's able - /// to lean on [`scale_decode::DecodeAsType`]. + /// Attempt to decode these [`ExtrinsicDetails`] into a type representing the extrinsic fields. + /// Such types are exposed in the codegen as `pallet_name::calls::types::CallName` types. pub fn as_extrinsic(&self) -> Result, Error> { let extrinsic_metadata = self.extrinsic_metadata()?; if extrinsic_metadata.pallet() == E::PALLET && extrinsic_metadata.call() == E::CALL { @@ -408,24 +405,6 @@ where } } - /// Attempt to decode these [`ExtrinsicDetails`] into a pallet extrinsic type (which includes - /// the pallet enum variants as well as the extrinsic fields). These extrinsics can be found in - /// the static codegen under a path like `pallet_name::Call`. - pub fn as_pallet_extrinsic(&self) -> Result { - let pallet = self.metadata.pallet(self.pallet_name()?)?; - let extrinsic_ty = pallet.call_ty_id().ok_or_else(|| { - Error::Metadata(crate::metadata::MetadataError::ExtrinsicNotFound( - pallet.index(), - self.variant_index(), - )) - })?; - - // Ignore the root enum index, so start 1 byte after that: - let decoded = - E::decode_with_metadata(&mut &self.call_bytes()[1..], extrinsic_ty, &self.metadata)?; - Ok(decoded) - } - /// Attempt to decode these [`ExtrinsicDetails`] into a root extrinsic type (which includes /// the pallet and extrinsic enum variants as well as the extrinsic fields). A compatible /// type for this is exposed via static codegen as a root level `Call` type. @@ -623,6 +602,7 @@ impl ExtrinsicEvents { #[cfg(test)] mod tests { use super::*; + use crate::metadata::DecodeWithMetadata; use crate::{rpc::types::RuntimeVersion, OfflineClient, PolkadotConfig}; use assert_matches::assert_matches; use codec::{Decode, Encode}; @@ -887,20 +867,6 @@ mod tests { }) ); - // Decode the extrinsic to the pallet enum. - let decoded_extrinsic = extrinsic - .as_pallet_extrinsic::() - .expect("can decode extrinsic to pallet enum"); - - assert_eq!( - decoded_extrinsic, - Pallet::TestCall { - value: 10, - signed: true, - name: "SomeValue".into(), - } - ); - // Decode the extrinsic to the extrinsic variant. let decoded_extrinsic = extrinsic .as_extrinsic::() diff --git a/subxt/src/events/events_type.rs b/subxt/src/events/events_type.rs index cf43cea162..6dc48de8c1 100644 --- a/subxt/src/events/events_type.rs +++ b/subxt/src/events/events_type.rs @@ -6,11 +6,8 @@ use super::{Phase, StaticEvent}; use crate::{ - client::OnlineClientT, - error::Error, - events::events_client::get_event_bytes, - metadata::{DecodeWithMetadata, EventMetadata}, - Config, Metadata, + client::OnlineClientT, error::Error, events::events_client::get_event_bytes, + metadata::EventMetadata, Config, Metadata, }; use codec::{Compact, Decode}; use derivative::Derivative; @@ -325,8 +322,7 @@ impl EventDetails { } /// Decode and provide the event fields back in the form of a [`scale_value::Composite`] - /// type which represents the named or unnamed fields that were - /// present in the event. + /// type which represents the named or unnamed fields that were present in the event. pub fn field_values( &self, ) -> Result, Error> { @@ -343,10 +339,8 @@ impl EventDetails { Ok(decoded) } - /// Attempt to statically decode these [`EventDetails`] into a type representing the event - /// fields. This leans directly on [`codec::Decode`]. You can also attempt to decode the entirety - /// of the event using [`EventDetails::as_root_event()`], which is more lenient because it's able - /// to lean on [`scale_decode::DecodeAsType`]. + /// Attempt to decode these [`EventDetails`] into a type representing the event fields. + /// Such types are exposed in the codegen as `pallet_name::events::EventName` types. pub fn as_event(&self) -> Result, Error> { let ev_metadata = self.event_metadata(); if ev_metadata.pallet() == E::PALLET && ev_metadata.event() == E::EVENT { @@ -361,29 +355,6 @@ impl EventDetails { } } - /// Attempt to decode these [`EventDetails`] into a pallet event type (which includes - /// the pallet enum variants as well as the event fields). These events can be found in - /// the static codegen under a path like `pallet_name::Event`. - pub fn as_pallet_event(&self) -> Result { - let pallet = self.metadata.pallet(self.pallet_name())?; - let event_ty = pallet.event_ty_id().ok_or_else(|| { - Error::Metadata(crate::metadata::MetadataError::EventNotFound( - pallet.index(), - self.variant_index(), - )) - })?; - - // Ignore the root enum index, so start 1 byte after that: - let start_idx = self.event_start_idx + 1; - - let decoded = E::decode_with_metadata( - &mut &self.all_bytes[start_idx..self.event_fields_end_idx], - event_ty, - &self.metadata, - )?; - Ok(decoded) - } - /// Attempt to decode these [`EventDetails`] into a root event type (which includes /// the pallet and event enum variants as well as the event fields). A compatible /// type for this is exposed via static codegen as a root level `Event` type. @@ -427,6 +398,7 @@ pub trait RootEvent: Sized { #[cfg(test)] pub(crate) mod test_utils { use super::*; + use crate::metadata::DecodeWithMetadata; use crate::{Config, SubstrateConfig}; use codec::Encode; use frame_metadata::{ @@ -675,39 +647,6 @@ mod tests { assert_eq!(decoded_event, AllEvents::Test(event)); } - #[test] - fn statically_decode_single_pallet_event() { - #[derive(Clone, Debug, PartialEq, Decode, Encode, TypeInfo, scale_decode::DecodeAsType)] - enum Event { - A(u8, bool, Vec), - } - - // Create fake metadata that knows about our single event, above: - let metadata = metadata::(); - - // Encode our events in the format we expect back from a node, and - // construst an Events object to iterate them: - let event = Event::A(1, true, vec!["Hi".into()]); - let events = events::( - metadata, - vec![event_record(Phase::ApplyExtrinsic(123), event.clone())], - ); - - let ev = events - .iter() - .next() - .expect("one event expected") - .expect("event should be extracted OK"); - - // This is the line we're testing; decode into our "pallet event" enum. - let decoded_event = ev - .as_pallet_event::() - .expect("can decode event into root enum again"); - - // It should equal the event we put in: - assert_eq!(decoded_event, event); - } - #[test] fn dynamically_decode_single_event() { #[derive(Clone, Debug, PartialEq, Decode, Encode, TypeInfo)] diff --git a/testing/integration-tests/src/blocks/mod.rs b/testing/integration-tests/src/blocks/mod.rs index bf9e707997..a54df4c6d0 100644 --- a/testing/integration-tests/src/blocks/mod.rs +++ b/testing/integration-tests/src/blocks/mod.rs @@ -172,9 +172,6 @@ async fn decode_extrinsics() { assert_eq!(block_extrinsics.len(), 2); let timestamp = block_extrinsics.get(0).unwrap(); timestamp.as_root_extrinsic::().unwrap(); - timestamp - .as_pallet_extrinsic::() - .unwrap(); timestamp .as_extrinsic::() .unwrap(); @@ -182,8 +179,6 @@ async fn decode_extrinsics() { let tx = block_extrinsics.get(1).unwrap(); tx.as_root_extrinsic::().unwrap(); - tx.as_pallet_extrinsic::() - .unwrap(); tx.as_extrinsic::() .unwrap(); assert!(tx.is_signed());