This project demonstrates how to interact with a Polkadot SDK-based blockchain (like Paseo) using Rust and the subxt library.
- Connects to a blockchain node using a WebSocket endpoint.
- Fetches account information for a given address.
- Sends a "remark" transaction (a simple on-chain message).
- Watches for events to confirm that the transaction was successful.
SubXT is a Rust client for Polkadot SDK-based chains that generates a type-safe API from a chain’s runtime metadata and then uses it to read storage, submit extrinsics, and decode events.
-
Fetch metadata with the
subxt-cli:subxt metadata -f bytes --url <WS> -o metadata.scale
Installing the
subxt-cliwill greatly aid in fetching metadata from nodes -cargo install subxt-cli
-
Generate types dynamically using the macro:
use #[subxt::subxt(runtime_metadata_path = "metadata.scale")]
or, you can also generate the types for pallets/calls/events/storage/consts statically using
subxt-cli:subxt codegen --url <WS> > src/chain.rs
-
Init client:
let api = OnlineClient::<PolkadotConfig>::from_url(<WS>).await?;
-
Prepare a signer (you will need
subxt-signer):let uri = SecretUri::from_str(mnemonic_str).expect("valid mnemonic"); let signer = Keypair::from_uri(&uri).expect("valid keypair")
From here, you can query storage, send transactions, and watch events with ease.
main.rs: Example entry point showing how to fetch account info and send a remark.remark.rs: Utility functions for interacting with the blockchain.config.rs: Configuration and type definitions, including loading the types from the metadata located inartifacts/
-
Ensure you have Rust installed.
-
Clone this repository and enter the directory.
-
Ensure you have the correct metadata:
The metadata for the Paseo testnet (
artifacts/paseo.scale) is already included, however if you wish to use a different network, you will need to use thesubxt-cli. -
Build and run:
cargo run
This will:
- Fetch Alice's account info.
- Send a remark transaction.
- Print the result and any events.
Account info for ALICE: Ok(AccountInfo { ... })
Remark success: Some(Remarked { ... })
- To use a different account, change the address or mnemonic in the code.
- To send a different message, change the string passed to the
remarkfunction.
Because of subxt's native support for Wasm, there are a number of Rust-based UI/web frameworks you can use:
- Yew - A framework for creating reliable and efficient web applications.
- Iced - A cross-platform GUI library for Rust focused on simplicity and type-safety.
This project is for educational purposes and works with public test networks like Paseo.