Blockchain development is like building a complex network building, and each function and each transaction requires a safe and stable support structure to ensure the efficient operation of the system. Subxt was born for such a demand - it is like a "bridge" in the hands of developers, designed for efficient interaction with the Substrate blockchain in the Rust ecosystem. With Subxt, developers can easily call on-chain data, execute transactions, and even obtain key dynamics in real-time events on the chain as if they were opening a toolbox.
This article is a collection of seven technical articles on the Substrate Advanced Course written by PaperMoon teacher Kaichao, aiming to help developers gain an in-depth understanding of how to use Subxt to achieve seamless integration with Substrate. From submitting external transactions to reading and monitoring on-chain data, Subxt simplifies operations for developers and enhances system security. This article will lead you to explore the core functions and usage of Subxt in depth, providing solid technical support for your on-chain development.
Subxt, the core tool for connecting to Substrate
Subxt is a Rust library designed to provide an efficient interface to the Substrate blockchain. It allows developers to interact with the blockchain in a safe, type-safe way, simplifying the process of executing transactions, querying status, and subscribing to events. Subxt is designed with an emphasis on performance and security, leveraging Rust's static type system to prevent runtime errors. This allows developers to reduce potential errors and security risks when building decentralized applications.
Functional Overview: Key Concepts and Basic Modules
Before introducing subxt related functions, you should be familiar with the following key concepts:
Extrinsics : refers to transactions or commands submitted to the chain. Subxt allows users to create and submit these extrinsics.
Storage : The state of Substrate is stored in the form of key-value pairs, and Subxt can read and query these stored values.
Metadata : Contains information about the blockchain runtime, defining all available functions and data structures on the chain.
Events : Notifications when blockchain operations occur. Subxt can subscribe to these events to obtain the dynamically changing status of the chain.
Subxt provides a rich interface for interacting with on-chain data and transactions, including:
Submit extrinsics (hence the library name).
Subscribe to blocks to read external transactions and related events.
Read and iterate over stored values.
Read constants and custom values from metadata.
Calls the runtime API and returns the result.
Do all of the above through safe, statically typed interfaces, or through dynamic interfaces when flexibility is needed.
WASM support: Subxt can be compiled to WASM and run in the browser, supporting Rust-based browser applications and can even be bound to JS applications.
The subxt-core crate performs several operations in a #[no_std] environment.
Use the built-in light client (smoldot) to interact with the blockchain.
Practical Guide: Detailed Explanation of Subxt Usage Examples
Download metadata
Use the subxt-cli
tool to download the target runtime's metadata from the node.
Install:
Save the encoded metadata to a file:
By default, this command queries the metadata of the local node running at http://localhost:9933/. If you want to query a different node, you can specify the node address using the --url
parameter.
Adding Dependencies
Add the following content to the cargo.toml file of the Rust project:
Get on-chain storage
In the Substrate blockchain, storage is used to persist on-chain state data. The process of obtaining on-chain storage usually involves the following steps:
Establishing the connection : First, we need to create an API client to connect to the target blockchain node. This is done through the OnlineClient, which automatically handles the connection and communication with the node.
Define storage query : Next, we need to define the storage item to query. In Substrate, storage data is stored in the form of key-value pairs, and we can access specific data through these keys. For example, if we want to get the balance information of an account, we need to build a storage query for that account.
Execute a query : Send a request to retrieve stored data by calling the fetch method of the API client.
Processing results : After successfully obtaining the data, we can extract the required fields, such as balance, and perform subsequent processing or display.
Sending transactions
In the Substrate blockchain, sending transactions (or external transactions) is one of the core operations for interacting with the blockchain. This process usually includes the following steps:
Establish connection: and query storage type, first establish API client connection with the chain node to ensure that transactions can be sent.
Construct a transaction: Use the interface provided by Subxt to define the specific content of the transaction. In this process, you need to specify the transaction type, such as balance transfer, and set the target address and transfer amount of the transaction.
Signature transaction: Before submitting a transaction, it needs to be signed to prove that the initiator of the transaction has the relevant authority. This is usually signed by a private key to ensure the legitimacy of the transaction.
Submit transaction: Use the API client to submit the signed transaction to the blockchain. After submission, the transaction will be added to the pending queue and confirmed in the next block.
Monitoring results: You can monitor transaction events to obtain the status of the transaction and confirm whether it is successful. If successful, you can perform subsequent processing based on the returned event information.
Community activity and future development, Subxt's ecological support and innovation prospects
With high flexibility and security, subxt can be used in a variety of application scenarios, including:
Decentralized Applications (DApps): Subxt provides a simple API interface when building front-end applications that interact with the blockchain.
Automation scripts: Developers can use Subxt to write scripts to automate the management of on-chain assets or perform periodic tasks.
On-chain analytics tools: Use Subxt to read blockchain data to analyze on-chain activity and user behavior.
Subxt has an active development campaign with constant updates and feature expansions. Developers can access rich documentation and examples, participate in community discussions, report issues, and contribute code. As the Substrate ecosystem continues to grow, Subxt will continue to evolve, including more performance optimizations, support for new features, and more cross-chain interaction capabilities. This makes it an important tool for developers to continue to innovate.
Sign up for the 16th Substrate introductory course
OneBlock+ and Polkadot jointly launched the 16th Substrate Blockchain Development Introductory Course and registration is now open! This course is designed for developers and explains in depth how to quickly build a blockchain with customized functions within 15 minutes, helping you to efficiently integrate into the Polkadot ecosystem. Through 6 core classes, you will master the theoretical knowledge and basic concepts of blockchain and be proficient in using Substrate technology. In addition, the course also provides weekly live Q&A and after-class exercises to help consolidate knowledge and solve learning difficulties. After completing the course, you will have the opportunity to receive referrals for multiple high-quality development positions, opening up new directions for your career development!
📪 Countdown 20 days, sign up for the course now:
https://wj.qq.com/s2/14957929/h3bh/