Add a method to fetch an unhashed key, close #100 (#152)

* Add a method to fetch an unhashed key, close #100

* Return decoded value

* Refactoring
This commit is contained in:
Yuanchao Sun
2020-08-14 04:06:30 +08:00
committed by GitHub
parent 9aa32ef63d
commit 59b9c117b7
+14 -5
View File
@@ -258,6 +258,19 @@ impl<T: Runtime> Client<T> {
&self.metadata
}
/// Fetch the value under an unhashed storage key
pub async fn fetch_unhashed<V: Decode>(
&self,
key: StorageKey,
hash: Option<T::Hash>,
) -> Result<Option<V>, Error> {
if let Some(data) = self.rpc.storage(&key, hash).await? {
Ok(Some(Decode::decode(&mut &data.0[..])?))
} else {
Ok(None)
}
}
/// Fetch a StorageKey with an optional block hash.
pub async fn fetch<F: Store<T>>(
&self,
@@ -265,11 +278,7 @@ impl<T: Runtime> Client<T> {
hash: Option<T::Hash>,
) -> Result<Option<F::Returns>, Error> {
let key = store.key(&self.metadata)?;
if let Some(data) = self.rpc.storage(&key, hash).await? {
Ok(Some(Decode::decode(&mut &data.0[..])?))
} else {
Ok(None)
}
self.fetch_unhashed::<F::Returns>(key, hash).await
}
/// Fetch a StorageKey that has a default value with an optional block hash.