mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 04:41:02 +00:00
Add LightClient::builder() (#1088)
* Add LightClient::build() * newline * fmt * Tweaks to make LightClient more like OnlineClient in interface
This commit is contained in:
@@ -12,14 +12,15 @@ use std::sync::Arc;
|
||||
|
||||
/// Builder for [`LightClient`].
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct LightClientBuilder {
|
||||
pub struct LightClientBuilder<T: Config> {
|
||||
max_pending_requests: NonZeroU32,
|
||||
max_subscriptions: u32,
|
||||
bootnodes: Option<Vec<serde_json::Value>>,
|
||||
potential_relay_chains: Option<Vec<ChainId>>,
|
||||
_marker: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
impl Default for LightClientBuilder {
|
||||
impl<T: Config> Default for LightClientBuilder<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_pending_requests: NonZeroU32::new(128)
|
||||
@@ -27,13 +28,14 @@ impl Default for LightClientBuilder {
|
||||
max_subscriptions: 1024,
|
||||
bootnodes: None,
|
||||
potential_relay_chains: None,
|
||||
_marker: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LightClientBuilder {
|
||||
impl<T: Config> LightClientBuilder<T> {
|
||||
/// Create a new [`LightClientBuilder`].
|
||||
pub fn new() -> LightClientBuilder {
|
||||
pub fn new() -> LightClientBuilder<T> {
|
||||
LightClientBuilder::default()
|
||||
}
|
||||
|
||||
@@ -90,10 +92,7 @@ impl LightClientBuilder {
|
||||
///
|
||||
/// Panics if being called outside of `tokio` runtime context.
|
||||
#[cfg(feature = "jsonrpsee")]
|
||||
pub async fn build_from_url<T: Config, Url: AsRef<str>>(
|
||||
self,
|
||||
url: Url,
|
||||
) -> Result<LightClient<T>, Error> {
|
||||
pub async fn build_from_url<Url: AsRef<str>>(self, url: Url) -> Result<LightClient<T>, Error> {
|
||||
let chain_spec = fetch_url(url.as_ref()).await?;
|
||||
|
||||
self.build_client(chain_spec).await
|
||||
@@ -120,7 +119,7 @@ impl LightClientBuilder {
|
||||
/// ## Panics
|
||||
///
|
||||
/// Panics if being called outside of `tokio` runtime context.
|
||||
pub async fn build<T: Config>(self, chain_spec: &str) -> Result<LightClient<T>, Error> {
|
||||
pub async fn build(self, chain_spec: &str) -> Result<LightClient<T>, Error> {
|
||||
let chain_spec = serde_json::from_str(chain_spec)
|
||||
.map_err(|_| Error::LightClient(LightClientError::InvalidChainSpec))?;
|
||||
|
||||
@@ -128,7 +127,7 @@ impl LightClientBuilder {
|
||||
}
|
||||
|
||||
/// Build the light client.
|
||||
async fn build_client<T: Config>(
|
||||
async fn build_client(
|
||||
self,
|
||||
mut chain_spec: serde_json::Value,
|
||||
) -> Result<LightClient<T>, Error> {
|
||||
|
||||
@@ -8,8 +8,14 @@ mod builder;
|
||||
mod rpc;
|
||||
|
||||
use crate::{
|
||||
blocks::BlocksClient,
|
||||
client::{OfflineClientT, OnlineClientT},
|
||||
config::Config,
|
||||
constants::ConstantsClient,
|
||||
events::EventsClient,
|
||||
runtime_api::RuntimeApiClient,
|
||||
storage::StorageClient,
|
||||
tx::TxClient,
|
||||
OnlineClient,
|
||||
};
|
||||
pub use builder::LightClientBuilder;
|
||||
@@ -49,6 +55,62 @@ pub enum LightClientError {
|
||||
#[derivative(Clone(bound = ""))]
|
||||
pub struct LightClient<T: Config>(OnlineClient<T>);
|
||||
|
||||
impl<T: Config> LightClient<T> {
|
||||
/// Construct a [`LightClient`] using its builder interface.
|
||||
pub fn builder() -> LightClientBuilder<T> {
|
||||
LightClientBuilder::new()
|
||||
}
|
||||
|
||||
// We add the below impls so that we don't need to
|
||||
// think about importing the OnlineClientT/OfflineClientT
|
||||
// traits to use these things:
|
||||
|
||||
/// Return the [`crate::Metadata`] used in this client.
|
||||
fn metadata(&self) -> crate::Metadata {
|
||||
self.0.metadata()
|
||||
}
|
||||
|
||||
/// Return the genesis hash.
|
||||
fn genesis_hash(&self) -> <T as Config>::Hash {
|
||||
self.0.genesis_hash()
|
||||
}
|
||||
|
||||
/// Return the runtime version.
|
||||
fn runtime_version(&self) -> crate::rpc::types::RuntimeVersion {
|
||||
self.0.runtime_version()
|
||||
}
|
||||
|
||||
/// Work with transactions.
|
||||
pub fn tx(&self) -> TxClient<T, Self> {
|
||||
<Self as OfflineClientT<T>>::tx(self)
|
||||
}
|
||||
|
||||
/// Work with events.
|
||||
pub fn events(&self) -> EventsClient<T, Self> {
|
||||
<Self as OfflineClientT<T>>::events(self)
|
||||
}
|
||||
|
||||
/// Work with storage.
|
||||
pub fn storage(&self) -> StorageClient<T, Self> {
|
||||
<Self as OfflineClientT<T>>::storage(self)
|
||||
}
|
||||
|
||||
/// Access constants.
|
||||
pub fn constants(&self) -> ConstantsClient<T, Self> {
|
||||
<Self as OfflineClientT<T>>::constants(self)
|
||||
}
|
||||
|
||||
/// Work with blocks.
|
||||
pub fn blocks(&self) -> BlocksClient<T, Self> {
|
||||
<Self as OfflineClientT<T>>::blocks(self)
|
||||
}
|
||||
|
||||
/// Work with runtime API.
|
||||
pub fn runtime_api(&self) -> RuntimeApiClient<T, Self> {
|
||||
<Self as OfflineClientT<T>>::runtime_api(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> OnlineClientT<T> for LightClient<T> {
|
||||
fn rpc(&self) -> &crate::rpc::Rpc<T> {
|
||||
self.0.rpc()
|
||||
@@ -57,14 +119,14 @@ impl<T: Config> OnlineClientT<T> for LightClient<T> {
|
||||
|
||||
impl<T: Config> OfflineClientT<T> for LightClient<T> {
|
||||
fn metadata(&self) -> crate::Metadata {
|
||||
self.0.metadata()
|
||||
self.metadata()
|
||||
}
|
||||
|
||||
fn genesis_hash(&self) -> <T as Config>::Hash {
|
||||
self.0.genesis_hash()
|
||||
self.genesis_hash()
|
||||
}
|
||||
|
||||
fn runtime_version(&self) -> crate::rpc::types::RuntimeVersion {
|
||||
self.0.runtime_version()
|
||||
self.runtime_version()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user