From fce85c4af017757bc17990b1d8beba7d69e5639c Mon Sep 17 00:00:00 2001 From: Tadeo hepperle Date: Fri, 2 Feb 2024 16:50:41 +0100 Subject: [PATCH] start fixing examples and cleaning up interface --- core/src/config/extrinsic_params.rs | 7 ++--- core/src/config/signed_extensions.rs | 18 +++++------ core/src/config/substrate.rs | 6 +--- core/src/constants/mod.rs | 2 +- core/src/custom_values/mod.rs | 2 +- core/src/lib.rs | 8 +---- core/src/utils/mod.rs | 8 +++++ subxt/examples/setup_client_offline.rs | 2 +- subxt/examples/setup_config_custom.rs | 3 +- .../examples/setup_config_signed_extension.rs | 8 ++--- subxt/src/backend/legacy/rpc_methods.rs | 7 +---- subxt/src/backend/unstable/rpc_methods.rs | 5 +-- subxt/src/client/light_client/mod.rs | 12 ++++--- subxt/src/client/online_client.rs | 2 +- subxt/src/constants/constants_client.rs | 7 +---- subxt/src/dynamic.rs | 31 ------------------- subxt/src/lib.rs | 9 +++--- subxt/src/macros.rs | 2 +- subxt/src/tx/mod.rs | 2 -- subxt/src/tx/tx_client.rs | 2 +- 20 files changed, 48 insertions(+), 95 deletions(-) delete mode 100644 subxt/src/dynamic.rs diff --git a/core/src/config/extrinsic_params.rs b/core/src/config/extrinsic_params.rs index 5965843c5b..5e7b702a78 100644 --- a/core/src/config/extrinsic_params.rs +++ b/core/src/config/extrinsic_params.rs @@ -11,11 +11,8 @@ use crate::client::ClientBase; use super::Config; use crate::ExtrinsicParamsError; -use alloc::string::String; -use alloc::vec::Vec; -use core::fmt::Debug; -use derive_more::Display; +use alloc::vec::Vec; /// This trait allows you to configure the "signed extra" and /// "additional" parameters that are a part of the transaction payload @@ -29,7 +26,7 @@ pub trait ExtrinsicParams: ExtrinsicParamsEncoder + Sized + 'static { /// Construct a new instance of our [`ExtrinsicParams`]. fn new( nonce: u64, - client: &ClientBase, + client: ClientBase, other_params: Self::OtherParams, ) -> Result; } diff --git a/core/src/config/signed_extensions.rs b/core/src/config/signed_extensions.rs index 79a8f027d6..e42e8e2d1d 100644 --- a/core/src/config/signed_extensions.rs +++ b/core/src/config/signed_extensions.rs @@ -45,7 +45,7 @@ impl ExtrinsicParams for CheckSpecVersion { fn new( _nonce: u64, - client: &ClientBase, + client: ClientBase, _other_params: Self::OtherParams, ) -> Result { Ok(CheckSpecVersion(client.runtime_version().spec_version)) @@ -73,7 +73,7 @@ impl ExtrinsicParams for CheckNonce { fn new( nonce: u64, - _client: &ClientBase, + _client: ClientBase, _other_params: Self::OtherParams, ) -> Result { Ok(CheckNonce(Compact(nonce))) @@ -101,7 +101,7 @@ impl ExtrinsicParams for CheckTxVersion { fn new( _nonce: u64, - client: &ClientBase, + client: ClientBase, _other_params: Self::OtherParams, ) -> Result { Ok(CheckTxVersion(client.runtime_version().transaction_version)) @@ -129,7 +129,7 @@ impl ExtrinsicParams for CheckGenesis { fn new( _nonce: u64, - client: &ClientBase, + client: ClientBase, _other_params: Self::OtherParams, ) -> Result { Ok(CheckGenesis(client.genesis_hash())) @@ -195,7 +195,7 @@ impl ExtrinsicParams for CheckMortality { fn new( _nonce: u64, - client: &ClientBase, + client: ClientBase, other_params: Self::OtherParams, ) -> Result { Ok(CheckMortality { @@ -286,7 +286,7 @@ impl ExtrinsicParams for ChargeAssetTxPayment { fn new( _nonce: u64, - _client: &ClientBase, + _client: ClientBase, other_params: Self::OtherParams, ) -> Result { Ok(ChargeAssetTxPayment { @@ -344,7 +344,7 @@ impl ExtrinsicParams for ChargeTransactionPayment { fn new( _nonce: u64, - _client: &ClientBase, + _client: ClientBase, other_params: Self::OtherParams, ) -> Result { Ok(ChargeTransactionPayment { @@ -388,7 +388,7 @@ macro_rules! impl_tuples { fn new( nonce: u64, - client: &ClientBase, + client: ClientBase, other_params: Self::OtherParams, ) -> Result { let metadata = client.metadata(); @@ -405,7 +405,7 @@ macro_rules! impl_tuples { } // Break and record as soon as we find a match: if $ident::matches(e.identifier(), e.extra_ty(), types) { - let ext = $ident::new(nonce, client, other_params.$index)?; + let ext = $ident::new(nonce, client.clone(), other_params.$index)?; let boxed_ext: Box = Box::new(ext); exts_by_index.insert(idx, boxed_ext); break diff --git a/core/src/config/substrate.rs b/core/src/config/substrate.rs index a7c5e99215..30b333c2e7 100644 --- a/core/src/config/substrate.rs +++ b/core/src/config/substrate.rs @@ -6,7 +6,6 @@ use super::{Config, DefaultExtrinsicParams, DefaultExtrinsicParamsBuilder, Hasher, Header}; pub use crate::utils::{AccountId32, MultiAddress, MultiSignature}; -use alloc::string::String; use alloc::vec::Vec; use codec::{Decode, Encode}; pub use primitive_types::{H256, U256}; @@ -289,10 +288,7 @@ impl From for NumberOrHex { } } -/// A quick helper to encode some bytes to hex. -fn to_hex(bytes: impl AsRef<[u8]>) -> String { - alloc::format!("0x{}", hex::encode(bytes.as_ref())) -} + #[cfg(test)] mod test { diff --git a/core/src/constants/mod.rs b/core/src/constants/mod.rs index 607877c001..2d98e0e2a7 100644 --- a/core/src/constants/mod.rs +++ b/core/src/constants/mod.rs @@ -52,7 +52,7 @@ pub fn get_constant( let value = ::decode_with_metadata( &mut constant.value(), constant.ty(), - &metadata, + metadata, )?; Ok(value) } diff --git a/core/src/custom_values/mod.rs b/core/src/custom_values/mod.rs index d0d418bc0e..9a58c94db1 100644 --- a/core/src/custom_values/mod.rs +++ b/core/src/custom_values/mod.rs @@ -51,7 +51,7 @@ pub fn get_custom_value + ?Sized> let value = ::decode_with_metadata( &mut custom_value.bytes(), custom_value.type_id(), - &metadata, + metadata, )?; Ok(value) } diff --git a/core/src/lib.rs b/core/src/lib.rs index e071e28c20..76239d19d7 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -33,14 +33,8 @@ pub use config::{ pub use metadata::Metadata; pub use signer::Signer; pub use storage::StorageAddress; -pub use utils::{AccountId32, MultiAddress, MultiSignature, H160, H256, H512}; +pub use utils::{AccountId32, MultiAddress, MultiSignature, H160, H256, H512, Yes, to_hex}; #[macro_use] mod macros; -mod marker { - /// A unit marker struct signalling that some property is true - pub struct Yes; -} - -pub use marker::Yes; diff --git a/core/src/utils/mod.rs b/core/src/utils/mod.rs index 54eaf230ed..1d0f254075 100644 --- a/core/src/utils/mod.rs +++ b/core/src/utils/mod.rs @@ -78,3 +78,11 @@ unsafe impl Sync for PhantomDataSendSync {} /// with collections like BTreeMap. This has the same type params /// as `BTreeMap` which allows us to easily swap the two during codegen. pub type KeyedVec = Vec<(K, V)>; + + /// A unit marker struct signalling that some property is true + pub struct Yes; + + /// A quick helper to encode some bytes to hex. +pub fn to_hex(bytes: impl AsRef<[u8]>) -> String { + alloc::format!("0x{}", hex::encode(bytes.as_ref())) +} \ No newline at end of file diff --git a/subxt/examples/setup_client_offline.rs b/subxt/examples/setup_client_offline.rs index 70afc0a41c..b2e952c2fd 100644 --- a/subxt/examples/setup_client_offline.rs +++ b/subxt/examples/setup_client_offline.rs @@ -16,7 +16,7 @@ async fn main() -> Result<(), Box> { }; // 2. A runtime version (system_version constant on a Substrate node has these): - let runtime_version = subxt::backend::RuntimeVersion { + let runtime_version = subxt_core::RuntimeVersion { spec_version: 9370, transaction_version: 20, }; diff --git a/subxt/examples/setup_config_custom.rs b/subxt/examples/setup_config_custom.rs index 4748202e1f..713145d6dc 100644 --- a/subxt/examples/setup_config_custom.rs +++ b/subxt/examples/setup_config_custom.rs @@ -1,7 +1,8 @@ #![allow(missing_docs)] use codec::Encode; use subxt::client::OfflineClientT; -use subxt::config::{Config, ExtrinsicParams, ExtrinsicParamsEncoder, ExtrinsicParamsError}; +use subxt::config::{Config, ExtrinsicParams, ExtrinsicParamsEncoder}; +use subxt_core::error::ExtrinsicParamsError; use subxt_signer::sr25519::dev; #[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale")] diff --git a/subxt/examples/setup_config_signed_extension.rs b/subxt/examples/setup_config_signed_extension.rs index 2442de629b..a5492ff91c 100644 --- a/subxt/examples/setup_config_signed_extension.rs +++ b/subxt/examples/setup_config_signed_extension.rs @@ -3,11 +3,11 @@ use codec::Encode; use scale_encode::EncodeAsType; use scale_info::PortableRegistry; use subxt::client::OfflineClientT; -use subxt::config::signed_extensions; use subxt::config::{ Config, DefaultExtrinsicParamsBuilder, ExtrinsicParams, ExtrinsicParamsEncoder, - ExtrinsicParamsError, }; +use subxt_core::config::signed_extensions; +use subxt_core::error::ExtrinsicParamsError; use subxt_signer::sr25519::dev; #[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")] @@ -60,9 +60,9 @@ impl signed_extensions::SignedExtension for CustomSignedExtension impl ExtrinsicParams for CustomSignedExtension { type OtherParams = (); - fn new>( + fn new( _nonce: u64, - _client: Client, + _client: subxt_core::ClientBase, _other_params: Self::OtherParams, ) -> Result { Ok(CustomSignedExtension) diff --git a/subxt/src/backend/legacy/rpc_methods.rs b/subxt/src/backend/legacy/rpc_methods.rs index 84a8200adb..616cc0adca 100644 --- a/subxt/src/backend/legacy/rpc_methods.rs +++ b/subxt/src/backend/legacy/rpc_methods.rs @@ -10,7 +10,7 @@ use codec::Decode; use derivative::Derivative; use primitive_types::U256; use serde::{Deserialize, Serialize}; -use subxt_core::metadata::Metadata; +use subxt_core::{metadata::Metadata, to_hex}; /// An interface to call the legacy RPC methods. This interface is instantiated with /// some `T: Config` trait which determines some of the types that the RPC methods will @@ -656,11 +656,6 @@ impl From for NumberOrHex { } } -/// A quick helper to encode some bytes to hex. -fn to_hex(bytes: impl AsRef<[u8]>) -> String { - format!("0x{}", hex::encode(bytes.as_ref())) -} - /// Hex-serialized shim for `Vec`. #[derive(PartialEq, Eq, Clone, Serialize, Deserialize, Hash, PartialOrd, Ord, Debug)] pub struct Bytes(#[serde(with = "impl_serde::serialize")] pub Vec); diff --git a/subxt/src/backend/unstable/rpc_methods.rs b/subxt/src/backend/unstable/rpc_methods.rs index c51e5020cb..a397d2fbdb 100644 --- a/subxt/src/backend/unstable/rpc_methods.rs +++ b/subxt/src/backend/unstable/rpc_methods.rs @@ -14,6 +14,7 @@ use futures::{Stream, StreamExt}; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, VecDeque}; use std::task::Poll; +use subxt_core::to_hex; /// An interface to call the unstable RPC methods. This interface is instantiated with /// some `T: Config` trait which determines some of the types that the RPC methods will @@ -739,10 +740,6 @@ impl From> for Bytes { } } -fn to_hex(bytes: impl AsRef<[u8]>) -> String { - format!("0x{}", hex::encode(bytes.as_ref())) -} - /// Attempt to deserialize either a string or integer into an integer. /// See pub(crate) mod unsigned_number_as_string { diff --git a/subxt/src/client/light_client/mod.rs b/subxt/src/client/light_client/mod.rs index cc8c022976..15846355e8 100644 --- a/subxt/src/client/light_client/mod.rs +++ b/subxt/src/client/light_client/mod.rs @@ -118,7 +118,7 @@ impl LightClient { // traits to use these things: /// Return the [`subxt_core::metadata`] used in this client. - fn metadata(&self) -> subxt_core::metadata { + fn metadata(&self) -> subxt_core::Metadata { self.client.metadata() } @@ -128,7 +128,7 @@ impl LightClient { } /// Return the runtime version. - fn runtime_version(&self) -> crate::backend::RuntimeVersion { + fn runtime_version(&self) -> subxt_core::RuntimeVersion { self.client.runtime_version() } @@ -180,7 +180,7 @@ impl OnlineClientT for LightClient { } impl OfflineClientT for LightClient { - fn metadata(&self) -> subxt_core::metadata { + fn metadata(&self) -> subxt_core::Metadata { self.metadata() } @@ -188,7 +188,11 @@ impl OfflineClientT for LightClient { self.genesis_hash() } - fn runtime_version(&self) -> crate::backend::RuntimeVersion { + fn runtime_version(&self) -> subxt_core::RuntimeVersion { self.runtime_version() } + + fn base(&self) -> subxt_core::ClientBase { + self.client.base() + } } diff --git a/subxt/src/client/online_client.rs b/subxt/src/client/online_client.rs index 36a742ee3f..8fbc9c821e 100644 --- a/subxt/src/client/online_client.rs +++ b/subxt/src/client/online_client.rs @@ -17,7 +17,7 @@ use crate::{ }; use derivative::Derivative; use futures::future; -use std::borrow::Borrow; + use std::sync::{Arc, RwLock}; use subxt_core::{ClientBase, RuntimeVersion}; diff --git a/subxt/src/constants/constants_client.rs b/subxt/src/constants/constants_client.rs index b3e4b74aca..4af0fb96ce 100644 --- a/subxt/src/constants/constants_client.rs +++ b/subxt/src/constants/constants_client.rs @@ -3,14 +3,9 @@ // see LICENSE for license details. use super::ConstantAddress; -use crate::{ - client::OfflineClientT, - error::{Error, MetadataError}, - Config, -}; +use crate::{client::OfflineClientT, error::Error, Config}; use derivative::Derivative; -use subxt_core::metadata::DecodeWithMetadata; /// A client for accessing constants. #[derive(Derivative)] diff --git a/subxt/src/dynamic.rs b/subxt/src/dynamic.rs deleted file mode 100644 index 771815825a..0000000000 --- a/subxt/src/dynamic.rs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2019-2023 Parity Technologies (UK) Ltd. -// This file is dual-licensed as Apache-2.0 or GPL-3.0. -// see LICENSE for license details. - -//! This module provides the entry points to create dynamic -//! transactions, storage and constant lookups. - -use crate::error::Error; - -use scale_decode::DecodeAsType; -use subxt_core::metadata::{DecodeWithMetadata, Metadata}; - -pub use scale_value::{At, Value}; - -/// A [`scale_value::Value`] type endowed with contextual information -/// regarding what type was used to decode each part of it. This implements -/// [`subxt_core::metadata::DecodeWithMetadata`], and is used as a return type -/// for dynamic requests. -pub type DecodedValue = scale_value::Value; - -// Submit dynamic transactions. -pub use crate::tx::dynamic as tx; - -// Lookup constants dynamically. -pub use crate::constants::dynamic as constant; - -// Lookup storage values dynamically. -pub use crate::storage::dynamic as storage; - -// Execute runtime API function call dynamically. -pub use crate::runtime_api::dynamic as runtime_api_call; diff --git a/subxt/src/lib.rs b/subxt/src/lib.rs index 94fa7697f8..723a363a1f 100644 --- a/subxt/src/lib.rs +++ b/subxt/src/lib.rs @@ -47,7 +47,6 @@ pub mod blocks; pub mod client; pub mod constants; pub mod custom_values; -pub mod dynamic; pub mod error; pub mod events; pub mod runtime_api; @@ -66,10 +65,10 @@ pub use crate::{ error::Error, }; -pub use subxt_core::config; -pub use subxt_core::config::{Config, PolkadotConfig, SubstrateConfig}; - -use subxt_core::metadata::Metadata; +// pub use subxt_core::config; +// pub use subxt_core::config::{Config, PolkadotConfig, SubstrateConfig}; +// pub use subxt_core::dynamic; +pub use subxt_core::*; /// Re-export external crates that are made use of in the subxt API. pub mod ext { diff --git a/subxt/src/macros.rs b/subxt/src/macros.rs index a8d86ada69..4b2e91766e 100644 --- a/subxt/src/macros.rs +++ b/subxt/src/macros.rs @@ -52,7 +52,7 @@ macro_rules! cfg_jsonrpsee_web { } } -pub(crate) use {cfg_feature, cfg_jsonrpsee, cfg_substrate_compat, cfg_unstable_light_client}; +pub(crate) use {cfg_feature, cfg_jsonrpsee, cfg_unstable_light_client}; // Only used by light-client. #[allow(unused)] diff --git a/subxt/src/tx/mod.rs b/subxt/src/tx/mod.rs index 655198712e..b1e988c40d 100644 --- a/subxt/src/tx/mod.rs +++ b/subxt/src/tx/mod.rs @@ -9,8 +9,6 @@ //! additional and signed extra parameters are used when constructing an extrinsic, and is a part //! of the chain configuration (see [`crate::config::Config`]). -use crate::macros::cfg_substrate_compat; - mod tx_client; mod tx_progress; diff --git a/subxt/src/tx/tx_client.rs b/subxt/src/tx/tx_client.rs index d3121d9eb7..8029781f91 100644 --- a/subxt/src/tx/tx_client.rs +++ b/subxt/src/tx/tx_client.rs @@ -124,7 +124,7 @@ impl> TxClient { // 3. Construct our custom additional/extra params. let additional_and_extra_params = >::new( account_nonce, - &self.client.base(), + self.client.base(), other_params, )?;