diff --git a/proc-macro/src/call.rs b/proc-macro/src/call.rs index d1eab81899..095e0952eb 100644 --- a/proc-macro/src/call.rs +++ b/proc-macro/src/call.rs @@ -54,9 +54,8 @@ pub fn call(s: Structure) -> TokenStream { const FUNCTION: &'static str = #call_name; fn events_decoder( decoder: &mut #subxt::EventsDecoder, - ) -> Result<(), #subxt::EventsError> { - decoder.#with_module()?; - Ok(()) + ) { + decoder.#with_module(); } } @@ -125,9 +124,8 @@ mod tests { const FUNCTION: &'static str = "transfer"; fn events_decoder( decoder: &mut substrate_subxt::EventsDecoder, - ) -> Result<(), substrate_subxt::EventsError> { - decoder.with_balances()?; - Ok(()) + ) { + decoder.with_balances(); } } diff --git a/proc-macro/src/module.rs b/proc-macro/src/module.rs index 55e0a01a56..162bb584a7 100644 --- a/proc-macro/src/module.rs +++ b/proc-macro/src/module.rs @@ -85,7 +85,7 @@ pub fn module(_args: TokenStream, tokens: TokenStream) -> TokenStream { let module = utils::path_to_ident(path); let with_module = with_module_ident(module); Some(quote! { - self.#with_module()?; + self.#with_module(); }) } else { None @@ -99,7 +99,7 @@ pub fn module(_args: TokenStream, tokens: TokenStream) -> TokenStream { let ident = &ty.ident; let ident_str = ident.to_string(); Some(quote! { - self.register_type_size::(#ident_str)?; + self.register_type_size::(#ident_str); }) } else { None @@ -114,16 +114,15 @@ pub fn module(_args: TokenStream, tokens: TokenStream) -> TokenStream { /// `EventsDecoder` extension trait. pub trait #module_events_decoder { /// Registers this modules types. - fn #with_module(&mut self) -> Result<(), #subxt::EventsError>; + fn #with_module(&mut self); } impl #module_events_decoder for #subxt::EventsDecoder { - fn #with_module(&mut self) -> Result<(), #subxt::EventsError> { + fn #with_module(&mut self) { #(#bounds)* #(#types)* - Ok(()) } } } @@ -167,16 +166,15 @@ mod tests { /// `EventsDecoder` extension trait. pub trait BalancesEventsDecoder { /// Registers this modules types. - fn with_balances(&mut self) -> Result<(), substrate_subxt::EventsError>; + fn with_balances(&mut self); } impl BalancesEventsDecoder for substrate_subxt::EventsDecoder { - fn with_balances(&mut self) -> Result<(), substrate_subxt::EventsError> { - self.with_system()?; - self.register_type_size::("Balance")?; - Ok(()) + fn with_balances(&mut self) { + self.with_system(); + self.register_type_size::("Balance"); } } }; diff --git a/src/events.rs b/src/events.rs index 801641d2a6..81b3376d22 100644 --- a/src/events.rs +++ b/src/events.rs @@ -28,7 +28,6 @@ use std::{ HashMap, HashSet, }, - convert::TryFrom, marker::{ PhantomData, Send, @@ -87,51 +86,43 @@ pub struct EventsDecoder { marker: PhantomData T>, } -impl TryFrom for EventsDecoder { - type Error = EventsError; - - fn try_from(metadata: Metadata) -> Result { +impl EventsDecoder { + /// Creates a new `EventsDecoder`. + pub fn new(metadata: Metadata) -> Self { let mut decoder = Self { metadata, type_sizes: HashMap::new(), marker: PhantomData, }; // register default event arg type sizes for dynamic decoding of events - decoder.register_type_size::("bool")?; - decoder.register_type_size::("ReferendumIndex")?; - decoder.register_type_size::<[u8; 16]>("Kind")?; - decoder.register_type_size::<[u8; 32]>("AuthorityId")?; - decoder.register_type_size::("u8")?; - decoder.register_type_size::("u32")?; - decoder.register_type_size::("AccountIndex")?; - decoder.register_type_size::("SessionIndex")?; - decoder.register_type_size::("PropIndex")?; - decoder.register_type_size::("ProposalIndex")?; - decoder.register_type_size::("AuthorityIndex")?; - decoder.register_type_size::("AuthorityWeight")?; - decoder.register_type_size::("MemberCount")?; - decoder.register_type_size::("AccountId")?; - decoder.register_type_size::("BlockNumber")?; - decoder.register_type_size::("Hash")?; - decoder.register_type_size::("VoteThreshold")?; - - Ok(decoder) + decoder.register_type_size::("bool"); + decoder.register_type_size::("ReferendumIndex"); + decoder.register_type_size::<[u8; 16]>("Kind"); + decoder.register_type_size::<[u8; 32]>("AuthorityId"); + decoder.register_type_size::("u8"); + decoder.register_type_size::("u32"); + decoder.register_type_size::("AccountIndex"); + decoder.register_type_size::("SessionIndex"); + decoder.register_type_size::("PropIndex"); + decoder.register_type_size::("ProposalIndex"); + decoder.register_type_size::("AuthorityIndex"); + decoder.register_type_size::("AuthorityWeight"); + decoder.register_type_size::("MemberCount"); + decoder.register_type_size::("AccountId"); + decoder.register_type_size::("BlockNumber"); + decoder.register_type_size::("Hash"); + decoder.register_type_size::("VoteThreshold"); + decoder } -} -impl EventsDecoder { /// Register a type. - pub fn register_type_size(&mut self, name: &str) -> Result + pub fn register_type_size(&mut self, name: &str) -> usize where U: Default + Codec + Send + 'static, { let size = U::default().encode().len(); - if size > 0 { - self.type_sizes.insert(name.to_string(), size); - Ok(size) - } else { - Err(EventsError::TypeSizeUnavailable(name.to_owned())) - } + self.type_sizes.insert(name.to_string(), size); + size } /// Check missing type sizes. @@ -156,7 +147,7 @@ impl EventsDecoder { } } } - if missing.len() > 0 { + if !missing.is_empty() { log::warn!( "The following primitive types do not have registered sizes: {:?} \ If any of these events are received, an error will occur since we cannot decode them", diff --git a/src/frame/mod.rs b/src/frame/mod.rs index 7244b8c35f..8c8ada3893 100644 --- a/src/frame/mod.rs +++ b/src/frame/mod.rs @@ -17,10 +17,7 @@ //! Implements support for built-in runtime modules. use crate::{ - events::{ - EventsDecoder, - EventsError, - }, + events::EventsDecoder, metadata::{ Metadata, MetadataError, @@ -62,9 +59,7 @@ pub trait Call: Encode { /// Function name. const FUNCTION: &'static str; /// Load event decoder. - fn events_decoder(_decoder: &mut EventsDecoder) -> Result<(), EventsError> { - Ok(()) - } + fn events_decoder(_decoder: &mut EventsDecoder) {} } /// Event trait. diff --git a/src/lib.rs b/src/lib.rs index ed60910784..a972861a4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,10 +65,7 @@ use sp_runtime::{ MultiSignature, }; use sp_version::RuntimeVersion; -use std::{ - convert::TryFrom, - marker::PhantomData, -}; +use std::marker::PhantomData; mod error; mod events; @@ -148,11 +145,7 @@ impl ClientBuilder { let client = if let Some(client) = self.client { client } else { - let url = self - .url - .as_ref() - .map(|s| &**s) - .unwrap_or("ws://127.0.0.1:9944"); + let url = self.url.as_deref().unwrap_or("ws://127.0.0.1:9944"); if url.starts_with("ws://") || url.starts_with("wss://") { jsonrpsee::ws_client(url).await? } else { @@ -198,6 +191,11 @@ impl Clone for Client { } impl Client { + /// Returns the genesis hash. + pub fn genesis(&self) -> &T::Hash { + &self.genesis_hash + } + /// Returns the chain metadata. pub fn metadata(&self) -> &Metadata { &self.metadata @@ -362,11 +360,11 @@ where } /// Returns an events decoder for a call. - pub fn events_decoder>(&self) -> Result, Error> { + pub fn events_decoder>(&self) -> EventsDecoder { let metadata = self.metadata().clone(); - let mut decoder = EventsDecoder::try_from(metadata)?; - C::events_decoder(&mut decoder)?; - Ok(decoder) + let mut decoder = EventsDecoder::new(metadata); + C::events_decoder(&mut decoder); + decoder } /// Create and submit an extrinsic and return corresponding Hash if successful @@ -421,7 +419,7 @@ where <>::Extra as SignedExtension>::AdditionalSigned: Send + Sync, { let extrinsic = self.create_signed(call, signer).await?; - let decoder = self.events_decoder::()?; + let decoder = self.events_decoder::(); self.submit_and_watch_extrinsic(extrinsic, decoder).await } } diff --git a/src/metadata.rs b/src/metadata.rs index 80fcbe60aa..cf5f9c9852 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -223,8 +223,7 @@ impl StorageMetadata { } pub fn default(&self) -> Result { - Decode::decode(&mut &self.default[..]) - .map_err(|err| MetadataError::DefaultError(err)) + Decode::decode(&mut &self.default[..]).map_err(MetadataError::DefaultError) } pub fn hash(hasher: &StorageHasher, bytes: &[u8]) -> Vec { diff --git a/src/rpc.rs b/src/rpc.rs index 1a7c88443a..faba5db5a2 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -203,7 +203,7 @@ impl Rpc { &self, block_number: Option>, ) -> Result, Error> { - let block_number = block_number.map(|bn| ListOrValue::Value(bn)); + let block_number = block_number.map(ListOrValue::Value); let params = Params::Array(vec![to_json_value(block_number)?]); let list_or_value = self.client.request("chain_getBlockHash", params).await?; match list_or_value { @@ -490,7 +490,7 @@ pub async fn wait_for_block_events( } } } - return if events.len() > 0 { + return if !events.is_empty() { Ok(ExtrinsicSuccess { block: block_hash, extrinsic: ext_hash,