Skip to main content
Module

x/smoldot2/client.d.ts>Chain

Alternative client for Substrate-based chains.
Go to Latest
interface Chain
import { type Chain } from "https://deno.land/x/smoldot2@light-js-deno-v1.0.1/client.d.ts";

Active connection to a blockchain.

Methods

sendJsonRpc(rpc: string): void

Enqueues a JSON-RPC request that the client will process as soon as possible.

The response will be sent back using the callback passed when adding the chain.

See https://www.jsonrpc.org/specification for a specification of the JSON-RPC format. Only version 2 is supported. Be aware that some requests will cause notifications to be sent back using the same callback as the responses.

A MalformedJsonRpcError is thrown if the request isn't a valid JSON-RPC request (for example if it is not valid JSON) or if the request is unreasonably large (64 MiB at the time of writing of this comment). If, however, the request is a valid JSON-RPC request but that concerns an unknown method, or if for example some parameters are missing, an error response is properly generated and yielded through the JSON-RPC callback. In other words, a MalformedJsonRpcError is thrown in situations where something is so wrong with the request that it is not possible for smoldot to send back an error through the JSON-RPC callback.

Two JSON-RPC APIs are supported by smoldot:

nextJsonRpcResponse(): Promise<string>

Waits for a JSON-RPC response or notification to be generated.

Each chain contains a buffer of the responses waiting to be sent out. Calling this function pulls one element from the buffer. If this function is called at a slower rate than responses are generated, then the buffer will eventually become full, at which point calling Chain.sendJsonRpc will throw an exception.

If this function is called multiple times "simultaneously" (generating multiple different Promises), each Promise will return a different JSON-RPC response or notification. In that situation, there is no guarantee in the ordering in which the responses or notifications are yielded. Calling this function multiple times "simultaneously" is in general a niche corner case that you are encouraged to avoid.

remove(): void

Disconnects from the blockchain.

The JSON-RPC callback will no longer be called. This is the case immediately after this function is called. Any on-going JSON-RPC request is instantaneously aborted.

Trying to use the chain again will lead to an exception being thrown.

If this chain is a relay chain, then all parachains that use it will continue to work. Smoldot automatically keeps alive all relay chains that have an active parachains. There is no need to track parachains and relay chains, or to destroy them in the correct order, as this is handled automatically internally.