Skip to content

Commit

Permalink
Fix/storage key not found (#77)
Browse files Browse the repository at this point in the history
* avoid to throw storage key not found when the result is empty

* fix: examples

* chore: clippy
  • Loading branch information
S0c5 authored Aug 22, 2024
1 parent 864afab commit 1bbff6b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
12 changes: 12 additions & 0 deletions sube/examples/pallet_communities.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
use env_logger;
use futures_util::future::join_all;
use serde_json;
use sube::{sube, ExtrinsicBody, Response, Result, SubeBuilder};


#[async_std::main]
async fn main() -> Result<()> {
env_logger::init();

let response = sube!("wss://kreivo.io/communityMemberships/account/0xe25b1e3758a5fbedb956b36113252f9e866d3ece688364cc9d34eb01f4b2125d/2").await.expect("to work");

if let Response::ValueSet(value) = response {
let data = serde_json::to_value(&value).expect("to be serializable");
println!(
"Collection Array {}",
serde_json::to_string_pretty(&data).expect("it must return an str")
);
}

let response = sube!("ws://127.0.0.1:12281/communityMemberships/collection").await?;

if let Response::ValueSet(value) = response {
Expand Down
2 changes: 1 addition & 1 deletion sube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ pub trait Backend {

async fn get_storage_item(&self, key: RawKey, block: Option<u32>) -> crate::Result<RawValue> {
let res = self.get_storage_items(vec![key], block).await?;

log::info!("before it died");
res.into_iter()
.next()
.map(|(_, v)| v)
Expand Down
40 changes: 21 additions & 19 deletions sube/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Deserialize;
use crate::meta::{self, Metadata};
use crate::Backend;
use crate::Error;
use crate::{prelude::*, RawKey, StorageChangeSet};
use crate::{prelude::*, RawKey as RawStorageKey, StorageChangeSet};
use meta::from_bytes;

pub type RpcResult<T> = Result<T, error::Error>;
Expand All @@ -32,7 +32,7 @@ pub struct RpcClient<R>(pub R);
impl<R: Rpc> Backend for RpcClient<R> {
async fn get_storage_items(
&self,
keys: Vec<RawKey>,
keys: Vec<RawStorageKey>,
block: Option<u32>,
) -> crate::Result<impl Iterator<Item = (Vec<u8>, Vec<u8>)>> {
let keys = serde_json::to_string(
Expand All @@ -54,7 +54,6 @@ impl<R: Rpc> Backend for RpcClient<R> {
vec![keys]
};


let result = self
.0
.rpc::<Vec<StorageChangeSet>>(
Expand All @@ -71,28 +70,31 @@ impl<R: Rpc> Backend for RpcClient<R> {
crate::Error::StorageKeyNotFound
})?;

if let Some(change_set) = result.into_iter().next() {
let keys_response = change_set.changes.into_iter().map(|[k, v]| {
log::info!("key: {} value: {}", k, v);

(
hex::decode(&k[2..]).expect("to be an hex"),
hex::decode(&v[2..]).expect("to be an hex"),
)
});
let result = match result.into_iter().next() {
None => vec![],
Some(change_set) => change_set
.changes
.into_iter()
.map(|[k, v]| {
log::info!("key: {} value: {}", k, v);

(
hex::decode(&k[2..]).expect("to be an hex"),
hex::decode(&v[2..]).expect("to be an hex"),
)
})
.collect(),
};

Ok(keys_response)
} else {
Err(crate::Error::StorageKeyNotFound)
}
Ok(result.into_iter())
}

async fn get_keys_paged(
&self,
from: RawKey,
from: RawStorageKey,
size: u16,
to: Option<RawKey>,
) -> crate::Result<Vec<RawKey>> {
to: Option<RawStorageKey>,
) -> crate::Result<Vec<RawStorageKey>> {
let result: Vec<String> = self
.0
.rpc(
Expand Down

0 comments on commit 1bbff6b

Please sign in to comment.