The primary implementation of the light-client provider for any Substrate-based chain.
@substrate/connect
for library authorsThe connect
package searches for a light client provider via the discovery protocol. If none is found, it will initiate a smoldot instance in the user's browser tab.
To connect to a well-known chain ('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":[]}',
)
To connect to a custom Substrate chain using its 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":[]}',
)
To connect to a parachain, first instantiate the relay chain it is connected to, then instantiate the parachain on the same relay chain. The following example connects to a parachain on 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":[]}',
)
yarn add @polkadot/rpc-provider
yarn add @polkadot/api
import { ScProvider } from "@polkadot/rpc-provider/substrate-connect"
import * as Sc from "@substrate/connect"
import { ApiPromise } from "@polkadot/api"
// Connect to polkadot relay chain
const provider = new ScProvider(Sc, Sc.WellKnownChain.polkadot)
await provider.connect()
const api = await ApiPromise.create({ provider })
// Connect to parachain
const provider2 = new ScProvider(Sc, Sc.WellKnownChain.people, provider)
await provider2.connect()
const api2 = await ApiPromise.create({ provider })
pnpm test
to run the unit testspnpm build
to build @substrate-connectpnpm lint
to run linter for @substrate-connect
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