1

I use wss://rpc-mainnet.maticvigil.com/v1/MY-APP-ID as my polygon dApp provider, however I cannot subscribe on newBlockHeaders event with the following error:

Error: The current provider doesn't support subscriptions: HDWalletProvider
    at Timeout._onTimeout (...\node_modules\web3-core-subscriptions\lib\subscription.js:174:24)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7) null

I've already seen this link, but it does not make sense for my case (since I already use websocket RPC provider).

1 Answer 1

1

There is an issue in HDWalletProvider in which you can attach a ws provider but it does not believe it allows event subscriptions. See 1.

Thanks to mikec on that issue page, the following workaround works:

const nodeIP = 'ws://192.168.1.xxx:xxxx';
const wsProvider = new Web3.providers.WebsocketProvider(nodeIP);
HDWalletProvider.prototype.on = wsProvider.on.bind(wsProvider);
let provider = new HDWalletProvider({privateKeys: [privateKey], providerOrUrl: wsProvider});
const web3 = new Web3(provider);

But, there is not yet any official solution.

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.