Active connection to a blockchain.

interface Chain {
    addChain: AddChain;
    jsonRpcResponses: AsyncIterableIterator<string, any, any>;
    nextJsonRpcResponse: (() => Promise<string>);
    remove: (() => void);
    sendJsonRpc: ((rpc: string) => void);
}

Properties

addChain: AddChain

Connects to a parachain.

Throws an exception if the chain specification isn't valid, or if the chain specification concerns a parachain but no corresponding relay chain can be found.

Smoldot will automatically de-duplicate chains if multiple identical chains are added, in order to save resources. In other words, it is not a problem to call addChain multiple times with the same chain specifications and obtain multiple Chain. When the same client is used for multiple different purposes, you are in fact strongly encouraged to trust smoldot and not attempt to de-duplicate chains yourself, as determining whether two chains are identical is complicated and might have security implications.

Smoldot tries to distribute CPU resources equally between all active Chain objects.

Specification of the chain to add.

AddChainError If the chain can't be added.

CrashError If the background client has crashed.

jsonRpcResponses: AsyncIterableIterator<string, any, any>

JSON-RPC responses or notifications async iterable.

Each chain contains a buffer of the responses waiting to be sent out. Iterating over this pulls one element from the buffer. If the iteration happen 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. The size of this buffer can be configured through AddChainOptions.jsonRpcMaxPendingRequests.

JsonRpcDisabledError If the JSON-RPC system was disabled in the options of the chain.

CrashError If the background client has crashed.

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. The size of this buffer can be configured through AddChainOptions.jsonRpcMaxPendingRequests.

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.

AlreadyDestroyedError If the chain has been removed or the client has been terminated.

JsonRpcDisabledError If the JSON-RPC system was disabled in the options of the chain.

CrashError If the background client has crashed.

remove: (() => void)

Disconnects from the blockchain.

The JSON-RPC callback will no longer be called.

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 relaychains, or to destroy them in the correct order, as this is handled automatically.

AlreadyDestroyedError If the chain has already been removed.

CrashError If the background client has crashed.

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.

No response is generated if the request isn't a valid JSON-RPC request or if the request is unreasonably large (8 MiB at the time of writing of this comment). The request is then silently discarded. If, however, the request is a valid JSON-RPC request but that concerns an unknown method, a error response is properly generated.

Two JSON-RPC APIs are supported:

Type declaration

    • (rpc): void
    • Parameters

      • rpc: string

        JSON-encoded RPC request.

      Returns void

If the chain has been removed.

If no JSON-RPC callback was passed in the options of the chain.

If the background client has crashed.

MMNEPVFCICPMFPCPTTAAATR