diff --git a/examples/examples/storage_iterating.rs b/examples/examples/storage_iterating.rs index 7b7864cf7c..07704d1d01 100644 --- a/examples/examples/storage_iterating.rs +++ b/examples/examples/storage_iterating.rs @@ -12,7 +12,7 @@ async fn main() -> Result<(), Box> { let storage_query = polkadot::storage().system().account_root(); // Get back an iterator of results (here, we are fetching 10 items at - // a time from the node, but we always iterate over oen at a time). + // a time from the node, but we always iterate over one at a time). let mut results = api .storage() .at_latest() diff --git a/examples/examples/unstable_light_client_tx_basic.rs b/examples/examples/unstable_light_client_tx_basic.rs new file mode 100644 index 0000000000..16f8f231ba --- /dev/null +++ b/examples/examples/unstable_light_client_tx_basic.rs @@ -0,0 +1,70 @@ +use sp_keyring::AccountKeyring; +use std::sync::Arc; +use subxt::{rpc::LightClient, tx::PairSigner, OnlineClient, PolkadotConfig}; + +// 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> { + // Create a light client from the provided chain spec. + // + // # Note + // + // This example uses a local running polkadot node and the + // provided spec might differ depending on the version used. + // + // The spec can be generated in the following manner: + // - start the polkadot node: + // + // `./polkadot --dev --node-key 0000000000000000000000000000000000000000000000000000000000000001 --alice --validator` + // + // - fetch the spec + // + // ```bash + // curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "sync_state_genSyncSpec", "params":[true]}' http://localhost:9944/ | jq .result > res.spec + // ``` + // + // - remove the `lightSyncState` entry from the spec + // + // - add the boot nodes entry to the spec + // + // ```json + // "bootNodes": [ + // "/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp" + // ], + // + // Where `12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp` should be replaced with the + // polkadot nodes identity extracted from the process's logs: + // + // ```bash + // 👴 Loading GRANDPA authority set from genesis on what appears to be first startup. + // 👶 Creating empty BABE epoch changes on what appears to be first startup. + // 🏷 Local node identity is: 12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp + // ``` + let light_client = LightClient::new(include_str!("../../artifacts/dev_spec.json"))?; + let api = OnlineClient::::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::()?; + if let Some(event) = transfer_event { + println!("Balance transfer success: {event:?}"); + } + + Ok(()) +}