|
| 1 | +--- |
| 2 | +title: Subxt Rust API |
| 3 | +description: Subxt is a Rust library for type-safe interaction with Polkadot SDK blockchains, enabling transactions, state queries, runtime API access, and more. |
| 4 | +--- |
| 5 | + |
| 6 | +# Subxt Rust API |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +subxt is a Rust library designed to interact with Polkadot SDK-based blockchains. It provides a type-safe interface for submitting transactions, querying on-chain state, and performing other blockchain interactions. By leveraging Rust's strong type system, subxt ensures that your code is validated at compile time, reducing runtime errors and improving reliability. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +Before using subxt, ensure you have the following requirements: |
| 15 | + |
| 16 | +- Rust and Cargo installed on your system. You can install them using [Rustup](https://rustup.rs/){target=\_blank} |
| 17 | +- A Rust project initialized. If you don't have one, create it with: |
| 18 | + ```bash |
| 19 | + cargo new my_project && cd my_project |
| 20 | + ``` |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +To use subxt in your project, you must install the necessary dependencies. Each plays a specific role in enabling interaction with the blockchain: |
| 25 | + |
| 26 | +1. **Install the subxt CLI** - [`subxt-cli`](https://crates.io/crates/subxt-cli){target=\_blank} is a command-line tool that provides utilities for working with Polkadot SDK metadata. In the context of subxt, it is essential to download chain metadata, which is required to generate type-safe Rust interfaces for interacting with the blockchain. Install it using: |
| 27 | + |
| 28 | + ```bash |
| 29 | + cargo install subxt-cli |
| 30 | + ``` |
| 31 | + |
| 32 | +2. **Add core dependencies** - these dependencies are essential for interacting with the blockchain: |
| 33 | + |
| 34 | + - **[subxt](https://crates.io/crates/subxt){target=\_blank}** - the main library for communicating with Polkadot SDK nodes. It handles RPC requests, encoding/decoding, and type generation |
| 35 | + |
| 36 | + ```bash |
| 37 | + cargo add subxt |
| 38 | + ``` |
| 39 | + |
| 40 | + - **[subxt-signer](https://crates.io/crates/subxt-signer){target=\_blank}** - provides cryptographic functionality for signing transactions. Without this, you can only read data but cannot submit transactions |
| 41 | + |
| 42 | + ```bash |
| 43 | + cargo add subxt-signer |
| 44 | + ``` |
| 45 | + |
| 46 | + - **[tokio](https://crates.io/crates/tokio){target=\_blank}** - an asynchronous runtime for Rust. Since blockchain operations are async, Tokio enables the efficient handling of network requests. The `rt` feature enables Tokio's runtime, including the current-thread single-threaded scheduler, which is necessary for async execution. The `macros` feature provides procedural macros like `#[tokio::main]` to simplify runtime setup |
| 47 | +
|
| 48 | + ```bash |
| 49 | + cargo add tokio --features rt,macros |
| 50 | + ``` |
| 51 | +
|
| 52 | + After adding the dependencies, your `Cargo.toml` should look like this: |
| 53 | +
|
| 54 | + ```toml |
| 55 | + --8<-- 'code/develop/toolkit/api-libraries/subxt/Cargo.toml' |
| 56 | + ``` |
| 57 | +
|
| 58 | +## Get Started |
| 59 | +
|
| 60 | +This guide will walk you through the fundamental operations of subxt, from setting up your environment to executing transactions and querying blockchain state. |
| 61 | +
|
| 62 | +### Download Chain Metadata |
| 63 | +
|
| 64 | +Before interacting with a blockchain, you need to retrieve its metadata. This metadata defines storage structures, extrinsics, and other runtime details. Use the `subxt-cli` tool to download the metadata, replacing `INSERT_NODE_URL` with the URL of the node you want to interact with: |
| 65 | +
|
| 66 | +```bash |
| 67 | +subxt metadata --url INSERT_NODE_URL > polkadot_metadata.scale |
| 68 | +``` |
| 69 | +
|
| 70 | +### Generate Type-Safe Interfaces |
| 71 | +
|
| 72 | +Use the `#[subxt::subxt]` macro to generate a type-safe Rust interface from the downloaded metadata: |
| 73 | +
|
| 74 | +```rust |
| 75 | +--8<-- 'code/develop/toolkit/api-libraries/subxt/subxt.rs:6:8' |
| 76 | +``` |
| 77 | +
|
| 78 | +Once subxt interfaces are generated, you can interact with your node in the following ways. You can use the links below to view the related subxt documentation: |
| 79 | +
|
| 80 | +- **[Transactions](https://docs.rs/subxt/latest/subxt/book/usage/transactions/index.html){target=\_blank}** - builds and submits transactions, monitors their inclusion in blocks, and retrieves associated events |
| 81 | +- **[Storage](https://docs.rs/subxt/latest/subxt/book/usage/storage/index.html){target=\_blank}** - enables querying of node storage data |
| 82 | +- **[Events](https://docs.rs/subxt/latest/subxt/book/usage/events/index.html){target=\_blank}** - retrieves events emitted from recent blocks |
| 83 | +- **[Constants](https://docs.rs/subxt/latest/subxt/book/usage/constants/index.html){target=\_blank}** - accesses constant values stored in nodes that remain unchanged across a specific runtime version. |
| 84 | +- **[Blocks](https://docs.rs/subxt/latest/subxt/book/usage/blocks/index.html){target=\_blank}** - loads recent blocks or subscribes to new/finalized blocks, allowing examination of extrinsics, events, and storage at those blocks |
| 85 | +- **[Runtime APIs](https://docs.rs/subxt/latest/subxt/book/usage/runtime_apis/index.html){target=\_blank}** - makes calls into pallet runtime APIs to fetch data |
| 86 | +- **[Custom values](https://docs.rs/subxt/latest/subxt/book/usage/custom_values/index.html){target=\_blank}** - accesses "custom values" contained within metadata |
| 87 | +- **[Raw RPC calls](https://docs.rs/subxt/latest/subxt/book/usage/rpc/index.html){target=\_blank}** - facilitates raw RPC requests to compatible nodes |
| 88 | +
|
| 89 | +### Initialize the Subxt client |
| 90 | +
|
| 91 | +To interact with a blockchain node using subxt, create an asynchronous main function and initialize the client. Replace `INSERT_NODE_URL` with the URL of your target node: |
| 92 | +
|
| 93 | +```rust |
| 94 | +--8<-- 'code/develop/toolkit/api-libraries/subxt/subxt.rs::17' |
| 95 | + // Your code here... |
| 96 | +--8<-- 'code/develop/toolkit/api-libraries/subxt/subxt.rs:73:75' |
| 97 | +``` |
| 98 | +
|
| 99 | +### Read Chain Data |
| 100 | +
|
| 101 | +subxt provides multiple ways to access on-chain data: |
| 102 | +
|
| 103 | +- **Constants** - constants are predefined values in the runtime that remain unchanged unless modified by a runtime upgrade |
| 104 | +
|
| 105 | + For example, to retrieve the existential deposit, use: |
| 106 | + |
| 107 | + ```rust |
| 108 | + --8<-- 'code/develop/toolkit/api-libraries/subxt/subxt.rs:18:24' |
| 109 | + ``` |
| 110 | +
|
| 111 | +- **State** - state refers to the current chain data, which updates with each block |
| 112 | +
|
| 113 | + To fetch account information, replace `INSERT_ADDRESS` with the address you want to fetch data from and use: |
| 114 | +
|
| 115 | + ```rust |
| 116 | + --8<-- 'code/develop/toolkit/api-libraries/subxt/subxt.rs:26:42' |
| 117 | + ``` |
| 118 | +
|
| 119 | +### Submit Transactions |
| 120 | +
|
| 121 | +To submit a transaction, you must construct an extrinsic, sign it with your private key, and send it to the blockchain. Replace `INSERT_DEST_ADDRESS` with the recipient's address, `INSERT_AMOUNT` with the amount to transfer, and `INSERT_SECRET_PHRASE` with the sender's mnemonic phrase: |
| 122 | +
|
| 123 | +```rust |
| 124 | +--8<-- 'code/develop/toolkit/api-libraries/subxt/subxt.rs:44:72' |
| 125 | +``` |
| 126 | +
|
| 127 | +## Where to Go Next |
| 128 | +
|
| 129 | +Now that you've covered the basics dive into the official [subxt documentation](https://docs.rs/subxt/latest/subxt/book/index.html){target=\_blank} for comprehensive reference materials and advanced features. |
0 commit comments