2

I have been having series of errors and I have not been able to find a solution online. Please can anyone help me? I want to prompt users to connect wallet i.e when the connect button is clicked talisman, polkadotjs etc should pop up for confirmation.

This is the function:

async function handleConnect () {
  const provider = new WsProvider(WS_SECOND_ENDPOINT);
  const oneApi = await ApiPromise.create({ provider });
  const [chain, nodeName, nodeVersion] = await Promise.all([
    oneApi.rpc.system.chain(),
    oneApi.rpc.system.name(),
    oneApi.rpc.system.version()
  ]);
await oneApi.connect();
  console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);
}

When i remove await oneApi.connect(); I get the error below on the console:

caught (in promise) Error: WebSocket is already connected
    at WsProvider.connect (index.js:314:19)
    at RpcCore.connect (bundle.js:91:30)
    at ApiPromise.connect (index.js:29:30)
    at newTest (App.tsx:49:14)

Thanks

1 Answer 1

1

Try something like this:

import { ApiPromise, WsProvider } from "@polkadot/api";

export const connect = async () => {
    const provider = new WsProvider("wss://karura.api.onfinality.io/public-ws");
    const oneApi = new ApiPromise({ provider });
    await oneApi.isReadyOrError;

    const [chain, nodeName, nodeVersion] = await Promise.all([
        oneApi.rpc.system.chain(),
        oneApi.rpc.system.name(),
        oneApi.rpc.system.version()
    ]);

    console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);
    process.exit()
}

Output:

2023-04-23 09:54:28        REGISTRY: Unknown signed extensions SetEvmOrigin found, treating them as no-effect
2023-04-23 09:54:28        API/INIT: RPC methods not decorated: chainHead_unstable_body, chainHead_unstable_call, chainHead_unstable_follow, chainHead_unstable_genesisHash, chainHead_unstable_header, chainHead_unstable_stopBody, chainHead_unstable_stopCall, chainHead_unstable_stopStorage, chainHead_unstable_storage, chainHead_unstable_unfollow, chainHead_unstable_unpin, evm_blockLimits, evm_call, evm_estimateResources, oracle_getAllValues, oracle_getValue, tokens_queryExistentialDeposit, transaction_unstable_submitAndWatch, transaction_unstable_unwatch
You are connected to chain Karura using Acala Node v2.13.0-unknown

Also, you may want to look at this:

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.