mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 00:31:07 +00:00
blocks: Fetch chainHead call result
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
@@ -10,13 +10,14 @@
|
|||||||
//! polkadot --dev --tmp
|
//! polkadot --dev --tmp
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
use codec::Encode;
|
||||||
|
use futures::StreamExt;
|
||||||
|
use sp_keyring::AccountKeyring;
|
||||||
use subxt::{
|
use subxt::{
|
||||||
OnlineClient,
|
OnlineClient,
|
||||||
PolkadotConfig,
|
PolkadotConfig,
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures::StreamExt;
|
|
||||||
|
|
||||||
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")]
|
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")]
|
||||||
pub mod polkadot {}
|
pub mod polkadot {}
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// Create a client to use:
|
// Create a client to use:
|
||||||
let api = OnlineClient::<PolkadotConfig>::new().await?;
|
let api = OnlineClient::<PolkadotConfig>::new().await?;
|
||||||
|
|
||||||
let mut follow_sub = api.blocks().subscribe_chainhead_finalized(false).await?;
|
let mut follow_sub = api.blocks().subscribe_chainhead_finalized(true).await?;
|
||||||
// Handle all subscriptions from the `chainHead_follow`.
|
// Handle all subscriptions from the `chainHead_follow`.
|
||||||
while let Some(block) = follow_sub.next().await {
|
while let Some(block) = follow_sub.next().await {
|
||||||
let block = block?;
|
let block = block?;
|
||||||
@@ -46,6 +47,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
era.index,
|
era.index,
|
||||||
era.start
|
era.start
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let call_params = AccountKeyring::Alice.to_account_id().encode();
|
||||||
|
let call = block
|
||||||
|
.call("AccountNonceApi_account_nonce".into(), &call_params)
|
||||||
|
.await?;
|
||||||
|
println!("[hash={:?}] call={:?}", block.hash(), call);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe to the `chainHead_follow` method.
|
// Subscribe to the `chainHead_follow` method.
|
||||||
|
|||||||
@@ -174,6 +174,41 @@ where
|
|||||||
|
|
||||||
Err(Error::Other("Failed to fetch the block body".into()))
|
Err(Error::Other("Failed to fetch the block body".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetch the body (vector of extrinsics) of this block.
|
||||||
|
pub async fn call(
|
||||||
|
&self,
|
||||||
|
function: String,
|
||||||
|
call_parameters: &[u8],
|
||||||
|
) -> Result<Vec<u8>, Error> {
|
||||||
|
let mut sub = self
|
||||||
|
.client
|
||||||
|
.rpc()
|
||||||
|
.subscribe_chainhead_call(
|
||||||
|
self.subscription_id.clone(),
|
||||||
|
self.hash,
|
||||||
|
function,
|
||||||
|
call_parameters,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if let Some(event) = sub.next().await {
|
||||||
|
let event = event?;
|
||||||
|
|
||||||
|
println!("Got event: {:?}", event);
|
||||||
|
|
||||||
|
return match event {
|
||||||
|
ChainHeadEvent::Done(ChainHeadResult { result }) => {
|
||||||
|
let bytes = hex::decode(result.trim_start_matches("0x"))
|
||||||
|
.map_err(|err| Error::Other(err.to_string()))?;
|
||||||
|
Ok(bytes)
|
||||||
|
}
|
||||||
|
_ => Err(Error::Other("Failed to fetch the block body".into())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(Error::Other("Failed to fetch the block body".into()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A representation of a block.
|
/// A representation of a block.
|
||||||
|
|||||||
@@ -681,6 +681,27 @@ impl<T: Config> Rpc<T> {
|
|||||||
|
|
||||||
Ok(subscription)
|
Ok(subscription)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Subscribe to the chain head call.
|
||||||
|
pub async fn subscribe_chainhead_call(
|
||||||
|
&self,
|
||||||
|
subscription_id: String,
|
||||||
|
hash: T::Hash,
|
||||||
|
function: String,
|
||||||
|
call_parameters: &[u8],
|
||||||
|
) -> Result<Subscription<ChainHeadEvent<String>>, Error> {
|
||||||
|
let subscription = self
|
||||||
|
.client
|
||||||
|
.subscribe(
|
||||||
|
"chainHead_unstable_call",
|
||||||
|
rpc_params![subscription_id, hash, function, to_hex(call_parameters)],
|
||||||
|
"chainHead_unstable_stopCall",
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(subscription)
|
||||||
|
}
|
||||||
|
|
||||||
/// Subscribe to finalized block headers.
|
/// Subscribe to finalized block headers.
|
||||||
///
|
///
|
||||||
/// Note: this may not produce _every_ block in the finalized chain;
|
/// Note: this may not produce _every_ block in the finalized chain;
|
||||||
|
|||||||
Reference in New Issue
Block a user