0

I need to access two smart contracts through two different providers in the same app, is it possible to have two instances of Web3 in the same dApp. Something like this:

const web3 = new Web3();
const web4 = new Web3();
...
web3.setProvider(myHttpProvider);
web4.setProvider(myWSSProvider);

I actually have this code inside my dApp and it is working, but the problem is that I set my account credentials for the first instance only (through HDWalletProvider), but the second one also uses the same account; so I'm not sure what is happening behind the scene!

1

Yes, it's possible to have several instances of web3 inside your dApp, but it only makes sense if you want to sign transactions or read data on multiple chains. I don't see any reason why you would want to include multiple providers which are pointing to the same chain.

2
  • I use two providers which both have a limit on per second requests, to ensure that all my transactions are being sent, I tend to use a separate provider that only sends transactions, while the other one is used for non-transactional requests. Is this approach going to work correctly? Jul 12 at 6:44
  • 1
    I don't see problem with this. In general I believe people will be reading more than sending transactions, so if one of the two providers has higher limits then the higher limit provider should be used for the reading part. Jul 12 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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