mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-20 11:41:02 +00:00
examples: Add light client example
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Generated
-2
@@ -4336,8 +4336,6 @@ dependencies = [
|
|||||||
"sp-keyring",
|
"sp-keyring",
|
||||||
"subxt",
|
"subxt",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
|
||||||
"tracing-subscriber 0.3.17",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ homepage.workspace = true
|
|||||||
description = "Subxt example usage"
|
description = "Subxt example usage"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
subxt = { workspace = true }
|
subxt = { workspace = true, default-features = false, features = ["default", "experimental-light-client"]}
|
||||||
tokio = { workspace = true }
|
tokio = { workspace = true }
|
||||||
futures = { workspace = true }
|
futures = { workspace = true }
|
||||||
hex = { 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(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user