What the issue is, in broad strokes.
Add HDWalletProvider to drizzle store
Drizzle successful subscribed to events
truffle version): v5.0.43node --version): 10.17.0npm --version): 6.11.3Hm, sounds like this is a Drizzle issue, for https://github.com/trufflesuite/drizzle/issues
I am afraid that if I inherit from ProviderEngine this problem will be solved
Hi @MrRefactoring! I don't totally understand what you mean about ProviderEngine, can you clarify? Do you mean instead of accessing HDWalletProvider directly?
I think that HDWalletProvider should inherit ProviderEngine, and not write an adapter for it
I'm generally opposed to inheritance in favor of composition, but that seems like an implementation detail. Is there more information about what is causing this not to work? Thanks!
Hey @MrRefactoring or maybe @adrianmcli, could either of you lend any clarity to this issue? Thanks in advance!
@gnidan I'm busy at work so far, I can鈥檛 see this issue, sorry :(
The issue is that HDWalletProvider does not have an on() function, so web3 assumes no subscription support and throws an error. Web3.providers.WebsocketProvider implements .on() and supports subscription callbacks. HDWalletProvider uses WebsocketProvider when rpcUrl has ws: or wss:, but it's added via web3-provider-engine, which also doesn't support subscriptions by implementing on(). web3 looks for the on() function on the top level provider (HDWalletProvider) and can't find it.
Here's the monkey patched workaround I'm using:
const Web3 = require('web3')
const HDWalletProvider = require('@truffle/hdwallet-provider')
const rpcUrl = 'http://localhost:8545'
const mnemonic = 'monkey patch bad ...'
const wsProvider = new Web3.providers.WebsocketProvider(rpcUrl)
HDWalletProvider.prototype.on = wsProvider.on.bind(wsProvider)
const provider = new HDWalletProvider(mnemonic, wsProvider)
const web3 = new Web3(provider)
// subscribe callbacks should work now
web3.eth.subscribe('newBlockHeaders', (error, block) => console.log(error || block))
Most helpful comment
The issue is that HDWalletProvider does not have an
on()function, so web3 assumes no subscription support and throws an error.Web3.providers.WebsocketProviderimplements.on()and supports subscription callbacks. HDWalletProvider uses WebsocketProvider when rpcUrl hasws:orwss:, but it's added viaweb3-provider-engine, which also doesn't support subscriptions by implementingon(). web3 looks for theon()function on the top level provider (HDWalletProvider) and can't find it.Here's the monkey patched workaround I'm using: