Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add AccountInfo and Data structs #56

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sube/examples/pallet_communities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ async fn main() -> Result<()> {
let data = serde_json::to_value(&value).expect("it must be a serialized object");
println!("Account info: {}", serde_json::to_string_pretty(&data).expect("it must return an str"));
}

Ok(())
}
35 changes: 31 additions & 4 deletions sube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ pub struct ExtrinsicBody<Body> {
pub body: Body,
}

#[derive(Deserialize, Debug)]
pub struct AccountInfo {
nonce: u64,
consumers: u64,
providers: u64,
sufficients: u64,
data: Data,
}

#[derive(Deserialize, Debug)]
pub struct Data {
free: u128,
reserved: u128,
frozen: u128,
flags: u128,
}


async fn submit<'m, V>(
chain: impl Backend,
meta: &'m Metadata,
Expand Down Expand Up @@ -163,9 +181,12 @@ where

match response {
Response::Value(value) => {
let bytes: [u8; 8] = value.as_ref()[..8].try_into().expect("fits");
let nonce = u64::from_le_bytes(bytes);
Ok(nonce)
log::info!("{:?}", serde_json::to_string(&value));
let str = serde_json::to_string(&value).expect("wrong account info");
let account_info: AccountInfo =
serde_json::from_str(&str).expect("it must serialize");
log::info!("{}", &account_info.nonce);
Ok(account_info.nonce)
}
_ => Err(Error::AccountNotFound),
}
Expand All @@ -174,7 +195,13 @@ where

let tip: u128 = 0;

[vec![era], Compact(nonce).encode(), Compact(tip).encode()].concat()
[
vec![era],
Compact(nonce).encode(),
Compact(tip).encode(),
vec![0x00u8], // chain extension for kreivo
]
.concat()
};

let additional_params = {
Expand Down
2 changes: 1 addition & 1 deletion sube/src/meta_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub trait EntryTy {
key.append(&mut hash(&Hasher::Twox128, &item));

if map_keys.len() != hashers.len() {
return None;
return Some(key);
}

for (i, h) in hashers.iter().enumerate() {
Expand Down
14 changes: 3 additions & 11 deletions sube/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait Rpc: Backend + Send + Sync {
fn convert_params(params: &[&str]) -> Vec<Box<RawValue>> {
params
.iter()
.map(|p| format!("\"{}\"", p))
.map(|p| format!("{}", p))
.map(RawValue::from_string)
.map(Result::unwrap)
.collect::<Vec<_>>()
Expand All @@ -30,7 +30,7 @@ impl<R: Rpc> Backend for R {
async fn query_storage(&self, key: &StorageKey) -> crate::Result<Vec<u8>> {
let key = key.to_string();
log::debug!("StorageKey encoded: {}", key);
self.rpc("state_getStorage", &[&key]).await.map_err(|e| {
self.rpc("state_getStorage", &[&format!("\"{}\"", &key)]).await.map_err(|e| {
log::debug!("RPC failure: {}", e);
// NOTE it could fail for more reasons
crate::Error::StorageKeyNotFound
Expand All @@ -45,7 +45,7 @@ impl<R: Rpc> Backend for R {
log::debug!("Extrinsic: {}", extrinsic);

let res = self
.rpc("author_submitExtrinsic", &[&extrinsic])
.rpc("author_submitExtrinsic", &[&format!("\"{}\"", &extrinsic)])
.await
.map_err(|e| crate::Error::Node(e.to_string()))?;
log::debug!("Extrinsic {:x?}", res);
Expand Down Expand Up @@ -80,14 +80,6 @@ impl<R: Rpc> Backend for R {
block_info(self, &[]).await?
};

// TODO: Make sure to complete this in a future
// Hint: RPC should not deserialize the JSON-RPC result
// This produces a deserialization error
// let block = self
// .rpc("chain_getBlock", &[&hex::encode(&block_hash)])
// .await
// .map_err(|e| crate::Error::Node(e.to_string()))?;

Ok(meta::BlockInfo {
number: at.unwrap_or(0) as u64,
hash: block_hash[0..32]
Expand Down
Loading