1- # subxt · ; [ ![ build] ( https://github.com/paritytech/subxt/actions/workflows/rust.yml/badge.svg )] ( https://github.com/paritytech/subxt/actions/workflows/rust.yml ) [ ![ Latest Version] ( https://img.shields.io/crates/v/subxt.svg )] ( https://crates.io/crates/subxt ) [ ![ Documentation] ( https://docs.rs/subxt/badge.svg )] ( https://docs.rs/subxt )
1+ <p align =" center " >
2+ <img src =" ./logo.svg " alt =" subxt logo " width =" 420 " />
3+ </p >
24
3- Subxt is a library for interacting with [ Substrate] ( https://github.com/paritytech/polkadot-sdk ) based nodes in Rust and WebAssembly. It can:
5+ <p align =" center " >
6+ <a href =" https://github.com/paritytech/subxt/actions/workflows/rust.yml " ><img src =" https://github.com/paritytech/subxt/actions/workflows/rust.yml/badge.svg " alt =" build " ></a >
7+ <a href =" https://crates.io/crates/subxt " ><img src =" https://img.shields.io/crates/v/subxt.svg " alt =" Latest Version " ></a >
8+ <a href =" https://docs.rs/subxt " ><img src =" https://docs.rs/subxt/badge.svg " alt =" Documentation " ></a >
9+ </p >
10+
11+ Subxt is a library for interacting with chains in the [ Polkadot] ( https://github.com/paritytech/polkadot-sdk ) network. It can:
412
513- Submit Extrinsics (this is where the name comes from).
6- - Subscribe to blocks, reading the extrinsics and associated events from them.
7- - Read and iterate over storage values.
8- - Read constants and custom values from the metadata.
9- - Call runtime APIs, returning the results.
10- - Do all of the above via a safe, statically typed interface or via a dynamic one when you need the flexibility.
11- - Compile to WASM and run entirely in the browser.
12- - Do a bunch of things in a ` #[no_std] ` environment via the ` subxt-core ` crate.
13- - Use a built-in light client (` smoldot ` ) to interact with chains.
14+ - Access information at any block (eg storage values, constants, Runtime APIs, View Functions).
15+ - Subscribe to new blocks (and then do the above at them).
16+ - Do all of the above via a safe, statically typed interface or via a flexible dynamic interface.
17+ - Do most of the above via a built-in light client to interact with chains trustlessly.
18+ - Compile to WASM and run [ entirely in the browser] ( ./examples/wasm-example ) , or be [ called via FFI] ( ./examples/ffi-example ) in many other languages.
1419
1520## Usage
1621
17- Take a look in the [ examples] ( ./subxt/examples ) folder or the [ examples] ( ./examples ) folder for various smaller or
18- larger ` subxt ` usage examples, or [ read the guide] ( https://docs.rs/subxt/latest/subxt/book/index.html ) to learn more.
19-
20- ### Downloading metadata from a Substrate node
21-
22- Use the [ ` subxt-cli ` ] ( ./cli ) tool to download the metadata for your target runtime from a node.
23-
24- 1 . Install:
25-
26- ``` bash
27- cargo install subxt-cli
28- ```
29-
30- 2 . Save the encoded metadata to a file:
31-
32- ``` bash
33- subxt metadata -f bytes > metadata.scale
34- ```
35-
36- This defaults to querying the metadata of a locally running node on the default ` http://localhost:9933/ ` . If querying
37- a different node then the ` metadata ` command accepts a ` --url ` argument.
38-
39- ## Subxt Documentation
40-
41- For more details regarding utilizing subxt, please visit the [ documentation] ( https://docs.rs/subxt/latest/subxt/ ) .
42-
43- ## Integration Testing
44-
45- Most tests require a running substrate node to communicate with. This is done by spawning an instance of the
46- substrate node per test. It requires an up-to-date ` substrate ` executable on your path.
47-
48- This can be installed from source via cargo:
49-
50- ``` bash
51- cargo install --git https://github.com/paritytech/polkadot-sdk staging-node-cli --force
22+ Take a look at the [ single-file examples] ( ./subxt/examples ) folder or the [ project based examples] ( ./examples ) folder for various smaller or
23+ larger ` subxt ` usage examples, or [ read the docs] ( https://docs.rs/subxt/latest/subxt ) to learn more.
24+
25+ ## Example
26+
27+ The "hello world" example of Subxt is submitting a transaction. This is what it looks like:
28+
29+ ``` rust
30+ use subxt :: {Error , OnlineClient , PolkadotConfig };
31+ use subxt_signer :: sr25519 :: dev;
32+
33+ // Generate an interface that we can use from the node's metadata.
34+ #[subxt:: subxt(runtime_metadata_path = " /path/to/polkadot_rc_metadata.scale" )]
35+ mod polkadot {}
36+
37+ #[tokio:: main]
38+ async fn main () -> Result <(), Error > {
39+ // Create a new API client, configured to talk to Polkadot nodes.
40+ let api = OnlineClient :: <PolkadotConfig >:: new (). await ? ;
41+
42+ // Almost all actions are performed at an explicit block. Here we use
43+ // the current block at the time of running this.
44+ let at_block = api . at_current_block (). await ? ;
45+
46+ // Build a balance transfer extrinsic.
47+ let dest = dev :: bob (). public_key (). into ();
48+ let balance_transfer_tx = polkadot :: transactions ()
49+ . balances ()
50+ . transfer_allow_death (dest , 10_000 );
51+
52+ // Submit the balance transfer extrinsic from Alice, and wait for it
53+ // to be successful and in a finalized block. We get back the extrinsic
54+ // events if all is well.
55+ let from = dev :: alice ();
56+ let events = at_block
57+ . transactions ()
58+ . sign_and_submit_then_watch_default (& balance_transfer_tx , & from )
59+ . await ?
60+ . wait_for_finalized_success ()
61+ . await ? ;
62+
63+ // (Optional) we can look for a specific event to learn more about
64+ // the submission.
65+ if let Some (event ) = events . find_first :: <polkadot :: balances :: events :: Transfer >() {
66+ println! (" Balance transfer success: {event:?}" );
67+ }
68+
69+ Ok (())
70+ }
5271```
5372
5473## Real world usage
@@ -68,9 +87,9 @@ Please add your project to this list via a PR.
6887- [ Hyperbridge] ( https://github.com/polytope-labs/hyperbridge ) A hyperscalable coprocessor for verifiable cross-chain interoperability.
6988- [ pop CLI] ( https://github.com/r0gue-io/pop-cli ) The all-in-one tool for Polkadot development.
7089
71- ** Alternatives**
90+ ## Alternatives
7291
73- [ substrate -api-client ] ( https://github.com/scs/substrate -api-client ) provides similar functionality .
92+ If you're working in TypeScript / JavaScript, [ polkadot -api] ( https://github.com/polkadot -api/polkadot-api ) is an excellent and actively developed alternative .
7493
7594#### License
7695
0 commit comments