Skip to content

CrackTheCode016/subxt-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Subxt Starter Example

This project demonstrates how to interact with a Polkadot SDK-based blockchain (like Paseo) using Rust and the subxt library.

What does this code do?

  • 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: How it works

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.

  1. Fetch metadata with the subxt-cli:

    subxt metadata -f bytes --url <WS> -o metadata.scale

Installing the subxt-cli will greatly aid in fetching metadata from nodes - cargo install subxt-cli

  1. 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
  2. Init client:

    let api = OnlineClient::<PolkadotConfig>::from_url(<WS>).await?;
  3. 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.

Repository Structure

  • 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 in artifacts/

Installation & Running

  1. Ensure you have Rust installed.

  2. Clone this repository and enter the directory.

  3. 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 the subxt-cli.

  4. Build and run:

    cargo run

    This will:

    • Fetch Alice's account info.
    • Send a remark transaction.
    • Print the result and any events.

Example Output

Account info for ALICE: Ok(AccountInfo { ... })
Remark success: Some(Remarked { ... })

Customizing

  • To use a different account, change the address or mnemonic in the code.
  • To send a different message, change the string passed to the remark function.

Moving Forward: Adding a UI

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.

More Information


This project is for educational purposes and works with public test networks like Paseo.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages