1

I'm running the substrate-contracts-node 0.40.0-17065f31ac4 with the command

./artifacts/substrate-contracts-node-linux/substrate-contracts-node --log info,runtime::contracts=debug

, and am trying to connect to it over websocket from a js/ts script using polkadotjs api:

const wsProvider = new WsProvider('ws://127.0.0.1:9944');
const api = await ApiPromise.create({ provider: wsProvider });

But I get this error:

node:internal/event_target:1096
  process.nextTick(() => { throw err; });
                           ^

TypeError: Cannot read properties of null (reading 'length')
    at WebSocket.__internal__onSocketMessage (file:///home/dotdev/gits/accounts_setup/node_modules/@polkadot/rpc-provider/ws/index.js:393:40)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:822:20)
    at WebSocket.dispatchEvent (node:internal/event_target:757:26)
    at fireEvent (node:internal/deps/undici/undici:10978:14)
    at websocketMessageReceived (node:internal/deps/undici/undici:11000:7)

The length property is read in polkadotjs at this line:

        const bytesRecv = message.data.length;

When I'm connection with polkadotjs ui to my local node, it works fine though (I checked it's using the same endpoint as my script).

The contracts IU at https://ui.use.ink/?rpc=ws://127.0.0.1:9944 also doesn't complain.

The complete script is in the file index.ts:

import { ApiPromise, WsProvider, Keyring } from '@polkadot/api';
const wsProvider = new WsProvider('ws://127.0.0.1:9944');
const api = await ApiPromise.create({ provider: wsProvider });
console.log(api.genesisHash.toHex());
await api.disconnect()

And I run it with esrun index.ts.

What am I doing wrong?

1 Answer 1

1

This was due to this project using node 22. Switching back to node 21.7.3 made it work fine.

Note that changing the WsProvider to a public node like rpc.polkadot.io actually gave the same problems. The problem was unrelated to substrate-contracts-node, and only due to the node version used.

Your Answer

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

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