4

I am trying to implement smoldot @substrate-connect light client to my DApp using chain specs of a substrate-based parachain. Substrate connect extension is able to discover the parachain as it shows the current block and keeps on syncing but I do not get apiPromise needed to make rpc request. The bootnodes I am using in my chainspecs is publically accessible as I can make RPC calls using curl but the issue is I cannot get apiPromise as suggested by substrate_connect npm package. Can anybody point me toward possible issues...

import chainSpecs from '../parachain.json';
import { ScProvider } from '@polkadot/rpc-provider/substrate-connect';
import { ApiPromise } from '@polkadot/api';
import { WellKnownChain } from '@substrate/connect';

const getPolkadotApi = async () => {
try {
    const relayProvider = new ScProvider(JSON.stringify(relayChainSpecs));
    await relayProvider.connect();

    const stringifyChainSpec = JSON.stringify(chainSpecs);
    const provider = new ScProvider(stringifyChainSpec, relayProvider);

    console.log('waiting for connection');
    await provider.connect();
    console.log('check, provider);
    const _api = await ApiPromise.create({ provider });
    console.log(`Connected to ${POLKADOT_RPC_URL},API is ${_api}`);
    return _api;
} catch (err) {
    console.log('this is the error', err);
    return null;
}
};
output:
waiting for connection
polkadot.js:42 checkkk ScProvider {...}

2 Answers 2

1

Your bootnode needs to listen on secure Websocket to catch with substrate connect:

  • Node needs to listen on WS instead of default http --listen-addr /ip4/0.0.0.0/tcp/30333/ws (do no use --port in this case)
  • Set a proxy with WS upgrade and SSL enabled, pointing a DNS
  • Specify the WSS address of the bootnode in chainspecs (example: "/dns/astar-02.wss-bootnode.astar.network/tcp/443/wss/p2p/12D3KooWMfHf9G1Mtawz4qySe1EqaBmrieidqn2xnEYckUYkpe52")
0

With your error message is not very clear what is happening.

I have followed this tutorial in the Substrate Docs about How to Connect to a custom chain specification and with a similar code as yours works fine.

You can check if there is something wrong with your parachain nodes or the chain spec. To do that, test the connection to a Common Good Parachain Statemint with the same code to see if it works.

Just change in your code your parachain.json with the Statemint spec chain and the relay chain connection to Polkadot:

const relayProvider = new ScProvider(WellKnownChain.polkadot);

You will see that the connections goes well with that message:

[smoldot] Parachain initialization complete for statemint. Name: "Statemint". Genesis hash: 0x68d5…de2f. State root hash: 0xc1ef26b567de07159e4ecd415fbbb0340c56a09c4d72c82516d0f3bc2b782c80. Network identity: 12D3KooWNicu1ZCX6ZNUC96B4TQSiet2NkoQc7SfitxWWE4fQgpK. Relay chain: polkadot (id: 1000)

And add this code if you want to subscribe to the new blocks and make sure you are getting the info from the chain:

await _api.rpc.chain.subscribeNewHeads((lastHeader: { number: unknown; hash: unknown }) => {
    console.log(`New block #${lastHeader.number} has hash ${lastHeader.hash}`);
  });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.