From fb46b4ec60f99fc7f6b30821ab89f801f2370b41 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 2 Dec 2021 15:38:32 +0000 Subject: [PATCH 1/8] Make system properties an arbitrary JSON object --- src/rpc.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/rpc.rs b/src/rpc.rs index 1543a6b228..1e43c14cf6 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -141,17 +141,8 @@ impl From for BlockNumber { } } -/// System properties for a Substrate-based runtime -#[derive(serde::Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)] -#[serde(rename_all = "camelCase")] -pub struct SystemProperties { - /// The address format - pub ss58_format: u8, - /// The number of digits after the decimal point in the native token - pub token_decimals: u8, - /// The symbol of the native token - pub token_symbol: String, -} +/// Arbitrary properties defined in the chain spec as a JSON object. +pub type SystemProperties = serde_json::Map; /// Possible transaction status events. /// From 7814dadc6ea14151148be714f64030d3816a3654 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 2 Dec 2021 15:53:22 +0000 Subject: [PATCH 2/8] Add comment --- src/client.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index c243d0fc91..8dfd0b1411 100644 --- a/src/client.rs +++ b/src/client.rs @@ -157,7 +157,14 @@ impl Client { &self.metadata } - /// Returns the system properties + /// Returns the properties defined in the chain spec as a JSON object. + /// + /// # Note + /// + /// Some chains use this to define common properties such as `token_decimals` and `token_symbol` + /// required for UIs, but this is merely a convention. So it is up to the library user to + /// deserialize the JSON into the appropriate type or otherwise extract the properties defined + /// in the target chain's spec. pub fn properties(&self) -> &SystemProperties { &self.properties } From 2a8aa6f871d0c0c41e3e2cfd1d1e7952fe20ccf8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 2 Dec 2021 15:57:06 +0000 Subject: [PATCH 3/8] Make timestamp test more reliable --- tests/integration/frame/timestamp.rs | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/tests/integration/frame/timestamp.rs b/tests/integration/frame/timestamp.rs index 301305783f..5d1b302183 100644 --- a/tests/integration/frame/timestamp.rs +++ b/tests/integration/frame/timestamp.rs @@ -15,34 +15,17 @@ // along with subxt. If not, see . use crate::test_context; -use std::time::{ - SystemTime, - UNIX_EPOCH, -}; #[async_std::test] async fn storage_get_current_timestamp() { - let sys_timestamp = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_millis() as u64; let cxt = test_context().await; - // wait until blocks are produced to get the timestamp - let mut sub = cxt.client().rpc().subscribe_blocks().await.unwrap(); - let block_hash = loop { - if let Ok(Some(block)) = sub.next().await { - break block.hash() - } - }; - let timestamp = cxt .api .storage() .timestamp() - .now(Some(block_hash)) - .await - .unwrap(); + .now(None) + .await; - assert!(timestamp > sys_timestamp) + assert!(timestamp.is_ok()) } From 6faa6b255ed771c77803ef5984880f5bf4984a98 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 2 Dec 2021 16:06:23 +0000 Subject: [PATCH 4/8] Fmt --- tests/integration/frame/timestamp.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/integration/frame/timestamp.rs b/tests/integration/frame/timestamp.rs index 5d1b302183..1aa9f78dd9 100644 --- a/tests/integration/frame/timestamp.rs +++ b/tests/integration/frame/timestamp.rs @@ -20,12 +20,7 @@ use crate::test_context; async fn storage_get_current_timestamp() { let cxt = test_context().await; - let timestamp = cxt - .api - .storage() - .timestamp() - .now(None) - .await; + let timestamp = cxt.api.storage().timestamp().now(None).await; assert!(timestamp.is_ok()) } From a8308fcc8f9980560fe6f30ed20555921670b7bf Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 2 Dec 2021 16:18:16 +0000 Subject: [PATCH 5/8] Update src/client.rs Co-authored-by: David --- src/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 8dfd0b1411..ebb84b4441 100644 --- a/src/client.rs +++ b/src/client.rs @@ -161,7 +161,7 @@ impl Client { /// /// # Note /// - /// Some chains use this to define common properties such as `token_decimals` and `token_symbol` + /// Many chains use this to define common properties such as `token_decimals` and `token_symbol` /// required for UIs, but this is merely a convention. So it is up to the library user to /// deserialize the JSON into the appropriate type or otherwise extract the properties defined /// in the target chain's spec. From 6f962d366acd470bc6a803f01567b879dc5af9e9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 2 Dec 2021 16:18:23 +0000 Subject: [PATCH 6/8] Update src/client.rs Co-authored-by: David --- src/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index ebb84b4441..b2bd582616 100644 --- a/src/client.rs +++ b/src/client.rs @@ -162,7 +162,7 @@ impl Client { /// # Note /// /// Many chains use this to define common properties such as `token_decimals` and `token_symbol` - /// required for UIs, but this is merely a convention. So it is up to the library user to + /// required for UIs, but this is merely a convention. It is up to the library user to /// deserialize the JSON into the appropriate type or otherwise extract the properties defined /// in the target chain's spec. pub fn properties(&self) -> &SystemProperties { From b93d637ee58b1711061b6493839f1bfb6ef0e484 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 2 Dec 2021 16:35:30 +0000 Subject: [PATCH 7/8] Fix clippy errors and warnings --- cli/src/main.rs | 4 ++-- codegen/src/ir.rs | 1 + codegen/src/types/tests.rs | 6 +++--- src/metadata.rs | 5 +++++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 271a720903..44a7189eae 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -120,7 +120,7 @@ fn main() -> color_eyre::Result<()> { }); let (_, bytes) = fetch_metadata(&url)?; codegen(&mut &bytes[..])?; - return Ok(()) + Ok(()) } } } @@ -139,7 +139,7 @@ fn fetch_metadata(url: &url::Url) -> color_eyre::Result<(String, Vec)> { let hex_data = json["result"] .as_str() .map(ToString::to_string) - .ok_or(eyre::eyre!("metadata result field should be a string"))?; + .ok_or_else(|| eyre::eyre!("metadata result field should be a string"))?; let bytes = hex::decode(hex_data.trim_start_matches("0x"))?; Ok((hex_data, bytes)) diff --git a/codegen/src/ir.rs b/codegen/src/ir.rs index f214241fda..779fee4545 100644 --- a/codegen/src/ir.rs +++ b/codegen/src/ir.rs @@ -73,6 +73,7 @@ impl ItemMod { } #[derive(Debug, PartialEq, Eq)] +#[allow(clippy::large_enum_variant)] pub enum Item { Rust(syn::Item), Subxt(SubxtItem), diff --git a/codegen/src/types/tests.rs b/codegen/src/types/tests.rs index dac5bf7c62..1fc1e3539b 100644 --- a/codegen/src/types/tests.rs +++ b/codegen/src/types/tests.rs @@ -22,7 +22,7 @@ use scale_info::{ TypeInfo, }; -const MOD_PATH: &'static [&'static str] = &["subxt_codegen", "types", "tests"]; +const MOD_PATH: &[&str] = &["subxt_codegen", "types", "tests"]; fn get_mod<'a>(module: &'a Module, path_segs: &[&'static str]) -> Option<&'a Module<'a>> { let (mod_name, rest) = path_segs.split_first()?; @@ -790,7 +790,7 @@ fn generics_with_alias_adds_phantom_data_marker() { #[test] fn modules() { - mod modules { + mod m { pub mod a { #[allow(unused)] #[derive(scale_info::TypeInfo)] @@ -815,7 +815,7 @@ fn modules() { } let mut registry = Registry::new(); - registry.register_type(&meta_type::()); + registry.register_type(&meta_type::()); let portable_types: PortableRegistry = registry.into(); let type_gen = TypeGenerator::new( diff --git a/src/metadata.rs b/src/metadata.rs index f8f4e2ad01..51af03f282 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -143,6 +143,11 @@ pub struct PalletMetadata { } impl PalletMetadata { + /// Get the name of the pallet. + pub fn name(&self) -> &str { + &self.name + } + /// Encode a call based on this pallet metadata. pub fn encode_call(&self, call: &C) -> Result where From bb4ac9a6d3a3ddc9bf7ab2478bff0fda1d761222 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 2 Dec 2021 16:54:17 +0000 Subject: [PATCH 8/8] Fix tests --- codegen/src/types/tests.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/codegen/src/types/tests.rs b/codegen/src/types/tests.rs index 1fc1e3539b..21364f429e 100644 --- a/codegen/src/types/tests.rs +++ b/codegen/src/types/tests.rs @@ -832,7 +832,7 @@ fn modules() { quote! { pub mod tests { use super::root; - pub mod modules { + pub mod m { use super::root; pub mod a { use super::root; @@ -842,7 +842,7 @@ fn modules() { #[derive(::subxt::codec::Encode, ::subxt::codec::Decode)] pub struct Bar { - pub a: root::subxt_codegen::types::tests::modules::a::Foo, + pub a: root::subxt_codegen::types::tests::m::a::Foo, } } @@ -855,7 +855,7 @@ fn modules() { #[derive(::subxt::codec::Encode, ::subxt::codec::Decode)] pub struct Foo { - pub a: root::subxt_codegen::types::tests::modules::a::b::Bar, + pub a: root::subxt_codegen::types::tests::m::a::b::Bar, } } }