@substrate/connect
for library authorsProvide a well-known chain name ('polkadot', 'ksmcc3', 'westend2', 'rococo_v2_2'):
import { createScClient, WellKnownChain } from '@substrate/connect';
const scClient = createScClient();
const chain = await scClient.addWellKnownChain(
WellKnownChain.westend2,
function jsonRpcCallback(response) {
console.log("response", response);
}
);
chain.sendJsonRpc(
'{"jsonrpc":"2.0","id":"1","method":"system_health","params":[]}'
);
...or provide your custom substrate chain's name and chainspec:
import { createScClient } from '@substrate/connect';
import myJsonChainSpec from './mySubstrateChainSpec.json';
const myChainSpec = JSON.stringify(myJsonChainSpec);
const scClient = createScClient();
const chain = await scClient.addChain(
myChainSpec,
function jsonRpcCallback(response) {
console.log("response", response);
}
);
chain.sendJsonRpc(
'{"jsonrpc":"2.0","id":"1","method":"system_health","params":[]}'
);
In order to connect to a parachain, you must first instantiate the relay chain this parachain is connected to, then instantiate the parachain on the same relay chain. The following example connects to a parachain of the Westend test network:
import { createScClient, WellKnownChain } from '@substrate/connect';
import jsonParachainSpec from './myParaChainSpec.json';
const parachainSpec = JSON.stringify(jsonParachainSpec);
const scClient = createScClient();
const relayChain = await scClient.addWellKnownChain(WellKnownChain.westend2)
const parachain = await relayChain.addChain(
parachainSpec,
function jsonRpcCallback(response) {
console.log("response", response);
}
);
parachain.sendJsonRpc(
'{"jsonrpc":"2.0","id":"1","method":"system_health","params":[]}'
);
pnpm test
to run the unit testspnpm build
to build @substrate-connectpnpm lint
to run linter for @substrate-connectGenerated using TypeDoc
The substrate-connect package makes it possible to connect to Substrate-compatible blockchains with a light client.
Connecting to a chain is done in two steps:
Note that this library is a low-level library where you directly send JSON-RPC requests and receive responses.
Adding parachains
Connecting to a parachain is done by obtaining a relay chain instance and then calling addChain.
While this will not work, and an exception will be thrown when adding the parachain:
Resources sharing
While calling createScClient multiple times leads to a different observable behaviour when it comes to parachains (see previous section), internally resources are shared between all the clients.
In order words, it is not a problem to do this:
From an API perspective,
chain1
andchain2
should be treated as two completely separate connections to the same chain. Internally, however, only one "actual" connection to that chain will exist.This means that there is no problem in calling createScClient from within a library for example.
Well-known chains
This package contains a list of so-called WellKnownChains. This is a list of popular chains that users are likely to connect to. Instead of calling
addChain
with a chain specification, one can calladdWellKnownChain
, passing only the name of a well-known chain as parameter.Using WellKnownChains doesn't provide any benefit when the substrate-connect extension is not installed.
If, however, the substrate-connect extension is installed, using addWellKnownChain has several benefits:
Usage with a worker
By default, when the substrate-connect extension is not installed, createScClient will run the smoldot light client entirely in the current thread. This can cause performance issues if other CPU-heavy operations are done in that thread.
In order to help with this, it possible to run the smoldot light client in a worker. To do so, you must provide a workerFactory to createScClient and setup the worker to import
@substrate/connect/worker
.For example