#chain #light #json-rpc #blockchain #download #requests #smoldot

no-std smoldot-light

Browser bindings to a light client for Substrate-based blockchains

16 breaking releases

new 0.17.0 Nov 8, 2024
0.16.2 Jun 17, 2024
0.16.1 May 30, 2024
0.15.0 Jan 24, 2024
0.2.0 Sep 7, 2022

#154 in Magic Beans

Download history 33842/week @ 2024-07-25 30111/week @ 2024-08-01 42553/week @ 2024-08-08 35851/week @ 2024-08-15 35800/week @ 2024-08-22 40220/week @ 2024-08-29 41396/week @ 2024-09-05 54812/week @ 2024-09-12 68777/week @ 2024-09-19 63453/week @ 2024-09-26 82195/week @ 2024-10-03 79940/week @ 2024-10-10 79554/week @ 2024-10-17 80160/week @ 2024-10-24 86728/week @ 2024-10-31 77206/week @ 2024-11-07

342,208 downloads per month
Used in 28 crates (2 directly)

GPL-3.0-or-later…

4.5MB
79K SLoC

Smoldot light client library.

This library provides an easy way to create a light client.

This light client is opinionated towards certain aspects: what it downloads, how much memory and CPU it is willing to consume, etc.

Usage

Initialization

In order to use the light client, call Client::new, passing an implementation of the platform::PlatformRef trait. See the documentation of the platform::PlatformRef trait for more information.

The Client contains two generic parameters:

  • An implementation of the platform::PlatformRef trait.
  • An opaque user data. If you do not use this, you can simply use ().

When the std feature of this library is enabled, the platform::DefaultPlatform struct can be used as an implementation of platform::PlatformRef.

For example:

use smoldot_light::{Client, platform::DefaultPlatform};
let client = Client::new(DefaultPlatform::new(env!("CARGO_PKG_NAME").into(), env!("CARGO_PKG_VERSION").into()));

If the std feature of this library is disabled, then you need to implement the platform::PlatformRef trait manually.

Adding a chain

After the client has been initialized, use Client::add_chain to ask the client to connect to said chain. See the documentation of AddChainConfig for information about what to provide.

Client::add_chain returns a ChainId, which identifies the chain within the Client. A Client can be thought of as a collection of chain connections, each identified by their ChainId, akin to a HashMap<ChainId, ...>.

A chain can be removed at any time using Client::remove_chain. This will cause the client to stop all connections and clean up its internal services. The ChainId is instantly considered as invalid as soon as the method is called.

JSON-RPC requests and responses

Once a chain has been added, one can send JSON-RPC requests using Client::json_rpc_request.

The request parameter of this function must be a JSON-RPC request in its text form. For example: {"id":53,"jsonrpc":"2.0","method":"system_name","params":[]}.

Calling Client::json_rpc_request queues the request in the internals of the client. Later, the client will process it.

Responses can be pulled by calling the AddChainSuccess::json_rpc_responses that is returned after a chain has been added.

Dependencies

~15–30MB
~509K SLoC