examples: Add light client example

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2023-05-18 20:14:14 +03:00
parent 0dc47f2060
commit d59a33156f
3 changed files with 39 additions and 3 deletions
Generated
-2
View File
@@ -4336,8 +4336,6 @@ dependencies = [
"sp-keyring",
"subxt",
"tokio",
"tracing",
"tracing-subscriber 0.3.17",
]
[[package]]
+1 -1
View File
@@ -13,7 +13,7 @@ homepage.workspace = true
description = "Subxt example usage"
[dev-dependencies]
subxt = { workspace = true }
subxt = { workspace = true, default-features = false, features = ["default", "experimental-light-client"]}
tokio = { workspace = true }
futures = { workspace = true }
hex = { workspace = true }
@@ -0,0 +1,38 @@
use sp_keyring::AccountKeyring;
use subxt::rpc::LightClient;
use subxt::{tx::PairSigner, OnlineClient, PolkadotConfig};
use std::sync::Arc;
// Generate an interface that we can use from the node's metadata.
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
pub mod polkadot {}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a light client from the provided chain spec.
let light_client = LightClient::new(include_str!("../../artifacts/dev_spec.json"))?;
let api = OnlineClient::<PolkadotConfig>::from_rpc_client(Arc::new(light_client)).await?;
// Build a balance transfer extrinsic.
let dest = AccountKeyring::Bob.to_account_id().into();
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
// and in a finalized block. We get back the extrinsic events if all is well.
let from = PairSigner::new(AccountKeyring::Alice.pair());
let events = api
.tx()
.sign_and_submit_then_watch_default(&balance_transfer_tx, &from)
.await?
.wait_for_finalized_success()
.await?;
// Find a Transfer event and print it.
let transfer_event = events.find_first::<polkadot::balances::events::Transfer>()?;
if let Some(event) = transfer_event {
println!("Balance transfer success: {event:?}");
}
Ok(())
}