mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-22 03:18:05 +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:
@@ -6,3 +6,4 @@ cargo-timing*
|
|||||||
/examples/wasm-example/target
|
/examples/wasm-example/target
|
||||||
/examples/parachain-example/target
|
/examples/parachain-example/target
|
||||||
/examples/parachain-example/metadata/target
|
/examples/parachain-example/metadata/target
|
||||||
|
.vscode
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
use sp_keyring::AccountKeyring;
|
use subxt::{client::LightClient, PolkadotConfig};
|
||||||
use subxt::{
|
use subxt_signer::sr25519::dev;
|
||||||
client::{LightClient, LightClientBuilder, OfflineClientT},
|
|
||||||
tx::PairSigner,
|
|
||||||
PolkadotConfig,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generate an interface that we can use from the node's metadata.
|
// Generate an interface that we can use from the node's metadata.
|
||||||
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
|
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
|
||||||
@@ -11,6 +7,7 @@ pub mod polkadot {}
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
// The smoldot logs are informative:
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
// Create a light client by fetching the chain spec of a local running node.
|
// Create a light client by fetching the chain spec of a local running node.
|
||||||
@@ -20,7 +17,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// The `12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp` is the P2P address
|
// The `12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp` is the P2P address
|
||||||
// from a local polkadot node starting with
|
// from a local polkadot node starting with
|
||||||
// `--node-key 0000000000000000000000000000000000000000000000000000000000000001`
|
// `--node-key 0000000000000000000000000000000000000000000000000000000000000001`
|
||||||
let api: LightClient<PolkadotConfig> = LightClientBuilder::new()
|
let api = LightClient::<PolkadotConfig>::builder()
|
||||||
.bootnodes([
|
.bootnodes([
|
||||||
"/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp",
|
"/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp",
|
||||||
])
|
])
|
||||||
@@ -28,12 +25,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Build a balance transfer extrinsic.
|
// Build a balance transfer extrinsic.
|
||||||
let dest = AccountKeyring::Bob.to_account_id().into();
|
let dest = dev::bob().public_key().into();
|
||||||
let balance_transfer_tx = polkadot::tx().balances().transfer(dest, 10_000);
|
let balance_transfer_tx = polkadot::tx().balances().transfer(dest, 10_000);
|
||||||
|
|
||||||
// Submit the balance transfer extrinsic from Alice, and wait for it to be successful
|
// Submit the balance transfer extrinsic from Alice, and wait for it to be successful
|
||||||
// and in a finalized block. We get back the extrinsic events if all is well.
|
// and in a finalized block. We get back the extrinsic events if all is well.
|
||||||
let from = PairSigner::new(AccountKeyring::Alice.pair());
|
let from = dev::alice();
|
||||||
let events = api
|
let events = api
|
||||||
.tx()
|
.tx()
|
||||||
.sign_and_submit_then_watch_default(&balance_transfer_tx, &from)
|
.sign_and_submit_then_watch_default(&balance_transfer_tx, &from)
|
||||||
|
|||||||
@@ -12,14 +12,15 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
/// Builder for [`LightClient`].
|
/// Builder for [`LightClient`].
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct LightClientBuilder {
|
pub struct LightClientBuilder<T: Config> {
|
||||||
max_pending_requests: NonZeroU32,
|
max_pending_requests: NonZeroU32,
|
||||||
max_subscriptions: u32,
|
max_subscriptions: u32,
|
||||||
bootnodes: Option<Vec<serde_json::Value>>,
|
bootnodes: Option<Vec<serde_json::Value>>,
|
||||||
potential_relay_chains: Option<Vec<ChainId>>,
|
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 {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
max_pending_requests: NonZeroU32::new(128)
|
max_pending_requests: NonZeroU32::new(128)
|
||||||
@@ -27,13 +28,14 @@ impl Default for LightClientBuilder {
|
|||||||
max_subscriptions: 1024,
|
max_subscriptions: 1024,
|
||||||
bootnodes: None,
|
bootnodes: None,
|
||||||
potential_relay_chains: None,
|
potential_relay_chains: None,
|
||||||
|
_marker: std::marker::PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LightClientBuilder {
|
impl<T: Config> LightClientBuilder<T> {
|
||||||
/// Create a new [`LightClientBuilder`].
|
/// Create a new [`LightClientBuilder`].
|
||||||
pub fn new() -> LightClientBuilder {
|
pub fn new() -> LightClientBuilder<T> {
|
||||||
LightClientBuilder::default()
|
LightClientBuilder::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,10 +92,7 @@ impl LightClientBuilder {
|
|||||||
///
|
///
|
||||||
/// Panics if being called outside of `tokio` runtime context.
|
/// Panics if being called outside of `tokio` runtime context.
|
||||||
#[cfg(feature = "jsonrpsee")]
|
#[cfg(feature = "jsonrpsee")]
|
||||||
pub async fn build_from_url<T: Config, Url: AsRef<str>>(
|
pub async fn build_from_url<Url: AsRef<str>>(self, url: Url) -> Result<LightClient<T>, Error> {
|
||||||
self,
|
|
||||||
url: Url,
|
|
||||||
) -> Result<LightClient<T>, Error> {
|
|
||||||
let chain_spec = fetch_url(url.as_ref()).await?;
|
let chain_spec = fetch_url(url.as_ref()).await?;
|
||||||
|
|
||||||
self.build_client(chain_spec).await
|
self.build_client(chain_spec).await
|
||||||
@@ -120,7 +119,7 @@ impl LightClientBuilder {
|
|||||||
/// ## Panics
|
/// ## Panics
|
||||||
///
|
///
|
||||||
/// Panics if being called outside of `tokio` runtime context.
|
/// 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)
|
let chain_spec = serde_json::from_str(chain_spec)
|
||||||
.map_err(|_| Error::LightClient(LightClientError::InvalidChainSpec))?;
|
.map_err(|_| Error::LightClient(LightClientError::InvalidChainSpec))?;
|
||||||
|
|
||||||
@@ -128,7 +127,7 @@ impl LightClientBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Build the light client.
|
/// Build the light client.
|
||||||
async fn build_client<T: Config>(
|
async fn build_client(
|
||||||
self,
|
self,
|
||||||
mut chain_spec: serde_json::Value,
|
mut chain_spec: serde_json::Value,
|
||||||
) -> Result<LightClient<T>, Error> {
|
) -> Result<LightClient<T>, Error> {
|
||||||
|
|||||||
@@ -8,8 +8,14 @@ mod builder;
|
|||||||
mod rpc;
|
mod rpc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
blocks::BlocksClient,
|
||||||
client::{OfflineClientT, OnlineClientT},
|
client::{OfflineClientT, OnlineClientT},
|
||||||
config::Config,
|
config::Config,
|
||||||
|
constants::ConstantsClient,
|
||||||
|
events::EventsClient,
|
||||||
|
runtime_api::RuntimeApiClient,
|
||||||
|
storage::StorageClient,
|
||||||
|
tx::TxClient,
|
||||||
OnlineClient,
|
OnlineClient,
|
||||||
};
|
};
|
||||||
pub use builder::LightClientBuilder;
|
pub use builder::LightClientBuilder;
|
||||||
@@ -49,6 +55,62 @@ pub enum LightClientError {
|
|||||||
#[derivative(Clone(bound = ""))]
|
#[derivative(Clone(bound = ""))]
|
||||||
pub struct LightClient<T: Config>(OnlineClient<T>);
|
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> {
|
impl<T: Config> OnlineClientT<T> for LightClient<T> {
|
||||||
fn rpc(&self) -> &crate::rpc::Rpc<T> {
|
fn rpc(&self) -> &crate::rpc::Rpc<T> {
|
||||||
self.0.rpc()
|
self.0.rpc()
|
||||||
@@ -57,14 +119,14 @@ impl<T: Config> OnlineClientT<T> for LightClient<T> {
|
|||||||
|
|
||||||
impl<T: Config> OfflineClientT<T> for LightClient<T> {
|
impl<T: Config> OfflineClientT<T> for LightClient<T> {
|
||||||
fn metadata(&self) -> crate::Metadata {
|
fn metadata(&self) -> crate::Metadata {
|
||||||
self.0.metadata()
|
self.metadata()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn genesis_hash(&self) -> <T as Config>::Hash {
|
fn genesis_hash(&self) -> <T as Config>::Hash {
|
||||||
self.0.genesis_hash()
|
self.genesis_hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn runtime_version(&self) -> crate::rpc::types::RuntimeVersion {
|
fn runtime_version(&self) -> crate::rpc::types::RuntimeVersion {
|
||||||
self.0.runtime_version()
|
self.runtime_version()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user