diff --git a/proc-macro/src/store.rs b/proc-macro/src/store.rs index 8e80922b14..cb5e2cc79e 100644 --- a/proc-macro/src/store.rs +++ b/proc-macro/src/store.rs @@ -113,7 +113,7 @@ pub fn store(s: Structure) -> TokenStream { fn #store<'a>( &'a self, #args - ) -> core::pin::Pin, #subxt::Error>> + Send + 'a>>; + ) -> core::pin::Pin> + Send + 'a>>; } impl #store_trait for #subxt::Client @@ -125,7 +125,7 @@ pub fn store(s: Structure) -> TokenStream { fn #store<'a>( &'a self, #args - ) -> core::pin::Pin, #subxt::Error>> + Send + 'a>> { + ) -> core::pin::Pin> + Send + 'a>> { let #marker = core::marker::PhantomData::; Box::pin(self.fetch(#build_struct, None)) } @@ -169,7 +169,7 @@ mod tests { fn account<'a>( &'a self, account_id: &'a ::AccountId, - ) -> core::pin::Pin >, substrate_subxt::Error>> + Send + 'a>>; + ) -> core::pin::Pin, substrate_subxt::Error>> + Send + 'a>>; } impl AccountStoreExt for substrate_subxt::Client @@ -181,7 +181,7 @@ mod tests { fn account<'a>( &'a self, account_id: &'a ::AccountId, - ) -> core::pin::Pin >, substrate_subxt::Error>> + Send + 'a>> + ) -> core::pin::Pin, substrate_subxt::Error>> + Send + 'a>> { let _ = core::marker::PhantomData::; Box::pin(self.fetch(AccountStore { account_id, }, None)) diff --git a/proc-macro/src/test.rs b/proc-macro/src/test.rs index 663bf255eb..5a8f7a29d1 100644 --- a/proc-macro/src/test.rs +++ b/proc-macro/src/test.rs @@ -322,13 +322,10 @@ impl Step { .or(test_state) .map(|state| { let State { - state_name, - state, - state_param, + state_name, + state, + state_param, } = state; - let expect_state = state_name - .iter() - .map(|state| format!("failed to fetch state {}", state.to_string())); let state_struct = quote! { struct State<#(#state_param),*> { #(#state_name: #state_param,)* @@ -336,7 +333,7 @@ impl Step { }; let build_struct = quote! { #( - let #state_name = client.fetch(#state, None).await.unwrap().expect(#expect_state); + let #state_name = client.fetch(#state, None).await.unwrap(); )* State { #(#state_name),* } }; @@ -489,13 +486,11 @@ mod tests { let alice = client .fetch(AccountStore { account_id: &alice }, None) .await - .unwrap() - .expect("failed to fetch state alice"); + .unwrap(); let bob = client .fetch(AccountStore { account_id: &bob }, None) .await - .unwrap() - .expect("failed to fetch state bob"); + .unwrap(); State { alice, bob } }; @@ -525,13 +520,11 @@ mod tests { let alice = client .fetch(AccountStore { account_id: &alice }, None) .await - .unwrap() - .expect("failed to fetch state alice"); + .unwrap(); let bob = client .fetch(AccountStore { account_id: &bob }, None) .await - .unwrap() - .expect("failed to fetch state bob"); + .unwrap(); State { alice, bob } }; diff --git a/proc-macro/tests/balances.rs b/proc-macro/tests/balances.rs index 6f66e9d13c..a600277c16 100644 --- a/proc-macro/tests/balances.rs +++ b/proc-macro/tests/balances.rs @@ -115,8 +115,8 @@ async fn transfer_balance_example() -> Result<(), Box> { let alice = AccountKeyring::Alice.to_account_id(); let bob = AccountKeyring::Bob.to_account_id(); - let alice_account = client.account(&alice).await?.unwrap_or_default(); - let bob_account = client.account(&bob).await?.unwrap_or_default(); + let alice_account = client.account(&alice).await?; + let bob_account = client.account(&bob).await?; let pre = (alice_account, bob_account); let builder = client.xt(AccountKeyring::Alice.pair(), None).await?; @@ -137,8 +137,8 @@ async fn transfer_balance_example() -> Result<(), Box> { }) ); - let alice_account = client.account(&alice).await?.unwrap_or_default(); - let bob_account = client.account(&bob).await?.unwrap_or_default(); + let alice_account = client.account(&alice).await?; + let bob_account = client.account(&bob).await?; let post = (alice_account, bob_account); assert_eq!(pre.0.free, post.0.free - 10_000); diff --git a/src/frame/balances.rs b/src/frame/balances.rs index 41f6738fef..02e41d9342 100644 --- a/src/frame/balances.rs +++ b/src/frame/balances.rs @@ -146,7 +146,7 @@ mod tests { async fn test_state_total_issuance() { env_logger::try_init().ok(); let client = test_client().await; - let total_issuance = client.total_issuance().await.unwrap().unwrap(); + let total_issuance = client.total_issuance().await.unwrap(); assert_ne!(total_issuance, 0); } @@ -156,7 +156,7 @@ mod tests { env_logger::try_init().ok(); let client = test_client().await; let account = AccountKeyring::Alice.to_account_id(); - let info = client.account(&account).await.unwrap().unwrap(); + let info = client.account(&account).await.unwrap(); assert_ne!(info.data.free, 0); } } diff --git a/src/frame/mod.rs b/src/frame/mod.rs index 3fca355ee9..7244b8c35f 100644 --- a/src/frame/mod.rs +++ b/src/frame/mod.rs @@ -47,14 +47,11 @@ pub trait Store: Encode { /// Returns the `StorageKey`. fn key(&self, metadata: &Metadata) -> Result; /// Returns the default value. - fn default( - &self, - metadata: &Metadata, - ) -> Result, MetadataError> { + fn default(&self, metadata: &Metadata) -> Result { Ok(metadata .module(Self::MODULE)? .storage(Self::FIELD)? - .default()) + .default()?) } } diff --git a/src/lib.rs b/src/lib.rs index 709ba1f8cd..1c6bc8bc5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -217,11 +217,11 @@ impl Client { &self, store: F, hash: Option, - ) -> Result, Error> { + ) -> Result { let key = store.key(&self.metadata)?; let value = self.rpc.storage::(key, hash).await?; if let Some(v) = value { - Ok(Some(v)) + Ok(v) } else { Ok(store.default(&self.metadata)?) } @@ -340,7 +340,7 @@ where account_id: &::AccountId, call: C, ) -> Result>::Extra>, Error> { - let account_nonce = self.account(account_id).await?.unwrap().nonce; + let account_nonce = self.account(account_id).await?.nonce; let version = self.runtime_version.spec_version; let genesis_hash = self.genesis_hash; let call = self @@ -367,7 +367,7 @@ where let account_id = S::Signer::from(signer.public()).into_account(); let nonce = match nonce { Some(nonce) => nonce, - None => self.account(&account_id).await?.unwrap().nonce, + None => self.account(&account_id).await?.nonce, }; let genesis_hash = self.genesis_hash; diff --git a/src/metadata.rs b/src/metadata.rs index ae0b67bf56..80fcbe60aa 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -24,6 +24,7 @@ use std::{ use codec::{ Decode, Encode, + Error as CodecError, }; use frame_metadata::{ @@ -63,6 +64,9 @@ pub enum MetadataError { /// Storage type does not match requested type. #[error("Storage type error")] StorageTypeError, + /// Default error. + #[error("Failed to decode default: {0}")] + DefaultError(CodecError), } /// Runtime metadata. @@ -218,9 +222,9 @@ impl StorageMetadata { bytes } - pub fn default(&self) -> Option { - // substrate handles the default different for A => B vs A => Option - Decode::decode(&mut &self.default[..]).ok() + pub fn default(&self) -> Result { + Decode::decode(&mut &self.default[..]) + .map_err(|err| MetadataError::DefaultError(err)) } pub fn hash(hasher: &StorageHasher, bytes: &[u8]) -> Vec {