|
| 1 | +## Smoldot provider |
| 2 | + |
| 3 | +We love light-clients, and smoldot is our favorite way to connect to networks! |
| 4 | + |
| 5 | +Smoldot can be instantiated from PAPI using both the main thread or a worker. We strongly recommend using workers for it. |
| 6 | + |
| 7 | +## Instantiation |
| 8 | + |
| 9 | +### Main thread |
| 10 | + |
| 11 | +This is the easiest way of them all of instantiating smoldot. It blocks the main thread and it might have some performance issues: |
| 12 | + |
| 13 | +```ts |
| 14 | +import { start } from "polkadot-api/smoldot" |
| 15 | + |
| 16 | +const smoldot = start() |
| 17 | +``` |
| 18 | + |
| 19 | +### WebWorker |
| 20 | + |
| 21 | +[WebWorkers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) are available in modern browser environments and [Bun](https://bun.sh). Having smoldot in a worker allows the main thread to be free to perform other tasks, since smoldot might block the main thread in some demanding tasks. |
| 22 | + |
| 23 | +Different bundlers have slightly different ways of creating workers, let's see them. |
| 24 | + |
| 25 | +- Vite: |
| 26 | + |
| 27 | +This option is only guaranteed to work on Vite, but might work on other bundlers. |
| 28 | + |
| 29 | +```ts |
| 30 | +import { startFromWorker } from "polkadot-api/smoldot/from-worker" |
| 31 | +import SmWorker from "polkadot-api/smoldot/worker?worker" |
| 32 | + |
| 33 | +const smoldot = startFromWorker(new SmWorker()) |
| 34 | +``` |
| 35 | + |
| 36 | +- Bun |
| 37 | + |
| 38 | +This option is safer than the previous one and could work in other bundlers as well. |
| 39 | + |
| 40 | +```ts |
| 41 | +import { startFromWorker } from "polkadot-api/smoldot/from-worker" |
| 42 | + |
| 43 | +const smWorker = new Worker(import.meta.resolve("polkadot-api/smoldot/worker")) |
| 44 | +const smoldot = startFromWorker(smWorker) |
| 45 | +``` |
| 46 | + |
| 47 | +- Webpack |
| 48 | + |
| 49 | +This option is the safest and should work in (almost) every bundler. |
| 50 | + |
| 51 | +```ts |
| 52 | +import { startFromWorker } from "polkadot-api/smoldot/from-worker" |
| 53 | + |
| 54 | +const smWorker = new Worker( |
| 55 | + new URL("polkadot-api/smoldot/worker", import.meta.url), |
| 56 | +) |
| 57 | +const smoldot = startFromWorker(smWorker) |
| 58 | +``` |
| 59 | + |
| 60 | +## Adding a chain |
| 61 | + |
| 62 | +Once we have an instance of smoldot, we need to tell smoldot to connect to the chain we want. For that, we need the `chainSpec` of the chain. With `polkadot-api` we bundle chainspecs for certain well-known chains. We try to keep all 5 relay chains (Polkadot, Kusama, Westend, Paseo, and Rococo) and its system chains. |
| 63 | + |
| 64 | +In order to add a solo-chain (or a relay chain), it is very simple: |
| 65 | + |
| 66 | +```ts |
| 67 | +import { chainSpec } from "polkadot-api/chains/polkadot" |
| 68 | + |
| 69 | +const polkadotChain: Promise<Chain> = smoldot.addChain({ chainSpec }) |
| 70 | +``` |
| 71 | + |
| 72 | +In case it is a parachain, we will need both the `chainSpec` of the relay chain, and the parachain one. It is simple as well: |
| 73 | + |
| 74 | +```ts |
| 75 | +import { polkadot, polkadot_asset_hub } from "polkadot-api/chains" |
| 76 | + |
| 77 | +// without async-await |
| 78 | +const assetHubChain: Promise<Chain> = smoldot |
| 79 | + .addChain({ chainSpec: polkadot }) |
| 80 | + .then((relayChain) => |
| 81 | + smoldot.addChain({ |
| 82 | + chainSpec: polkadot_asset_hub, |
| 83 | + potentialRelayChains: [relayChain], |
| 84 | + }), |
| 85 | + ) |
| 86 | + |
| 87 | +// or with an async-await structure: |
| 88 | +const assetHubChain: Promise<Chain> = smoldot.addChain({ |
| 89 | + chainSpec: polkadot_asset_hub, |
| 90 | + potentialRelayChains: [await smoldot.addChain({ chainSpec: polkadot })], |
| 91 | +}) |
| 92 | +``` |
| 93 | + |
| 94 | +## Getting the provider and initializing the client |
| 95 | + |
| 96 | +Once we have a `Chain` (or `Promise<Chain>`), we can initialize the provider and the client. |
| 97 | + |
| 98 | +```ts |
| 99 | +import { createClient } from "polkadot-api" |
| 100 | +import { getSmProvider } from "polkadot-api/sm-provider" |
| 101 | + |
| 102 | +const chain = smoldot.addChain({ chainSpec }) |
| 103 | +// no need to await! |
| 104 | +const provider = getSmProvider(chain) |
| 105 | + |
| 106 | +const client = createClient(provider) |
| 107 | +``` |
0 commit comments