• Stars
    star
    260
  • Rank 157,189 (Top 4 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Library for connecting to substrate API over WebSockets

substrate-api-client

substrate-api-client is a library written in Rust for connecting to the substrate's RPC interface via WebSockets allowing to

  • Compose extrinsics, send them (asynchronously and synchronously) and subscribe to updates (synchronously).
  • Support no_std builds. Only the rpc-client is std only. For no_std builds, a custom rpc client needs to be implemented.
  • Watch events and execute code upon events.
  • Parse and print the node metadata.
  • Support three different websocket crates (jsonrpsee, tungstenite and ws). See Cargo.toml for more information and limitations.

Prerequisites

In order to build the substrate-api-client and the examples, Rust and the wasm target are needed. For Linux:

curl https://sh.rustup.rs -sSf | sh
# Install the rust toolchain specified in rust-toolchain.toml
rustup show

Substrate node

To execute the examples, a running substrate node is needed. You can download a node artifact from substrate directly: https://github.com/paritytech/substrate or run the kitchensink-node with docker:

docker run -p 9944:9944 -p 9933:9933 -p 30333:30333 parity/substrate:latest --dev --ws-external --rpc-external

For more information, please refer to the substrate repository.

Examples

To run an example, clone the substrate-api-client repository and run the desired example directly with the cargo command:

git clone https://github.com/scs/substrate-api-client.git
cd substrate-api-client
cargo run -p ac-examples --example get_storage

or download the already built binaries from GitHub Actions and run them without any previous building:

# Add execution rights to the chosen example.
chmod +x <example>
# And run it.
./<example>

Set the output verbosity by prepending RUST_LOG=info or RUST_LOG=debug.

The following examples can be found in the examples folder:

no_std build

Almost everything in the api-client, except for the rpc-clients and a few additional features, is no_std compatible. Many helpful features, such as extrinsic and call creation (see the macros), metadata and event types (see the node-api and primitives) are available in no_std right away. However, to directly connect to a Substrate node a RPC client is necessary. Because websocket connection features are often hardware dependent, a generic no_std RPC client implementation is hardly possible. So for most use cases a self-implemented RPC client is required. To make this as simple as possible, the interface between the Api, which provides all the features, and the RPC client, providing the node connection, is kept very basic. Check out the following explanations for more info.

Import

To import the api-client in no_std make sure the default features are turned off and disable_target_static_assertions is enabled:

# In the Cargo.toml import the api-client as following:
substrate-api-client = { git = "https://github.com/scs/substrate-api-client.git", default-features = false, features = ["disable_target_static_assertions"] }

RPC Client

Depending on the usage, there are two traits that the RPC Client needs to implement.

Request

For simple requests (send one request and receive one answer) the trait Request is required:

/// Trait to be implemented by the ws-client for sending rpc requests and extrinsic.
pub trait Request {
	/// Sends a RPC request to the substrate node and returns the answer as string.
	fn request<R: DeserializeOwned>(&self, method: &str, params: RpcParams) -> Result<R>;
}

By implementing this trait with a custom RPC client, most basic functionalities of the Api can already be used. Currently, there is no no_std example available. But the tungstenite_client provides a relatively simple std example. If a websocket library is available in your no_std environment, then your implementation may look similar.

Subscription

A little more complex is the second trait Subscribe, which does not only send a subscription request to the node, it also keeps listening and updating accordingly. Two traits need to be implemented for this feature. The Subscribe trait itself:

/// Trait to be implemented by the ws-client for subscribing to the substrate node.
pub trait Subscribe {
	type Subscription<Notification>: HandleSubscription<Notification>
	where
		Notification: DeserializeOwned;

	fn subscribe<Notification: DeserializeOwned>(
		&self,
		sub: &str,
		params: RpcParams,
		unsub: &str,
	) -> Result<Self::Subscription<Notification>>;
}

and the HandleSubscription trait, which is returned by the subscribe function:

/// Trait to use the full functionality of jsonrpseee Subscription type
/// without actually enforcing it.
pub trait HandleSubscription<Notification: DeserializeOwned> {
	/// Returns the next notification from the stream.
	/// This may return `None` if the subscription has been terminated,
	/// which may happen if the channel becomes full or is dropped.
	fn next(&mut self) -> Option<Result<Notification>>;

	/// Unsubscribe and consume the subscription.
	fn unsubscribe(self) -> Result<()>;
}

Refering to the std example of the tungstenite, the HandleSubscription impl can be looked up here. It implements a simple channel receiver, waiting for the sender of the websocket client to send something. The Subscribe implementation can be found here.

A more complex RPC client, but also with more functionalities, is the jsonrpsee client.

Example Upgrades from older to newer versions

There have been some breaking API changes as of late to catch up with the newer Substrate versions and to fully support different Substrate nodes. An example project on how to upgrade from older tags can be found in the Integritee worker repository:

If you still experience issues during upgrading, do not hesitate to create an issue for support.

Alternatives

Parity offers a Rust client with similar functionality: https://github.com/paritytech/subxt

Acknowledgements

The development of the substrate-api-client has been financed by:

We also thank the teams at

Projects using substrate-api-client

If you intend to or are using substrate-api-client, please add your project here

In alphabetical order

More Repositories

1

smartmeter-datacollector

Smart Meter Data Collector
Python
64
star
2

uclinux

The uCLinux operating system adapted for the leanXcam
C
38
star
3

substraTEE-node-pre2.0.0

substrate node with TEE
Rust
19
star
4

intel_sgx_setup

Ansible scripts for setting up a development machine with Intel SGX
Shell
6
star
5

jumpstart-docs

SCS Jumpstart Documentation
C++
5
star
6

leanXcam

Documentation of the LeanXcam
5
star
7

app-template

A template for applications for the leanXcam.
C
4
star
8

substraTEE-client

SubstraTEE client connecting to SubstraTEE node
Rust
3
star
9

oscar

The Oscar Software Framework, used as a hardware abstraction layer for the leanXcam.
C
3
star
10

substraTEE-doc

documentation for substraTEE
2
star
11

ma-thesis-no-data-collection

Rust
2
star
12

toolchain-bin

The compiler toolchain used for the leanXcam
C
2
star
13

leanxsugus

The application that runs the Sugus-sorting-machine in the SCS museum.
C
2
star
14

leanXopencv

The popular open source vision library OpenCV adapted for leanXcam
C++
2
star
15

uclinux-bin

Binary packages of the uCLinux operating system on the camera
2
star
16

basic-template

Simple template to write your first leanXcam application.
C
1
star
17

web-view

Live image viewer for the browser.
JavaScript
1
star
18

osc-cc

OSCar Code Collection: A collection of useful code samples for the leanXcam.
C
1
star
19

leanXzebra

Simple application based on leanXstream that reads bar codes
C
1
star
20

tutorial

A tutorial for leanXcam software development with many small projects.
C
1
star
21

linux-2.6.x

The uCLinux kernel adapted for the leanXcam
C
1
star
22

rich-view

Rich-client for the leanXcam with live image view and configurability.
C
1
star
23

leanXstream

Simple video streaming from leanXcam
C
1
star
24

ux-view

Simple live image daemon and viewer tool for the leanXcam.
C
1
star
25

ba-thesis-oauth-tee

Rust
1
star
26

jumpstart-vm

SCS Jumpstart VM
Shell
1
star
27

u-boot

The boot loader sources of the leanXcam
C
1
star